1 //===-- llvm/Module.h - C++ class to represent a VM module -------*- C++ -*--=//
3 // This file contains the declarations for the Module class that is used to
4 // maintain all the information related to a VM module.
6 // A module also maintains a GlobalValRefMap object that is used to hold all
7 // constant references to global variables in the module. When a global
8 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
10 //===----------------------------------------------------------------------===//
15 #include "llvm/Value.h"
16 #include "llvm/ValueHolder.h"
18 class GlobalValueRefMap; // Used by ConstantVals.cpp
19 class ConstantPointerRef;
23 class Module : public Annotable {
25 typedef ValueHolder<GlobalVariable, Module, Module> GlobalListType;
26 typedef ValueHolder<Function, Module, Module> FunctionListType;
28 // Global Variable iterators...
29 typedef GlobalListType::iterator giterator;
30 typedef GlobalListType::const_iterator const_giterator;
31 typedef std::reverse_iterator<giterator> reverse_giterator;
32 typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
34 // Function iterators...
35 typedef FunctionListType::iterator iterator;
36 typedef FunctionListType::const_iterator const_iterator;
37 typedef std::reverse_iterator<iterator> reverse_iterator;
38 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
41 GlobalListType GlobalList; // The Global Variables
42 FunctionListType FunctionList; // The Functions
44 GlobalValueRefMap *GVRefMap;
48 // Accessor for the underlying GlobalValRefMap... only through the
49 // ConstantPointerRef class...
50 friend class ConstantPointerRef;
51 void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
52 ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
58 // getOrInsertFunction - Look up the specified function in the module symbol
59 // table. If it does not exist, add a prototype for the function and return
61 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
63 // getFunction - Look up the specified function in the module symbol table.
64 // If it does not exist, return null.
66 Function *getFunction(const std::string &Name, const FunctionType *Ty);
68 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
69 // there is already an entry for this name, true is returned and the symbol
70 // table is not modified.
72 bool addTypeName(const std::string &Name, const Type *Ty);
74 // getTypeName - If there is at least one entry in the symbol table for the
75 // specified type, return it.
77 std::string getTypeName(const Type *Ty);
79 // Get the underlying elements of the Module...
80 inline const GlobalListType &getGlobalList() const { return GlobalList; }
81 inline GlobalListType &getGlobalList() { return GlobalList; }
82 inline const FunctionListType &getFunctionList() const { return FunctionList;}
83 inline FunctionListType &getFunctionList() { return FunctionList;}
86 //===--------------------------------------------------------------------===//
87 // Symbol table support functions...
89 // hasSymbolTable() - Returns true if there is a symbol table allocated to
90 // this object AND if there is at least one name in it!
92 bool hasSymbolTable() const;
94 // CAUTION: The current symbol table may be null if there are no names (ie,
95 // the symbol table is empty)
97 inline SymbolTable *getSymbolTable() { return SymTab; }
98 inline const SymbolTable *getSymbolTable() const { return SymTab; }
100 // getSymbolTableSure is guaranteed to not return a null pointer, because if
101 // the method does not already have a symtab, one is created. Use this if
102 // you intend to put something into the symbol table for the method.
104 SymbolTable *getSymbolTableSure();
107 //===--------------------------------------------------------------------===//
108 // Module iterator forwarding functions
110 inline giterator gbegin() { return GlobalList.begin(); }
111 inline const_giterator gbegin() const { return GlobalList.begin(); }
112 inline giterator gend () { return GlobalList.end(); }
113 inline const_giterator gend () const { return GlobalList.end(); }
115 inline reverse_giterator grbegin() { return GlobalList.rbegin(); }
116 inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
117 inline reverse_giterator grend () { return GlobalList.rend(); }
118 inline const_reverse_giterator grend () const { return GlobalList.rend(); }
120 inline unsigned gsize() const { return GlobalList.size(); }
121 inline bool gempty() const { return GlobalList.empty(); }
122 inline const GlobalVariable *gfront() const { return GlobalList.front(); }
123 inline GlobalVariable *gfront() { return GlobalList.front(); }
124 inline const GlobalVariable *gback() const { return GlobalList.back(); }
125 inline GlobalVariable *gback() { return GlobalList.back(); }
129 inline iterator begin() { return FunctionList.begin(); }
130 inline const_iterator begin() const { return FunctionList.begin(); }
131 inline iterator end () { return FunctionList.end(); }
132 inline const_iterator end () const { return FunctionList.end(); }
134 inline reverse_iterator rbegin() { return FunctionList.rbegin(); }
135 inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
136 inline reverse_iterator rend () { return FunctionList.rend(); }
137 inline const_reverse_iterator rend () const { return FunctionList.rend(); }
139 inline unsigned size() const { return FunctionList.size(); }
140 inline bool empty() const { return FunctionList.empty(); }
141 inline const Function *front() const { return FunctionList.front(); }
142 inline Function *front() { return FunctionList.front(); }
143 inline const Function *back() const { return FunctionList.back(); }
144 inline Function *back() { return FunctionList.back(); }
146 void print(std::ostream &OS) const;
148 // dropAllReferences() - This function causes all the subinstructions to "let
149 // go" of all references that they are maintaining. This allows one to
150 // 'delete' a whole class at a time, even though there may be circular
151 // references... first all references are dropped, and all use counts go to
152 // zero. Then everything is delete'd for real. Note that no operations are
153 // valid on an object that has "dropped all references", except operator
156 void dropAllReferences();
159 inline std::ostream &operator<<(std::ostream &O, const Module *M) {