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