Don't attribute in file headers anymore. See llvmdev for the
[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 is distributed under the University of Illinois Open Source
6 // 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/ParameterAttributes.h"
25 #include "llvm/Support/Annotation.h"
26
27 namespace llvm {
28
29 class FunctionType;
30 class ParamAttrsList;
31
32 // Traits for intrusive list of instructions...
33 template<> struct ilist_traits<BasicBlock>
34   : public SymbolTableListTraits<BasicBlock, Function> {
35
36   // createSentinel is used to create a node that marks the end of the list...
37   static BasicBlock *createSentinel();
38   static void destroySentinel(BasicBlock *BB) { delete BB; }
39   static iplist<BasicBlock> &getList(Function *F);
40   static ValueSymbolTable *getSymTab(Function *ItemParent);
41   static int getListOffset();
42 };
43
44 template<> struct ilist_traits<Argument>
45   : public SymbolTableListTraits<Argument, Function> {
46
47   // createSentinel is used to create a node that marks the end of the list...
48   static Argument *createSentinel();
49   static void destroySentinel(Argument *A) { delete A; }
50   static iplist<Argument> &getList(Function *F);
51   static ValueSymbolTable *getSymTab(Function *ItemParent);
52   static int getListOffset();
53 };
54
55 class Function : public GlobalValue, public Annotable {
56 public:
57   typedef iplist<Argument> ArgumentListType;
58   typedef iplist<BasicBlock> BasicBlockListType;
59
60   // BasicBlock iterators...
61   typedef BasicBlockListType::iterator iterator;
62   typedef BasicBlockListType::const_iterator const_iterator;
63
64   typedef ArgumentListType::iterator arg_iterator;
65   typedef ArgumentListType::const_iterator const_arg_iterator;
66
67 private:
68   // Important things that make up a function!
69   BasicBlockListType  BasicBlocks;        ///< The basic blocks
70   mutable ArgumentListType ArgumentList;  ///< The formal arguments
71   ValueSymbolTable *SymTab;               ///< Symbol table of args/instructions
72   const ParamAttrsList *ParamAttrs;       ///< Parameter attributes
73   
74   // The Calling Convention is stored in Value::SubclassData.
75   /*unsigned CallingConvention;*/
76
77   friend class SymbolTableListTraits<Function, Module>;
78
79   void setParent(Module *parent);
80   Function *Prev, *Next;
81   void setNext(Function *N) { Next = N; }
82   void setPrev(Function *N) { Prev = N; }
83
84   // getNext/Prev - Return the next or previous function in the list.  These
85   // methods should never be used directly, and are only used to implement the
86   // function list as part of the module.
87   //
88   Function *getNext()             { return Next; }
89   const Function *getNext() const { return Next; }
90   Function *getPrev()             { return Prev; }
91   const Function *getPrev() const { return Prev; }
92
93   /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
94   /// built on demand, so that the list isn't allocated until the first client
95   /// needs it.  The hasLazyArguments predicate returns true if the arg list
96   /// hasn't been set up yet.
97   bool hasLazyArguments() const {
98     return SubclassData & 1;
99   }
100   void CheckLazyArguments() const {
101     if (hasLazyArguments())
102       BuildLazyArguments();
103   }
104   void BuildLazyArguments() const;
105 public:
106   /// Function ctor - If the (optional) Module argument is specified, the
107   /// function is automatically inserted into the end of the function list for
108   /// the module.
109   ///
110   Function(const FunctionType *Ty, LinkageTypes Linkage,
111            const std::string &N = "", Module *M = 0);
112   ~Function();
113
114   const Type *getReturnType() const;           // Return the type of the ret val
115   const FunctionType *getFunctionType() const; // Return the FunctionType for me
116
117   /// isVarArg - Return true if this function takes a variable number of
118   /// arguments.
119   bool isVarArg() const;
120
121   /// isDeclaration - Is the body of this function unknown? (The basic block 
122   /// list is empty if so.) This is true for function declarations, but not 
123   /// true for function definitions.
124   ///
125   virtual bool isDeclaration() const { return BasicBlocks.empty(); }
126
127   /// getIntrinsicID - This method returns the ID number of the specified
128   /// function, or Intrinsic::not_intrinsic if the function is not an
129   /// instrinsic, or if the pointer is null.  This value is always defined to be
130   /// zero to allow easy checking for whether a function is intrinsic or not.
131   /// The particular intrinsic functions which correspond to this value are
132   /// defined in llvm/Intrinsics.h.
133   ///
134   unsigned getIntrinsicID(bool noAssert = false) const;
135   bool isIntrinsic() const { return getIntrinsicID() != 0; }
136
137   /// getCallingConv()/setCallingConv(uint) - These method get and set the
138   /// calling convention of this function.  The enum values for the known
139   /// calling conventions are defined in CallingConv.h.
140   unsigned getCallingConv() const { return SubclassData >> 1; }
141   void setCallingConv(unsigned CC) {
142     SubclassData = (SubclassData & 1) | (CC << 1);
143   }
144   
145   /// Obtains a constant pointer to the ParamAttrsList object which holds the
146   /// parameter attributes information, if any. 
147   /// @returns 0 if no parameter attributes have been set.
148   /// @brief Get the parameter attributes.
149   const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
150
151   /// Sets the parameter attributes for this Function. To construct a 
152   /// ParamAttrsList, see ParameterAttributes.h
153   /// @brief Set the parameter attributes.
154   void setParamAttrs(const ParamAttrsList *attrs);
155
156   /// hasCollector/getCollector/setCollector/clearCollector - The name of the
157   /// garbage collection algorithm to use during code generation.
158   bool hasCollector() const;
159   const char *getCollector() const;
160   void setCollector(const char *Str);
161   void clearCollector();
162
163   /// @brief Determine whether the function has the given attribute.
164   bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
165     return ParamAttrs && ParamAttrs->paramHasAttr(i, attr);
166   }
167
168   /// @brief Determine if the function cannot return.
169   bool doesNotReturn() const {
170     return paramHasAttr(0, ParamAttr::NoReturn);
171   }
172
173   /// @brief Determine if the function cannot unwind.
174   bool doesNotThrow() const {
175     return paramHasAttr(0, ParamAttr::NoUnwind);
176   }
177
178   /// @brief Determine if the function does not access memory.
179   bool doesNotAccessMemory() const {
180     return paramHasAttr(0, ParamAttr::ReadNone);
181   }
182
183   /// @brief Determine if the function does not access or only reads memory.
184   bool onlyReadsMemory() const {
185     return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
186   }
187
188   /// @brief Determine if the function returns a structure.
189   bool isStructReturn() const {
190     return paramHasAttr(1, ParamAttr::StructRet);
191   }
192
193   /// deleteBody - This method deletes the body of the function, and converts
194   /// the linkage to external.
195   ///
196   void deleteBody() {
197     dropAllReferences();
198     setLinkage(ExternalLinkage);
199   }
200
201   /// removeFromParent - This method unlinks 'this' from the containing module,
202   /// but does not delete it.
203   ///
204   void removeFromParent();
205
206   /// eraseFromParent - This method unlinks 'this' from the containing module
207   /// and deletes it.
208   ///
209   void eraseFromParent();
210
211
212   /// Get the underlying elements of the Function... the basic block list is
213   /// empty for external functions.
214   ///
215   const ArgumentListType &getArgumentList() const {
216     CheckLazyArguments();
217     return ArgumentList;
218   }
219   ArgumentListType &getArgumentList() {
220     CheckLazyArguments();
221     return ArgumentList;
222   }
223
224   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
225         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
226
227   const BasicBlock       &getEntryBlock() const   { return front(); }
228         BasicBlock       &getEntryBlock()         { return front(); }
229
230   //===--------------------------------------------------------------------===//
231   // Symbol Table Accessing functions...
232
233   /// getSymbolTable() - Return the symbol table...
234   ///
235   inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
236   inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
237
238
239   //===--------------------------------------------------------------------===//
240   // BasicBlock iterator forwarding functions
241   //
242   iterator                begin()       { return BasicBlocks.begin(); }
243   const_iterator          begin() const { return BasicBlocks.begin(); }
244   iterator                end  ()       { return BasicBlocks.end();   }
245   const_iterator          end  () const { return BasicBlocks.end();   }
246
247   size_t                   size() const { return BasicBlocks.size();  }
248   bool                    empty() const { return BasicBlocks.empty(); }
249   const BasicBlock       &front() const { return BasicBlocks.front(); }
250         BasicBlock       &front()       { return BasicBlocks.front(); }
251   const BasicBlock        &back() const { return BasicBlocks.back();  }
252         BasicBlock        &back()       { return BasicBlocks.back();  }
253
254   //===--------------------------------------------------------------------===//
255   // Argument iterator forwarding functions
256   //
257   arg_iterator arg_begin() {
258     CheckLazyArguments();
259     return ArgumentList.begin();
260   }
261   const_arg_iterator arg_begin() const {
262     CheckLazyArguments();
263     return ArgumentList.begin();
264   }
265   arg_iterator arg_end() {
266     CheckLazyArguments();
267     return ArgumentList.end();
268   }
269   const_arg_iterator arg_end() const {
270     CheckLazyArguments();
271     return ArgumentList.end();
272   }
273
274   size_t arg_size() const;
275   bool arg_empty() const;
276
277   virtual void print(std::ostream &OS) const { print(OS, 0); }
278   void print(std::ostream *OS) const { if (OS) print(*OS); }
279   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
280
281   /// viewCFG - This function is meant for use from the debugger.  You can just
282   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
283   /// program, displaying the CFG of the current function with the code for each
284   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
285   /// in your path.
286   ///
287   void viewCFG() const;
288
289   /// viewCFGOnly - This function is meant for use from the debugger.  It works
290   /// just like viewCFG, but it does not include the contents of basic blocks
291   /// into the nodes, just the label.  If you are only interested in the CFG
292   /// this can make the graph smaller.
293   ///
294   void viewCFGOnly() const;
295
296   /// Methods for support type inquiry through isa, cast, and dyn_cast:
297   static inline bool classof(const Function *) { return true; }
298   static inline bool classof(const Value *V) {
299     return V->getValueID() == Value::FunctionVal;
300   }
301
302   /// dropAllReferences() - This method causes all the subinstructions to "let
303   /// go" of all references that they are maintaining.  This allows one to
304   /// 'delete' a whole module at a time, even though there may be circular
305   /// references... first all references are dropped, and all use counts go to
306   /// zero.  Then everything is deleted for real.  Note that no operations are
307   /// valid on an object that has "dropped all references", except operator
308   /// delete.
309   ///
310   /// Since no other object in the module can have references into the body of a
311   /// function, dropping all references deletes the entire body of the function,
312   /// including any contained basic blocks.
313   ///
314   void dropAllReferences();
315   
316   static unsigned getBasicBlockListOffset() {
317     Function *Obj = 0;
318     return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
319   }
320   static unsigned getArgumentListOffset() {
321     Function *Obj = 0;
322     return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
323   }
324 };
325
326 inline ValueSymbolTable *
327 ilist_traits<BasicBlock>::getSymTab(Function *F) {
328   return F ? &F->getValueSymbolTable() : 0;
329 }
330
331 inline ValueSymbolTable *
332 ilist_traits<Argument>::getSymTab(Function *F) {
333   return F ? &F->getValueSymbolTable() : 0;
334 }
335
336 inline int 
337 ilist_traits<BasicBlock>::getListOffset() {
338   return Function::getBasicBlockListOffset();
339 }
340
341 inline int 
342 ilist_traits<Argument>::getListOffset() {
343   return Function::getArgumentListOffset();
344 }
345
346
347 } // End llvm namespace
348
349 #endif