forked from OSchip/llvm-project
[ADT] Adopt the new casting infrastructure for PointerUnion
Reviewed By: lattner, bzcheeseman Differential Revision: https://reviews.llvm.org/D125609
This commit is contained in:
parent
485dd0b752
commit
aab5bd180a
|
@ -18,6 +18,7 @@
|
|||
#include "llvm/ADT/DenseMapInfo.h"
|
||||
#include "llvm/ADT/PointerIntPair.h"
|
||||
#include "llvm/ADT/STLExtras.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
#include "llvm/Support/PointerLikeTypeTraits.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
@ -87,6 +88,9 @@ namespace pointer_union_detail {
|
|||
};
|
||||
}
|
||||
|
||||
// This is a forward declaration of CastInfoPointerUnionImpl
|
||||
// Refer to its definition below for further details
|
||||
template <typename... PTs> struct CastInfoPointerUnionImpl;
|
||||
/// A discriminated union of two or more pointer types, with the discriminator
|
||||
/// in the low bit of the pointer.
|
||||
///
|
||||
|
@ -122,6 +126,11 @@ class PointerUnion
|
|||
using First = TypeAtIndex<0, PTs...>;
|
||||
using Base = typename PointerUnion::PointerUnionMembers;
|
||||
|
||||
/// This is needed to give the CastInfo implementation below access
|
||||
/// to protected members.
|
||||
/// Refer to its definition for further details.
|
||||
friend struct CastInfoPointerUnionImpl<PTs...>;
|
||||
|
||||
public:
|
||||
PointerUnion() = default;
|
||||
|
||||
|
@ -134,25 +143,24 @@ public:
|
|||
|
||||
explicit operator bool() const { return !isNull(); }
|
||||
|
||||
// FIXME: Replace the uses of is(), get() and dyn_cast() with
|
||||
// isa<T>, cast<T> and the llvm::dyn_cast<T>
|
||||
|
||||
/// Test if the Union currently holds the type matching T.
|
||||
template <typename T> bool is() const {
|
||||
return this->Val.getInt() == FirstIndexOfType<T, PTs...>::value;
|
||||
}
|
||||
template <typename T> inline bool is() const { return isa<T>(*this); }
|
||||
|
||||
/// Returns the value of the specified pointer type.
|
||||
///
|
||||
/// If the specified pointer type is incorrect, assert.
|
||||
template <typename T> T get() const {
|
||||
assert(is<T>() && "Invalid accessor called");
|
||||
return PointerLikeTypeTraits<T>::getFromVoidPointer(this->Val.getPointer());
|
||||
template <typename T> inline T get() const {
|
||||
assert(isa<T>(*this) && "Invalid accessor called");
|
||||
return cast<T>(*this);
|
||||
}
|
||||
|
||||
/// Returns the current pointer if it is of the specified pointer type,
|
||||
/// otherwise returns null.
|
||||
template <typename T> T dyn_cast() const {
|
||||
if (is<T>())
|
||||
return get<T>();
|
||||
return T();
|
||||
template <typename T> inline T dyn_cast() const {
|
||||
return llvm::dyn_cast<T>(*this);
|
||||
}
|
||||
|
||||
/// If the union is set to the first pointer type get an address pointing to
|
||||
|
@ -205,6 +213,52 @@ bool operator<(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
|
|||
return lhs.getOpaqueValue() < rhs.getOpaqueValue();
|
||||
}
|
||||
|
||||
/// We can't (at least, at this moment with C++14) declare CastInfo
|
||||
/// as a friend of PointerUnion like this:
|
||||
/// ```
|
||||
/// template<typename To>
|
||||
/// friend struct CastInfo<To, PointerUnion<PTs...>>;
|
||||
/// ```
|
||||
/// The compiler complains 'Partial specialization cannot be declared as a
|
||||
/// friend'.
|
||||
/// So we define this struct to be a bridge between CastInfo and
|
||||
/// PointerUnion.
|
||||
template <typename... PTs> struct CastInfoPointerUnionImpl {
|
||||
using From = PointerUnion<PTs...>;
|
||||
|
||||
template <typename To> static inline bool isPossible(From &F) {
|
||||
return F.Val.getInt() == FirstIndexOfType<To, PTs...>::value;
|
||||
}
|
||||
|
||||
template <typename To> static To doCast(From &F) {
|
||||
assert(isPossible<To>(F) && "cast to an incompatible type !");
|
||||
return PointerLikeTypeTraits<To>::getFromVoidPointer(F.Val.getPointer());
|
||||
}
|
||||
};
|
||||
|
||||
// Specialization of CastInfo for PointerUnion
|
||||
template <typename To, typename... PTs>
|
||||
struct CastInfo<To, PointerUnion<PTs...>>
|
||||
: public DefaultDoCastIfPossible<To, PointerUnion<PTs...>,
|
||||
CastInfo<To, PointerUnion<PTs...>>> {
|
||||
using From = PointerUnion<PTs...>;
|
||||
using Impl = CastInfoPointerUnionImpl<PTs...>;
|
||||
|
||||
static inline bool isPossible(From &f) {
|
||||
return Impl::template isPossible<To>(f);
|
||||
}
|
||||
|
||||
static To doCast(From &f) { return Impl::template doCast<To>(f); }
|
||||
|
||||
static inline To castFailed() { return To(); }
|
||||
};
|
||||
|
||||
template <typename To, typename... PTs>
|
||||
struct CastInfo<To, const PointerUnion<PTs...>>
|
||||
: public ConstStrippingForwardingCast<To, const PointerUnion<PTs...>,
|
||||
CastInfo<To, PointerUnion<PTs...>>> {
|
||||
};
|
||||
|
||||
// Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
|
||||
// # low bits available = min(PT1bits,PT2bits)-1.
|
||||
template <typename ...PTs>
|
||||
|
|
|
@ -156,4 +156,136 @@ TEST_F(PointerUnionTest, GetAddrOfPtr1) {
|
|||
EXPECT_TRUE((void *)n.getAddrOfPtr1() == (void *)&n);
|
||||
}
|
||||
|
||||
TEST_F(PointerUnionTest, NewCastInfra) {
|
||||
// test isa<>
|
||||
EXPECT_TRUE(isa<float *>(a));
|
||||
EXPECT_TRUE(isa<int *>(b));
|
||||
EXPECT_TRUE(isa<int *>(c));
|
||||
EXPECT_TRUE(isa<int *>(n));
|
||||
EXPECT_TRUE(isa<int *>(i3));
|
||||
EXPECT_TRUE(isa<float *>(f3));
|
||||
EXPECT_TRUE(isa<long long *>(l3));
|
||||
EXPECT_TRUE(isa<int *>(i4));
|
||||
EXPECT_TRUE(isa<float *>(f4));
|
||||
EXPECT_TRUE(isa<long long *>(l4));
|
||||
EXPECT_TRUE(isa<double *>(d4));
|
||||
EXPECT_TRUE(isa<int *>(i4null));
|
||||
EXPECT_TRUE(isa<float *>(f4null));
|
||||
EXPECT_TRUE(isa<long long *>(l4null));
|
||||
EXPECT_TRUE(isa<double *>(d4null));
|
||||
EXPECT_FALSE(isa<int *>(a));
|
||||
EXPECT_FALSE(isa<float *>(b));
|
||||
EXPECT_FALSE(isa<float *>(c));
|
||||
EXPECT_FALSE(isa<float *>(n));
|
||||
EXPECT_FALSE(isa<float *>(i3));
|
||||
EXPECT_FALSE(isa<long long *>(i3));
|
||||
EXPECT_FALSE(isa<int *>(f3));
|
||||
EXPECT_FALSE(isa<long long *>(f3));
|
||||
EXPECT_FALSE(isa<int *>(l3));
|
||||
EXPECT_FALSE(isa<float *>(l3));
|
||||
EXPECT_FALSE(isa<float *>(i4));
|
||||
EXPECT_FALSE(isa<long long *>(i4));
|
||||
EXPECT_FALSE(isa<double *>(i4));
|
||||
EXPECT_FALSE(isa<int *>(f4));
|
||||
EXPECT_FALSE(isa<long long *>(f4));
|
||||
EXPECT_FALSE(isa<double *>(f4));
|
||||
EXPECT_FALSE(isa<int *>(l4));
|
||||
EXPECT_FALSE(isa<float *>(l4));
|
||||
EXPECT_FALSE(isa<double *>(l4));
|
||||
EXPECT_FALSE(isa<int *>(d4));
|
||||
EXPECT_FALSE(isa<float *>(d4));
|
||||
EXPECT_FALSE(isa<long long *>(d4));
|
||||
EXPECT_FALSE(isa<float *>(i4null));
|
||||
EXPECT_FALSE(isa<long long *>(i4null));
|
||||
EXPECT_FALSE(isa<double *>(i4null));
|
||||
EXPECT_FALSE(isa<int *>(f4null));
|
||||
EXPECT_FALSE(isa<long long *>(f4null));
|
||||
EXPECT_FALSE(isa<double *>(f4null));
|
||||
EXPECT_FALSE(isa<int *>(l4null));
|
||||
EXPECT_FALSE(isa<float *>(l4null));
|
||||
EXPECT_FALSE(isa<double *>(l4null));
|
||||
EXPECT_FALSE(isa<int *>(d4null));
|
||||
EXPECT_FALSE(isa<float *>(d4null));
|
||||
EXPECT_FALSE(isa<long long *>(d4null));
|
||||
|
||||
// test cast<>
|
||||
EXPECT_EQ(cast<float *>(a), &f);
|
||||
EXPECT_EQ(cast<int *>(b), &i);
|
||||
EXPECT_EQ(cast<int *>(c), &i);
|
||||
EXPECT_EQ(cast<int *>(i3), &i);
|
||||
EXPECT_EQ(cast<float *>(f3), &f);
|
||||
EXPECT_EQ(cast<long long *>(l3), &l);
|
||||
EXPECT_EQ(cast<int *>(i4), &i);
|
||||
EXPECT_EQ(cast<float *>(f4), &f);
|
||||
EXPECT_EQ(cast<long long *>(l4), &l);
|
||||
EXPECT_EQ(cast<double *>(d4), &d);
|
||||
|
||||
// test dyn_cast
|
||||
EXPECT_EQ(dyn_cast<int *>(a), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(a), &f);
|
||||
EXPECT_EQ(dyn_cast<int *>(b), &i);
|
||||
EXPECT_EQ(dyn_cast<float *>(b), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(c), &i);
|
||||
EXPECT_EQ(dyn_cast<float *>(c), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(n), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(n), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(i3), &i);
|
||||
EXPECT_EQ(dyn_cast<float *>(i3), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(i3), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(f3), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(f3), &f);
|
||||
EXPECT_EQ(dyn_cast<long long *>(f3), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(l3), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(l3), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(l3), &l);
|
||||
EXPECT_EQ(dyn_cast<int *>(i4), &i);
|
||||
EXPECT_EQ(dyn_cast<float *>(i4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(i4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<double *>(i4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(f4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(f4), &f);
|
||||
EXPECT_EQ(dyn_cast<long long *>(f4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<double *>(f4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(l4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(l4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(l4), &l);
|
||||
EXPECT_EQ(dyn_cast<double *>(l4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(d4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(d4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(d4), nullptr);
|
||||
EXPECT_EQ(dyn_cast<double *>(d4), &d);
|
||||
EXPECT_EQ(dyn_cast<int *>(i4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(i4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(i4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<double *>(i4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(f4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(f4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(f4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<double *>(f4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(l4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(l4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(l4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<double *>(l4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<int *>(d4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<float *>(d4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<long long *>(d4null), nullptr);
|
||||
EXPECT_EQ(dyn_cast<double *>(d4null), nullptr);
|
||||
|
||||
// test for const
|
||||
const PU4 constd4(&d);
|
||||
EXPECT_TRUE(isa<double *>(constd4));
|
||||
EXPECT_FALSE(isa<int *>(constd4));
|
||||
EXPECT_EQ(cast<double *>(constd4), &d);
|
||||
EXPECT_EQ(dyn_cast<long long *>(constd4), nullptr);
|
||||
|
||||
auto *result1 = cast<double *>(constd4);
|
||||
static_assert(std::is_same<double *, decltype(result1)>::value,
|
||||
"type mismatch for cast with PointerUnion");
|
||||
|
||||
PointerUnion<int *, const double *> constd2(&d);
|
||||
auto *result2 = cast<const double *>(d);
|
||||
static_assert(std::is_same<const double *, decltype(result2)>::value,
|
||||
"type mismatch for cast with PointerUnion");
|
||||
}
|
||||
|
||||
} // end anonymous namespace
|
||||
|
|
Loading…
Reference in New Issue