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_COMMON_REFERENCE_H
10 #define _LIBCPP___TYPE_TRAITS_COMMON_REFERENCE_H
11 
12 #include <__config>
13 #include <__type_traits/common_type.h>
14 #include <__type_traits/copy_cv.h>
15 #include <__type_traits/copy_cvref.h>
16 #include <__type_traits/is_convertible.h>
17 #include <__type_traits/is_reference.h>
18 #include <__type_traits/remove_cv.h>
19 #include <__type_traits/remove_cvref.h>
20 #include <__type_traits/remove_reference.h>
21 #include <__utility/declval.h>
22 
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 #  pragma GCC system_header
25 #endif
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 // common_reference
30 #if _LIBCPP_STD_VER >= 20
31 // Let COND_RES(X, Y) be:
32 template <class _Xp, class _Yp>
33 using __cond_res = decltype(false ? std::declval<_Xp (&)()>()() : std::declval<_Yp (&)()>()());
34 
35 // Let `XREF(A)` denote a unary alias template `T` such that `T<U>` denotes the same type as `U`
36 // with the addition of `A`'s cv and reference qualifiers, for a non-reference cv-unqualified type
37 // `U`.
38 // [Note: `XREF(A)` is `__xref<A>::template __apply`]
39 template <class _Tp>
40 struct __xref {
41   template <class _Up>
42   using __apply = __copy_cvref_t<_Tp, _Up>;
43 };
44 
45 // Given types A and B, let X be remove_reference_t<A>, let Y be remove_reference_t<B>,
46 // and let COMMON-REF(A, B) be:
47 template <class _Ap, class _Bp, class _Xp = remove_reference_t<_Ap>, class _Yp = remove_reference_t<_Bp>>
48 struct __common_ref;
49 
50 template <class _Xp, class _Yp>
51 using __common_ref_t = typename __common_ref<_Xp, _Yp>::__type;
52 
53 template <class _Xp, class _Yp>
54 using __cv_cond_res = __cond_res<__copy_cv_t<_Xp, _Yp>&, __copy_cv_t<_Yp, _Xp>&>;
55 
56 //    If A and B are both lvalue reference types, COMMON-REF(A, B) is
57 //    COND-RES(COPYCV(X, Y)&, COPYCV(Y, X)&) if that type exists and is a reference type.
58 // clang-format off
59 template <class _Ap, class _Bp, class _Xp, class _Yp>
60   requires
61     requires { typename __cv_cond_res<_Xp, _Yp>; } &&
62     is_reference_v<__cv_cond_res<_Xp, _Yp>>
63 struct __common_ref<_Ap&, _Bp&, _Xp, _Yp> {
64   using __type = __cv_cond_res<_Xp, _Yp>;
65 };
66 // clang-format on
67 
68 //    Otherwise, let C be remove_reference_t<COMMON-REF(X&, Y&)>&&. ...
69 template <class _Xp, class _Yp>
70 using __common_ref_C = remove_reference_t<__common_ref_t<_Xp&, _Yp&>>&&;
71 
72 //    .... If A and B are both rvalue reference types, C is well-formed, and
73 //    is_convertible_v<A, C> && is_convertible_v<B, C> is true, then COMMON-REF(A, B) is C.
74 // clang-format off
75 template <class _Ap, class _Bp, class _Xp, class _Yp>
76   requires
77     requires { typename __common_ref_C<_Xp, _Yp>; } &&
78     is_convertible_v<_Ap&&, __common_ref_C<_Xp, _Yp>> &&
79     is_convertible_v<_Bp&&, __common_ref_C<_Xp, _Yp>>
80 struct __common_ref<_Ap&&, _Bp&&, _Xp, _Yp> {
81   using __type = __common_ref_C<_Xp, _Yp>;
82 };
83 // clang-format on
84 
85 //    Otherwise, let D be COMMON-REF(const X&, Y&). ...
86 template <class _Tp, class _Up>
87 using __common_ref_D = __common_ref_t<const _Tp&, _Up&>;
88 
89 //    ... If A is an rvalue reference and B is an lvalue reference and D is well-formed and
90 //    is_convertible_v<A, D> is true, then COMMON-REF(A, B) is D.
91 // clang-format off
92 template <class _Ap, class _Bp, class _Xp, class _Yp>
93   requires
94     requires { typename __common_ref_D<_Xp, _Yp>; } &&
95     is_convertible_v<_Ap&&, __common_ref_D<_Xp, _Yp>>
96 struct __common_ref<_Ap&&, _Bp&, _Xp, _Yp> {
97   using __type = __common_ref_D<_Xp, _Yp>;
98 };
99 // clang-format on
100 
101 //    Otherwise, if A is an lvalue reference and B is an rvalue reference, then
102 //    COMMON-REF(A, B) is COMMON-REF(B, A).
103 template <class _Ap, class _Bp, class _Xp, class _Yp>
104 struct __common_ref<_Ap&, _Bp&&, _Xp, _Yp> : __common_ref<_Bp&&, _Ap&> {};
105 
106 //    Otherwise, COMMON-REF(A, B) is ill-formed.
107 template <class _Ap, class _Bp, class _Xp, class _Yp>
108 struct __common_ref {};
109 
110 // Note C: For the common_reference trait applied to a parameter pack [...]
111 
112 template <class...>
113 struct common_reference;
114 
115 template <class... _Types>
116 using common_reference_t = typename common_reference<_Types...>::type;
117 
118 // bullet 1 - sizeof...(T) == 0
119 template <>
120 struct common_reference<> {};
121 
122 // bullet 2 - sizeof...(T) == 1
123 template <class _Tp>
124 struct common_reference<_Tp> {
125   using type = _Tp;
126 };
127 
128 // bullet 3 - sizeof...(T) == 2
129 template <class _Tp, class _Up>
130 struct __common_reference_sub_bullet3;
131 template <class _Tp, class _Up>
132 struct __common_reference_sub_bullet2 : __common_reference_sub_bullet3<_Tp, _Up> {};
133 template <class _Tp, class _Up>
134 struct __common_reference_sub_bullet1 : __common_reference_sub_bullet2<_Tp, _Up> {};
135 
136 // sub-bullet 1 - If T1 and T2 are reference types and COMMON-REF(T1, T2) is well-formed, then
137 // the member typedef `type` denotes that type.
138 template <class _Tp, class _Up>
139 struct common_reference<_Tp, _Up> : __common_reference_sub_bullet1<_Tp, _Up> {};
140 
141 template <class _Tp, class _Up>
142   requires is_reference_v<_Tp> && is_reference_v<_Up> && requires { typename __common_ref_t<_Tp, _Up>; }
143 struct __common_reference_sub_bullet1<_Tp, _Up> {
144   using type = __common_ref_t<_Tp, _Up>;
145 };
146 
147 // sub-bullet 2 - Otherwise, if basic_common_reference<remove_cvref_t<T1>, remove_cvref_t<T2>, XREF(T1), XREF(T2)>::type
148 // is well-formed, then the member typedef `type` denotes that type.
149 template <class, class, template <class> class, template <class> class>
150 struct basic_common_reference {};
151 
152 template <class _Tp, class _Up>
153 using __basic_common_reference_t =
154     typename basic_common_reference<remove_cvref_t<_Tp>,
155                                     remove_cvref_t<_Up>,
156                                     __xref<_Tp>::template __apply,
157                                     __xref<_Up>::template __apply>::type;
158 
159 template <class _Tp, class _Up>
160   requires requires { typename __basic_common_reference_t<_Tp, _Up>; }
161 struct __common_reference_sub_bullet2<_Tp, _Up> {
162   using type = __basic_common_reference_t<_Tp, _Up>;
163 };
164 
165 // sub-bullet 3 - Otherwise, if COND-RES(T1, T2) is well-formed,
166 // then the member typedef `type` denotes that type.
167 template <class _Tp, class _Up>
168   requires requires { typename __cond_res<_Tp, _Up>; }
169 struct __common_reference_sub_bullet3<_Tp, _Up> {
170   using type = __cond_res<_Tp, _Up>;
171 };
172 
173 // sub-bullet 4 & 5 - Otherwise, if common_type_t<T1, T2> is well-formed,
174 //                    then the member typedef `type` denotes that type.
175 //                  - Otherwise, there shall be no member `type`.
176 template <class _Tp, class _Up>
177 struct __common_reference_sub_bullet3 : common_type<_Tp, _Up> {};
178 
179 // bullet 4 - If there is such a type `C`, the member typedef type shall denote the same type, if
180 //            any, as `common_reference_t<C, Rest...>`.
181 template <class _Tp, class _Up, class _Vp, class... _Rest>
182   requires requires { typename common_reference_t<_Tp, _Up>; }
183 struct common_reference<_Tp, _Up, _Vp, _Rest...> : common_reference<common_reference_t<_Tp, _Up>, _Vp, _Rest...> {};
184 
185 // bullet 5 - Otherwise, there shall be no member `type`.
186 template <class...>
187 struct common_reference {};
188 
189 #endif // _LIBCPP_STD_VER >= 20
190 
191 _LIBCPP_END_NAMESPACE_STD
192 
193 #endif // _LIBCPP___TYPE_TRAITS_COMMON_REFERENCE_H
194