- Remove Value::use_push_back & Value::use_remove
[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, Type, etc...
5 //
6 // This file also defines the Use<> template for users of value.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_VALUE_H
11 #define LLVM_VALUE_H
12
13 #include "llvm/Annotation.h"
14 #include "llvm/AbstractTypeUser.h"
15 #include "Support/Casting.h"
16 #include <iostream>
17 #include <vector>
18
19 class User;
20 class Type;
21 class Constant;
22 class Argument;
23 class Instruction;
24 class BasicBlock;
25 class GlobalValue;
26 class Function;
27 class GlobalVariable;
28 class SymbolTable;
29
30 //===----------------------------------------------------------------------===//
31 //                                 Value Class
32 //===----------------------------------------------------------------------===//
33
34 /// Value - The base class of all values computed by a program that may be used
35 /// as operands to other values.
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     ArgumentVal,            // This is an instance of Argument
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   };
49
50 private:
51   std::vector<User *> Uses;
52   std::string Name;
53   PATypeHandle<Type> Ty;
54   ValueTy VTy;
55
56   void operator=(const Value &);     // Do not implement
57   Value(const Value &);              // Do not implement
58 public:
59   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
60   virtual ~Value();
61   
62   /// dump - Support for debugging, callable in GDB: V->dump()
63   //
64   void dump() const;
65
66   /// print - Implement operator<< on Value...
67   ///
68   virtual void print(std::ostream &O) const = 0;
69   
70   /// All values are typed, get the type of this value.
71   ///
72   inline const Type *getType() const { return Ty; }
73   
74   // All values can potentially be named...
75   inline bool               hasName() const { return Name != ""; }
76   inline const std::string &getName() const { return Name; }
77
78   virtual void setName(const std::string &name, SymbolTable * = 0) {
79     Name = name;
80   }
81   
82   /// getValueType - Return the immediate subclass of this Value.
83   ///
84   inline ValueTy getValueType() const { return VTy; }
85   
86   /// replaceAllUsesWith - Go through the uses list for this definition and make
87   /// each use point to "V" instead of "this".  After this completes, 'this's 
88   /// use list is guaranteed to be empty.
89   ///
90   void replaceAllUsesWith(Value *V);
91
92   /// refineAbstractType - This function is implemented because we use
93   /// potentially abstract types, and these types may be resolved to more
94   /// concrete types after we are constructed.
95   ///
96   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
97   
98   //----------------------------------------------------------------------
99   // Methods for handling the vector of uses of this Value.
100   //
101   typedef std::vector<User*>::iterator       use_iterator;
102   typedef std::vector<User*>::const_iterator use_const_iterator;
103
104   inline unsigned           use_size()  const { return Uses.size();  }
105   inline bool               use_empty() const { return Uses.empty(); }
106   inline use_iterator       use_begin()       { return Uses.begin(); }
107   inline use_const_iterator use_begin() const { return Uses.begin(); }
108   inline use_iterator       use_end()         { return Uses.end();   }
109   inline use_const_iterator use_end()   const { return Uses.end();   }
110   inline User              *use_back()        { return Uses.back();  }
111   inline const User        *use_back()  const { return Uses.back();  }
112
113   /// addUse/killUse - These two methods should only be used by the Use class
114   /// below.
115   inline void addUse(User *I)      { Uses.push_back(I); }
116   void killUse(User *I);
117 };
118
119 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
120   if (V == 0)
121     OS << "<null> value!\n";
122   else
123     V->print(OS);
124   return OS;
125 }
126
127 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
128   V.print(OS);
129   return OS;
130 }
131
132
133 //===----------------------------------------------------------------------===//
134 //                                 UseTy Class
135 //===----------------------------------------------------------------------===//
136
137 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
138 // list of a definition node up-to-date really easy.
139 //
140 template<class ValueSubclass>
141 class UseTy {
142   ValueSubclass *Val;
143   User *U;
144 public:
145   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
146     Val = v; U = user;
147     if (Val) Val->addUse(U);
148   }
149
150   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
151
152   inline operator ValueSubclass *() const { return Val; }
153
154   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
155     Val = 0;
156     U = user.U;
157     operator=(user.Val);
158   }
159   inline ValueSubclass *operator=(ValueSubclass *V) { 
160     if (Val) Val->killUse(U);
161     Val = V;
162     if (V) V->addUse(U);
163     return V;
164   }
165
166   inline       ValueSubclass *operator->()       { return Val; }
167   inline const ValueSubclass *operator->() const { return Val; }
168
169   inline       ValueSubclass *get()       { return Val; }
170   inline const ValueSubclass *get() const { return Val; }
171
172   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
173     if (Val) Val->killUse(U);
174     Val = user.Val;
175     Val->addUse(U);
176     return *this;
177   }
178 };
179
180 typedef UseTy<Value> Use;    // Provide Use as a common UseTy type
181
182 template<typename From> struct simplify_type<UseTy<From> > {
183   typedef typename simplify_type<From*>::SimpleType SimpleType;
184   
185   static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
186     return (SimpleType)Val.get();
187   }
188 };
189 template<typename From> struct simplify_type<const UseTy<From> > {
190   typedef typename simplify_type<From*>::SimpleType SimpleType;
191   
192   static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
193     return (SimpleType)Val.get();
194   }
195 };
196
197 // isa - Provide some specializations of isa so that we don't have to include
198 // the subtype header files to test to see if the value is a subclass...
199 //
200 template <> inline bool isa_impl<Type, Value>(const Value &Val) { 
201   return Val.getValueType() == Value::TypeVal;
202 }
203 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
204   return Val.getValueType() == Value::ConstantVal; 
205 }
206 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
207   return Val.getValueType() == Value::ArgumentVal;
208 }
209 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
210   return Val.getValueType() == Value::InstructionVal;
211 }
212 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
213   return Val.getValueType() == Value::BasicBlockVal;
214 }
215 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
216   return Val.getValueType() == Value::FunctionVal;
217 }
218 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
219   return Val.getValueType() == Value::GlobalVariableVal;
220 }
221 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
222   return isa<GlobalVariable>(Val) || isa<Function>(Val);
223 }
224
225 #endif