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