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