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 // 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(); }
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; }
91 // Get the underlying elements of the Function... both the argument list and
92 // basic block list are empty for external functions.
94 const ArgumentListType &getArgumentList() const { return ArgumentList; }
95 ArgumentListType &getArgumentList() { return ArgumentList; }
97 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
98 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
100 const BasicBlock &getEntryNode() const { return front(); }
101 BasicBlock &getEntryNode() { return front(); }
103 //===--------------------------------------------------------------------===//
104 // Symbol Table Accessing functions...
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!
109 bool hasSymbolTable() const;
111 // CAUTION: The current symbol table may be null if there are no names (ie,
112 // the symbol table is empty)
114 inline SymbolTable *getSymbolTable() { return SymTab; }
115 inline const SymbolTable *getSymbolTable() const { return SymTab; }
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.
121 SymbolTable *getSymbolTableSure(); // Implemented in Value.cpp
124 //===--------------------------------------------------------------------===//
125 // BasicBlock iterator forwarding functions
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(); }
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(); }
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(); }
144 //===--------------------------------------------------------------------===//
145 // Argument iterator forwarding functions
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(); }
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(); }
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(); }
164 virtual void print(std::ostream &OS) const;
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;
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
180 void dropAllReferences();