Function.h revision 9aa4142dad5316aed1ee1014b6bc1977092ce9fb
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/GlobalValue.h"
16#include "llvm/ValueHolder.h"
17
18class Instruction;
19class BasicBlock;
20class MethodArgument;
21class MethodType;
22class Module;
23
24class Method : public GlobalValue, public SymTabValue {
25public:
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 std::reverse_iterator<const_iterator> const_reverse_iterator;
33  typedef std::reverse_iterator<iterator>             reverse_iterator;
34
35private:
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
44public:
45  Method(const MethodType *Ty, bool isInternal, const std::string &Name = "");
46  ~Method();
47
48  // Specialize setName to handle symbol table majik...
49  virtual void setName(const std::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  // Get the underlying elements of the Method... both the argument list and
59  // basic block list are empty for external methods.
60  //
61  inline const ArgumentListType &getArgumentList() const{ return ArgumentList; }
62  inline       ArgumentListType &getArgumentList()      { return ArgumentList; }
63
64  inline const BasicBlocksType  &getBasicBlocks() const { return BasicBlocks; }
65  inline       BasicBlocksType  &getBasicBlocks()       { return BasicBlocks; }
66
67  inline const BasicBlock       *getEntryNode() const   { return front(); }
68  inline       BasicBlock       *getEntryNode()         { return front(); }
69
70  //===--------------------------------------------------------------------===//
71  // BasicBlock iterator forwarding functions
72  //
73  inline iterator                begin()       { return BasicBlocks.begin(); }
74  inline const_iterator          begin() const { return BasicBlocks.begin(); }
75  inline iterator                end  ()       { return BasicBlocks.end();   }
76  inline const_iterator          end  () const { return BasicBlocks.end();   }
77
78  inline reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
79  inline const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
80  inline reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
81  inline const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
82
83  inline unsigned                 size() const { return BasicBlocks.size(); }
84  inline bool                    empty() const { return BasicBlocks.empty(); }
85  inline const BasicBlock       *front() const { return BasicBlocks.front(); }
86  inline       BasicBlock       *front()       { return BasicBlocks.front(); }
87  inline const BasicBlock        *back() const { return BasicBlocks.back(); }
88  inline       BasicBlock        *back()       { return BasicBlocks.back(); }
89
90
91  // Methods for support type inquiry through isa, cast, and dyn_cast:
92  static inline bool classof(const Method *T) { return true; }
93  static inline bool classof(const Value *V) {
94    return V->getValueType() == Value::MethodVal;
95  }
96
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
103  // delete.
104  //
105  void dropAllReferences();
106};
107
108#endif
109