35b04b1017f95c4ad121bc48ce18a27387af838f
[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/ADT/SetVector.h"
20 #include "llvm/Support/DataTypes.h"
21
22 namespace llvm {
23
24 class GlobalVariable;
25 class GlobalValueRefMap;   // Used by ConstantVals.cpp
26 class FunctionType;
27 class SymbolTable;
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 SetVector<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 *SymTab;           ///< Symbol Table for the module
95   std::string ModuleID;          ///< Human readable identifier for the module
96   std::string TargetTriple;      ///< Platform target triple Module compiled on
97   std::string DataLayout;        ///< Target data description
98
99   friend class Constant;
100
101 /// @}
102 /// @name Constructors
103 /// @{
104 public:
105   /// The Module constructor. Note that there is no default constructor. You
106   /// must provide a name for the module upon construction.
107   Module(const std::string &ModuleID);
108   /// The module destructor. This will dropAllReferences.
109   ~Module();
110
111 /// @}
112 /// @name Module Level Accessors
113 /// @{
114 public:
115   /// Get the module identifier which is, essentially, the name of the module.
116   /// @returns the module identifier as a string
117   const std::string &getModuleIdentifier() const { return ModuleID; }
118
119   /// Get the data layout string for the module's target platform.  This encodes
120   /// the type sizes and alignments expected by this module.
121   /// @returns the data layout as a string
122   std::string getDataLayout() const { return DataLayout; }
123
124   /// Get the target triple which is a string describing the target host.
125   /// @returns a string containing the target triple.
126   const std::string &getTargetTriple() const { return TargetTriple; }
127
128   /// Get the target endian information.
129   /// @returns Endianess - an enumeration for the endianess of the target
130   Endianness getEndianness() const;
131
132   /// Get the target pointer size.
133   /// @returns PointerSize - an enumeration for the size of the target's pointer
134   PointerSize getPointerSize() const;
135
136   /// Get any module-scope inline assembly blocks.
137   /// @returns a string containing the module-scope inline assembly blocks.
138   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
139 /// @}
140 /// @name Module Level Mutators
141 /// @{
142 public:
143
144   /// Set the module identifier.
145   void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
146
147   /// Set the data layout
148   void setDataLayout(std::string DL) { DataLayout = DL; }
149
150   /// Set the target triple.
151   void setTargetTriple(const std::string &T) { TargetTriple = T; }
152
153   /// Set the target endian information.
154   void setEndianness(Endianness E);
155
156   /// Set the target pointer size.
157   void setPointerSize(PointerSize PS);
158
159   /// Set the module-scope inline assembly blocks.
160   void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
161   
162 /// @}
163 /// @name Function Accessors
164 /// @{
165 public:
166   /// getOrInsertFunction - Look up the specified function in the module symbol
167   /// table.  If it does not exist, add a prototype for the function and return
168   /// it.
169   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
170
171   /// getOrInsertFunction - Look up the specified function in the module symbol
172   /// table.  If it does not exist, add a prototype for the function and return
173   /// it.  This version of the method takes a null terminated list of function
174   /// arguments, which makes it easier for clients to use.
175   Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
176     END_WITH_NULL;
177
178   /// getFunction - Look up the specified function in the module symbol table.
179   /// If it does not exist, return null.
180   Function *getFunction(const std::string &Name, const FunctionType *Ty);
181
182   /// getMainFunction - This function looks up main efficiently.  This is such a
183   /// common case, that it is a method in Module.  If main cannot be found, a
184   /// null pointer is returned.
185   Function *getMainFunction();
186
187   /// getNamedFunction - Return the first function in the module with the
188   /// specified name, of arbitrary type.  This method returns null if a function
189   /// with the specified name is not found.
190   Function *getNamedFunction(const std::string &Name) const;
191
192 /// @}
193 /// @name Global Variable Accessors 
194 /// @{
195 public:
196   /// getGlobalVariable - Look up the specified global variable in the module
197   /// symbol table.  If it does not exist, return null.  The type argument
198   /// should be the underlying type of the global, i.e., it should not have
199   /// the top-level PointerType, which represents the address of the global.
200   /// If AllowInternal is set to true, this function will return types that
201   /// have InternalLinkage. By default, these types are not returned.
202   GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty,
203                                     bool AllowInternal = false);
204
205   /// getNamedGlobal - Return the first global variable in the module with the
206   /// specified name, of arbitrary type.  This method returns null if a global
207   /// with the specified name is not found.
208   GlobalVariable *getNamedGlobal(const std::string &Name) const;
209   
210 /// @}
211 /// @name Type Accessors
212 /// @{
213 public:
214   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
215   /// there is already an entry for this name, true is returned and the symbol
216   /// table is not modified.
217   bool addTypeName(const std::string &Name, const Type *Ty);
218
219   /// getTypeName - If there is at least one entry in the symbol table for the
220   /// specified type, return it.
221   std::string getTypeName(const Type *Ty) const;
222
223   /// getTypeByName - Return the type with the specified name in this module, or
224   /// null if there is none by that name.
225   const Type *getTypeByName(const std::string &Name) const;
226
227 /// @}
228 /// @name Direct access to the globals list, functions list, and symbol table
229 /// @{
230 public:
231   /// Get the Module's list of global variables (constant).
232   const GlobalListType   &getGlobalList() const       { return GlobalList; }
233   /// Get the Module's list of global variables.
234   GlobalListType         &getGlobalList()             { return GlobalList; }
235   /// Get the Module's list of functions (constant).
236   const FunctionListType &getFunctionList() const     { return FunctionList; }
237   /// Get the Module's list of functions.
238   FunctionListType       &getFunctionList()           { return FunctionList; }
239   /// Get the symbol table of global variable and function identifiers
240   const SymbolTable      &getSymbolTable() const      { return *SymTab; }
241   /// Get the Module's symbol table of global variable and function identifiers.
242   SymbolTable            &getSymbolTable()            { return *SymTab; }
243
244 /// @}
245 /// @name Global Variable Iteration
246 /// @{
247 public:
248   /// Get an iterator to the first global variable
249   global_iterator       global_begin()       { return GlobalList.begin(); }
250   /// Get a constant iterator to the first global variable
251   const_global_iterator global_begin() const { return GlobalList.begin(); }
252   /// Get an iterator to the last global variable
253   global_iterator       global_end  ()       { return GlobalList.end(); }
254   /// Get a constant iterator to the last global variable
255   const_global_iterator global_end  () const { return GlobalList.end(); }
256   /// Determine if the list of globals is empty.
257   bool                  global_empty() const { return GlobalList.empty(); }
258
259 /// @}
260 /// @name Function Iteration
261 /// @{
262 public:
263   /// Get an iterator to the first function.
264   iterator                begin()       { return FunctionList.begin(); }
265   /// Get a constant iterator to the first function.
266   const_iterator          begin() const { return FunctionList.begin(); }
267   /// Get an iterator to the last function.
268   iterator                end  ()       { return FunctionList.end();   }
269   /// Get a constant iterator to the last function.
270   const_iterator          end  () const { return FunctionList.end();   }
271   /// Determine how many functions are in the Module's list of functions.
272   size_t                   size() const { return FunctionList.size(); }
273   /// Determine if the list of functions is empty.
274   bool                    empty() const { return FunctionList.empty(); }
275
276 /// @}
277 /// @name Dependent Library Iteration 
278 /// @{
279 public:
280   /// @brief Get a constant iterator to beginning of dependent library list.
281   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
282   /// @brief Get a constant iterator to end of dependent library list.
283   inline lib_iterator lib_end() const { return LibraryList.end(); }
284   /// @brief Returns the number of items in the list of libraries.
285   inline size_t lib_size() const { return LibraryList.size(); }
286   /// @brief Add a library to the list of dependent libraries
287   inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
288   /// @brief Remove a library from the list of dependent libraries
289   inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
290   /// @brief Get all the libraries
291   inline const LibraryListType& getLibraries() const { return LibraryList; }
292
293 /// @}
294 /// @name Utility functions for printing and dumping Module objects
295 /// @{
296 public:
297   /// Print the module to an output stream
298   void print(OStream &OS) const {
299     if (OS.stream()) print(*OS.stream(), 0);
300   }
301   void print(std::ostream &OS) const { print(OS, 0); }
302   /// Print the module to an output stream with AssemblyAnnotationWriter.
303   void print(OStream &OS, AssemblyAnnotationWriter *AAW) const {
304     if (OS.stream()) print(*OS.stream(), AAW);
305   }
306   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
307   /// Dump the module to std::cerr (for debugging).
308   void dump() const;
309   /// This function causes all the subinstructions to "let go" of all references
310   /// that they are maintaining.  This allows one to 'delete' a whole class at 
311   /// a time, even though there may be circular references... first all 
312   /// references are dropped, and all use counts go to zero.  Then everything 
313   /// is delete'd for real.  Note that no operations are valid on an object 
314   /// that has "dropped all references", except operator delete.
315   void dropAllReferences();
316 /// @}
317 };
318
319 /// An iostream inserter for modules.
320 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
321   M.print(O);
322   return O;
323 }
324
325 } // End llvm namespace
326
327 #endif