[flang] Fix crash in HLFIR generation (#118399)
[llvm-project.git] / libcxx / include / __format / formatter_pointer.h
blob4ef48c168d0d8b8e8c1cafc6e51f78da11010474
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FORMAT_FORMATTER_POINTER_H
11 #define _LIBCPP___FORMAT_FORMATTER_POINTER_H
13 #include <__config>
14 #include <__cstddef/nullptr_t.h>
15 #include <__format/concepts.h>
16 #include <__format/format_parse_context.h>
17 #include <__format/formatter.h>
18 #include <__format/formatter_integral.h>
19 #include <__format/formatter_output.h>
20 #include <__format/parser_std_format_spec.h>
21 #include <cstdint>
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 # pragma GCC system_header
25 #endif
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 #if _LIBCPP_STD_VER >= 20
31 template <__fmt_char_type _CharT>
32 struct _LIBCPP_TEMPLATE_VIS __formatter_pointer {
33 public:
34 template <class _ParseContext>
35 _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
36 typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_pointer);
37 __format_spec::__process_display_type_pointer(__parser_.__type_, "a pointer");
38 return __result;
41 template <class _FormatContext>
42 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const void* __ptr, _FormatContext& __ctx) const {
43 __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
44 __specs.__std_.__alternate_form_ = true;
45 __specs.__std_.__type_ =
46 __specs.__std_.__type_ == __format_spec::__type::__pointer_upper_case
47 ? __format_spec::__type::__hexadecimal_upper_case
48 : __format_spec::__type::__hexadecimal_lower_case;
50 return __formatter::__format_integer(reinterpret_cast<uintptr_t>(__ptr), __ctx, __specs);
53 __format_spec::__parser<_CharT> __parser_;
56 // [format.formatter.spec]/2.4
57 // For each charT, the pointer type specializations template<>
58 // - struct formatter<nullptr_t, charT>;
59 // - template<> struct formatter<void*, charT>;
60 // - template<> struct formatter<const void*, charT>;
61 template <__fmt_char_type _CharT>
62 struct _LIBCPP_TEMPLATE_VIS formatter<nullptr_t, _CharT> : public __formatter_pointer<_CharT> {};
63 template <__fmt_char_type _CharT>
64 struct _LIBCPP_TEMPLATE_VIS formatter<void*, _CharT> : public __formatter_pointer<_CharT> {};
65 template <__fmt_char_type _CharT>
66 struct _LIBCPP_TEMPLATE_VIS formatter<const void*, _CharT> : public __formatter_pointer<_CharT> {};
68 # if _LIBCPP_STD_VER >= 23
69 template <>
70 inline constexpr bool enable_nonlocking_formatter_optimization<nullptr_t> = true;
71 template <>
72 inline constexpr bool enable_nonlocking_formatter_optimization<void*> = true;
73 template <>
74 inline constexpr bool enable_nonlocking_formatter_optimization<const void*> = true;
75 # endif // _LIBCPP_STD_VER >= 23
76 #endif // _LIBCPP_STD_VER >= 20
78 _LIBCPP_END_NAMESPACE_STD
80 #endif // _LIBCPP___FORMAT_FORMATTER_POINTER_H