PointerUnion.h revision ba315c1ee7bef78c6824441fe6c1761596ec9880
1//===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- 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 defines the PointerUnion class, which is a discriminated union of 11// pointer types. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_ADT_POINTERUNION_H 16#define LLVM_ADT_POINTERUNION_H 17 18#include "llvm/ADT/PointerIntPair.h" 19 20namespace llvm { 21 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() } 31 32 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 { 37 public: 38 static inline void *getAsVoidPointer(void *P) { return P; } 39 static inline void *getFromVoidPointer(void *P) { return P; } 40 enum { 41 PT1BitsAv = PointerLikeTypeTraits<PT1>::NumLowBitsAvailable, 42 PT2BitsAv = PointerLikeTypeTraits<PT2>::NumLowBitsAvailable, 43 NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv 44 }; 45 }; 46 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. 51 /// 52 /// Common use patterns would be something like this: 53 /// PointerUnion<int*, float*> P; 54 /// P = (int*)0; 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. 59 /// P = (float*)0; 60 /// Y = P.get<float*>(); // ok. 61 /// X = P.get<int*>(); // runtime assertion failure. 62 template <typename PT1, typename PT2> 63 class PointerUnion { 64 public: 65 typedef PointerIntPair<void*, 1, bool, 66 PointerUnionUIntTraits<PT1,PT2> > ValTy; 67 private: 68 ValTy Val; 69 public: 70 PointerUnion() {} 71 72 PointerUnion(PT1 V) { 73 Val.setPointer(V); 74 Val.setInt(0); 75 } 76 PointerUnion(PT2 V) { 77 Val.setPointer(V); 78 Val.setInt(1); 79 } 80 81 bool isNull() const { return Val.getPointer() == 0; } 82 83 template<typename T> 84 int is() const { 85 return Val.getInt() == ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0); 86 } 87 template<typename T> 88 T get() const { 89 assert(is<T>() && "Invalid accessor called"); 90 return static_cast<T>(Val.getPointer()); 91 } 92 93 const PointerUnion &operator=(const PT1 &RHS) { 94 Val.setPointer(RHS); 95 Val.setInt(0); 96 return *this; 97 } 98 const PointerUnion &operator=(const PT2 &RHS) { 99 Val.setPointer(RHS); 100 Val.setInt(1); 101 return *this; 102 } 103 104 void *getOpaqueValue() const { return Val.getOpaqueValue(); } 105 static PointerUnion getFromOpaqueValue(void *VP) { 106 PointerUnion V; 107 V.Val = ValTy::getFromOpaqueValue(VP); 108 return V; 109 } 110 }; 111 112 // Teach SmallPtrSet that PointerIntPair is "basically a pointer", that has 113 // # low bits available = min(PT1bits,PT2bits)-1. 114 template<typename PT1, typename PT2> 115 class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > { 116 public: 117 static inline void * 118 getAsVoidPointer(const PointerUnion<PT1, PT2> &P) { 119 return P.getOpaqueValue(); 120 } 121 static inline PointerUnion<PT1, PT2> 122 getFromVoidPointer(void *P) { 123 return PointerUnion<PT1, PT2>::getFromOpaqueValue(P); 124 } 125 126 // The number of bits available are the min of the two pointer types. 127 enum { 128 NumLowBitsAvailable = 129 PointerUnion<PT1,PT2>::ValTy::NumLowBitsAvailable 130 }; 131 }; 132} 133 134#endif 135