Okay, the list of link-time passes wasn't such a hot idea. Its prone to
[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 #include "llvm/ADT/SetVector.h"
25
26 namespace llvm {
27
28 class GlobalVariable;
29 class GlobalValueRefMap;   // Used by ConstantVals.cpp
30 class ConstantPointerRef;
31 class FunctionType;
32 class SymbolTable;
33
34 template<> struct ilist_traits<Function>
35   : public SymbolTableListTraits<Function, Module, Module> {
36   // createNode is used to create a node that marks the end of the list...
37   static Function *createNode();
38   static iplist<Function> &getList(Module *M);
39 };
40 template<> struct ilist_traits<GlobalVariable>
41   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
42   // createNode is used to create a node that marks the end of the list...
43   static GlobalVariable *createNode();
44   static iplist<GlobalVariable> &getList(Module *M);
45 };
46
47 class Module {
48 public:
49   typedef iplist<GlobalVariable> GlobalListType;
50   typedef iplist<Function> FunctionListType;
51   typedef SetVector<std::string> LibraryListType;
52
53   // Global Variable iterators...
54   typedef GlobalListType::iterator                             giterator;
55   typedef GlobalListType::const_iterator                 const_giterator;
56   typedef std::reverse_iterator<giterator>             reverse_giterator;
57   typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
58
59   // Function iterators...
60   typedef FunctionListType::iterator                          iterator;
61   typedef FunctionListType::const_iterator              const_iterator;
62   typedef std::reverse_iterator<iterator>             reverse_iterator;
63   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
64
65   // Library list iterators
66   typedef LibraryListType::const_iterator lib_iterator;
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(const 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   //===--------------------------------------------------------------------===//
230   // List of dependent library access functions
231
232   /// @brief Get a constant iterator to beginning of dependent library list.
233   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
234
235   /// @brief Get a constant iterator to end of dependent library list.
236   inline lib_iterator lib_end() const { return LibraryList.end(); }
237
238   /// @brief Returns the number of items in the list of libraries.
239   inline unsigned lib_size() const { return LibraryList.size(); }
240
241   /// @brief Add a library to the list of dependent libraries
242   inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
243
244   /// @brief Remove a library from the list of dependent libraries
245   inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
246
247   /// @brief Get all the libraries 
248   inline const LibraryListType& getLibraries() const { return LibraryList; }
249
250   //===--------------------------------------------------------------------===//
251   // Utility functions for printing and dumping Module objects
252
253   void print(std::ostream &OS) const { print(OS, 0); }
254   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
255
256   void dump() const;
257
258   /// dropAllReferences() - This function causes all the subinstructions to "let
259   /// go" of all references that they are maintaining.  This allows one to
260   /// 'delete' a whole class at a time, even though there may be circular
261   /// references... first all references are dropped, and all use counts go to
262   /// zero.  Then everything is delete'd for real.  Note that no operations are
263   /// valid on an object that has "dropped all references", except operator 
264   /// delete.
265   ///
266   void dropAllReferences();
267 };
268
269 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
270   M->print(O);
271   return O;
272 }
273
274 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
275   M.print(O);
276   return O;
277 }
278
279 } // End llvm namespace
280
281 #endif