Include an operator<<, to print modules
[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/Value.h"
16 #include "llvm/ValueHolder.h"
17 class GlobalVariable;
18 class GlobalValueRefMap;   // Used by ConstantVals.cpp
19 class ConstantPointerRef;
20 class FunctionType;
21 class SymbolTable;
22
23 class Module : public Annotable {
24 public:
25   typedef ValueHolder<GlobalVariable, Module, Module> GlobalListType;
26   typedef ValueHolder<Function, Module, Module> FunctionListType;
27
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;
33
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;
39
40 private:
41   GlobalListType GlobalList;     // The Global Variables
42   FunctionListType FunctionList;     // The Functions
43
44   GlobalValueRefMap *GVRefMap;
45
46   SymbolTable *SymTab;
47
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);
53
54 public:
55   Module();
56   ~Module();
57
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
60   // it.
61   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
62
63   // getFunction - Look up the specified function in the module symbol table.
64   // If it does not exist, return null.
65   //
66   Function *getFunction(const std::string &Name, const FunctionType *Ty);
67
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.
71   //
72   bool addTypeName(const std::string &Name, const Type *Ty);
73
74   // getTypeName - If there is at least one entry in the symbol table for the
75   // specified type, return it.
76   //
77   std::string getTypeName(const Type *Ty);
78
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;}
84
85
86   //===--------------------------------------------------------------------===//
87   // Symbol table support functions...
88   
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!
91   //
92   bool hasSymbolTable() const;
93
94   // CAUTION: The current symbol table may be null if there are no names (ie, 
95   // the symbol table is empty) 
96   //
97   inline       SymbolTable *getSymbolTable()       { return SymTab; }
98   inline const SymbolTable *getSymbolTable() const { return SymTab; }
99
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.
103   //
104   SymbolTable *getSymbolTableSure();
105
106
107   //===--------------------------------------------------------------------===//
108   // Module iterator forwarding functions
109   //
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();   }
114
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();   }
119
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(); }
126
127
128
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();   }
133
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();   }
138
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(); }
145
146   void print(std::ostream &OS) const;
147
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 
154   // delete.
155   //
156   void dropAllReferences();
157 };
158
159 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
160   M->print(O);
161   return O;
162 }
163
164 #endif