LLVM API Documentation
00001 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declaration of the Function class, which represents a 00011 // single function/procedure in LLVM. 00012 // 00013 // A function basically consists of a list of basic blocks, a list of arguments, 00014 // and a symbol table. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_FUNCTION_H 00019 #define LLVM_FUNCTION_H 00020 00021 #include "llvm/GlobalValue.h" 00022 #include "llvm/BasicBlock.h" 00023 #include "llvm/Argument.h" 00024 #include "llvm/Support/Annotation.h" 00025 00026 namespace llvm { 00027 00028 class FunctionType; 00029 00030 // Traits for intrusive list of instructions... 00031 template<> struct ilist_traits<BasicBlock> 00032 : public SymbolTableListTraits<BasicBlock, Function, Function> { 00033 00034 // createSentinel is used to create a node that marks the end of the list... 00035 static BasicBlock *createSentinel(); 00036 static void destroySentinel(BasicBlock *BB) { delete BB; } 00037 static iplist<BasicBlock> &getList(Function *F); 00038 }; 00039 00040 template<> struct ilist_traits<Argument> 00041 : public SymbolTableListTraits<Argument, Function, Function> { 00042 00043 // createSentinel is used to create a node that marks the end of the list... 00044 static Argument *createSentinel(); 00045 static void destroySentinel(Argument *A) { delete A; } 00046 static iplist<Argument> &getList(Function *F); 00047 }; 00048 00049 class Function : public GlobalValue, public Annotable { 00050 public: 00051 typedef iplist<Argument> ArgumentListType; 00052 typedef iplist<BasicBlock> BasicBlockListType; 00053 00054 // BasicBlock iterators... 00055 typedef BasicBlockListType::iterator iterator; 00056 typedef BasicBlockListType::const_iterator const_iterator; 00057 00058 typedef ArgumentListType::iterator arg_iterator; 00059 typedef ArgumentListType::const_iterator const_arg_iterator; 00060 00061 private: 00062 // Important things that make up a function! 00063 BasicBlockListType BasicBlocks; // The basic blocks 00064 ArgumentListType ArgumentList; // The formal arguments 00065 00066 SymbolTable *SymTab; 00067 unsigned CallingConvention; 00068 00069 friend class SymbolTableListTraits<Function, Module, Module>; 00070 00071 void setParent(Module *parent); 00072 Function *Prev, *Next; 00073 void setNext(Function *N) { Next = N; } 00074 void setPrev(Function *N) { Prev = N; } 00075 00076 public: 00077 /// Function ctor - If the (optional) Module argument is specified, the 00078 /// function is automatically inserted into the end of the function list for 00079 /// the module. 00080 /// 00081 Function(const FunctionType *Ty, LinkageTypes Linkage, 00082 const std::string &N = "", Module *M = 0); 00083 ~Function(); 00084 00085 const Type *getReturnType() const; // Return the type of the ret val 00086 const FunctionType *getFunctionType() const; // Return the FunctionType for me 00087 00088 /// isVarArg - Return true if this function takes a variable number of 00089 /// arguments. 00090 bool isVarArg() const; 00091 00092 /// isExternal - Is the body of this function unknown? (The basic block list 00093 /// is empty if so.) This is true for external functions, defined as forward 00094 /// "declare"ations 00095 /// 00096 virtual bool isExternal() const { return BasicBlocks.empty(); } 00097 00098 /// getIntrinsicID - This method returns the ID number of the specified 00099 /// function, or Intrinsic::not_intrinsic if the function is not an 00100 /// instrinsic, or if the pointer is null. This value is always defined to be 00101 /// zero to allow easy checking for whether a function is intrinsic or not. 00102 /// The particular intrinsic functions which correspond to this value are 00103 /// defined in llvm/Intrinsics.h. 00104 /// 00105 unsigned getIntrinsicID() const; 00106 bool isIntrinsic() const { return getIntrinsicID() != 0; } 00107 00108 /// getCallingConv()/setCallingConv(uint) - These method get and set the 00109 /// calling convention of this function. The enum values for the known 00110 /// calling conventions are defined in CallingConv.h. 00111 unsigned getCallingConv() const { return CallingConvention; } 00112 void setCallingConv(unsigned CC) { CallingConvention = CC; } 00113 00114 /// renameLocalSymbols - This method goes through the Function's symbol table 00115 /// and renames any symbols that conflict with symbols at global scope. This 00116 /// is required before printing out to a textual form, to ensure that there is 00117 /// no ambiguity when parsing. 00118 void renameLocalSymbols(); 00119 00120 00121 /// deleteBody - This method deletes the body of the function, and converts 00122 /// the linkage to external. 00123 /// 00124 void deleteBody() { 00125 dropAllReferences(); 00126 setLinkage(ExternalLinkage); 00127 } 00128 00129 /// removeFromParent - This method unlinks 'this' from the containing module, 00130 /// but does not delete it. 00131 /// 00132 void removeFromParent(); 00133 00134 /// eraseFromParent - This method unlinks 'this' from the containing module 00135 /// and deletes it. 00136 /// 00137 void eraseFromParent(); 00138 00139 00140 // getNext/Prev - Return the next or previous function in the list. These 00141 // methods should never be used directly, and are only used to implement the 00142 // function list as part of the module. 00143 // 00144 Function *getNext() { return Next; } 00145 const Function *getNext() const { return Next; } 00146 Function *getPrev() { return Prev; } 00147 const Function *getPrev() const { return Prev; } 00148 00149 /// Get the underlying elements of the Function... the basic block list is 00150 /// empty for external functions. 00151 /// 00152 const ArgumentListType &getArgumentList() const { return ArgumentList; } 00153 ArgumentListType &getArgumentList() { return ArgumentList; } 00154 00155 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } 00156 BasicBlockListType &getBasicBlockList() { return BasicBlocks; } 00157 00158 const BasicBlock &getEntryBlock() const { return front(); } 00159 BasicBlock &getEntryBlock() { return front(); } 00160 00161 //===--------------------------------------------------------------------===// 00162 // Symbol Table Accessing functions... 00163 00164 /// getSymbolTable() - Return the symbol table... 00165 /// 00166 inline SymbolTable &getSymbolTable() { return *SymTab; } 00167 inline const SymbolTable &getSymbolTable() const { return *SymTab; } 00168 00169 00170 //===--------------------------------------------------------------------===// 00171 // BasicBlock iterator forwarding functions 00172 // 00173 iterator begin() { return BasicBlocks.begin(); } 00174 const_iterator begin() const { return BasicBlocks.begin(); } 00175 iterator end () { return BasicBlocks.end(); } 00176 const_iterator end () const { return BasicBlocks.end(); } 00177 00178 size_t size() const { return BasicBlocks.size(); } 00179 bool empty() const { return BasicBlocks.empty(); } 00180 const BasicBlock &front() const { return BasicBlocks.front(); } 00181 BasicBlock &front() { return BasicBlocks.front(); } 00182 const BasicBlock &back() const { return BasicBlocks.back(); } 00183 BasicBlock &back() { return BasicBlocks.back(); } 00184 00185 //===--------------------------------------------------------------------===// 00186 // Argument iterator forwarding functions 00187 // 00188 arg_iterator arg_begin() { return ArgumentList.begin(); } 00189 const_arg_iterator arg_begin() const { return ArgumentList.begin(); } 00190 arg_iterator arg_end () { return ArgumentList.end(); } 00191 const_arg_iterator arg_end () const { return ArgumentList.end(); } 00192 00193 size_t arg_size () const { return ArgumentList.size(); } 00194 bool arg_empty() const { return ArgumentList.empty(); } 00195 00196 virtual void print(std::ostream &OS) const { print(OS, 0); } 00197 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 00198 00199 /// viewCFG - This function is meant for use from the debugger. You can just 00200 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 00201 /// program, displaying the CFG of the current function with the code for each 00202 /// basic block inside. This depends on there being a 'dot' and 'gv' program 00203 /// in your path. 00204 /// 00205 void viewCFG() const; 00206 00207 /// viewCFGOnly - This function is meant for use from the debugger. It works 00208 /// just like viewCFG, but it does not include the contents of basic blocks 00209 /// into the nodes, just the label. If you are only interested in the CFG 00210 /// this can make the graph smaller. 00211 /// 00212 void viewCFGOnly() const; 00213 00214 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00215 static inline bool classof(const Function *) { return true; } 00216 static inline bool classof(const Value *V) { 00217 return V->getValueType() == Value::FunctionVal; 00218 } 00219 00220 /// dropAllReferences() - This method causes all the subinstructions to "let 00221 /// go" of all references that they are maintaining. This allows one to 00222 /// 'delete' a whole module at a time, even though there may be circular 00223 /// references... first all references are dropped, and all use counts go to 00224 /// zero. Then everything is deleted for real. Note that no operations are 00225 /// valid on an object that has "dropped all references", except operator 00226 /// delete. 00227 /// 00228 /// Since no other object in the module can have references into the body of a 00229 /// function, dropping all references deletes the entire body of the function, 00230 /// including any contained basic blocks. 00231 /// 00232 void dropAllReferences(); 00233 }; 00234 00235 } // End llvm namespace 00236 00237 #endif