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