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) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK)
28 
29 template <class _T1, class _T2>
30 struct _LIBCPP_TEMPLATE_VIS is_convertible : public integral_constant<bool, __is_convertible(_T1, _T2)> {};
31 
32 #elif __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK)
33 
34 template <class _T1, class _T2>
35 struct _LIBCPP_TEMPLATE_VIS is_convertible : public integral_constant<bool, __is_convertible_to(_T1, _T2)> {};
36 
37 // TODO: Remove this fallback when GCC < 13 support is no longer required.
38 // GCC 13 has the __is_convertible built-in.
39 #else // __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK)
40 
41 namespace __is_convertible_imp {
42 template <class _Tp>
43 void __test_convert(_Tp);
44 
45 template <class _From, class _To, class = void>
46 struct __is_convertible_test : public false_type {};
47 
48 template <class _From, class _To>
49 struct __is_convertible_test<_From, _To, decltype(__is_convertible_imp::__test_convert<_To>(std::declval<_From>()))>
50     : public true_type {};
51 
52 // clang-format off
53 template <class _Tp,
54           bool _IsArray    = is_array<_Tp>::value,
55           bool _IsFunction = is_function<_Tp>::value,
56           bool _IsVoid     = is_void<_Tp>::value>
57                      struct __is_array_function_or_void                          { enum { value = 0 }; };
58 template <class _Tp> struct __is_array_function_or_void<_Tp, true, false, false> { enum { value = 1 }; };
59 template <class _Tp> struct __is_array_function_or_void<_Tp, false, true, false> { enum { value = 2 }; };
60 template <class _Tp> struct __is_array_function_or_void<_Tp, false, false, true> { enum { value = 3 }; };
61 // clang-format on
62 } // namespace __is_convertible_imp
63 
64 template <class _Tp,
65           unsigned = __is_convertible_imp::__is_array_function_or_void<__libcpp_remove_reference_t<_Tp> >::value>
66 struct __is_convertible_check {
67   static const size_t __v = 0;
68 };
69 
70 template <class _Tp>
71 struct __is_convertible_check<_Tp, 0> {
72   static const size_t __v = sizeof(_Tp);
73 };
74 
75 template <class _T1,
76           class _T2,
77           unsigned _T1_is_array_function_or_void = __is_convertible_imp::__is_array_function_or_void<_T1>::value,
78           unsigned _T2_is_array_function_or_void = __is_convertible_imp::__is_array_function_or_void<_T2>::value>
79 struct __is_convertible
80     : public integral_constant<bool, __is_convertible_imp::__is_convertible_test<_T1, _T2>::value >{};
81 
82 // clang-format off
83 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 1> : public false_type{};
84 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 1> : public false_type{};
85 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 1> : public false_type{};
86 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 1> : public false_type{};
87 
88 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 2> : public false_type{};
89 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 2> : public false_type{};
90 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 2> : public false_type{};
91 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 2> : public false_type{};
92 
93 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 3> : public false_type{};
94 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 3> : public false_type{};
95 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 3> : public false_type{};
96 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 3> : public true_type{};
97 // clang-format on
98 
99 template <class _T1, class _T2>
100 struct _LIBCPP_TEMPLATE_VIS is_convertible : public __is_convertible<_T1, _T2> {
101   static const size_t __complete_check1 = __is_convertible_check<_T1>::__v;
102   static const size_t __complete_check2 = __is_convertible_check<_T2>::__v;
103 };
104 
105 #endif // __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK)
106 
107 #if _LIBCPP_STD_VER >= 17
108 template <class _From, class _To>
109 inline constexpr bool is_convertible_v = is_convertible<_From, _To>::value;
110 #endif
111 
112 _LIBCPP_END_NAMESPACE_STD
113 
114 #endif // _LIBCPP___TYPE_TRAITS_IS_CONVERTIBLE_H
115