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