Convert comments to Doxygen style
[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   /// isExternal - Is the body of this function unknown? (the basic block list
81   /// is empty if so) this is true for external functions, defined as forward
82   /// "declare"ations
83   ///
84   bool isExternal() const { return BasicBlocks.empty(); }
85
86   // getNext/Prev - Return the next or previous instruction in the list.  The
87   // last node in the list is a terminator instruction.
88         Function *getNext()       { return Next; }
89   const Function *getNext() const { return Next; }
90         Function *getPrev()       { return Prev; }
91   const Function *getPrev() const { return Prev; }
92
93   /// Get the underlying elements of the Function... both the argument list and
94   /// basic block list are empty for external functions.
95   ///
96   const ArgumentListType &getArgumentList() const { return ArgumentList; }
97         ArgumentListType &getArgumentList()       { return ArgumentList; }
98
99   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
100         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
101
102   const BasicBlock       &getEntryNode() const   { return front(); }
103         BasicBlock       &getEntryNode()         { return front(); }
104
105   //===--------------------------------------------------------------------===//
106   // Symbol Table Accessing functions...
107
108   /// hasSymbolTable() - Returns true if there is a symbol table allocated to
109   /// this object AND if there is at least one name in it!
110   ///
111   bool hasSymbolTable() const;
112
113   /// getSymbolTable() - CAUTION: The current symbol table may be null if there
114   /// are no names (ie, the symbol table is empty)
115   ///
116   inline       SymbolTable *getSymbolTable()       { return SymTab; }
117   inline const SymbolTable *getSymbolTable() const { return SymTab; }
118
119   /// getSymbolTableSure is guaranteed to not return a null pointer, because if
120   /// the function does not already have a symtab, one is created.  Use this if
121   /// you intend to put something into the symbol table for the function.
122   ///
123   SymbolTable *getSymbolTableSure();  // Implemented in Value.cpp
124
125   
126   //===--------------------------------------------------------------------===//
127   // BasicBlock iterator forwarding functions
128   //
129   iterator                begin()       { return BasicBlocks.begin(); }
130   const_iterator          begin() const { return BasicBlocks.begin(); }
131   iterator                end  ()       { return BasicBlocks.end();   }
132   const_iterator          end  () const { return BasicBlocks.end();   }
133
134   reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
135   const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
136   reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
137   const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
138
139   unsigned                 size() const { return BasicBlocks.size(); }
140   bool                    empty() const { return BasicBlocks.empty(); }
141   const BasicBlock       &front() const { return BasicBlocks.front(); }
142         BasicBlock       &front()       { return BasicBlocks.front(); }
143   const BasicBlock        &back() const { return BasicBlocks.back(); }
144         BasicBlock        &back()       { return BasicBlocks.back(); }
145
146   //===--------------------------------------------------------------------===//
147   // Argument iterator forwarding functions
148   //
149   aiterator                abegin()       { return ArgumentList.begin(); }
150   const_aiterator          abegin() const { return ArgumentList.begin(); }
151   aiterator                aend  ()       { return ArgumentList.end();   }
152   const_aiterator          aend  () const { return ArgumentList.end();   }
153
154   reverse_aiterator       arbegin()       { return ArgumentList.rbegin(); }
155   const_reverse_aiterator arbegin() const { return ArgumentList.rbegin(); }
156   reverse_aiterator       arend  ()       { return ArgumentList.rend();   }
157   const_reverse_aiterator arend  () const { return ArgumentList.rend();   }
158
159   unsigned                 asize() const { return ArgumentList.size(); }
160   bool                    aempty() const { return ArgumentList.empty(); }
161   const Argument       &afront() const { return ArgumentList.front(); }
162         Argument       &afront()       { return ArgumentList.front(); }
163   const Argument        &aback() const { return ArgumentList.back(); }
164         Argument        &aback()       { return ArgumentList.back(); }
165
166   virtual void print(std::ostream &OS) const;
167
168   /// Methods for support type inquiry through isa, cast, and dyn_cast:
169   static inline bool classof(const Function *) { return true; }
170   static inline bool classof(const Value *V) {
171     return V->getValueType() == Value::FunctionVal;
172   }
173
174   /// dropAllReferences() - This function causes all the subinstructions to "let
175   /// go" of all references that they are maintaining.  This allows one to
176   /// 'delete' a whole class at a time, even though there may be circular
177   /// references... first all references are dropped, and all use counts go to
178   /// zero.  Then everything is delete'd for real.  Note that no operations are
179   /// valid on an object that has "dropped all references", except operator 
180   /// delete.
181   ///
182   void dropAllReferences();
183 };
184
185 #endif