bdd72e1c22293e9015e76559b13b4636b4306949
[oota-llvm.git] / include / llvm / Module.h
1 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations for the Module class that is used to 
11 // maintain all the information related to a VM module.
12 //
13 // A module also maintains a GlobalValRefMap object that is used to hold all
14 // constant references to global variables in the module.  When a global
15 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_MODULE_H
20 #define LLVM_MODULE_H
21
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24
25 namespace llvm {
26
27 class GlobalVariable;
28 class GlobalValueRefMap;   // Used by ConstantVals.cpp
29 class ConstantPointerRef;
30 class FunctionType;
31 class SymbolTable;
32
33 template<> struct ilist_traits<Function>
34   : public SymbolTableListTraits<Function, Module, Module> {
35   // createNode is used to create a node that marks the end of the list...
36   static Function *createNode();
37   static iplist<Function> &getList(Module *M);
38 };
39 template<> struct ilist_traits<GlobalVariable>
40   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
41   // createNode is used to create a node that marks the end of the list...
42   static GlobalVariable *createNode();
43   static iplist<GlobalVariable> &getList(Module *M);
44 };
45
46 struct Module : public Annotable {
47   typedef iplist<GlobalVariable> GlobalListType;
48   typedef iplist<Function> FunctionListType;
49
50   // Global Variable iterators...
51   typedef GlobalListType::iterator                             giterator;
52   typedef GlobalListType::const_iterator                 const_giterator;
53   typedef std::reverse_iterator<giterator>             reverse_giterator;
54   typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
55
56   // Function iterators...
57   typedef FunctionListType::iterator                          iterator;
58   typedef FunctionListType::const_iterator              const_iterator;
59   typedef std::reverse_iterator<iterator>             reverse_iterator;
60   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
61
62   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
63   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
64
65 private:
66   GlobalListType GlobalList;     // The Global Variables in the module
67   FunctionListType FunctionList; // The Functions in the module
68   GlobalValueRefMap *GVRefMap;   // Keep track of GlobalValueRef's
69   SymbolTable *SymTab;           // Symbol Table for the module
70   std::string ModuleID;    // Human readable identifier for the module
71
72   // These flags are probably not the right long-term way to handle this kind of
73   // target information, but it is sufficient for now.
74   Endianness  Endian;       // True if target is little endian
75   PointerSize PtrSize;    // True if target has 32-bit pointers (false = 64-bit)
76
77   // Accessor for the underlying GVRefMap... only through the Constant class...
78   friend class Constant;
79   friend class ConstantPointerRef;
80   void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
81   ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
82   void destroyConstantPointerRef(ConstantPointerRef *CPR);
83
84 public:
85   Module(const std::string &ModuleID);
86   ~Module();
87
88   const std::string &getModuleIdentifier() const { return ModuleID; }
89
90   /// Target endian information...
91   Endianness getEndianness() const { return Endian; }
92   void setEndianness(Endianness E) { Endian = E; }
93
94   /// Target Pointer Size information...
95   PointerSize getPointerSize() const { return PtrSize; }
96   void setPointerSize(PointerSize PS) { PtrSize = PS; }
97
98   //===--------------------------------------------------------------------===//
99   // Methods for easy access to the functions in the module.
100   //
101
102   /// getOrInsertFunction - Look up the specified function in the module symbol
103   /// table.  If it does not exist, add a prototype for the function and return
104   /// it.
105   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
106
107   /// getOrInsertFunction - Look up the specified function in the module symbol
108   /// table.  If it does not exist, add a prototype for the function and return
109   /// it.  This version of the method takes a null terminated list of function
110   /// arguments, which makes it easier for clients to use.
111   Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
112
113   /// getFunction - Look up the specified function in the module symbol table.
114   /// If it does not exist, return null.
115   ///
116   Function *getFunction(const std::string &Name, const FunctionType *Ty);
117
118   /// getMainFunction - This function looks up main efficiently.  This is such a
119   /// common case, that it is a method in Module.  If main cannot be found, a
120   /// null pointer is returned.
121   ///
122   Function *getMainFunction();
123
124   /// getNamedFunction - Return the first function in the module with the
125   /// specified name, of arbitrary type.  This method returns null if a function
126   /// with the specified name is not found.
127   ///
128   Function *getNamedFunction(const std::string &Name);
129
130   //===--------------------------------------------------------------------===//
131   // Methods for easy access to the global variables in the module.
132   //
133
134   /// getGlobalVariable - Look up the specified global variable in the module
135   /// symbol table.  If it does not exist, return null.  Note that this only
136   /// returns a global variable if it does not have internal linkage.  The type
137   /// argument should be the underlying type of the global, ie, it should not
138   /// have the top-level PointerType, which represents the address of the
139   /// global.
140   ///
141   GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
142
143
144   //===--------------------------------------------------------------------===//
145   // Methods for easy access to the types in the module.
146   //
147
148   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
149   /// there is already an entry for this name, true is returned and the symbol
150   /// table is not modified.
151   ///
152   bool addTypeName(const std::string &Name, const Type *Ty);
153
154   /// getTypeName - If there is at least one entry in the symbol table for the
155   /// specified type, return it.
156   ///
157   std::string getTypeName(const Type *Ty) const;
158
159   /// getTypeByName - Return the type with the specified name in this module, or
160   /// null if there is none by that name.
161   const Type *getTypeByName(const std::string &Name) const;
162
163
164   //===--------------------------------------------------------------------===//
165   // Methods for direct access to the globals list, functions list, and symbol
166   // table.
167   //
168
169   /// Get the underlying elements of the Module...
170   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
171   inline       GlobalListType &getGlobalList()        { return GlobalList; }
172   inline const FunctionListType &getFunctionList() const { return FunctionList;}
173   inline       FunctionListType &getFunctionList()       { return FunctionList;}
174
175   /// getSymbolTable() - Get access to the symbol table for the module, where
176   /// global variables and functions are identified.
177   ///
178   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
179   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
180
181
182   //===--------------------------------------------------------------------===//
183   // Module iterator forwarding functions
184   //
185   inline giterator                gbegin()       { return GlobalList.begin(); }
186   inline const_giterator          gbegin() const { return GlobalList.begin(); }
187   inline giterator                gend  ()       { return GlobalList.end();   }
188   inline const_giterator          gend  () const { return GlobalList.end();   }
189
190   inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
191   inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
192   inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
193   inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
194
195   inline unsigned                  gsize() const { return GlobalList.size(); }
196   inline bool                     gempty() const { return GlobalList.empty(); }
197   inline const GlobalVariable    &gfront() const { return GlobalList.front(); }
198   inline       GlobalVariable    &gfront()       { return GlobalList.front(); }
199   inline const GlobalVariable     &gback() const { return GlobalList.back(); }
200   inline       GlobalVariable     &gback()       { return GlobalList.back(); }
201
202
203
204   inline iterator                begin()       { return FunctionList.begin(); }
205   inline const_iterator          begin() const { return FunctionList.begin(); }
206   inline iterator                end  ()       { return FunctionList.end();   }
207   inline const_iterator          end  () const { return FunctionList.end();   }
208
209   inline reverse_iterator       rbegin()       { return FunctionList.rbegin(); }
210   inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
211   inline reverse_iterator       rend  ()       { return FunctionList.rend();   }
212   inline const_reverse_iterator rend  () const { return FunctionList.rend();   }
213
214   inline unsigned                 size() const { return FunctionList.size(); }
215   inline bool                    empty() const { return FunctionList.empty(); }
216   inline const Function         &front() const { return FunctionList.front(); }
217   inline       Function         &front()       { return FunctionList.front(); }
218   inline const Function          &back() const { return FunctionList.back(); }
219   inline       Function          &back()       { return FunctionList.back(); }
220
221   void print(std::ostream &OS) const { print(OS, 0); }
222   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
223
224   void dump() const;
225
226   /// dropAllReferences() - This function causes all the subinstructions to "let
227   /// go" of all references that they are maintaining.  This allows one to
228   /// 'delete' a whole class at a time, even though there may be circular
229   /// references... first all references are dropped, and all use counts go to
230   /// zero.  Then everything is delete'd for real.  Note that no operations are
231   /// valid on an object that has "dropped all references", except operator 
232   /// delete.
233   ///
234   void dropAllReferences();
235 };
236
237 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
238   M->print(O);
239   return O;
240 }
241
242 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
243   M.print(O);
244   return O;
245 }
246
247 } // End llvm namespace
248
249 #endif