Add an explicit keyword.
[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/PointerIntPair.h"
18 #include "llvm/Value.h"
19
20 namespace llvm {
21 class ValueHandleBase;
22
23 // ValueHandleBase** is only 4-byte aligned.
24 template<>
25 class PointerLikeTypeTraits<ValueHandleBase**> {
26 public:
27   static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
28   static inline ValueHandleBase **getFromVoidPointer(void *P) {
29     return static_cast<ValueHandleBase**>(P);
30   }
31   enum { NumLowBitsAvailable = 2 };
32 };
33
34 /// ValueHandleBase - This is the common base class of value handles.
35 /// ValueHandle's are smart pointers to Value's that have special behavior when
36 /// the value is deleted or ReplaceAllUsesWith'd.  See the specific handles
37 /// below for details.
38 ///
39 class ValueHandleBase {
40   friend class Value;
41 protected:
42   /// HandleBaseKind - This indicates what sub class the handle actually is.
43   /// This is to avoid having a vtable for the light-weight handle pointers. The
44   /// fully general Callback version does have a vtable.
45   enum HandleBaseKind {
46     Assert,
47     Weak,
48     Callback
49   };
50 private:
51   
52   PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
53   ValueHandleBase *Next;
54   Value *VP;
55 public:
56   explicit ValueHandleBase(HandleBaseKind Kind)
57     : PrevPair(0, Kind), Next(0), VP(0) {}
58   ValueHandleBase(HandleBaseKind Kind, Value *V)
59     : PrevPair(0, Kind), Next(0), VP(V) {
60     if (V)
61       AddToUseList();
62   }
63   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
64     : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
65     if (VP)
66       AddToExistingUseList(RHS.getPrevPtr());
67   }
68   ~ValueHandleBase() {
69     if (VP)
70       RemoveFromUseList();   
71   }
72   
73   Value *operator=(Value *RHS) {
74     if (VP == RHS) return RHS;
75     if (VP) RemoveFromUseList();
76     VP = RHS;
77     if (VP) AddToUseList();
78     return RHS;
79   }
80
81   Value *operator=(const ValueHandleBase &RHS) {
82     if (VP == RHS.VP) return RHS.VP;
83     if (VP) RemoveFromUseList();
84     VP = RHS.VP;
85     if (VP) AddToExistingUseList(RHS.getPrevPtr());
86     return VP;
87   }
88   
89   Value *operator->() const { return getValPtr(); }
90   Value &operator*() const { return *getValPtr(); }
91
92 protected:
93   Value *getValPtr() const { return VP; }
94 private:
95   // Callbacks made from Value.
96   static void ValueIsDeleted(Value *V);
97   static void ValueIsRAUWd(Value *Old, Value *New);
98   
99   // Internal implementation details.
100   ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
101   HandleBaseKind getKind() const { return PrevPair.getInt(); }
102   void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
103   
104   /// AddToUseList - Add this ValueHandle to the use list for VP, where List is
105   /// known to point into the existing use list.
106   void AddToExistingUseList(ValueHandleBase **List);
107   
108   /// AddToUseList - Add this ValueHandle to the use list for VP.
109   void AddToUseList();
110   /// RemoveFromUseList - Remove this ValueHandle from its current use list.
111   void RemoveFromUseList();
112 };
113   
114 /// WeakVH - This is a value handle that tries hard to point to a Value, even
115 /// across RAUW operations, but will null itself out if the value is destroyed.
116 /// this is useful for advisory sorts of information, but should not be used as
117 /// the key of a map (since the map would have to rearrange itself when the
118 /// pointer changes).
119 class WeakVH : public ValueHandleBase {
120 public:
121   WeakVH() : ValueHandleBase(Weak) {}
122   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
123   WeakVH(const WeakVH &RHS)
124     : ValueHandleBase(Weak, RHS) {}
125
126   operator Value*() const {
127     return getValPtr();
128   }
129 };  
130   
131 /// AssertingVH - This is a Value Handle that points to a value and asserts out
132 /// if the value is destroyed while the handle is still live.  This is very
133 /// useful for catching dangling pointer bugs and other things which can be
134 /// non-obvious.  One particularly useful place to use this is as the Key of a
135 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
136 /// if another object happens to get allocated to the same address as the old
137 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
138 /// the bad delete occurs.
139 ///
140 /// Note that an AssertingVH handle does *not* follow values across RAUW
141 /// operations.  This means that RAUW's need to explicitly update the
142 /// AssertingVH's as it moves.  This is required because in non-assert mode this
143 /// class turns into a trivial wrapper around a pointer.
144 template <typename ValueTy>
145 class AssertingVH 
146 #ifndef NDEBUG
147   : public ValueHandleBase
148 #endif
149   {
150
151 #ifndef NDEBUG
152   ValueTy *getValPtr() const {
153     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
154   }
155   void setValPtr(ValueTy *P) {
156     ValueHandleBase::operator=(P);
157   }
158 #else
159   ValueTy *ThePtr;
160   ValueTy *getValPtr() const { return ThePtr; }
161   void setValPtr(ValueTy *P) { ThePtr = P; }
162 #endif
163
164 public:
165 #ifndef NDEBUG
166   AssertingVH() : ValueHandleBase(Assert) {}
167   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
168   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
169 #else
170   AssertingVH() : ThePtr(0) {}
171   AssertingVH(ValueTy *P) : ThePtr(P) {}
172 #endif
173
174   operator ValueTy*() const {
175     return getValPtr();
176   }
177
178   ValueTy *operator=(ValueTy *RHS) {
179     setValPtr(RHS);
180     return getValPtr();
181   }
182   ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
183     setValPtr(RHS.getValPtr());
184     return getValPtr();
185   }
186
187   ValueTy *operator->() const { return getValPtr(); }
188   ValueTy &operator*() const { return *getValPtr(); }
189 };
190
191 /// CallbackVH - This is a value handle that allows subclasses to define
192 /// callbacks that run when the underlying Value has RAUW called on it or is
193 /// destroyed.  This class can be used as the key of a map, as long as the user
194 /// takes it out of the map before calling setValPtr() (since the map has to
195 /// rearrange itself when the pointer changes).  Unlike ValueHandleBase, this
196 /// class has a vtable and a virtual destructor.
197 class CallbackVH : public ValueHandleBase {
198 protected:
199   CallbackVH(const CallbackVH &RHS)
200     : ValueHandleBase(Callback, RHS) {}
201
202   virtual ~CallbackVH();
203
204   void setValPtr(Value *P) {
205     ValueHandleBase::operator=(P);
206   }
207
208 public:
209   CallbackVH() : ValueHandleBase(Callback) {}
210   CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
211
212   operator Value*() const {
213     return getValPtr();
214   }
215
216   /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
217   /// call any non-virtual Value method on getValPtr(), but no subclass methods.
218   /// If WeakVH were implemented as a CallbackVH, it would use this method to
219   /// call setValPtr(NULL).  AssertingVH would use this method to cause an
220   /// assertion failure.
221   ///
222   /// All implementations must remove the reference from this object to the
223   /// Value that's being destroyed.
224   virtual void deleted() {
225     setValPtr(NULL);
226   }
227
228   /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
229   /// _before_ any of the uses have actually been replaced.  If WeakVH were
230   /// implemented as a CallbackVH, it would use this method to call
231   /// setValPtr(new_value).  AssertingVH would do nothing in this method.
232   virtual void allUsesReplacedWith(Value *new_value) {}
233 };
234
235 } // End llvm namespace
236
237 #endif