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 bool getPointerUnionTypeNum(PT1 *P) { return false; }
26 template <typename PT1, typename PT2>
27 static inline bool getPointerUnionTypeNum(PT2 *P) { return true; }
28 // Enable, if we could use static_assert.
29 //template <typename PT1, typename PT2>
30 //static inline bool getPointerUnionTypeNum(...) { abort() }
33 /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
34 /// for the two template arguments.
35 template <typename PT1, typename PT2>
36 class PointerUnionUIntTraits {
38 static inline void *getAsVoidPointer(void *P) { return P; }
39 static inline void *getFromVoidPointer(void *P) { return P; }
41 PT1BitsAv = PointerLikeTypeTraits<PT1>::NumLowBitsAvailable,
42 PT2BitsAv = PointerLikeTypeTraits<PT2>::NumLowBitsAvailable,
43 NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv
47 /// PointerUnion - This implements a discriminated union of two pointer types,
48 /// and keeps the discriminator bit-mangled into the low bits of the pointer.
49 /// This allows the implementation to be extremely efficient in space, but
50 /// permits a very natural and type-safe API.
52 /// Common use patterns would be something like this:
53 /// PointerUnion<int*, float*> P;
55 /// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0"
56 /// X = P.get<int*>(); // ok.
57 /// Y = P.get<float*>(); // runtime assertion failure.
58 /// Z = P.get<double*>(); // does not compile.
60 /// Y = P.get<float*>(); // ok.
61 /// X = P.get<int*>(); // runtime assertion failure.
62 template <typename PT1, typename PT2>
65 typedef PointerIntPair<void*, 1, bool,
66 PointerUnionUIntTraits<PT1,PT2> > ValTy;
81 /// isNull - Return true if the pointer help in the union is null,
82 /// regardless of which type it is.
83 bool isNull() const { return Val.getPointer() == 0; }
85 /// is<T>() return true if the Union currently holds the type matching T.
88 return Val.getInt() == ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0);
91 /// get<T>() - Return the value of the specified pointer type. If the
92 /// specified pointer type is incorrect, assert.
95 assert(is<T>() && "Invalid accessor called");
96 return static_cast<T>(Val.getPointer());
99 /// dyn_cast<T>() - If the current value is of the specified pointer type,
100 /// return it, otherwise return null.
103 if (is<T>()) return static_cast<T>(Val.getPointer());
107 /// Assignment operators - Allow assigning into this union from either
108 /// pointer type, setting the discriminator to remember what it came from.
109 const PointerUnion &operator=(const PT1 &RHS) {
114 const PointerUnion &operator=(const PT2 &RHS) {
120 void *getOpaqueValue() const { return Val.getOpaqueValue(); }
121 static PointerUnion getFromOpaqueValue(void *VP) {
123 V.Val = ValTy::getFromOpaqueValue(VP);
128 // Teach SmallPtrSet that PointerIntPair is "basically a pointer", that has
129 // # low bits available = min(PT1bits,PT2bits)-1.
130 template<typename PT1, typename PT2>
131 class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
134 getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
135 return P.getOpaqueValue();
137 static inline PointerUnion<PT1, PT2>
138 getFromVoidPointer(void *P) {
139 return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
142 // The number of bits available are the min of the two pointer types.
144 NumLowBitsAvailable =
145 PointerUnion<PT1,PT2>::ValTy::NumLowBitsAvailable