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 Instruction, Function, Module, Type,
7 // This file also defines the Use<> template for users of value.
9 // This file also defines the isa<X>(), cast<X>(), and dyn_cast<X>() templates.
11 //===----------------------------------------------------------------------===//
17 #include "llvm/Annotation.h"
18 #include "llvm/AbstractTypeUser.h"
23 class FunctionArgument;
28 typedef Function Method;
32 template<class ValueSubclass, class ItemParentType, class SymTabType>
35 //===----------------------------------------------------------------------===//
37 //===----------------------------------------------------------------------===//
39 class Value : public Annotable, // Values are annotable
40 public AbstractTypeUser { // Values use potentially abstract types
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
54 std::vector<User *> Uses;
56 PATypeHandle<Type> Ty;
59 Value(const Value &); // Do not implement
61 inline void setType(const Type *ty) { Ty = ty; }
63 Value(const Type *Ty, ValueTy vty, const std::string &name = "");
66 // Support for debugging
69 // All values can potentially be typed
70 inline const Type *getType() const { return Ty; }
72 // All values can potentially be named...
73 inline bool hasName() const { return Name != ""; }
74 inline const std::string &getName() const { return Name; }
76 virtual void setName(const std::string &name, SymbolTable * = 0) {
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.
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 // 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.
95 void replaceAllUsesWith(Value *D);
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.
101 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
103 //----------------------------------------------------------------------
104 // Methods for handling the vector of uses of this Value.
106 typedef std::vector<User*>::iterator use_iterator;
107 typedef std::vector<User*>::const_iterator use_const_iterator;
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(); }
118 inline void use_push_back(User *I) { Uses.push_back(I); }
119 User *use_remove(use_iterator &I);
121 inline void addUse(User *I) { Uses.push_back(I); }
122 void killUse(User *I);
126 //===----------------------------------------------------------------------===//
128 //===----------------------------------------------------------------------===//
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.
133 template<class ValueSubclass>
138 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
140 if (Val) Val->addUse(U);
143 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
145 inline operator ValueSubclass *() const { return Val; }
147 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
152 inline ValueSubclass *operator=(ValueSubclass *V) {
153 if (Val) Val->killUse(U);
159 inline ValueSubclass *operator->() { return Val; }
160 inline const ValueSubclass *operator->() const { return Val; }
162 inline ValueSubclass *get() { return Val; }
163 inline const ValueSubclass *get() const { return Val; }
165 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
166 if (Val) Val->killUse(U);
173 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
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
180 template <class X> class real_type { typedef X Type; };
181 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
183 //===----------------------------------------------------------------------===//
184 // Type Checking Templates
185 //===----------------------------------------------------------------------===//
187 // isa<X> - Return true if the parameter to the template is an instance of the
188 // template type argument. Used like this:
190 // if (isa<Type>(myVal)) { ... }
192 template <class X, class Y>
193 inline bool isa(Y Val) {
194 assert(Val && "isa<Ty>(NULL) invoked!");
195 return X::classof(Val);
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.
204 // cast< Instruction>(myVal)->getParent()
205 // cast<const Instruction>(myVal)->getParent()
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;
213 // cast_or_null<X> - Functionally identical to cast, except that a null value is
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;
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:
229 // if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
232 template <class X, class Y>
233 inline X *dyn_cast(Y Val) {
234 return isa<X>(Val) ? cast<X>(Val) : 0;
237 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
238 // value is accepted.
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;
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...
249 template <> inline bool isa<Type, const Value*>(const Value *Val) {
250 return Val->getValueType() == Value::TypeVal;
252 template <> inline bool isa<Type, Value*>(Value *Val) {
253 return Val->getValueType() == Value::TypeVal;
255 template <> inline bool isa<Constant, const Value*>(const Value *Val) {
256 return Val->getValueType() == Value::ConstantVal;
258 template <> inline bool isa<Constant, Value*>(Value *Val) {
259 return Val->getValueType() == Value::ConstantVal;
261 template <> inline bool isa<FunctionArgument, const Value*>(const Value *Val) {
262 return Val->getValueType() == Value::FunctionArgumentVal;
264 template <> inline bool isa<FunctionArgument, Value*>(Value *Val) {
265 return Val->getValueType() == Value::FunctionArgumentVal;
267 template <> inline bool isa<Instruction, const Value*>(const Value *Val) {
268 return Val->getValueType() == Value::InstructionVal;
270 template <> inline bool isa<Instruction, Value*>(Value *Val) {
271 return Val->getValueType() == Value::InstructionVal;
273 template <> inline bool isa<BasicBlock, const Value*>(const Value *Val) {
274 return Val->getValueType() == Value::BasicBlockVal;
276 template <> inline bool isa<BasicBlock, Value*>(Value *Val) {
277 return Val->getValueType() == Value::BasicBlockVal;
279 template <> inline bool isa<Function, const Value*>(const Value *Val) {
280 return Val->getValueType() == Value::FunctionVal;
282 template <> inline bool isa<Function, Value*>(Value *Val) {
283 return Val->getValueType() == Value::FunctionVal;
285 template <> inline bool isa<GlobalVariable, const Value*>(const Value *Val) {
286 return Val->getValueType() == Value::GlobalVariableVal;
288 template <> inline bool isa<GlobalVariable, Value*>(Value *Val) {
289 return Val->getValueType() == Value::GlobalVariableVal;
291 template <> inline bool isa<GlobalValue, const Value*>(const Value *Val) {
292 return isa<GlobalVariable>(Val) || isa<Function>(Val);
294 template <> inline bool isa<GlobalValue, Value*>(Value *Val) {
295 return isa<GlobalVariable>(Val) || isa<Function>(Val);
297 template <> inline bool isa<Module, const Value*>(const Value *Val) {
298 return Val->getValueType() == Value::ModuleVal;
300 template <> inline bool isa<Module, Value*>(Value *Val) {
301 return Val->getValueType() == Value::ModuleVal;