When a function takes a variable number of pointer arguments, with a zero
[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 // This file contains the declarations for the Module class that is used to
11 // maintain all the information related to a VM module.
12 //
13 // A module also maintains a GlobalValRefMap object that is used to hold all
14 // constant references to global variables in the module.  When a global
15 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_MODULE_H
20 #define LLVM_MODULE_H
21
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/Support/DataTypes.h"
26
27 namespace llvm {
28
29 class GlobalVariable;
30 class GlobalValueRefMap;   // Used by ConstantVals.cpp
31 class FunctionType;
32 class SymbolTable;
33
34 template<> struct ilist_traits<Function>
35   : public SymbolTableListTraits<Function, Module, Module> {
36   // createSentinel is used to create a node that marks the end of the list.
37   static Function *createSentinel();
38   static void destroySentinel(Function *F) { delete F; }
39   static iplist<Function> &getList(Module *M);
40 };
41 template<> struct ilist_traits<GlobalVariable>
42   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
43   // createSentinel is used to create a node that marks the end of the list.
44   static GlobalVariable *createSentinel();
45   static void destroySentinel(GlobalVariable *GV) { delete GV; }
46   static iplist<GlobalVariable> &getList(Module *M);
47 };
48
49 class Module {
50 public:
51   typedef iplist<GlobalVariable> GlobalListType;
52   typedef iplist<Function> FunctionListType;
53   typedef SetVector<std::string> LibraryListType;
54
55   // Global Variable iterators...
56   typedef GlobalListType::iterator                                   global_iterator;
57   typedef GlobalListType::const_iterator                       const_global_iterator;
58   typedef global_iterator giterator; // these are legacy, deprecated
59   typedef const_global_iterator const_giterator;
60
61   // Function iterators...
62   typedef FunctionListType::iterator                          iterator;
63   typedef FunctionListType::const_iterator              const_iterator;
64
65   // Library list iterators
66   typedef LibraryListType::const_iterator lib_iterator;
67
68   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
69   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
70
71 private:
72   GlobalListType GlobalList;     // The Global Variables in the module
73   FunctionListType FunctionList; // The Functions in the module
74   LibraryListType LibraryList;   // The Libraries needed by the module
75   SymbolTable *SymTab;           // Symbol Table for the module
76   std::string ModuleID;          // Human readable identifier for the module
77   std::string TargetTriple;      // Platform target triple Module compiled on
78
79   // These flags are probably not the right long-term way to handle this kind of
80   // target information, but it is sufficient for now.
81   Endianness  Endian;     // True if target is little endian
82   PointerSize PtrSize;    // True if target has 32-bit pointers (false = 64-bit)
83
84   friend class Constant;
85
86 public:
87   Module(const std::string &ModuleID);
88   ~Module();
89
90   const std::string& getModuleIdentifier() const { return ModuleID; }
91   const std::string& getTargetTriple() const { return TargetTriple; }
92   void setTargetTriple(const std::string& T) { TargetTriple = T; }
93
94   /// Target endian information...
95   Endianness getEndianness() const { return Endian; }
96   void setEndianness(Endianness E) { Endian = E; }
97
98   /// Target Pointer Size information...
99   PointerSize getPointerSize() const { return PtrSize; }
100   void setPointerSize(PointerSize PS) { PtrSize = PS; }
101
102   //===--------------------------------------------------------------------===//
103   // Methods for easy access to the functions in the module.
104   //
105
106   /// getOrInsertFunction - Look up the specified function in the module symbol
107   /// table.  If it does not exist, add a prototype for the function and return
108   /// it.
109   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
110
111   /// getOrInsertFunction - Look up the specified function in the module symbol
112   /// table.  If it does not exist, add a prototype for the function and return
113   /// it.  This version of the method takes a null terminated list of function
114   /// arguments, which makes it easier for clients to use.
115   Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
116     END_WITH_NULL;
117
118   /// getFunction - Look up the specified function in the module symbol table.
119   /// If it does not exist, return null.
120   ///
121   Function *getFunction(const std::string &Name, const FunctionType *Ty);
122
123   /// getMainFunction - This function looks up main efficiently.  This is such a
124   /// common case, that it is a method in Module.  If main cannot be found, a
125   /// null pointer is returned.
126   ///
127   Function *getMainFunction();
128
129   /// getNamedFunction - Return the first function in the module with the
130   /// specified name, of arbitrary type.  This method returns null if a function
131   /// with the specified name is not found.
132   ///
133   Function *getNamedFunction(const std::string &Name);
134
135   //===--------------------------------------------------------------------===//
136   // Methods for easy access to the global variables in the module.
137   //
138
139   /// getGlobalVariable - Look up the specified global variable in the module
140   /// symbol table.  If it does not exist, return null.  Note that this only
141   /// returns a global variable if it does not have internal linkage.  The type
142   /// argument should be the underlying type of the global, i.e., it should not
143   /// have the top-level PointerType, which represents the address of the
144   /// global.
145   ///
146   GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
147
148
149   //===--------------------------------------------------------------------===//
150   // Methods for easy access to the types in the module.
151   //
152
153   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
154   /// there is already an entry for this name, true is returned and the symbol
155   /// table is not modified.
156   ///
157   bool addTypeName(const std::string &Name, const Type *Ty);
158
159   /// getTypeName - If there is at least one entry in the symbol table for the
160   /// specified type, return it.
161   ///
162   std::string getTypeName(const Type *Ty) const;
163
164   /// getTypeByName - Return the type with the specified name in this module, or
165   /// null if there is none by that name.
166   const Type *getTypeByName(const std::string &Name) const;
167
168
169   //===--------------------------------------------------------------------===//
170   // Methods for direct access to the globals list, functions list, and symbol
171   // table.
172   //
173
174   /// Get the underlying elements of the Module...
175   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
176   inline       GlobalListType &getGlobalList()        { return GlobalList; }
177   inline const FunctionListType &getFunctionList() const { return FunctionList;}
178   inline       FunctionListType &getFunctionList()       { return FunctionList;}
179
180   /// getSymbolTable() - Get access to the symbol table for the module, where
181   /// global variables and functions are identified.
182   ///
183   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
184   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
185
186
187   //===--------------------------------------------------------------------===//
188   // Module iterator forwarding functions
189   //
190   // Globals list interface
191   global_iterator       global_begin()       { return GlobalList.begin(); }
192   const_global_iterator global_begin() const { return GlobalList.begin(); }
193   global_iterator       global_end  ()       { return GlobalList.end(); }
194   const_global_iterator global_end  () const { return GlobalList.end(); }
195   bool                  global_empty() const { return GlobalList.empty(); }
196
197   // FunctionList interface
198   inline iterator                begin()       { return FunctionList.begin(); }
199   inline const_iterator          begin() const { return FunctionList.begin(); }
200   inline iterator                end  ()       { return FunctionList.end();   }
201   inline const_iterator          end  () const { return FunctionList.end();   }
202
203   inline size_t                   size() const { return FunctionList.size(); }
204   inline bool                    empty() const { return FunctionList.empty(); }
205
206   //===--------------------------------------------------------------------===//
207   // List of dependent library access functions
208
209   /// @brief Get a constant iterator to beginning of dependent library list.
210   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
211
212   /// @brief Get a constant iterator to end of dependent library list.
213   inline lib_iterator lib_end() const { return LibraryList.end(); }
214
215   /// @brief Returns the number of items in the list of libraries.
216   inline size_t lib_size() const { return LibraryList.size(); }
217
218   /// @brief Add a library to the list of dependent libraries
219   inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
220
221   /// @brief Remove a library from the list of dependent libraries
222   inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
223
224   /// @brief Get all the libraries
225   inline const LibraryListType& getLibraries() const { return LibraryList; }
226
227   //===--------------------------------------------------------------------===//
228   // Utility functions for printing and dumping Module objects
229
230   void print(std::ostream &OS) const { print(OS, 0); }
231   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
232
233   void dump() const;
234
235   /// dropAllReferences() - This function causes all the subinstructions to "let
236   /// go" of all references that they are maintaining.  This allows one to
237   /// 'delete' a whole class at a time, even though there may be circular
238   /// references... first all references are dropped, and all use counts go to
239   /// zero.  Then everything is delete'd for real.  Note that no operations are
240   /// valid on an object that has "dropped all references", except operator
241   /// delete.
242   ///
243   void dropAllReferences();
244 };
245
246 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
247   M.print(O);
248   return O;
249 }
250
251 } // End llvm namespace
252
253 #endif