//===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===// // // 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 // //===----------------------------------------------------------------------===// // // This file provides useful additions to the standard type_traits library. // //===----------------------------------------------------------------------===// #ifndef LLVM_SUPPORT_TYPE_TRAITS_H #define LLVM_SUPPORT_TYPE_TRAITS_H #include "llvm/Support/Compiler.h" #include #include namespace llvm { /// Metafunction that determines whether the given type is either an /// integral type or an enumeration type, including enum classes. /// /// Note that this accepts potentially more integral types than is_integral /// because it is based on being implicitly convertible to an integral type. /// Also note that enum classes aren't implicitly convertible to integral types, /// the value may therefore need to be explicitly converted before being used. template class is_integral_or_enum { using UnderlyingT = typename std::remove_reference::type; public: static const bool value = !std::is_class::value && // Filter conversion operators. !std::is_pointer::value && !std::is_floating_point::value && (std::is_enum::value || std::is_convertible::value); }; /// If T is a pointer, just return it. If it is not, return T&. template struct add_lvalue_reference_if_not_pointer { using type = T &; }; template struct add_lvalue_reference_if_not_pointer< T, typename std::enable_if::value>::type> { using type = T; }; /// If T is a pointer to X, return a pointer to const X. If it is not, /// return const T. template struct add_const_past_pointer { using type = const T; }; template struct add_const_past_pointer< T, typename std::enable_if::value>::type> { using type = const typename std::remove_pointer::type *; }; template struct const_pointer_or_const_ref { using type = const T &; }; template struct const_pointer_or_const_ref< T, typename std::enable_if::value>::type> { using type = typename add_const_past_pointer::type; }; namespace detail { /// Internal utility to detect trivial copy construction. template union copy_construction_triviality_helper { T t; copy_construction_triviality_helper() = default; copy_construction_triviality_helper(const copy_construction_triviality_helper&) = default; ~copy_construction_triviality_helper() = default; }; /// Internal utility to detect trivial move construction. template union move_construction_triviality_helper { T t; move_construction_triviality_helper() = default; move_construction_triviality_helper(move_construction_triviality_helper&&) = default; ~move_construction_triviality_helper() = default; }; template union trivial_helper { T t; }; } // end namespace detail /// An implementation of `std::is_trivially_copy_constructible` since we have /// users with STLs that don't yet include it. template struct is_trivially_copy_constructible : std::is_copy_constructible< ::llvm::detail::copy_construction_triviality_helper> {}; template struct is_trivially_copy_constructible : std::true_type {}; template struct is_trivially_copy_constructible : std::false_type {}; /// An implementation of `std::is_trivially_move_constructible` since we have /// users with STLs that don't yet include it. template struct is_trivially_move_constructible : std::is_move_constructible< ::llvm::detail::move_construction_triviality_helper> {}; template struct is_trivially_move_constructible : std::true_type {}; template struct is_trivially_move_constructible : std::true_type {}; template struct is_copy_assignable { template static auto get(F*) -> decltype(std::declval() = std::declval(), std::true_type{}); static std::false_type get(...); static constexpr bool value = decltype(get((T*)nullptr))::value; }; template struct is_move_assignable { template static auto get(F*) -> decltype(std::declval() = std::declval(), std::true_type{}); static std::false_type get(...); static constexpr bool value = decltype(get((T*)nullptr))::value; }; // An implementation of `std::is_trivially_copyable` since STL version // is not equally supported by all compilers, especially GCC 4.9. // Uniform implementation of this trait is important for ABI compatibility // as it has an impact on SmallVector's ABI (among others). template class is_trivially_copyable { // copy constructors static constexpr bool has_trivial_copy_constructor = std::is_copy_constructible>::value; static constexpr bool has_deleted_copy_constructor = !std::is_copy_constructible::value; // move constructors static constexpr bool has_trivial_move_constructor = std::is_move_constructible>::value; static constexpr bool has_deleted_move_constructor = !std::is_move_constructible::value; // copy assign static constexpr bool has_trivial_copy_assign = is_copy_assignable>::value; static constexpr bool has_deleted_copy_assign = !is_copy_assignable::value; // move assign static constexpr bool has_trivial_move_assign = is_move_assignable>::value; static constexpr bool has_deleted_move_assign = !is_move_assignable::value; // destructor static constexpr bool has_trivial_destructor = std::is_destructible>::value; public: static constexpr bool value = has_trivial_destructor && (has_deleted_move_assign || has_trivial_move_assign) && (has_deleted_move_constructor || has_trivial_move_constructor) && (has_deleted_copy_assign || has_trivial_copy_assign) && (has_deleted_copy_constructor || has_trivial_copy_constructor); #ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE static_assert(value == std::is_trivially_copyable::value, "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable"); #endif }; template class is_trivially_copyable : public std::true_type { }; } // end namespace llvm #endif // LLVM_SUPPORT_TYPE_TRAITS_H