1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___FORMAT_FORMAT_ARG_STORE_H
11 #define _LIBCPP___FORMAT_FORMAT_ARG_STORE_H
12
13 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
14 # pragma GCC system_header
15 #endif
16
17 #include <__concepts/arithmetic.h>
18 #include <__concepts/same_as.h>
19 #include <__config>
20 #include <__format/concepts.h>
21 #include <__format/format_arg.h>
22 #include <__type_traits/conditional.h>
23 #include <__type_traits/extent.h>
24 #include <__type_traits/remove_const.h>
25 #include <string>
26 #include <string_view>
27
28 _LIBCPP_BEGIN_NAMESPACE_STD
29
30 #if _LIBCPP_STD_VER >= 20
31
32 namespace __format {
33
34 /// \returns The @c __arg_t based on the type of the formatting argument.
35 ///
36 /// \pre \c __formattable<_Tp, typename _Context::char_type>
37 template <class _Context, class _Tp>
38 consteval __arg_t __determine_arg_t();
39
40 // Boolean
41 template <class, same_as<bool> _Tp>
__determine_arg_t()42 consteval __arg_t __determine_arg_t() {
43 return __arg_t::__boolean;
44 }
45
46 // Char
47 template <class _Context, same_as<typename _Context::char_type> _Tp>
__determine_arg_t()48 consteval __arg_t __determine_arg_t() {
49 return __arg_t::__char_type;
50 }
51 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
52 template <class _Context, class _CharT>
requires(same_as<typename _Context::char_type,wchar_t> && same_as<_CharT,char>)53 requires(same_as<typename _Context::char_type, wchar_t> && same_as<_CharT, char>)
54 consteval __arg_t __determine_arg_t() {
55 return __arg_t::__char_type;
56 }
57 # endif
58
59 // Signed integers
60 template <class, __libcpp_signed_integer _Tp>
__determine_arg_t()61 consteval __arg_t __determine_arg_t() {
62 if constexpr (sizeof(_Tp) <= sizeof(int))
63 return __arg_t::__int;
64 else if constexpr (sizeof(_Tp) <= sizeof(long long))
65 return __arg_t::__long_long;
66 # ifndef _LIBCPP_HAS_NO_INT128
67 else if constexpr (sizeof(_Tp) == sizeof(__int128_t))
68 return __arg_t::__i128;
69 # endif
70 else
71 static_assert(sizeof(_Tp) == 0, "an unsupported signed integer was used");
72 }
73
74 // Unsigned integers
75 template <class, __libcpp_unsigned_integer _Tp>
__determine_arg_t()76 consteval __arg_t __determine_arg_t() {
77 if constexpr (sizeof(_Tp) <= sizeof(unsigned))
78 return __arg_t::__unsigned;
79 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
80 return __arg_t::__unsigned_long_long;
81 # ifndef _LIBCPP_HAS_NO_INT128
82 else if constexpr (sizeof(_Tp) == sizeof(__uint128_t))
83 return __arg_t::__u128;
84 # endif
85 else
86 static_assert(sizeof(_Tp) == 0, "an unsupported unsigned integer was used");
87 }
88
89 // Floating-point
90 template <class, same_as<float> _Tp>
__determine_arg_t()91 consteval __arg_t __determine_arg_t() {
92 return __arg_t::__float;
93 }
94 template <class, same_as<double> _Tp>
__determine_arg_t()95 consteval __arg_t __determine_arg_t() {
96 return __arg_t::__double;
97 }
98 template <class, same_as<long double> _Tp>
__determine_arg_t()99 consteval __arg_t __determine_arg_t() {
100 return __arg_t::__long_double;
101 }
102
103 // Char pointer
104 template <class _Context, class _Tp>
105 requires(same_as<typename _Context::char_type*, _Tp> || same_as<const typename _Context::char_type*, _Tp>)
__determine_arg_t()106 consteval __arg_t __determine_arg_t() {
107 return __arg_t::__const_char_type_ptr;
108 }
109
110 // Char array
111 template <class _Context, class _Tp>
requires(is_array_v<_Tp> && same_as<_Tp,typename _Context::char_type[extent_v<_Tp>]>)112 requires(is_array_v<_Tp> && same_as<_Tp, typename _Context::char_type[extent_v<_Tp>]>)
113 consteval __arg_t __determine_arg_t() {
114 return __arg_t::__string_view;
115 }
116
117 // String view
118 template <class _Context, class _Tp>
requires(same_as<typename _Context::char_type,typename _Tp::value_type> && same_as<_Tp,basic_string_view<typename _Tp::value_type,typename _Tp::traits_type>>)119 requires(same_as<typename _Context::char_type, typename _Tp::value_type> &&
120 same_as<_Tp, basic_string_view<typename _Tp::value_type, typename _Tp::traits_type>>)
121 consteval __arg_t __determine_arg_t() {
122 return __arg_t::__string_view;
123 }
124
125 // String
126 template <class _Context, class _Tp>
requires(same_as<typename _Context::char_type,typename _Tp::value_type> && same_as<_Tp,basic_string<typename _Tp::value_type,typename _Tp::traits_type,typename _Tp::allocator_type>>)127 requires(
128 same_as<typename _Context::char_type, typename _Tp::value_type> &&
129 same_as<_Tp, basic_string<typename _Tp::value_type, typename _Tp::traits_type, typename _Tp::allocator_type>>)
130 consteval __arg_t __determine_arg_t() {
131 return __arg_t::__string_view;
132 }
133
134 // Pointers
135 template <class, class _Ptr>
136 requires(same_as<_Ptr, void*> || same_as<_Ptr, const void*> || same_as<_Ptr, nullptr_t>)
__determine_arg_t()137 consteval __arg_t __determine_arg_t() {
138 return __arg_t::__ptr;
139 }
140
141 // Handle
142 //
143 // Note this version can't be constrained avoiding ambiguous overloads.
144 // That means it can be instantiated by disabled formatters. To solve this, a
145 // constrained version for not formattable formatters is added.
146 template <class _Context, class _Tp>
__determine_arg_t()147 consteval __arg_t __determine_arg_t() {
148 return __arg_t::__handle;
149 }
150
151 // The overload for not formattable types allows triggering the static
152 // assertion below.
153 template <class _Context, class _Tp>
154 requires(!__formattable_with<_Tp, _Context>)
__determine_arg_t()155 consteval __arg_t __determine_arg_t() {
156 return __arg_t::__none;
157 }
158
159 // Pseudo constuctor for basic_format_arg
160 //
161 // Modeled after template<class T> explicit basic_format_arg(T& v) noexcept;
162 // [format.arg]/4-6
163 template <class _Context, class _Tp>
__create_format_arg(_Tp & __value)164 _LIBCPP_HIDE_FROM_ABI basic_format_arg<_Context> __create_format_arg(_Tp& __value) noexcept {
165 using _Dp = remove_const_t<_Tp>;
166 constexpr __arg_t __arg = __determine_arg_t<_Context, _Dp>();
167 static_assert(__arg != __arg_t::__none, "the supplied type is not formattable");
168 static_assert(__formattable_with<_Tp, _Context>);
169
170 // Not all types can be used to directly initialize the
171 // __basic_format_arg_value. First handle all types needing adjustment, the
172 // final else requires no adjustment.
173 if constexpr (__arg == __arg_t::__char_type)
174
175 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
176 if constexpr (same_as<typename _Context::char_type, wchar_t> && same_as<_Dp, char>)
177 return basic_format_arg<_Context>{__arg, static_cast<wchar_t>(static_cast<unsigned char>(__value))};
178 else
179 # endif
180 return basic_format_arg<_Context>{__arg, __value};
181 else if constexpr (__arg == __arg_t::__int)
182 return basic_format_arg<_Context>{__arg, static_cast<int>(__value)};
183 else if constexpr (__arg == __arg_t::__long_long)
184 return basic_format_arg<_Context>{__arg, static_cast<long long>(__value)};
185 else if constexpr (__arg == __arg_t::__unsigned)
186 return basic_format_arg<_Context>{__arg, static_cast<unsigned>(__value)};
187 else if constexpr (__arg == __arg_t::__unsigned_long_long)
188 return basic_format_arg<_Context>{__arg, static_cast<unsigned long long>(__value)};
189 else if constexpr (__arg == __arg_t::__string_view)
190 // Using std::size on a character array will add the NUL-terminator to the size.
191 if constexpr (is_array_v<_Dp>)
192 return basic_format_arg<_Context>{
193 __arg, basic_string_view<typename _Context::char_type>{__value, extent_v<_Dp> - 1}};
194 else
195 // When the _Traits or _Allocator are different an implicit conversion will
196 // fail.
197 return basic_format_arg<_Context>{
198 __arg, basic_string_view<typename _Context::char_type>{__value.data(), __value.size()}};
199 else if constexpr (__arg == __arg_t::__ptr)
200 return basic_format_arg<_Context>{__arg, static_cast<const void*>(__value)};
201 else if constexpr (__arg == __arg_t::__handle)
202 return basic_format_arg<_Context>{__arg, typename __basic_format_arg_value<_Context>::__handle{__value}};
203 else
204 return basic_format_arg<_Context>{__arg, __value};
205 }
206
207 template <class _Context, class... _Args>
208 _LIBCPP_HIDE_FROM_ABI void
__create_packed_storage(uint64_t & __types,__basic_format_arg_value<_Context> * __values,_Args &...__args)209 __create_packed_storage(uint64_t& __types, __basic_format_arg_value<_Context>* __values, _Args&... __args) noexcept {
210 int __shift = 0;
211 (
212 [&] {
213 basic_format_arg<_Context> __arg = __format::__create_format_arg<_Context>(__args);
214 if (__shift != 0)
215 __types |= static_cast<uint64_t>(__arg.__type_) << __shift;
216 else
217 // Assigns the initial value.
218 __types = static_cast<uint64_t>(__arg.__type_);
219 __shift += __packed_arg_t_bits;
220 *__values++ = __arg.__value_;
221 }(),
222 ...);
223 }
224
225 template <class _Context, class... _Args>
__store_basic_format_arg(basic_format_arg<_Context> * __data,_Args &...__args)226 _LIBCPP_HIDE_FROM_ABI void __store_basic_format_arg(basic_format_arg<_Context>* __data, _Args&... __args) noexcept {
227 ([&] { *__data++ = __format::__create_format_arg<_Context>(__args); }(), ...);
228 }
229
230 template <class _Context, size_t _Np>
231 struct __packed_format_arg_store {
232 __basic_format_arg_value<_Context> __values_[_Np];
233 uint64_t __types_ = 0;
234 };
235
236 template <class _Context, size_t _Np>
237 struct __unpacked_format_arg_store {
238 basic_format_arg<_Context> __args_[_Np];
239 };
240
241 } // namespace __format
242
243 template <class _Context, class... _Args>
244 struct _LIBCPP_TEMPLATE_VIS __format_arg_store {
__format_arg_store__format_arg_store245 _LIBCPP_HIDE_FROM_ABI __format_arg_store(_Args&... __args) noexcept {
246 if constexpr (sizeof...(_Args) != 0) {
247 if constexpr (__format::__use_packed_format_arg_store(sizeof...(_Args)))
248 __format::__create_packed_storage(__storage.__types_, __storage.__values_, __args...);
249 else
250 __format::__store_basic_format_arg<_Context>(__storage.__args_, __args...);
251 }
252 }
253
254 using _Storage =
255 conditional_t<__format::__use_packed_format_arg_store(sizeof...(_Args)),
256 __format::__packed_format_arg_store<_Context, sizeof...(_Args)>,
257 __format::__unpacked_format_arg_store<_Context, sizeof...(_Args)>>;
258
259 _Storage __storage;
260 };
261
262 #endif //_LIBCPP_STD_VER >= 20
263
264 _LIBCPP_END_NAMESPACE_STD
265
266 #endif // _LIBCPP___FORMAT_FORMAT_ARG_STORE_H
267