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