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