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