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