29046ad78227efeb8a7038c58248fef4b38b77fb
[oota-llvm.git] / include / llvm / Function.h
1 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
2 //
3 // This file contains the declaration of the Function class, which represents a 
4 // single function/procedure in LLVM.
5 //
6 // A function basically consists of a list of basic blocks, a list of arguments,
7 // and a symbol table.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_FUNCTION_H
12 #define LLVM_FUNCTION_H
13
14 #include "llvm/GlobalValue.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/Argument.h"
17
18 class FunctionType;
19
20 // Traits for intrusive list of instructions...
21 template<> struct ilist_traits<BasicBlock>
22   : public SymbolTableListTraits<BasicBlock, Function, Function> {
23
24   // createNode is used to create a node that marks the end of the list...
25   static BasicBlock *createNode();
26
27   static iplist<BasicBlock> &getList(Function *F);
28 };
29
30 template<> struct ilist_traits<Argument>
31   : public SymbolTableListTraits<Argument, Function, Function> {
32
33   // createNode is used to create a node that marks the end of the list...
34   static Argument *createNode();
35   static iplist<Argument> &getList(Function *F);
36 };
37
38 class Function : public GlobalValue {
39 public:
40   typedef iplist<Argument> ArgumentListType;
41   typedef iplist<BasicBlock> BasicBlockListType;
42
43   // BasicBlock iterators...
44   typedef BasicBlockListType::iterator iterator;
45   typedef BasicBlockListType::const_iterator const_iterator;
46   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
47   typedef std::reverse_iterator<iterator>             reverse_iterator;
48
49   typedef ArgumentListType::iterator aiterator;
50   typedef ArgumentListType::const_iterator const_aiterator;
51   typedef std::reverse_iterator<const_aiterator> const_reverse_aiterator;
52   typedef std::reverse_iterator<aiterator>             reverse_aiterator;
53
54 private:
55
56   // Important things that make up a function!
57   BasicBlockListType  BasicBlocks;      // The basic blocks
58   ArgumentListType ArgumentList;        // The formal arguments
59
60   SymbolTable *SymTab;
61   
62   friend class SymbolTableListTraits<Function, Module, Module>;
63
64   void setParent(Module *parent);
65   Function *Prev, *Next;
66   void setNext(Function *N) { Next = N; }
67   void setPrev(Function *N) { Prev = N; }
68
69 public:
70   /// Function ctor - If the (optional) Module argument is specified, the
71   /// function is automatically inserted into the end of the function list for
72   /// the module.
73   ///
74   Function(const FunctionType *Ty, LinkageTypes Linkage,
75            const std::string &N = "", Module *M = 0);
76   ~Function();
77
78   // Specialize setName to handle symbol table majik...
79   virtual void setName(const std::string &name, SymbolTable *ST = 0);
80
81   const Type *getReturnType() const;           // Return the type of the ret val
82   const FunctionType *getFunctionType() const; // Return the FunctionType for me
83
84   /// isExternal - Is the body of this function unknown? (the basic block list
85   /// is empty if so) this is true for external functions, defined as forward
86   /// "declare"ations
87   ///
88   virtual bool isExternal() const { return BasicBlocks.empty(); }
89
90   /// getIntrinsicID - This method returns the ID number of the specified
91   /// function, or LLVMIntrinsic::not_intrinsic if the function is not an
92   /// instrinsic, or if the pointer is null.  This value is always defined to be
93   /// zero to allow easy checking for whether a function is intrinsic or not.
94   /// The particular intrinsic functions which correspond to this value are
95   /// defined in llvm/Intrinsics.h.
96   ///
97   unsigned getIntrinsicID() const;
98   bool isIntrinsic() const { return getIntrinsicID() != 0; }
99
100   /// deleteBody - This method deletes the body of the function, and converts
101   /// the linkage to external.
102   void deleteBody() {
103     dropAllReferences();
104     setLinkage(ExternalLinkage);
105   }
106
107   // getNext/Prev - Return the next or previous function in the list.  These
108   // methods should never be used directly, and are only used to implement the
109   // function list as part of the module.
110   //
111         Function *getNext()       { return Next; }
112   const Function *getNext() const { return Next; }
113         Function *getPrev()       { return Prev; }
114   const Function *getPrev() const { return Prev; }
115
116   /// Get the underlying elements of the Function... the basic block list is
117   /// empty for external functions.
118   ///
119   const ArgumentListType &getArgumentList() const { return ArgumentList; }
120         ArgumentListType &getArgumentList()       { return ArgumentList; }
121
122   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
123         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
124
125   const BasicBlock       &getEntryBlock() const   { return front(); }
126         BasicBlock       &getEntryBlock()         { return front(); }
127
128   //===--------------------------------------------------------------------===//
129   // Symbol Table Accessing functions...
130
131   /// getSymbolTable() - Return the symbol table...
132   ///
133   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
134   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
135
136   
137   //===--------------------------------------------------------------------===//
138   // BasicBlock iterator forwarding functions
139   //
140   iterator                begin()       { return BasicBlocks.begin(); }
141   const_iterator          begin() const { return BasicBlocks.begin(); }
142   iterator                end  ()       { return BasicBlocks.end();   }
143   const_iterator          end  () const { return BasicBlocks.end();   }
144
145   reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
146   const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
147   reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
148   const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
149
150   unsigned                 size() const { return BasicBlocks.size(); }
151   bool                    empty() const { return BasicBlocks.empty(); }
152   const BasicBlock       &front() const { return BasicBlocks.front(); }
153         BasicBlock       &front()       { return BasicBlocks.front(); }
154   const BasicBlock        &back() const { return BasicBlocks.back(); }
155         BasicBlock        &back()       { return BasicBlocks.back(); }
156
157   //===--------------------------------------------------------------------===//
158   // Argument iterator forwarding functions
159   //
160   aiterator                abegin()       { return ArgumentList.begin(); }
161   const_aiterator          abegin() const { return ArgumentList.begin(); }
162   aiterator                aend  ()       { return ArgumentList.end();   }
163   const_aiterator          aend  () const { return ArgumentList.end();   }
164
165   reverse_aiterator       arbegin()       { return ArgumentList.rbegin(); }
166   const_reverse_aiterator arbegin() const { return ArgumentList.rbegin(); }
167   reverse_aiterator       arend  ()       { return ArgumentList.rend();   }
168   const_reverse_aiterator arend  () const { return ArgumentList.rend();   }
169
170   unsigned                  asize() const { return ArgumentList.size(); }
171   bool                     aempty() const { return ArgumentList.empty(); }
172   const Argument          &afront() const { return ArgumentList.front(); }
173         Argument          &afront()       { return ArgumentList.front(); }
174   const Argument           &aback() const { return ArgumentList.back(); }
175         Argument           &aback()       { return ArgumentList.back(); }
176
177   virtual void print(std::ostream &OS) const;
178
179   /// Methods for support type inquiry through isa, cast, and dyn_cast:
180   static inline bool classof(const Function *) { return true; }
181   static inline bool classof(const Value *V) {
182     return V->getValueType() == Value::FunctionVal;
183   }
184
185   /// dropAllReferences() - This method causes all the subinstructions to "let
186   /// go" of all references that they are maintaining.  This allows one to
187   /// 'delete' a whole module at a time, even though there may be circular
188   /// references... first all references are dropped, and all use counts go to
189   /// zero.  Then everything is delete'd for real.  Note that no operations are
190   /// valid on an object that has "dropped all references", except operator 
191   /// delete.
192   ///
193   /// Since no other object in the module can have references into the body of a
194   /// function, dropping all references deletes the entire body of the function,
195   /// including any contained basic blocks.
196   ///
197   void dropAllReferences();
198 };
199
200 #endif