Fix a bunch of Doxygen syntax issues. Escape special characters,
[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 is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file
11 /// Module.h This file contains the declarations for the Module class.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_MODULE_H
16 #define LLVM_MODULE_H
17
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <vector>
23
24 namespace llvm {
25
26 class GlobalValueRefMap;   // Used by ConstantVals.cpp
27 class FunctionType;
28
29 template<> struct ilist_traits<Function>
30   : public SymbolTableListTraits<Function, Module> {
31   // createSentinel is used to create a node that marks the end of the list.
32   static Function *createSentinel();
33   static void destroySentinel(Function *F) { delete F; }
34   static iplist<Function> &getList(Module *M);
35   static inline ValueSymbolTable *getSymTab(Module *M);
36   static int getListOffset();
37 };
38 template<> struct ilist_traits<GlobalVariable>
39   : public SymbolTableListTraits<GlobalVariable, Module> {
40   // createSentinel is used to create a node that marks the end of the list.
41   static GlobalVariable *createSentinel();
42   static void destroySentinel(GlobalVariable *GV) { delete GV; }
43   static iplist<GlobalVariable> &getList(Module *M);
44   static inline ValueSymbolTable *getSymTab(Module *M);
45   static int getListOffset();
46 };
47 template<> struct ilist_traits<GlobalAlias>
48   : public SymbolTableListTraits<GlobalAlias, Module> {
49   // createSentinel is used to create a node that marks the end of the list.
50   static GlobalAlias *createSentinel();
51   static void destroySentinel(GlobalAlias *GA) { delete GA; }
52   static iplist<GlobalAlias> &getList(Module *M);
53   static inline ValueSymbolTable *getSymTab(Module *M);
54   static int getListOffset();
55 };
56
57 /// A Module instance is used to store all the information related to an
58 /// LLVM module. Modules are the top level container of all other LLVM
59 /// Intermediate Representation (IR) objects. Each module directly contains a
60 /// list of globals variables, a list of functions, a list of libraries (or
61 /// other modules) this module depends on, a symbol table, and various data
62 /// about the target's characteristics.
63 ///
64 /// A module maintains a GlobalValRefMap object that is used to hold all
65 /// constant references to global variables in the module.  When a global
66 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
67 /// @brief The main container class for the LLVM Intermediate Representation.
68 class Module {
69 /// @name Types And Enumerations
70 /// @{
71 public:
72   /// The type for the list of global variables.
73   typedef iplist<GlobalVariable> GlobalListType;
74   /// The type for the list of functions.
75   typedef iplist<Function> FunctionListType;
76   /// The type for the list of aliases.
77   typedef iplist<GlobalAlias> AliasListType;
78
79   /// The type for the list of dependent libraries.
80   typedef std::vector<std::string> LibraryListType;
81
82   /// The Global Variable iterator.
83   typedef GlobalListType::iterator                     global_iterator;
84   /// The Global Variable constant iterator.
85   typedef GlobalListType::const_iterator         const_global_iterator;
86
87   /// The Function iterators.
88   typedef FunctionListType::iterator                          iterator;
89   /// The Function constant iterator
90   typedef FunctionListType::const_iterator              const_iterator;
91
92   /// The Global Alias iterators.
93   typedef AliasListType::iterator                       alias_iterator;
94   /// The Global Alias constant iterator
95   typedef AliasListType::const_iterator           const_alias_iterator;
96
97   /// The Library list iterator.
98   typedef LibraryListType::const_iterator lib_iterator;
99
100   /// An enumeration for describing the endianess of the target machine.
101   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
102
103   /// An enumeration for describing the size of a pointer on the target machine.
104   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
105
106 /// @}
107 /// @name Member Variables
108 /// @{
109 private:
110   GlobalListType GlobalList;     ///< The Global Variables in the module
111   FunctionListType FunctionList; ///< The Functions in the module
112   AliasListType AliasList;       ///< The Aliases in the module
113   LibraryListType LibraryList;   ///< The Libraries needed by the module
114   std::string GlobalScopeAsm;    ///< Inline Asm at global scope.
115   ValueSymbolTable *ValSymTab;   ///< Symbol table for values
116   TypeSymbolTable *TypeSymTab;   ///< Symbol table for types
117   std::string ModuleID;          ///< Human readable identifier for the module
118   std::string TargetTriple;      ///< Platform target triple Module compiled on
119   std::string DataLayout;        ///< Target data description
120
121   friend class Constant;
122
123 /// @}
124 /// @name Constructors
125 /// @{
126 public:
127   /// The Module constructor. Note that there is no default constructor. You
128   /// must provide a name for the module upon construction.
129   explicit Module(const std::string &ModuleID);
130   /// The module destructor. This will dropAllReferences.
131   ~Module();
132
133 /// @}
134 /// @name Module Level Accessors
135 /// @{
136 public:
137   /// Get the module identifier which is, essentially, the name of the module.
138   /// @returns the module identifier as a string
139   const std::string &getModuleIdentifier() const { return ModuleID; }
140
141   /// Get the data layout string for the module's target platform.  This encodes
142   /// the type sizes and alignments expected by this module.
143   /// @returns the data layout as a string
144   const std::string& getDataLayout() const { return DataLayout; }
145
146   /// Get the target triple which is a string describing the target host.
147   /// @returns a string containing the target triple.
148   const std::string &getTargetTriple() const { return TargetTriple; }
149
150   /// Get the target endian information.
151   /// @returns Endianess - an enumeration for the endianess of the target
152   Endianness getEndianness() const;
153
154   /// Get the target pointer size.
155   /// @returns PointerSize - an enumeration for the size of the target's pointer
156   PointerSize getPointerSize() const;
157
158   /// Get any module-scope inline assembly blocks.
159   /// @returns a string containing the module-scope inline assembly blocks.
160   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
161 /// @}
162 /// @name Module Level Mutators
163 /// @{
164 public:
165
166   /// Set the module identifier.
167   void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
168
169   /// Set the data layout
170   void setDataLayout(const std::string& DL) { DataLayout = DL; }
171
172   /// Set the target triple.
173   void setTargetTriple(const std::string &T) { TargetTriple = T; }
174
175   /// Set the module-scope inline assembly blocks.
176   void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
177
178   /// Append to the module-scope inline assembly blocks, automatically
179   /// appending a newline to the end.
180   void appendModuleInlineAsm(const std::string &Asm) {
181     GlobalScopeAsm += Asm;
182     GlobalScopeAsm += '\n';
183   }
184
185 /// @}
186 /// @name Function Accessors
187 /// @{
188 public:
189   /// getOrInsertFunction - Look up the specified function in the module symbol
190   /// table.  Four possibilities:
191   ///   1. If it does not exist, add a prototype for the function and return it.
192   ///   2. If it exists, and has a local linkage, the existing function is
193   ///      renamed and a new one is inserted.
194   ///   3. Otherwise, if the existing function has the correct prototype, return
195   ///      the existing function.
196   ///   4. Finally, the function exists but has the wrong prototype: return the
197   ///      function with a constantexpr cast to the right prototype.
198   Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T,
199                                 AttrListPtr AttributeList);
200
201   Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
202
203   /// getOrInsertFunction - Look up the specified function in the module symbol
204   /// table.  If it does not exist, add a prototype for the function and return
205   /// it.  This function guarantees to return a constant of pointer to the
206   /// specified function type or a ConstantExpr BitCast of that type if the
207   /// named function has a different type.  This version of the method takes a
208   /// null terminated list of function arguments, which makes it easier for
209   /// clients to use.
210   Constant *getOrInsertFunction(const std::string &Name,
211                                 AttrListPtr AttributeList,
212                                 const Type *RetTy, ...)  END_WITH_NULL;
213
214   Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy, ...)
215     END_WITH_NULL;
216
217   Constant *getOrInsertTargetIntrinsic(const std::string &Name,
218                                        const FunctionType *Ty,
219                                        AttrListPtr AttributeList);
220   
221   /// getFunction - Look up the specified function in the module symbol table.
222   /// If it does not exist, return null.
223   Function *getFunction(const std::string &Name) const;
224   Function *getFunction(const char *Name) const;
225
226 /// @}
227 /// @name Global Variable Accessors
228 /// @{
229 public:
230   /// getGlobalVariable - Look up the specified global variable in the module
231   /// symbol table.  If it does not exist, return null. If AllowInternal is set
232   /// to true, this function will return types that have InternalLinkage. By
233   /// default, these types are not returned.
234   GlobalVariable *getGlobalVariable(const std::string &Name,
235                                     bool AllowInternal = false) const;
236
237   /// getNamedGlobal - Return the first global variable in the module with the
238   /// specified name, of arbitrary type.  This method returns null if a global
239   /// with the specified name is not found.
240   GlobalVariable *getNamedGlobal(const std::string &Name) const {
241     return getGlobalVariable(Name, true);
242   }
243
244   /// getOrInsertGlobal - Look up the specified global in the module symbol
245   /// table.
246   ///   1. If it does not exist, add a declaration of the global and return it.
247   ///   2. Else, the global exists but has the wrong type: return the function
248   ///      with a constantexpr cast to the right type.
249   ///   3. Finally, if the existing global is the correct delclaration, return
250   ///      the existing global.
251   Constant *getOrInsertGlobal(const std::string &Name, const Type *Ty);
252
253 /// @}
254 /// @name Global Alias Accessors
255 /// @{
256 public:
257   /// getNamedAlias - Return the first global alias in the module with the
258   /// specified name, of arbitrary type.  This method returns null if a global
259   /// with the specified name is not found.
260   GlobalAlias *getNamedAlias(const std::string &Name) const;
261
262 /// @}
263 /// @name Type Accessors
264 /// @{
265 public:
266   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
267   /// there is already an entry for this name, true is returned and the symbol
268   /// table is not modified.
269   bool addTypeName(const std::string &Name, const Type *Ty);
270
271   /// getTypeName - If there is at least one entry in the symbol table for the
272   /// specified type, return it.
273   std::string getTypeName(const Type *Ty) const;
274
275   /// getTypeByName - Return the type with the specified name in this module, or
276   /// null if there is none by that name.
277   const Type *getTypeByName(const std::string &Name) const;
278
279 /// @}
280 /// @name Direct access to the globals list, functions list, and symbol table
281 /// @{
282 public:
283   /// Get the Module's list of global variables (constant).
284   const GlobalListType   &getGlobalList() const       { return GlobalList; }
285   /// Get the Module's list of global variables.
286   GlobalListType         &getGlobalList()             { return GlobalList; }
287   /// Get the Module's list of functions (constant).
288   const FunctionListType &getFunctionList() const     { return FunctionList; }
289   /// Get the Module's list of functions.
290   FunctionListType       &getFunctionList()           { return FunctionList; }
291   /// Get the Module's list of aliases (constant).
292   const AliasListType    &getAliasList() const        { return AliasList; }
293   /// Get the Module's list of aliases.
294   AliasListType          &getAliasList()              { return AliasList; }
295   /// Get the symbol table of global variable and function identifiers
296   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
297   /// Get the Module's symbol table of global variable and function identifiers.
298   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
299   /// Get the symbol table of types
300   const TypeSymbolTable  &getTypeSymbolTable() const  { return *TypeSymTab; }
301   /// Get the Module's symbol table of types
302   TypeSymbolTable        &getTypeSymbolTable()        { return *TypeSymTab; }
303
304 /// @}
305 /// @name Global Variable Iteration
306 /// @{
307 public:
308   /// Get an iterator to the first global variable
309   global_iterator       global_begin()       { return GlobalList.begin(); }
310   /// Get a constant iterator to the first global variable
311   const_global_iterator global_begin() const { return GlobalList.begin(); }
312   /// Get an iterator to the last global variable
313   global_iterator       global_end  ()       { return GlobalList.end(); }
314   /// Get a constant iterator to the last global variable
315   const_global_iterator global_end  () const { return GlobalList.end(); }
316   /// Determine if the list of globals is empty.
317   bool                  global_empty() const { return GlobalList.empty(); }
318
319 /// @}
320 /// @name Function Iteration
321 /// @{
322 public:
323   /// Get an iterator to the first function.
324   iterator                begin()       { return FunctionList.begin(); }
325   /// Get a constant iterator to the first function.
326   const_iterator          begin() const { return FunctionList.begin(); }
327   /// Get an iterator to the last function.
328   iterator                end  ()       { return FunctionList.end();   }
329   /// Get a constant iterator to the last function.
330   const_iterator          end  () const { return FunctionList.end();   }
331   /// Determine how many functions are in the Module's list of functions.
332   size_t                  size() const  { return FunctionList.size(); }
333   /// Determine if the list of functions is empty.
334   bool                    empty() const { return FunctionList.empty(); }
335
336 /// @}
337 /// @name Dependent Library Iteration
338 /// @{
339 public:
340   /// @brief Get a constant iterator to beginning of dependent library list.
341   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
342   /// @brief Get a constant iterator to end of dependent library list.
343   inline lib_iterator lib_end()   const { return LibraryList.end();   }
344   /// @brief Returns the number of items in the list of libraries.
345   inline size_t       lib_size()  const { return LibraryList.size();  }
346   /// @brief Add a library to the list of dependent libraries
347   void addLibrary(const std::string& Lib);
348   /// @brief Remove a library from the list of dependent libraries
349   void removeLibrary(const std::string& Lib);
350   /// @brief Get all the libraries
351   inline const LibraryListType& getLibraries() const { return LibraryList; }
352
353 /// @}
354 /// @name Alias Iteration
355 /// @{
356 public:
357   /// Get an iterator to the first alias.
358   alias_iterator       alias_begin()            { return AliasList.begin(); }
359   /// Get a constant iterator to the first alias.
360   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
361   /// Get an iterator to the last alias.
362   alias_iterator       alias_end  ()            { return AliasList.end();   }
363   /// Get a constant iterator to the last alias.
364   const_alias_iterator alias_end  () const      { return AliasList.end();   }
365   /// Determine how many functions are in the Module's list of aliases.
366   size_t               alias_size () const      { return AliasList.size();  }
367   /// Determine if the list of aliases is empty.
368   bool                 alias_empty() const      { return AliasList.empty(); }
369
370 /// @}
371 /// @name Utility functions for printing and dumping Module objects
372 /// @{
373 public:
374   /// Print the module to an output stream with AssemblyAnnotationWriter.
375   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
376   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
377   
378   /// Dump the module to stderr (for debugging).
379   void dump() const;
380   /// This function causes all the subinstructions to "let go" of all references
381   /// that they are maintaining.  This allows one to 'delete' a whole class at
382   /// a time, even though there may be circular references... first all
383   /// references are dropped, and all use counts go to zero.  Then everything
384   /// is delete'd for real.  Note that no operations are valid on an object
385   /// that has "dropped all references", except operator delete.
386   void dropAllReferences();
387 /// @}
388
389   static unsigned getFunctionListOffset() {
390     Module *Obj = 0;
391     return unsigned(reinterpret_cast<uintptr_t>(&Obj->FunctionList));
392   }
393   static unsigned getGlobalVariableListOffset() {
394     Module *Obj = 0;
395     return unsigned(reinterpret_cast<uintptr_t>(&Obj->GlobalList));
396   }
397   static unsigned getAliasListOffset() {
398     Module *Obj = 0;
399     return unsigned(reinterpret_cast<uintptr_t>(&Obj->AliasList));
400   }
401 };
402
403 /// An iostream inserter for modules.
404 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
405   M.print(O, 0);
406   return O;
407 }
408 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
409   M.print(O, 0);
410   return O;
411 }
412   
413
414 inline ValueSymbolTable *
415 ilist_traits<Function>::getSymTab(Module *M) {
416   return M ? &M->getValueSymbolTable() : 0;
417 }
418
419 inline ValueSymbolTable *
420 ilist_traits<GlobalVariable>::getSymTab(Module *M) {
421   return M ? &M->getValueSymbolTable() : 0;
422 }
423
424 inline ValueSymbolTable *
425 ilist_traits<GlobalAlias>::getSymTab(Module *M) {
426   return M ? &M->getValueSymbolTable() : 0;
427 }
428
429 inline int
430 ilist_traits<Function>::getListOffset() {
431   return Module::getFunctionListOffset();
432 }
433
434 inline int
435 ilist_traits<GlobalVariable>::getListOffset() {
436   return Module::getGlobalVariableListOffset();
437 }
438
439 inline int
440 ilist_traits<GlobalAlias>::getListOffset() {
441   return Module::getAliasListOffset();
442 }
443
444 } // End llvm namespace
445
446 #endif