1 //===----------------------------------------------------------------------===//
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 _LIBCPP___TYPE_TRAITS_IS_CONVERTIBLE_H
10 #define _LIBCPP___TYPE_TRAITS_IS_CONVERTIBLE_H
11 
12 #include <__config>
13 #include <__type_traits/integral_constant.h>
14 #include <__type_traits/is_array.h>
15 #include <__type_traits/is_function.h>
16 #include <__type_traits/is_void.h>
17 #include <__type_traits/remove_reference.h>
18 #include <__utility/declval.h>
19 #include <cstddef>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 #if __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK)
28 
29 template <class _T1, class _T2> struct _LIBCPP_TEMPLATE_VIS is_convertible
30     : public integral_constant<bool, __is_convertible_to(_T1, _T2)> {};
31 
32 #else  // __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK)
33 
34 namespace __is_convertible_imp
35 {
36 template <class _Tp> void  __test_convert(_Tp);
37 
38 template <class _From, class _To, class = void>
39 struct __is_convertible_test : public false_type {};
40 
41 template <class _From, class _To>
42 struct __is_convertible_test<_From, _To,
43     decltype(__is_convertible_imp::__test_convert<_To>(declval<_From>()))> : public true_type
44 {};
45 
46 template <class _Tp, bool _IsArray =    is_array<_Tp>::value,
47                      bool _IsFunction = is_function<_Tp>::value,
48                      bool _IsVoid =     is_void<_Tp>::value>
49                      struct __is_array_function_or_void                          {enum {value = 0};};
50 template <class _Tp> struct __is_array_function_or_void<_Tp, true, false, false> {enum {value = 1};};
51 template <class _Tp> struct __is_array_function_or_void<_Tp, false, true, false> {enum {value = 2};};
52 template <class _Tp> struct __is_array_function_or_void<_Tp, false, false, true> {enum {value = 3};};
53 }
54 
55 template <class _Tp,
56     unsigned = __is_convertible_imp::__is_array_function_or_void<typename remove_reference<_Tp>::type>::value>
57 struct __is_convertible_check
58 {
59     static const size_t __v = 0;
60 };
61 
62 template <class _Tp>
63 struct __is_convertible_check<_Tp, 0>
64 {
65     static const size_t __v = sizeof(_Tp);
66 };
67 
68 template <class _T1, class _T2,
69     unsigned _T1_is_array_function_or_void = __is_convertible_imp::__is_array_function_or_void<_T1>::value,
70     unsigned _T2_is_array_function_or_void = __is_convertible_imp::__is_array_function_or_void<_T2>::value>
71 struct __is_convertible
72     : public integral_constant<bool,
73         __is_convertible_imp::__is_convertible_test<_T1, _T2>::value
74     >
75 {};
76 
77 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 1> : public false_type {};
78 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 1> : public false_type {};
79 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 1> : public false_type {};
80 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 1> : public false_type {};
81 
82 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 2> : public false_type {};
83 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 2> : public false_type {};
84 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 2> : public false_type {};
85 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 2> : public false_type {};
86 
87 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 3> : public false_type {};
88 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 3> : public false_type {};
89 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 3> : public false_type {};
90 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 3> : public true_type {};
91 
92 template <class _T1, class _T2> struct _LIBCPP_TEMPLATE_VIS is_convertible
93     : public __is_convertible<_T1, _T2>
94 {
95     static const size_t __complete_check1 = __is_convertible_check<_T1>::__v;
96     static const size_t __complete_check2 = __is_convertible_check<_T2>::__v;
97 };
98 
99 #endif // __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK)
100 
101 #if _LIBCPP_STD_VER > 14
102 template <class _From, class _To>
103 inline constexpr bool is_convertible_v = is_convertible<_From, _To>::value;
104 #endif
105 
106 _LIBCPP_END_NAMESPACE_STD
107 
108 #endif // _LIBCPP___TYPE_TRAITS_IS_CONVERTIBLE_H
109