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