For PR1146:
[oota-llvm.git] / include / llvm / Function.h
1 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Function class, which represents a
11 // single function/procedure in LLVM.
12 //
13 // A function basically consists of a list of basic blocks, a list of arguments,
14 // and a symbol table.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_FUNCTION_H
19 #define LLVM_FUNCTION_H
20
21 #include "llvm/GlobalValue.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/Argument.h"
24 #include "llvm/Support/Annotation.h"
25
26 namespace llvm {
27
28 class FunctionType;
29 class ParamAttrsList;
30
31 // Traits for intrusive list of instructions...
32 template<> struct ilist_traits<BasicBlock>
33   : public SymbolTableListTraits<BasicBlock, Function, Function> {
34
35   // createSentinel is used to create a node that marks the end of the list...
36   static BasicBlock *createSentinel();
37   static void destroySentinel(BasicBlock *BB) { delete BB; }
38   static iplist<BasicBlock> &getList(Function *F);
39 };
40
41 template<> struct ilist_traits<Argument>
42   : public SymbolTableListTraits<Argument, Function, Function> {
43
44   // createSentinel is used to create a node that marks the end of the list...
45   static Argument *createSentinel();
46   static void destroySentinel(Argument *A) { delete A; }
47   static iplist<Argument> &getList(Function *F);
48 };
49
50 class Function : public GlobalValue, public Annotable {
51 public:
52   typedef iplist<Argument> ArgumentListType;
53   typedef iplist<BasicBlock> BasicBlockListType;
54
55   // BasicBlock iterators...
56   typedef BasicBlockListType::iterator iterator;
57   typedef BasicBlockListType::const_iterator const_iterator;
58
59   typedef ArgumentListType::iterator arg_iterator;
60   typedef ArgumentListType::const_iterator const_arg_iterator;
61
62 private:
63   // Important things that make up a function!
64   BasicBlockListType  BasicBlocks;   ///< The basic blocks
65   ArgumentListType ArgumentList;     ///< The formal arguments
66   ValueSymbolTable *SymTab;          ///< Symbol table of args/instructions
67   ParamAttrsList *ParamAttrs;        ///< Parameter attributes
68   unsigned CallingConvention;        ///< Calling convention to use
69
70   friend class SymbolTableListTraits<Function, Module, Module>;
71
72   void setParent(Module *parent);
73   Function *Prev, *Next;
74   void setNext(Function *N) { Next = N; }
75   void setPrev(Function *N) { Prev = N; }
76
77 public:
78   /// Function ctor - If the (optional) Module argument is specified, the
79   /// function is automatically inserted into the end of the function list for
80   /// the module.
81   ///
82   Function(const FunctionType *Ty, LinkageTypes Linkage,
83            const std::string &N = "", Module *M = 0);
84   ~Function();
85
86   const Type *getReturnType() const;           // Return the type of the ret val
87   const FunctionType *getFunctionType() const; // Return the FunctionType for me
88
89   /// isVarArg - Return true if this function takes a variable number of
90   /// arguments.
91   bool isVarArg() const;
92
93   /// isDeclaration - Is the body of this function unknown? (The basic block 
94   /// list is empty if so.) This is true for function declarations, but not 
95   /// true for function definitions.
96   ///
97   virtual bool isDeclaration() const { return BasicBlocks.empty(); }
98
99   /// getIntrinsicID - This method returns the ID number of the specified
100   /// function, or Intrinsic::not_intrinsic if the function is not an
101   /// instrinsic, or if the pointer is null.  This value is always defined to be
102   /// zero to allow easy checking for whether a function is intrinsic or not.
103   /// The particular intrinsic functions which correspond to this value are
104   /// defined in llvm/Intrinsics.h.
105   ///
106   unsigned getIntrinsicID() const;
107   bool isIntrinsic() const { return getIntrinsicID() != 0; }
108
109   /// getCallingConv()/setCallingConv(uint) - These method get and set the
110   /// calling convention of this function.  The enum values for the known
111   /// calling conventions are defined in CallingConv.h.
112   unsigned getCallingConv() const { return CallingConvention; }
113   void setCallingConv(unsigned CC) { CallingConvention = CC; }
114
115   /// Obtains a constant pointer to the ParamAttrsList object which holds the
116   /// parameter attributes information, if any. 
117   /// @returns 0 if no parameter attributes have been set.
118   /// @brief Get the parameter attributes.
119   const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
120
121   /// Sets the parameter attributes for this Function. To construct a 
122   /// ParamAttrsList, see ParameterAttributes.h
123   /// @brief Set the parameter attributes.
124   void setParamAttrs(ParamAttrsList *attrs) { ParamAttrs = attrs; }
125
126   /// deleteBody - This method deletes the body of the function, and converts
127   /// the linkage to external.
128   ///
129   void deleteBody() {
130     dropAllReferences();
131     setLinkage(ExternalLinkage);
132   }
133
134   /// removeFromParent - This method unlinks 'this' from the containing module,
135   /// but does not delete it.
136   ///
137   void removeFromParent();
138
139   /// eraseFromParent - This method unlinks 'this' from the containing module
140   /// and deletes it.
141   ///
142   void eraseFromParent();
143
144
145   // getNext/Prev - Return the next or previous function in the list.  These
146   // methods should never be used directly, and are only used to implement the
147   // function list as part of the module.
148   //
149         Function *getNext()       { return Next; }
150   const Function *getNext() const { return Next; }
151         Function *getPrev()       { return Prev; }
152   const Function *getPrev() const { return Prev; }
153
154   /// Get the underlying elements of the Function... the basic block list is
155   /// empty for external functions.
156   ///
157   const ArgumentListType &getArgumentList() const { return ArgumentList; }
158         ArgumentListType &getArgumentList()       { return ArgumentList; }
159
160   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
161         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
162
163   const BasicBlock       &getEntryBlock() const   { return front(); }
164         BasicBlock       &getEntryBlock()         { return front(); }
165
166   //===--------------------------------------------------------------------===//
167   // Symbol Table Accessing functions...
168
169   /// getSymbolTable() - Return the symbol table...
170   ///
171   inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
172   inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
173
174
175   //===--------------------------------------------------------------------===//
176   // BasicBlock iterator forwarding functions
177   //
178   iterator                begin()       { return BasicBlocks.begin(); }
179   const_iterator          begin() const { return BasicBlocks.begin(); }
180   iterator                end  ()       { return BasicBlocks.end();   }
181   const_iterator          end  () const { return BasicBlocks.end();   }
182
183   size_t                   size() const { return BasicBlocks.size();  }
184   bool                    empty() const { return BasicBlocks.empty(); }
185   const BasicBlock       &front() const { return BasicBlocks.front(); }
186         BasicBlock       &front()       { return BasicBlocks.front(); }
187   const BasicBlock        &back() const { return BasicBlocks.back();  }
188         BasicBlock        &back()       { return BasicBlocks.back();  }
189
190   //===--------------------------------------------------------------------===//
191   // Argument iterator forwarding functions
192   //
193   arg_iterator                arg_begin()       { return ArgumentList.begin(); }
194   const_arg_iterator          arg_begin() const { return ArgumentList.begin(); }
195   arg_iterator                arg_end  ()       { return ArgumentList.end();   }
196   const_arg_iterator          arg_end  () const { return ArgumentList.end();   }
197
198   size_t                      arg_size () const { return ArgumentList.size();  }
199   bool                        arg_empty() const { return ArgumentList.empty(); }
200
201   virtual void print(std::ostream &OS) const { print(OS, 0); }
202   void print(std::ostream *OS) const { if (OS) print(*OS); }
203   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
204
205   /// viewCFG - This function is meant for use from the debugger.  You can just
206   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
207   /// program, displaying the CFG of the current function with the code for each
208   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
209   /// in your path.
210   ///
211   void viewCFG() const;
212
213   /// viewCFGOnly - This function is meant for use from the debugger.  It works
214   /// just like viewCFG, but it does not include the contents of basic blocks
215   /// into the nodes, just the label.  If you are only interested in the CFG
216   /// this can make the graph smaller.
217   ///
218   void viewCFGOnly() const;
219
220   /// Methods for support type inquiry through isa, cast, and dyn_cast:
221   static inline bool classof(const Function *) { return true; }
222   static inline bool classof(const Value *V) {
223     return V->getValueType() == Value::FunctionVal;
224   }
225
226   /// dropAllReferences() - This method causes all the subinstructions to "let
227   /// go" of all references that they are maintaining.  This allows one to
228   /// 'delete' a whole module at a time, even though there may be circular
229   /// references... first all references are dropped, and all use counts go to
230   /// zero.  Then everything is deleted for real.  Note that no operations are
231   /// valid on an object that has "dropped all references", except operator
232   /// delete.
233   ///
234   /// Since no other object in the module can have references into the body of a
235   /// function, dropping all references deletes the entire body of the function,
236   /// including any contained basic blocks.
237   ///
238   void dropAllReferences();
239 };
240
241 } // End llvm namespace
242
243 #endif