a2a8e507285ee15f41c7e7c804b3fac97908e882
[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/Value.h"
16 #include "llvm/SymTabValue.h"
17 #include "llvm/ValueHolder.h"
18 class GlobalVariable;
19 class GlobalValueRefMap;   // Used by ConstantVals.cpp
20 class ConstantPointerRef;
21 class FunctionType;
22
23 class Module : public Value, public SymTabValue {
24 public:
25   typedef ValueHolder<GlobalVariable, Module, Module> GlobalListType;
26   typedef ValueHolder<Function, Module, Module> FunctionListType;
27
28   // Global Variable iterators...
29   typedef GlobalListType::iterator                             giterator;
30   typedef GlobalListType::const_iterator                 const_giterator;
31   typedef std::reverse_iterator<giterator>             reverse_giterator;
32   typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
33
34   // Function iterators...
35   typedef FunctionListType::iterator                            iterator;
36   typedef FunctionListType::const_iterator                const_iterator;
37   typedef std::reverse_iterator<iterator>             reverse_iterator;
38   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
39
40 private:
41   GlobalListType GlobalList;     // The Global Variables
42   FunctionListType FunctionList;     // The Functions
43
44   GlobalValueRefMap *GVRefMap;
45
46   // Accessor for the underlying GlobalValRefMap... only through the
47   // ConstantPointerRef class...
48   friend class ConstantPointerRef;
49   void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
50   ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
51
52 public:
53   Module();
54   ~Module();
55
56   // getOrInsertFunction - Look up the specified function in the module symbol
57   // table.  If it does not exist, add a prototype for the function and return
58   // it.
59   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
60
61   // getFunction - Look up the specified function in the module symbol table.
62   // If it does not exist, return null.
63   //
64   Function *getFunction(const std::string &Name, const FunctionType *Ty);
65
66   // Get the underlying elements of the Module...
67   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
68   inline       GlobalListType &getGlobalList()        { return GlobalList; }
69   inline const FunctionListType &getFunctionList() const { return FunctionList;}
70   inline       FunctionListType &getFunctionList()       { return FunctionList;}
71
72   //===--------------------------------------------------------------------===//
73   // Module iterator forwarding functions
74   //
75   inline giterator                gbegin()       { return GlobalList.begin(); }
76   inline const_giterator          gbegin() const { return GlobalList.begin(); }
77   inline giterator                gend  ()       { return GlobalList.end();   }
78   inline const_giterator          gend  () const { return GlobalList.end();   }
79
80   inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
81   inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
82   inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
83   inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
84
85   inline unsigned                  gsize() const { return GlobalList.size(); }
86   inline bool                     gempty() const { return GlobalList.empty(); }
87   inline const GlobalVariable    *gfront() const { return GlobalList.front(); }
88   inline       GlobalVariable    *gfront()       { return GlobalList.front(); }
89   inline const GlobalVariable     *gback() const { return GlobalList.back(); }
90   inline       GlobalVariable     *gback()       { return GlobalList.back(); }
91
92
93
94   inline iterator                begin()       { return FunctionList.begin(); }
95   inline const_iterator          begin() const { return FunctionList.begin(); }
96   inline iterator                end  ()       { return FunctionList.end();   }
97   inline const_iterator          end  () const { return FunctionList.end();   }
98
99   inline reverse_iterator       rbegin()       { return FunctionList.rbegin(); }
100   inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
101   inline reverse_iterator       rend  ()       { return FunctionList.rend();   }
102   inline const_reverse_iterator rend  () const { return FunctionList.rend();   }
103
104   inline unsigned                 size() const { return FunctionList.size(); }
105   inline bool                    empty() const { return FunctionList.empty(); }
106   inline const Function         *front() const { return FunctionList.front(); }
107   inline       Function         *front()       { return FunctionList.front(); }
108   inline const Function          *back() const { return FunctionList.back(); }
109   inline       Function          *back()       { return FunctionList.back(); }
110
111   // Methods for support type inquiry through isa, cast, and dyn_cast:
112   static inline bool classof(const Module *T) { return true; }
113   static inline bool classof(const Value *V) {
114     return V->getValueType() == Value::ModuleVal;
115   }
116
117   // dropAllReferences() - This function causes all the subinstructions to "let
118   // go" of all references that they are maintaining.  This allows one to
119   // 'delete' a whole class at a time, even though there may be circular
120   // references... first all references are dropped, and all use counts go to
121   // zero.  Then everything is delete'd for real.  Note that no operations are
122   // valid on an object that has "dropped all references", except operator 
123   // delete.
124   //
125   void dropAllReferences();
126 };
127
128 #endif