1 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
3 // This file contains the declaration of the Function class, which represents a
4 // single function/procedure in LLVM.
6 // A function basically consists of a list of basic blocks, a list of arguments,
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_FUNCTION_H
12 #define LLVM_FUNCTION_H
14 #include "llvm/GlobalValue.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/Argument.h"
20 // Traits for intrusive list of instructions...
21 template<> struct ilist_traits<BasicBlock>
22 : public SymbolTableListTraits<BasicBlock, Function, Function> {
24 // createNode is used to create a node that marks the end of the list...
25 static BasicBlock *createNode();
27 static iplist<BasicBlock> &getList(Function *F);
30 template<> struct ilist_traits<Argument>
31 : public SymbolTableListTraits<Argument, Function, Function> {
33 // createNode is used to create a node that marks the end of the list...
34 static Argument *createNode();
35 static iplist<Argument> &getList(Function *F);
38 class Function : public GlobalValue {
40 typedef iplist<Argument> ArgumentListType;
41 typedef iplist<BasicBlock> BasicBlockListType;
43 // BasicBlock iterators...
44 typedef BasicBlockListType::iterator iterator;
45 typedef BasicBlockListType::const_iterator const_iterator;
46 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
47 typedef std::reverse_iterator<iterator> reverse_iterator;
49 typedef ArgumentListType::iterator aiterator;
50 typedef ArgumentListType::const_iterator const_aiterator;
51 typedef std::reverse_iterator<const_aiterator> const_reverse_aiterator;
52 typedef std::reverse_iterator<aiterator> reverse_aiterator;
56 // Important things that make up a function!
57 BasicBlockListType BasicBlocks; // The basic blocks
58 ArgumentListType ArgumentList; // The formal arguments
62 friend class SymbolTableListTraits<Function, Module, Module>;
64 void setParent(Module *parent);
65 Function *Prev, *Next;
66 void setNext(Function *N) { Next = N; }
67 void setPrev(Function *N) { Prev = N; }
70 /// Function ctor - If the (optional) Module argument is specified, the
71 /// function is automatically inserted into the end of the function list for
74 Function(const FunctionType *Ty, bool isInternal, const std::string &N = "",
78 // Specialize setName to handle symbol table majik...
79 virtual void setName(const std::string &name, SymbolTable *ST = 0);
81 const Type *getReturnType() const; // Return the type of the ret val
82 const FunctionType *getFunctionType() const; // Return the FunctionType for me
84 /// isExternal - Is the body of this function unknown? (the basic block list
85 /// is empty if so) this is true for external functions, defined as forward
88 virtual bool isExternal() const { return BasicBlocks.empty(); }
90 // getNext/Prev - Return the next or previous function in the list. These
91 // methods should never be used directly, and are only used to implement the
92 // function list as part of the module.
94 Function *getNext() { return Next; }
95 const Function *getNext() const { return Next; }
96 Function *getPrev() { return Prev; }
97 const Function *getPrev() const { return Prev; }
99 /// Get the underlying elements of the Function... both the argument list and
100 /// basic block list are empty for external functions.
102 const ArgumentListType &getArgumentList() const { return ArgumentList; }
103 ArgumentListType &getArgumentList() { return ArgumentList; }
105 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
106 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
108 const BasicBlock &getEntryNode() const { return front(); }
109 BasicBlock &getEntryNode() { return front(); }
111 //===--------------------------------------------------------------------===//
112 // Symbol Table Accessing functions...
114 /// getSymbolTable() - Return the symbol table...
116 inline SymbolTable &getSymbolTable() { return *SymTab; }
117 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
120 //===--------------------------------------------------------------------===//
121 // BasicBlock iterator forwarding functions
123 iterator begin() { return BasicBlocks.begin(); }
124 const_iterator begin() const { return BasicBlocks.begin(); }
125 iterator end () { return BasicBlocks.end(); }
126 const_iterator end () const { return BasicBlocks.end(); }
128 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
129 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
130 reverse_iterator rend () { return BasicBlocks.rend(); }
131 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
133 unsigned size() const { return BasicBlocks.size(); }
134 bool empty() const { return BasicBlocks.empty(); }
135 const BasicBlock &front() const { return BasicBlocks.front(); }
136 BasicBlock &front() { return BasicBlocks.front(); }
137 const BasicBlock &back() const { return BasicBlocks.back(); }
138 BasicBlock &back() { return BasicBlocks.back(); }
140 //===--------------------------------------------------------------------===//
141 // Argument iterator forwarding functions
143 aiterator abegin() { return ArgumentList.begin(); }
144 const_aiterator abegin() const { return ArgumentList.begin(); }
145 aiterator aend () { return ArgumentList.end(); }
146 const_aiterator aend () const { return ArgumentList.end(); }
148 reverse_aiterator arbegin() { return ArgumentList.rbegin(); }
149 const_reverse_aiterator arbegin() const { return ArgumentList.rbegin(); }
150 reverse_aiterator arend () { return ArgumentList.rend(); }
151 const_reverse_aiterator arend () const { return ArgumentList.rend(); }
153 unsigned asize() const { return ArgumentList.size(); }
154 bool aempty() const { return ArgumentList.empty(); }
155 const Argument &afront() const { return ArgumentList.front(); }
156 Argument &afront() { return ArgumentList.front(); }
157 const Argument &aback() const { return ArgumentList.back(); }
158 Argument &aback() { return ArgumentList.back(); }
160 virtual void print(std::ostream &OS) const;
162 /// Methods for support type inquiry through isa, cast, and dyn_cast:
163 static inline bool classof(const Function *) { return true; }
164 static inline bool classof(const Value *V) {
165 return V->getValueType() == Value::FunctionVal;
168 /// dropAllReferences() - This function causes all the subinstructions to "let
169 /// go" of all references that they are maintaining. This allows one to
170 /// 'delete' a whole class at a time, even though there may be circular
171 /// references... first all references are dropped, and all use counts go to
172 /// zero. Then everything is delete'd for real. Note that no operations are
173 /// valid on an object that has "dropped all references", except operator
176 void dropAllReferences();