Cast NULL when requested.
[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) { return X::classof(Val); }
192
193
194 // cast<X> - Return the argument parameter cast to the specified type.  This
195 // casting operator asserts that the type is correct, so it does not return null
196 // on failure.  But it will correctly return NULL when the input is NULL.
197 // Used Like this:
198 //
199 //  cast<      Instruction>(myVal)->getParent()
200 //  cast<const Instruction>(myVal)->getParent()
201 //
202 template <class X, class Y>
203 inline X *cast(Y Val) {
204   assert((Val == 0 || isa<X>(Val)) && "Invalid cast argument type!");
205   return (X*)(real_type<Y>::Type)Val;
206 }
207
208
209 // dyn_cast<X> - Return the argument parameter cast to the specified type.  This
210 // casting operator returns null if the argument is of the wrong type, so it can
211 // be used to test for a type as well as cast if successful.  This should be
212 // used in the context of an if statement like this:
213 //
214 //  if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
215 //
216
217 template <class X, class Y>
218 inline X *dyn_cast(Y Val) {
219   return isa<X>(Val) ? cast<X>(Val) : 0;
220 }
221
222
223 // isa - Provide some specializations of isa so that we have to include the
224 // subtype header files to test to see if the value is a subclass...
225 //
226 template <> inline bool isa<Type, const Value*>(const Value *Val) { 
227   return Val->getValueType() == Value::TypeVal;
228 }
229 template <> inline bool isa<Type, Value*>(Value *Val) { 
230   return Val->getValueType() == Value::TypeVal;
231 }
232 template <> inline bool isa<ConstPoolVal, const Value*>(const Value *Val) { 
233   return Val->getValueType() == Value::ConstantVal; 
234 }
235 template <> inline bool isa<ConstPoolVal, Value*>(Value *Val) { 
236   return Val->getValueType() == Value::ConstantVal; 
237 }
238 template <> inline bool isa<MethodArgument, const Value*>(const Value *Val) { 
239   return Val->getValueType() == Value::MethodArgumentVal;
240 }
241 template <> inline bool isa<MethodArgument, Value*>(Value *Val) { 
242   return Val->getValueType() == Value::MethodArgumentVal;
243 }
244 template <> inline bool isa<Instruction, const Value*>(const Value *Val) { 
245   return Val->getValueType() == Value::InstructionVal;
246 }
247 template <> inline bool isa<Instruction, Value*>(Value *Val) { 
248   return Val->getValueType() == Value::InstructionVal;
249 }
250 template <> inline bool isa<BasicBlock, const Value*>(const Value *Val) { 
251   return Val->getValueType() == Value::BasicBlockVal;
252 }
253 template <> inline bool isa<BasicBlock, Value*>(Value *Val) { 
254   return Val->getValueType() == Value::BasicBlockVal;
255 }
256 template <> inline bool isa<Method, const Value*>(const Value *Val) { 
257   return Val->getValueType() == Value::MethodVal;
258 }
259 template <> inline bool isa<Method, Value*>(Value *Val) { 
260   return Val->getValueType() == Value::MethodVal;
261 }
262 template <> inline bool isa<GlobalVariable, const Value*>(const Value *Val) { 
263   return Val->getValueType() == Value::GlobalVariableVal;
264 }
265 template <> inline bool isa<GlobalVariable, Value*>(Value *Val) { 
266   return Val->getValueType() == Value::GlobalVariableVal;
267 }
268 template <> inline bool isa<GlobalValue, const Value*>(const Value *Val) { 
269   return isa<GlobalVariable>(Val) || isa<Method>(Val);
270 }
271 template <> inline bool isa<GlobalValue, Value*>(Value *Val) { 
272   return isa<GlobalVariable>(Val) || isa<Method>(Val);
273 }
274 template <> inline bool isa<Module, const Value*>(const Value *Val) { 
275   return Val->getValueType() == Value::ModuleVal;
276 }
277 template <> inline bool isa<Module, Value*>(Value *Val) { 
278   return Val->getValueType() == Value::ModuleVal;
279 }
280
281 #endif