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;
72 Val.setPointer(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V));
76 Val.setPointer(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V));
80 /// isNull - Return true if the pointer help in the union is null,
81 /// regardless of which type it is.
82 bool isNull() const { return Val.getPointer() == 0; }
83 operator bool() const { return !isNull(); }
85 /// is<T>() return true if the Union currently holds the type matching T.
88 int TyNo = ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0);
89 assert(TyNo != -1 && "Type query could never succeed on PointerUnion!");
90 return Val.getInt() == TyNo;
93 /// get<T>() - Return the value of the specified pointer type. If the
94 /// specified pointer type is incorrect, assert.
97 assert(is<T>() && "Invalid accessor called");
98 return PointerLikeTypeTraits<T>::getFromVoidPointer(Val.getPointer());
101 /// dyn_cast<T>() - If the current value is of the specified pointer type,
102 /// return it, otherwise return null.
105 if (is<T>()) return get<T>();
109 /// Assignment operators - Allow assigning into this union from either
110 /// pointer type, setting the discriminator to remember what it came from.
111 const PointerUnion &operator=(const PT1 &RHS) {
112 Val.setPointer(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS));
116 const PointerUnion &operator=(const PT2 &RHS) {
117 Val.setPointer(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS));
122 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
123 static PointerUnion getFromOpaqueValue(void *VP) {
125 V.Val = ValTy::getFromOpaqueValue(VP);
130 // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
131 // # low bits available = min(PT1bits,PT2bits)-1.
132 template<typename PT1, typename PT2>
133 class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
136 getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
137 return P.getOpaqueValue();
139 static inline PointerUnion<PT1, PT2>
140 getFromVoidPointer(void *P) {
141 return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
144 // The number of bits available are the min of the two pointer types.
146 NumLowBitsAvailable =
147 PointerLikeTypeTraits<typename PointerUnion<PT1,PT2>::ValTy>
148 ::NumLowBitsAvailable
153 /// PointerUnion3 - This is a pointer union of three pointer types. See
154 /// documentation for PointerUnion for usage.
155 template <typename PT1, typename PT2, typename PT3>
156 class PointerUnion3 {
158 typedef PointerUnion<PT1, PT2> InnerUnion;
159 typedef PointerUnion<InnerUnion, PT3> ValTy;
165 PointerUnion3(PT1 V) {
168 PointerUnion3(PT2 V) {
171 PointerUnion3(PT3 V) {
175 /// isNull - Return true if the pointer help in the union is null,
176 /// regardless of which type it is.
177 bool isNull() const { return Val.isNull(); }
178 operator bool() const { return !isNull(); }
180 /// is<T>() return true if the Union currently holds the type matching T.
184 if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
185 return Val.is<InnerUnion>() && Val.get<InnerUnion>().is<T>();
186 // Must be PT3 or statically invalid.
191 /// get<T>() - Return the value of the specified pointer type. If the
192 /// specified pointer type is incorrect, assert.
195 assert(is<T>() && "Invalid accessor called");
197 if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
198 return Val.get<InnerUnion>().get<T>();
203 /// dyn_cast<T>() - If the current value is of the specified pointer type,
204 /// return it, otherwise return null.
207 if (is<T>()) return get<T>();
211 /// Assignment operators - Allow assigning into this union from either
212 /// pointer type, setting the discriminator to remember what it came from.
213 const PointerUnion3 &operator=(const PT1 &RHS) {
214 Val = InnerUnion(RHS);
217 const PointerUnion3 &operator=(const PT2 &RHS) {
218 Val = InnerUnion(RHS);
221 const PointerUnion3 &operator=(const PT3 &RHS) {
226 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
227 static PointerUnion3 getFromOpaqueValue(void *VP) {
229 V.Val = ValTy::getFromOpaqueValue(VP);
234 // Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has
235 // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
236 template<typename PT1, typename PT2, typename PT3>
237 class PointerLikeTypeTraits<PointerUnion3<PT1, PT2, PT3> > {
240 getAsVoidPointer(const PointerUnion3<PT1, PT2, PT3> &P) {
241 return P.getOpaqueValue();
243 static inline PointerUnion3<PT1, PT2, PT3>
244 getFromVoidPointer(void *P) {
245 return PointerUnion3<PT1, PT2, PT3>::getFromOpaqueValue(P);
248 // The number of bits available are the min of the two pointer types.
250 NumLowBitsAvailable =
251 PointerLikeTypeTraits<typename PointerUnion3<PT1, PT2, PT3>::ValTy>
252 ::NumLowBitsAvailable