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