mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 12:41:49 +01:00
[llvm][STLExtras] Add various type_trait utilities currently present in MLIR
This revision moves several type_trait utilities from MLIR into LLVM. Namely, this revision adds: is_detected - This matches the experimental std::is_detected is_invocable - This matches the c++17 std::is_invocable function_traits - A utility traits class for getting the argument and result types of a callable type Differential Revision: https://reviews.llvm.org/D78059
This commit is contained in:
parent
a151a2e594
commit
1b5b677215
@ -75,6 +75,79 @@ template <typename T> struct make_const_ref {
|
||||
typename std::add_const<T>::type>::type;
|
||||
};
|
||||
|
||||
/// Utilities for detecting if a given trait holds for some set of arguments
|
||||
/// 'Args'. For example, the given trait could be used to detect if a given type
|
||||
/// has a copy assignment operator:
|
||||
/// template<class T>
|
||||
/// using has_copy_assign_t = decltype(std::declval<T&>()
|
||||
/// = std::declval<const T&>());
|
||||
/// bool fooHasCopyAssign = is_detected<has_copy_assign_t, FooClass>::value;
|
||||
namespace detail {
|
||||
template <typename...> using void_t = void;
|
||||
template <class, template <class...> class Op, class... Args> struct detector {
|
||||
using value_t = std::false_type;
|
||||
};
|
||||
template <template <class...> class Op, class... Args>
|
||||
struct detector<void_t<Op<Args...>>, Op, Args...> {
|
||||
using value_t = std::true_type;
|
||||
};
|
||||
} // end namespace detail
|
||||
|
||||
template <template <class...> class Op, class... Args>
|
||||
using is_detected = typename detail::detector<void, Op, Args...>::value_t;
|
||||
|
||||
/// Check if a Callable type can be invoked with the given set of arg types.
|
||||
namespace detail {
|
||||
template <typename Callable, typename... Args>
|
||||
using is_invocable =
|
||||
decltype(std::declval<Callable &>()(std::declval<Args>()...));
|
||||
} // namespace detail
|
||||
|
||||
template <typename Callable, typename... Args>
|
||||
using is_invocable = is_detected<detail::is_invocable, Callable, Args...>;
|
||||
|
||||
/// This class provides various trait information about a callable object.
|
||||
/// * To access the number of arguments: Traits::num_args
|
||||
/// * To access the type of an argument: Traits::arg_t<i>
|
||||
/// * To access the type of the result: Traits::result_t
|
||||
template <typename T, bool isClass = std::is_class<T>::value>
|
||||
struct function_traits : public function_traits<decltype(&T::operator())> {};
|
||||
|
||||
/// Overload for class function types.
|
||||
template <typename ClassType, typename ReturnType, typename... Args>
|
||||
struct function_traits<ReturnType (ClassType::*)(Args...) const, false> {
|
||||
/// The number of arguments to this function.
|
||||
enum { num_args = sizeof...(Args) };
|
||||
|
||||
/// The result type of this function.
|
||||
using result_t = ReturnType;
|
||||
|
||||
/// The type of an argument to this function.
|
||||
template <size_t i>
|
||||
using arg_t = typename std::tuple_element<i, std::tuple<Args...>>::type;
|
||||
};
|
||||
/// Overload for class function types.
|
||||
template <typename ClassType, typename ReturnType, typename... Args>
|
||||
struct function_traits<ReturnType (ClassType::*)(Args...), false>
|
||||
: function_traits<ReturnType (ClassType::*)(Args...) const> {};
|
||||
/// Overload for non-class function types.
|
||||
template <typename ReturnType, typename... Args>
|
||||
struct function_traits<ReturnType (*)(Args...), false> {
|
||||
/// The number of arguments to this function.
|
||||
enum { num_args = sizeof...(Args) };
|
||||
|
||||
/// The result type of this function.
|
||||
using result_t = ReturnType;
|
||||
|
||||
/// The type of an argument to this function.
|
||||
template <size_t i>
|
||||
using arg_t = typename std::tuple_element<i, std::tuple<Args...>>::type;
|
||||
};
|
||||
/// Overload for non-class function type references.
|
||||
template <typename ReturnType, typename... Args>
|
||||
struct function_traits<ReturnType (&)(Args...), false>
|
||||
: public function_traits<ReturnType (*)(Args...)> {};
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Extra additions to <functional>
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
@ -73,6 +73,7 @@ add_llvm_unittest(ADTTests
|
||||
TinyPtrVectorTest.cpp
|
||||
TripleTest.cpp
|
||||
TwineTest.cpp
|
||||
TypeTraitsTest.cpp
|
||||
WaymarkingTest.cpp
|
||||
)
|
||||
|
||||
|
80
unittests/ADT/TypeTraitsTest.cpp
Normal file
80
unittests/ADT/TypeTraitsTest.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
//===- TypeTraitsTest.cpp - type_traits unit tests ------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/ADT/STLExtras.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// function_traits
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
namespace {
|
||||
/// Check a callable type of the form `bool(const int &)`.
|
||||
template <typename CallableT> struct CheckFunctionTraits {
|
||||
static_assert(
|
||||
std::is_same<typename function_traits<CallableT>::result_t, bool>::value,
|
||||
"expected result_t to be `bool`");
|
||||
static_assert(
|
||||
std::is_same<typename function_traits<CallableT>::template arg_t<0>,
|
||||
const int &>::value,
|
||||
"expected arg_t<0> to be `const int &`");
|
||||
static_assert(function_traits<CallableT>::num_args == 1,
|
||||
"expected num_args to be 1");
|
||||
};
|
||||
|
||||
/// Test function pointers.
|
||||
using FuncType = bool (*)(const int &);
|
||||
struct CheckFunctionPointer : CheckFunctionTraits<FuncType> {};
|
||||
|
||||
static bool func(const int &v);
|
||||
struct CheckFunctionPointer2 : CheckFunctionTraits<decltype(&func)> {};
|
||||
|
||||
/// Test method pointers.
|
||||
struct Foo {
|
||||
bool func(const int &v);
|
||||
};
|
||||
struct CheckMethodPointer : CheckFunctionTraits<decltype(&Foo::func)> {};
|
||||
|
||||
/// Test lambda references.
|
||||
auto lambdaFunc = [](const int &v) -> bool { return true; };
|
||||
struct CheckLambda : CheckFunctionTraits<decltype(lambdaFunc)> {};
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// is_detected
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
namespace {
|
||||
struct HasFooMethod {
|
||||
void foo() {}
|
||||
};
|
||||
struct NoFooMethod {};
|
||||
|
||||
template <class T> using has_foo_method_t = decltype(std::declval<T &>().foo());
|
||||
|
||||
static_assert(is_detected<has_foo_method_t, HasFooMethod>::value,
|
||||
"expected foo method to be detected");
|
||||
static_assert(!is_detected<has_foo_method_t, NoFooMethod>::value,
|
||||
"expected no foo method to be detected");
|
||||
} // end anonymous namespace
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// is_invocable
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
static void invocable_fn(int) {}
|
||||
|
||||
static_assert(is_invocable<decltype(invocable_fn), int>::value,
|
||||
"expected function to be invocable");
|
||||
static_assert(!is_invocable<decltype(invocable_fn), void *>::value,
|
||||
"expected function not to be invocable");
|
||||
static_assert(!is_invocable<decltype(invocable_fn), int, int>::value,
|
||||
"expected function not to be invocable");
|
Loading…
x
Reference in New Issue
Block a user