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