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