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 <__concepts/arithmetic.h> 14 #include <__config> 15 #include <__format/format_error.h> 16 #include <__format/format_fwd.h> 17 #include <__functional_base> 18 #include <__variant/monostate.h> 19 #include <string> 20 #include <string_view> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 #pragma GCC system_header 24 #endif 25 26 _LIBCPP_PUSH_MACROS 27 #include <__undef_macros> 28 29 _LIBCPP_BEGIN_NAMESPACE_STD 30 31 #if _LIBCPP_STD_VER > 17 32 33 // TODO FMT Remove this once we require compilers with proper C++20 support. 34 // If the compiler has no concepts support, the format header will be disabled. 35 // Without concepts support enable_if needs to be used and that too much effort 36 // to support compilers with partial C++20 support. 37 #if !defined(_LIBCPP_HAS_NO_CONCEPTS) 38 39 namespace __format { 40 /** The type stored in @ref basic_format_arg. */ 41 enum class _LIBCPP_ENUM_VIS __arg_t : uint8_t { 42 __none, 43 __boolean, 44 __char_type, 45 __int, 46 __long_long, 47 #ifndef _LIBCPP_HAS_NO_INT128 48 __i128, 49 #endif 50 __unsigned, 51 __unsigned_long_long, 52 #ifndef _LIBCPP_HAS_NO_INT128 53 __u128, 54 #endif 55 __float, 56 __double, 57 __long_double, 58 __const_char_type_ptr, 59 __string_view, 60 __ptr 61 }; 62 } // namespace __format 63 64 template <class _Visitor, class _Context> 65 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT decltype(auto) 66 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) { 67 switch (__arg.__type_) { 68 case __format::__arg_t::__none: 69 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), monostate{}); 70 case __format::__arg_t::__boolean: 71 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__boolean); 72 case __format::__arg_t::__char_type: 73 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__char_type); 74 case __format::__arg_t::__int: 75 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__int); 76 case __format::__arg_t::__long_long: 77 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__long_long); 78 #ifndef _LIBCPP_HAS_NO_INT128 79 case __format::__arg_t::__i128: 80 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__i128); 81 #endif 82 case __format::__arg_t::__unsigned: 83 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__unsigned); 84 case __format::__arg_t::__unsigned_long_long: 85 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), 86 __arg.__unsigned_long_long); 87 #ifndef _LIBCPP_HAS_NO_INT128 88 case __format::__arg_t::__u128: 89 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__u128); 90 #endif 91 case __format::__arg_t::__float: 92 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__float); 93 case __format::__arg_t::__double: 94 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__double); 95 case __format::__arg_t::__long_double: 96 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__long_double); 97 case __format::__arg_t::__const_char_type_ptr: 98 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), 99 __arg.__const_char_type_ptr); 100 case __format::__arg_t::__string_view: 101 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__string_view); 102 case __format::__arg_t::__ptr: 103 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__ptr); 104 } 105 _LIBCPP_UNREACHABLE(); 106 } 107 108 template <class _Context> 109 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_arg { 110 public: 111 // TODO FMT Define the handle class. 112 class handle; 113 114 _LIBCPP_HIDE_FROM_ABI basic_format_arg() noexcept 115 : __type_{__format::__arg_t::__none} {} 116 117 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { 118 return __type_ != __format::__arg_t::__none; 119 } 120 121 private: 122 using char_type = typename _Context::char_type; 123 124 // TODO FMT Implement constrain [format.arg]/4 125 // Constraints: The template specialization 126 // typename Context::template formatter_type<T> 127 // meets the Formatter requirements ([formatter.requirements]). The extent 128 // to which an implementation determines that the specialization meets the 129 // Formatter requirements is unspecified, except that as a minimum the 130 // expression 131 // typename Context::template formatter_type<T>() 132 // .format(declval<const T&>(), declval<Context&>()) 133 // shall be well-formed when treated as an unevaluated operand. 134 135 template <class _Ctx, class... _Args> 136 _LIBCPP_HIDE_FROM_ABI 137 _LIBCPP_AVAILABILITY_FORMAT friend __format_arg_store<_Ctx, _Args...> 138 _VSTD::make_format_args(const _Args&...); 139 140 template <class _Visitor, class _Ctx> 141 _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT friend decltype(auto) 142 _VSTD::visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg); 143 144 union { 145 bool __boolean; 146 char_type __char_type; 147 int __int; 148 unsigned __unsigned; 149 long long __long_long; 150 unsigned long long __unsigned_long_long; 151 #ifndef _LIBCPP_HAS_NO_INT128 152 __int128_t __i128; 153 __uint128_t __u128; 154 #endif 155 float __float; 156 double __double; 157 long double __long_double; 158 const char_type* __const_char_type_ptr; 159 basic_string_view<char_type> __string_view; 160 const void* __ptr; 161 // TODO FMT Add the handle. 162 }; 163 __format::__arg_t __type_; 164 165 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(bool __v) noexcept 166 : __boolean(__v), __type_(__format::__arg_t::__boolean) {} 167 168 template <class _Tp> 169 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(_Tp __v) noexcept 170 requires(same_as<_Tp, char_type> || 171 (same_as<_Tp, char> && same_as<char_type, wchar_t>)) 172 : __char_type(__v), __type_(__format::__arg_t::__char_type) {} 173 174 template <__libcpp_signed_integer _Tp> 175 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(_Tp __v) noexcept { 176 if constexpr (sizeof(_Tp) <= sizeof(int)) { 177 __int = static_cast<int>(__v); 178 __type_ = __format::__arg_t::__int; 179 } else if constexpr (sizeof(_Tp) <= sizeof(long long)) { 180 __long_long = static_cast<long long>(__v); 181 __type_ = __format::__arg_t::__long_long; 182 } 183 #ifndef _LIBCPP_HAS_NO_INT128 184 else if constexpr (sizeof(_Tp) == sizeof(__int128_t)) { 185 __i128 = __v; 186 __type_ = __format::__arg_t::__i128; 187 } 188 #endif 189 else 190 static_assert(sizeof(_Tp) == 0, "An unsupported signed integer was used"); 191 } 192 193 template <__libcpp_unsigned_integer _Tp> 194 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(_Tp __v) noexcept { 195 if constexpr (sizeof(_Tp) <= sizeof(unsigned)) { 196 __unsigned = static_cast<unsigned>(__v); 197 __type_ = __format::__arg_t::__unsigned; 198 } else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long)) { 199 __unsigned_long_long = static_cast<unsigned long long>(__v); 200 __type_ = __format::__arg_t::__unsigned_long_long; 201 } 202 #ifndef _LIBCPP_HAS_NO_INT128 203 else if constexpr (sizeof(_Tp) == sizeof(__int128_t)) { 204 __u128 = __v; 205 __type_ = __format::__arg_t::__u128; 206 } 207 #endif 208 else 209 static_assert(sizeof(_Tp) == 0, 210 "An unsupported unsigned integer was used"); 211 } 212 213 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(float __v) noexcept 214 : __float(__v), __type_(__format::__arg_t::__float) {} 215 216 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(double __v) noexcept 217 : __double(__v), __type_(__format::__arg_t::__double) {} 218 219 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(long double __v) noexcept 220 : __long_double(__v), __type_(__format::__arg_t::__long_double) {} 221 222 // Note not a 'noexcept' function. 223 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(const char_type* __s) 224 : __const_char_type_ptr(__s), 225 __type_(__format::__arg_t::__const_char_type_ptr) { 226 _LIBCPP_ASSERT(__s, "Used a nullptr argument to initialize a C-string"); 227 } 228 229 template <class _Traits> 230 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg( 231 basic_string_view<char_type, _Traits> __s) noexcept 232 : __string_view{__s.data(), __s.size()}, 233 __type_(__format::__arg_t::__string_view) {} 234 235 template <class _Traits, class _Allocator> 236 _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg( 237 const basic_string<char_type, _Traits, _Allocator>& __s) noexcept 238 : __string_view{__s.data(), __s.size()}, 239 __type_(__format::__arg_t::__string_view) {} 240 241 _LIBCPP_HIDE_FROM_ABI 242 explicit basic_format_arg(nullptr_t) noexcept 243 : __ptr(nullptr), __type_(__format::__arg_t::__ptr) {} 244 245 // TODO FMT Implement the _Tp* constructor. 246 }; 247 248 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) 249 250 #endif //_LIBCPP_STD_VER > 17 251 252 _LIBCPP_END_NAMESPACE_STD 253 254 _LIBCPP_POP_MACROS 255 256 #endif // _LIBCPP___FORMAT_FORMAT_ARG_H 257