Rename Method to Function
[oota-llvm.git] / include / llvm / Value.h
1 //===-- llvm/Value.h - Definition of the Value class -------------*- C++ -*--=//
2 //
3 // This file defines the very important Value class.  This is subclassed by a
4 // bunch of other important classes, like Def, Method, Module, Type, etc...
5 //
6 // This file also defines the Use<> template for users of value.
7 //
8 // This file also defines the isa<X>(), cast<X>(), and dyn_cast<X>() templates.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_VALUE_H
13 #define LLVM_VALUE_H
14
15 #include <vector>
16 #include "llvm/Annotation.h"
17 #include "llvm/AbstractTypeUser.h"
18
19 class User;
20 class Type;
21 class Constant;
22 class MethodArgument;
23 class Instruction;
24 class BasicBlock;
25 class GlobalValue;
26 class Function;
27 typedef Function Method;
28 class GlobalVariable;
29 class Module;
30 class SymbolTable;
31 template<class ValueSubclass, class ItemParentType, class SymTabType> 
32   class ValueHolder;
33
34 //===----------------------------------------------------------------------===//
35 //                                 Value Class
36 //===----------------------------------------------------------------------===//
37
38 class Value : public Annotable,         // Values are annotable
39               public AbstractTypeUser { // Values use potentially abstract types
40 public:
41   enum ValueTy {
42     TypeVal,                // This is an instance of Type
43     ConstantVal,            // This is an instance of Constant
44     MethodArgumentVal,      // This is an instance of MethodArgument
45     InstructionVal,         // This is an instance of Instruction
46     BasicBlockVal,          // This is an instance of BasicBlock
47     MethodVal,              // This is an instance of Method
48     GlobalVariableVal,      // This is an instance of GlobalVariable
49     ModuleVal,              // This is an instance of Module
50   };
51
52 private:
53   std::vector<User *> Uses;
54   std::string Name;
55   PATypeHandle<Type> Ty;
56   ValueTy VTy;
57
58   Value(const Value &);              // Do not implement
59 protected:
60   inline void setType(const Type *ty) { Ty = ty; }
61 public:
62   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
63   virtual ~Value();
64   
65   // Support for debugging 
66   void dump() const;
67   
68   // All values can potentially be typed
69   inline const Type *getType() const { return Ty; }
70   
71   // All values can potentially be named...
72   inline bool               hasName() const { return Name != ""; }
73   inline const std::string &getName() const { return Name; }
74
75   virtual void setName(const std::string &name, SymbolTable * = 0) {
76     Name = name;
77   }
78   
79   // Methods for determining the subtype of this Value.  The getValueType()
80   // method returns the type of the value directly.  The cast*() methods are
81   // equivalent to using dynamic_cast<>... if the cast is successful, this is
82   // returned, otherwise you get a null pointer.
83   //
84   // The family of functions Val->cast<type>Asserting() is used in the same
85   // way as the Val->cast<type>() instructions, but they assert the expected
86   // type instead of checking it at runtime.
87   //
88   inline ValueTy getValueType() const { return VTy; }
89   
90   // replaceAllUsesWith - Go through the uses list for this definition and make
91   // each use point to "D" instead of "this".  After this completes, 'this's 
92   // use list should be empty.
93   //
94   void replaceAllUsesWith(Value *D);
95
96   // refineAbstractType - This function is implemented because we use
97   // potentially abstract types, and these types may be resolved to more
98   // concrete types after we are constructed.
99   //
100   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
101   
102   //----------------------------------------------------------------------
103   // Methods for handling the vector of uses of this Value.
104   //
105   typedef std::vector<User*>::iterator       use_iterator;
106   typedef std::vector<User*>::const_iterator use_const_iterator;
107
108   inline unsigned           use_size()  const { return Uses.size();  }
109   inline bool               use_empty() const { return Uses.empty(); }
110   inline use_iterator       use_begin()       { return Uses.begin(); }
111   inline use_const_iterator use_begin() const { return Uses.begin(); }
112   inline use_iterator       use_end()         { return Uses.end();   }
113   inline use_const_iterator use_end()   const { return Uses.end();   }
114   inline User              *use_back()        { return Uses.back();  }
115   inline const User        *use_back()  const { return Uses.back();  }
116
117   inline void use_push_back(User *I)   { Uses.push_back(I); }
118   User *use_remove(use_iterator &I);
119
120   inline void addUse(User *I)      { Uses.push_back(I); }
121   void killUse(User *I);
122 };
123
124
125 //===----------------------------------------------------------------------===//
126 //                                 UseTy Class
127 //===----------------------------------------------------------------------===//
128
129 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
130 // list of a definition node up-to-date really easy.
131 //
132 template<class ValueSubclass>
133 class UseTy {
134   ValueSubclass *Val;
135   User *U;
136 public:
137   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
138     Val = v; U = user;
139     if (Val) Val->addUse(U);
140   }
141
142   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
143
144   inline operator ValueSubclass *() const { return Val; }
145
146   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
147     Val = 0;
148     U = user.U;
149     operator=(user.Val);
150   }
151   inline ValueSubclass *operator=(ValueSubclass *V) { 
152     if (Val) Val->killUse(U);
153     Val = V;
154     if (V) V->addUse(U);
155     return V;
156   }
157
158   inline       ValueSubclass *operator->()       { return Val; }
159   inline const ValueSubclass *operator->() const { return Val; }
160
161   inline       ValueSubclass *get()       { return Val; }
162   inline const ValueSubclass *get() const { return Val; }
163
164   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
165     if (Val) Val->killUse(U);
166     Val = user.Val;
167     Val->addUse(U);
168     return *this;
169   }
170 };
171
172 typedef UseTy<Value> Use;    // Provide Use as a common UseTy type
173
174 // real_type - Provide a macro to get the real type of a value that might be 
175 // a use.  This provides a typedef 'Type' that is the argument type for all
176 // non UseTy types, and is the contained pointer type of the use if it is a
177 // UseTy.
178 //
179 template <class X> class real_type { typedef X Type; };
180 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
181
182 //===----------------------------------------------------------------------===//
183 //                          Type Checking Templates
184 //===----------------------------------------------------------------------===//
185
186 // isa<X> - Return true if the parameter to the template is an instance of the
187 // template type argument.  Used like this:
188 //
189 //  if (isa<Type>(myVal)) { ... }
190 //
191 template <class X, class Y>
192 inline bool isa(Y Val) {
193   assert(Val && "isa<Ty>(NULL) invoked!");
194   return X::classof(Val);
195 }
196
197
198 // cast<X> - Return the argument parameter cast to the specified type.  This
199 // casting operator asserts that the type is correct, so it does not return null
200 // on failure.  But it will correctly return NULL when the input is NULL.
201 // Used Like this:
202 //
203 //  cast<      Instruction>(myVal)->getParent()
204 //  cast<const Instruction>(myVal)->getParent()
205 //
206 template <class X, class Y>
207 inline X *cast(Y Val) {
208   assert(isa<X>(Val) && "cast<Ty>() argument of uncompatible type!");
209   return (X*)(real_type<Y>::Type)Val;
210 }
211
212 // cast_or_null<X> - Functionally identical to cast, except that a null value is
213 // accepted.
214 //
215 template <class X, class Y>
216 inline X *cast_or_null(Y Val) {
217   assert((Val == 0 || isa<X>(Val)) &&
218          "cast_or_null<Ty>() argument of uncompatible type!");
219   return (X*)(real_type<Y>::Type)Val;
220 }
221
222
223 // dyn_cast<X> - Return the argument parameter cast to the specified type.  This
224 // casting operator returns null if the argument is of the wrong type, so it can
225 // be used to test for a type as well as cast if successful.  This should be
226 // used in the context of an if statement like this:
227 //
228 //  if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
229 //
230
231 template <class X, class Y>
232 inline X *dyn_cast(Y Val) {
233   return isa<X>(Val) ? cast<X>(Val) : 0;
234 }
235
236 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
237 // value is accepted.
238 //
239 template <class X, class Y>
240 inline X *dyn_cast_or_null(Y Val) {
241   return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
242 }
243
244
245 // isa - Provide some specializations of isa so that we have to include the
246 // subtype header files to test to see if the value is a subclass...
247 //
248 template <> inline bool isa<Type, const Value*>(const Value *Val) { 
249   return Val->getValueType() == Value::TypeVal;
250 }
251 template <> inline bool isa<Type, Value*>(Value *Val) { 
252   return Val->getValueType() == Value::TypeVal;
253 }
254 template <> inline bool isa<Constant, const Value*>(const Value *Val) { 
255   return Val->getValueType() == Value::ConstantVal; 
256 }
257 template <> inline bool isa<Constant, Value*>(Value *Val) { 
258   return Val->getValueType() == Value::ConstantVal; 
259 }
260 template <> inline bool isa<MethodArgument, const Value*>(const Value *Val) { 
261   return Val->getValueType() == Value::MethodArgumentVal;
262 }
263 template <> inline bool isa<MethodArgument, Value*>(Value *Val) { 
264   return Val->getValueType() == Value::MethodArgumentVal;
265 }
266 template <> inline bool isa<Instruction, const Value*>(const Value *Val) { 
267   return Val->getValueType() == Value::InstructionVal;
268 }
269 template <> inline bool isa<Instruction, Value*>(Value *Val) { 
270   return Val->getValueType() == Value::InstructionVal;
271 }
272 template <> inline bool isa<BasicBlock, const Value*>(const Value *Val) { 
273   return Val->getValueType() == Value::BasicBlockVal;
274 }
275 template <> inline bool isa<BasicBlock, Value*>(Value *Val) { 
276   return Val->getValueType() == Value::BasicBlockVal;
277 }
278 template <> inline bool isa<Function, const Value*>(const Value *Val) { 
279   return Val->getValueType() == Value::MethodVal;
280 }
281 template <> inline bool isa<Function, Value*>(Value *Val) { 
282   return Val->getValueType() == Value::MethodVal;
283 }
284 template <> inline bool isa<GlobalVariable, const Value*>(const Value *Val) { 
285   return Val->getValueType() == Value::GlobalVariableVal;
286 }
287 template <> inline bool isa<GlobalVariable, Value*>(Value *Val) { 
288   return Val->getValueType() == Value::GlobalVariableVal;
289 }
290 template <> inline bool isa<GlobalValue, const Value*>(const Value *Val) { 
291   return isa<GlobalVariable>(Val) || isa<Function>(Val);
292 }
293 template <> inline bool isa<GlobalValue, Value*>(Value *Val) { 
294   return isa<GlobalVariable>(Val) || isa<Function>(Val);
295 }
296 template <> inline bool isa<Module, const Value*>(const Value *Val) { 
297   return Val->getValueType() == Value::ModuleVal;
298 }
299 template <> inline bool isa<Module, Value*>(Value *Val) { 
300   return Val->getValueType() == Value::ModuleVal;
301 }
302
303 #endif