diff options
author | Chris Lattner <sabre@nondot.org> | 2009-03-29 06:06:02 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-03-29 06:06:02 +0000 |
commit | 2491e4657d95f7ff61fb2a741ba9996e492df515 (patch) | |
tree | c48a2fd0a701d382b5b5ac273a81aa1fa4e861f5 /include/llvm/ADT | |
parent | 540db8bcb8d7dae55456d8564c1d54071ec6ed50 (diff) | |
download | external_llvm-2491e4657d95f7ff61fb2a741ba9996e492df515.zip external_llvm-2491e4657d95f7ff61fb2a741ba9996e492df515.tar.gz external_llvm-2491e4657d95f7ff61fb2a741ba9996e492df515.tar.bz2 |
Add a simple type-safe bit-mangling pointer union class. This allows
you to do things like:
/// PointerUnion<int*, float*> P;
/// P = (int*)0;
/// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0"
/// X = P.get<int*>(); // ok.
/// Y = P.get<float*>(); // runtime assertion failure.
/// Z = P.get<double*>(); // does not compile.
/// P = (float*)0;
/// Y = P.get<float*>(); // ok.
/// X = P.get<int*>(); // runtime assertion failure.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@67987 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT')
-rw-r--r-- | include/llvm/ADT/PointerUnion.h | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/include/llvm/ADT/PointerUnion.h b/include/llvm/ADT/PointerUnion.h new file mode 100644 index 0000000..8ea1315 --- /dev/null +++ b/include/llvm/ADT/PointerUnion.h @@ -0,0 +1,132 @@ +//===- 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 <typename PT1, typename PT2> + static inline bool getPointerUnionTypeNum(PT1 *P) { return false; } + template <typename PT1, typename PT2> + static inline bool getPointerUnionTypeNum(PT2 *P) { return true; } + // Enable, if we could use static_assert. + //template <typename PT1, typename PT2> + //static inline bool getPointerUnionTypeNum(...) { abort() } + + + /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion + /// for the two template arguments. + template <typename PT1, typename PT2> + class PointerUnionUIntTraits { + public: + static inline void *getAsVoidPointer(void *P) { return P; } + static inline void *getFromVoidPointer(void *P) { return P; } + enum { + PT1BitsAv = PointerLikeTypeTraits<PT1>::NumLowBitsAvailable, + PT2BitsAv = PointerLikeTypeTraits<PT2>::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<int*, float*> P; + /// P = (int*)0; + /// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0" + /// X = P.get<int*>(); // ok. + /// Y = P.get<float*>(); // runtime assertion failure. + /// Z = P.get<double*>(); // does not compile. + /// P = (float*)0; + /// Y = P.get<float*>(); // ok. + /// X = P.get<int*>(); // runtime assertion failure. + template <typename PT1, typename PT2> + class PointerUnion { + public: + typedef PointerIntPair<void*, 1, bool, + PointerUnionUIntTraits<PT1,PT2> > ValTy; + private: + ValTy Val; + public: + PointerUnion() {} + + PointerUnion(PT1 V) { + Val.setPointer(V); + Val.setInt(0); + } + PointerUnion(PT2 V) { + Val.setPointer(V); + Val.setInt(1); + } + + template<typename T> + int is() const { + return Val.getInt() == ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0); + } + template<typename T> + T get() const { + assert(is<T>() && "Invalid accessor called"); + return static_cast<T>(Val.getPointer()); + } + + const PointerUnion &operator=(const PT1 &RHS) { + Val.setPointer(RHS); + Val.setInt(0); + return *this; + } + const PointerUnion &operator=(const PT2 &RHS) { + Val.setPointer(RHS); + Val.setInt(1); + return *this; + } + + void *getOpaqueValue() const { return Val.getOpaqueValue(); } + static PointerUnion getFromOpaqueValue(void *VP) { + PointerUnion V; + V.Val = ValTy::getFromOpaqueValue(VP); + return V; + } + }; + + // Teach SmallPtrSet that PointerIntPair is "basically a pointer", that has + // # low bits available = min(PT1bits,PT2bits)-1. + template<typename PT1, typename PT2> + class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > { + public: + static inline void * + getAsVoidPointer(const PointerUnion<PT1, PT2> &P) { + return P.getOpaqueValue(); + } + static inline PointerUnion<PT1, PT2> + getFromVoidPointer(void *P) { + return PointerUnion<PT1, PT2>::getFromOpaqueValue(P); + } + + // The number of bits available are the min of the two pointer types. + enum { + NumLowBitsAvailable = + PointerUnion<PT1,PT2>::ValTy::NumLowBitsAvailable + }; + }; +} + +#endif |