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