[libc++][format] Add __format_arg_store.
This implements the struct `__format_arg_store` and its dependencies:
* the class basic_format_arg,
* the class basic_format_args,
* the class basic_format_context,
* the function make_format_args,
* the function wmake_format_args,
* the function visit_format_arg,
* several Standard required typedefs.
The following parts will be implemented in a later patch:
* the child class `basic_format_arg::handle`,
* the function `basic_format_arg::basic_format_arg(const T* p)`.
The following extension has been implemented:
* the class basic_format_arg supports `__[u]int128_t` on platform where libc++ supports 128 bit integrals.
Implements parts of:
* P0645 Text Formatting
Completes:
* LWG3371 visit_format_arg and make_format_args are not hidden friends
* LWG3542 basic_format_arg mishandles basic_string_view with custom traits
Note https://mordante.github.io/blog/2021/06/05/format.html gives a bit more information about the goals and non-goals of this initial patch series.
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103357
NOKEYCHECK=True
GitOrigin-RevId: 0922ce56f4f04fbcacead6cdf0416341fe44e4bb
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index 07faed9..33bc1ec 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -128,7 +128,11 @@
__config
__debug
__errc
+ __format/format_arg.h
+ __format/format_args.h
+ __format/format_context.h
__format/format_error.h
+ __format/format_fwd.h
__format/format_parse_context.h
__function_like.h
__functional_base
diff --git a/include/__availability b/include/__availability
index 13d1195..87d43ed 100644
--- a/include/__availability
+++ b/include/__availability
@@ -139,9 +139,9 @@
// # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore
// This controls the availability of the C++20 format library.
- // The library is in development and not ABI stable yet. Currently
- // P2216 is aiming to be retroactively accepted in C++20. This paper
- // contains ABI breaking changes.
+ // The library is in development and not ABI stable yet. P2216 is
+ // retroactively accepted in C++20. This paper contains ABI breaking
+ // changes.
# define _LIBCPP_AVAILABILITY_FORMAT
// # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format
@@ -238,9 +238,9 @@
# endif
// This controls the availability of the C++20 format library.
- // The library is in development and not ABI stable yet. Currently
- // P2216 is aiming to be retroactively accepted in C++20. This paper
- // contains ABI breaking changes.
+ // The library is in development and not ABI stable yet. P2216 is
+ // retroactively accepted in C++20. This paper contains ABI breaking
+ // changes.
# define _LIBCPP_AVAILABILITY_FORMAT \
__attribute__((unavailable))
# define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format
diff --git a/include/__format/format_arg.h b/include/__format/format_arg.h
new file mode 100644
index 0000000..dd8ef48
--- /dev/null
+++ b/include/__format/format_arg.h
@@ -0,0 +1,256 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FORMAT_FORMAT_ARG_H
+#define _LIBCPP___FORMAT_FORMAT_ARG_H
+
+#include <__config>
+#include <__format/format_error.h>
+#include <__format/format_fwd.h>
+#include <__functional_base>
+#include <__variant/monostate.h>
+#include <concepts>
+#include <string>
+#include <string_view>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17
+
+// TODO FMT Remove this once we require compilers with proper C++20 support.
+// If the compiler has no concepts support, the format header will be disabled.
+// Without concepts support enable_if needs to be used and that too much effort
+// to support compilers with partial C++20 support.
+#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+namespace __format {
+/** The type stored in @ref basic_format_arg. */
+enum class _LIBCPP_ENUM_VIS __arg_t : uint8_t {
+ __none,
+ __bool,
+ __char_type,
+ __int,
+ __long_long,
+#ifndef _LIBCPP_HAS_NO_INT128
+ __i128,
+#endif
+ __unsigned,
+ __unsigned_long_long,
+#ifndef _LIBCPP_HAS_NO_INT128
+ __u128,
+#endif
+ __float,
+ __double,
+ __long_double,
+ __const_char_type_ptr,
+ __string_view,
+ __ptr
+};
+} // namespace __format
+
+template <class _Visitor, class _Context>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT decltype(auto)
+visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
+ switch (__arg.__type_) {
+ case __format::__arg_t::__none:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), monostate{});
+ case __format::__arg_t::__bool:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__bool);
+ case __format::__arg_t::__char_type:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__char_type);
+ case __format::__arg_t::__int:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__int);
+ case __format::__arg_t::__long_long:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__long_long);
+#ifndef _LIBCPP_HAS_NO_INT128
+ case __format::__arg_t::__i128:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__i128);
+#endif
+ case __format::__arg_t::__unsigned:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__unsigned);
+ case __format::__arg_t::__unsigned_long_long:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis),
+ __arg.__unsigned_long_long);
+#ifndef _LIBCPP_HAS_NO_INT128
+ case __format::__arg_t::__u128:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__u128);
+#endif
+ case __format::__arg_t::__float:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__float);
+ case __format::__arg_t::__double:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__double);
+ case __format::__arg_t::__long_double:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__long_double);
+ case __format::__arg_t::__const_char_type_ptr:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis),
+ __arg.__const_char_type_ptr);
+ case __format::__arg_t::__string_view:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__string_view);
+ case __format::__arg_t::__ptr:
+ return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__ptr);
+ }
+ _LIBCPP_UNREACHABLE();
+}
+
+template <class _Context>
+class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_arg {
+public:
+ // TODO FMT Define the handle class.
+ class handle;
+
+ _LIBCPP_HIDE_FROM_ABI basic_format_arg() noexcept
+ : __type_{__format::__arg_t::__none} {}
+
+ _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept {
+ return __type_ != __format::__arg_t::__none;
+ }
+
+private:
+ using char_type = typename _Context::char_type;
+
+ // TODO FMT Implement constrain [format.arg]/4
+ // Constraints: The template specialization
+ // typename Context::template formatter_type<T>
+ // meets the Formatter requirements ([formatter.requirements]). The extent
+ // to which an implementation determines that the specialization meets the
+ // Formatter requirements is unspecified, except that as a minimum the
+ // expression
+ // typename Context::template formatter_type<T>()
+ // .format(declval<const T&>(), declval<Context&>())
+ // shall be well-formed when treated as an unevaluated operand.
+
+ template <class _Ctx, class... _Args>
+ _LIBCPP_HIDE_FROM_ABI
+ _LIBCPP_AVAILABILITY_FORMAT friend __format_arg_store<_Ctx, _Args...>
+ _VSTD::make_format_args(const _Args&...);
+
+ template <class _Visitor, class _Ctx>
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT friend decltype(auto)
+ _VSTD::visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg);
+
+ union {
+ bool __bool;
+ char_type __char_type;
+ int __int;
+ unsigned __unsigned;
+ long long __long_long;
+ unsigned long long __unsigned_long_long;
+#ifndef _LIBCPP_HAS_NO_INT128
+ __int128_t __i128;
+ __uint128_t __u128;
+#endif
+ float __float;
+ double __double;
+ long double __long_double;
+ const char_type* __const_char_type_ptr;
+ basic_string_view<char_type> __string_view;
+ const void* __ptr;
+ // TODO FMT Add the handle.
+ };
+ __format::__arg_t __type_;
+
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(bool __v) noexcept
+ : __bool(__v), __type_(__format::__arg_t::__bool) {}
+
+ template <class _Tp>
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(_Tp __v) noexcept
+ requires(same_as<_Tp, char_type> ||
+ (same_as<_Tp, char> && same_as<char_type, wchar_t>))
+ : __char_type(__v), __type_(__format::__arg_t::__char_type) {}
+
+ template <__libcpp_signed_integer _Tp>
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(_Tp __v) noexcept {
+ if constexpr (sizeof(_Tp) <= sizeof(int)) {
+ __int = static_cast<int>(__v);
+ __type_ = __format::__arg_t::__int;
+ } else if constexpr (sizeof(_Tp) <= sizeof(long long)) {
+ __long_long = static_cast<long long>(__v);
+ __type_ = __format::__arg_t::__long_long;
+ }
+#ifndef _LIBCPP_HAS_NO_INT128
+ else if constexpr (sizeof(_Tp) == sizeof(__int128_t)) {
+ __i128 = __v;
+ __type_ = __format::__arg_t::__i128;
+ }
+#endif
+ else
+ static_assert(sizeof(_Tp) == 0, "An unsupported signed integer was used");
+ }
+
+ template <__libcpp_unsigned_integer _Tp>
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(_Tp __v) noexcept {
+ if constexpr (sizeof(_Tp) <= sizeof(unsigned)) {
+ __unsigned = static_cast<unsigned>(__v);
+ __type_ = __format::__arg_t::__unsigned;
+ } else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long)) {
+ __unsigned_long_long = static_cast<unsigned long long>(__v);
+ __type_ = __format::__arg_t::__unsigned_long_long;
+ }
+#ifndef _LIBCPP_HAS_NO_INT128
+ else if constexpr (sizeof(_Tp) == sizeof(__int128_t)) {
+ __u128 = __v;
+ __type_ = __format::__arg_t::__u128;
+ }
+#endif
+ else
+ static_assert(sizeof(_Tp) == 0,
+ "An unsupported unsigned integer was used");
+ }
+
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(float __v) noexcept
+ : __float(__v), __type_(__format::__arg_t::__float) {}
+
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(double __v) noexcept
+ : __double(__v), __type_(__format::__arg_t::__double) {}
+
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(long double __v) noexcept
+ : __long_double(__v), __type_(__format::__arg_t::__long_double) {}
+
+ // Note not a 'noexcept' function.
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(const char_type* __s)
+ : __const_char_type_ptr(__s),
+ __type_(__format::__arg_t::__const_char_type_ptr) {
+ _LIBCPP_ASSERT(__s, "Used a nullptr argument to initialize a C-string");
+ }
+
+ template <class _Traits>
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(
+ basic_string_view<char_type, _Traits> __s) noexcept
+ : __string_view{__s.data(), __s.size()},
+ __type_(__format::__arg_t::__string_view) {}
+
+ template <class _Traits, class _Allocator>
+ _LIBCPP_HIDE_FROM_ABI explicit basic_format_arg(
+ const basic_string<char_type, _Traits, _Allocator>& __s) noexcept
+ : __string_view{__s.data(), __s.size()},
+ __type_(__format::__arg_t::__string_view) {}
+
+ _LIBCPP_HIDE_FROM_ABI
+ explicit basic_format_arg(nullptr_t) noexcept
+ : __ptr(nullptr), __type_(__format::__arg_t::__ptr) {}
+
+ // TODO FMT Implement the _Tp* constructor.
+};
+
+#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+#endif //_LIBCPP_STD_VER > 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___FORMAT_FORMAT_ARG_H
diff --git a/include/__format/format_args.h b/include/__format/format_args.h
new file mode 100644
index 0000000..0a26b95
--- /dev/null
+++ b/include/__format/format_args.h
@@ -0,0 +1,71 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FORMAT_FORMAT_ARGS_H
+#define _LIBCPP___FORMAT_FORMAT_ARGS_H
+
+#include <__availability>
+#include <__config>
+#include <__format/format_fwd.h>
+#include <cstddef>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17
+
+// TODO FMT Remove this once we require compilers with proper C++20 support.
+// If the compiler has no concepts support, the format header will be disabled.
+// Without concepts support enable_if needs to be used and that too much effort
+// to support compilers with partial C++20 support.
+#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+template <class _Context>
+class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_args {
+public:
+ // TODO FMT Implement [format.args]/5
+ // [Note 1: Implementations are encouraged to optimize the representation of
+ // basic_format_args for small number of formatting arguments by storing
+ // indices of type alternatives separately from values and packing the
+ // former. - end note]
+ // Note: Change __format_arg_store to use a built-in array.
+ _LIBCPP_HIDE_FROM_ABI basic_format_args() noexcept = default;
+
+ template <class... _Args>
+ _LIBCPP_HIDE_FROM_ABI basic_format_args(
+ const __format_arg_store<_Context, _Args...>& __store) noexcept
+ : __size_(sizeof...(_Args)), __data_(__store.__args.data()) {}
+
+ _LIBCPP_HIDE_FROM_ABI
+ basic_format_arg<_Context> get(size_t __id) const noexcept {
+ return __id < __size_ ? __data_[__id] : basic_format_arg<_Context>{};
+ }
+
+ _LIBCPP_HIDE_FROM_ABI size_t __size() const noexcept { return __size_; }
+
+private:
+ size_t __size_{0};
+ const basic_format_arg<_Context>* __data_{nullptr};
+};
+
+#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+#endif //_LIBCPP_STD_VER > 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___FORMAT_FORMAT_ARGS_H
diff --git a/include/__format/format_context.h b/include/__format/format_context.h
new file mode 100644
index 0000000..e277aa0
--- /dev/null
+++ b/include/__format/format_context.h
@@ -0,0 +1,160 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FORMAT_FORMAT_CONTEXT_H
+#define _LIBCPP___FORMAT_FORMAT_CONTEXT_H
+
+#include <__availability>
+#include <__config>
+#include <__format/format_args.h>
+#include <__format/format_fwd.h>
+#include <__iterator/concepts.h>
+#include <concepts>
+#include <iterator>
+#include <string>
+
+#ifndef _LIBCPP_HAS_NO_LOCALIZATION
+#include <locale>
+#include <optional>
+#endif
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17
+
+// TODO FMT Remove this once we require compilers with proper C++20 support.
+// If the compiler has no concepts support, the format header will be disabled.
+// Without concepts support enable_if needs to be used and that too much effort
+// to support compilers with partial C++20 support.
+#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+template <class _OutIt, class _CharT>
+requires output_iterator<_OutIt, const _CharT&>
+class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_context;
+
+#ifndef _LIBCPP_HAS_NO_LOCALIZATION
+/**
+ * Helper to create a basic_format_context.
+ *
+ * This is needed since the constructor is private.
+ */
+template <class _OutIt, class _CharT>
+_LIBCPP_HIDE_FROM_ABI basic_format_context<_OutIt, _CharT>
+__format_context_create(
+ _OutIt __out_it,
+ basic_format_args<basic_format_context<_OutIt, _CharT>> __args,
+ optional<_VSTD::locale>&& __loc = nullopt) {
+ return _VSTD::basic_format_context(_VSTD::move(__out_it), __args,
+ _VSTD::move(__loc));
+}
+#else
+template <class _OutIt, class _CharT>
+_LIBCPP_HIDE_FROM_ABI basic_format_context<_OutIt, _CharT>
+__format_context_create(
+ _OutIt __out_it,
+ basic_format_args<basic_format_context<_OutIt, _CharT>> __args) {
+ return _VSTD::basic_format_context(_VSTD::move(__out_it), __args);
+}
+#endif
+
+template <class _OutIt, class _CharT>
+requires output_iterator<_OutIt, const _CharT&>
+class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_context {
+public:
+ using iterator = _OutIt;
+ using char_type = _CharT;
+ template <class _Tp>
+ using formatter_type = formatter<_Tp, _CharT>;
+
+ basic_format_context(const basic_format_context&) = delete;
+ basic_format_context& operator=(const basic_format_context&) = delete;
+
+ _LIBCPP_HIDE_FROM_ABI basic_format_arg<basic_format_context>
+ arg(size_t __id) const {
+ return __args_.get(__id);
+ }
+#ifndef _LIBCPP_HAS_NO_LOCALIZATION
+ _LIBCPP_HIDE_FROM_ABI _VSTD::locale locale() {
+ if (!__loc_)
+ __loc_ = _VSTD::locale{};
+ return *__loc_;
+ }
+#endif
+ _LIBCPP_HIDE_FROM_ABI iterator out() { return __out_it_; }
+ _LIBCPP_HIDE_FROM_ABI void advance_to(iterator __it) { __out_it_ = __it; }
+
+private:
+ iterator __out_it_;
+ basic_format_args<basic_format_context> __args_;
+#ifndef _LIBCPP_HAS_NO_LOCALIZATION
+
+ // The Standard doesn't specify how the locale is stored.
+ // [format.context]/6
+ // std::locale locale();
+ // Returns: The locale passed to the formatting function if the latter
+ // takes one, and std::locale() otherwise.
+ // This is done by storing the locale of the constructor in this optional. If
+ // locale() is called and the optional has no value the value will be created.
+ // This allows the implementation to lazily create the locale.
+ // TODO FMT Validate whether lazy creation is the best solution.
+ optional<_VSTD::locale> __loc_;
+
+ template <class __OutIt, class __CharT>
+ friend _LIBCPP_HIDE_FROM_ABI basic_format_context<__OutIt, __CharT>
+ _VSTD::__format_context_create(
+ __OutIt, basic_format_args<basic_format_context<__OutIt, __CharT>>,
+ optional<_VSTD::locale>&&);
+
+ // Note: the Standard doesn't specify the required constructors.
+ _LIBCPP_HIDE_FROM_ABI
+ explicit basic_format_context(_OutIt __out_it,
+ basic_format_args<basic_format_context> __args,
+ optional<_VSTD::locale>&& __loc)
+ : __out_it_(_VSTD::move(__out_it)), __args_(__args),
+ __loc_(_VSTD::move(__loc)) {}
+#else
+ template <class __OutIt, class __CharT>
+ friend _LIBCPP_HIDE_FROM_ABI basic_format_context<__OutIt, __CharT>
+ _VSTD::__format_context_create(
+ __OutIt, basic_format_args<basic_format_context<__OutIt, __CharT>>);
+
+ _LIBCPP_HIDE_FROM_ABI
+ explicit basic_format_context(_OutIt __out_it,
+ basic_format_args<basic_format_context> __args)
+ : __out_it_(_VSTD::move(__out_it)), __args_(__args) {}
+#endif
+};
+
+// TODO FMT Implement [format.context]/4
+// [Note 1: For a given type charT, implementations are encouraged to provide a
+// single instantiation of basic_format_context for appending to
+// basic_string<charT>, vector<charT>, or any other container with contiguous
+// storage by wrapping those in temporary objects with a uniform interface
+// (such as a span<charT>) and polymorphic reallocation. - end note]
+
+using format_context = basic_format_context<back_insert_iterator<string>, char>;
+using wformat_context =
+ basic_format_context<back_insert_iterator<wstring>, wchar_t>;
+
+#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+#endif //_LIBCPP_STD_VER > 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___FORMAT_FORMAT_CONTEXT_H
diff --git a/include/__format/format_fwd.h b/include/__format/format_fwd.h
new file mode 100644
index 0000000..7da30ae
--- /dev/null
+++ b/include/__format/format_fwd.h
@@ -0,0 +1,56 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FORMAT_FORMAT_FWD_H
+#define _LIBCPP___FORMAT_FORMAT_FWD_H
+
+#include <__availability>
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__utility/forward.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17
+
+// TODO FMT Remove this once we require compilers with proper C++20 support.
+// If the compiler has no concepts support, the format header will be disabled.
+// Without concepts support enable_if needs to be used and that too much effort
+// to support compilers with partial C++20 support.
+#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+template <class _Context>
+class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_arg;
+
+template <class _Context, class... _Args>
+struct _LIBCPP_TEMPLATE_VIS __format_arg_store;
+
+template <class _Ctx, class... _Args>
+_LIBCPP_HIDE_FROM_ABI __format_arg_store<_Ctx, _Args...>
+make_format_args(const _Args&...);
+
+template <class _Tp, class _CharT = char>
+struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter;
+
+#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+#endif //_LIBCPP_STD_VER > 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___FORMAT_FORMAT_FWD_H
diff --git a/include/concepts b/include/concepts
index bfa27dd..99dd85c 100644
--- a/include/concepts
+++ b/include/concepts
@@ -152,10 +152,28 @@
#include <__concepts/swappable.h>
#include <__concepts/totally_ordered.h>
#include <__config>
+#include <type_traits>
#include <version>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+// Concept helpers for the internal type traits for the fundamental types.
+
+template <class _Tp>
+concept __libcpp_unsigned_integer = __libcpp_is_unsigned_integer<_Tp>::value;
+template <class _Tp>
+concept __libcpp_signed_integer = __libcpp_is_signed_integer<_Tp>::value;
+template <class _Tp>
+concept __libcpp_floating_point = __libcpp_is_floating_point<_Tp>::value;
+
+#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+_LIBCPP_END_NAMESPACE_STD
+
#endif // _LIBCPP_CONCEPTS
diff --git a/include/format b/include/format
index 0f5f842..b320b75 100644
--- a/include/format
+++ b/include/format
@@ -13,6 +13,136 @@
/*
namespace std {
+ // [format.context], class template basic_format_context
+ template<class Out, class charT>
+ class basic_format_context {
+ basic_format_args<basic_format_context> args_; // exposition only
+ Out out_; // exposition only
+
+ public:
+ using iterator = Out;
+ using char_type = charT;
+ template<class T> using formatter_type = formatter<T, charT>;
+
+ basic_format_arg<basic_format_context> arg(size_t id) const;
+ std::locale locale();
+
+ iterator out();
+ void advance_to(iterator it);
+ };
+ using format_context = basic_format_context<unspecified, char>;
+ using wformat_context = basic_format_context<unspecified, wchar_t>;
+
+ // [format.args], class template basic_format_args
+ template<class Context>
+ class basic_format_args {
+ size_t size_; // exposition only
+ const basic_format_arg<Context>* data_; // exposition only
+
+ public:
+ basic_format_args() noexcept;
+
+ template<class... Args>
+ basic_format_args(const format-arg-store<Context, Args...>& store) noexcept;
+
+ basic_format_arg<Context> get(size_t i) const noexcept;
+ };
+ using format_args = basic_format_args<format_context>;
+ using wformat_args = basic_format_args<wformat_context>;
+
+
+ template<class Out, class charT>
+ using format_args_t = basic_format_args<basic_format_context<Out, charT>>;
+
+ // [format.parse.ctx], class template basic_format_parse_context
+ template<class charT>
+ class basic_format_parse_context {
+ public:
+ using char_type = charT;
+ using const_iterator = typename basic_string_view<charT>::const_iterator;
+ using iterator = const_iterator;
+
+ private:
+ iterator begin_; // exposition only
+ iterator end_; // exposition only
+ enum indexing { unknown, manual, automatic }; // exposition only
+ indexing indexing_; // exposition only
+ size_t next_arg_id_; // exposition only
+ size_t num_args_; // exposition only
+
+ public:
+ constexpr explicit basic_format_parse_context(basic_string_view<charT> fmt,
+ size_t num_args = 0) noexcept;
+ basic_format_parse_context(const basic_format_parse_context&) = delete;
+ basic_format_parse_context& operator=(const basic_format_parse_context&) = delete;
+
+ constexpr const_iterator begin() const noexcept;
+ constexpr const_iterator end() const noexcept;
+ constexpr void advance_to(const_iterator it);
+
+ constexpr size_t next_arg_id();
+ constexpr void check_arg_id(size_t id);
+ };
+ using format_parse_context = basic_format_parse_context<char>;
+ using wformat_parse_context = basic_format_parse_context<wchar_t>;
+
+ // [format.arguments], arguments
+ // [format.arg], class template basic_format_arg
+ template<class Context>
+ class basic_format_arg {
+ public:
+ class handle;
+
+ private:
+ using char_type = typename Context::char_type; // exposition only
+
+ variant<monostate, bool, char_type,
+ int, unsigned int, long long int, unsigned long long int,
+ float, double, long double,
+ const char_type*, basic_string_view<char_type>,
+ const void*, handle> value; // exposition only
+
+ template<class T> explicit basic_format_arg(const T& v) noexcept; // exposition only
+ explicit basic_format_arg(float n) noexcept; // exposition only
+ explicit basic_format_arg(double n) noexcept; // exposition only
+ explicit basic_format_arg(long double n) noexcept; // exposition only
+ explicit basic_format_arg(const char_type* s); // exposition only
+
+ template<class traits>
+ explicit basic_format_arg(
+ basic_string_view<char_type, traits> s) noexcept; // exposition only
+
+ template<class traits, class Allocator>
+ explicit basic_format_arg(
+ const basic_string<char_type, traits, Allocator>& s) noexcept; // exposition only
+
+ explicit basic_format_arg(nullptr_t) noexcept; // exposition only
+
+ template<class T>
+ explicit basic_format_arg(const T* p) noexcept; // exposition only
+
+ public:
+ basic_format_arg() noexcept;
+
+ explicit operator bool() const noexcept;
+ };
+
+ template<class Visitor, class Context>
+ see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
+
+ // [format.arg.store], class template format-arg-store
+ template<class Context, class... Args>
+ struct format-arg-store { // exposition only
+ array<basic_format_arg<Context>, sizeof...(Args)> args;
+ };
+
+ template<class Context = format_context, class... Args>
+ format-arg-store<Context, Args...>
+ make_format_args(const Args&... args);
+ template<class... Args>
+ format-arg-store<wformat_context, Args...>
+ make_wformat_args(const Args&... args);
+
// [format.error], class format_error
class format_error : public runtime_error {
public:
@@ -61,13 +191,61 @@
#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
#include <__config>
+#include <__format/format_arg.h>
+#include <__format/format_args.h>
+#include <__format/format_context.h>
#include <__format/format_error.h>
#include <__format/format_parse_context.h>
+#include <array>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-# pragma GCC system_header
+#pragma GCC system_header
#endif
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17
+
+// TODO FMT Remove this once we require compilers with proper C++20 support.
+// If the compiler has no concepts support, the format header will be disabled.
+// Without concepts support enable_if needs to be used and that too much effort
+// to support compilers with partial C++20 support.
+#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+// TODO FMT Evaluate which templates should be external templates. This
+// improves the efficiency of the header. However since the header is still
+// under heavy development and not all classes are stable it makes no sense
+// to do this optimization now.
+
+using format_args = basic_format_args<format_context>;
+using wformat_args = basic_format_args<wformat_context>;
+
+template <class _OutIt, class _CharT>
+using format_args_t = basic_format_args<basic_format_context<_OutIt, _CharT>>;
+
+template <class _Context, class... _Args>
+struct _LIBCPP_TEMPLATE_VIS __format_arg_store {
+ // TODO FMT Use a built-in array.
+ array<basic_format_arg<_Context>, sizeof...(_Args)> __args;
+};
+
+template <class _Context = format_context, class... _Args>
+_LIBCPP_HIDE_FROM_ABI __format_arg_store<_Context, _Args...>
+make_format_args(const _Args&... __args) {
+ return {basic_format_arg<_Context>(__args)...};
+}
+
+template <class... _Args>
+_LIBCPP_HIDE_FROM_ABI __format_arg_store<wformat_context, _Args...>
+make_wformat_args(const _Args&... __args) {
+ return _VSTD::make_format_args<wformat_context>(__args...);
+}
+
+#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
+#endif //_LIBCPP_STD_VER > 17
+
+_LIBCPP_END_NAMESPACE_STD
+
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
#endif // _LIBCPP_FORMAT
diff --git a/include/module.modulemap b/include/module.modulemap
index f0d6023..5ef0f9a 100644
--- a/include/module.modulemap
+++ b/include/module.modulemap
@@ -431,7 +431,15 @@
export *
module __format {
+ module format_arg { private header "__format/format_arg.h" }
+ module format_args { private header "__format/format_args.h" }
+ module format_context {
+ private header "__format/format_context.h"
+ export optional
+ export locale
+ }
module format_error { private header "__format/format_error.h" }
+ module format_fwd { private header "__format/format_fwd.h" }
module format_parse_context { private header "__format/format_parse_context.h" }
}
}
diff --git a/include/type_traits b/include/type_traits
index a1d1640..63f2d3a 100644
--- a/include/type_traits
+++ b/include/type_traits
@@ -790,6 +790,7 @@
#endif // __has_keyword(__is_integral)
// __libcpp_is_signed_integer, __libcpp_is_unsigned_integer
+// <concepts> implements __libcpp_signed_integer, __libcpp_unsigned_integer
// [basic.fundamental] defines five standard signed integer types;
// __int128_t is an extended signed integer type.
@@ -817,6 +818,7 @@
#endif
// is_floating_point
+// <concepts> implements __libcpp_floating_point
template <class _Tp> struct __libcpp_is_floating_point : public false_type {};
template <> struct __libcpp_is_floating_point<float> : public true_type {};