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