* Both Method & GlobalVariable now subclass GlobalValue
[oota-llvm.git] / include / llvm / Function.h
1 //===-- llvm/Method.h - Class to represent a single VM method ----*- C++ -*--=//
2 //
3 // This file contains the declaration of the Method class, which represents a 
4 // single Method/function/procedure in the VM.
5 //
6 // Note that basic blocks themselves are Def's, because they are referenced
7 // by instructions like calls and can go in virtual function tables and stuff.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_METHOD_H
12 #define LLVM_METHOD_H
13
14 #include "llvm/SymTabValue.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/GlobalValue.h"
17
18 class Instruction;
19 class BasicBlock;
20 class MethodArgument;
21 class MethodType;
22 class Module;
23
24 class Method : public GlobalValue, public SymTabValue {
25 public:
26   typedef ValueHolder<MethodArgument, Method, Method> ArgumentListType;
27   typedef ValueHolder<BasicBlock    , Method, Method> BasicBlocksType;
28
29   // BasicBlock iterators...
30   typedef BasicBlocksType::iterator iterator;
31   typedef BasicBlocksType::const_iterator const_iterator;
32   typedef reverse_iterator<const_iterator> const_reverse_iterator;
33   typedef reverse_iterator<iterator>             reverse_iterator;
34
35 private:
36
37   // Important things that make up a method!
38   BasicBlocksType  BasicBlocks;    // The basic blocks
39   ArgumentListType ArgumentList;   // The formal arguments
40
41   Module *Parent;                  // The module that contains this method
42
43   friend class ValueHolder<Method, Module, Module>;
44   void setParent(Module *parent);
45
46 public:
47   Method(const MethodType *Ty, const string &Name = "");
48   ~Method();
49
50   // Specialize setName to handle symbol table majik...
51   virtual void setName(const string &name, SymbolTable *ST = 0);
52
53   const Type *getReturnType() const;        // Return the return type of method
54   const MethodType *getMethodType() const;  // Return the MethodType for me
55
56   // Is the body of this method unknown? (the basic block list is empty if so)
57   // this is true for external methods, defined as forward "declare"ations
58   bool isExternal() const { return BasicBlocks.empty(); }
59
60
61   // Get the class structure that this method is contained inside of...
62   inline Module *getParent() { return Parent; }
63   inline const Module *getParent() const { return Parent; }
64
65   // Get the underlying elements of the Method...
66   inline const ArgumentListType &getArgumentList() const{ return ArgumentList; }
67   inline       ArgumentListType &getArgumentList()      { return ArgumentList; }
68
69   inline const BasicBlocksType  &getBasicBlocks() const { return BasicBlocks; }
70   inline       BasicBlocksType  &getBasicBlocks()       { return BasicBlocks; }
71
72
73   //===--------------------------------------------------------------------===//
74   // BasicBlock iterator forwarding functions
75   //
76   inline iterator                begin()       { return BasicBlocks.begin(); }
77   inline const_iterator          begin() const { return BasicBlocks.begin(); }
78   inline iterator                end  ()       { return BasicBlocks.end();   }
79   inline const_iterator          end  () const { return BasicBlocks.end();   }
80
81   inline reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
82   inline const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
83   inline reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
84   inline const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
85
86   inline unsigned                 size() const { return BasicBlocks.size(); }
87   inline bool                    empty() const { return BasicBlocks.empty(); }
88   inline const BasicBlock       *front() const { return BasicBlocks.front(); }
89   inline       BasicBlock       *front()       { return BasicBlocks.front(); }
90   inline const BasicBlock        *back() const { return BasicBlocks.back(); }
91   inline       BasicBlock        *back()       { return BasicBlocks.back(); }
92
93
94   // Methods for support type inquiry through isa, cast, and dyn_cast:
95   static inline bool classof(const Method *T) { return true; }
96   static inline bool classof(const Value *V) {
97     return V->getValueType() == Value::MethodVal;
98   }
99
100   // dropAllReferences() - This function causes all the subinstructions to "let
101   // go" of all references that they are maintaining.  This allows one to
102   // 'delete' a whole class at a time, even though there may be circular
103   // references... first all references are dropped, and all use counts go to
104   // zero.  Then everything is delete'd for real.  Note that no operations are
105   // valid on an object that has "dropped all references", except operator 
106   // delete.
107   //
108   void dropAllReferences();
109
110   //===--------------------------------------------------------------------===//
111   // Method Instruction iterator code
112   //===--------------------------------------------------------------------===//
113   // 
114   template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t> 
115   class InstIterator;
116   typedef InstIterator<BasicBlocksType, iterator, 
117                        BasicBlock::iterator, Instruction*> inst_iterator;
118   typedef InstIterator<const BasicBlocksType, const_iterator, 
119                        BasicBlock::const_iterator,
120                        const Instruction*> inst_const_iterator;
121
122   // This inner class is used to implement inst_begin() & inst_end() for
123   // inst_iterator and inst_const_iterator's.
124   //
125   template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
126   class InstIterator {
127     typedef _BB_t   BBty;
128     typedef _BB_i_t BBIty;
129     typedef _BI_t   BIty;
130     typedef _II_t   IIty;
131     _BB_t  &BBs;      // BasicBlocksType
132     _BB_i_t BB;       // BasicBlocksType::iterator
133     _BI_t   BI;       // BasicBlock::iterator
134   public:
135     typedef bidirectional_iterator_tag iterator_category;
136
137     template<class M> InstIterator(M &m) 
138       : BBs(m.getBasicBlocks()), BB(BBs.begin()) {    // begin ctor
139       if (BB != BBs.end()) {
140         BI = (*BB)->begin();
141         resyncInstructionIterator();
142       }
143     }
144
145     template<class M> InstIterator(M &m, bool) 
146       : BBs(m.getBasicBlocks()), BB(BBs.end()) {    // end ctor
147     }
148
149     // Accessors to get at the underlying iterators...
150     inline BBIty &getBasicBlockIterator()  { return BB; }
151     inline BIty  &getInstructionIterator() { return BI; }
152
153     inline IIty operator*()  const { return *BI; }
154     inline IIty operator->() const { return operator*(); }
155
156     inline bool operator==(const InstIterator &y) const { 
157       return BB == y.BB && (BI == y.BI || BB == BBs.end());
158     }
159     inline bool operator!=(const InstIterator& y) const { 
160       return !operator==(y);
161     }
162
163     // resyncInstructionIterator - This should be called if the 
164     // InstructionIterator is modified outside of our control.  This resynchs
165     // the internals of the InstIterator to a consistent state.
166     //
167     inline void resyncInstructionIterator() {
168       // The only way that the II could be broken is if it is now pointing to
169       // the end() of the current BasicBlock and there are successor BBs.
170       while (BI == (*BB)->end()) {
171         ++BB;
172         if (BB == BBs.end()) break;
173         BI = (*BB)->begin();
174       }
175     }
176
177     InstIterator& operator++() { 
178       ++BI;
179       resyncInstructionIterator();   // Make sure it is still valid.
180       return *this; 
181     }
182     inline InstIterator operator++(int) { 
183       InstIterator tmp = *this; ++*this; return tmp; 
184     }
185     
186     InstIterator& operator--() { 
187       while (BB == BBs.end() || BI == (*BB)->begin()) {
188         --BB;
189         BI = (*BB)->end();
190       }
191       --BI;
192       return *this; 
193     }
194     inline InstIterator  operator--(int) { 
195       InstIterator tmp = *this; --*this; return tmp; 
196     }
197
198     inline bool atEnd() const { return BB == BBs.end(); }
199   };
200
201   inline inst_iterator inst_begin() { return inst_iterator(*this); }
202   inline inst_iterator inst_end()   { return inst_iterator(*this, true); }
203   inline inst_const_iterator inst_begin() const { return inst_const_iterator(*this); }
204   inline inst_const_iterator inst_end()   const { return inst_const_iterator(*this, true); }
205 };
206
207 // Provide specializations of GraphTraits to be able to treat a method as a 
208 // graph of basic blocks... these are the same as the basic block iterators,
209 // except that the root node is implicitly the first node of the method.
210 //
211 template <> struct GraphTraits<Method*> : public GraphTraits<BasicBlock*> {
212   static NodeType *getEntryNode(Method *M) { return M->front(); }
213 };
214 template <> struct GraphTraits<const Method*> :
215   public GraphTraits<const BasicBlock*> {
216   static NodeType *getEntryNode(const Method *M) { return M->front(); }
217 };
218
219 // Provide specializations of GraphTraits to be able to treat a method as a 
220 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
221 // a method is considered to be when traversing the predecessor edges of a BB
222 // instead of the successor edges.
223 //
224 template <> struct GraphTraits<Inverse<Method*> > :
225   public GraphTraits<Inverse<BasicBlock*> > {
226   static NodeType *getEntryNode(Inverse<Method *> G) { return G.Graph->front();}
227 };
228 template <> struct GraphTraits<Inverse<const Method*> > :
229   public GraphTraits<Inverse<const BasicBlock*> > {
230   static NodeType *getEntryNode(Inverse<const Method *> G) {
231     return G.Graph->front();
232   }
233 };
234
235 #endif