LLVM  8.0.0svn
type_traits.h
Go to the documentation of this file.
1 //===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides useful additions to the standard type_traits library.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_TYPE_TRAITS_H
15 #define LLVM_SUPPORT_TYPE_TRAITS_H
16 
17 #include "llvm/Support/Compiler.h"
18 #include <type_traits>
19 #include <utility>
20 
21 #ifndef __has_feature
22 #define LLVM_DEFINED_HAS_FEATURE
23 #define __has_feature(x) 0
24 #endif
25 
26 namespace llvm {
27 
28 /// isPodLike - This is a type trait that is used to determine whether a given
29 /// type can be copied around with memcpy instead of running ctors etc.
30 template <typename T>
31 struct isPodLike {
32  // std::is_trivially_copyable is available in libc++ with clang, libstdc++
33  // that comes with GCC 5. MSVC 2015 and newer also have
34  // std::is_trivially_copyable.
35 #if (__has_feature(is_trivially_copyable) && defined(_LIBCPP_VERSION)) || \
36  (defined(__GNUC__) && __GNUC__ >= 5) || defined(_MSC_VER)
37  // If the compiler supports the is_trivially_copyable trait use it, as it
38  // matches the definition of isPodLike closely.
39  static const bool value = std::is_trivially_copyable<T>::value;
40 #elif __has_feature(is_trivially_copyable)
41  // Use the internal name if the compiler supports is_trivially_copyable but we
42  // don't know if the standard library does. This is the case for clang in
43  // conjunction with libstdc++ from GCC 4.x.
44  static const bool value = __is_trivially_copyable(T);
45 #else
46  // If we don't know anything else, we can (at least) assume that all non-class
47  // types are PODs.
48  static const bool value = !std::is_class<T>::value;
49 #endif
50 };
51 
52 // std::pair's are pod-like if their elements are.
53 template<typename T, typename U>
54 struct isPodLike<std::pair<T, U>> {
56 };
57 
58 /// Metafunction that determines whether the given type is either an
59 /// integral type or an enumeration type, including enum classes.
60 ///
61 /// Note that this accepts potentially more integral types than is_integral
62 /// because it is based on being implicitly convertible to an integral type.
63 /// Also note that enum classes aren't implicitly convertible to integral types,
64 /// the value may therefore need to be explicitly converted before being used.
65 template <typename T> class is_integral_or_enum {
66  using UnderlyingT = typename std::remove_reference<T>::type;
67 
68 public:
69  static const bool value =
70  !std::is_class<UnderlyingT>::value && // Filter conversion operators.
71  !std::is_pointer<UnderlyingT>::value &&
72  !std::is_floating_point<UnderlyingT>::value &&
73  (std::is_enum<UnderlyingT>::value ||
74  std::is_convertible<UnderlyingT, unsigned long long>::value);
75 };
76 
77 /// If T is a pointer, just return it. If it is not, return T&.
78 template<typename T, typename Enable = void>
80 
81 template <typename T>
83  T, typename std::enable_if<std::is_pointer<T>::value>::type> {
84  using type = T;
85 };
86 
87 /// If T is a pointer to X, return a pointer to const X. If it is not,
88 /// return const T.
89 template<typename T, typename Enable = void>
90 struct add_const_past_pointer { using type = const T; };
91 
92 template <typename T>
94  T, typename std::enable_if<std::is_pointer<T>::value>::type> {
95  using type = const typename std::remove_pointer<T>::type *;
96 };
97 
98 template <typename T, typename Enable = void>
100  using type = const T &;
101 };
102 template <typename T>
104  T, typename std::enable_if<std::is_pointer<T>::value>::type> {
106 };
107 
108 namespace detail {
109 /// Internal utility to detect trivial copy construction.
110 template<typename T> union copy_construction_triviality_helper {
111  T t;
115 };
116 /// Internal utility to detect trivial move construction.
117 template<typename T> union move_construction_triviality_helper {
118  T t;
122 };
123 } // end namespace detail
124 
125 /// An implementation of `std::is_trivially_copy_constructible` since we have
126 /// users with STLs that don't yet include it.
127 template <typename T>
129  : std::is_copy_constructible<
130  ::llvm::detail::copy_construction_triviality_helper<T>> {};
131 template <typename T>
132 struct is_trivially_copy_constructible<T &> : std::true_type {};
133 template <typename T>
134 struct is_trivially_copy_constructible<T &&> : std::false_type {};
135 
136 /// An implementation of `std::is_trivially_move_constructible` since we have
137 /// users with STLs that don't yet include it.
138 template <typename T>
140  : std::is_move_constructible<
141  ::llvm::detail::move_construction_triviality_helper<T>> {};
142 template <typename T>
143 struct is_trivially_move_constructible<T &> : std::true_type {};
144 template <typename T>
145 struct is_trivially_move_constructible<T &&> : std::true_type {};
146 
147 } // end namespace llvm
148 
149 // If the compiler supports detecting whether a class is final, define
150 // an LLVM_IS_FINAL macro. If it cannot be defined properly, this
151 // macro will be left undefined.
152 #if __cplusplus >= 201402L || defined(_MSC_VER)
153 #define LLVM_IS_FINAL(Ty) std::is_final<Ty>()
154 #elif __has_feature(is_final) || LLVM_GNUC_PREREQ(4, 7, 0)
155 #define LLVM_IS_FINAL(Ty) __is_final(Ty)
156 #endif
157 
158 #ifdef LLVM_DEFINED_HAS_FEATURE
159 #undef __has_feature
160 #endif
161 
162 #endif // LLVM_SUPPORT_TYPE_TRAITS_H
This class represents lattice values for constants.
Definition: AllocatorList.h:24
static const bool value
Definition: type_traits.h:48
Definition: BitVector.h:938
Metafunction that determines whether the given type is either an integral type or an enumeration type...
Definition: type_traits.h:65
#define T
An implementation of std::is_trivially_move_constructible since we have users with STLs that don&#39;t ye...
Definition: type_traits.h:139
An implementation of std::is_trivially_copy_constructible since we have users with STLs that don&#39;t ye...
Definition: type_traits.h:128
If T is a pointer to X, return a pointer to const X.
Definition: type_traits.h:90
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:530
Internal utility to detect trivial copy construction.
Definition: type_traits.h:110
If T is a pointer, just return it. If it is not, return T&.
Definition: type_traits.h:79
Internal utility to detect trivial move construction.
Definition: type_traits.h:117