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