* Move casting stuff out to Support/Casting.h
[oota-llvm.git] / include / llvm / Value.h
1 //===-- llvm/Value.h - Definition of the Value class -------------*- C++ -*--=//
2 //
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,
5 // etc...
6 //
7 // This file also defines the Use<> template for users of value.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_VALUE_H
12 #define LLVM_VALUE_H
13
14 #include <vector>
15 #include "llvm/Annotation.h"
16 #include "llvm/AbstractTypeUser.h"
17 #include "Support/Casting.h"
18
19 class User;
20 class Type;
21 class Constant;
22 class FunctionArgument;
23 class Instruction;
24 class BasicBlock;
25 class GlobalValue;
26 class Function;
27 class GlobalVariable;
28 class Module;
29 class SymbolTable;
30 template<class ValueSubclass, class ItemParentType, class SymTabType> 
31   class ValueHolder;
32
33 //===----------------------------------------------------------------------===//
34 //                                 Value Class
35 //===----------------------------------------------------------------------===//
36
37 class Value : public Annotable,         // Values are annotable
38               public AbstractTypeUser { // Values use potentially abstract types
39 public:
40   enum ValueTy {
41     TypeVal,                // This is an instance of Type
42     ConstantVal,            // This is an instance of Constant
43     FunctionArgumentVal,    // This is an instance of FunctionArgument
44     InstructionVal,         // This is an instance of Instruction
45     BasicBlockVal,          // This is an instance of BasicBlock
46     FunctionVal,            // This is an instance of Function
47     GlobalVariableVal,      // This is an instance of GlobalVariable
48     ModuleVal,              // This is an instance of Module
49   };
50
51 private:
52   std::vector<User *> Uses;
53   std::string Name;
54   PATypeHandle<Type> Ty;
55   ValueTy VTy;
56
57   Value(const Value &);              // Do not implement
58 protected:
59   inline void setType(const Type *ty) { Ty = ty; }
60 public:
61   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
62   virtual ~Value();
63   
64   // Support for debugging 
65   void dump() const;
66
67   // Implement operator<< on Value...
68   virtual void print(std::ostream &O) const = 0;
69   
70   // All values can potentially be typed
71   inline const Type *getType() const { return Ty; }
72   
73   // All values can potentially be named...
74   inline bool               hasName() const { return Name != ""; }
75   inline const std::string &getName() const { return Name; }
76
77   virtual void setName(const std::string &name, SymbolTable * = 0) {
78     Name = name;
79   }
80   
81   // Methods for determining the subtype of this Value.  The getValueType()
82   // method returns the type of the value directly.  The cast*() methods are
83   // equivalent to using dynamic_cast<>... if the cast is successful, this is
84   // returned, otherwise you get a null pointer.
85   //
86   // The family of functions Val->cast<type>Asserting() is used in the same
87   // way as the Val->cast<type>() instructions, but they assert the expected
88   // type instead of checking it at runtime.
89   //
90   inline ValueTy getValueType() const { return VTy; }
91   
92   // replaceAllUsesWith - Go through the uses list for this definition and make
93   // each use point to "D" instead of "this".  After this completes, 'this's 
94   // use list should be empty.
95   //
96   void replaceAllUsesWith(Value *D);
97
98   // refineAbstractType - This function is implemented because we use
99   // potentially abstract types, and these types may be resolved to more
100   // concrete types after we are constructed.
101   //
102   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
103   
104   //----------------------------------------------------------------------
105   // Methods for handling the vector of uses of this Value.
106   //
107   typedef std::vector<User*>::iterator       use_iterator;
108   typedef std::vector<User*>::const_iterator use_const_iterator;
109
110   inline unsigned           use_size()  const { return Uses.size();  }
111   inline bool               use_empty() const { return Uses.empty(); }
112   inline use_iterator       use_begin()       { return Uses.begin(); }
113   inline use_const_iterator use_begin() const { return Uses.begin(); }
114   inline use_iterator       use_end()         { return Uses.end();   }
115   inline use_const_iterator use_end()   const { return Uses.end();   }
116   inline User              *use_back()        { return Uses.back();  }
117   inline const User        *use_back()  const { return Uses.back();  }
118
119   inline void use_push_back(User *I)   { Uses.push_back(I); }
120   User *use_remove(use_iterator &I);
121
122   inline void addUse(User *I)      { Uses.push_back(I); }
123   void killUse(User *I);
124 };
125
126 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
127   if (V == 0)
128     OS << "<null> value!\n";
129   else
130     V->print(OS);
131   return OS;
132 }
133
134
135 //===----------------------------------------------------------------------===//
136 //                                 UseTy Class
137 //===----------------------------------------------------------------------===//
138
139 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
140 // list of a definition node up-to-date really easy.
141 //
142 template<class ValueSubclass>
143 class UseTy {
144   ValueSubclass *Val;
145   User *U;
146 public:
147   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
148     Val = v; U = user;
149     if (Val) Val->addUse(U);
150   }
151
152   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
153
154   inline operator ValueSubclass *() const { return Val; }
155
156   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
157     Val = 0;
158     U = user.U;
159     operator=(user.Val);
160   }
161   inline ValueSubclass *operator=(ValueSubclass *V) { 
162     if (Val) Val->killUse(U);
163     Val = V;
164     if (V) V->addUse(U);
165     return V;
166   }
167
168   inline       ValueSubclass *operator->()       { return Val; }
169   inline const ValueSubclass *operator->() const { return Val; }
170
171   inline       ValueSubclass *get()       { return Val; }
172   inline const ValueSubclass *get() const { return Val; }
173
174   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
175     if (Val) Val->killUse(U);
176     Val = user.Val;
177     Val->addUse(U);
178     return *this;
179   }
180 };
181
182 typedef UseTy<Value> Use;    // Provide Use as a common UseTy type
183
184 // Provide a specialization of real_type to work with use's... to make them a
185 // bit more transparent.
186 //
187 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
188
189
190 // isa - Provide some specializations of isa so that we don't have to include
191 // the subtype header files to test to see if the value is a subclass...
192 //
193 template <> inline bool isa<Type, const Value*>(const Value *Val) { 
194   return Val->getValueType() == Value::TypeVal;
195 }
196 template <> inline bool isa<Type, Value*>(Value *Val) { 
197   return Val->getValueType() == Value::TypeVal;
198 }
199 template <> inline bool isa<Constant, const Value*>(const Value *Val) { 
200   return Val->getValueType() == Value::ConstantVal; 
201 }
202 template <> inline bool isa<Constant, Value*>(Value *Val) { 
203   return Val->getValueType() == Value::ConstantVal; 
204 }
205 template <> inline bool isa<FunctionArgument, const Value*>(const Value *Val) { 
206   return Val->getValueType() == Value::FunctionArgumentVal;
207 }
208 template <> inline bool isa<FunctionArgument, Value*>(Value *Val) { 
209   return Val->getValueType() == Value::FunctionArgumentVal;
210 }
211 template <> inline bool isa<Instruction, const Value*>(const Value *Val) { 
212   return Val->getValueType() == Value::InstructionVal;
213 }
214 template <> inline bool isa<Instruction, Value*>(Value *Val) { 
215   return Val->getValueType() == Value::InstructionVal;
216 }
217 template <> inline bool isa<BasicBlock, const Value*>(const Value *Val) { 
218   return Val->getValueType() == Value::BasicBlockVal;
219 }
220 template <> inline bool isa<BasicBlock, Value*>(Value *Val) { 
221   return Val->getValueType() == Value::BasicBlockVal;
222 }
223 template <> inline bool isa<Function, const Value*>(const Value *Val) { 
224   return Val->getValueType() == Value::FunctionVal;
225 }
226 template <> inline bool isa<Function, Value*>(Value *Val) { 
227   return Val->getValueType() == Value::FunctionVal;
228 }
229 template <> inline bool isa<GlobalVariable, const Value*>(const Value *Val) { 
230   return Val->getValueType() == Value::GlobalVariableVal;
231 }
232 template <> inline bool isa<GlobalVariable, Value*>(Value *Val) { 
233   return Val->getValueType() == Value::GlobalVariableVal;
234 }
235 template <> inline bool isa<GlobalValue, const Value*>(const Value *Val) { 
236   return isa<GlobalVariable>(Val) || isa<Function>(Val);
237 }
238 template <> inline bool isa<GlobalValue, Value*>(Value *Val) { 
239   return isa<GlobalVariable>(Val) || isa<Function>(Val);
240 }
241 template <> inline bool isa<Module, const Value*>(const Value *Val) { 
242   return Val->getValueType() == Value::ModuleVal;
243 }
244 template <> inline bool isa<Module, Value*>(Value *Val) { 
245   return Val->getValueType() == Value::ModuleVal;
246 }
247
248 #endif