1 //===-- llvm/Value.h - Definition of the Value class -------------*- C++ -*--=//
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...
6 // This file also defines the Use<> template for users of value.
8 // This file also defines the isa<X>(), cast<X>(), and dyn_cast<X>() templates.
10 //===----------------------------------------------------------------------===//
16 #include "llvm/Annotation.h"
17 #include "llvm/AbstractTypeUser.h"
27 typedef Function Method;
31 template<class ValueSubclass, class ItemParentType, class SymTabType>
34 //===----------------------------------------------------------------------===//
36 //===----------------------------------------------------------------------===//
38 class Value : public Annotable, // Values are annotable
39 public AbstractTypeUser { // Values use potentially abstract types
42 TypeVal, // This is an instance of Type
43 ConstantVal, // This is an instance of Constant
44 MethodArgumentVal, // This is an instance of MethodArgument
45 InstructionVal, // This is an instance of Instruction
46 BasicBlockVal, // This is an instance of BasicBlock
47 MethodVal, // This is an instance of Method
48 GlobalVariableVal, // This is an instance of GlobalVariable
49 ModuleVal, // This is an instance of Module
53 std::vector<User *> Uses;
55 PATypeHandle<Type> Ty;
58 Value(const Value &); // Do not implement
60 inline void setType(const Type *ty) { Ty = ty; }
62 Value(const Type *Ty, ValueTy vty, const std::string &name = "");
65 // Support for debugging
68 // All values can potentially be typed
69 inline const Type *getType() const { return Ty; }
71 // All values can potentially be named...
72 inline bool hasName() const { return Name != ""; }
73 inline const std::string &getName() const { return Name; }
75 virtual void setName(const std::string &name, SymbolTable * = 0) {
79 // Methods for determining the subtype of this Value. The getValueType()
80 // method returns the type of the value directly. The cast*() methods are
81 // equivalent to using dynamic_cast<>... if the cast is successful, this is
82 // returned, otherwise you get a null pointer.
84 // The family of functions Val->cast<type>Asserting() is used in the same
85 // way as the Val->cast<type>() instructions, but they assert the expected
86 // type instead of checking it at runtime.
88 inline ValueTy getValueType() const { return VTy; }
90 // replaceAllUsesWith - Go through the uses list for this definition and make
91 // each use point to "D" instead of "this". After this completes, 'this's
92 // use list should be empty.
94 void replaceAllUsesWith(Value *D);
96 // refineAbstractType - This function is implemented because we use
97 // potentially abstract types, and these types may be resolved to more
98 // concrete types after we are constructed.
100 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
102 //----------------------------------------------------------------------
103 // Methods for handling the vector of uses of this Value.
105 typedef std::vector<User*>::iterator use_iterator;
106 typedef std::vector<User*>::const_iterator use_const_iterator;
108 inline unsigned use_size() const { return Uses.size(); }
109 inline bool use_empty() const { return Uses.empty(); }
110 inline use_iterator use_begin() { return Uses.begin(); }
111 inline use_const_iterator use_begin() const { return Uses.begin(); }
112 inline use_iterator use_end() { return Uses.end(); }
113 inline use_const_iterator use_end() const { return Uses.end(); }
114 inline User *use_back() { return Uses.back(); }
115 inline const User *use_back() const { return Uses.back(); }
117 inline void use_push_back(User *I) { Uses.push_back(I); }
118 User *use_remove(use_iterator &I);
120 inline void addUse(User *I) { Uses.push_back(I); }
121 void killUse(User *I);
125 //===----------------------------------------------------------------------===//
127 //===----------------------------------------------------------------------===//
129 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
130 // list of a definition node up-to-date really easy.
132 template<class ValueSubclass>
137 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
139 if (Val) Val->addUse(U);
142 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
144 inline operator ValueSubclass *() const { return Val; }
146 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
151 inline ValueSubclass *operator=(ValueSubclass *V) {
152 if (Val) Val->killUse(U);
158 inline ValueSubclass *operator->() { return Val; }
159 inline const ValueSubclass *operator->() const { return Val; }
161 inline ValueSubclass *get() { return Val; }
162 inline const ValueSubclass *get() const { return Val; }
164 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
165 if (Val) Val->killUse(U);
172 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
174 // real_type - Provide a macro to get the real type of a value that might be
175 // a use. This provides a typedef 'Type' that is the argument type for all
176 // non UseTy types, and is the contained pointer type of the use if it is a
179 template <class X> class real_type { typedef X Type; };
180 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
182 //===----------------------------------------------------------------------===//
183 // Type Checking Templates
184 //===----------------------------------------------------------------------===//
186 // isa<X> - Return true if the parameter to the template is an instance of the
187 // template type argument. Used like this:
189 // if (isa<Type>(myVal)) { ... }
191 template <class X, class Y>
192 inline bool isa(Y Val) {
193 assert(Val && "isa<Ty>(NULL) invoked!");
194 return X::classof(Val);
198 // cast<X> - Return the argument parameter cast to the specified type. This
199 // casting operator asserts that the type is correct, so it does not return null
200 // on failure. But it will correctly return NULL when the input is NULL.
203 // cast< Instruction>(myVal)->getParent()
204 // cast<const Instruction>(myVal)->getParent()
206 template <class X, class Y>
207 inline X *cast(Y Val) {
208 assert(isa<X>(Val) && "cast<Ty>() argument of uncompatible type!");
209 return (X*)(real_type<Y>::Type)Val;
212 // cast_or_null<X> - Functionally identical to cast, except that a null value is
215 template <class X, class Y>
216 inline X *cast_or_null(Y Val) {
217 assert((Val == 0 || isa<X>(Val)) &&
218 "cast_or_null<Ty>() argument of uncompatible type!");
219 return (X*)(real_type<Y>::Type)Val;
223 // dyn_cast<X> - Return the argument parameter cast to the specified type. This
224 // casting operator returns null if the argument is of the wrong type, so it can
225 // be used to test for a type as well as cast if successful. This should be
226 // used in the context of an if statement like this:
228 // if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
231 template <class X, class Y>
232 inline X *dyn_cast(Y Val) {
233 return isa<X>(Val) ? cast<X>(Val) : 0;
236 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
237 // value is accepted.
239 template <class X, class Y>
240 inline X *dyn_cast_or_null(Y Val) {
241 return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
245 // isa - Provide some specializations of isa so that we have to include the
246 // subtype header files to test to see if the value is a subclass...
248 template <> inline bool isa<Type, const Value*>(const Value *Val) {
249 return Val->getValueType() == Value::TypeVal;
251 template <> inline bool isa<Type, Value*>(Value *Val) {
252 return Val->getValueType() == Value::TypeVal;
254 template <> inline bool isa<Constant, const Value*>(const Value *Val) {
255 return Val->getValueType() == Value::ConstantVal;
257 template <> inline bool isa<Constant, Value*>(Value *Val) {
258 return Val->getValueType() == Value::ConstantVal;
260 template <> inline bool isa<MethodArgument, const Value*>(const Value *Val) {
261 return Val->getValueType() == Value::MethodArgumentVal;
263 template <> inline bool isa<MethodArgument, Value*>(Value *Val) {
264 return Val->getValueType() == Value::MethodArgumentVal;
266 template <> inline bool isa<Instruction, const Value*>(const Value *Val) {
267 return Val->getValueType() == Value::InstructionVal;
269 template <> inline bool isa<Instruction, Value*>(Value *Val) {
270 return Val->getValueType() == Value::InstructionVal;
272 template <> inline bool isa<BasicBlock, const Value*>(const Value *Val) {
273 return Val->getValueType() == Value::BasicBlockVal;
275 template <> inline bool isa<BasicBlock, Value*>(Value *Val) {
276 return Val->getValueType() == Value::BasicBlockVal;
278 template <> inline bool isa<Function, const Value*>(const Value *Val) {
279 return Val->getValueType() == Value::MethodVal;
281 template <> inline bool isa<Function, Value*>(Value *Val) {
282 return Val->getValueType() == Value::MethodVal;
284 template <> inline bool isa<GlobalVariable, const Value*>(const Value *Val) {
285 return Val->getValueType() == Value::GlobalVariableVal;
287 template <> inline bool isa<GlobalVariable, Value*>(Value *Val) {
288 return Val->getValueType() == Value::GlobalVariableVal;
290 template <> inline bool isa<GlobalValue, const Value*>(const Value *Val) {
291 return isa<GlobalVariable>(Val) || isa<Function>(Val);
293 template <> inline bool isa<GlobalValue, Value*>(Value *Val) {
294 return isa<GlobalVariable>(Val) || isa<Function>(Val);
296 template <> inline bool isa<Module, const Value*>(const Value *Val) {
297 return Val->getValueType() == Value::ModuleVal;
299 template <> inline bool isa<Module, Value*>(Value *Val) {
300 return Val->getValueType() == Value::ModuleVal;