[ADT] Add new type traits for type pack indexes

Similar versions of these already exist, this effectively just just
factors them out into STLExtras. I plan to use these in future patches.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D100672
This commit is contained in:
Scott Linder 2021-12-14 19:27:36 +00:00
parent eea568927b
commit 78a26c7daf
3 changed files with 96 additions and 20 deletions

View File

@ -16,6 +16,7 @@
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include <algorithm>
#include <cassert>
@ -35,21 +36,6 @@ 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;
@ -116,6 +102,7 @@ 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<
@ -124,12 +111,14 @@ class PointerUnion
void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int,
pointer_union_detail::PointerUnionUIntTraits<PTs...>>,
0, PTs...> {
static_assert(TypesAreDistinct<PTs...>::value,
"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 = typename pointer_union_detail::GetFirstType<PTs...>::type;
using First = TypeAtIndex<0, PTs...>;
using Base = typename PointerUnion::PointerUnionMembers;
public:
@ -146,10 +135,7 @@ public:
/// Test if the Union currently holds the type matching T.
template <typename T> bool is() const {
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;
return this->Val.getInt() == FirstIndexOfType<T, PTs...>::value;
}
/// Returns the value of the specified pointer type.

View File

@ -144,6 +144,61 @@ template <typename ReturnType, typename... Args>
struct function_traits<ReturnType (&)(Args...), false>
: public function_traits<ReturnType (*)(Args...)> {};
/// traits class for checking whether type T is one of any of the given
/// types in the variadic list.
template <typename T, typename... Ts>
using is_one_of = disjunction<std::is_same<T, Ts>...>;
/// traits class for checking whether type T is a base class for all
/// the given types in the variadic list.
template <typename T, typename... Ts>
using are_base_of = conjunction<std::is_base_of<T, Ts>...>;
namespace detail {
template <typename T, typename... Us> struct TypesAreDistinct;
template <typename T, typename... Us>
struct TypesAreDistinct
: std::integral_constant<bool, !is_one_of<T, Us...>::value &&
TypesAreDistinct<Us...>::value> {};
template <typename T> struct TypesAreDistinct<T> : std::true_type {};
} // 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.
///
/// Expensive (currently quadratic in sizeof(Ts...)), and so should only be
/// asserted once per instantiation of a type which requires it.
template <typename... Ts> struct TypesAreDistinct;
template <> struct TypesAreDistinct<> : std::true_type {};
template <typename... Ts>
struct TypesAreDistinct
: std::integral_constant<bool, detail::TypesAreDistinct<Ts...>::value> {};
/// Find the first index where a type appears in a list of types.
///
/// FirstIndexOfType<T, Us...>::value is the first index of T in Us.
///
/// 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<Us...>::value).
///
/// It is a compile-time error to instantiate when T is not present in Us, i.e.
/// if is_one_of<T, Us...>::value is false.
template <typename T, typename... Us> struct FirstIndexOfType;
template <typename T, typename U, typename... Us>
struct FirstIndexOfType<T, U, Us...>
: std::integral_constant<size_t, 1 + FirstIndexOfType<T, Us...>::value> {};
template <typename T, typename... Us>
struct FirstIndexOfType<T, T, Us...> : std::integral_constant<size_t, 0> {};
/// 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

@ -905,4 +905,39 @@ TEST(STLExtrasTest, AllOfZip) {
all_of_zip(v1, v2, v_short, [](int, int, int) { return true; }));
}
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