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"
25 typedef UseTy<Method> MethodUse;
27 class Method : public SymTabValue {
29 typedef ValueHolder<MethodArgument, Method> ArgumentListType;
30 typedef ValueHolder<BasicBlock , Method> BasicBlocksType;
33 // Important things that make up a method!
34 BasicBlocksType BasicBlocks; // The basic blocks
35 ArgumentListType ArgumentList; // The formal arguments
37 Module *Parent; // The module that contains this method
39 friend class ValueHolder<Method,Module>;
40 void setParent(Module *parent);
43 Method(const MethodType *Ty, const string &Name = "");
46 // Specialize setName to handle symbol table majik...
47 virtual void setName(const string &name);
49 const Type *getReturnType() const;
50 const MethodType *getMethodType() const;
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(); }
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; }
61 inline const BasicBlocksType &getBasicBlocks() const { return BasicBlocks; }
62 inline BasicBlocksType &getBasicBlocks() { return BasicBlocks; }
64 inline const ArgumentListType &getArgumentList() const{ return ArgumentList; }
65 inline ArgumentListType &getArgumentList() { return ArgumentList; }
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
76 void dropAllReferences();
78 //===--------------------------------------------------------------------===//
79 // Method Instruction iterator code
80 //===--------------------------------------------------------------------===//
82 template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
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;
91 // This inner class is used to implement inst_begin() & inst_end() for
92 // inst_iterator and inst_const_iterator's.
94 template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
97 typedef _BB_i_t BBIty;
100 _BB_t &BBs; // BasicBlocksType
101 _BB_i_t BB; // BasicBlocksType::iterator
102 _BI_t BI; // BasicBlock::InstListType::iterator
104 typedef bidirectional_iterator_tag iterator_category;
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();
114 template<class M> InstIterator(M &m, bool)
115 : BBs(m.getBasicBlocks()), BB(BBs.end()) { // end ctor
118 // Accessors to get at the underlying iterators...
119 inline BBIty &getBasicBlockIterator() { return BB; }
120 inline BIty &getInstructionIterator() { return BI; }
122 inline IIty operator*() const { return *BI; }
123 inline IIty *operator->() const { return &(operator*()); }
125 inline bool operator==(const InstIterator &y) const {
126 return BB == y.BB && (BI == y.BI || BB == BBs.end());
128 inline bool operator!=(const InstIterator& y) const {
129 return !operator==(y);
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.
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()) {
141 if (BB == BBs.end()) break;
142 BI = (*BB)->getInstList().begin();
146 InstIterator& operator++() {
148 resyncInstructionIterator(); // Make sure it is still valid.
151 inline InstIterator operator++(int) {
152 InstIterator tmp = *this; ++*this; return tmp;
155 InstIterator& operator--() {
156 while (BB == BBs.end() || BI == (*BB)->getInstList().begin()) {
158 BI = (*BB)->getInstList().end();
163 inline InstIterator operator--(int) {
164 InstIterator tmp = *this; --*this; return tmp;
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); }