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 74 constexpr bool __use_packed_format_arg_store(size_t __size) { return __size <= __packed_types_max; } 75 76 _LIBCPP_HIDE_FROM_ABI 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> 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> 150 _LIBCPP_HIDE_FROM_ABI explicit __handle(const _Tp& __v) noexcept 151 : __ptr_(_VSTD::addressof(__v)), 152 __format_([](basic_format_parse_context<_CharT>& __parse_ctx, _Context& __ctx, const void* __ptr) { 153 using _Formatter = typename _Context::template formatter_type<_Tp>; 154 using _Qp = conditional_t<requires { _Formatter().format(declval<const _Tp&>(), declval<_Context&>()); }, 155 const _Tp, _Tp>; 156 _Formatter __f; 157 __parse_ctx.advance_to(__f.parse(__parse_ctx)); 158 __ctx.advance_to(__f.format(*const_cast<_Qp*>(static_cast<const _Tp*>(__ptr)), __ctx)); 159 }) {} 160 161 const void* __ptr_; 162 void (*__format_)(basic_format_parse_context<_CharT>&, _Context&, const void*); 163 }; 164 165 union { 166 monostate __monostate_; 167 bool __boolean_; 168 _CharT __char_type_; 169 int __int_; 170 unsigned __unsigned_; 171 long long __long_long_; 172 unsigned long long __unsigned_long_long_; 173 # ifndef _LIBCPP_HAS_NO_INT128 174 __int128_t __i128_; 175 __uint128_t __u128_; 176 # endif 177 float __float_; 178 double __double_; 179 long double __long_double_; 180 const _CharT* __const_char_type_ptr_; 181 basic_string_view<_CharT> __string_view_; 182 const void* __ptr_; 183 __handle __handle_; 184 }; 185 186 // These constructors contain the exact storage type used. If adjustments are 187 // required, these will be done in __create_format_arg. 188 189 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value() noexcept : __monostate_() {} 190 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(bool __value) noexcept : __boolean_(__value) {} 191 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(_CharT __value) noexcept : __char_type_(__value) {} 192 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(int __value) noexcept : __int_(__value) {} 193 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(unsigned __value) noexcept : __unsigned_(__value) {} 194 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(long long __value) noexcept : __long_long_(__value) {} 195 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(unsigned long long __value) noexcept 196 : __unsigned_long_long_(__value) {} 197 # ifndef _LIBCPP_HAS_NO_INT128 198 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__int128_t __value) noexcept : __i128_(__value) {} 199 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__uint128_t __value) noexcept : __u128_(__value) {} 200 # endif 201 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(float __value) noexcept : __float_(__value) {} 202 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(double __value) noexcept : __double_(__value) {} 203 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(long double __value) noexcept : __long_double_(__value) {} 204 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(const _CharT* __value) noexcept : __const_char_type_ptr_(__value) {} 205 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(basic_string_view<_CharT> __value) noexcept 206 : __string_view_(__value) {} 207 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(const void* __value) noexcept : __ptr_(__value) {} 208 _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__handle __value) noexcept : __handle_(__value) {} 209 }; 210 211 template <class _Context> 212 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_arg { 213 public: 214 class _LIBCPP_TEMPLATE_VIS handle; 215 216 _LIBCPP_HIDE_FROM_ABI basic_format_arg() noexcept 217 : __type_{__format::__arg_t::__none} {} 218 219 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { 220 return __type_ != __format::__arg_t::__none; 221 } 222 223 private: 224 using char_type = typename _Context::char_type; 225 226 // TODO FMT Implement constrain [format.arg]/4 227 // Constraints: The template specialization 228 // typename Context::template formatter_type<T> 229 // meets the Formatter requirements ([formatter.requirements]). The extent 230 // to which an implementation determines that the specialization meets the 231 // Formatter requirements is unspecified, except that as a minimum the 232 // expression 233 // typename Context::template formatter_type<T>() 234 // .format(declval<const T&>(), declval<Context&>()) 235 // shall be well-formed when treated as an unevaluated operand. 236 237 public: 238 __basic_format_arg_value<_Context> __value_; 239 __format::__arg_t __type_; 240 241 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(__format::__arg_t __type, 242 __basic_format_arg_value<_Context> __value) noexcept 243 : __value_(__value), __type_(__type) {} 244 }; 245 246 template <class _Context> 247 class _LIBCPP_TEMPLATE_VIS basic_format_arg<_Context>::handle { 248 public: 249 _LIBCPP_HIDE_FROM_ABI 250 void format(basic_format_parse_context<char_type>& __parse_ctx, _Context& __ctx) const { 251 __handle_.__format_(__parse_ctx, __ctx, __handle_.__ptr_); 252 } 253 254 _LIBCPP_HIDE_FROM_ABI explicit handle(typename __basic_format_arg_value<_Context>::__handle __handle) noexcept 255 : __handle_(__handle) {} 256 257 private: 258 typename __basic_format_arg_value<_Context>::__handle __handle_; 259 }; 260 261 #endif //_LIBCPP_STD_VER > 17 262 263 _LIBCPP_END_NAMESPACE_STD 264 265 #endif // _LIBCPP___FORMAT_FORMAT_ARG_H 266