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/Function.h"
16 #include "llvm/GlobalVariable.h"
18 class GlobalValueRefMap; // Used by ConstantVals.cpp
19 class ConstantPointerRef;
23 template<> struct ilist_traits<Function>
24 : public SymbolTableListTraits<Function, Module, Module> {
25 // createNode is used to create a node that marks the end of the list...
26 static Function *createNode();
27 static iplist<Function> &getList(Module *M);
29 template<> struct ilist_traits<GlobalVariable>
30 : public SymbolTableListTraits<GlobalVariable, Module, Module> {
31 // createNode is used to create a node that marks the end of the list...
32 static GlobalVariable *createNode();
33 static iplist<GlobalVariable> &getList(Module *M);
36 class Module : public Annotable {
38 typedef iplist<GlobalVariable> GlobalListType;
39 typedef iplist<Function> FunctionListType;
41 // Global Variable iterators...
42 typedef GlobalListType::iterator giterator;
43 typedef GlobalListType::const_iterator const_giterator;
44 typedef std::reverse_iterator<giterator> reverse_giterator;
45 typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
47 // Function iterators...
48 typedef FunctionListType::iterator iterator;
49 typedef FunctionListType::const_iterator const_iterator;
50 typedef std::reverse_iterator<iterator> reverse_iterator;
51 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
54 GlobalListType GlobalList; // The Global Variables
55 FunctionListType FunctionList; // The Functions
57 GlobalValueRefMap *GVRefMap;
61 // Accessor for the underlying GlobalValRefMap... only through the
63 friend class Constant;
64 friend class ConstantPointerRef;
65 void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
66 ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
67 void destroyConstantPointerRef(ConstantPointerRef *CPR);
73 /// getOrInsertFunction - Look up the specified function in the module symbol
74 /// table. If it does not exist, add a prototype for the function and return
76 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
78 /// getFunction - Look up the specified function in the module symbol table.
79 /// If it does not exist, return null.
81 Function *getFunction(const std::string &Name, const FunctionType *Ty);
83 /// getMainFunction - This function looks up main efficiently. This is such a
84 /// common case, that it is a method in Module. If main cannot be found, a
85 /// null pointer is returned.
87 Function *getMainFunction();
89 /// getNamedFunction - Return the first function in the module with the
90 /// specified name, of arbitrary type. This method returns null if a function
91 /// with the specified name is not found.
93 Function *getNamedFunction(const std::string &Name);
95 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
96 /// there is already an entry for this name, true is returned and the symbol
97 /// table is not modified.
99 bool addTypeName(const std::string &Name, const Type *Ty);
101 /// getTypeName - If there is at least one entry in the symbol table for the
102 /// specified type, return it.
104 std::string getTypeName(const Type *Ty);
106 /// Get the underlying elements of the Module...
107 inline const GlobalListType &getGlobalList() const { return GlobalList; }
108 inline GlobalListType &getGlobalList() { return GlobalList; }
109 inline const FunctionListType &getFunctionList() const { return FunctionList;}
110 inline FunctionListType &getFunctionList() { return FunctionList;}
113 //===--------------------------------------------------------------------===//
114 // Symbol table support functions...
116 /// hasSymbolTable() - Returns true if there is a symbol table allocated to
117 /// this object AND if there is at least one name in it!
119 bool hasSymbolTable() const;
121 /// getSymbolTable() - CAUTION: The current symbol table may be null if there
122 /// are no names (ie, the symbol table is empty)
124 inline SymbolTable *getSymbolTable() { return SymTab; }
125 inline const SymbolTable *getSymbolTable() const { return SymTab; }
127 /// getSymbolTableSure is guaranteed to not return a null pointer, because if
128 /// the method does not already have a symtab, one is created. Use this if
129 /// you intend to put something into the symbol table for the method.
131 SymbolTable *getSymbolTableSure();
134 //===--------------------------------------------------------------------===//
135 // Module iterator forwarding functions
137 inline giterator gbegin() { return GlobalList.begin(); }
138 inline const_giterator gbegin() const { return GlobalList.begin(); }
139 inline giterator gend () { return GlobalList.end(); }
140 inline const_giterator gend () const { return GlobalList.end(); }
142 inline reverse_giterator grbegin() { return GlobalList.rbegin(); }
143 inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
144 inline reverse_giterator grend () { return GlobalList.rend(); }
145 inline const_reverse_giterator grend () const { return GlobalList.rend(); }
147 inline unsigned gsize() const { return GlobalList.size(); }
148 inline bool gempty() const { return GlobalList.empty(); }
149 inline const GlobalVariable &gfront() const { return GlobalList.front(); }
150 inline GlobalVariable &gfront() { return GlobalList.front(); }
151 inline const GlobalVariable &gback() const { return GlobalList.back(); }
152 inline GlobalVariable &gback() { return GlobalList.back(); }
156 inline iterator begin() { return FunctionList.begin(); }
157 inline const_iterator begin() const { return FunctionList.begin(); }
158 inline iterator end () { return FunctionList.end(); }
159 inline const_iterator end () const { return FunctionList.end(); }
161 inline reverse_iterator rbegin() { return FunctionList.rbegin(); }
162 inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
163 inline reverse_iterator rend () { return FunctionList.rend(); }
164 inline const_reverse_iterator rend () const { return FunctionList.rend(); }
166 inline unsigned size() const { return FunctionList.size(); }
167 inline bool empty() const { return FunctionList.empty(); }
168 inline const Function &front() const { return FunctionList.front(); }
169 inline Function &front() { return FunctionList.front(); }
170 inline const Function &back() const { return FunctionList.back(); }
171 inline Function &back() { return FunctionList.back(); }
173 void print(std::ostream &OS) const;
176 /// dropAllReferences() - This function causes all the subinstructions to "let
177 /// go" of all references that they are maintaining. This allows one to
178 /// 'delete' a whole class at a time, even though there may be circular
179 /// references... first all references are dropped, and all use counts go to
180 /// zero. Then everything is delete'd for real. Note that no operations are
181 /// valid on an object that has "dropped all references", except operator
184 void dropAllReferences();
187 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
192 inline std::ostream &operator<<(std::ostream &O, const Module &M) {