bug 263:
[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 class Module {
47 public:
48   typedef iplist<GlobalVariable> GlobalListType;
49   typedef iplist<Function> FunctionListType;
50   typedef std::vector<std::string> LibraryListType;
51
52   // Global Variable iterators...
53   typedef GlobalListType::iterator                             giterator;
54   typedef GlobalListType::const_iterator                 const_giterator;
55   typedef std::reverse_iterator<giterator>             reverse_giterator;
56   typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
57
58   // Function iterators...
59   typedef FunctionListType::iterator                          iterator;
60   typedef FunctionListType::const_iterator              const_iterator;
61   typedef std::reverse_iterator<iterator>             reverse_iterator;
62   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
63
64   // Library list iterators
65   typedef LibraryListType::iterator literator;
66   typedef LibraryListType::const_iterator const_literator;
67
68   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
69   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
70
71 private:
72   GlobalListType GlobalList;     // The Global Variables in the module
73   FunctionListType FunctionList; // The Functions in the module
74   LibraryListType LibraryList;   // The Libraries needed by the module
75   SymbolTable *SymTab;           // Symbol Table for the module
76   std::string ModuleID;          // Human readable identifier for the module
77   std::string TargetTriple;      // Platform target triple Module compiled on
78
79   // These flags are probably not the right long-term way to handle this kind of
80   // target information, but it is sufficient for now.
81   Endianness  Endian;     // True if target is little endian
82   PointerSize PtrSize;    // True if target has 32-bit pointers (false = 64-bit)
83
84   // Accessor for the underlying GVRefMap... only through the Constant class...
85   friend class Constant;
86   friend class ConstantPointerRef;
87   ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
88   void destroyConstantPointerRef(ConstantPointerRef *CPR);
89
90 public:
91   Module(const std::string &ModuleID);
92   ~Module();
93
94   const std::string& getModuleIdentifier() const { return ModuleID; }
95   const std::string& getTargetTriple() const { return TargetTriple; }
96   void setTargetTriple(std::string& T) { TargetTriple = T; }
97
98   /// Target endian information...
99   Endianness getEndianness() const { return Endian; }
100   void setEndianness(Endianness E) { Endian = E; }
101
102   /// Target Pointer Size information...
103   PointerSize getPointerSize() const { return PtrSize; }
104   void setPointerSize(PointerSize PS) { PtrSize = PS; }
105
106   //===--------------------------------------------------------------------===//
107   // Methods for easy access to the functions in the module.
108   //
109
110   /// getOrInsertFunction - Look up the specified function in the module symbol
111   /// table.  If it does not exist, add a prototype for the function and return
112   /// it.
113   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
114
115   /// getOrInsertFunction - Look up the specified function in the module symbol
116   /// table.  If it does not exist, add a prototype for the function and return
117   /// it.  This version of the method takes a null terminated list of function
118   /// arguments, which makes it easier for clients to use.
119   Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
120
121   /// getFunction - Look up the specified function in the module symbol table.
122   /// If it does not exist, return null.
123   ///
124   Function *getFunction(const std::string &Name, const FunctionType *Ty);
125
126   /// getMainFunction - This function looks up main efficiently.  This is such a
127   /// common case, that it is a method in Module.  If main cannot be found, a
128   /// null pointer is returned.
129   ///
130   Function *getMainFunction();
131
132   /// getNamedFunction - Return the first function in the module with the
133   /// specified name, of arbitrary type.  This method returns null if a function
134   /// with the specified name is not found.
135   ///
136   Function *getNamedFunction(const std::string &Name);
137
138   //===--------------------------------------------------------------------===//
139   // Methods for easy access to the global variables in the module.
140   //
141
142   /// getGlobalVariable - Look up the specified global variable in the module
143   /// symbol table.  If it does not exist, return null.  Note that this only
144   /// returns a global variable if it does not have internal linkage.  The type
145   /// argument should be the underlying type of the global, ie, it should not
146   /// have the top-level PointerType, which represents the address of the
147   /// global.
148   ///
149   GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
150
151
152   //===--------------------------------------------------------------------===//
153   // Methods for easy access to the types in the module.
154   //
155
156   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
157   /// there is already an entry for this name, true is returned and the symbol
158   /// table is not modified.
159   ///
160   bool addTypeName(const std::string &Name, const Type *Ty);
161
162   /// getTypeName - If there is at least one entry in the symbol table for the
163   /// specified type, return it.
164   ///
165   std::string getTypeName(const Type *Ty) const;
166
167   /// getTypeByName - Return the type with the specified name in this module, or
168   /// null if there is none by that name.
169   const Type *getTypeByName(const std::string &Name) const;
170
171
172   //===--------------------------------------------------------------------===//
173   // Methods for direct access to the globals list, functions list, and symbol
174   // table.
175   //
176
177   /// Get the underlying elements of the Module...
178   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
179   inline       GlobalListType &getGlobalList()        { return GlobalList; }
180   inline const FunctionListType &getFunctionList() const { return FunctionList;}
181   inline       FunctionListType &getFunctionList()       { return FunctionList;}
182
183   /// getSymbolTable() - Get access to the symbol table for the module, where
184   /// global variables and functions are identified.
185   ///
186   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
187   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
188
189
190   //===--------------------------------------------------------------------===//
191   // Module iterator forwarding functions
192   //
193   // Globals list interface
194   inline giterator                gbegin()       { return GlobalList.begin(); }
195   inline const_giterator          gbegin() const { return GlobalList.begin(); }
196   inline giterator                gend  ()       { return GlobalList.end();   }
197   inline const_giterator          gend  () const { return GlobalList.end();   }
198
199   inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
200   inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
201   inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
202   inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
203
204   inline unsigned                  gsize() const { return GlobalList.size(); }
205   inline bool                     gempty() const { return GlobalList.empty(); }
206   inline const GlobalVariable    &gfront() const { return GlobalList.front(); }
207   inline       GlobalVariable    &gfront()       { return GlobalList.front(); }
208   inline const GlobalVariable     &gback() const { return GlobalList.back(); }
209   inline       GlobalVariable     &gback()       { return GlobalList.back(); }
210
211   // FunctionList interface
212   inline iterator                begin()       { return FunctionList.begin(); }
213   inline const_iterator          begin() const { return FunctionList.begin(); }
214   inline iterator                end  ()       { return FunctionList.end();   }
215   inline const_iterator          end  () const { return FunctionList.end();   }
216
217   inline reverse_iterator       rbegin()       { return FunctionList.rbegin(); }
218   inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
219   inline reverse_iterator       rend  ()       { return FunctionList.rend();   }
220   inline const_reverse_iterator rend  () const { return FunctionList.rend();   }
221
222   inline unsigned                 size() const { return FunctionList.size(); }
223   inline bool                    empty() const { return FunctionList.empty(); }
224   inline const Function         &front() const { return FunctionList.front(); }
225   inline       Function         &front()       { return FunctionList.front(); }
226   inline const Function          &back() const { return FunctionList.back(); }
227   inline       Function          &back()       { return FunctionList.back(); }
228
229   // LibraryList interface
230   inline literator           lbegin()       { return LibraryList.begin(); }
231   inline const_literator     lbegin() const { return LibraryList.begin(); }
232   inline literator           lend  ()       { return LibraryList.end();   }
233   inline const_literator     lend  () const { return LibraryList.end();   }
234
235   inline unsigned             lsize() const { return LibraryList.size(); }
236   inline bool                lempty() const { return LibraryList.empty(); }
237   inline const std::string&  lfront() const { return LibraryList.front(); }
238   inline       std::string&  lfront()       { return LibraryList.front(); }
239   inline const std::string&   lback() const { return LibraryList.back(); }
240   inline       std::string&   lback()       { return LibraryList.back(); }
241
242   inline void linsert(std::string& Lib){ LibraryList.push_back(Lib); }
243   inline void lremove(std::string& Lib);
244
245   void print(std::ostream &OS) const { print(OS, 0); }
246   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
247
248   void dump() const;
249
250   /// dropAllReferences() - This function causes all the subinstructions to "let
251   /// go" of all references that they are maintaining.  This allows one to
252   /// 'delete' a whole class at a time, even though there may be circular
253   /// references... first all references are dropped, and all use counts go to
254   /// zero.  Then everything is delete'd for real.  Note that no operations are
255   /// valid on an object that has "dropped all references", except operator 
256   /// delete.
257   ///
258   void dropAllReferences();
259 };
260
261 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
262   M->print(O);
263   return O;
264 }
265
266 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
267   M.print(O);
268   return O;
269 }
270
271 } // End llvm namespace
272
273 #endif