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/SymTabValue.h"
17 #include "llvm/ValueHolder.h"
19 class GlobalValueRefMap; // Used by ConstantVals.cpp
20 class ConstantPointerRef;
23 class Module : public Value, public SymTabValue {
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;
46 // Accessor for the underlying GlobalValRefMap... only through the
47 // ConstantPointerRef class...
48 friend class ConstantPointerRef;
49 void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
50 ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
56 // getOrInsertFunction - Look up the specified function in the module symbol
57 // table. If it does not exist, add a prototype for the function and return
59 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
61 // getFunction - Look up the specified function in the module symbol table.
62 // If it does not exist, return null.
64 Function *getFunction(const std::string &Name, const FunctionType *Ty);
66 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
67 // there is already an entry for this name, true is returned and the symbol
68 // table is not modified.
70 bool addTypeName(const std::string &Name, const Type *Ty);
72 // getTypeName - If there is at least one entry in the symbol table for the
73 // specified type, return it.
75 std::string getTypeName(const Type *Ty);
77 // Get the underlying elements of the Module...
78 inline const GlobalListType &getGlobalList() const { return GlobalList; }
79 inline GlobalListType &getGlobalList() { return GlobalList; }
80 inline const FunctionListType &getFunctionList() const { return FunctionList;}
81 inline FunctionListType &getFunctionList() { return FunctionList;}
83 //===--------------------------------------------------------------------===//
84 // Module iterator forwarding functions
86 inline giterator gbegin() { return GlobalList.begin(); }
87 inline const_giterator gbegin() const { return GlobalList.begin(); }
88 inline giterator gend () { return GlobalList.end(); }
89 inline const_giterator gend () const { return GlobalList.end(); }
91 inline reverse_giterator grbegin() { return GlobalList.rbegin(); }
92 inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
93 inline reverse_giterator grend () { return GlobalList.rend(); }
94 inline const_reverse_giterator grend () const { return GlobalList.rend(); }
96 inline unsigned gsize() const { return GlobalList.size(); }
97 inline bool gempty() const { return GlobalList.empty(); }
98 inline const GlobalVariable *gfront() const { return GlobalList.front(); }
99 inline GlobalVariable *gfront() { return GlobalList.front(); }
100 inline const GlobalVariable *gback() const { return GlobalList.back(); }
101 inline GlobalVariable *gback() { return GlobalList.back(); }
105 inline iterator begin() { return FunctionList.begin(); }
106 inline const_iterator begin() const { return FunctionList.begin(); }
107 inline iterator end () { return FunctionList.end(); }
108 inline const_iterator end () const { return FunctionList.end(); }
110 inline reverse_iterator rbegin() { return FunctionList.rbegin(); }
111 inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
112 inline reverse_iterator rend () { return FunctionList.rend(); }
113 inline const_reverse_iterator rend () const { return FunctionList.rend(); }
115 inline unsigned size() const { return FunctionList.size(); }
116 inline bool empty() const { return FunctionList.empty(); }
117 inline const Function *front() const { return FunctionList.front(); }
118 inline Function *front() { return FunctionList.front(); }
119 inline const Function *back() const { return FunctionList.back(); }
120 inline Function *back() { return FunctionList.back(); }
122 // Methods for support type inquiry through isa, cast, and dyn_cast:
123 static inline bool classof(const Module *T) { return true; }
124 static inline bool classof(const Value *V) {
125 return V->getValueType() == Value::ModuleVal;
128 virtual void print(std::ostream &OS) const;
130 // dropAllReferences() - This function causes all the subinstructions to "let
131 // go" of all references that they are maintaining. This allows one to
132 // 'delete' a whole class at a time, even though there may be circular
133 // references... first all references are dropped, and all use counts go to
134 // zero. Then everything is delete'd for real. Note that no operations are
135 // valid on an object that has "dropped all references", except operator
138 void dropAllReferences();