1 //===-- llvm/Function.h - Class to represent a single VM function -*- C++ -*-=//
3 // This file contains the declaration of the Function class, which represents a
4 // single function/procedure in the VM.
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
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_FUNCTION_H
13 #define LLVM_FUNCTION_H
15 #include "llvm/GlobalValue.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/Argument.h"
21 // Traits for intrusive list of instructions...
22 template<> struct ilist_traits<BasicBlock>
23 : public SymbolTableListTraits<BasicBlock, Function, Function> {
25 // createNode is used to create a node that marks the end of the list...
26 static BasicBlock *createNode() { return new BasicBlock(); }
28 static iplist<BasicBlock> &getList(Function *F);
31 template<> struct ilist_traits<Argument>
32 : public SymbolTableListTraits<Argument, Function, Function> {
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);
39 class Function : public GlobalValue {
41 typedef iplist<Argument> ArgumentListType;
42 typedef iplist<BasicBlock> BasicBlockListType;
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;
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;
57 // Important things that make up a function!
58 BasicBlockListType BasicBlocks; // The basic blocks
59 ArgumentListType ArgumentList; // The formal arguments
61 SymbolTable *SymTab, *ParentSymTab;
63 friend class SymbolTableListTraits<Function, Module, Module>;
65 void setParent(Module *parent);
66 Function *Prev, *Next;
67 void setNext(Function *N) { Next = N; }
68 void setPrev(Function *N) { Prev = N; }
71 Function(const FunctionType *Ty, bool isInternal, const std::string &N = "");
74 // Specialize setName to handle symbol table majik...
75 virtual void setName(const std::string &name, SymbolTable *ST = 0);
77 const Type *getReturnType() const; // Return the type of the ret val
78 const FunctionType *getFunctionType() const; // Return the FunctionType for me
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
84 bool isExternal() const { return BasicBlocks.empty(); }
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; }
93 /// Get the underlying elements of the Function... both the argument list and
94 /// basic block list are empty for external functions.
96 const ArgumentListType &getArgumentList() const { return ArgumentList; }
97 ArgumentListType &getArgumentList() { return ArgumentList; }
99 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
100 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
102 const BasicBlock &getEntryNode() const { return front(); }
103 BasicBlock &getEntryNode() { return front(); }
105 //===--------------------------------------------------------------------===//
106 // Symbol Table Accessing functions...
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!
111 bool hasSymbolTable() const;
113 /// getSymbolTable() - CAUTION: The current symbol table may be null if there
114 /// are no names (ie, the symbol table is empty)
116 inline SymbolTable *getSymbolTable() { return SymTab; }
117 inline const SymbolTable *getSymbolTable() const { return SymTab; }
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.
123 SymbolTable *getSymbolTableSure(); // Implemented in Value.cpp
126 //===--------------------------------------------------------------------===//
127 // BasicBlock iterator forwarding functions
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(); }
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(); }
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(); }
146 //===--------------------------------------------------------------------===//
147 // Argument iterator forwarding functions
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(); }
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(); }
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(); }
166 virtual void print(std::ostream &OS) const;
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;
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
182 void dropAllReferences();