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/ValueHolder.h"
20 class Function : public GlobalValue {
22 typedef ValueHolder<Argument , Function, Function> ArgumentListType;
23 typedef ValueHolder<BasicBlock, Function, Function> BasicBlocksType;
25 // BasicBlock iterators...
26 typedef BasicBlocksType::iterator iterator;
27 typedef BasicBlocksType::const_iterator const_iterator;
28 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
29 typedef std::reverse_iterator<iterator> reverse_iterator;
33 // Important things that make up a function!
34 BasicBlocksType BasicBlocks; // The basic blocks
35 ArgumentListType ArgumentList; // The formal arguments
37 SymbolTable *SymTab, *ParentSymTab;
39 friend class ValueHolder<Function, Module, Module>;
40 void setParent(Module *parent);
43 Function(const FunctionType *Ty, bool isInternal, const std::string &N = "");
46 // Specialize setName to handle symbol table majik...
47 virtual void setName(const std::string &name, SymbolTable *ST = 0);
49 const Type *getReturnType() const; // Return the type of the ret val
50 const FunctionType *getFunctionType() const; // Return the FunctionType for me
52 // Is the body of this function unknown? (the basic block list is empty if so)
53 // this is true for external functions, defined as forward "declare"ations
54 bool isExternal() const { return BasicBlocks.empty(); }
56 // Get the underlying elements of the Function... both the argument list and
57 // basic block list are empty for external functions.
59 inline const ArgumentListType &getArgumentList() const{ return ArgumentList; }
60 inline ArgumentListType &getArgumentList() { return ArgumentList; }
62 inline const BasicBlocksType &getBasicBlocks() const { return BasicBlocks; }
63 inline BasicBlocksType &getBasicBlocks() { return BasicBlocks; }
65 inline const BasicBlock *getEntryNode() const { return front(); }
66 inline BasicBlock *getEntryNode() { return front(); }
68 //===--------------------------------------------------------------------===//
69 // Symbol Table Accessing functions...
71 // hasSymbolTable() - Returns true if there is a symbol table allocated to
72 // this object AND if there is at least one name in it!
74 bool hasSymbolTable() const;
76 // CAUTION: The current symbol table may be null if there are no names (ie,
77 // the symbol table is empty)
79 inline SymbolTable *getSymbolTable() { return SymTab; }
80 inline const SymbolTable *getSymbolTable() const { return SymTab; }
82 // getSymbolTableSure is guaranteed to not return a null pointer, because if
83 // the function does not already have a symtab, one is created. Use this if
84 // you intend to put something into the symbol table for the function.
86 SymbolTable *getSymbolTableSure(); // Implemented in Value.cpp
89 //===--------------------------------------------------------------------===//
90 // BasicBlock iterator forwarding functions
92 inline iterator begin() { return BasicBlocks.begin(); }
93 inline const_iterator begin() const { return BasicBlocks.begin(); }
94 inline iterator end () { return BasicBlocks.end(); }
95 inline const_iterator end () const { return BasicBlocks.end(); }
97 inline reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
98 inline const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
99 inline reverse_iterator rend () { return BasicBlocks.rend(); }
100 inline const_reverse_iterator rend () const { return BasicBlocks.rend(); }
102 inline unsigned size() const { return BasicBlocks.size(); }
103 inline bool empty() const { return BasicBlocks.empty(); }
104 inline const BasicBlock *front() const { return BasicBlocks.front(); }
105 inline BasicBlock *front() { return BasicBlocks.front(); }
106 inline const BasicBlock *back() const { return BasicBlocks.back(); }
107 inline BasicBlock *back() { return BasicBlocks.back(); }
109 virtual void print(std::ostream &OS) const;
111 // Methods for support type inquiry through isa, cast, and dyn_cast:
112 static inline bool classof(const Function *) { return true; }
113 static inline bool classof(const Value *V) {
114 return V->getValueType() == Value::FunctionVal;
117 // dropAllReferences() - This function causes all the subinstructions to "let
118 // go" of all references that they are maintaining. This allows one to
119 // 'delete' a whole class at a time, even though there may be circular
120 // references... first all references are dropped, and all use counts go to
121 // zero. Then everything is delete'd for real. Note that no operations are
122 // valid on an object that has "dropped all references", except operator
125 void dropAllReferences();