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"
22 template<class ValueSubclass, class ItemParentType, class SymTabType>
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 class Value : public Annotable { // Value's are annotable
32 TypeVal, // This is an instance of Type
33 ConstantVal, // This is an instance of ConstPoolVal
34 MethodArgumentVal, // This is an instance of MethodArgument
35 InstructionVal, // This is an instance of Instruction
37 BasicBlockVal, // This is an instance of BasicBlock
38 MethodVal, // This is an instance of Method
39 ModuleVal, // This is an instance of Module
48 Value(const Value &); // Do not implement
50 inline void setType(const Type *ty) { Ty = ty; }
52 Value(const Type *Ty, ValueTy vty, const string &name = "");
55 inline const Type *getType() const { return Ty; }
57 // All values can potentially be named...
58 inline bool hasName() const { return Name != ""; }
59 inline const string &getName() const { return Name; }
60 virtual void setName(const string &name) { Name = name; }
62 // Methods for determining the subtype of this Value. The getValueType()
63 // method returns the type of the value directly. The cast*() methods are
64 // equilivent to using dynamic_cast<>... if the cast is successful, this is
65 // returned, otherwise you get a null pointer, allowing expressions like this:
67 // if (Instruction *I = Val->castInstruction()) { ... }
69 // This section also defines a family of isType, isConstant, isMethodArgument,
72 // The family of functions Val->cast<type>Asserting() is used in the same
73 // way as the Val->cast<type>() instructions, but they assert the expected
74 // type instead of checking it at runtime.
76 inline ValueTy getValueType() const { return VTy; }
78 // Use a macro to define the functions, otherwise these definitions are just
79 // really long and ugly.
80 #define CAST_FN(NAME, CLASS) \
81 inline bool is##NAME() const { return VTy == NAME##Val; } \
82 inline const CLASS *cast##NAME() const { /*const version */ \
83 return is##NAME() ? (const CLASS*)this : 0; \
85 inline CLASS *cast##NAME() { /* nonconst version */ \
86 return is##NAME() ? (CLASS*)this : 0; \
88 inline const CLASS *cast##NAME##Asserting() const { /*const version */ \
89 assert(is##NAME() && "Expected Value Type: " #NAME); \
90 return (const CLASS*)this; \
92 inline CLASS *cast##NAME##Asserting() { /* nonconst version */ \
93 assert(is##NAME() && "Expected Value Type: " #NAME); \
94 return (CLASS*)this; \
97 CAST_FN(Constant , ConstPoolVal )
98 CAST_FN(MethodArgument, MethodArgument)
99 CAST_FN(Instruction , Instruction )
100 CAST_FN(BasicBlock , BasicBlock )
101 CAST_FN(Method , Method )
102 CAST_FN(Module , Module )
105 // Type value is special, because there is no nonconst version of functions!
106 inline bool isType() const { return VTy == TypeVal; }
107 inline const Type *castType() const {
108 return (VTy == TypeVal) ? (const Type*)this : 0;
110 inline const Type *castTypeAsserting() const {
111 assert(isType() && "Expected Value Type: Type");
112 return (const Type*)this;
115 // replaceAllUsesWith - Go through the uses list for this definition and make
116 // each use point to "D" instead of "this". After this completes, 'this's
117 // use list should be empty.
119 void replaceAllUsesWith(Value *D);
121 //----------------------------------------------------------------------
122 // Methods for handling the list of uses of this DEF.
124 typedef list<User*>::iterator use_iterator;
125 typedef list<User*>::const_iterator use_const_iterator;
127 inline unsigned use_size() const { return Uses.size(); }
128 inline bool use_empty() const { return Uses.empty(); }
129 inline use_iterator use_begin() { return Uses.begin(); }
130 inline use_const_iterator use_begin() const { return Uses.begin(); }
131 inline use_iterator use_end() { return Uses.end(); }
132 inline use_const_iterator use_end() const { return Uses.end(); }
134 inline void use_push_back(User *I) { Uses.push_back(I); }
135 User *use_remove(use_iterator &I);
137 inline void addUse(User *I) { Uses.push_back(I); }
138 void killUse(User *I);
141 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
142 // list of a definition node up-to-date really easy.
144 template<class ValueSubclass>
149 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
151 if (Val) Val->addUse(U);
154 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
156 inline operator ValueSubclass *() const { return Val; }
158 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
163 inline ValueSubclass *operator=(ValueSubclass *V) {
164 if (Val) Val->killUse(U);
170 inline ValueSubclass *operator->() { return Val; }
171 inline const ValueSubclass *operator->() const { return Val; }
173 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
174 if (Val) Val->killUse(U);
181 typedef UseTy<Value> Use;