Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:49

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___FORMAT_FORMAT_ARG_H
0011 #define _LIBCPP___FORMAT_FORMAT_ARG_H
0012 
0013 #include <__assert>
0014 #include <__concepts/arithmetic.h>
0015 #include <__config>
0016 #include <__cstddef/size_t.h>
0017 #include <__format/concepts.h>
0018 #include <__format/format_parse_context.h>
0019 #include <__functional/invoke.h>
0020 #include <__fwd/format.h>
0021 #include <__memory/addressof.h>
0022 #include <__type_traits/conditional.h>
0023 #include <__type_traits/remove_const.h>
0024 #include <__utility/forward.h>
0025 #include <__utility/move.h>
0026 #include <__utility/unreachable.h>
0027 #include <__variant/monostate.h>
0028 #include <cstdint>
0029 #include <string_view>
0030 
0031 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0032 #  pragma GCC system_header
0033 #endif
0034 
0035 _LIBCPP_PUSH_MACROS
0036 #include <__undef_macros>
0037 
0038 _LIBCPP_BEGIN_NAMESPACE_STD
0039 
0040 #if _LIBCPP_STD_VER >= 20
0041 
0042 namespace __format {
0043 /// The type stored in @ref basic_format_arg.
0044 ///
0045 /// @note The 128-bit types are unconditionally in the list to avoid the values
0046 /// of the enums to depend on the availability of 128-bit integers.
0047 ///
0048 /// @note The value is stored as a 5-bit value in the __packed_arg_t_bits. This
0049 /// limits the maximum number of elements to 32.
0050 /// When modifying update the test
0051 /// test/libcxx/utilities/format/format.arguments/format.arg/arg_t.compile.pass.cpp
0052 /// It could be packed in 4-bits but that means a new type directly becomes an
0053 /// ABI break. The packed type is 64-bit so this reduces the maximum number of
0054 /// packed elements from 16 to 12.
0055 ///
0056 /// @note Some members of this enum are an extension. These extensions need
0057 /// special behaviour in visit_format_arg. There they need to be wrapped in a
0058 /// handle to satisfy the user observable behaviour. The internal function
0059 /// __visit_format_arg doesn't do this wrapping. So in the format functions
0060 /// this function is used to avoid unneeded overhead.
0061 enum class __arg_t : uint8_t {
0062   __none,
0063   __boolean,
0064   __char_type,
0065   __int,
0066   __long_long,
0067   __i128, // extension
0068   __unsigned,
0069   __unsigned_long_long,
0070   __u128, // extension
0071   __float,
0072   __double,
0073   __long_double,
0074   __const_char_type_ptr,
0075   __string_view,
0076   __ptr,
0077   __handle
0078 };
0079 
0080 inline constexpr unsigned __packed_arg_t_bits = 5;
0081 inline constexpr uint8_t __packed_arg_t_mask  = 0x1f;
0082 
0083 inline constexpr unsigned __packed_types_storage_bits = 64;
0084 inline constexpr unsigned __packed_types_max          = __packed_types_storage_bits / __packed_arg_t_bits;
0085 
0086 _LIBCPP_HIDE_FROM_ABI constexpr bool __use_packed_format_arg_store(size_t __size) {
0087   return __size <= __packed_types_max;
0088 }
0089 
0090 _LIBCPP_HIDE_FROM_ABI constexpr __arg_t __get_packed_type(uint64_t __types, size_t __id) {
0091   _LIBCPP_ASSERT_INTERNAL(__id <= __packed_types_max, "");
0092 
0093   if (__id > 0)
0094     __types >>= __id * __packed_arg_t_bits;
0095 
0096   return static_cast<__format::__arg_t>(__types & __packed_arg_t_mask);
0097 }
0098 
0099 } // namespace __format
0100 
0101 // This function is not user observable, so it can directly use the non-standard
0102 // types of the "variant". See __arg_t for more details.
0103 template <class _Visitor, class _Context>
0104 _LIBCPP_HIDE_FROM_ABI decltype(auto) __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
0105   switch (__arg.__type_) {
0106   case __format::__arg_t::__none:
0107     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__monostate_);
0108   case __format::__arg_t::__boolean:
0109     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__boolean_);
0110   case __format::__arg_t::__char_type:
0111     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__char_type_);
0112   case __format::__arg_t::__int:
0113     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__int_);
0114   case __format::__arg_t::__long_long:
0115     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__long_long_);
0116   case __format::__arg_t::__i128:
0117 #  if _LIBCPP_HAS_INT128
0118     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__i128_);
0119 #  else
0120     __libcpp_unreachable();
0121 #  endif
0122   case __format::__arg_t::__unsigned:
0123     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_);
0124   case __format::__arg_t::__unsigned_long_long:
0125     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_long_long_);
0126   case __format::__arg_t::__u128:
0127 #  if _LIBCPP_HAS_INT128
0128     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__u128_);
0129 #  else
0130     __libcpp_unreachable();
0131 #  endif
0132   case __format::__arg_t::__float:
0133     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__float_);
0134   case __format::__arg_t::__double:
0135     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__double_);
0136   case __format::__arg_t::__long_double:
0137     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__long_double_);
0138   case __format::__arg_t::__const_char_type_ptr:
0139     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__const_char_type_ptr_);
0140   case __format::__arg_t::__string_view:
0141     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__string_view_);
0142   case __format::__arg_t::__ptr:
0143     return std::invoke(std::forward<_Visitor>(__vis), __arg.__value_.__ptr_);
0144   case __format::__arg_t::__handle:
0145     return std::invoke(
0146         std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__arg.__value_.__handle_});
0147   }
0148 
0149   __libcpp_unreachable();
0150 }
0151 
0152 #  if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
0153 
0154 template <class _Rp, class _Visitor, class _Context>
0155 _LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
0156   switch (__arg.__type_) {
0157   case __format::__arg_t::__none:
0158     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__monostate_);
0159   case __format::__arg_t::__boolean:
0160     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__boolean_);
0161   case __format::__arg_t::__char_type:
0162     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__char_type_);
0163   case __format::__arg_t::__int:
0164     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__int_);
0165   case __format::__arg_t::__long_long:
0166     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__long_long_);
0167   case __format::__arg_t::__i128:
0168 #    if _LIBCPP_HAS_INT128
0169     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__i128_);
0170 #    else
0171     __libcpp_unreachable();
0172 #    endif
0173   case __format::__arg_t::__unsigned:
0174     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_);
0175   case __format::__arg_t::__unsigned_long_long:
0176     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_long_long_);
0177   case __format::__arg_t::__u128:
0178 #    if _LIBCPP_HAS_INT128
0179     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__u128_);
0180 #    else
0181     __libcpp_unreachable();
0182 #    endif
0183   case __format::__arg_t::__float:
0184     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__float_);
0185   case __format::__arg_t::__double:
0186     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__double_);
0187   case __format::__arg_t::__long_double:
0188     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__long_double_);
0189   case __format::__arg_t::__const_char_type_ptr:
0190     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__const_char_type_ptr_);
0191   case __format::__arg_t::__string_view:
0192     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__string_view_);
0193   case __format::__arg_t::__ptr:
0194     return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__ptr_);
0195   case __format::__arg_t::__handle:
0196     return std::invoke_r<_Rp>(
0197         std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__arg.__value_.__handle_});
0198   }
0199 
0200   __libcpp_unreachable();
0201 }
0202 
0203 #  endif // _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
0204 
0205 /// Contains the values used in basic_format_arg.
0206 ///
0207 /// This is a separate type so it's possible to store the values and types in
0208 /// separate arrays.
0209 template <class _Context>
0210 class __basic_format_arg_value {
0211   using _CharT _LIBCPP_NODEBUG = typename _Context::char_type;
0212 
0213 public:
0214   /// Contains the implementation for basic_format_arg::handle.
0215   struct __handle {
0216     template <class _Tp>
0217     _LIBCPP_HIDE_FROM_ABI explicit __handle(_Tp& __v) noexcept
0218         : __ptr_(std::addressof(__v)),
0219           __format_([](basic_format_parse_context<_CharT>& __parse_ctx, _Context& __ctx, const void* __ptr) {
0220             using _Dp = remove_const_t<_Tp>;
0221             using _Qp = conditional_t<__formattable_with<const _Dp, _Context>, const _Dp, _Dp>;
0222             static_assert(__formattable_with<_Qp, _Context>, "Mandated by [format.arg]/10");
0223 
0224             typename _Context::template formatter_type<_Dp> __f;
0225             __parse_ctx.advance_to(__f.parse(__parse_ctx));
0226             __ctx.advance_to(__f.format(*const_cast<_Qp*>(static_cast<const _Dp*>(__ptr)), __ctx));
0227           }) {}
0228 
0229     const void* __ptr_;
0230     void (*__format_)(basic_format_parse_context<_CharT>&, _Context&, const void*);
0231   };
0232 
0233   union {
0234     monostate __monostate_;
0235     bool __boolean_;
0236     _CharT __char_type_;
0237     int __int_;
0238     unsigned __unsigned_;
0239     long long __long_long_;
0240     unsigned long long __unsigned_long_long_;
0241 #  if _LIBCPP_HAS_INT128
0242     __int128_t __i128_;
0243     __uint128_t __u128_;
0244 #  endif
0245     float __float_;
0246     double __double_;
0247     long double __long_double_;
0248     const _CharT* __const_char_type_ptr_;
0249     basic_string_view<_CharT> __string_view_;
0250     const void* __ptr_;
0251     __handle __handle_;
0252   };
0253 
0254   // These constructors contain the exact storage type used. If adjustments are
0255   // required, these will be done in __create_format_arg.
0256 
0257   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value() noexcept : __monostate_() {}
0258   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(bool __value) noexcept : __boolean_(__value) {}
0259   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(_CharT __value) noexcept : __char_type_(__value) {}
0260   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(int __value) noexcept : __int_(__value) {}
0261   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(unsigned __value) noexcept : __unsigned_(__value) {}
0262   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(long long __value) noexcept : __long_long_(__value) {}
0263   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(unsigned long long __value) noexcept
0264       : __unsigned_long_long_(__value) {}
0265 #  if _LIBCPP_HAS_INT128
0266   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__int128_t __value) noexcept : __i128_(__value) {}
0267   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__uint128_t __value) noexcept : __u128_(__value) {}
0268 #  endif
0269   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(float __value) noexcept : __float_(__value) {}
0270   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(double __value) noexcept : __double_(__value) {}
0271   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(long double __value) noexcept : __long_double_(__value) {}
0272   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(const _CharT* __value) noexcept : __const_char_type_ptr_(__value) {}
0273   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(basic_string_view<_CharT> __value) noexcept
0274       : __string_view_(__value) {}
0275   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(const void* __value) noexcept : __ptr_(__value) {}
0276   _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__handle&& __value) noexcept : __handle_(std::move(__value)) {}
0277 };
0278 
0279 template <class _Context>
0280 class _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS basic_format_arg {
0281 public:
0282   class _LIBCPP_TEMPLATE_VIS handle;
0283 
0284   _LIBCPP_HIDE_FROM_ABI basic_format_arg() noexcept : __type_{__format::__arg_t::__none} {}
0285 
0286   _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __type_ != __format::__arg_t::__none; }
0287 
0288 #  if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
0289 
0290   // This function is user facing, so it must wrap the non-standard types of
0291   // the "variant" in a handle to stay conforming. See __arg_t for more details.
0292   template <class _Visitor>
0293   _LIBCPP_HIDE_FROM_ABI decltype(auto) visit(this basic_format_arg __arg, _Visitor&& __vis) {
0294     switch (__arg.__type_) {
0295 #    if _LIBCPP_HAS_INT128
0296     case __format::__arg_t::__i128: {
0297       typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__i128_};
0298       return std::invoke(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
0299     }
0300 
0301     case __format::__arg_t::__u128: {
0302       typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__u128_};
0303       return std::invoke(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
0304     }
0305 #    endif
0306     default:
0307       return std::__visit_format_arg(std::forward<_Visitor>(__vis), __arg);
0308     }
0309   }
0310 
0311   // This function is user facing, so it must wrap the non-standard types of
0312   // the "variant" in a handle to stay conforming. See __arg_t for more details.
0313   template <class _Rp, class _Visitor>
0314   _LIBCPP_HIDE_FROM_ABI _Rp visit(this basic_format_arg __arg, _Visitor&& __vis) {
0315     switch (__arg.__type_) {
0316 #    if _LIBCPP_HAS_INT128
0317     case __format::__arg_t::__i128: {
0318       typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__i128_};
0319       return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
0320     }
0321 
0322     case __format::__arg_t::__u128: {
0323       typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__u128_};
0324       return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
0325     }
0326 #    endif
0327     default:
0328       return std::__visit_format_arg<_Rp>(std::forward<_Visitor>(__vis), __arg);
0329     }
0330   }
0331 
0332 #  endif // _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
0333 
0334 private:
0335   using char_type = typename _Context::char_type;
0336 
0337   // TODO FMT Implement constrain [format.arg]/4
0338   // Constraints: The template specialization
0339   //   typename Context::template formatter_type<T>
0340   // meets the Formatter requirements ([formatter.requirements]).  The extent
0341   // to which an implementation determines that the specialization meets the
0342   // Formatter requirements is unspecified, except that as a minimum the
0343   // expression
0344   //   typename Context::template formatter_type<T>()
0345   //    .format(declval<const T&>(), declval<Context&>())
0346   // shall be well-formed when treated as an unevaluated operand.
0347 
0348 public:
0349   __basic_format_arg_value<_Context> __value_;
0350   __format::__arg_t __type_;
0351 
0352   _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(__format::__arg_t __type,
0353                                                   __basic_format_arg_value<_Context> __value) noexcept
0354       : __value_(__value), __type_(__type) {}
0355 };
0356 
0357 template <class _Context>
0358 class _LIBCPP_TEMPLATE_VIS basic_format_arg<_Context>::handle {
0359 public:
0360   _LIBCPP_HIDE_FROM_ABI void format(basic_format_parse_context<char_type>& __parse_ctx, _Context& __ctx) const {
0361     __handle_.__format_(__parse_ctx, __ctx, __handle_.__ptr_);
0362   }
0363 
0364   _LIBCPP_HIDE_FROM_ABI explicit handle(typename __basic_format_arg_value<_Context>::__handle& __handle) noexcept
0365       : __handle_(__handle) {}
0366 
0367 private:
0368   typename __basic_format_arg_value<_Context>::__handle& __handle_;
0369 };
0370 
0371 // This function is user facing, so it must wrap the non-standard types of
0372 // the "variant" in a handle to stay conforming. See __arg_t for more details.
0373 template <class _Visitor, class _Context>
0374 #  if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
0375 _LIBCPP_DEPRECATED_IN_CXX26
0376 #  endif
0377     _LIBCPP_HIDE_FROM_ABI decltype(auto)
0378     visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
0379   switch (__arg.__type_) {
0380 #  if _LIBCPP_HAS_INT128
0381   case __format::__arg_t::__i128: {
0382     typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__i128_};
0383     return std::invoke(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
0384   }
0385 
0386   case __format::__arg_t::__u128: {
0387     typename __basic_format_arg_value<_Context>::__handle __h{__arg.__value_.__u128_};
0388     return std::invoke(std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__h});
0389   }
0390 #  endif // _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
0391   default:
0392     return std::__visit_format_arg(std::forward<_Visitor>(__vis), __arg);
0393   }
0394 }
0395 
0396 #endif // _LIBCPP_STD_VER >= 20
0397 
0398 _LIBCPP_END_NAMESPACE_STD
0399 
0400 _LIBCPP_POP_MACROS
0401 
0402 #endif // _LIBCPP___FORMAT_FORMAT_ARG_H