1 //===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PointerUnion class, which is a discriminated union of
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_POINTERUNION_H
16 #define LLVM_ADT_POINTERUNION_H
18 #include "llvm/ADT/PointerIntPair.h"
22 /// getPointerUnionTypeNum - If the argument has type PT1* or PT2* return
23 /// false or true respectively.
24 template <typename PT1, typename PT2>
25 static inline int getPointerUnionTypeNum(PT1 *P) { return 0; }
26 template <typename PT1, typename PT2>
27 static inline int getPointerUnionTypeNum(PT2 *P) { return 1; }
28 template <typename PT1, typename PT2>
29 static inline int getPointerUnionTypeNum(...) { return -1; }
32 /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
33 /// for the two template arguments.
34 template <typename PT1, typename PT2>
35 class PointerUnionUIntTraits {
37 static inline void *getAsVoidPointer(void *P) { return P; }
38 static inline void *getFromVoidPointer(void *P) { return P; }
40 PT1BitsAv = PointerLikeTypeTraits<PT1>::NumLowBitsAvailable,
41 PT2BitsAv = PointerLikeTypeTraits<PT2>::NumLowBitsAvailable,
42 NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv
46 /// PointerUnion - This implements a discriminated union of two pointer types,
47 /// and keeps the discriminator bit-mangled into the low bits of the pointer.
48 /// This allows the implementation to be extremely efficient in space, but
49 /// permits a very natural and type-safe API.
51 /// Common use patterns would be something like this:
52 /// PointerUnion<int*, float*> P;
54 /// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0"
55 /// X = P.get<int*>(); // ok.
56 /// Y = P.get<float*>(); // runtime assertion failure.
57 /// Z = P.get<double*>(); // runtime assertion failure (regardless of tag)
59 /// Y = P.get<float*>(); // ok.
60 /// X = P.get<int*>(); // runtime assertion failure.
61 template <typename PT1, typename PT2>
64 typedef PointerIntPair<void*, 1, bool,
65 PointerUnionUIntTraits<PT1,PT2> > ValTy;
73 const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V)));
78 const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)));
82 /// isNull - Return true if the pointer held in the union is null,
83 /// regardless of which type it is.
84 bool isNull() const { return Val.getPointer() == 0; }
85 operator bool() const { return !isNull(); }
87 /// is<T>() return true if the Union currently holds the type matching T.
90 int TyNo = ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0);
91 assert(TyNo != -1 && "Type query could never succeed on PointerUnion!");
92 return static_cast<int>(Val.getInt()) == TyNo;
95 /// get<T>() - Return the value of the specified pointer type. If the
96 /// specified pointer type is incorrect, assert.
99 assert(is<T>() && "Invalid accessor called");
100 return PointerLikeTypeTraits<T>::getFromVoidPointer(Val.getPointer());
103 /// dyn_cast<T>() - If the current value is of the specified pointer type,
104 /// return it, otherwise return null.
107 if (is<T>()) return get<T>();
111 /// \brief If the union is set to the first pointer type we can get an
112 /// address pointing to it.
113 template <typename T>
114 PT1 const *getAddrOf() const {
115 assert(is<PT1>() && "Val is not the first pointer");
116 assert(get<PT1>() == Val.getPointer() &&
117 "Can't get the address because PointerLikeTypeTraits changes the ptr");
118 T const *can_only_get_address_of_first_pointer_type
119 = reinterpret_cast<PT1 const *>(Val.getAddrOfPointer());
120 return can_only_get_address_of_first_pointer_type;
123 /// Assignment operators - Allow assigning into this union from either
124 /// pointer type, setting the discriminator to remember what it came from.
125 const PointerUnion &operator=(const PT1 &RHS) {
127 const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS)));
131 const PointerUnion &operator=(const PT2 &RHS) {
133 const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS)));
138 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
139 static inline PointerUnion getFromOpaqueValue(void *VP) {
141 V.Val = ValTy::getFromOpaqueValue(VP);
146 // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
147 // # low bits available = min(PT1bits,PT2bits)-1.
148 template<typename PT1, typename PT2>
149 class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
152 getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
153 return P.getOpaqueValue();
155 static inline PointerUnion<PT1, PT2>
156 getFromVoidPointer(void *P) {
157 return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
160 // The number of bits available are the min of the two pointer types.
162 NumLowBitsAvailable =
163 PointerLikeTypeTraits<typename PointerUnion<PT1,PT2>::ValTy>
164 ::NumLowBitsAvailable
169 /// PointerUnion3 - This is a pointer union of three pointer types. See
170 /// documentation for PointerUnion for usage.
171 template <typename PT1, typename PT2, typename PT3>
172 class PointerUnion3 {
174 typedef PointerUnion<PT1, PT2> InnerUnion;
175 typedef PointerUnion<InnerUnion, PT3> ValTy;
181 PointerUnion3(PT1 V) {
184 PointerUnion3(PT2 V) {
187 PointerUnion3(PT3 V) {
191 /// isNull - Return true if the pointer held in the union is null,
192 /// regardless of which type it is.
193 bool isNull() const { return Val.isNull(); }
194 operator bool() const { return !isNull(); }
196 /// is<T>() return true if the Union currently holds the type matching T.
200 if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
201 return Val.template is<InnerUnion>() &&
202 Val.template get<InnerUnion>().template is<T>();
203 return Val.template is<T>();
206 /// get<T>() - Return the value of the specified pointer type. If the
207 /// specified pointer type is incorrect, assert.
210 assert(is<T>() && "Invalid accessor called");
212 if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
213 return Val.template get<InnerUnion>().template get<T>();
215 return Val.template get<T>();
218 /// dyn_cast<T>() - If the current value is of the specified pointer type,
219 /// return it, otherwise return null.
222 if (is<T>()) return get<T>();
226 /// Assignment operators - Allow assigning into this union from either
227 /// pointer type, setting the discriminator to remember what it came from.
228 const PointerUnion3 &operator=(const PT1 &RHS) {
229 Val = InnerUnion(RHS);
232 const PointerUnion3 &operator=(const PT2 &RHS) {
233 Val = InnerUnion(RHS);
236 const PointerUnion3 &operator=(const PT3 &RHS) {
241 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
242 static inline PointerUnion3 getFromOpaqueValue(void *VP) {
244 V.Val = ValTy::getFromOpaqueValue(VP);
249 // Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has
250 // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
251 template<typename PT1, typename PT2, typename PT3>
252 class PointerLikeTypeTraits<PointerUnion3<PT1, PT2, PT3> > {
255 getAsVoidPointer(const PointerUnion3<PT1, PT2, PT3> &P) {
256 return P.getOpaqueValue();
258 static inline PointerUnion3<PT1, PT2, PT3>
259 getFromVoidPointer(void *P) {
260 return PointerUnion3<PT1, PT2, PT3>::getFromOpaqueValue(P);
263 // The number of bits available are the min of the two pointer types.
265 NumLowBitsAvailable =
266 PointerLikeTypeTraits<typename PointerUnion3<PT1, PT2, PT3>::ValTy>
267 ::NumLowBitsAvailable
271 /// PointerUnion4 - This is a pointer union of four pointer types. See
272 /// documentation for PointerUnion for usage.
273 template <typename PT1, typename PT2, typename PT3, typename PT4>
274 class PointerUnion4 {
276 typedef PointerUnion<PT1, PT2> InnerUnion1;
277 typedef PointerUnion<PT3, PT4> InnerUnion2;
278 typedef PointerUnion<InnerUnion1, InnerUnion2> ValTy;
284 PointerUnion4(PT1 V) {
285 Val = InnerUnion1(V);
287 PointerUnion4(PT2 V) {
288 Val = InnerUnion1(V);
290 PointerUnion4(PT3 V) {
291 Val = InnerUnion2(V);
293 PointerUnion4(PT4 V) {
294 Val = InnerUnion2(V);
297 /// isNull - Return true if the pointer held in the union is null,
298 /// regardless of which type it is.
299 bool isNull() const { return Val.isNull(); }
300 operator bool() const { return !isNull(); }
302 /// is<T>() return true if the Union currently holds the type matching T.
306 if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
307 return Val.template is<InnerUnion1>() &&
308 Val.template get<InnerUnion1>().template is<T>();
309 return Val.template is<InnerUnion2>() &&
310 Val.template get<InnerUnion2>().template is<T>();
313 /// get<T>() - Return the value of the specified pointer type. If the
314 /// specified pointer type is incorrect, assert.
317 assert(is<T>() && "Invalid accessor called");
319 if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
320 return Val.template get<InnerUnion1>().template get<T>();
322 return Val.template get<InnerUnion2>().template get<T>();
325 /// dyn_cast<T>() - If the current value is of the specified pointer type,
326 /// return it, otherwise return null.
329 if (is<T>()) return get<T>();
333 /// Assignment operators - Allow assigning into this union from either
334 /// pointer type, setting the discriminator to remember what it came from.
335 const PointerUnion4 &operator=(const PT1 &RHS) {
336 Val = InnerUnion1(RHS);
339 const PointerUnion4 &operator=(const PT2 &RHS) {
340 Val = InnerUnion1(RHS);
343 const PointerUnion4 &operator=(const PT3 &RHS) {
344 Val = InnerUnion2(RHS);
347 const PointerUnion4 &operator=(const PT4 &RHS) {
348 Val = InnerUnion2(RHS);
352 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
353 static inline PointerUnion4 getFromOpaqueValue(void *VP) {
355 V.Val = ValTy::getFromOpaqueValue(VP);
360 // Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has
361 // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
362 template<typename PT1, typename PT2, typename PT3, typename PT4>
363 class PointerLikeTypeTraits<PointerUnion4<PT1, PT2, PT3, PT4> > {
366 getAsVoidPointer(const PointerUnion4<PT1, PT2, PT3, PT4> &P) {
367 return P.getOpaqueValue();
369 static inline PointerUnion4<PT1, PT2, PT3, PT4>
370 getFromVoidPointer(void *P) {
371 return PointerUnion4<PT1, PT2, PT3, PT4>::getFromOpaqueValue(P);
374 // The number of bits available are the min of the two pointer types.
376 NumLowBitsAvailable =
377 PointerLikeTypeTraits<typename PointerUnion4<PT1, PT2, PT3, PT4>::ValTy>
378 ::NumLowBitsAvailable