7448dce9fe3a3ec912cf140984825943ab10fabc
[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 <list>
17
18 class Instruction;
19 class BasicBlock;
20 class MethodArgument;
21 class MethodType;
22 class Method;
23 class Module;
24
25 typedef UseTy<Method> MethodUse;
26
27 class Method : public SymTabValue {
28 public:
29   typedef ValueHolder<MethodArgument, Method> ArgumentListType;
30   typedef ValueHolder<BasicBlock    , Method> BasicBlocksType;
31 private:
32
33   // Important things that make up a method!
34   BasicBlocksType  BasicBlocks;    // The basic blocks
35   ArgumentListType ArgumentList;   // The formal arguments
36
37   Module *Parent;                  // The module that contains this method
38
39   friend class ValueHolder<Method,Module>;
40   void setParent(Module *parent);
41
42 public:
43   Method(const MethodType *Ty, const string &Name = "");
44   ~Method();
45
46   // Specialize setName to handle symbol table majik...
47   virtual void setName(const string &name);
48
49   const Type *getReturnType() const;
50   const MethodType *getMethodType() const;
51
52   // Is the body of this method unknown? (the basic block list is empty if so)
53   // this is true for "extern"al methods.
54   bool isMethodExternal() const { return BasicBlocks.empty(); }
55
56
57   // Get the class structure that this method is contained inside of...
58   inline Module *getParent() { return Parent; }
59   inline const Module *getParent() const { return Parent; }
60
61   inline const BasicBlocksType  &getBasicBlocks() const { return BasicBlocks; }
62   inline       BasicBlocksType  &getBasicBlocks()       { return BasicBlocks; }
63
64   inline const ArgumentListType &getArgumentList() const{ return ArgumentList; }
65   inline       ArgumentListType &getArgumentList()      { return ArgumentList; }
66
67
68   // dropAllReferences() - This function causes all the subinstructions to "let
69   // go" of all references that they are maintaining.  This allows one to
70   // 'delete' a whole class at a time, even though there may be circular
71   // references... first all references are dropped, and all use counts go to
72   // zero.  Then everything is delete'd for real.  Note that no operations are
73   // valid on an object that has "dropped all references", except operator 
74   // delete.
75   //
76   void dropAllReferences();
77
78   //===--------------------------------------------------------------------===//
79   // Method Instruction iterator code
80   //===--------------------------------------------------------------------===//
81   // 
82   template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t> 
83   class InstIterator;
84   typedef InstIterator<BasicBlocksType, BasicBlocksType::iterator, 
85                        BasicBlock::InstListType::iterator,
86                        Instruction*> inst_iterator;
87   typedef InstIterator<const BasicBlocksType, BasicBlocksType::const_iterator, 
88                        BasicBlock::InstListType::const_iterator,
89                        const Instruction*> inst_const_iterator;
90
91   // This inner class is used to implement inst_begin() & inst_end() for
92   // inst_iterator and inst_const_iterator's.
93   //
94   template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
95   class InstIterator {
96     typedef _BB_t   BBty;
97     typedef _BB_i_t BBIty;
98     typedef _BI_t   BIty;
99     typedef _II_t   IIty;
100     _BB_t  &BBs;      // BasicBlocksType
101     _BB_i_t BB;       // BasicBlocksType::iterator
102     _BI_t   BI;       // BasicBlock::InstListType::iterator
103   public:
104     typedef bidirectional_iterator_tag iterator_category;
105
106     template<class M> InstIterator(M &m) 
107       : BBs(m.getBasicBlocks()), BB(BBs.begin()) {    // begin ctor
108       if (BB != BBs.end()) {
109         BI = (*BB)->getInstList().begin();
110         resyncInstructionIterator();
111       }
112     }
113
114     template<class M> InstIterator(M &m, bool) 
115       : BBs(m.getBasicBlocks()), BB(BBs.end()) {    // end ctor
116     }
117
118     // Accessors to get at the underlying iterators...
119     inline BBIty &getBasicBlockIterator()  { return BB; }
120     inline BIty  &getInstructionIterator() { return BI; }
121
122     inline IIty operator*()  const { return *BI; }
123     inline IIty *operator->() const { return &(operator*()); }
124
125     inline bool operator==(const InstIterator &y) const { 
126       return BB == y.BB && (BI == y.BI || BB == BBs.end());
127     }
128     inline bool operator!=(const InstIterator& y) const { 
129       return !operator==(y);
130     }
131
132     // resyncInstructionIterator - This should be called if the 
133     // InstructionIterator is modified outside of our control.  This resynchs
134     // the internals of the InstIterator to a consistent state.
135     //
136     inline void resyncInstructionIterator() {
137       // The only way that the II could be broken is if it is now pointing to
138       // the end() of the current BasicBlock and there are successor BBs.
139       while (BI == (*BB)->getInstList().end()) {
140         ++BB; 
141         if (BB == BBs.end()) break;
142         BI = (*BB)->getInstList().begin();
143       }
144     }
145
146     InstIterator& operator++() { 
147       ++BI;
148       resyncInstructionIterator();   // Make sure it is still valid.
149       return *this; 
150     }
151     inline InstIterator operator++(int) { 
152       InstIterator tmp = *this; ++*this; return tmp; 
153     }
154     
155     InstIterator& operator--() { 
156       while (BB == BBs.end() || BI == (*BB)->getInstList().begin()) {
157         --BB;
158         BI = (*BB)->getInstList().end();
159       }
160       --BI;
161       return *this; 
162     }
163     inline InstIterator  operator--(int) { 
164       InstIterator tmp = *this; --*this; return tmp; 
165     }
166   };
167
168   inline inst_iterator inst_begin() { return inst_iterator(*this); }
169   inline inst_iterator inst_end()   { return inst_iterator(*this, true); }
170   inline inst_const_iterator inst_begin() const { return inst_const_iterator(*this); }
171   inline inst_const_iterator inst_end()   const { return inst_const_iterator(*this, true); }
172 };
173
174 #endif