1c97d14095a6fcaf031ee58e8542a3aa2054dcc4
[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
25 class FunctionType;
26
27 // Traits for intrusive list of instructions...
28 template<> struct ilist_traits<BasicBlock>
29   : public SymbolTableListTraits<BasicBlock, Function, Function> {
30
31   // createNode is used to create a node that marks the end of the list...
32   static BasicBlock *createNode();
33
34   static iplist<BasicBlock> &getList(Function *F);
35 };
36
37 template<> struct ilist_traits<Argument>
38   : public SymbolTableListTraits<Argument, Function, Function> {
39
40   // createNode is used to create a node that marks the end of the list...
41   static Argument *createNode();
42   static iplist<Argument> &getList(Function *F);
43 };
44
45 class Function : public GlobalValue {
46 public:
47   typedef iplist<Argument> ArgumentListType;
48   typedef iplist<BasicBlock> BasicBlockListType;
49
50   // BasicBlock iterators...
51   typedef BasicBlockListType::iterator iterator;
52   typedef BasicBlockListType::const_iterator const_iterator;
53   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
54   typedef std::reverse_iterator<iterator>             reverse_iterator;
55
56   typedef ArgumentListType::iterator aiterator;
57   typedef ArgumentListType::const_iterator const_aiterator;
58   typedef std::reverse_iterator<const_aiterator> const_reverse_aiterator;
59   typedef std::reverse_iterator<aiterator>             reverse_aiterator;
60
61 private:
62
63   // Important things that make up a function!
64   BasicBlockListType  BasicBlocks;      // The basic blocks
65   ArgumentListType ArgumentList;        // The formal arguments
66
67   SymbolTable *SymTab;
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   // Specialize setName to handle symbol table majik...
86   virtual void setName(const std::string &name, SymbolTable *ST = 0);
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   /// isExternal - Is the body of this function unknown? (the basic block list
92   /// is empty if so) this is true for external functions, defined as forward
93   /// "declare"ations
94   ///
95   virtual bool isExternal() const { return BasicBlocks.empty(); }
96
97   /// getIntrinsicID - This method returns the ID number of the specified
98   /// function, or LLVMIntrinsic::not_intrinsic if the function is not an
99   /// instrinsic, or if the pointer is null.  This value is always defined to be
100   /// zero to allow easy checking for whether a function is intrinsic or not.
101   /// The particular intrinsic functions which correspond to this value are
102   /// defined in llvm/Intrinsics.h.
103   ///
104   unsigned getIntrinsicID() const;
105   bool isIntrinsic() const { return getIntrinsicID() != 0; }
106
107   /// deleteBody - This method deletes the body of the function, and converts
108   /// the linkage to external.
109   void deleteBody() {
110     dropAllReferences();
111     setLinkage(ExternalLinkage);
112   }
113
114   // getNext/Prev - Return the next or previous function in the list.  These
115   // methods should never be used directly, and are only used to implement the
116   // function list as part of the module.
117   //
118         Function *getNext()       { return Next; }
119   const Function *getNext() const { return Next; }
120         Function *getPrev()       { return Prev; }
121   const Function *getPrev() const { return Prev; }
122
123   /// Get the underlying elements of the Function... the basic block list is
124   /// empty for external functions.
125   ///
126   const ArgumentListType &getArgumentList() const { return ArgumentList; }
127         ArgumentListType &getArgumentList()       { return ArgumentList; }
128
129   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
130         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
131
132   const BasicBlock       &getEntryBlock() const   { return front(); }
133         BasicBlock       &getEntryBlock()         { return front(); }
134
135   //===--------------------------------------------------------------------===//
136   // Symbol Table Accessing functions...
137
138   /// getSymbolTable() - Return the symbol table...
139   ///
140   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
141   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
142
143   
144   //===--------------------------------------------------------------------===//
145   // BasicBlock iterator forwarding functions
146   //
147   iterator                begin()       { return BasicBlocks.begin(); }
148   const_iterator          begin() const { return BasicBlocks.begin(); }
149   iterator                end  ()       { return BasicBlocks.end();   }
150   const_iterator          end  () const { return BasicBlocks.end();   }
151
152   reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
153   const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
154   reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
155   const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
156
157   unsigned                 size() const { return BasicBlocks.size(); }
158   bool                    empty() const { return BasicBlocks.empty(); }
159   const BasicBlock       &front() const { return BasicBlocks.front(); }
160         BasicBlock       &front()       { return BasicBlocks.front(); }
161   const BasicBlock        &back() const { return BasicBlocks.back(); }
162         BasicBlock        &back()       { return BasicBlocks.back(); }
163
164   //===--------------------------------------------------------------------===//
165   // Argument iterator forwarding functions
166   //
167   aiterator                abegin()       { return ArgumentList.begin(); }
168   const_aiterator          abegin() const { return ArgumentList.begin(); }
169   aiterator                aend  ()       { return ArgumentList.end();   }
170   const_aiterator          aend  () const { return ArgumentList.end();   }
171
172   reverse_aiterator       arbegin()       { return ArgumentList.rbegin(); }
173   const_reverse_aiterator arbegin() const { return ArgumentList.rbegin(); }
174   reverse_aiterator       arend  ()       { return ArgumentList.rend();   }
175   const_reverse_aiterator arend  () const { return ArgumentList.rend();   }
176
177   unsigned                  asize() const { return ArgumentList.size(); }
178   bool                     aempty() const { return ArgumentList.empty(); }
179   const Argument          &afront() const { return ArgumentList.front(); }
180         Argument          &afront()       { return ArgumentList.front(); }
181   const Argument           &aback() const { return ArgumentList.back(); }
182         Argument           &aback()       { return ArgumentList.back(); }
183
184   virtual void print(std::ostream &OS) const;
185
186   /// Methods for support type inquiry through isa, cast, and dyn_cast:
187   static inline bool classof(const Function *) { return true; }
188   static inline bool classof(const Value *V) {
189     return V->getValueType() == Value::FunctionVal;
190   }
191
192   /// dropAllReferences() - This method causes all the subinstructions to "let
193   /// go" of all references that they are maintaining.  This allows one to
194   /// 'delete' a whole module at a time, even though there may be circular
195   /// references... first all references are dropped, and all use counts go to
196   /// zero.  Then everything is delete'd for real.  Note that no operations are
197   /// valid on an object that has "dropped all references", except operator 
198   /// delete.
199   ///
200   /// Since no other object in the module can have references into the body of a
201   /// function, dropping all references deletes the entire body of the function,
202   /// including any contained basic blocks.
203   ///
204   void dropAllReferences();
205 };
206
207 #endif