1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 18:42:46 +02:00

Revert "[ADT] Add new type traits for type pack indexes"

This reverts commit a6d3987b8ef3b7616f0835b89515c4264f2a7a64.
This commit is contained in:
Stella Stamenova 2021-05-17 20:26:59 -07:00
parent 0ce9cad66f
commit 06a52ce651
3 changed files with 20 additions and 95 deletions

View File

@ -64,6 +64,21 @@ namespace pointer_union_detail {
return std::min<int>({PointerLikeTypeTraits<Ts>::NumLowBitsAvailable...});
}
/// Find the index of a type in a list of types. TypeIndex<T, Us...>::Index
/// is the index of T in Us, or sizeof...(Us) if T does not appear in the
/// list.
template <typename T, typename ...Us> struct TypeIndex;
template <typename T, typename ...Us> struct TypeIndex<T, T, Us...> {
static constexpr int Index = 0;
};
template <typename T, typename U, typename... Us>
struct TypeIndex<T, U, Us...> {
static constexpr int Index = 1 + TypeIndex<T, Us...>::Index;
};
template <typename T> struct TypeIndex<T> {
static constexpr int Index = 0;
};
/// Find the first type in a list of types.
template <typename T, typename...> struct GetFirstType {
using type = T;
@ -130,7 +145,6 @@ namespace pointer_union_detail {
/// P = (float*)0;
/// Y = P.get<float*>(); // ok.
/// X = P.get<int*>(); // runtime assertion failure.
/// PointerUnion<int*, int*> Q; // compile time failure.
template <typename... PTs>
class PointerUnion
: public pointer_union_detail::PointerUnionMembers<
@ -139,14 +153,12 @@ class PointerUnion
void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int,
pointer_union_detail::PointerUnionUIntTraits<PTs...>>,
0, PTs...> {
static_assert(TypesAreDistinct<PTs...>(),
"PointerUnion alternative types cannot be repeated");
// The first type is special because we want to directly cast a pointer to a
// default-initialized union to a pointer to the first type. But we don't
// want PointerUnion to be a 'template <typename First, typename ...Rest>'
// because it's much more convenient to have a name for the whole pack. So
// split off the first type here.
using First = TypeAtIndex<0, PTs...>;
using First = typename pointer_union_detail::GetFirstType<PTs...>::type;
using Base = typename PointerUnion::PointerUnionMembers;
public:
@ -163,7 +175,10 @@ public:
/// Test if the Union currently holds the type matching T.
template <typename T> bool is() const {
return this->Val.getInt() == FirstIndexOfType<T, PTs...>{};
constexpr int Index = pointer_union_detail::TypeIndex<T, PTs...>::Index;
static_assert(Index < sizeof...(PTs),
"PointerUnion::is<T> given type not in the union");
return this->Val.getInt() == Index;
}
/// Returns the value of the specified pointer type.

View File

@ -144,60 +144,6 @@ template <typename ReturnType, typename... Args>
struct function_traits<ReturnType (&)(Args...), false>
: public function_traits<ReturnType (*)(Args...)> {};
namespace detail {
template <typename T, typename... Ts>
constexpr size_t getFirstIndexOfTypeImpl() {
constexpr bool Matches[] = {false, std::is_same<T, Ts>{}...};
// TODO: use llvm::find once it's constexpr (std::find is constexpr in C++20)
for (size_t I = 1; I <= sizeof...(Ts); ++I)
if (Matches[I])
return I - 1;
return sizeof...(Ts);
}
template <typename T, typename... Ts> constexpr size_t getFirstIndexOfType() {
constexpr size_t Index = getFirstIndexOfTypeImpl<T, Ts...>();
static_assert(Index != sizeof...(Ts),
"type T is not present in type pack Ts");
return Index;
}
template <typename... Ts> static constexpr bool areTypesDistinct() {
constexpr size_t IndexOf[] = {0, getFirstIndexOfType<Ts, Ts...>()...};
// TODO: use llvm::find once it's constexpr (std::find is constexpr in C++20)
for (size_t I = 1; I <= sizeof...(Ts); ++I)
if (I - 1 != IndexOf[I])
return false;
return true;
}
} // namespace detail
/// Determine if all types in Ts are distinct.
///
/// Useful to statically assert when Ts is intended to describe a non-multi set
/// of types.
template <typename... Ts>
using TypesAreDistinct =
std::integral_constant<bool, detail::areTypesDistinct<Ts...>()>;
/// Find the first index where a type appears in a list of types.
///
/// FirstIndexOfType<T, Ts...>::value is the first index of T in Ts.
///
/// Typically only meaningful when it is otherwise statically known that the
/// type pack has no duplicate types. This should be guaranteed explicitly with
/// static_assert(TypesAreDistinct<Ts...>{}).
///
/// It is a compile-time error to instantiate when T is not present in Ts, i.e.
/// if is_one_of<T, Ts...>::value is false.
template <typename T, typename... Ts>
using FirstIndexOfType =
std::integral_constant<size_t, detail::getFirstIndexOfType<T, Ts...>()>;
/// Find the type at a given index in a list of types.
///
/// TypeAtIndex<I, Ts...> is the type at index I in Ts.
template <size_t I, typename... Ts>
using TypeAtIndex = std::tuple_element_t<I, std::tuple<Ts...>>;
//===----------------------------------------------------------------------===//
// Extra additions to <functional>
//===----------------------------------------------------------------------===//

View File

@ -711,40 +711,4 @@ TEST(STLExtras, MoveRange) {
EXPECT_EQ(V4.size(), 4U);
EXPECT_TRUE(llvm::all_of(V4, HasVal));
}
TEST(STLExtrasTest, TypesAreDistinct) {
EXPECT_TRUE((llvm::TypesAreDistinct<>::value));
EXPECT_TRUE((llvm::TypesAreDistinct<int>::value));
EXPECT_FALSE((llvm::TypesAreDistinct<int, int>::value));
EXPECT_TRUE((llvm::TypesAreDistinct<int, float>::value));
EXPECT_FALSE((llvm::TypesAreDistinct<int, float, int>::value));
EXPECT_TRUE((llvm::TypesAreDistinct<int, float, double>::value));
EXPECT_FALSE((llvm::TypesAreDistinct<int, float, double, float>::value));
EXPECT_TRUE((llvm::TypesAreDistinct<int, int *>::value));
EXPECT_TRUE((llvm::TypesAreDistinct<int, int &>::value));
EXPECT_TRUE((llvm::TypesAreDistinct<int, int &&>::value));
EXPECT_TRUE((llvm::TypesAreDistinct<int, const int>::value));
}
TEST(STLExtrasTest, FirstIndexOfType) {
EXPECT_EQ((llvm::FirstIndexOfType<int, int>::value), 0u);
EXPECT_EQ((llvm::FirstIndexOfType<int, int, int>::value), 0u);
EXPECT_EQ((llvm::FirstIndexOfType<int, float, int>::value), 1u);
EXPECT_EQ((llvm::FirstIndexOfType<int const *, float, int, int const *,
const int>::value),
2u);
}
TEST(STLExtrasTest, TypeAtIndex) {
EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int>>::value));
EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int, float>>::value));
EXPECT_TRUE((std::is_same<float, llvm::TypeAtIndex<1, int, float>>::value));
EXPECT_TRUE(
(std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value));
EXPECT_TRUE(
(std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value));
EXPECT_TRUE(
(std::is_same<double, llvm::TypeAtIndex<2, int, float, double>>::value));
}
} // namespace