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; \
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)
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.
114 void replaceAllUsesWith(Value *D);
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.
120 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
122 //----------------------------------------------------------------------
123 // Methods for handling the vector of uses of this Value.
125 typedef vector<User*>::iterator use_iterator;
126 typedef vector<User*>::const_iterator use_const_iterator;
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(); }
135 inline void use_push_back(User *I) { Uses.push_back(I); }
136 User *use_remove(use_iterator &I);
138 inline void addUse(User *I) { Uses.push_back(I); }
139 void killUse(User *I);
143 //===----------------------------------------------------------------------===//
145 //===----------------------------------------------------------------------===//
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.
150 template<class ValueSubclass>
155 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
157 if (Val) Val->addUse(U);
160 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
162 inline operator ValueSubclass *() const { return Val; }
164 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
169 inline ValueSubclass *operator=(ValueSubclass *V) {
170 if (Val) Val->killUse(U);
176 inline ValueSubclass *operator->() { return Val; }
177 inline const ValueSubclass *operator->() const { return Val; }
179 inline ValueSubclass *get() { return Val; }
180 inline const ValueSubclass *get() const { return Val; }
182 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
183 if (Val) Val->killUse(U);
190 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
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
197 template <class X> class real_type { typedef X Type; };
198 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
200 //===----------------------------------------------------------------------===//
201 // Type Checking Templates
202 //===----------------------------------------------------------------------===//
204 // isa<X> - Return true if the parameter to the template is an instance of the
205 // template type argument. Used like this:
207 // if (isa<Type>(myVal)) { ... }
209 template <class X, class Y>
210 bool isa(Y Val) { return X::isa(Val); }
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:
217 // cast< Instruction>(myVal)->getParent()
218 // cast<const Instruction>(myVal)->getParent()
220 template <class X, class Y>
222 assert(isa<X>(Val) && "Invalid cast argument type!");
223 return (X*)(real_type<Y>::Type)Val;
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:
232 // if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
235 template <class X, class Y>
237 return isa<X>(Val) ? cast<X>(Val) : 0;
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...
244 template <> bool isa<Type, Value*>(Value *Val) {
245 return Val->getValueType() == Value::TypeVal;
247 template <> bool isa<ConstPoolVal, Value*>(Value *Val) {
248 return Val->getValueType() == Value::ConstantVal;
250 template <> bool isa<MethodArgument, Value*>(Value *Val) {
251 return Val->getValueType() == Value::MethodArgumentVal;
253 template <> bool isa<Instruction, Value*>(Value *Val) {
254 return Val->getValueType() == Value::InstructionVal;
256 template <> bool isa<BasicBlock, Value*>(Value *Val) {
257 return Val->getValueType() == Value::BasicBlockVal;
259 template <> bool isa<Method, Value*>(Value *Val) {
260 return Val->getValueType() == Value::MethodVal;
262 template <> bool isa<GlobalVariable, Value*>(Value *Val) {
263 return Val->getValueType() == Value::GlobalVal;
265 template <> bool isa<Module, Value*>(Value *Val) {
266 return Val->getValueType() == Value::ModuleVal;