1 //===-- llvm/Method.h - Class to represent a single VM method ----*- C++ -*--=//
3 // This file contains the declaration of the Method class, which represents a
4 // single Method/function/procedure in the VM.
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.
9 //===----------------------------------------------------------------------===//
14 #include "llvm/SymTabValue.h"
15 #include "llvm/BasicBlock.h"
24 class Method : public Value, public SymTabValue {
26 typedef ValueHolder<MethodArgument, Method, Method> ArgumentListType;
27 typedef ValueHolder<BasicBlock , Method, Method> BasicBlocksType;
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;
37 // Important things that make up a method!
38 BasicBlocksType BasicBlocks; // The basic blocks
39 ArgumentListType ArgumentList; // The formal arguments
41 Module *Parent; // The module that contains this method
43 friend class ValueHolder<Method, Module, Module>;
44 void setParent(Module *parent);
47 Method(const MethodType *Ty, const string &Name = "");
50 // Specialize setName to handle symbol table majik...
51 virtual void setName(const string &name, SymbolTable *ST = 0);
53 const Type *getReturnType() const;
54 const MethodType *getType() const {
55 return (const MethodType*)Value::getType();
58 // Is the body of this method unknown? (the basic block list is empty if so)
59 // this is true for external methods, defined as forward "declare"ations
60 bool isExternal() const { return BasicBlocks.empty(); }
63 // Get the class structure that this method is contained inside of...
64 inline Module *getParent() { return Parent; }
65 inline const Module *getParent() const { return Parent; }
67 // Get the underlying elements of the Method...
68 inline const ArgumentListType &getArgumentList() const{ return ArgumentList; }
69 inline ArgumentListType &getArgumentList() { return ArgumentList; }
71 inline const BasicBlocksType &getBasicBlocks() const { return BasicBlocks; }
72 inline BasicBlocksType &getBasicBlocks() { return BasicBlocks; }
75 //===--------------------------------------------------------------------===//
76 // BasicBlock iterator forwarding functions
78 inline iterator begin() { return BasicBlocks.begin(); }
79 inline const_iterator begin() const { return BasicBlocks.begin(); }
80 inline iterator end () { return BasicBlocks.end(); }
81 inline const_iterator end () const { return BasicBlocks.end(); }
83 inline reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
84 inline const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
85 inline reverse_iterator rend () { return BasicBlocks.rend(); }
86 inline const_reverse_iterator rend () const { return BasicBlocks.rend(); }
88 inline unsigned size() const { return BasicBlocks.size(); }
89 inline bool empty() const { return BasicBlocks.empty(); }
90 inline const BasicBlock *front() const { return BasicBlocks.front(); }
91 inline BasicBlock *front() { return BasicBlocks.front(); }
92 inline const BasicBlock *back() const { return BasicBlocks.back(); }
93 inline BasicBlock *back() { return BasicBlocks.back(); }
97 // dropAllReferences() - This function causes all the subinstructions to "let
98 // go" of all references that they are maintaining. This allows one to
99 // 'delete' a whole class at a time, even though there may be circular
100 // references... first all references are dropped, and all use counts go to
101 // zero. Then everything is delete'd for real. Note that no operations are
102 // valid on an object that has "dropped all references", except operator
105 void dropAllReferences();
107 //===--------------------------------------------------------------------===//
108 // Method Instruction iterator code
109 //===--------------------------------------------------------------------===//
111 template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
113 typedef InstIterator<BasicBlocksType, iterator,
114 BasicBlock::iterator, Instruction*> inst_iterator;
115 typedef InstIterator<const BasicBlocksType, const_iterator,
116 BasicBlock::const_iterator,
117 const Instruction*> inst_const_iterator;
119 // This inner class is used to implement inst_begin() & inst_end() for
120 // inst_iterator and inst_const_iterator's.
122 template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
125 typedef _BB_i_t BBIty;
128 _BB_t &BBs; // BasicBlocksType
129 _BB_i_t BB; // BasicBlocksType::iterator
130 _BI_t BI; // BasicBlock::iterator
132 typedef bidirectional_iterator_tag iterator_category;
134 template<class M> InstIterator(M &m)
135 : BBs(m.getBasicBlocks()), BB(BBs.begin()) { // begin ctor
136 if (BB != BBs.end()) {
138 resyncInstructionIterator();
142 template<class M> InstIterator(M &m, bool)
143 : BBs(m.getBasicBlocks()), BB(BBs.end()) { // end ctor
146 // Accessors to get at the underlying iterators...
147 inline BBIty &getBasicBlockIterator() { return BB; }
148 inline BIty &getInstructionIterator() { return BI; }
150 inline IIty operator*() const { return *BI; }
151 inline IIty operator->() const { return operator*(); }
153 inline bool operator==(const InstIterator &y) const {
154 return BB == y.BB && (BI == y.BI || BB == BBs.end());
156 inline bool operator!=(const InstIterator& y) const {
157 return !operator==(y);
160 // resyncInstructionIterator - This should be called if the
161 // InstructionIterator is modified outside of our control. This resynchs
162 // the internals of the InstIterator to a consistent state.
164 inline void resyncInstructionIterator() {
165 // The only way that the II could be broken is if it is now pointing to
166 // the end() of the current BasicBlock and there are successor BBs.
167 while (BI == (*BB)->end()) {
169 if (BB == BBs.end()) break;
174 InstIterator& operator++() {
176 resyncInstructionIterator(); // Make sure it is still valid.
179 inline InstIterator operator++(int) {
180 InstIterator tmp = *this; ++*this; return tmp;
183 InstIterator& operator--() {
184 while (BB == BBs.end() || BI == (*BB)->begin()) {
191 inline InstIterator operator--(int) {
192 InstIterator tmp = *this; --*this; return tmp;
195 inline bool atEnd() const { return BB == BBs.end(); }
198 inline inst_iterator inst_begin() { return inst_iterator(*this); }
199 inline inst_iterator inst_end() { return inst_iterator(*this, true); }
200 inline inst_const_iterator inst_begin() const { return inst_const_iterator(*this); }
201 inline inst_const_iterator inst_end() const { return inst_const_iterator(*this, true); }