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