X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2FPointerUnion.h;h=da67c8e8c27b8aab06592cb40f22fef07931347c;hb=e8bc475668ddd2f31f44dd00b042d15b255e1b9e;hp=8ea1315a736e289e12354c6d58fae7a7cffafe5f;hpb=2491e4657d95f7ff61fb2a741ba9996e492df515;p=oota-llvm.git diff --git a/include/llvm/ADT/PointerUnion.h b/include/llvm/ADT/PointerUnion.h index 8ea1315a736..da67c8e8c27 100644 --- a/include/llvm/ADT/PointerUnion.h +++ b/include/llvm/ADT/PointerUnion.h @@ -22,12 +22,11 @@ namespace llvm { /// getPointerUnionTypeNum - If the argument has type PT1* or PT2* return /// false or true respectively. template - static inline bool getPointerUnionTypeNum(PT1 *P) { return false; } + static inline int getPointerUnionTypeNum(PT1 *P) { return 0; } template - static inline bool getPointerUnionTypeNum(PT2 *P) { return true; } - // Enable, if we could use static_assert. - //template - //static inline bool getPointerUnionTypeNum(...) { abort() } + static inline int getPointerUnionTypeNum(PT2 *P) { return 1; } + template + static inline int getPointerUnionTypeNum(...) { return -1; } /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion @@ -70,31 +69,52 @@ namespace llvm { PointerUnion() {} PointerUnion(PT1 V) { - Val.setPointer(V); + Val.setPointer(PointerLikeTypeTraits::getAsVoidPointer(V)); Val.setInt(0); } PointerUnion(PT2 V) { - Val.setPointer(V); + Val.setPointer(PointerLikeTypeTraits::getAsVoidPointer(V)); Val.setInt(1); } + /// isNull - Return true if the pointer help 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 { - return Val.getInt() == ::llvm::getPointerUnionTypeNum((T*)0); + int TyNo = ::llvm::getPointerUnionTypeNum((T*)0); + assert(TyNo != -1 && "Type query could never succeed on PointerUnion!"); + return 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 static_cast(Val.getPointer()); + 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(RHS); + Val.setPointer(PointerLikeTypeTraits::getAsVoidPointer(RHS)); Val.setInt(0); return *this; } const PointerUnion &operator=(const PT2 &RHS) { - Val.setPointer(RHS); + Val.setPointer(PointerLikeTypeTraits::getAsVoidPointer(RHS)); Val.setInt(1); return *this; } @@ -107,7 +127,7 @@ namespace llvm { } }; - // Teach SmallPtrSet that PointerIntPair is "basically a pointer", that has + // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has // # low bits available = min(PT1bits,PT2bits)-1. template class PointerLikeTypeTraits > { @@ -124,7 +144,110 @@ namespace llvm { // The number of bits available are the min of the two pointer types. enum { NumLowBitsAvailable = - PointerUnion::ValTy::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 help 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.get().is(); + // Must be PT3 or statically invalid. + assert(Val.is()); + return true; + } + + /// 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"); + if (Val.is()) + return Val.get(); + return Val.get().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 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 }; }; }