66a372e711b3f5002efec32dc34f72e234dde8c7
[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/BasicBlock.h"
17 #include "llvm/Argument.h"
18
19 class FunctionType;
20
21 // Traits for intrusive list of instructions...
22 template<> struct ilist_traits<BasicBlock>
23   : public SymbolTableListTraits<BasicBlock, Function, Function> {
24
25   // createNode is used to create a node that marks the end of the list...
26   static BasicBlock *createNode() { return new BasicBlock(); }
27
28   static iplist<BasicBlock> &getList(Function *F);
29 };
30
31 template<> struct ilist_traits<Argument>
32   : public SymbolTableListTraits<Argument, Function, Function> {
33
34   // createNode is used to create a node that marks the end of the list...
35   static Argument *createNode();
36   static iplist<Argument> &getList(Function *F);
37 };
38
39 class Function : public GlobalValue {
40 public:
41   typedef iplist<Argument> ArgumentListType;
42   typedef iplist<BasicBlock> BasicBlockListType;
43
44   // BasicBlock iterators...
45   typedef BasicBlockListType::iterator iterator;
46   typedef BasicBlockListType::const_iterator const_iterator;
47   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
48   typedef std::reverse_iterator<iterator>             reverse_iterator;
49
50   typedef ArgumentListType::iterator aiterator;
51   typedef ArgumentListType::const_iterator const_aiterator;
52   typedef std::reverse_iterator<const_aiterator> const_reverse_aiterator;
53   typedef std::reverse_iterator<aiterator>             reverse_aiterator;
54
55 private:
56
57   // Important things that make up a function!
58   BasicBlockListType  BasicBlocks;         // The basic blocks
59   ArgumentListType ArgumentList;        // The formal arguments
60
61   SymbolTable *SymTab, *ParentSymTab;
62   
63   friend class SymbolTableListTraits<Function, Module, Module>;
64
65   void setParent(Module *parent);
66   Function *Prev, *Next;
67   void setNext(Function *N) { Next = N; }
68   void setPrev(Function *N) { Prev = N; }
69
70 public:
71   Function(const FunctionType *Ty, bool isInternal, const std::string &N = "");
72   ~Function();
73
74   // Specialize setName to handle symbol table majik...
75   virtual void setName(const std::string &name, SymbolTable *ST = 0);
76
77   const Type *getReturnType() const;           // Return the type of the ret val
78   const FunctionType *getFunctionType() const; // Return the FunctionType for me
79
80   // Is the body of this function unknown? (the basic block list is empty if so)
81   // this is true for external functions, defined as forward "declare"ations
82   bool isExternal() const { return BasicBlocks.empty(); }
83
84   // getNext/Prev - Return the next or previous instruction in the list.  The
85   // last node in the list is a terminator instruction.
86         Function *getNext()       { return Next; }
87   const Function *getNext() const { return Next; }
88         Function *getPrev()       { return Prev; }
89   const Function *getPrev() const { return Prev; }
90
91   // Get the underlying elements of the Function... both the argument list and
92   // basic block list are empty for external functions.
93   //
94   const ArgumentListType &getArgumentList() const { return ArgumentList; }
95         ArgumentListType &getArgumentList()       { return ArgumentList; }
96
97   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
98         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
99
100   const BasicBlock       &getEntryNode() const   { return front(); }
101         BasicBlock       &getEntryNode()         { return front(); }
102
103   //===--------------------------------------------------------------------===//
104   // Symbol Table Accessing functions...
105
106   // hasSymbolTable() - Returns true if there is a symbol table allocated to
107   // this object AND if there is at least one name in it!
108   //
109   bool hasSymbolTable() const;
110
111   // CAUTION: The current symbol table may be null if there are no names (ie, 
112   // the symbol table is empty) 
113   //
114   inline       SymbolTable *getSymbolTable()       { return SymTab; }
115   inline const SymbolTable *getSymbolTable() const { return SymTab; }
116
117   // getSymbolTableSure is guaranteed to not return a null pointer, because if
118   // the function does not already have a symtab, one is created.  Use this if
119   // you intend to put something into the symbol table for the function.
120   //
121   SymbolTable *getSymbolTableSure();  // Implemented in Value.cpp
122
123   
124   //===--------------------------------------------------------------------===//
125   // BasicBlock iterator forwarding functions
126   //
127   iterator                begin()       { return BasicBlocks.begin(); }
128   const_iterator          begin() const { return BasicBlocks.begin(); }
129   iterator                end  ()       { return BasicBlocks.end();   }
130   const_iterator          end  () const { return BasicBlocks.end();   }
131
132   reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
133   const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
134   reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
135   const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
136
137   unsigned                 size() const { return BasicBlocks.size(); }
138   bool                    empty() const { return BasicBlocks.empty(); }
139   const BasicBlock       &front() const { return BasicBlocks.front(); }
140         BasicBlock       &front()       { return BasicBlocks.front(); }
141   const BasicBlock        &back() const { return BasicBlocks.back(); }
142         BasicBlock        &back()       { return BasicBlocks.back(); }
143
144   //===--------------------------------------------------------------------===//
145   // Argument iterator forwarding functions
146   //
147   aiterator                abegin()       { return ArgumentList.begin(); }
148   const_aiterator          abegin() const { return ArgumentList.begin(); }
149   aiterator                aend  ()       { return ArgumentList.end();   }
150   const_aiterator          aend  () const { return ArgumentList.end();   }
151
152   reverse_aiterator       arbegin()       { return ArgumentList.rbegin(); }
153   const_reverse_aiterator arbegin() const { return ArgumentList.rbegin(); }
154   reverse_aiterator       arend  ()       { return ArgumentList.rend();   }
155   const_reverse_aiterator arend  () const { return ArgumentList.rend();   }
156
157   unsigned                 asize() const { return ArgumentList.size(); }
158   bool                    aempty() const { return ArgumentList.empty(); }
159   const Argument       &afront() const { return ArgumentList.front(); }
160         Argument       &afront()       { return ArgumentList.front(); }
161   const Argument        &aback() const { return ArgumentList.back(); }
162         Argument        &aback()       { return ArgumentList.back(); }
163
164   virtual void print(std::ostream &OS) const;
165
166   // Methods for support type inquiry through isa, cast, and dyn_cast:
167   static inline bool classof(const Function *) { return true; }
168   static inline bool classof(const Value *V) {
169     return V->getValueType() == Value::FunctionVal;
170   }
171
172   // dropAllReferences() - This function causes all the subinstructions to "let
173   // go" of all references that they are maintaining.  This allows one to
174   // 'delete' a whole class at a time, even though there may be circular
175   // references... first all references are dropped, and all use counts go to
176   // zero.  Then everything is delete'd for real.  Note that no operations are
177   // valid on an object that has "dropped all references", except operator 
178   // delete.
179   //
180   void dropAllReferences();
181 };
182
183 #endif