For PR411:
[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 /// @file This file contains the declarations for the Module class. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_MODULE_H
15 #define LLVM_MODULE_H
16
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/Support/DataTypes.h"
20
21 namespace llvm {
22
23 class GlobalVariable;
24 class GlobalValueRefMap;   // Used by ConstantVals.cpp
25 class FunctionType;
26
27 template<> struct ilist_traits<Function>
28   : public SymbolTableListTraits<Function, Module, Module> {
29   // createSentinel is used to create a node that marks the end of the list.
30   static Function *createSentinel();
31   static void destroySentinel(Function *F) { delete F; }
32   static iplist<Function> &getList(Module *M);
33 };
34 template<> struct ilist_traits<GlobalVariable>
35   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
36   // createSentinel is used to create a node that marks the end of the list.
37   static GlobalVariable *createSentinel();
38   static void destroySentinel(GlobalVariable *GV) { delete GV; }
39   static iplist<GlobalVariable> &getList(Module *M);
40 };
41
42 /// A Module instance is used to store all the information related to an
43 /// LLVM module. Modules are the top level container of all other LLVM 
44 /// Intermediate Representation (IR) objects. Each module directly contains a
45 /// list of globals variables, a list of functions, a list of libraries (or 
46 /// other modules) this module depends on, a symbol table, and various data
47 /// about the target's characteristics.
48 ///
49 /// A module maintains a GlobalValRefMap object that is used to hold all
50 /// constant references to global variables in the module.  When a global
51 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
52 /// @brief The main container class for the LLVM Intermediate Representation.
53 class Module {
54 /// @name Types And Enumerations
55 /// @{
56 public:
57   /// The type for the list of global variables.
58   typedef iplist<GlobalVariable> GlobalListType;
59   /// The type for the list of functions.
60   typedef iplist<Function> FunctionListType;
61
62   /// The type for the list of dependent libraries.
63   typedef std::vector<std::string> LibraryListType;
64
65   /// The Global Variable iterator.
66   typedef GlobalListType::iterator                     global_iterator;
67   /// The Global Variable constant iterator.
68   typedef GlobalListType::const_iterator         const_global_iterator;
69
70   /// The Function iterators.
71   typedef FunctionListType::iterator                          iterator;
72   /// The Function constant iterator
73   typedef FunctionListType::const_iterator              const_iterator;
74
75   /// The Library list iterator.
76   typedef LibraryListType::const_iterator lib_iterator;
77
78   /// An enumeration for describing the endianess of the target machine.
79   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
80
81   /// An enumeration for describing the size of a pointer on the target machine.
82   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
83
84 /// @}
85 /// @name Member Variables
86 /// @{
87 private:
88   GlobalListType GlobalList;     ///< The Global Variables in the module
89   FunctionListType FunctionList; ///< The Functions in the module
90   LibraryListType LibraryList;   ///< The Libraries needed by the module
91   std::string GlobalScopeAsm;    ///< Inline Asm at global scope.
92   ValueSymbolTable *ValSymTab;   ///< Symbol table for values
93   TypeSymbolTable *TypeSymTab;   ///< Symbol table for types
94   std::string ModuleID;          ///< Human readable identifier for the module
95   std::string TargetTriple;      ///< Platform target triple Module compiled on
96   std::string DataLayout;        ///< Target data description
97
98   friend class Constant;
99
100 /// @}
101 /// @name Constructors
102 /// @{
103 public:
104   /// The Module constructor. Note that there is no default constructor. You
105   /// must provide a name for the module upon construction.
106   Module(const std::string &ModuleID);
107   /// The module destructor. This will dropAllReferences.
108   ~Module();
109
110 /// @}
111 /// @name Module Level Accessors
112 /// @{
113 public:
114   /// Get the module identifier which is, essentially, the name of the module.
115   /// @returns the module identifier as a string
116   const std::string &getModuleIdentifier() const { return ModuleID; }
117
118   /// Get the data layout string for the module's target platform.  This encodes
119   /// the type sizes and alignments expected by this module.
120   /// @returns the data layout as a string
121   const std::string& getDataLayout() const { return DataLayout; }
122
123   /// Get the target triple which is a string describing the target host.
124   /// @returns a string containing the target triple.
125   const std::string &getTargetTriple() const { return TargetTriple; }
126
127   /// Get the target endian information.
128   /// @returns Endianess - an enumeration for the endianess of the target
129   Endianness getEndianness() const;
130
131   /// Get the target pointer size.
132   /// @returns PointerSize - an enumeration for the size of the target's pointer
133   PointerSize getPointerSize() const;
134
135   /// Get any module-scope inline assembly blocks.
136   /// @returns a string containing the module-scope inline assembly blocks.
137   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
138 /// @}
139 /// @name Module Level Mutators
140 /// @{
141 public:
142
143   /// Set the module identifier.
144   void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
145
146   /// Set the data layout
147   void setDataLayout(const std::string& DL) { DataLayout = DL; }
148
149   /// Set the target triple.
150   void setTargetTriple(const std::string &T) { TargetTriple = T; }
151
152   /// Set the module-scope inline assembly blocks.
153   void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
154   
155 /// @}
156 /// @name Function Accessors
157 /// @{
158 public:
159   /// getOrInsertFunction - Look up the specified function in the module symbol
160   /// table.  Four possibilities:
161   ///   1. If it does not exist, add a prototype for the function and return it.
162   ///   2. If it exists, and has internal linkage, the existing function is
163   ///      renamed and a new one is inserted.
164   ///   3. Otherwise, if the existing function has the correct prototype, return
165   ///      the existing function.
166   ///   4. Finally, the function exists but has the wrong prototype: return the
167   ///      function with a constantexpr cast to the right prototype.
168   Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
169
170   /// getOrInsertFunction - Look up the specified function in the module symbol
171   /// table.  If it does not exist, add a prototype for the function and return
172   /// it.  This version of the method takes a null terminated list of function
173   /// arguments, which makes it easier for clients to use.
174   Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
175     END_WITH_NULL;
176
177   /// getFunction - Look up the specified function in the module symbol table.
178   /// If it does not exist, return null.
179   Function *getFunction(const std::string &Name) const;
180
181   /// getMainFunction - This function looks up main efficiently.  This is such a
182   /// common case, that it is a method in Module.  If main cannot be found, a
183   /// null pointer is returned.
184   Function *getMainFunction() { return getFunction("main"); }
185
186   /// getNamedFunction - Return the first function in the module with the
187   /// specified name, of arbitrary type.  This method returns null if a function
188   /// with the specified name is not found.
189   Function *getNamedFunction(const std::string &Name) const {
190     return getFunction(Name);
191   }
192
193 /// @}
194 /// @name Global Variable Accessors 
195 /// @{
196 public:
197   /// getGlobalVariable - Look up the specified global variable in the module
198   /// symbol table.  If it does not exist, return null.  The type argument
199   /// should be the underlying type of the global, i.e., it should not have
200   /// the top-level PointerType, which represents the address of the global.
201   /// If AllowInternal is set to true, this function will return types that
202   /// have InternalLinkage. By default, these types are not returned.
203   GlobalVariable *getGlobalVariable(const std::string &Name, 
204                                     bool AllowInternal = false) const;
205
206   /// getNamedGlobal - Return the first global variable in the module with the
207   /// specified name, of arbitrary type.  This method returns null if a global
208   /// with the specified name is not found.
209   GlobalVariable *getNamedGlobal(const std::string &Name) const {
210     return getGlobalVariable(Name, true);
211   }
212   
213 /// @}
214 /// @name Type Accessors
215 /// @{
216 public:
217   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
218   /// there is already an entry for this name, true is returned and the symbol
219   /// table is not modified.
220   bool addTypeName(const std::string &Name, const Type *Ty);
221
222   /// getTypeName - If there is at least one entry in the symbol table for the
223   /// specified type, return it.
224   std::string getTypeName(const Type *Ty) const;
225
226   /// getTypeByName - Return the type with the specified name in this module, or
227   /// null if there is none by that name.
228   const Type *getTypeByName(const std::string &Name) const;
229
230 /// @}
231 /// @name Direct access to the globals list, functions list, and symbol table
232 /// @{
233 public:
234   /// Get the Module's list of global variables (constant).
235   const GlobalListType   &getGlobalList() const       { return GlobalList; }
236   /// Get the Module's list of global variables.
237   GlobalListType         &getGlobalList()             { return GlobalList; }
238   /// Get the Module's list of functions (constant).
239   const FunctionListType &getFunctionList() const     { return FunctionList; }
240   /// Get the Module's list of functions.
241   FunctionListType       &getFunctionList()           { return FunctionList; }
242   /// Get the symbol table of global variable and function identifiers
243   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
244   /// Get the Module's symbol table of global variable and function identifiers.
245   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
246   /// Get the symbol table of types
247   const TypeSymbolTable   &getTypeSymbolTable() const { return *TypeSymTab; }
248   /// Get the Module's symbol table of types
249   TypeSymbolTable         &getTypeSymbolTable()       { return *TypeSymTab; }
250
251 /// @}
252 /// @name Global Variable Iteration
253 /// @{
254 public:
255   /// Get an iterator to the first global variable
256   global_iterator       global_begin()       { return GlobalList.begin(); }
257   /// Get a constant iterator to the first global variable
258   const_global_iterator global_begin() const { return GlobalList.begin(); }
259   /// Get an iterator to the last global variable
260   global_iterator       global_end  ()       { return GlobalList.end(); }
261   /// Get a constant iterator to the last global variable
262   const_global_iterator global_end  () const { return GlobalList.end(); }
263   /// Determine if the list of globals is empty.
264   bool                  global_empty() const { return GlobalList.empty(); }
265
266 /// @}
267 /// @name Function Iteration
268 /// @{
269 public:
270   /// Get an iterator to the first function.
271   iterator                begin()       { return FunctionList.begin(); }
272   /// Get a constant iterator to the first function.
273   const_iterator          begin() const { return FunctionList.begin(); }
274   /// Get an iterator to the last function.
275   iterator                end  ()       { return FunctionList.end();   }
276   /// Get a constant iterator to the last function.
277   const_iterator          end  () const { return FunctionList.end();   }
278   /// Determine how many functions are in the Module's list of functions.
279   size_t                   size() const { return FunctionList.size(); }
280   /// Determine if the list of functions is empty.
281   bool                    empty() const { return FunctionList.empty(); }
282
283 /// @}
284 /// @name Dependent Library Iteration 
285 /// @{
286 public:
287   /// @brief Get a constant iterator to beginning of dependent library list.
288   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
289   /// @brief Get a constant iterator to end of dependent library list.
290   inline lib_iterator lib_end() const { return LibraryList.end(); }
291   /// @brief Returns the number of items in the list of libraries.
292   inline size_t lib_size() const { return LibraryList.size(); }
293   /// @brief Add a library to the list of dependent libraries
294   void addLibrary(const std::string& Lib);
295   /// @brief Remove a library from the list of dependent libraries
296   void removeLibrary(const std::string& Lib);
297   /// @brief Get all the libraries
298   inline const LibraryListType& getLibraries() const { return LibraryList; }
299
300 /// @}
301 /// @name Utility functions for printing and dumping Module objects
302 /// @{
303 public:
304   /// Print the module to an output stream
305   void print(std::ostream &OS) const { print(OS, 0); }
306   void print(std::ostream *OS) const { if (OS) print(*OS); }
307   /// Print the module to an output stream with AssemblyAnnotationWriter.
308   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
309   void print(std::ostream *OS, AssemblyAnnotationWriter *AAW) const {
310     if (OS) print(*OS, AAW);
311   }
312   /// Dump the module to std::cerr (for debugging).
313   void dump() const;
314   /// This function causes all the subinstructions to "let go" of all references
315   /// that they are maintaining.  This allows one to 'delete' a whole class at 
316   /// a time, even though there may be circular references... first all 
317   /// references are dropped, and all use counts go to zero.  Then everything 
318   /// is delete'd for real.  Note that no operations are valid on an object 
319   /// that has "dropped all references", except operator delete.
320   void dropAllReferences();
321 /// @}
322 };
323
324 /// An iostream inserter for modules.
325 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
326   M.print(O);
327   return O;
328 }
329
330 } // End llvm namespace
331
332 #endif