28e8b868c83fc42d05f8ebade2ac6a93f1a2475a
[oota-llvm.git] / include / llvm / Module.h
1 //===-- llvm/Module.h - C++ class to represent a VM module -------*- C++ -*--=//
2 //
3 // This file contains the declarations for the Module class that is used to 
4 // maintain all the information related to a VM module.
5 //
6 // A module also maintains a GlobalValRefMap object that is used to hold all
7 // constant references to global variables in the module.  When a global
8 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_MODULE_H
13 #define LLVM_MODULE_H
14
15 #include "llvm/Function.h"
16 #include "llvm/GlobalVariable.h"
17 class GlobalVariable;
18 class GlobalValueRefMap;   // Used by ConstantVals.cpp
19 class ConstantPointerRef;
20 class FunctionType;
21 class SymbolTable;
22
23 template<> struct ilist_traits<Function>
24   : public SymbolTableListTraits<Function, Module, Module> {
25   // createNode is used to create a node that marks the end of the list...
26   static Function *createNode();
27   static iplist<Function> &getList(Module *M);
28 };
29 template<> struct ilist_traits<GlobalVariable>
30   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
31   // createNode is used to create a node that marks the end of the list...
32   static GlobalVariable *createNode();
33   static iplist<GlobalVariable> &getList(Module *M);
34 };
35
36 struct Module : public Annotable {
37   typedef iplist<GlobalVariable> GlobalListType;
38   typedef iplist<Function> FunctionListType;
39
40   // Global Variable iterators...
41   typedef GlobalListType::iterator                             giterator;
42   typedef GlobalListType::const_iterator                 const_giterator;
43   typedef std::reverse_iterator<giterator>             reverse_giterator;
44   typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
45
46   // Function iterators...
47   typedef FunctionListType::iterator                          iterator;
48   typedef FunctionListType::const_iterator              const_iterator;
49   typedef std::reverse_iterator<iterator>             reverse_iterator;
50   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
51
52   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
53   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
54
55 private:
56   GlobalListType GlobalList;     // The Global Variables in the module
57   FunctionListType FunctionList; // The Functions in the module
58   GlobalValueRefMap *GVRefMap;   // Keep track of GlobalValueRef's
59   SymbolTable *SymTab;           // Symbol Table for the module
60   std::string ModuleID;    // Human readable identifier for the module
61
62   // These flags are probably not the right long-term way to handle this kind of
63   // target information, but it is sufficient for now.
64   Endianness  Endian;       // True if target is little endian
65   PointerSize PtrSize;    // True if target has 32-bit pointers (false = 64-bit)
66
67   // Accessor for the underlying GVRefMap... only through the Constant class...
68   friend class Constant;
69   friend class ConstantPointerRef;
70   void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
71   ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
72   void destroyConstantPointerRef(ConstantPointerRef *CPR);
73
74 public:
75   Module(const std::string &ModuleID);
76   ~Module();
77
78   const std::string &getModuleIdentifier() const { return ModuleID; }
79
80   /// Target endian information...
81   Endianness getEndianness() const { return Endian; }
82   void setEndianness(Endianness E) { Endian = E; }
83
84   /// Target Pointer Size information...
85   PointerSize getPointerSize() const { return PtrSize; }
86   void setPointerSize(PointerSize PS) { PtrSize = PS; }
87
88   /// getOrInsertFunction - Look up the specified function in the module symbol
89   /// table.  If it does not exist, add a prototype for the function and return
90   /// it.
91   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
92
93   /// getFunction - Look up the specified function in the module symbol table.
94   /// If it does not exist, return null.
95   ///
96   Function *getFunction(const std::string &Name, const FunctionType *Ty);
97
98   /// getMainFunction - This function looks up main efficiently.  This is such a
99   /// common case, that it is a method in Module.  If main cannot be found, a
100   /// null pointer is returned.
101   ///
102   Function *getMainFunction();
103
104   /// getNamedFunction - Return the first function in the module with the
105   /// specified name, of arbitrary type.  This method returns null if a function
106   /// with the specified name is not found.
107   ///
108   Function *getNamedFunction(const std::string &Name);
109
110   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
111   /// there is already an entry for this name, true is returned and the symbol
112   /// table is not modified.
113   ///
114   bool addTypeName(const std::string &Name, const Type *Ty);
115
116   /// getTypeName - If there is at least one entry in the symbol table for the
117   /// specified type, return it.
118   ///
119   std::string getTypeName(const Type *Ty);
120
121   /// Get the underlying elements of the Module...
122   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
123   inline       GlobalListType &getGlobalList()        { return GlobalList; }
124   inline const FunctionListType &getFunctionList() const { return FunctionList;}
125   inline       FunctionListType &getFunctionList()       { return FunctionList;}
126
127
128   //===--------------------------------------------------------------------===//
129   // Symbol table support functions...
130   
131   /// getSymbolTable() - Get access to the symbol table for the module, where
132   /// global variables and functions are identified.
133   ///
134   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
135   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
136
137
138   //===--------------------------------------------------------------------===//
139   // Module iterator forwarding functions
140   //
141   inline giterator                gbegin()       { return GlobalList.begin(); }
142   inline const_giterator          gbegin() const { return GlobalList.begin(); }
143   inline giterator                gend  ()       { return GlobalList.end();   }
144   inline const_giterator          gend  () const { return GlobalList.end();   }
145
146   inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
147   inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
148   inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
149   inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
150
151   inline unsigned                  gsize() const { return GlobalList.size(); }
152   inline bool                     gempty() const { return GlobalList.empty(); }
153   inline const GlobalVariable    &gfront() const { return GlobalList.front(); }
154   inline       GlobalVariable    &gfront()       { return GlobalList.front(); }
155   inline const GlobalVariable     &gback() const { return GlobalList.back(); }
156   inline       GlobalVariable     &gback()       { return GlobalList.back(); }
157
158
159
160   inline iterator                begin()       { return FunctionList.begin(); }
161   inline const_iterator          begin() const { return FunctionList.begin(); }
162   inline iterator                end  ()       { return FunctionList.end();   }
163   inline const_iterator          end  () const { return FunctionList.end();   }
164
165   inline reverse_iterator       rbegin()       { return FunctionList.rbegin(); }
166   inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
167   inline reverse_iterator       rend  ()       { return FunctionList.rend();   }
168   inline const_reverse_iterator rend  () const { return FunctionList.rend();   }
169
170   inline unsigned                 size() const { return FunctionList.size(); }
171   inline bool                    empty() const { return FunctionList.empty(); }
172   inline const Function         &front() const { return FunctionList.front(); }
173   inline       Function         &front()       { return FunctionList.front(); }
174   inline const Function          &back() const { return FunctionList.back(); }
175   inline       Function          &back()       { return FunctionList.back(); }
176
177   void print(std::ostream &OS) const;
178   void dump() const;
179
180   /// dropAllReferences() - This function causes all the subinstructions to "let
181   /// go" of all references that they are maintaining.  This allows one to
182   /// 'delete' a whole class at a time, even though there may be circular
183   /// references... first all references are dropped, and all use counts go to
184   /// zero.  Then everything is delete'd for real.  Note that no operations are
185   /// valid on an object that has "dropped all references", except operator 
186   /// delete.
187   ///
188   void dropAllReferences();
189 };
190
191 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
192   M->print(O);
193   return O;
194 }
195
196 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
197   M.print(O);
198   return O;
199 }
200
201 #endif