Convert comments to Doxygen style
[oota-llvm.git] / include / llvm / Module.h
1 //===-- llvm/Module.h - C++ class to represent a VM module -------*- C++ -*--=//
2 //
3 // This file contains the declarations for the Module class that is used to 
4 // maintain all the information related to a VM module.
5 //
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.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_MODULE_H
13 #define LLVM_MODULE_H
14
15 #include "llvm/Function.h"
16 #include "llvm/GlobalVariable.h"
17 class GlobalVariable;
18 class GlobalValueRefMap;   // Used by ConstantVals.cpp
19 class ConstantPointerRef;
20 class FunctionType;
21 class SymbolTable;
22
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);
28 };
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);
34 };
35
36 class Module : public Annotable {
37 public:
38   typedef iplist<GlobalVariable> GlobalListType;
39   typedef iplist<Function> FunctionListType;
40
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;
46
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;
52
53 private:
54   GlobalListType GlobalList;     // The Global Variables
55   FunctionListType FunctionList;     // The Functions
56
57   GlobalValueRefMap *GVRefMap;
58
59   SymbolTable *SymTab;
60
61   // Accessor for the underlying GlobalValRefMap... only through the
62   // ConstantPointerRef class...
63   friend class ConstantPointerRef;
64   void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
65   ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
66   void destroyConstantPointerRef(ConstantPointerRef *CPR);
67
68 public:
69   Module();
70   ~Module();
71
72   /// getOrInsertFunction - Look up the specified function in the module symbol
73   /// table.  If it does not exist, add a prototype for the function and return
74   /// it.
75   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
76
77   /// getFunction - Look up the specified function in the module symbol table.
78   /// If it does not exist, return null.
79   ///
80   Function *getFunction(const std::string &Name, const FunctionType *Ty);
81
82   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
83   /// there is already an entry for this name, true is returned and the symbol
84   /// table is not modified.
85   ///
86   bool addTypeName(const std::string &Name, const Type *Ty);
87
88   /// getTypeName - If there is at least one entry in the symbol table for the
89   /// specified type, return it.
90   ///
91   std::string getTypeName(const Type *Ty);
92
93   /// Get the underlying elements of the Module...
94   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
95   inline       GlobalListType &getGlobalList()        { return GlobalList; }
96   inline const FunctionListType &getFunctionList() const { return FunctionList;}
97   inline       FunctionListType &getFunctionList()       { return FunctionList;}
98
99
100   //===--------------------------------------------------------------------===//
101   // Symbol table support functions...
102   
103   /// hasSymbolTable() - Returns true if there is a symbol table allocated to
104   /// this object AND if there is at least one name in it!
105   ///
106   bool hasSymbolTable() const;
107
108   /// getSymbolTable() - CAUTION: The current symbol table may be null if there
109   /// are no names (ie, the symbol table is empty)
110   ///
111   inline       SymbolTable *getSymbolTable()       { return SymTab; }
112   inline const SymbolTable *getSymbolTable() const { return SymTab; }
113   
114   /// getSymbolTableSure is guaranteed to not return a null pointer, because if
115   /// the method does not already have a symtab, one is created.  Use this if
116   /// you intend to put something into the symbol table for the method.
117   ///
118   SymbolTable *getSymbolTableSure();
119
120
121   //===--------------------------------------------------------------------===//
122   // Module iterator forwarding functions
123   //
124   inline giterator                gbegin()       { return GlobalList.begin(); }
125   inline const_giterator          gbegin() const { return GlobalList.begin(); }
126   inline giterator                gend  ()       { return GlobalList.end();   }
127   inline const_giterator          gend  () const { return GlobalList.end();   }
128
129   inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
130   inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
131   inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
132   inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
133
134   inline unsigned                  gsize() const { return GlobalList.size(); }
135   inline bool                     gempty() const { return GlobalList.empty(); }
136   inline const GlobalVariable    &gfront() const { return GlobalList.front(); }
137   inline       GlobalVariable    &gfront()       { return GlobalList.front(); }
138   inline const GlobalVariable     &gback() const { return GlobalList.back(); }
139   inline       GlobalVariable     &gback()       { return GlobalList.back(); }
140
141
142
143   inline iterator                begin()       { return FunctionList.begin(); }
144   inline const_iterator          begin() const { return FunctionList.begin(); }
145   inline iterator                end  ()       { return FunctionList.end();   }
146   inline const_iterator          end  () const { return FunctionList.end();   }
147
148   inline reverse_iterator       rbegin()       { return FunctionList.rbegin(); }
149   inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
150   inline reverse_iterator       rend  ()       { return FunctionList.rend();   }
151   inline const_reverse_iterator rend  () const { return FunctionList.rend();   }
152
153   inline unsigned                 size() const { return FunctionList.size(); }
154   inline bool                    empty() const { return FunctionList.empty(); }
155   inline const Function         &front() const { return FunctionList.front(); }
156   inline       Function         &front()       { return FunctionList.front(); }
157   inline const Function          &back() const { return FunctionList.back(); }
158   inline       Function          &back()       { return FunctionList.back(); }
159
160   void print(std::ostream &OS) const;
161   void dump() const;
162
163   /// dropAllReferences() - This function causes all the subinstructions to "let
164   /// go" of all references that they are maintaining.  This allows one to
165   /// 'delete' a whole class at a time, even though there may be circular
166   /// references... first all references are dropped, and all use counts go to
167   /// zero.  Then everything is delete'd for real.  Note that no operations are
168   /// valid on an object that has "dropped all references", except operator 
169   /// delete.
170   ///
171   void dropAllReferences();
172 };
173
174 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
175   M->print(O);
176   return O;
177 }
178
179 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
180   M.print(O);
181   return O;
182 }
183
184 #endif