More warnings patrol: Another unused argument and more implicit
[oota-llvm.git] / include / llvm / Support / ValueHandle.h
1 //===- llvm/Support/ValueHandle.h - Value Smart Pointer classes -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ValueHandle class and its sub-classes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_VALUEHANDLE_H
15 #define LLVM_SUPPORT_VALUEHANDLE_H
16
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/Value.h"
20
21 namespace llvm {
22 class ValueHandleBase;
23
24 // ValueHandleBase** is only 4-byte aligned.
25 template<>
26 class PointerLikeTypeTraits<ValueHandleBase**> {
27 public:
28   static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
29   static inline ValueHandleBase **getFromVoidPointer(void *P) {
30     return static_cast<ValueHandleBase**>(P);
31   }
32   enum { NumLowBitsAvailable = 2 };
33 };
34
35 /// ValueHandleBase - This is the common base class of value handles.
36 /// ValueHandle's are smart pointers to Value's that have special behavior when
37 /// the value is deleted or ReplaceAllUsesWith'd.  See the specific handles
38 /// below for details.
39 ///
40 class ValueHandleBase {
41   friend class Value;
42 protected:
43   /// HandleBaseKind - This indicates what sub class the handle actually is.
44   /// This is to avoid having a vtable for the light-weight handle pointers. The
45   /// fully general Callback version does have a vtable.
46   enum HandleBaseKind {
47     Assert,
48     Callback,
49     Tracking,
50     Weak
51   };
52 private:
53
54   PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
55   ValueHandleBase *Next;
56   Value *VP;
57   
58   explicit ValueHandleBase(const ValueHandleBase&); // DO NOT IMPLEMENT.
59 public:
60   explicit ValueHandleBase(HandleBaseKind Kind)
61     : PrevPair(0, Kind), Next(0), VP(0) {}
62   ValueHandleBase(HandleBaseKind Kind, Value *V)
63     : PrevPair(0, Kind), Next(0), VP(V) {
64     if (isValid(VP))
65       AddToUseList();
66   }
67   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
68     : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
69     if (isValid(VP))
70       AddToExistingUseList(RHS.getPrevPtr());
71   }
72   ~ValueHandleBase() {
73     if (isValid(VP))
74       RemoveFromUseList();
75   }
76
77   Value *operator=(Value *RHS) {
78     if (VP == RHS) return RHS;
79     if (isValid(VP)) RemoveFromUseList();
80     VP = RHS;
81     if (isValid(VP)) AddToUseList();
82     return RHS;
83   }
84
85   Value *operator=(const ValueHandleBase &RHS) {
86     if (VP == RHS.VP) return RHS.VP;
87     if (isValid(VP)) RemoveFromUseList();
88     VP = RHS.VP;
89     if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr());
90     return VP;
91   }
92
93   Value *operator->() const { return getValPtr(); }
94   Value &operator*() const { return *getValPtr(); }
95
96 protected:
97   Value *getValPtr() const { return VP; }
98   static bool isValid(Value *V) {
99     return V &&
100            V != DenseMapInfo<Value *>::getEmptyKey() &&
101            V != DenseMapInfo<Value *>::getTombstoneKey();
102   }
103
104 private:
105   // Callbacks made from Value.
106   static void ValueIsDeleted(Value *V);
107   static void ValueIsRAUWd(Value *Old, Value *New);
108
109   // Internal implementation details.
110   ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
111   HandleBaseKind getKind() const { return PrevPair.getInt(); }
112   void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
113
114   /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
115   /// List is the address of either the head of the list or a Next node within
116   /// the existing use list.
117   void AddToExistingUseList(ValueHandleBase **List);
118
119   /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
120   /// Node.
121   void AddToExistingUseListAfter(ValueHandleBase *Node);
122
123   /// AddToUseList - Add this ValueHandle to the use list for VP.
124   void AddToUseList();
125   /// RemoveFromUseList - Remove this ValueHandle from its current use list.
126   void RemoveFromUseList();
127 };
128
129 /// WeakVH - This is a value handle that tries hard to point to a Value, even
130 /// across RAUW operations, but will null itself out if the value is destroyed.
131 /// this is useful for advisory sorts of information, but should not be used as
132 /// the key of a map (since the map would have to rearrange itself when the
133 /// pointer changes).
134 class WeakVH : public ValueHandleBase {
135 public:
136   WeakVH() : ValueHandleBase(Weak) {}
137   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
138   WeakVH(const WeakVH &RHS)
139     : ValueHandleBase(Weak, RHS) {}
140
141   Value *operator=(Value *RHS) {
142     return ValueHandleBase::operator=(RHS);
143   }
144   Value *operator=(const ValueHandleBase &RHS) {
145     return ValueHandleBase::operator=(RHS);
146   }
147
148   operator Value*() const {
149     return getValPtr();
150   }
151 };
152
153 // Specialize simplify_type to allow WeakVH to participate in
154 // dyn_cast, isa, etc.
155 template<typename From> struct simplify_type;
156 template<> struct simplify_type<const WeakVH> {
157   typedef Value* SimpleType;
158   static SimpleType getSimplifiedValue(const WeakVH &WVH) {
159     return static_cast<Value *>(WVH);
160   }
161 };
162 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
163
164 /// AssertingVH - This is a Value Handle that points to a value and asserts out
165 /// if the value is destroyed while the handle is still live.  This is very
166 /// useful for catching dangling pointer bugs and other things which can be
167 /// non-obvious.  One particularly useful place to use this is as the Key of a
168 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
169 /// if another object happens to get allocated to the same address as the old
170 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
171 /// the bad delete occurs.
172 ///
173 /// Note that an AssertingVH handle does *not* follow values across RAUW
174 /// operations.  This means that RAUW's need to explicitly update the
175 /// AssertingVH's as it moves.  This is required because in non-assert mode this
176 /// class turns into a trivial wrapper around a pointer.
177 template <typename ValueTy>
178 class AssertingVH
179 #ifndef NDEBUG
180   : public ValueHandleBase
181 #endif
182   {
183
184 #ifndef NDEBUG
185   ValueTy *getValPtr() const {
186     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
187   }
188   void setValPtr(ValueTy *P) {
189     ValueHandleBase::operator=(GetAsValue(P));
190   }
191 #else
192   ValueTy *ThePtr;
193   ValueTy *getValPtr() const { return ThePtr; }
194   void setValPtr(ValueTy *P) { ThePtr = P; }
195 #endif
196
197   // Convert a ValueTy*, which may be const, to the type the base
198   // class expects.
199   static Value *GetAsValue(Value *V) { return V; }
200   static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
201
202 public:
203 #ifndef NDEBUG
204   AssertingVH() : ValueHandleBase(Assert) {}
205   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
206   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
207 #else
208   AssertingVH() : ThePtr(0) {}
209   AssertingVH(ValueTy *P) : ThePtr(P) {}
210 #endif
211
212   operator ValueTy*() const {
213     return getValPtr();
214   }
215
216   ValueTy *operator=(ValueTy *RHS) {
217     setValPtr(RHS);
218     return getValPtr();
219   }
220   ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
221     setValPtr(RHS.getValPtr());
222     return getValPtr();
223   }
224
225   ValueTy *operator->() const { return getValPtr(); }
226   ValueTy &operator*() const { return *getValPtr(); }
227 };
228
229 // Specialize simplify_type to allow AssertingVH to participate in
230 // dyn_cast, isa, etc.
231 template<typename From> struct simplify_type;
232 template<> struct simplify_type<const AssertingVH<Value> > {
233   typedef Value* SimpleType;
234   static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
235     return static_cast<Value *>(AVH);
236   }
237 };
238 template<> struct simplify_type<AssertingVH<Value> >
239   : public simplify_type<const AssertingVH<Value> > {};
240
241 /// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
242 /// even across RAUW operations.
243 ///
244 /// TrackingVH is designed for situations where a client needs to hold a handle
245 /// to a Value (or subclass) across some operations which may move that value,
246 /// but should never destroy it or replace it with some unacceptable type.
247 ///
248 /// It is an error to do anything with a TrackingVH whose value has been
249 /// destroyed, except to destruct it.
250 ///
251 /// It is an error to attempt to replace a value with one of a type which is
252 /// incompatible with any of its outstanding TrackingVHs.
253 template<typename ValueTy>
254 class TrackingVH : public ValueHandleBase {
255   void CheckValidity() const {
256     Value *VP = ValueHandleBase::getValPtr();
257
258     // Null is always ok.
259     if (!VP)
260         return;
261
262     // Check that this value is valid (i.e., it hasn't been deleted). We
263     // explicitly delay this check until access to avoid requiring clients to be
264     // unnecessarily careful w.r.t. destruction.
265     assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
266
267     // Check that the value is a member of the correct subclass. We would like
268     // to check this property on assignment for better debugging, but we don't
269     // want to require a virtual interface on this VH. Instead we allow RAUW to
270     // replace this value with a value of an invalid type, and check it here.
271     assert(isa<ValueTy>(VP) &&
272            "Tracked Value was replaced by one with an invalid type!");
273   }
274
275   ValueTy *getValPtr() const {
276     CheckValidity();
277     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
278   }
279   void setValPtr(ValueTy *P) {
280     CheckValidity();
281     ValueHandleBase::operator=(GetAsValue(P));
282   }
283
284   // Convert a ValueTy*, which may be const, to the type the base
285   // class expects.
286   static Value *GetAsValue(Value *V) { return V; }
287   static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
288
289 public:
290   TrackingVH() : ValueHandleBase(Tracking) {}
291   TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, P) {}
292   TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
293
294   operator ValueTy*() const {
295     return getValPtr();
296   }
297
298   ValueTy *operator=(ValueTy *RHS) {
299     setValPtr(RHS);
300     return getValPtr();
301   }
302   ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
303     setValPtr(RHS.getValPtr());
304     return getValPtr();
305   }
306
307   ValueTy *operator->() const { return getValPtr(); }
308   ValueTy &operator*() const { return *getValPtr(); }
309 };
310
311 // Specialize simplify_type to allow TrackingVH to participate in
312 // dyn_cast, isa, etc.
313 template<typename From> struct simplify_type;
314 template<> struct simplify_type<const TrackingVH<Value> > {
315   typedef Value* SimpleType;
316   static SimpleType getSimplifiedValue(const TrackingVH<Value> &AVH) {
317     return static_cast<Value *>(AVH);
318   }
319 };
320 template<> struct simplify_type<TrackingVH<Value> >
321   : public simplify_type<const TrackingVH<Value> > {};
322
323 /// CallbackVH - This is a value handle that allows subclasses to define
324 /// callbacks that run when the underlying Value has RAUW called on it or is
325 /// destroyed.  This class can be used as the key of a map, as long as the user
326 /// takes it out of the map before calling setValPtr() (since the map has to
327 /// rearrange itself when the pointer changes).  Unlike ValueHandleBase, this
328 /// class has a vtable and a virtual destructor.
329 class CallbackVH : public ValueHandleBase {
330 protected:
331   CallbackVH(const CallbackVH &RHS)
332     : ValueHandleBase(Callback, RHS) {}
333
334   virtual ~CallbackVH();
335
336   void setValPtr(Value *P) {
337     ValueHandleBase::operator=(P);
338   }
339
340 public:
341   CallbackVH() : ValueHandleBase(Callback) {}
342   CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
343
344   operator Value*() const {
345     return getValPtr();
346   }
347
348   /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
349   /// call any non-virtual Value method on getValPtr(), but no subclass methods.
350   /// If WeakVH were implemented as a CallbackVH, it would use this method to
351   /// call setValPtr(NULL).  AssertingVH would use this method to cause an
352   /// assertion failure.
353   ///
354   /// All implementations must remove the reference from this object to the
355   /// Value that's being destroyed.
356   virtual void deleted() {
357     setValPtr(NULL);
358   }
359
360   /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
361   /// _before_ any of the uses have actually been replaced.  If WeakVH were
362   /// implemented as a CallbackVH, it would use this method to call
363   /// setValPtr(new_value).  AssertingVH would do nothing in this method.
364   virtual void allUsesReplacedWith(Value *) {}
365 };
366
367 // Specialize simplify_type to allow CallbackVH to participate in
368 // dyn_cast, isa, etc.
369 template<typename From> struct simplify_type;
370 template<> struct simplify_type<const CallbackVH> {
371   typedef Value* SimpleType;
372   static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
373     return static_cast<Value *>(CVH);
374   }
375 };
376 template<> struct simplify_type<CallbackVH>
377   : public simplify_type<const CallbackVH> {};
378
379 } // End llvm namespace
380
381 #endif