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