1 //===-- Self contained C++ type traits --------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_UTILS_CPP_TYPETRAITS_H
10 #define LLVM_LIBC_UTILS_CPP_TYPETRAITS_H
11 
12 namespace __llvm_libc {
13 namespace cpp {
14 
15 template <bool B, typename T> struct EnableIf;
16 template <typename T> struct EnableIf<true, T> { typedef T Type; };
17 
18 template <bool B, typename T>
19 using EnableIfType = typename EnableIf<B, T>::Type;
20 
21 struct TrueValue {
22   static constexpr bool Value = true;
23 };
24 
25 struct FalseValue {
26   static constexpr bool Value = false;
27 };
28 
29 template <typename T> struct TypeIdentity { typedef T Type; };
30 
31 template <typename T1, typename T2> struct IsSame : public FalseValue {};
32 template <typename T> struct IsSame<T, T> : public TrueValue {};
33 template <typename T1, typename T2>
34 static constexpr bool IsSameV = IsSame<T1, T2>::Value;
35 
36 template <typename T> struct RemoveCV : public TypeIdentity<T> {};
37 template <typename T> struct RemoveCV<const T> : public TypeIdentity<T> {};
38 template <typename T> struct RemoveCV<volatile T> : public TypeIdentity<T> {};
39 template <typename T>
40 struct RemoveCV<const volatile T> : public TypeIdentity<T> {};
41 
42 template <typename T> using RemoveCVType = typename RemoveCV<T>::Type;
43 
44 template <typename Type> struct IsIntegral {
45   using TypeNoCV = RemoveCVType<Type>;
46   static constexpr bool Value =
47       IsSameV<char, TypeNoCV> || IsSameV<signed char, TypeNoCV> ||
48       IsSameV<unsigned char, TypeNoCV> || IsSameV<short, TypeNoCV> ||
49       IsSameV<unsigned short, TypeNoCV> || IsSameV<int, TypeNoCV> ||
50       IsSameV<unsigned int, TypeNoCV> || IsSameV<long, TypeNoCV> ||
51       IsSameV<unsigned long, TypeNoCV> || IsSameV<long long, TypeNoCV> ||
52       IsSameV<unsigned long long, TypeNoCV> || IsSameV<bool, TypeNoCV> ||
53       IsSameV<__uint128_t, TypeNoCV> || IsSameV<__int128_t, TypeNoCV>;
54 };
55 
56 template <typename T> struct IsPointerTypeNoCV : public FalseValue {};
57 template <typename T> struct IsPointerTypeNoCV<T *> : public TrueValue {};
58 template <typename T> struct IsPointerType {
59   static constexpr bool Value = IsPointerTypeNoCV<RemoveCVType<T>>::Value;
60 };
61 
62 template <typename Type> struct IsFloatingPointType {
63   using TypeNoCV = RemoveCVType<Type>;
64   static constexpr bool Value = IsSame<float, TypeNoCV>::Value ||
65                                 IsSame<double, TypeNoCV>::Value ||
66                                 IsSame<long double, TypeNoCV>::Value;
67 };
68 
69 template <typename Type> struct IsArithmetic {
70   static constexpr bool Value =
71       IsIntegral<Type>::Value || IsFloatingPointType<Type>::Value;
72 };
73 
74 } // namespace cpp
75 } // namespace __llvm_libc
76 
77 #endif // LLVM_LIBC_UTILS_CPP_TYPETRAITS_H
78