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"
29 template<class ValueSubclass, class ItemParentType, class SymTabType>
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
36 class Value : public Annotable, // Values are annotable
37 public AbstractTypeUser { // Values use potentially abstract types
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
53 PATypeHandle<Type> Ty;
56 Value(const Value &); // Do not implement
58 inline void setType(const Type *ty) { Ty = ty; }
60 Value(const Type *Ty, ValueTy vty, const string &name = "");
63 // Support for debugging
66 // All values can potentially be typed
67 inline const Type *getType() const { return Ty; }
69 // All values can potentially be named...
70 inline bool hasName() const { return Name != ""; }
71 inline const string &getName() const { return Name; }
73 virtual void setName(const string &name, SymbolTable * = 0) {
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.
82 // This section also defines a family of isType, isConstant,
83 // isMethodArgument, etc functions...
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.
89 inline ValueTy getValueType() const { return VTy; }
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; \
98 inline CLASS *cast##NAME() { /* nonconst version */ \
99 return is##NAME() ? (CLASS*)this : 0; \
101 inline const CLASS *cast##NAME##Asserting() const { /*const version */ \
102 assert(is##NAME() && "Expected Value Type: " #NAME); \
103 return (const CLASS*)this; \
105 inline CLASS *cast##NAME##Asserting() { /* nonconst version */ \
106 assert(is##NAME() && "Expected Value Type: " #NAME); \
107 return (CLASS*)this; \
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 )
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;
124 inline const Type *castTypeAsserting() const {
125 assert(isType() && "Expected Value Type: Type");
126 return (const Type*)this;
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.
133 void replaceAllUsesWith(Value *D);
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.
139 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
141 //----------------------------------------------------------------------
142 // Methods for handling the vector of uses of this Value.
144 typedef vector<User*>::iterator use_iterator;
145 typedef vector<User*>::const_iterator use_const_iterator;
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(); }
154 inline void use_push_back(User *I) { Uses.push_back(I); }
155 User *use_remove(use_iterator &I);
157 inline void addUse(User *I) { Uses.push_back(I); }
158 void killUse(User *I);
162 //===----------------------------------------------------------------------===//
164 //===----------------------------------------------------------------------===//
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.
169 template<class ValueSubclass>
174 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
176 if (Val) Val->addUse(U);
179 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
181 inline operator ValueSubclass *() const { return Val; }
183 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
188 inline ValueSubclass *operator=(ValueSubclass *V) {
189 if (Val) Val->killUse(U);
195 inline ValueSubclass *operator->() { return Val; }
196 inline const ValueSubclass *operator->() const { return Val; }
198 inline ValueSubclass *get() { return Val; }
199 inline const ValueSubclass *get() const { return Val; }
201 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
202 if (Val) Val->killUse(U);
209 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
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
216 template <class X> class real_type { typedef X Type; };
217 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
219 //===----------------------------------------------------------------------===//
220 // Type Checking Templates
221 //===----------------------------------------------------------------------===//
223 // isa<X> - Return true if the parameter to the template is an instance of the
224 // template type argument. Used like this:
226 // if (isa<Type>(myVal)) { ... }
228 template <class X, class Y>
229 bool isa(Y Val) { return X::isa(Val); }
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:
236 // cast< Instruction>(myVal)->getParent()
237 // cast<const Instruction>(myVal)->getParent()
239 template <class X, class Y>
241 assert(isa<X>(Val) && "Invalid cast argument type!");
242 return (X*)(real_type<Y>::Type)Val;
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:
251 // if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
254 template <class X, class Y>
256 return isa<X>(Val) ? cast<X>(Val) : 0;