5bba530ed064fc3d5d412ac94fa0917aca7a55ca
[oota-llvm.git] / include / llvm / Function.h
1 //===-- llvm/Function.h - Class to represent a single VM function -*- C++ -*-=//
2 //
3 // This file contains the declaration of the Function class, which represents a 
4 // single function/procedure in the VM.
5 //
6 // Note that BasicBlock's in the Function are Value's, because they are
7 // referenced by instructions like calls and can go into virtual function tables
8 // and stuff.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_FUNCTION_H
13 #define LLVM_FUNCTION_H
14
15 #include "llvm/GlobalValue.h"
16 #include "llvm/ValueHolder.h"
17
18 class FunctionType;
19
20 class Function : public GlobalValue {
21 public:
22   typedef ValueHolder<Argument  , Function, Function> ArgumentListType;
23   typedef ValueHolder<BasicBlock, Function, Function> BasicBlocksType;
24
25   // BasicBlock iterators...
26   typedef BasicBlocksType::iterator iterator;
27   typedef BasicBlocksType::const_iterator const_iterator;
28   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
29   typedef std::reverse_iterator<iterator>             reverse_iterator;
30
31 private:
32
33   // Important things that make up a function!
34   BasicBlocksType  BasicBlocks;         // The basic blocks
35   ArgumentListType ArgumentList;        // The formal arguments
36
37   SymbolTable *SymTab, *ParentSymTab;
38   
39   friend class ValueHolder<Function, Module, Module>;
40   void setParent(Module *parent);
41
42 public:
43   Function(const FunctionType *Ty, bool isInternal, const std::string &N = "");
44   ~Function();
45
46   // Specialize setName to handle symbol table majik...
47   virtual void setName(const std::string &name, SymbolTable *ST = 0);
48
49   const Type *getReturnType() const;           // Return the type of the ret val
50   const FunctionType *getFunctionType() const; // Return the FunctionType for me
51
52   // Is the body of this function unknown? (the basic block list is empty if so)
53   // this is true for external functions, defined as forward "declare"ations
54   bool isExternal() const { return BasicBlocks.empty(); }
55
56   // Get the underlying elements of the Function... both the argument list and
57   // basic block list are empty for external functions.
58   //
59   inline const ArgumentListType &getArgumentList() const{ return ArgumentList; }
60   inline       ArgumentListType &getArgumentList()      { return ArgumentList; }
61
62   inline const BasicBlocksType  &getBasicBlocks() const { return BasicBlocks; }
63   inline       BasicBlocksType  &getBasicBlocks()       { return BasicBlocks; }
64
65   inline const BasicBlock       *getEntryNode() const   { return front(); }
66   inline       BasicBlock       *getEntryNode()         { return front(); }
67
68   //===--------------------------------------------------------------------===//
69   // Symbol Table Accessing functions...
70
71   // hasSymbolTable() - Returns true if there is a symbol table allocated to
72   // this object AND if there is at least one name in it!
73   //
74   bool hasSymbolTable() const;
75
76   // CAUTION: The current symbol table may be null if there are no names (ie, 
77   // the symbol table is empty) 
78   //
79   inline       SymbolTable *getSymbolTable()       { return SymTab; }
80   inline const SymbolTable *getSymbolTable() const { return SymTab; }
81
82   // getSymbolTableSure is guaranteed to not return a null pointer, because if
83   // the function does not already have a symtab, one is created.  Use this if
84   // you intend to put something into the symbol table for the function.
85   //
86   SymbolTable *getSymbolTableSure();  // Implemented in Value.cpp
87
88   
89   //===--------------------------------------------------------------------===//
90   // BasicBlock iterator forwarding functions
91   //
92   inline iterator                begin()       { return BasicBlocks.begin(); }
93   inline const_iterator          begin() const { return BasicBlocks.begin(); }
94   inline iterator                end  ()       { return BasicBlocks.end();   }
95   inline const_iterator          end  () const { return BasicBlocks.end();   }
96
97   inline reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
98   inline const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
99   inline reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
100   inline const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
101
102   inline unsigned                 size() const { return BasicBlocks.size(); }
103   inline bool                    empty() const { return BasicBlocks.empty(); }
104   inline const BasicBlock       *front() const { return BasicBlocks.front(); }
105   inline       BasicBlock       *front()       { return BasicBlocks.front(); }
106   inline const BasicBlock        *back() const { return BasicBlocks.back(); }
107   inline       BasicBlock        *back()       { return BasicBlocks.back(); }
108
109   virtual void print(std::ostream &OS) const;
110
111   // Methods for support type inquiry through isa, cast, and dyn_cast:
112   static inline bool classof(const Function *) { return true; }
113   static inline bool classof(const Value *V) {
114     return V->getValueType() == Value::FunctionVal;
115   }
116
117   // dropAllReferences() - This function causes all the subinstructions to "let
118   // go" of all references that they are maintaining.  This allows one to
119   // 'delete' a whole class at a time, even though there may be circular
120   // references... first all references are dropped, and all use counts go to
121   // zero.  Then everything is delete'd for real.  Note that no operations are
122   // valid on an object that has "dropped all references", except operator 
123   // delete.
124   //
125   void dropAllReferences();
126 };
127
128 #endif