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_H
11 #define _LIBCPP___FORMAT_FORMAT_ARG_H
12
13 #include <__assert>
14 #include <__concepts/arithmetic.h>
15 #include <__config>
16 #include <__format/format_error.h>
17 #include <__format/format_fwd.h>
18 #include <__format/format_parse_context.h>
19 #include <__functional/invoke.h>
20 #include <__memory/addressof.h>
21 #include <__utility/declval.h>
22 #include <__utility/forward.h>
23 #include <__utility/unreachable.h>
24 #include <__variant/monostate.h>
25 #include <string>
26 #include <string_view>
27
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
30 #endif
31
32 _LIBCPP_BEGIN_NAMESPACE_STD
33
34 #if _LIBCPP_STD_VER >= 20
35
36 namespace __format {
37 /// The type stored in @ref basic_format_arg.
38 ///
39 /// @note The 128-bit types are unconditionally in the list to avoid the values
40 /// of the enums to depend on the availability of 128-bit integers.
41 ///
42 /// @note The value is stored as a 5-bit value in the __packed_arg_t_bits. This
43 /// limits the maximum number of elements to 32.
44 /// When modifying update the test
45 /// test/libcxx/utilities/format/format.arguments/format.arg/arg_t.compile.pass.cpp
46 /// It could be packed in 4-bits but that means a new type directly becomes an
47 /// ABI break. The packed type is 64-bit so this reduces the maximum number of
48 /// packed elements from 16 to 12.
49 ///
50 /// @note Some members of this enum are an extension. These extensions need
51 /// special behaviour in visit_format_arg. There they need to be wrapped in a
52 /// handle to satisfy the user observable behaviour. The internal function
53 /// __visit_format_arg doesn't do this wrapping. So in the format functions
54 /// this function is used to avoid unneeded overhead.
55 enum class _LIBCPP_ENUM_VIS __arg_t : uint8_t {
56 __none,
57 __boolean,
58 __char_type,
59 __int,
60 __long_long,
61 __i128, // extension
62 __unsigned,
63 __unsigned_long_long,
64 __u128, // extension
65 __float,
66 __double,
67 __long_double,
68 __const_char_type_ptr,
69 __string_view,
70 __ptr,
71 __handle
72 };
73
74 inline constexpr unsigned __packed_arg_t_bits = 5;
75 inline constexpr uint8_t __packed_arg_t_mask = 0x1f;
76
77 inline constexpr unsigned __packed_types_storage_bits = 64;
78 inline constexpr unsigned __packed_types_max = __packed_types_storage_bits / __packed_arg_t_bits;
79
80 _LIBCPP_HIDE_FROM_ABI
__use_packed_format_arg_store(size_t __size)81 constexpr bool __use_packed_format_arg_store(size_t __size) { return __size <= __packed_types_max; }
82
83 _LIBCPP_HIDE_FROM_ABI
__get_packed_type(uint64_t __types,size_t __id)84 constexpr __arg_t __get_packed_type(uint64_t __types, size_t __id) {
85 _LIBCPP_ASSERT(__id <= __packed_types_max, "");
86
87 if (__id > 0)
88 __types >>= __id * __packed_arg_t_bits;
89
90 return static_cast<__format::__arg_t>(__types & __packed_arg_t_mask);
91 }
92
93 } // namespace __format
94
95 // This function is not user obervable, so it can directly use the non-standard
96 // types of the "variant". See __arg_t for more details.
97 template <class _Visitor, class _Context>
decltype(auto)98 _LIBCPP_HIDE_FROM_ABI decltype(auto)
99 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
100 switch (__arg.__type_) {
101 case __format::__arg_t::__none:
102 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__monostate_);
103 case __format::__arg_t::__boolean:
104 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__boolean_);
105 case __format::__arg_t::__char_type:
106 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__char_type_);
107 case __format::__arg_t::__int:
108 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__int_);
109 case __format::__arg_t::__long_long:
110 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__long_long_);
111 case __format::__arg_t::__i128:
112 # ifndef _LIBCPP_HAS_NO_INT128
113 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__i128_);
114 # else
115 __libcpp_unreachable();
116 # endif
117 case __format::__arg_t::__unsigned:
118 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__unsigned_);
119 case __format::__arg_t::__unsigned_long_long:
120 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__unsigned_long_long_);
121 case __format::__arg_t::__u128:
122 # ifndef _LIBCPP_HAS_NO_INT128
123 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__u128_);
124 # else
125 __libcpp_unreachable();
126 # endif
127 case __format::__arg_t::__float:
128 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__float_);
129 case __format::__arg_t::__double:
130 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__double_);
131 case __format::__arg_t::__long_double:
132 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__long_double_);
133 case __format::__arg_t::__const_char_type_ptr:
134 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__const_char_type_ptr_);
135 case __format::__arg_t::__string_view:
136 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__string_view_);
137 case __format::__arg_t::__ptr:
138 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__value_.__ptr_);
139 case __format::__arg_t::__handle:
140 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis),
141 typename basic_format_arg<_Context>::handle{__arg.__value_.__handle_});
142 }
143
144 __libcpp_unreachable();
145 }
146
147 /// Contains the values used in basic_format_arg.
148 ///
149 /// This is a separate type so it's possible to store the values and types in
150 /// separate arrays.
151 template <class _Context>
152 class __basic_format_arg_value {
153 using _CharT = typename _Context::char_type;
154
155 public:
156 /// Contains the implementation for basic_format_arg::handle.
157 struct __handle {
158 template <class _Tp>
__handle__handle159 _LIBCPP_HIDE_FROM_ABI explicit __handle(_Tp&& __v) noexcept
160 : __ptr_(_VSTD::addressof(__v)),
161 __format_([](basic_format_parse_context<_CharT>& __parse_ctx, _Context& __ctx, const void* __ptr) {
162 using _Dp = remove_cvref_t<_Tp>;
163 using _Formatter = typename _Context::template formatter_type<_Dp>;
164 constexpr bool __const_formattable =
165 requires { _Formatter().format(std::declval<const _Dp&>(), std::declval<_Context&>()); };
166 using _Qp = conditional_t<__const_formattable, const _Dp, _Dp>;
167
168 static_assert(__const_formattable || !is_const_v<remove_reference_t<_Tp>>, "Mandated by [format.arg]/18");
169
170 _Formatter __f;
171 __parse_ctx.advance_to(__f.parse(__parse_ctx));
172 __ctx.advance_to(__f.format(*const_cast<_Qp*>(static_cast<const _Dp*>(__ptr)), __ctx));
173 }) {}
174
175 const void* __ptr_;
176 void (*__format_)(basic_format_parse_context<_CharT>&, _Context&, const void*);
177 };
178
179 union {
180 monostate __monostate_;
181 bool __boolean_;
182 _CharT __char_type_;
183 int __int_;
184 unsigned __unsigned_;
185 long long __long_long_;
186 unsigned long long __unsigned_long_long_;
187 # ifndef _LIBCPP_HAS_NO_INT128
188 __int128_t __i128_;
189 __uint128_t __u128_;
190 # endif
191 float __float_;
192 double __double_;
193 long double __long_double_;
194 const _CharT* __const_char_type_ptr_;
195 basic_string_view<_CharT> __string_view_;
196 const void* __ptr_;
197 __handle __handle_;
198 };
199
200 // These constructors contain the exact storage type used. If adjustments are
201 // required, these will be done in __create_format_arg.
202
__basic_format_arg_value()203 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value() noexcept : __monostate_() {}
__basic_format_arg_value(bool __value)204 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(bool __value) noexcept : __boolean_(__value) {}
__basic_format_arg_value(_CharT __value)205 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(_CharT __value) noexcept : __char_type_(__value) {}
__basic_format_arg_value(int __value)206 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(int __value) noexcept : __int_(__value) {}
__basic_format_arg_value(unsigned __value)207 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(unsigned __value) noexcept : __unsigned_(__value) {}
__basic_format_arg_value(long long __value)208 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(long long __value) noexcept : __long_long_(__value) {}
__basic_format_arg_value(unsigned long long __value)209 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(unsigned long long __value) noexcept
210 : __unsigned_long_long_(__value) {}
211 # ifndef _LIBCPP_HAS_NO_INT128
__basic_format_arg_value(__int128_t __value)212 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__int128_t __value) noexcept : __i128_(__value) {}
__basic_format_arg_value(__uint128_t __value)213 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__uint128_t __value) noexcept : __u128_(__value) {}
214 # endif
__basic_format_arg_value(float __value)215 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(float __value) noexcept : __float_(__value) {}
__basic_format_arg_value(double __value)216 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(double __value) noexcept : __double_(__value) {}
__basic_format_arg_value(long double __value)217 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(long double __value) noexcept : __long_double_(__value) {}
__basic_format_arg_value(const _CharT * __value)218 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(const _CharT* __value) noexcept : __const_char_type_ptr_(__value) {}
__basic_format_arg_value(basic_string_view<_CharT> __value)219 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(basic_string_view<_CharT> __value) noexcept
220 : __string_view_(__value) {}
__basic_format_arg_value(const void * __value)221 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(const void* __value) noexcept : __ptr_(__value) {}
__basic_format_arg_value(__handle __value)222 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__handle __value) noexcept
223 // TODO FMT Investigate why it doesn't work without the forward.
224 : __handle_(std::forward<__handle>(__value)) {}
225 };
226
227 template <class _Context>
228 class _LIBCPP_TEMPLATE_VIS basic_format_arg {
229 public:
230 class _LIBCPP_TEMPLATE_VIS handle;
231
basic_format_arg()232 _LIBCPP_HIDE_FROM_ABI basic_format_arg() noexcept
233 : __type_{__format::__arg_t::__none} {}
234
235 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept {
236 return __type_ != __format::__arg_t::__none;
237 }
238
239 private:
240 using char_type = typename _Context::char_type;
241
242 // TODO FMT Implement constrain [format.arg]/4
243 // Constraints: The template specialization
244 // typename Context::template formatter_type<T>
245 // meets the Formatter requirements ([formatter.requirements]). The extent
246 // to which an implementation determines that the specialization meets the
247 // Formatter requirements is unspecified, except that as a minimum the
248 // expression
249 // typename Context::template formatter_type<T>()
250 // .format(declval<const T&>(), declval<Context&>())
251 // shall be well-formed when treated as an unevaluated operand.
252
253 public:
254 __basic_format_arg_value<_Context> __value_;
255 __format::__arg_t __type_;
256
basic_format_arg(__format::__arg_t __type,__basic_format_arg_value<_Context> __value)257 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(__format::__arg_t __type,
258 __basic_format_arg_value<_Context> __value) noexcept
259 : __value_(__value), __type_(__type) {}
260 };
261
262 template <class _Context>
263 class _LIBCPP_TEMPLATE_VIS basic_format_arg<_Context>::handle {
264 public:
265 _LIBCPP_HIDE_FROM_ABI
format(basic_format_parse_context<char_type> & __parse_ctx,_Context & __ctx)266 void format(basic_format_parse_context<char_type>& __parse_ctx, _Context& __ctx) const {
267 __handle_.__format_(__parse_ctx, __ctx, __handle_.__ptr_);
268 }
269
handle(typename __basic_format_arg_value<_Context>::__handle & __handle)270 _LIBCPP_HIDE_FROM_ABI explicit handle(typename __basic_format_arg_value<_Context>::__handle& __handle) noexcept
271 : __handle_(__handle) {}
272
273 private:
274 typename __basic_format_arg_value<_Context>::__handle& __handle_;
275 };
276
277 // This function is user facing, so it must wrap the non-standard types of
278 // the "variant" in a handle to stay conforming. See __arg_t for more details.
279 template <class _Visitor, class _Context>
decltype(auto)280 _LIBCPP_HIDE_FROM_ABI decltype(auto)
281 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
282 switch (__arg.__type_) {
283 # ifndef _LIBCPP_HAS_NO_INT128
284 case __format::__arg_t::__i128: {
285 typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__i128_};
286 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
287 }
288
289 case __format::__arg_t::__u128: {
290 typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__u128_};
291 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
292 }
293 # endif
294 default:
295 return _VSTD::__visit_format_arg(_VSTD::forward<_Visitor>(__vis), __arg);
296 }
297 }
298
299 #endif //_LIBCPP_STD_VER >= 20
300
301 _LIBCPP_END_NAMESPACE_STD
302
303 #endif // _LIBCPP___FORMAT_FORMAT_ARG_H
304