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 //===----------------------------------------------------------------------===//
12 #include "llvm/Annotation.h"
13 #include "llvm/AbstractTypeUser.h"
25 template<class ValueSubclass, class ItemParentType, class SymTabType>
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 class Value : public Annotable, // Values are annotable
33 public AbstractTypeUser { // Values use potentially abstract types
36 TypeVal, // This is an instance of Type
37 ConstantVal, // This is an instance of ConstPoolVal
38 MethodArgumentVal, // This is an instance of MethodArgument
39 InstructionVal, // This is an instance of Instruction
40 BasicBlockVal, // This is an instance of BasicBlock
41 MethodVal, // This is an instance of Method
42 GlobalVal, // This is an instance of GlobalVariable
43 ModuleVal, // This is an instance of Module
49 PATypeHandle<Type> Ty;
52 Value(const Value &); // Do not implement
54 inline void setType(const Type *ty) { Ty = ty; }
56 Value(const Type *Ty, ValueTy vty, const string &name = "");
59 // Support for debugging
62 // All values can potentially be typed
63 inline const Type *getType() const { return Ty; }
65 // All values can potentially be named...
66 inline bool hasName() const { return Name != ""; }
67 inline const string &getName() const { return Name; }
69 virtual void setName(const string &name, SymbolTable * = 0) {
73 // Methods for determining the subtype of this Value. The getValueType()
74 // method returns the type of the value directly. The cast*() methods are
75 // equivalent to using dynamic_cast<>... if the cast is successful, this is
76 // returned, otherwise you get a null pointer, allowing expressions like:
78 // if (Instruction *I = Val->castInstruction()) { ... }
80 // This section also defines a family of isType, isConstant,
81 // isMethodArgument, etc functions...
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 // Use a macro to define the functions, otherwise these definitions are just
90 // really long and ugly.
91 #define CAST_FN(NAME, CLASS) \
92 inline bool is##NAME() const { return VTy == NAME##Val; } \
93 inline const CLASS *cast##NAME() const { /*const version */ \
94 return is##NAME() ? (const CLASS*)this : 0; \
96 inline CLASS *cast##NAME() { /* nonconst version */ \
97 return is##NAME() ? (CLASS*)this : 0; \
99 inline const CLASS *cast##NAME##Asserting() const { /*const version */ \
100 assert(is##NAME() && "Expected Value Type: " #NAME); \
101 return (const CLASS*)this; \
103 inline CLASS *cast##NAME##Asserting() { /* nonconst version */ \
104 assert(is##NAME() && "Expected Value Type: " #NAME); \
105 return (CLASS*)this; \
108 CAST_FN(Constant , ConstPoolVal )
109 CAST_FN(MethodArgument, MethodArgument)
110 CAST_FN(Instruction , Instruction )
111 CAST_FN(BasicBlock , BasicBlock )
112 CAST_FN(Method , Method )
113 CAST_FN(Global , GlobalVariable)
114 CAST_FN(Module , Module )
117 // Type value is special, because there is no nonconst version of functions!
118 inline bool isType() const { return VTy == TypeVal; }
119 inline const Type *castType() const {
120 return (VTy == TypeVal) ? (const Type*)this : 0;
122 inline const Type *castTypeAsserting() const {
123 assert(isType() && "Expected Value Type: Type");
124 return (const Type*)this;
127 // replaceAllUsesWith - Go through the uses list for this definition and make
128 // each use point to "D" instead of "this". After this completes, 'this's
129 // use list should be empty.
131 void replaceAllUsesWith(Value *D);
133 // refineAbstractType - This function is implemented because we use
134 // potentially abstract types, and these types may be resolved to more
135 // concrete types after we are constructed.
137 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
139 //----------------------------------------------------------------------
140 // Methods for handling the vector of uses of this Value.
142 typedef vector<User*>::iterator use_iterator;
143 typedef vector<User*>::const_iterator use_const_iterator;
145 inline unsigned use_size() const { return Uses.size(); }
146 inline bool use_empty() const { return Uses.empty(); }
147 inline use_iterator use_begin() { return Uses.begin(); }
148 inline use_const_iterator use_begin() const { return Uses.begin(); }
149 inline use_iterator use_end() { return Uses.end(); }
150 inline use_const_iterator use_end() const { return Uses.end(); }
152 inline void use_push_back(User *I) { Uses.push_back(I); }
153 User *use_remove(use_iterator &I);
155 inline void addUse(User *I) { Uses.push_back(I); }
156 void killUse(User *I);
159 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
160 // list of a definition node up-to-date really easy.
162 template<class ValueSubclass>
167 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
169 if (Val) Val->addUse(U);
172 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
174 inline operator ValueSubclass *() const { return Val; }
176 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
181 inline ValueSubclass *operator=(ValueSubclass *V) {
182 if (Val) Val->killUse(U);
188 inline ValueSubclass *operator->() { return Val; }
189 inline const ValueSubclass *operator->() const { return Val; }
191 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
192 if (Val) Val->killUse(U);
199 typedef UseTy<Value> Use;