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"
30 template<class ValueSubclass, class ItemParentType, class SymTabType>
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
37 class Value : public Annotable, // Values are annotable
38 public AbstractTypeUser { // Values use potentially abstract types
41 TypeVal, // This is an instance of Type
42 ConstantVal, // This is an instance of ConstPoolVal
43 MethodArgumentVal, // This is an instance of MethodArgument
44 InstructionVal, // This is an instance of Instruction
45 BasicBlockVal, // This is an instance of BasicBlock
46 MethodVal, // This is an instance of Method
47 GlobalVariableVal, // This is an instance of GlobalVariable
48 ModuleVal, // This is an instance of Module
54 PATypeHandle<Type> Ty;
57 Value(const Value &); // Do not implement
59 inline void setType(const Type *ty) { Ty = ty; }
61 Value(const Type *Ty, ValueTy vty, const string &name = "");
64 // Support for debugging
67 // All values can potentially be typed
68 inline const Type *getType() const { return Ty; }
70 // All values can potentially be named...
71 inline bool hasName() const { return Name != ""; }
72 inline const string &getName() const { return Name; }
74 virtual void setName(const string &name, SymbolTable * = 0) {
78 // Methods for determining the subtype of this Value. The getValueType()
79 // method returns the type of the value directly. The cast*() methods are
80 // equivalent to using dynamic_cast<>... if the cast is successful, this is
81 // returned, otherwise you get a null pointer.
83 // The family of functions Val->cast<type>Asserting() is used in the same
84 // way as the Val->cast<type>() instructions, but they assert the expected
85 // type instead of checking it at runtime.
87 inline ValueTy getValueType() const { return VTy; }
89 // replaceAllUsesWith - Go through the uses list for this definition and make
90 // each use point to "D" instead of "this". After this completes, 'this's
91 // use list should be empty.
93 void replaceAllUsesWith(Value *D);
95 // refineAbstractType - This function is implemented because we use
96 // potentially abstract types, and these types may be resolved to more
97 // concrete types after we are constructed.
99 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
101 //----------------------------------------------------------------------
102 // Methods for handling the vector of uses of this Value.
104 typedef vector<User*>::iterator use_iterator;
105 typedef vector<User*>::const_iterator use_const_iterator;
107 inline unsigned use_size() const { return Uses.size(); }
108 inline bool use_empty() const { return Uses.empty(); }
109 inline use_iterator use_begin() { return Uses.begin(); }
110 inline use_const_iterator use_begin() const { return Uses.begin(); }
111 inline use_iterator use_end() { return Uses.end(); }
112 inline use_const_iterator use_end() const { return Uses.end(); }
113 inline User *use_back() { return Uses.back(); }
114 inline const User *use_back() const { return Uses.back(); }
116 inline void use_push_back(User *I) { Uses.push_back(I); }
117 User *use_remove(use_iterator &I);
119 inline void addUse(User *I) { Uses.push_back(I); }
120 void killUse(User *I);
124 //===----------------------------------------------------------------------===//
126 //===----------------------------------------------------------------------===//
128 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
129 // list of a definition node up-to-date really easy.
131 template<class ValueSubclass>
136 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
138 if (Val) Val->addUse(U);
141 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
143 inline operator ValueSubclass *() const { return Val; }
145 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
150 inline ValueSubclass *operator=(ValueSubclass *V) {
151 if (Val) Val->killUse(U);
157 inline ValueSubclass *operator->() { return Val; }
158 inline const ValueSubclass *operator->() const { return Val; }
160 inline ValueSubclass *get() { return Val; }
161 inline const ValueSubclass *get() const { return Val; }
163 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
164 if (Val) Val->killUse(U);
171 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
173 // real_type - Provide a macro to get the real type of a value that might be
174 // a use. This provides a typedef 'Type' that is the argument type for all
175 // non UseTy types, and is the contained pointer type of the use if it is a
178 template <class X> class real_type { typedef X Type; };
179 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
181 //===----------------------------------------------------------------------===//
182 // Type Checking Templates
183 //===----------------------------------------------------------------------===//
185 // isa<X> - Return true if the parameter to the template is an instance of the
186 // template type argument. Used like this:
188 // if (isa<Type>(myVal)) { ... }
190 template <class X, class Y>
191 inline bool isa(Y Val) { return X::classof(Val); }
194 // cast<X> - Return the argument parameter cast to the specified type. This
195 // casting operator asserts that the type is correct, so it does not return null
196 // on failure. But it will correctly return NULL when the input is NULL.
199 // cast< Instruction>(myVal)->getParent()
200 // cast<const Instruction>(myVal)->getParent()
202 template <class X, class Y>
203 inline X *cast(Y Val) {
204 assert((Val == 0 || isa<X>(Val)) && "Invalid cast argument type!");
205 return (X*)(real_type<Y>::Type)Val;
209 // dyn_cast<X> - Return the argument parameter cast to the specified type. This
210 // casting operator returns null if the argument is of the wrong type, so it can
211 // be used to test for a type as well as cast if successful. This should be
212 // used in the context of an if statement like this:
214 // if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
217 template <class X, class Y>
218 inline X *dyn_cast(Y Val) {
219 return isa<X>(Val) ? cast<X>(Val) : 0;
223 // isa - Provide some specializations of isa so that we have to include the
224 // subtype header files to test to see if the value is a subclass...
226 template <> inline bool isa<Type, const Value*>(const Value *Val) {
227 return Val->getValueType() == Value::TypeVal;
229 template <> inline bool isa<Type, Value*>(Value *Val) {
230 return Val->getValueType() == Value::TypeVal;
232 template <> inline bool isa<ConstPoolVal, const Value*>(const Value *Val) {
233 return Val->getValueType() == Value::ConstantVal;
235 template <> inline bool isa<ConstPoolVal, Value*>(Value *Val) {
236 return Val->getValueType() == Value::ConstantVal;
238 template <> inline bool isa<MethodArgument, const Value*>(const Value *Val) {
239 return Val->getValueType() == Value::MethodArgumentVal;
241 template <> inline bool isa<MethodArgument, Value*>(Value *Val) {
242 return Val->getValueType() == Value::MethodArgumentVal;
244 template <> inline bool isa<Instruction, const Value*>(const Value *Val) {
245 return Val->getValueType() == Value::InstructionVal;
247 template <> inline bool isa<Instruction, Value*>(Value *Val) {
248 return Val->getValueType() == Value::InstructionVal;
250 template <> inline bool isa<BasicBlock, const Value*>(const Value *Val) {
251 return Val->getValueType() == Value::BasicBlockVal;
253 template <> inline bool isa<BasicBlock, Value*>(Value *Val) {
254 return Val->getValueType() == Value::BasicBlockVal;
256 template <> inline bool isa<Method, const Value*>(const Value *Val) {
257 return Val->getValueType() == Value::MethodVal;
259 template <> inline bool isa<Method, Value*>(Value *Val) {
260 return Val->getValueType() == Value::MethodVal;
262 template <> inline bool isa<GlobalVariable, const Value*>(const Value *Val) {
263 return Val->getValueType() == Value::GlobalVariableVal;
265 template <> inline bool isa<GlobalVariable, Value*>(Value *Val) {
266 return Val->getValueType() == Value::GlobalVariableVal;
268 template <> inline bool isa<GlobalValue, const Value*>(const Value *Val) {
269 return isa<GlobalVariable>(Val) || isa<Method>(Val);
271 template <> inline bool isa<GlobalValue, Value*>(Value *Val) {
272 return isa<GlobalVariable>(Val) || isa<Method>(Val);
274 template <> inline bool isa<Module, const Value*>(const Value *Val) {
275 return Val->getValueType() == Value::ModuleVal;
277 template <> inline bool isa<Module, Value*>(Value *Val) {
278 return Val->getValueType() == Value::ModuleVal;