3ef46cac86a12008cedabcdbb62f0855c579977e
[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
102   CAST_FN(Constant      ,       ConstPoolVal  )
103   CAST_FN(MethodArgument,       MethodArgument)
104   CAST_FN(Instruction   ,       Instruction   )
105   CAST_FN(BasicBlock    ,       BasicBlock    )
106   CAST_FN(Method        ,       Method        )
107   CAST_FN(Global        ,       GlobalVariable)
108 #undef CAST_FN
109
110   // replaceAllUsesWith - Go through the uses list for this definition and make
111   // each use point to "D" instead of "this".  After this completes, 'this's 
112   // use list should be empty.
113   //
114   void replaceAllUsesWith(Value *D);
115
116   // refineAbstractType - This function is implemented because we use
117   // potentially abstract types, and these types may be resolved to more
118   // concrete types after we are constructed.
119   //
120   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
121   
122   //----------------------------------------------------------------------
123   // Methods for handling the vector of uses of this Value.
124   //
125   typedef vector<User*>::iterator       use_iterator;
126   typedef vector<User*>::const_iterator use_const_iterator;
127
128   inline unsigned           use_size()  const { return Uses.size();  }
129   inline bool               use_empty() const { return Uses.empty(); }
130   inline use_iterator       use_begin()       { return Uses.begin(); }
131   inline use_const_iterator use_begin() const { return Uses.begin(); }
132   inline use_iterator       use_end()         { return Uses.end();   }
133   inline use_const_iterator use_end()   const { return Uses.end();   }
134
135   inline void use_push_back(User *I)   { Uses.push_back(I); }
136   User *use_remove(use_iterator &I);
137
138   inline void addUse(User *I)      { Uses.push_back(I); }
139   void killUse(User *I);
140 };
141
142
143 //===----------------------------------------------------------------------===//
144 //                                 UseTy Class
145 //===----------------------------------------------------------------------===//
146
147 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
148 // list of a definition node up-to-date really easy.
149 //
150 template<class ValueSubclass>
151 class UseTy {
152   ValueSubclass *Val;
153   User *U;
154 public:
155   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
156     Val = v; U = user;
157     if (Val) Val->addUse(U);
158   }
159
160   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
161
162   inline operator ValueSubclass *() const { return Val; }
163
164   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
165     Val = 0;
166     U = user.U;
167     operator=(user.Val);
168   }
169   inline ValueSubclass *operator=(ValueSubclass *V) { 
170     if (Val) Val->killUse(U);
171     Val = V;
172     if (V) V->addUse(U);
173     return V;
174   }
175
176   inline       ValueSubclass *operator->()       { return Val; }
177   inline const ValueSubclass *operator->() const { return Val; }
178
179   inline       ValueSubclass *get()       { return Val; }
180   inline const ValueSubclass *get() const { return Val; }
181
182   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
183     if (Val) Val->killUse(U);
184     Val = user.Val;
185     Val->addUse(U);
186     return *this;
187   }
188 };
189
190 typedef UseTy<Value> Use;    // Provide Use as a common UseTy type
191
192 // real_type - Provide a macro to get the real type of a value that might be 
193 // a use.  This provides a typedef 'Type' that is the argument type for all
194 // non UseTy types, and is the contained pointer type of the use if it is a
195 // UseTy.
196 //
197 template <class X> class real_type { typedef X Type; };
198 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
199
200 //===----------------------------------------------------------------------===//
201 //                          Type Checking Templates
202 //===----------------------------------------------------------------------===//
203
204 // isa<X> - Return true if the parameter to the template is an instance of the
205 // template type argument.  Used like this:
206 //
207 //  if (isa<Type>(myVal)) { ... }
208 //
209 template <class X, class Y>
210 bool isa(Y Val) { return X::isa(Val); }
211
212
213 // cast<X> - Return the argument parameter cast to the specified type.  This
214 // casting operator asserts that the type is correct, so it does not return null
215 // on failure.  Used Like this:
216 //
217 //  cast<      Instruction>(myVal)->getParent()
218 //  cast<const Instruction>(myVal)->getParent()
219 //
220 template <class X, class Y>
221 X *cast(Y Val) {
222   assert(isa<X>(Val) && "Invalid cast argument type!");
223   return (X*)(real_type<Y>::Type)Val;
224 }
225
226
227 // dyn_cast<X> - Return the argument parameter cast to the specified type.  This
228 // casting operator returns null if the argument is of the wrong type, so it can
229 // be used to test for a type as well as cast if successful.  This should be
230 // used in the context of an if statement like this:
231 //
232 //  if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
233 //
234
235 template <class X, class Y>
236 X *dyn_cast(Y Val) {
237   return isa<X>(Val) ? cast<X>(Val) : 0;
238 }
239
240
241 // isa - Provide some specializations of isa so that we have to include the
242 // subtype header files to test to see if the value is a subclass...
243 //
244 template <> bool isa<Type, Value*>(Value *Val) { 
245   return Val->getValueType() == Value::TypeVal;
246 }
247 template <> bool isa<ConstPoolVal, Value*>(Value *Val) { 
248   return Val->getValueType() == Value::ConstantVal; 
249 }
250 template <> bool isa<MethodArgument, Value*>(Value *Val) { 
251   return Val->getValueType() == Value::MethodArgumentVal;
252 }
253 template <> bool isa<Instruction, Value*>(Value *Val) { 
254   return Val->getValueType() == Value::InstructionVal;
255 }
256 template <> bool isa<BasicBlock, Value*>(Value *Val) { 
257   return Val->getValueType() == Value::BasicBlockVal;
258 }
259 template <> bool isa<Method, Value*>(Value *Val) { 
260   return Val->getValueType() == Value::MethodVal;
261 }
262 template <> bool isa<GlobalVariable, Value*>(Value *Val) { 
263   return Val->getValueType() == Value::GlobalVal;
264 }
265 template <> bool isa<Module, Value*>(Value *Val) { 
266   return Val->getValueType() == Value::ModuleVal;
267 }
268
269 #endif