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