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/Support/Compiler.h"
19 #include "llvm/ADT/PointerIntPair.h"
24 struct PointerUnionTypeSelectorReturn {
28 /// \brief Get a type based on whether two types are the same or not. For:
30 /// typedef typename PointerUnionTypeSelector<T1, T2, EQ, NE>::Return Ret;
32 /// Ret will be EQ type if T1 is same as T2 or NE type otherwise.
33 template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
34 struct PointerUnionTypeSelector {
35 typedef typename PointerUnionTypeSelectorReturn<RET_NE>::Return Return;
38 template <typename T, typename RET_EQ, typename RET_NE>
39 struct PointerUnionTypeSelector<T, T, RET_EQ, RET_NE> {
40 typedef typename PointerUnionTypeSelectorReturn<RET_EQ>::Return Return;
43 template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
44 struct PointerUnionTypeSelectorReturn<
45 PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE> > {
46 typedef typename PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>::Return
50 /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
51 /// for the two template arguments.
52 template <typename PT1, typename PT2>
53 class PointerUnionUIntTraits {
55 static inline void *getAsVoidPointer(void *P) { return P; }
56 static inline void *getFromVoidPointer(void *P) { return P; }
58 PT1BitsAv = (int)(PointerLikeTypeTraits<PT1>::NumLowBitsAvailable),
59 PT2BitsAv = (int)(PointerLikeTypeTraits<PT2>::NumLowBitsAvailable),
60 NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv
64 /// PointerUnion - This implements a discriminated union of two pointer types,
65 /// and keeps the discriminator bit-mangled into the low bits of the pointer.
66 /// This allows the implementation to be extremely efficient in space, but
67 /// permits a very natural and type-safe API.
69 /// Common use patterns would be something like this:
70 /// PointerUnion<int*, float*> P;
72 /// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0"
73 /// X = P.get<int*>(); // ok.
74 /// Y = P.get<float*>(); // runtime assertion failure.
75 /// Z = P.get<double*>(); // compile time failure.
77 /// Y = P.get<float*>(); // ok.
78 /// X = P.get<int*>(); // runtime assertion failure.
79 template <typename PT1, typename PT2>
82 typedef PointerIntPair<void*, 1, bool,
83 PointerUnionUIntTraits<PT1,PT2> > ValTy;
88 static const int Num = 0;
91 static const int Num = 1;
94 struct UNION_DOESNT_CONTAIN_TYPE { };
99 PointerUnion(PT1 V) : Val(
100 const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V))) {
102 PointerUnion(PT2 V) : Val(
103 const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)), 1) {
106 /// isNull - Return true if the pointer held in the union is null,
107 /// regardless of which type it is.
108 bool isNull() const {
109 // Convert from the void* to one of the pointer types, to make sure that
110 // we recursively strip off low bits if we have a nested PointerUnion.
111 return !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());
113 LLVM_EXPLICIT operator bool() const { return !isNull(); }
115 /// is<T>() return true if the Union currently holds the type matching T.
119 ::llvm::PointerUnionTypeSelector<PT1, T, IsPT1,
120 ::llvm::PointerUnionTypeSelector<PT2, T, IsPT2,
121 UNION_DOESNT_CONTAIN_TYPE<T> > >::Return Ty;
123 return static_cast<int>(Val.getInt()) == TyNo;
126 /// get<T>() - Return the value of the specified pointer type. If the
127 /// specified pointer type is incorrect, assert.
130 assert(is<T>() && "Invalid accessor called");
131 return PointerLikeTypeTraits<T>::getFromVoidPointer(Val.getPointer());
134 /// dyn_cast<T>() - If the current value is of the specified pointer type,
135 /// return it, otherwise return null.
138 if (is<T>()) return get<T>();
142 /// \brief If the union is set to the first pointer type get an address
144 PT1 const *getAddrOfPtr1() const {
145 return const_cast<PointerUnion *>(this)->getAddrOfPtr1();
148 /// \brief If the union is set to the first pointer type get an address
150 PT1 *getAddrOfPtr1() {
151 assert(is<PT1>() && "Val is not the first pointer");
152 assert(get<PT1>() == Val.getPointer() &&
153 "Can't get the address because PointerLikeTypeTraits changes the ptr");
154 return (PT1 *)Val.getAddrOfPointer();
157 /// Assignment operators - Allow assigning into this union from either
158 /// pointer type, setting the discriminator to remember what it came from.
159 const PointerUnion &operator=(const PT1 &RHS) {
161 const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS)));
164 const PointerUnion &operator=(const PT2 &RHS) {
165 Val.setPointerAndInt(
166 const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS)),
171 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
172 static inline PointerUnion getFromOpaqueValue(void *VP) {
174 V.Val = ValTy::getFromOpaqueValue(VP);
179 template<typename PT1, typename PT2>
180 static bool operator==(PointerUnion<PT1, PT2> lhs,
181 PointerUnion<PT1, PT2> rhs) {
182 return lhs.getOpaqueValue() == rhs.getOpaqueValue();
185 template<typename PT1, typename PT2>
186 static bool operator!=(PointerUnion<PT1, PT2> lhs,
187 PointerUnion<PT1, PT2> rhs) {
188 return lhs.getOpaqueValue() != rhs.getOpaqueValue();
191 // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
192 // # low bits available = min(PT1bits,PT2bits)-1.
193 template<typename PT1, typename PT2>
194 class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
197 getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
198 return P.getOpaqueValue();
200 static inline PointerUnion<PT1, PT2>
201 getFromVoidPointer(void *P) {
202 return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
205 // The number of bits available are the min of the two pointer types.
207 NumLowBitsAvailable =
208 PointerLikeTypeTraits<typename PointerUnion<PT1,PT2>::ValTy>
209 ::NumLowBitsAvailable
214 /// PointerUnion3 - This is a pointer union of three pointer types. See
215 /// documentation for PointerUnion for usage.
216 template <typename PT1, typename PT2, typename PT3>
217 class PointerUnion3 {
219 typedef PointerUnion<PT1, PT2> InnerUnion;
220 typedef PointerUnion<InnerUnion, PT3> ValTy;
224 struct IsInnerUnion {
226 IsInnerUnion(ValTy val) : Val(val) { }
229 return Val.template is<InnerUnion>() &&
230 Val.template get<InnerUnion>().template is<T>();
234 return Val.template get<InnerUnion>().template get<T>();
240 IsPT3(ValTy val) : Val(val) { }
243 return Val.template is<T>();
247 return Val.template get<T>();
254 PointerUnion3(PT1 V) {
257 PointerUnion3(PT2 V) {
260 PointerUnion3(PT3 V) {
264 /// isNull - Return true if the pointer held in the union is null,
265 /// regardless of which type it is.
266 bool isNull() const { return Val.isNull(); }
267 LLVM_EXPLICIT operator bool() const { return !isNull(); }
269 /// is<T>() return true if the Union currently holds the type matching T.
272 // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
274 ::llvm::PointerUnionTypeSelector<PT1, T, IsInnerUnion,
275 ::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3 >
277 return Ty(Val).template is<T>();
280 /// get<T>() - Return the value of the specified pointer type. If the
281 /// specified pointer type is incorrect, assert.
284 assert(is<T>() && "Invalid accessor called");
285 // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
287 ::llvm::PointerUnionTypeSelector<PT1, T, IsInnerUnion,
288 ::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3 >
290 return Ty(Val).template get<T>();
293 /// dyn_cast<T>() - If the current value is of the specified pointer type,
294 /// return it, otherwise return null.
297 if (is<T>()) return get<T>();
301 /// Assignment operators - Allow assigning into this union from either
302 /// pointer type, setting the discriminator to remember what it came from.
303 const PointerUnion3 &operator=(const PT1 &RHS) {
304 Val = InnerUnion(RHS);
307 const PointerUnion3 &operator=(const PT2 &RHS) {
308 Val = InnerUnion(RHS);
311 const PointerUnion3 &operator=(const PT3 &RHS) {
316 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
317 static inline PointerUnion3 getFromOpaqueValue(void *VP) {
319 V.Val = ValTy::getFromOpaqueValue(VP);
324 // Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has
325 // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
326 template<typename PT1, typename PT2, typename PT3>
327 class PointerLikeTypeTraits<PointerUnion3<PT1, PT2, PT3> > {
330 getAsVoidPointer(const PointerUnion3<PT1, PT2, PT3> &P) {
331 return P.getOpaqueValue();
333 static inline PointerUnion3<PT1, PT2, PT3>
334 getFromVoidPointer(void *P) {
335 return PointerUnion3<PT1, PT2, PT3>::getFromOpaqueValue(P);
338 // The number of bits available are the min of the two pointer types.
340 NumLowBitsAvailable =
341 PointerLikeTypeTraits<typename PointerUnion3<PT1, PT2, PT3>::ValTy>
342 ::NumLowBitsAvailable
346 /// PointerUnion4 - This is a pointer union of four pointer types. See
347 /// documentation for PointerUnion for usage.
348 template <typename PT1, typename PT2, typename PT3, typename PT4>
349 class PointerUnion4 {
351 typedef PointerUnion<PT1, PT2> InnerUnion1;
352 typedef PointerUnion<PT3, PT4> InnerUnion2;
353 typedef PointerUnion<InnerUnion1, InnerUnion2> ValTy;
359 PointerUnion4(PT1 V) {
360 Val = InnerUnion1(V);
362 PointerUnion4(PT2 V) {
363 Val = InnerUnion1(V);
365 PointerUnion4(PT3 V) {
366 Val = InnerUnion2(V);
368 PointerUnion4(PT4 V) {
369 Val = InnerUnion2(V);
372 /// isNull - Return true if the pointer held in the union is null,
373 /// regardless of which type it is.
374 bool isNull() const { return Val.isNull(); }
375 LLVM_EXPLICIT operator bool() const { return !isNull(); }
377 /// is<T>() return true if the Union currently holds the type matching T.
380 // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
382 ::llvm::PointerUnionTypeSelector<PT1, T, InnerUnion1,
383 ::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1, InnerUnion2 >
385 return Val.template is<Ty>() &&
386 Val.template get<Ty>().template is<T>();
389 /// get<T>() - Return the value of the specified pointer type. If the
390 /// specified pointer type is incorrect, assert.
393 assert(is<T>() && "Invalid accessor called");
394 // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
396 ::llvm::PointerUnionTypeSelector<PT1, T, InnerUnion1,
397 ::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1, InnerUnion2 >
399 return Val.template get<Ty>().template get<T>();
402 /// dyn_cast<T>() - If the current value is of the specified pointer type,
403 /// return it, otherwise return null.
406 if (is<T>()) return get<T>();
410 /// Assignment operators - Allow assigning into this union from either
411 /// pointer type, setting the discriminator to remember what it came from.
412 const PointerUnion4 &operator=(const PT1 &RHS) {
413 Val = InnerUnion1(RHS);
416 const PointerUnion4 &operator=(const PT2 &RHS) {
417 Val = InnerUnion1(RHS);
420 const PointerUnion4 &operator=(const PT3 &RHS) {
421 Val = InnerUnion2(RHS);
424 const PointerUnion4 &operator=(const PT4 &RHS) {
425 Val = InnerUnion2(RHS);
429 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
430 static inline PointerUnion4 getFromOpaqueValue(void *VP) {
432 V.Val = ValTy::getFromOpaqueValue(VP);
437 // Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has
438 // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
439 template<typename PT1, typename PT2, typename PT3, typename PT4>
440 class PointerLikeTypeTraits<PointerUnion4<PT1, PT2, PT3, PT4> > {
443 getAsVoidPointer(const PointerUnion4<PT1, PT2, PT3, PT4> &P) {
444 return P.getOpaqueValue();
446 static inline PointerUnion4<PT1, PT2, PT3, PT4>
447 getFromVoidPointer(void *P) {
448 return PointerUnion4<PT1, PT2, PT3, PT4>::getFromOpaqueValue(P);
451 // The number of bits available are the min of the two pointer types.
453 NumLowBitsAvailable =
454 PointerLikeTypeTraits<typename PointerUnion4<PT1, PT2, PT3, PT4>::ValTy>
455 ::NumLowBitsAvailable