Reduce the footprint of the dependent library interface
[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
25 namespace llvm {
26
27 class GlobalVariable;
28 class GlobalValueRefMap;   // Used by ConstantVals.cpp
29 class ConstantPointerRef;
30 class FunctionType;
31 class SymbolTable;
32
33 template<> struct ilist_traits<Function>
34   : public SymbolTableListTraits<Function, Module, Module> {
35   // createNode is used to create a node that marks the end of the list...
36   static Function *createNode();
37   static iplist<Function> &getList(Module *M);
38 };
39 template<> struct ilist_traits<GlobalVariable>
40   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
41   // createNode is used to create a node that marks the end of the list...
42   static GlobalVariable *createNode();
43   static iplist<GlobalVariable> &getList(Module *M);
44 };
45
46 class Module {
47 public:
48   typedef iplist<GlobalVariable> GlobalListType;
49   typedef iplist<Function> FunctionListType;
50   typedef std::vector<std::string> LibraryListType;
51
52   // Global Variable iterators...
53   typedef GlobalListType::iterator                             giterator;
54   typedef GlobalListType::const_iterator                 const_giterator;
55   typedef std::reverse_iterator<giterator>             reverse_giterator;
56   typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
57
58   // Function iterators...
59   typedef FunctionListType::iterator                          iterator;
60   typedef FunctionListType::const_iterator              const_iterator;
61   typedef std::reverse_iterator<iterator>             reverse_iterator;
62   typedef std::reverse_iterator<const_iterator> const_reverse_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   // Accessor for the underlying GVRefMap... only through the Constant class...
84   friend class Constant;
85   friend class ConstantPointerRef;
86   ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
87   void destroyConstantPointerRef(ConstantPointerRef *CPR);
88
89 public:
90   Module(const std::string &ModuleID);
91   ~Module();
92
93   const std::string& getModuleIdentifier() const { return ModuleID; }
94   const std::string& getTargetTriple() const { return TargetTriple; }
95   void setTargetTriple(const std::string& T) { TargetTriple = T; }
96
97   /// Target endian information...
98   Endianness getEndianness() const { return Endian; }
99   void setEndianness(Endianness E) { Endian = E; }
100
101   /// Target Pointer Size information...
102   PointerSize getPointerSize() const { return PtrSize; }
103   void setPointerSize(PointerSize PS) { PtrSize = PS; }
104
105   //===--------------------------------------------------------------------===//
106   // Methods for easy access to the functions in the module.
107   //
108
109   /// getOrInsertFunction - Look up the specified function in the module symbol
110   /// table.  If it does not exist, add a prototype for the function and return
111   /// it.
112   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
113
114   /// getOrInsertFunction - Look up the specified function in the module symbol
115   /// table.  If it does not exist, add a prototype for the function and return
116   /// it.  This version of the method takes a null terminated list of function
117   /// arguments, which makes it easier for clients to use.
118   Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
119
120   /// getFunction - Look up the specified function in the module symbol table.
121   /// If it does not exist, return null.
122   ///
123   Function *getFunction(const std::string &Name, const FunctionType *Ty);
124
125   /// getMainFunction - This function looks up main efficiently.  This is such a
126   /// common case, that it is a method in Module.  If main cannot be found, a
127   /// null pointer is returned.
128   ///
129   Function *getMainFunction();
130
131   /// getNamedFunction - Return the first function in the module with the
132   /// specified name, of arbitrary type.  This method returns null if a function
133   /// with the specified name is not found.
134   ///
135   Function *getNamedFunction(const std::string &Name);
136
137   //===--------------------------------------------------------------------===//
138   // Methods for easy access to the global variables in the module.
139   //
140
141   /// getGlobalVariable - Look up the specified global variable in the module
142   /// symbol table.  If it does not exist, return null.  Note that this only
143   /// returns a global variable if it does not have internal linkage.  The type
144   /// argument should be the underlying type of the global, ie, it should not
145   /// have the top-level PointerType, which represents the address of the
146   /// global.
147   ///
148   GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
149
150
151   //===--------------------------------------------------------------------===//
152   // Methods for easy access to the types in the module.
153   //
154
155   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
156   /// there is already an entry for this name, true is returned and the symbol
157   /// table is not modified.
158   ///
159   bool addTypeName(const std::string &Name, const Type *Ty);
160
161   /// getTypeName - If there is at least one entry in the symbol table for the
162   /// specified type, return it.
163   ///
164   std::string getTypeName(const Type *Ty) const;
165
166   /// getTypeByName - Return the type with the specified name in this module, or
167   /// null if there is none by that name.
168   const Type *getTypeByName(const std::string &Name) const;
169
170
171   //===--------------------------------------------------------------------===//
172   // Methods for direct access to the globals list, functions list, and symbol
173   // table.
174   //
175
176   /// Get the underlying elements of the Module...
177   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
178   inline       GlobalListType &getGlobalList()        { return GlobalList; }
179   inline const FunctionListType &getFunctionList() const { return FunctionList;}
180   inline       FunctionListType &getFunctionList()       { return FunctionList;}
181
182   /// getSymbolTable() - Get access to the symbol table for the module, where
183   /// global variables and functions are identified.
184   ///
185   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
186   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
187
188
189   //===--------------------------------------------------------------------===//
190   // Module iterator forwarding functions
191   //
192   // Globals list interface
193   inline giterator                gbegin()       { return GlobalList.begin(); }
194   inline const_giterator          gbegin() const { return GlobalList.begin(); }
195   inline giterator                gend  ()       { return GlobalList.end();   }
196   inline const_giterator          gend  () const { return GlobalList.end();   }
197
198   inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
199   inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
200   inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
201   inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
202
203   inline unsigned                  gsize() const { return GlobalList.size(); }
204   inline bool                     gempty() const { return GlobalList.empty(); }
205   inline const GlobalVariable    &gfront() const { return GlobalList.front(); }
206   inline       GlobalVariable    &gfront()       { return GlobalList.front(); }
207   inline const GlobalVariable     &gback() const { return GlobalList.back(); }
208   inline       GlobalVariable     &gback()       { return GlobalList.back(); }
209
210   // FunctionList interface
211   inline iterator                begin()       { return FunctionList.begin(); }
212   inline const_iterator          begin() const { return FunctionList.begin(); }
213   inline iterator                end  ()       { return FunctionList.end();   }
214   inline const_iterator          end  () const { return FunctionList.end();   }
215
216   inline reverse_iterator       rbegin()       { return FunctionList.rbegin(); }
217   inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
218   inline reverse_iterator       rend  ()       { return FunctionList.rend();   }
219   inline const_reverse_iterator rend  () const { return FunctionList.rend();   }
220
221   inline unsigned                 size() const { return FunctionList.size(); }
222   inline bool                    empty() const { return FunctionList.empty(); }
223   inline const Function         &front() const { return FunctionList.front(); }
224   inline       Function         &front()       { return FunctionList.front(); }
225   inline const Function          &back() const { return FunctionList.back(); }
226   inline       Function          &back()       { return FunctionList.back(); }
227
228   //===--------------------------------------------------------------------===//
229   // List of dependent library access functionsns
230
231   /// @brief Get a constant iterator to beginning of dependent library list.
232   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
233
234   /// @brief Get a constant iterator to end of dependent library list.
235   inline lib_iterator lib_end() const { return LibraryList.end(); }
236
237   /// @brief Returns the number of items in the list of libraries.
238   inline unsigned lib_size() const { return LibraryList.size(); }
239
240   /// @brief Add a library to the list of dependent libraries
241   inline void addLibrary(const std::string& Lib){ LibraryList.push_back(Lib); }
242
243   /// @brief Remove a library from the list of dependent libraries
244   inline void removeLibrary(const std::string& Lib);
245
246   void print(std::ostream &OS) const { print(OS, 0); }
247   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
248
249   void dump() const;
250
251   /// dropAllReferences() - This function causes all the subinstructions to "let
252   /// go" of all references that they are maintaining.  This allows one to
253   /// 'delete' a whole class at a time, even though there may be circular
254   /// references... first all references are dropped, and all use counts go to
255   /// zero.  Then everything is delete'd for real.  Note that no operations are
256   /// valid on an object that has "dropped all references", except operator 
257   /// delete.
258   ///
259   void dropAllReferences();
260 };
261
262 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
263   M->print(O);
264   return O;
265 }
266
267 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
268   M.print(O);
269   return O;
270 }
271
272 } // End llvm namespace
273
274 #endif