//===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file defines the PointerUnion class, which is a discriminated union of // pointer types. // //===----------------------------------------------------------------------===// #ifndef LLVM_ADT_POINTERUNION_H #define LLVM_ADT_POINTERUNION_H #include "llvm/ADT/PointerIntPair.h" namespace llvm { /// getPointerUnionTypeNum - If the argument has type PT1* or PT2* return /// false or true respectively. template static inline int getPointerUnionTypeNum(PT1 *P) { return 0; } template static inline int getPointerUnionTypeNum(PT2 *P) { return 1; } template static inline int getPointerUnionTypeNum(...) { return -1; } /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion /// for the two template arguments. template class PointerUnionUIntTraits { public: static inline void *getAsVoidPointer(void *P) { return P; } static inline void *getFromVoidPointer(void *P) { return P; } enum { PT1BitsAv = PointerLikeTypeTraits::NumLowBitsAvailable, PT2BitsAv = PointerLikeTypeTraits::NumLowBitsAvailable, NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv }; }; /// PointerUnion - This implements a discriminated union of two pointer types, /// and keeps the discriminator bit-mangled into the low bits of the pointer. /// This allows the implementation to be extremely efficient in space, but /// permits a very natural and type-safe API. /// /// Common use patterns would be something like this: /// PointerUnion P; /// P = (int*)0; /// printf("%d %d", P.is(), P.is()); // prints "1 0" /// X = P.get(); // ok. /// Y = P.get(); // runtime assertion failure. /// Z = P.get(); // runtime assertion failure (regardless of tag) /// P = (float*)0; /// Y = P.get(); // ok. /// X = P.get(); // runtime assertion failure. template class PointerUnion { public: typedef PointerIntPair > ValTy; private: ValTy Val; public: PointerUnion() {} PointerUnion(PT1 V) { Val.setPointer( const_cast(PointerLikeTypeTraits::getAsVoidPointer(V))); Val.setInt(0); } PointerUnion(PT2 V) { Val.setPointer( const_cast(PointerLikeTypeTraits::getAsVoidPointer(V))); Val.setInt(1); } /// isNull - Return true if the pointer held in the union is null, /// regardless of which type it is. bool isNull() const { return Val.getPointer() == 0; } operator bool() const { return !isNull(); } /// is() return true if the Union currently holds the type matching T. template int is() const { int TyNo = ::llvm::getPointerUnionTypeNum((T*)0); assert(TyNo != -1 && "Type query could never succeed on PointerUnion!"); return static_cast(Val.getInt()) == TyNo; } /// get() - Return the value of the specified pointer type. If the /// specified pointer type is incorrect, assert. template T get() const { assert(is() && "Invalid accessor called"); return PointerLikeTypeTraits::getFromVoidPointer(Val.getPointer()); } /// dyn_cast() - If the current value is of the specified pointer type, /// return it, otherwise return null. template T dyn_cast() const { if (is()) return get(); return T(); } /// Assignment operators - Allow assigning into this union from either /// pointer type, setting the discriminator to remember what it came from. const PointerUnion &operator=(const PT1 &RHS) { Val.setPointer( const_cast(PointerLikeTypeTraits::getAsVoidPointer(RHS))); Val.setInt(0); return *this; } const PointerUnion &operator=(const PT2 &RHS) { Val.setPointer( const_cast(PointerLikeTypeTraits::getAsVoidPointer(RHS))); Val.setInt(1); return *this; } void *getOpaqueValue() const { return Val.getOpaqueValue(); } static inline PointerUnion getFromOpaqueValue(void *VP) { PointerUnion V; V.Val = ValTy::getFromOpaqueValue(VP); return V; } }; // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has // # low bits available = min(PT1bits,PT2bits)-1. template class PointerLikeTypeTraits > { public: static inline void * getAsVoidPointer(const PointerUnion &P) { return P.getOpaqueValue(); } static inline PointerUnion getFromVoidPointer(void *P) { return PointerUnion::getFromOpaqueValue(P); } // The number of bits available are the min of the two pointer types. enum { NumLowBitsAvailable = PointerLikeTypeTraits::ValTy> ::NumLowBitsAvailable }; }; /// PointerUnion3 - This is a pointer union of three pointer types. See /// documentation for PointerUnion for usage. template class PointerUnion3 { public: typedef PointerUnion InnerUnion; typedef PointerUnion ValTy; private: ValTy Val; public: PointerUnion3() {} PointerUnion3(PT1 V) { Val = InnerUnion(V); } PointerUnion3(PT2 V) { Val = InnerUnion(V); } PointerUnion3(PT3 V) { Val = V; } /// isNull - Return true if the pointer held in the union is null, /// regardless of which type it is. bool isNull() const { return Val.isNull(); } operator bool() const { return !isNull(); } /// is() return true if the Union currently holds the type matching T. template int is() const { // Is it PT1/PT2? if (::llvm::getPointerUnionTypeNum((T*)0) != -1) return Val.template is() && Val.template get().template is(); return Val.template is(); } /// get() - Return the value of the specified pointer type. If the /// specified pointer type is incorrect, assert. template T get() const { assert(is() && "Invalid accessor called"); // Is it PT1/PT2? if (::llvm::getPointerUnionTypeNum((T*)0) != -1) return Val.template get().template get(); return Val.template get(); } /// dyn_cast() - If the current value is of the specified pointer type, /// return it, otherwise return null. template T dyn_cast() const { if (is()) return get(); return T(); } /// Assignment operators - Allow assigning into this union from either /// pointer type, setting the discriminator to remember what it came from. const PointerUnion3 &operator=(const PT1 &RHS) { Val = InnerUnion(RHS); return *this; } const PointerUnion3 &operator=(const PT2 &RHS) { Val = InnerUnion(RHS); return *this; } const PointerUnion3 &operator=(const PT3 &RHS) { Val = RHS; return *this; } void *getOpaqueValue() const { return Val.getOpaqueValue(); } static inline PointerUnion3 getFromOpaqueValue(void *VP) { PointerUnion3 V; V.Val = ValTy::getFromOpaqueValue(VP); return V; } }; // Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has // # low bits available = min(PT1bits,PT2bits,PT2bits)-2. template class PointerLikeTypeTraits > { public: static inline void * getAsVoidPointer(const PointerUnion3 &P) { return P.getOpaqueValue(); } static inline PointerUnion3 getFromVoidPointer(void *P) { return PointerUnion3::getFromOpaqueValue(P); } // The number of bits available are the min of the two pointer types. enum { NumLowBitsAvailable = PointerLikeTypeTraits::ValTy> ::NumLowBitsAvailable }; }; /// PointerUnion4 - This is a pointer union of four pointer types. See /// documentation for PointerUnion for usage. template class PointerUnion4 { public: typedef PointerUnion InnerUnion1; typedef PointerUnion InnerUnion2; typedef PointerUnion ValTy; private: ValTy Val; public: PointerUnion4() {} PointerUnion4(PT1 V) { Val = InnerUnion1(V); } PointerUnion4(PT2 V) { Val = InnerUnion1(V); } PointerUnion4(PT3 V) { Val = InnerUnion2(V); } PointerUnion4(PT4 V) { Val = InnerUnion2(V); } /// isNull - Return true if the pointer held in the union is null, /// regardless of which type it is. bool isNull() const { return Val.isNull(); } operator bool() const { return !isNull(); } /// is() return true if the Union currently holds the type matching T. template int is() const { // Is it PT1/PT2? if (::llvm::getPointerUnionTypeNum((T*)0) != -1) return Val.template is() && Val.template get().template is(); return Val.template is() && Val.template get().template is(); } /// get() - Return the value of the specified pointer type. If the /// specified pointer type is incorrect, assert. template T get() const { assert(is() && "Invalid accessor called"); // Is it PT1/PT2? if (::llvm::getPointerUnionTypeNum((T*)0) != -1) return Val.template get().template get(); return Val.template get().template get(); } /// dyn_cast() - If the current value is of the specified pointer type, /// return it, otherwise return null. template T dyn_cast() const { if (is()) return get(); return T(); } /// Assignment operators - Allow assigning into this union from either /// pointer type, setting the discriminator to remember what it came from. const PointerUnion4 &operator=(const PT1 &RHS) { Val = InnerUnion1(RHS); return *this; } const PointerUnion4 &operator=(const PT2 &RHS) { Val = InnerUnion1(RHS); return *this; } const PointerUnion4 &operator=(const PT3 &RHS) { Val = InnerUnion2(RHS); return *this; } const PointerUnion4 &operator=(const PT4 &RHS) { Val = InnerUnion2(RHS); return *this; } void *getOpaqueValue() const { return Val.getOpaqueValue(); } static inline PointerUnion4 getFromOpaqueValue(void *VP) { PointerUnion4 V; V.Val = ValTy::getFromOpaqueValue(VP); return V; } }; // Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has // # low bits available = min(PT1bits,PT2bits,PT2bits)-2. template class PointerLikeTypeTraits > { public: static inline void * getAsVoidPointer(const PointerUnion4 &P) { return P.getOpaqueValue(); } static inline PointerUnion4 getFromVoidPointer(void *P) { return PointerUnion4::getFromOpaqueValue(P); } // The number of bits available are the min of the two pointer types. enum { NumLowBitsAvailable = PointerLikeTypeTraits::ValTy> ::NumLowBitsAvailable }; }; } #endif