[libcxx-commits] [libcxxabi] [libc++] [libc++abi] Initialize exception directly in make_exception_ptr if __cxa_init_primary_exception is available in ABI-library (PR #65534)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 20 11:48:26 PDT 2023


https://github.com/itrofimow updated https://github.com/llvm/llvm-project/pull/65534

>From 48298b86a13736e37a052b59ad45cd15066fc2be Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 23 Aug 2023 17:05:14 +0300
Subject: [PATCH 01/39] [libcxx] [libcxxabi] don't throw+catch in
 std::make_exception_ptr, if possible

Differential Revision: https://reviews.llvm.org/D158620
---
 libcxx/include/__exception/exception_ptr.h    | 27 +++++++++++++++++++
 .../runtime/exception_pointer_cxxabi.ipp      | 18 +++++++++++++
 libcxxabi/include/cxxabi.h                    |  7 +++++
 libcxxabi/src/cxa_exception.cpp               | 25 ++++++++++-------
 4 files changed, 68 insertions(+), 9 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 970d8196724b7cb..1db0a24be228023 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -14,6 +14,8 @@
 #include <__memory/addressof.h>
 #include <cstddef>
 #include <cstdlib>
+#include <typeinfo>
+#include <type_traits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -26,6 +28,18 @@ namespace std { // purposefully not using versioning namespace
 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   void* __ptr_;
 
+  template <class _Ep>
+  static inline void __dest_thunk(void *__x) {
+	static_cast<_Ep*>(__x)->~_Ep();
+  }
+
+  static void *init_ex(size_t, std::type_info*, void (*)(void *)) _NOEXCEPT;
+  static void free_ex(void *) _NOEXCEPT;
+  static exception_ptr from_ex_ptr(void *__e) _NOEXCEPT;
+
+  template <class _Ep>
+  friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
+
 public:
   _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}
   _LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
@@ -51,11 +65,24 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
 template <class _Ep>
 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+# if defined(LIBCXX_BUILDING_LIBCXXABI)
+  using _Ep2 = typename decay<_Ep>::type;
+  void *__ex = exception_ptr::init_ex(
+    sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
+  try {
+    ::new (__ex) _Ep2(__e);
+    return exception_ptr::from_ex_ptr(__ex);
+  } catch (...) {
+    exception_ptr::free_ex(__ex);
+    return current_exception();
+  }
+# else
   try {
     throw __e;
   } catch (...) {
     return current_exception();
   }
+# endif
 #  else
   ((void)__e);
   std::abort();
diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index 33aa94502b73c9d..277c5b867f679a8 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -34,6 +34,24 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
     return *this;
 }
 
+void *exception_ptr::init_ex(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept {
+  void *__ex = __cxa_allocate_exception(size);
+  (void)__cxa_init_primary_exception(__ex, tinfo, dest);
+  return __ex;
+}
+
+void exception_ptr::free_ex(void *thrown_object) noexcept {
+  __cxa_free_exception(thrown_object);
+}
+
+exception_ptr exception_ptr::from_ex_ptr(void *__e) noexcept {
+    exception_ptr ptr;
+    ptr.__ptr_ = __e;
+    __cxa_increment_exception_refcount(ptr.__ptr_);
+
+    return ptr;
+}
+
 nested_exception::nested_exception() noexcept
     : __ptr_(current_exception())
 {
diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h
index 6f4f823bc273ff3..3fae04e35c39dbd 100644
--- a/libcxxabi/include/cxxabi.h
+++ b/libcxxabi/include/cxxabi.h
@@ -36,6 +36,9 @@ class type_info; // forward declaration
 
 // runtime routines use C calling conventions, but are in __cxxabiv1 namespace
 namespace __cxxabiv1 {
+
+struct __cxa_exception;
+
 extern "C"  {
 
 // 2.4.2 Allocating the Exception Object
@@ -43,6 +46,10 @@ extern _LIBCXXABI_FUNC_VIS void *
 __cxa_allocate_exception(size_t thrown_size) throw();
 extern _LIBCXXABI_FUNC_VIS void
 __cxa_free_exception(void *thrown_exception) throw();
+// This function is an llvm extension
+extern _LIBCXXABI_FUNC_VIS __cxa_exception *
+__cxa_init_primary_exception(void *object, std::type_info* tinfo,
+		void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) throw();
 
 // 2.4.3 Throwing the Exception Object
 extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void
diff --git a/libcxxabi/src/cxa_exception.cpp b/libcxxabi/src/cxa_exception.cpp
index f723ececfe3327a..d3c445f2c10ac8d 100644
--- a/libcxxabi/src/cxa_exception.cpp
+++ b/libcxxabi/src/cxa_exception.cpp
@@ -206,6 +206,20 @@ void __cxa_free_exception(void *thrown_object) throw() {
     __aligned_free_with_fallback((void *)raw_buffer);
 }
 
+__cxa_exception *__cxa_init_primary_exception(void *object, std::type_info* tinfo,
+		void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) throw() {
+    __cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
+    exception_header->referenceCount = 0;
+    exception_header->unexpectedHandler = std::get_unexpected();
+    exception_header->terminateHandler  = std::get_terminate();
+    exception_header->exceptionType = tinfo;
+    exception_header->exceptionDestructor = dest;
+    setOurExceptionClass(&exception_header->unwindHeader);
+    exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
+
+    return exception_header;
+}
+
 
 //  This function shall allocate a __cxa_dependent_exception and
 //  return a pointer to it. (Really to the object, not past its' end).
@@ -261,17 +275,10 @@ __cxa_throw(void *thrown_object, std::type_info *tinfo, void *(_LIBCXXABI_DTOR_F
 __cxa_throw(void *thrown_object, std::type_info *tinfo, void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) {
 #endif
     __cxa_eh_globals *globals = __cxa_get_globals();
-    __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object);
-
-    exception_header->unexpectedHandler = std::get_unexpected();
-    exception_header->terminateHandler  = std::get_terminate();
-    exception_header->exceptionType = tinfo;
-    exception_header->exceptionDestructor = dest;
-    setOurExceptionClass(&exception_header->unwindHeader);
-    exception_header->referenceCount = 1;  // This is a newly allocated exception, no need for thread safety.
     globals->uncaughtExceptions += 1;   // Not atomically, since globals are thread-local
 
-    exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
+    __cxa_exception *exception_header = __cxa_init_primary_exception(thrown_object, tinfo, dest);
+    exception_header->referenceCount = 1;  // This is a newly allocated exception, no need for thread safety.
 
 #if __has_feature(address_sanitizer)
     // Inform the ASan runtime that now might be a good time to clean stuff up.

>From 79a4995bcb51081f14a5294c584f6b2b4329fb6f Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 23 Aug 2023 17:53:38 +0300
Subject: [PATCH 02/39] + clang-format

---
 libcxx/include/__exception/exception_ptr.h | 22 +++++++++++-----------
 libcxxabi/include/cxxabi.h                 |  5 ++---
 libcxxabi/src/cxa_exception.cpp            | 13 ++++++-------
 3 files changed, 19 insertions(+), 21 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 1db0a24be228023..6ca4e9314f1bd14 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -14,8 +14,8 @@
 #include <__memory/addressof.h>
 #include <cstddef>
 #include <cstdlib>
-#include <typeinfo>
 #include <type_traits>
+#include <typeinfo>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -29,13 +29,13 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   void* __ptr_;
 
   template <class _Ep>
-  static inline void __dest_thunk(void *__x) {
-	static_cast<_Ep*>(__x)->~_Ep();
+  static inline void __dest_thunk(void* __x) {
+    static_cast<_Ep*>(__x)->~_Ep();
   }
 
-  static void *init_ex(size_t, std::type_info*, void (*)(void *)) _NOEXCEPT;
-  static void free_ex(void *) _NOEXCEPT;
-  static exception_ptr from_ex_ptr(void *__e) _NOEXCEPT;
+  static void* init_ex(size_t, std::type_info*, void (*)(void*)) _NOEXCEPT;
+  static void free_ex(void*) _NOEXCEPT;
+  static exception_ptr from_ex_ptr(void* __e) _NOEXCEPT;
 
   template <class _Ep>
   friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
@@ -65,10 +65,10 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
 template <class _Ep>
 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-# if defined(LIBCXX_BUILDING_LIBCXXABI)
+#    if defined(LIBCXX_BUILDING_LIBCXXABI)
   using _Ep2 = typename decay<_Ep>::type;
-  void *__ex = exception_ptr::init_ex(
-    sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
+  void* __ex =
+      exception_ptr::init_ex(sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
   try {
     ::new (__ex) _Ep2(__e);
     return exception_ptr::from_ex_ptr(__ex);
@@ -76,13 +76,13 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
     exception_ptr::free_ex(__ex);
     return current_exception();
   }
-# else
+#    else
   try {
     throw __e;
   } catch (...) {
     return current_exception();
   }
-# endif
+#    endif
 #  else
   ((void)__e);
   std::abort();
diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h
index 3fae04e35c39dbd..03ec7986e209126 100644
--- a/libcxxabi/include/cxxabi.h
+++ b/libcxxabi/include/cxxabi.h
@@ -47,9 +47,8 @@ __cxa_allocate_exception(size_t thrown_size) throw();
 extern _LIBCXXABI_FUNC_VIS void
 __cxa_free_exception(void *thrown_exception) throw();
 // This function is an llvm extension
-extern _LIBCXXABI_FUNC_VIS __cxa_exception *
-__cxa_init_primary_exception(void *object, std::type_info* tinfo,
-		void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) throw();
+extern _LIBCXXABI_FUNC_VIS __cxa_exception*
+__cxa_init_primary_exception(void* object, std::type_info* tinfo, void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
 
 // 2.4.3 Throwing the Exception Object
 extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void
diff --git a/libcxxabi/src/cxa_exception.cpp b/libcxxabi/src/cxa_exception.cpp
index d3c445f2c10ac8d..1e72d7f34f26a21 100644
--- a/libcxxabi/src/cxa_exception.cpp
+++ b/libcxxabi/src/cxa_exception.cpp
@@ -206,12 +206,12 @@ void __cxa_free_exception(void *thrown_object) throw() {
     __aligned_free_with_fallback((void *)raw_buffer);
 }
 
-__cxa_exception *__cxa_init_primary_exception(void *object, std::type_info* tinfo,
-		void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) throw() {
+__cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
+                                              void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
     __cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
     exception_header->referenceCount = 0;
     exception_header->unexpectedHandler = std::get_unexpected();
-    exception_header->terminateHandler  = std::get_terminate();
+    exception_header->terminateHandler = std::get_terminate();
     exception_header->exceptionType = tinfo;
     exception_header->exceptionDestructor = dest;
     setOurExceptionClass(&exception_header->unwindHeader);
@@ -220,7 +220,6 @@ __cxa_exception *__cxa_init_primary_exception(void *object, std::type_info* tinf
     return exception_header;
 }
 
-
 //  This function shall allocate a __cxa_dependent_exception and
 //  return a pointer to it. (Really to the object, not past its' end).
 //  Otherwise, it will work like __cxa_allocate_exception.
@@ -274,11 +273,11 @@ __cxa_throw(void *thrown_object, std::type_info *tinfo, void *(_LIBCXXABI_DTOR_F
 #else
 __cxa_throw(void *thrown_object, std::type_info *tinfo, void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) {
 #endif
-    __cxa_eh_globals *globals = __cxa_get_globals();
+    __cxa_eh_globals* globals = __cxa_get_globals();
     globals->uncaughtExceptions += 1;   // Not atomically, since globals are thread-local
 
-    __cxa_exception *exception_header = __cxa_init_primary_exception(thrown_object, tinfo, dest);
-    exception_header->referenceCount = 1;  // This is a newly allocated exception, no need for thread safety.
+    __cxa_exception* exception_header = __cxa_init_primary_exception(thrown_object, tinfo, dest);
+    exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
 
 #if __has_feature(address_sanitizer)
     // Inform the ASan runtime that now might be a good time to clean stuff up.

>From 9f41ef23b46cb60992fbfc7c63e81d33710110a8 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 23 Aug 2023 20:18:45 +0300
Subject: [PATCH 03/39] cleanup

---
 libcxx/include/CMakeLists.txt                 |   1 +
 libcxx/include/__exception/exception_ptr.h    |  18 +-
 libcxx/include/__typeinfo/typeinfo.h          | 333 ++++++++++++++++++
 libcxx/include/module.modulemap.in            |   2 +
 libcxx/include/typeinfo                       |   9 +-
 .../runtime/exception_pointer_cxxabi.ipp      |  21 +-
 6 files changed, 361 insertions(+), 23 deletions(-)
 create mode 100644 libcxx/include/__typeinfo/typeinfo.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 9b03430a87d8338..1407c7a84a742b3 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -694,6 +694,7 @@ set(files
   __tuple/tuple_like_ext.h
   __tuple/tuple_size.h
   __tuple/tuple_types.h
+  __typeinfo/typeinfo.h
   __type_traits/add_const.h
   __type_traits/add_cv.h
   __type_traits/add_lvalue_reference.h
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 6ca4e9314f1bd14..0e5310c767cbe92 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -12,10 +12,10 @@
 #include <__config>
 #include <__exception/operations.h>
 #include <__memory/addressof.h>
+#include <__typeinfo/typeinfo.h>
 #include <cstddef>
 #include <cstdlib>
 #include <type_traits>
-#include <typeinfo>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -28,17 +28,19 @@ namespace std { // purposefully not using versioning namespace
 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   void* __ptr_;
 
+#  if defined(LIBCXX_BUILDING_LIBCXXABI)
   template <class _Ep>
   static inline void __dest_thunk(void* __x) {
     static_cast<_Ep*>(__x)->~_Ep();
   }
 
-  static void* init_ex(size_t, std::type_info*, void (*)(void*)) _NOEXCEPT;
-  static void free_ex(void*) _NOEXCEPT;
-  static exception_ptr from_ex_ptr(void* __e) _NOEXCEPT;
+  static void* __init_native_exception(size_t, std::type_info*, void (*)(void*)) _NOEXCEPT;
+  static void __free_native_exception(void*) _NOEXCEPT;
+  static exception_ptr __from_native_exception_pointer(void* __e) _NOEXCEPT;
 
   template <class _Ep>
   friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
+#  endif
 
 public:
   _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}
@@ -67,13 +69,13 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
 #    if defined(LIBCXX_BUILDING_LIBCXXABI)
   using _Ep2 = typename decay<_Ep>::type;
-  void* __ex =
-      exception_ptr::init_ex(sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
+  void* __ex = exception_ptr::__init_native_exception(
+      sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
   try {
     ::new (__ex) _Ep2(__e);
-    return exception_ptr::from_ex_ptr(__ex);
+    return exception_ptr::__from_native_exception_pointer(__ex);
   } catch (...) {
-    exception_ptr::free_ex(__ex);
+    exception_ptr::__free_native_exception(__ex);
     return current_exception();
   }
 #    else
diff --git a/libcxx/include/__typeinfo/typeinfo.h b/libcxx/include/__typeinfo/typeinfo.h
new file mode 100644
index 000000000000000..64b50711bbfd4e4
--- /dev/null
+++ b/libcxx/include/__typeinfo/typeinfo.h
@@ -0,0 +1,333 @@
+// -*- 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___TYPEINFO_TYPEINFO_H
+#define _LIBCPP___TYPEINFO_TYPEINFO_H
+
+#include <__availability>
+#include <__config>
+#include <__exception/exception.h>
+#include <__type_traits/is_constant_evaluated.h>
+#include <__verbose_abort>
+#include <cstddef>
+#include <cstdint>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+#if defined(_LIBCPP_ABI_VCRUNTIME)
+#  include <vcruntime_typeinfo.h>
+#else
+
+namespace std // purposefully not using versioning namespace
+{
+
+#  if defined(_LIBCPP_ABI_MICROSOFT)
+
+class _LIBCPP_EXPORTED_FROM_ABI type_info {
+  type_info& operator=(const type_info&);
+  type_info(const type_info&);
+
+  mutable struct {
+    const char* __undecorated_name;
+    const char __decorated_name[1];
+  } __data;
+
+  int __compare(const type_info& __rhs) const _NOEXCEPT;
+
+public:
+  _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
+  virtual ~type_info();
+
+  const char* name() const _NOEXCEPT;
+
+  _LIBCPP_INLINE_VISIBILITY bool before(const type_info& __arg) const _NOEXCEPT { return __compare(__arg) < 0; }
+
+  size_t hash_code() const _NOEXCEPT;
+
+  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
+    // When evaluated in a constant expression, both type infos simply can't come
+    // from different translation units, so it is sufficient to compare their addresses.
+    if (__libcpp_is_constant_evaluated()) {
+      return this == &__arg;
+    }
+    return __compare(__arg) == 0;
+  }
+
+#    if _LIBCPP_STD_VER <= 17
+  _LIBCPP_INLINE_VISIBILITY bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); }
+#    endif
+};
+
+#  else // !defined(_LIBCPP_ABI_MICROSOFT)
+
+// ========================================================================== //
+//                           Implementations
+// ========================================================================== //
+// ------------------------------------------------------------------------- //
+//                               Unique
+//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
+// ------------------------------------------------------------------------- //
+// This implementation of type_info assumes a unique copy of the RTTI for a
+// given type inside a program. This is a valid assumption when abiding to the
+// Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
+// Under this assumption, we can always compare the addresses of the type names
+// to implement equality-comparison of type_infos instead of having to perform
+// a deep string comparison.
+// -------------------------------------------------------------------------- //
+//                             NonUnique
+//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
+// -------------------------------------------------------------------------- //
+// This implementation of type_info does not assume there is always a unique
+// copy of the RTTI for a given type inside a program. For various reasons
+// the linker may have failed to merge every copy of a types RTTI
+// (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
+// type_infos are equal if their addresses are equal or if a deep string
+// comparison is equal.
+// -------------------------------------------------------------------------- //
+//                          NonUniqueARMRTTIBit
+//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
+// -------------------------------------------------------------------------- //
+// This implementation is specific to ARM64 on Apple platforms.
+//
+// This implementation of type_info does not assume always a unique copy of
+// the RTTI for a given type inside a program. When constructing the type_info,
+// the compiler packs the pointer to the type name into a uintptr_t and reserves
+// the high bit of that pointer, which is assumed to be free for use under that
+// ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
+// to be unique within the program. If the high bit is unset, then the RTTI can
+// be assumed to be unique within the program.
+//
+// When comparing type_infos, if both RTTIs can be assumed to be unique, it
+// suffices to compare their addresses. If both the RTTIs can't be assumed to
+// be unique, we must perform a deep string comparison of the type names.
+// However, if one of the RTTIs is guaranteed unique and the other one isn't,
+// then both RTTIs are necessarily not to be considered equal.
+//
+// The intent of this design is to remove the need for weak symbols. Specifically,
+// if a type would normally have a default-visibility RTTI emitted as a weak
+// symbol, it is given hidden visibility instead and the non-unique bit is set.
+// Otherwise, types declared with hidden visibility are always considered to have
+// a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
+// to be deduplicated by the linker within the linked image. Across linked image
+// boundaries, such types are thus considered different types.
+
+// This value can be overriden in the __config_site. When it's not overriden,
+// we pick a default implementation based on the platform here.
+#    ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
+
+// Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation.
+#      if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)
+#        define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
+
+// On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
+#      elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
+#        define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
+
+// On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
+#      else
+#        define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
+#      endif
+#    endif
+
+struct __type_info_implementations {
+  struct __string_impl_base {
+    typedef const char* __type_name_t;
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR static const char*
+    __type_name_to_string(__type_name_t __v) _NOEXCEPT {
+      return __v;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR static __type_name_t
+    __string_to_type_name(const char* __v) _NOEXCEPT {
+      return __v;
+    }
+  };
+
+  struct __unique_impl : __string_impl_base {
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT {
+      return reinterpret_cast<size_t>(__v);
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
+    __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      return __lhs == __rhs;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
+    __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      return __lhs < __rhs;
+    }
+  };
+
+  struct __non_unique_impl : __string_impl_base {
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
+      size_t __hash = 5381;
+      while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
+        __hash = (__hash * 33) ^ __c;
+      return __hash;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
+    __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
+    __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      return __builtin_strcmp(__lhs, __rhs) < 0;
+    }
+  };
+
+  struct __non_unique_arm_rtti_bit_impl {
+    typedef uintptr_t __type_name_t;
+
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static const char*
+    __type_name_to_string(__type_name_t __v) _NOEXCEPT {
+      return reinterpret_cast<const char*>(__v & ~__non_unique_rtti_bit::value);
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static __type_name_t
+    __string_to_type_name(const char* __v) _NOEXCEPT {
+      return reinterpret_cast<__type_name_t>(__v);
+    }
+
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT {
+      if (__is_type_name_unique(__v))
+        return __v;
+      return __non_unique_impl::__hash(__type_name_to_string(__v));
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
+    __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      if (__lhs == __rhs)
+        return true;
+      if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
+        // Either both are unique and have a different address, or one of them
+        // is unique and the other one isn't. In both cases they are unequal.
+        return false;
+      return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
+    __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
+        return __lhs < __rhs;
+      return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
+    }
+
+  private:
+    // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
+    // this implementation is actually used.
+    typedef integral_constant<__type_name_t, (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))>
+        __non_unique_rtti_bit;
+
+    _LIBCPP_INLINE_VISIBILITY static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
+      return !(__lhs & __non_unique_rtti_bit::value);
+    }
+  };
+
+  typedef
+#    if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
+      __unique_impl
+#    elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
+      __non_unique_impl
+#    elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
+      __non_unique_arm_rtti_bit_impl
+#    else
+#      error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
+#    endif
+          __impl;
+};
+
+class _LIBCPP_EXPORTED_FROM_ABI type_info {
+  type_info& operator=(const type_info&);
+  type_info(const type_info&);
+
+protected:
+  typedef __type_info_implementations::__impl __impl;
+
+  __impl::__type_name_t __type_name;
+
+  _LIBCPP_INLINE_VISIBILITY explicit type_info(const char* __n) : __type_name(__impl::__string_to_type_name(__n)) {}
+
+public:
+  _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
+  virtual ~type_info();
+
+  _LIBCPP_INLINE_VISIBILITY const char* name() const _NOEXCEPT { return __impl::__type_name_to_string(__type_name); }
+
+  _LIBCPP_INLINE_VISIBILITY bool before(const type_info& __arg) const _NOEXCEPT {
+    return __impl::__lt(__type_name, __arg.__type_name);
+  }
+
+  _LIBCPP_INLINE_VISIBILITY size_t hash_code() const _NOEXCEPT { return __impl::__hash(__type_name); }
+
+  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
+    // When evaluated in a constant expression, both type infos simply can't come
+    // from different translation units, so it is sufficient to compare their addresses.
+    if (__libcpp_is_constant_evaluated()) {
+      return this == &__arg;
+    }
+    return __impl::__eq(__type_name, __arg.__type_name);
+  }
+
+#    if _LIBCPP_STD_VER <= 17
+  _LIBCPP_INLINE_VISIBILITY bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); }
+#    endif
+};
+#  endif // defined(_LIBCPP_ABI_MICROSOFT)
+
+class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception {
+public:
+  bad_cast() _NOEXCEPT;
+  _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
+  ~bad_cast() _NOEXCEPT override;
+  const char* what() const _NOEXCEPT override;
+};
+
+class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
+public:
+  bad_typeid() _NOEXCEPT;
+  ~bad_typeid() _NOEXCEPT override;
+  const char* what() const _NOEXCEPT override;
+};
+
+} // namespace std
+
+#endif   // defined(_LIBCPP_ABI_VCRUNTIME)
+
+#if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
+
+namespace std {
+
+class bad_cast : public exception {
+public:
+  bad_cast() _NOEXCEPT : exception("bad cast") {}
+
+private:
+  bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {}
+};
+
+class bad_typeid : public exception {
+public:
+  bad_typeid() _NOEXCEPT : exception("bad typeid") {}
+
+private:
+  bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {}
+};
+
+} // namespace std
+
+#endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY void __throw_bad_cast() {
+#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+  throw bad_cast();
+#else
+  _LIBCPP_VERBOSE_ABORT("bad_cast was thrown in -fno-exceptions mode");
+#endif
+}
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___TYPEINFO_TYPEINFO_H
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 3e5a8a391b6e69c..44ec9f29cd3b603 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1267,6 +1267,8 @@ module std_private_debug_utils_strict_weak_ordering_check [system] {
   export std_private_type_traits_is_constant_evaluated
 }
 
+module std_private_typeinfo_typeinfo          [system] { header "__typeinfo/typeinfo.h" }
+
 module std_private_exception_exception        [system] { header "__exception/exception.h" }
 module std_private_exception_exception_ptr    [system] {
   header "__exception/exception_ptr.h"
diff --git a/libcxx/include/typeinfo b/libcxx/include/typeinfo
index 59bc291454c3d90..61872a1510b313d 100644
--- a/libcxx/include/typeinfo
+++ b/libcxx/include/typeinfo
@@ -57,18 +57,15 @@ public:
 */
 
 #include <__assert> // all public C++ headers provide the assertion handler
-#include <__availability>
 #include <__config>
-#include <__exception/exception.h>
-#include <__type_traits/is_constant_evaluated.h>
-#include <__verbose_abort>
-#include <cstddef>
-#include <cstdint>
+#include <__typeinfo/typeinfo.h>
+#include <version>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
+<<<<<<< HEAD
 #if defined(_LIBCPP_ABI_VCRUNTIME)
 #include <vcruntime_typeinfo.h>
 #else
diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index 277c5b867f679a8..f8ad08c11b736b9 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -13,8 +13,9 @@
 
 namespace std {
 
-exception_ptr::~exception_ptr() noexcept {
-  __cxa_decrement_exception_refcount(__ptr_);
+exception_ptr::~exception_ptr() noexcept
+{
+    __cxa_decrement_exception_refcount(__ptr_);
 }
 
 exception_ptr::exception_ptr(const exception_ptr& other) noexcept
@@ -34,17 +35,19 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
     return *this;
 }
 
-void *exception_ptr::init_ex(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept {
-  void *__ex = __cxa_allocate_exception(size);
-  (void)__cxa_init_primary_exception(__ex, tinfo, dest);
-  return __ex;
+void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept 
+{
+    void *__ex = __cxa_allocate_exception(size);
+    (void)__cxa_init_primary_exception(__ex, tinfo, dest);
+    return __ex;
 }
 
-void exception_ptr::free_ex(void *thrown_object) noexcept {
-  __cxa_free_exception(thrown_object);
+void exception_ptr::__free_native_exception(void *thrown_object) noexcept
+{
+    __cxa_free_exception(thrown_object);
 }
 
-exception_ptr exception_ptr::from_ex_ptr(void *__e) noexcept {
+exception_ptr exception_ptr::__from_native_exception_pointer(void *__e) noexcept {
     exception_ptr ptr;
     ptr.__ptr_ = __e;
     __cxa_increment_exception_refcount(ptr.__ptr_);

>From 91fc2331964d4358941ba3c34fa551e4add09c07 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 23 Aug 2023 22:37:40 +0300
Subject: [PATCH 04/39] + clang-format, fix libcxx.imp

---
 libcxx/include/__typeinfo/typeinfo.h | 2 +-
 libcxx/include/libcxx.imp            | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__typeinfo/typeinfo.h b/libcxx/include/__typeinfo/typeinfo.h
index 64b50711bbfd4e4..ac3550be0842e54 100644
--- a/libcxx/include/__typeinfo/typeinfo.h
+++ b/libcxx/include/__typeinfo/typeinfo.h
@@ -294,7 +294,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
 
 } // namespace std
 
-#endif   // defined(_LIBCPP_ABI_VCRUNTIME)
+#endif // defined(_LIBCPP_ABI_VCRUNTIME)
 
 #if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
 
diff --git a/libcxx/include/libcxx.imp b/libcxx/include/libcxx.imp
index 3eb2aa849314824..f7e906e024f33a4 100644
--- a/libcxx/include/libcxx.imp
+++ b/libcxx/include/libcxx.imp
@@ -48,6 +48,7 @@
   { include: [ "@<__thread/.*>", "private", "<thread>", "public" ] },
   { include: [ "@<__tuple/.*>", "private", "<tuple>", "public" ] },
   { include: [ "@<__type_traits/.*>", "private", "<type_traits>", "public" ] },
+  { include: [ "@<__typeinfo/.*>", "private", "<typeinfo>", "public" ] },
   { include: [ "@<__utility/.*>", "private", "<utility>", "public" ] },
   { include: [ "@<__variant/.*>", "private", "<variant>", "public" ] },
 ]

>From e299d7053db565faaee9eee5cbd17f13dbf2e887 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Tue, 29 Aug 2023 14:36:53 +0300
Subject: [PATCH 05/39] wip on making the build green

---
 libcxx/include/CMakeLists.txt       | 2 +-
 libcxx/utils/data/ignore_format.txt | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 1407c7a84a742b3..482577069bd36ee 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -694,7 +694,6 @@ set(files
   __tuple/tuple_like_ext.h
   __tuple/tuple_size.h
   __tuple/tuple_types.h
-  __typeinfo/typeinfo.h
   __type_traits/add_const.h
   __type_traits/add_cv.h
   __type_traits/add_lvalue_reference.h
@@ -833,6 +832,7 @@ set(files
   __type_traits/underlying_type.h
   __type_traits/unwrap_ref.h
   __type_traits/void_t.h
+  __typeinfo/typeinfo.h
   __undef_macros
   __utility/as_const.h
   __utility/auto_cast.h
diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index 664e9f51e9ba7fd..3109f4884af1499 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -462,6 +462,7 @@ libcxx/include/__tuple/tuple_indices.h
 libcxx/include/__tuple/tuple_like_ext.h
 libcxx/include/__tuple/tuple_size.h
 libcxx/include/__tuple/tuple_types.h
+libcxx/include/__typeinfo/typeinfo.h
 libcxx/include/typeindex
 libcxx/include/typeinfo
 libcxx/include/uchar.h

>From 3b90bfa2475f25f0b0ce4aa2e0c563f7cae0f8a1 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Tue, 29 Aug 2023 19:25:34 +0300
Subject: [PATCH 06/39] wip on making the build green

---
 libcxx/include/module.modulemap.in               | 5 ++++-
 libcxx/include/typeinfo                          | 1 -
 libcxx/test/libcxx/transitive_includes/cxx03.csv | 1 +
 libcxx/test/libcxx/transitive_includes/cxx11.csv | 1 +
 libcxx/test/libcxx/transitive_includes/cxx14.csv | 1 +
 libcxx/test/libcxx/transitive_includes/cxx17.csv | 1 +
 libcxx/test/libcxx/transitive_includes/cxx20.csv | 1 +
 libcxx/test/libcxx/transitive_includes/cxx23.csv | 3 +++
 libcxx/test/libcxx/transitive_includes/cxx26.csv | 3 +++
 9 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 44ec9f29cd3b603..9c96b84b6263020 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1267,7 +1267,10 @@ module std_private_debug_utils_strict_weak_ordering_check [system] {
   export std_private_type_traits_is_constant_evaluated
 }
 
-module std_private_typeinfo_typeinfo          [system] { header "__typeinfo/typeinfo.h" }
+module std_private_typeinfo_typeinfo          [system] { 
+  header "__typeinfo/typeinfo.h" 
+  export *
+}
 
 module std_private_exception_exception        [system] { header "__exception/exception.h" }
 module std_private_exception_exception_ptr    [system] {
diff --git a/libcxx/include/typeinfo b/libcxx/include/typeinfo
index 61872a1510b313d..3a9087f7752fc17 100644
--- a/libcxx/include/typeinfo
+++ b/libcxx/include/typeinfo
@@ -59,7 +59,6 @@ public:
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__config>
 #include <__typeinfo/typeinfo.h>
-#include <version>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
diff --git a/libcxx/test/libcxx/transitive_includes/cxx03.csv b/libcxx/test/libcxx/transitive_includes/cxx03.csv
index f6aeb837a7292f6..04cc0354714493d 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx03.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx03.csv
@@ -219,6 +219,7 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
+exception cstdint
 exception cstdlib
 exception type_traits
 exception version
diff --git a/libcxx/test/libcxx/transitive_includes/cxx11.csv b/libcxx/test/libcxx/transitive_includes/cxx11.csv
index 08fd94393f47320..47048bef817749c 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx11.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx11.csv
@@ -220,6 +220,7 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
+exception cstdint
 exception cstdlib
 exception type_traits
 exception version
diff --git a/libcxx/test/libcxx/transitive_includes/cxx14.csv b/libcxx/test/libcxx/transitive_includes/cxx14.csv
index 33384a628e65c01..d7c12b65941b5e6 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx14.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx14.csv
@@ -220,6 +220,7 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
+exception cstdint
 exception cstdlib
 exception type_traits
 exception version
diff --git a/libcxx/test/libcxx/transitive_includes/cxx17.csv b/libcxx/test/libcxx/transitive_includes/cxx17.csv
index 33384a628e65c01..d7c12b65941b5e6 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx17.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx17.csv
@@ -220,6 +220,7 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
+exception cstdint
 exception cstdlib
 exception type_traits
 exception version
diff --git a/libcxx/test/libcxx/transitive_includes/cxx20.csv b/libcxx/test/libcxx/transitive_includes/cxx20.csv
index 8f6e8dd646df583..c7189e616402f73 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx20.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx20.csv
@@ -226,6 +226,7 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
+exception cstdint
 exception cstdlib
 exception type_traits
 exception version
diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index d0d858056b1b00d..408559119c3a02e 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -157,7 +157,9 @@ deque stdexcept
 deque tuple
 deque version
 exception cstddef
+exception cstdint
 exception cstdlib
+exception type_traits
 exception version
 execution cstddef
 execution version
@@ -289,6 +291,7 @@ future ratio
 future stdexcept
 future string
 future thread
+future type_traits
 future typeinfo
 future version
 initializer_list cstddef
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index d0d858056b1b00d..408559119c3a02e 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -157,7 +157,9 @@ deque stdexcept
 deque tuple
 deque version
 exception cstddef
+exception cstdint
 exception cstdlib
+exception type_traits
 exception version
 execution cstddef
 execution version
@@ -289,6 +291,7 @@ future ratio
 future stdexcept
 future string
 future thread
+future type_traits
 future typeinfo
 future version
 initializer_list cstddef

>From 95cd0bdfa88003f8b6d0f69d21e05fc84819132a Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 30 Aug 2023 11:48:05 +0300
Subject: [PATCH 07/39] regenerate ignore_format.txt

---
 libcxx/utils/data/ignore_format.txt | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index 3109f4884af1499..0d62ab5f39b83eb 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -122,6 +122,7 @@ libcxx/include/__chrono/literals.h
 libcxx/include/__chrono/monthday.h
 libcxx/include/__chrono/month.h
 libcxx/include/__chrono/month_weekday.h
+libcxx/include/__chrono/statically_widen.h
 libcxx/include/__chrono/steady_clock.h
 libcxx/include/__chrono/system_clock.h
 libcxx/include/__chrono/time_point.h
@@ -144,7 +145,9 @@ libcxx/include/__compare/strong_order.h
 libcxx/include/__compare/three_way_comparable.h
 libcxx/include/__compare/weak_order.h
 libcxx/include/complex
+libcxx/include/__concepts/swappable.h
 libcxx/include/condition_variable
+libcxx/include/__config
 libcxx/include/__coroutine/coroutine_handle.h
 libcxx/include/__coroutine/coroutine_traits.h
 libcxx/include/__coroutine/noop_coroutine_handle.h
@@ -155,8 +158,10 @@ libcxx/include/cuchar
 libcxx/include/__debug_utils/randomize_range.h
 libcxx/include/deque
 libcxx/include/errno.h
+libcxx/include/__exception/exception_ptr.h
 libcxx/include/expected
 libcxx/include/__expected/expected.h
+libcxx/include/__expected/unexpected.h
 libcxx/include/experimental/__config
 libcxx/include/experimental/iterator
 libcxx/include/experimental/map
@@ -331,6 +336,7 @@ libcxx/include/mutex
 libcxx/include/new
 libcxx/include/__node_handle
 libcxx/include/numbers
+libcxx/include/numeric
 libcxx/include/__numeric/accumulate.h
 libcxx/include/__numeric/adjacent_difference.h
 libcxx/include/__numeric/exclusive_scan.h
@@ -346,6 +352,7 @@ libcxx/include/__numeric/transform_inclusive_scan.h
 libcxx/include/__numeric/transform_reduce.h
 libcxx/include/optional
 libcxx/include/ostream
+libcxx/include/print
 libcxx/include/queue
 libcxx/include/__random/bernoulli_distribution.h
 libcxx/include/__random/binomial_distribution.h
@@ -409,6 +416,7 @@ libcxx/include/__ranges/single_view.h
 libcxx/include/__ranges/size.h
 libcxx/include/__ranges/subrange.h
 libcxx/include/__ranges/take_view.h
+libcxx/include/__ranges/to.h
 libcxx/include/__ranges/transform_view.h
 libcxx/include/__ranges/view_interface.h
 libcxx/include/__ranges/views.h
@@ -446,8 +454,12 @@ libcxx/include/__support/newlib/xlocale.h
 libcxx/include/__support/win32/locale_win32.h
 libcxx/include/__support/xlocale/__nop_locale_mgmt.h
 libcxx/include/__system_error/errc.h
+libcxx/include/__system_error/error_category.h
+libcxx/include/__system_error/error_code.h
+libcxx/include/__system_error/error_condition.h
 libcxx/include/thread
 libcxx/include/__thread/formatter.h
+libcxx/include/__thread/id.h
 libcxx/include/__threading_support
 libcxx/include/__thread/poll_with_backoff.h
 libcxx/include/__thread/this_thread.h
@@ -462,9 +474,12 @@ libcxx/include/__tuple/tuple_indices.h
 libcxx/include/__tuple/tuple_like_ext.h
 libcxx/include/__tuple/tuple_size.h
 libcxx/include/__tuple/tuple_types.h
-libcxx/include/__typeinfo/typeinfo.h
 libcxx/include/typeindex
-libcxx/include/typeinfo
+libcxx/include/__typeinfo/typeinfo.h
+libcxx/include/type_traits
+libcxx/include/__type_traits/is_nothrow_copy_constructible.h
+libcxx/include/__type_traits/is_nothrow_move_constructible.h
+libcxx/include/__type_traits/promote.h
 libcxx/include/uchar.h
 libcxx/include/unordered_map
 libcxx/include/unordered_set
@@ -478,6 +493,7 @@ libcxx/include/__utility/pair.h
 libcxx/include/__utility/piecewise_construct.h
 libcxx/include/__utility/priority_tag.h
 libcxx/include/__utility/rel_ops.h
+libcxx/include/__utility/terminate_on_exception.h
 libcxx/include/__utility/to_underlying.h
 libcxx/include/__utility/unreachable.h
 libcxx/include/valarray
@@ -527,6 +543,7 @@ libcxx/src/memory.cpp
 libcxx/src/mutex.cpp
 libcxx/src/mutex_destructor.cpp
 libcxx/src/optional.cpp
+libcxx/src/print.cpp
 libcxx/src/pstl/libdispatch.cpp
 libcxx/src/random.cpp
 libcxx/src/random_shuffle.cpp

>From a3cc304fbe5c9e921f6e28f6606378e2b436c976 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 30 Aug 2023 13:13:14 +0300
Subject: [PATCH 08/39] bits of formatting

---
 libcxx/include/module.modulemap.in            |  4 ++--
 .../runtime/exception_pointer_cxxabi.ipp      |  2 +-
 libcxx/utils/data/ignore_format.txt           | 20 +------------------
 3 files changed, 4 insertions(+), 22 deletions(-)

diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 9c96b84b6263020..2a72758a2dd1ed7 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1267,8 +1267,8 @@ module std_private_debug_utils_strict_weak_ordering_check [system] {
   export std_private_type_traits_is_constant_evaluated
 }
 
-module std_private_typeinfo_typeinfo          [system] { 
-  header "__typeinfo/typeinfo.h" 
+module std_private_typeinfo_typeinfo          [system] {
+  header "__typeinfo/typeinfo.h"
   export *
 }
 
diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index f8ad08c11b736b9..93e08dba3cd9bd5 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -35,7 +35,7 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
     return *this;
 }
 
-void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept 
+void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
 {
     void *__ex = __cxa_allocate_exception(size);
     (void)__cxa_init_primary_exception(__ex, tinfo, dest);
diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index 0d62ab5f39b83eb..664e9f51e9ba7fd 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -122,7 +122,6 @@ libcxx/include/__chrono/literals.h
 libcxx/include/__chrono/monthday.h
 libcxx/include/__chrono/month.h
 libcxx/include/__chrono/month_weekday.h
-libcxx/include/__chrono/statically_widen.h
 libcxx/include/__chrono/steady_clock.h
 libcxx/include/__chrono/system_clock.h
 libcxx/include/__chrono/time_point.h
@@ -145,9 +144,7 @@ libcxx/include/__compare/strong_order.h
 libcxx/include/__compare/three_way_comparable.h
 libcxx/include/__compare/weak_order.h
 libcxx/include/complex
-libcxx/include/__concepts/swappable.h
 libcxx/include/condition_variable
-libcxx/include/__config
 libcxx/include/__coroutine/coroutine_handle.h
 libcxx/include/__coroutine/coroutine_traits.h
 libcxx/include/__coroutine/noop_coroutine_handle.h
@@ -158,10 +155,8 @@ libcxx/include/cuchar
 libcxx/include/__debug_utils/randomize_range.h
 libcxx/include/deque
 libcxx/include/errno.h
-libcxx/include/__exception/exception_ptr.h
 libcxx/include/expected
 libcxx/include/__expected/expected.h
-libcxx/include/__expected/unexpected.h
 libcxx/include/experimental/__config
 libcxx/include/experimental/iterator
 libcxx/include/experimental/map
@@ -336,7 +331,6 @@ libcxx/include/mutex
 libcxx/include/new
 libcxx/include/__node_handle
 libcxx/include/numbers
-libcxx/include/numeric
 libcxx/include/__numeric/accumulate.h
 libcxx/include/__numeric/adjacent_difference.h
 libcxx/include/__numeric/exclusive_scan.h
@@ -352,7 +346,6 @@ libcxx/include/__numeric/transform_inclusive_scan.h
 libcxx/include/__numeric/transform_reduce.h
 libcxx/include/optional
 libcxx/include/ostream
-libcxx/include/print
 libcxx/include/queue
 libcxx/include/__random/bernoulli_distribution.h
 libcxx/include/__random/binomial_distribution.h
@@ -416,7 +409,6 @@ libcxx/include/__ranges/single_view.h
 libcxx/include/__ranges/size.h
 libcxx/include/__ranges/subrange.h
 libcxx/include/__ranges/take_view.h
-libcxx/include/__ranges/to.h
 libcxx/include/__ranges/transform_view.h
 libcxx/include/__ranges/view_interface.h
 libcxx/include/__ranges/views.h
@@ -454,12 +446,8 @@ libcxx/include/__support/newlib/xlocale.h
 libcxx/include/__support/win32/locale_win32.h
 libcxx/include/__support/xlocale/__nop_locale_mgmt.h
 libcxx/include/__system_error/errc.h
-libcxx/include/__system_error/error_category.h
-libcxx/include/__system_error/error_code.h
-libcxx/include/__system_error/error_condition.h
 libcxx/include/thread
 libcxx/include/__thread/formatter.h
-libcxx/include/__thread/id.h
 libcxx/include/__threading_support
 libcxx/include/__thread/poll_with_backoff.h
 libcxx/include/__thread/this_thread.h
@@ -475,11 +463,7 @@ libcxx/include/__tuple/tuple_like_ext.h
 libcxx/include/__tuple/tuple_size.h
 libcxx/include/__tuple/tuple_types.h
 libcxx/include/typeindex
-libcxx/include/__typeinfo/typeinfo.h
-libcxx/include/type_traits
-libcxx/include/__type_traits/is_nothrow_copy_constructible.h
-libcxx/include/__type_traits/is_nothrow_move_constructible.h
-libcxx/include/__type_traits/promote.h
+libcxx/include/typeinfo
 libcxx/include/uchar.h
 libcxx/include/unordered_map
 libcxx/include/unordered_set
@@ -493,7 +477,6 @@ libcxx/include/__utility/pair.h
 libcxx/include/__utility/piecewise_construct.h
 libcxx/include/__utility/priority_tag.h
 libcxx/include/__utility/rel_ops.h
-libcxx/include/__utility/terminate_on_exception.h
 libcxx/include/__utility/to_underlying.h
 libcxx/include/__utility/unreachable.h
 libcxx/include/valarray
@@ -543,7 +526,6 @@ libcxx/src/memory.cpp
 libcxx/src/mutex.cpp
 libcxx/src/mutex_destructor.cpp
 libcxx/src/optional.cpp
-libcxx/src/print.cpp
 libcxx/src/pstl/libdispatch.cpp
 libcxx/src/random.cpp
 libcxx/src/random_shuffle.cpp

>From 10176726b2c5772136c861cf73c8234112a8e89f Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 30 Aug 2023 14:42:45 +0300
Subject: [PATCH 09/39] wip on making the build green

---
 libcxx/utils/data/ignore_format.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index 664e9f51e9ba7fd..b633c606c1201e2 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -462,8 +462,8 @@ libcxx/include/__tuple/tuple_indices.h
 libcxx/include/__tuple/tuple_like_ext.h
 libcxx/include/__tuple/tuple_size.h
 libcxx/include/__tuple/tuple_types.h
+libcxx/include/__typeinfo/typeinfo.h
 libcxx/include/typeindex
-libcxx/include/typeinfo
 libcxx/include/uchar.h
 libcxx/include/unordered_map
 libcxx/include/unordered_set

>From 772c2eeeee9016058bcf6c66baa900eb7755e5b1 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 30 Aug 2023 16:37:23 +0300
Subject: [PATCH 10/39] wip on making the build green

---
 libcxx/utils/data/ignore_format.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index b633c606c1201e2..be01186536cb506 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -462,7 +462,6 @@ libcxx/include/__tuple/tuple_indices.h
 libcxx/include/__tuple/tuple_like_ext.h
 libcxx/include/__tuple/tuple_size.h
 libcxx/include/__tuple/tuple_types.h
-libcxx/include/__typeinfo/typeinfo.h
 libcxx/include/typeindex
 libcxx/include/uchar.h
 libcxx/include/unordered_map

>From fb593cb3458464cbf32011f4ecf9d2b728138d21 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Thu, 7 Sep 2023 00:25:28 +0300
Subject: [PATCH 11/39] ABI fixes

---
 libcxx/include/__exception/exception_ptr.h | 6 +++---
 libcxxabi/lib/exceptions.exp               | 1 +
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 0e5310c767cbe92..bcb84d8bc06e59e 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -34,9 +34,9 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
     static_cast<_Ep*>(__x)->~_Ep();
   }
 
-  static void* __init_native_exception(size_t, std::type_info*, void (*)(void*)) _NOEXCEPT;
-  static void __free_native_exception(void*) _NOEXCEPT;
-  static exception_ptr __from_native_exception_pointer(void* __e) _NOEXCEPT;
+  _LIBCPP_HIDE_FROM_ABI static void* __init_native_exception(size_t, std::type_info*, void (*)(void*)) _NOEXCEPT;
+  _LIBCPP_HIDE_FROM_ABI static void __free_native_exception(void*) _NOEXCEPT;
+  _LIBCPP_HIDE_FROM_ABI static exception_ptr __from_native_exception_pointer(void* __e) _NOEXCEPT;
 
   template <class _Ep>
   friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
diff --git a/libcxxabi/lib/exceptions.exp b/libcxxabi/lib/exceptions.exp
index c3780d2974903ce..9dcfbdbd3598e12 100644
--- a/libcxxabi/lib/exceptions.exp
+++ b/libcxxabi/lib/exceptions.exp
@@ -7,5 +7,6 @@ ___cxa_end_catch
 ___cxa_free_dependent_exception
 ___cxa_free_exception
 ___cxa_get_exception_ptr
+___cxa_init_primary_exception
 ___cxa_rethrow
 ___cxa_throw

>From c80ce00083f750b564f37ffbf1a3e993def18701 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Mon, 25 Sep 2023 16:46:25 +0300
Subject: [PATCH 12/39] rebase fix

---
 libcxx/include/typeinfo | 348 ----------------------------------------
 1 file changed, 348 deletions(-)

diff --git a/libcxx/include/typeinfo b/libcxx/include/typeinfo
index 3a9087f7752fc17..e869cd5e6750787 100644
--- a/libcxx/include/typeinfo
+++ b/libcxx/include/typeinfo
@@ -64,354 +64,6 @@ public:
 #  pragma GCC system_header
 #endif
 
-<<<<<<< HEAD
-#if defined(_LIBCPP_ABI_VCRUNTIME)
-#include <vcruntime_typeinfo.h>
-#else
-
-namespace std  // purposefully not using versioning namespace
-{
-
-
-#if defined(_LIBCPP_ABI_MICROSOFT)
-
-class _LIBCPP_EXPORTED_FROM_ABI type_info
-{
-    type_info& operator=(const type_info&);
-    type_info(const type_info&);
-
-    mutable struct {
-      const char *__undecorated_name;
-      const char __decorated_name[1];
-    } __data;
-
-    int __compare(const type_info &__rhs) const _NOEXCEPT;
-
-public:
-    _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
-    virtual ~type_info();
-
-    const char *name() const _NOEXCEPT;
-
-    _LIBCPP_INLINE_VISIBILITY
-    bool before(const type_info& __arg) const _NOEXCEPT {
-      return __compare(__arg) < 0;
-    }
-
-    size_t hash_code() const _NOEXCEPT;
-
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
-    bool operator==(const type_info& __arg) const _NOEXCEPT {
-      // When evaluated in a constant expression, both type infos simply can't come
-      // from different translation units, so it is sufficient to compare their addresses.
-      if (__libcpp_is_constant_evaluated()) {
-        return this == &__arg;
-      }
-      return __compare(__arg) == 0;
-    }
-
-#if _LIBCPP_STD_VER <= 17
-    _LIBCPP_INLINE_VISIBILITY
-    bool operator!=(const type_info& __arg) const _NOEXCEPT
-    { return !operator==(__arg); }
-#endif
-};
-
-#else // !defined(_LIBCPP_ABI_MICROSOFT)
-
-// ========================================================================== //
-//                           Implementations
-// ========================================================================== //
-// ------------------------------------------------------------------------- //
-//                               Unique
-//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
-// ------------------------------------------------------------------------- //
-// This implementation of type_info assumes a unique copy of the RTTI for a
-// given type inside a program. This is a valid assumption when abiding to the
-// Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
-// Under this assumption, we can always compare the addresses of the type names
-// to implement equality-comparison of type_infos instead of having to perform
-// a deep string comparison.
-// -------------------------------------------------------------------------- //
-//                             NonUnique
-//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
-// -------------------------------------------------------------------------- //
-// This implementation of type_info does not assume there is always a unique
-// copy of the RTTI for a given type inside a program. For various reasons
-// the linker may have failed to merge every copy of a types RTTI
-// (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
-// type_infos are equal if their addresses are equal or if a deep string
-// comparison is equal.
-// -------------------------------------------------------------------------- //
-//                          NonUniqueARMRTTIBit
-//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
-// -------------------------------------------------------------------------- //
-// This implementation is specific to ARM64 on Apple platforms.
-//
-// This implementation of type_info does not assume always a unique copy of
-// the RTTI for a given type inside a program. When constructing the type_info,
-// the compiler packs the pointer to the type name into a uintptr_t and reserves
-// the high bit of that pointer, which is assumed to be free for use under that
-// ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
-// to be unique within the program. If the high bit is unset, then the RTTI can
-// be assumed to be unique within the program.
-//
-// When comparing type_infos, if both RTTIs can be assumed to be unique, it
-// suffices to compare their addresses. If both the RTTIs can't be assumed to
-// be unique, we must perform a deep string comparison of the type names.
-// However, if one of the RTTIs is guaranteed unique and the other one isn't,
-// then both RTTIs are necessarily not to be considered equal.
-//
-// The intent of this design is to remove the need for weak symbols. Specifically,
-// if a type would normally have a default-visibility RTTI emitted as a weak
-// symbol, it is given hidden visibility instead and the non-unique bit is set.
-// Otherwise, types declared with hidden visibility are always considered to have
-// a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
-// to be deduplicated by the linker within the linked image. Across linked image
-// boundaries, such types are thus considered different types.
-
-// This value can be overriden in the __config_site. When it's not overriden,
-// we pick a default implementation based on the platform here.
-#ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
-
-  // Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation.
-# if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)
-#   define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
-
-  // On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
-# elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
-#   define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
-
-  // On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
-# else
-#   define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
-# endif
-#endif
-
-struct __type_info_implementations {
-  struct __string_impl_base {
-    typedef const char* __type_name_t;
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    _LIBCPP_CONSTEXPR static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
-      return __v;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    _LIBCPP_CONSTEXPR static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
-      return __v;
-    }
-  };
-
-  struct __unique_impl : __string_impl_base {
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static size_t __hash(__type_name_t __v) _NOEXCEPT {
-      return reinterpret_cast<size_t>(__v);
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      return __lhs == __rhs;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      return __lhs < __rhs;
-    }
-  };
-
-  struct __non_unique_impl : __string_impl_base {
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
-      size_t __hash = 5381;
-      while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
-        __hash = (__hash * 33) ^ __c;
-      return __hash;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      return __builtin_strcmp(__lhs, __rhs) < 0;
-    }
-  };
-
-  struct __non_unique_arm_rtti_bit_impl {
-    typedef uintptr_t __type_name_t;
-
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
-      return reinterpret_cast<const char*>(__v &
-          ~__non_unique_rtti_bit::value);
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
-      return reinterpret_cast<__type_name_t>(__v);
-    }
-
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static size_t __hash(__type_name_t __v) _NOEXCEPT {
-      if (__is_type_name_unique(__v))
-        return __v;
-      return __non_unique_impl::__hash(__type_name_to_string(__v));
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      if (__lhs == __rhs)
-        return true;
-      if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
-        // Either both are unique and have a different address, or one of them
-        // is unique and the other one isn't. In both cases they are unequal.
-        return false;
-      return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
-    static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
-        return __lhs < __rhs;
-      return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
-    }
-
-   private:
-    // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
-    // this implementation is actually used.
-    typedef integral_constant<__type_name_t,
-      (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))> __non_unique_rtti_bit;
-
-    _LIBCPP_INLINE_VISIBILITY
-    static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
-      return !(__lhs & __non_unique_rtti_bit::value);
-    }
-  };
-
-  typedef
-#if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
-    __unique_impl
-#elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
-    __non_unique_impl
-#elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
-    __non_unique_arm_rtti_bit_impl
-#else
-#   error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
-#endif
-     __impl;
-};
-
-class _LIBCPP_EXPORTED_FROM_ABI type_info
-{
-  type_info& operator=(const type_info&);
-  type_info(const type_info&);
-
- protected:
-    typedef __type_info_implementations::__impl __impl;
-
-    __impl::__type_name_t __type_name;
-
-    _LIBCPP_INLINE_VISIBILITY
-    explicit type_info(const char* __n)
-      : __type_name(__impl::__string_to_type_name(__n)) {}
-
-public:
-    _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
-    virtual ~type_info();
-
-    _LIBCPP_INLINE_VISIBILITY
-    const char* name() const _NOEXCEPT
-    {
-      return __impl::__type_name_to_string(__type_name);
-    }
-
-    _LIBCPP_INLINE_VISIBILITY
-    bool before(const type_info& __arg) const _NOEXCEPT
-    {
-      return __impl::__lt(__type_name, __arg.__type_name);
-    }
-
-    _LIBCPP_INLINE_VISIBILITY
-    size_t hash_code() const _NOEXCEPT
-    {
-      return __impl::__hash(__type_name);
-    }
-
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
-    bool operator==(const type_info& __arg) const _NOEXCEPT
-    {
-      // When evaluated in a constant expression, both type infos simply can't come
-      // from different translation units, so it is sufficient to compare their addresses.
-      if (__libcpp_is_constant_evaluated()) {
-        return this == &__arg;
-      }
-      return __impl::__eq(__type_name, __arg.__type_name);
-    }
-
-#if _LIBCPP_STD_VER <= 17
-    _LIBCPP_INLINE_VISIBILITY
-    bool operator!=(const type_info& __arg) const _NOEXCEPT
-    { return !operator==(__arg); }
-#endif
-};
-#endif // defined(_LIBCPP_ABI_MICROSOFT)
-
-class _LIBCPP_EXPORTED_FROM_ABI bad_cast
-    : public exception
-{
- public:
-  bad_cast() _NOEXCEPT;
-  _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
-  _LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
-  ~bad_cast() _NOEXCEPT override;
-  const char* what() const _NOEXCEPT override;
-};
-
-class _LIBCPP_EXPORTED_FROM_ABI bad_typeid
-    : public exception
-{
- public:
-  bad_typeid() _NOEXCEPT;
-  _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default;
-  _LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
-  ~bad_typeid() _NOEXCEPT override;
-  const char* what() const _NOEXCEPT override;
-};
-
-} // namespace std
-
-#endif // defined(_LIBCPP_ABI_VCRUNTIME)
-
-#if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
-
-namespace std {
-
-class bad_cast : public exception {
-public:
-  bad_cast() _NOEXCEPT : exception("bad cast") {}
-
-private:
-  bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {}
-};
-
-class bad_typeid : public exception {
-public:
-  bad_typeid() _NOEXCEPT : exception("bad typeid") {}
-
-private:
-  bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {}
-};
-
-} // namespace std
-
-#endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
-void __throw_bad_cast()
-{
-#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-    throw bad_cast();
-#else
-    _LIBCPP_VERBOSE_ABORT("bad_cast was thrown in -fno-exceptions mode");
-#endif
-}
-_LIBCPP_END_NAMESPACE_STD
-
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
 #  include <cstdlib>
 #  include <exception>

>From 1c26214f82d30e8d889ca9abfddcfeb00e43fe22 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 27 Sep 2023 17:25:23 +0300
Subject: [PATCH 13/39] rebase fix

---
 libcxx/include/__typeinfo/typeinfo.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libcxx/include/__typeinfo/typeinfo.h b/libcxx/include/__typeinfo/typeinfo.h
index ac3550be0842e54..394cac5967870aa 100644
--- a/libcxx/include/__typeinfo/typeinfo.h
+++ b/libcxx/include/__typeinfo/typeinfo.h
@@ -281,6 +281,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception {
 public:
   bad_cast() _NOEXCEPT;
   _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
+  _LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
   ~bad_cast() _NOEXCEPT override;
   const char* what() const _NOEXCEPT override;
 };
@@ -288,6 +289,8 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception {
 class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
 public:
   bad_typeid() _NOEXCEPT;
+  _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default;
+  _LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
   ~bad_typeid() _NOEXCEPT override;
   const char* what() const _NOEXCEPT override;
 };

>From d45e91eb5b687750df899a1b7f888a9cf371776a Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 27 Sep 2023 18:05:49 +0300
Subject: [PATCH 14/39] + clang-format

---
 libcxx/include/__typeinfo/typeinfo.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__typeinfo/typeinfo.h b/libcxx/include/__typeinfo/typeinfo.h
index 394cac5967870aa..07b53af2b478588 100644
--- a/libcxx/include/__typeinfo/typeinfo.h
+++ b/libcxx/include/__typeinfo/typeinfo.h
@@ -280,7 +280,7 @@ class _LIBCPP_EXPORTED_FROM_ABI type_info {
 class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception {
 public:
   bad_cast() _NOEXCEPT;
-  _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
+  _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT            = default;
   _LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
   ~bad_cast() _NOEXCEPT override;
   const char* what() const _NOEXCEPT override;
@@ -289,7 +289,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception {
 class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
 public:
   bad_typeid() _NOEXCEPT;
-  _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default;
+  _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT            = default;
   _LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
   ~bad_typeid() _NOEXCEPT override;
   const char* what() const _NOEXCEPT override;

>From f7f2ae3c3d612e89698cab9d12d10d778ed3be32 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Wed, 27 Sep 2023 23:55:46 +0300
Subject: [PATCH 15/39] + generate-cxx-abilist

---
 ...nknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist b/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
index 49e3579614ee8c1..478a615eb61cbce 100644
--- a/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
+++ b/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
@@ -54,6 +54,7 @@
 {'is_defined': False, 'name': '__cxa_guard_acquire', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_guard_release', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_increment_exception_refcount', 'type': 'FUNC'}
+{'is_defined': False, 'name': '__cxa_init_primary_exception', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_pure_virtual', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_rethrow', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_rethrow_primary_exception', 'type': 'FUNC'}

>From eab3aa447a366d5c6769edc9d119042e9a6aa56d Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Mon, 2 Oct 2023 16:29:26 +0300
Subject: [PATCH 16/39] fix compilcation under gcc

---
 libcxx/include/__exception/exception_ptr.h                  | 6 +++---
 ...n-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist | 3 +++
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index bcb84d8bc06e59e..0e5310c767cbe92 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -34,9 +34,9 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
     static_cast<_Ep*>(__x)->~_Ep();
   }
 
-  _LIBCPP_HIDE_FROM_ABI static void* __init_native_exception(size_t, std::type_info*, void (*)(void*)) _NOEXCEPT;
-  _LIBCPP_HIDE_FROM_ABI static void __free_native_exception(void*) _NOEXCEPT;
-  _LIBCPP_HIDE_FROM_ABI static exception_ptr __from_native_exception_pointer(void* __e) _NOEXCEPT;
+  static void* __init_native_exception(size_t, std::type_info*, void (*)(void*)) _NOEXCEPT;
+  static void __free_native_exception(void*) _NOEXCEPT;
+  static exception_ptr __from_native_exception_pointer(void* __e) _NOEXCEPT;
 
   template <class _Ep>
   friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
diff --git a/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist b/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
index 478a615eb61cbce..71c492deced3c6a 100644
--- a/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
+++ b/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
@@ -522,6 +522,9 @@
 {'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD0Ev', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD1Ev', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD2Ev', 'type': 'FUNC'}
+{'is_defined': True, 'name': '_ZNSt13exception_ptr23__free_native_exceptionEPv', 'type': 'FUNC'}
+{'is_defined': True, 'name': '_ZNSt13exception_ptr23__init_native_exceptionEmPSt9type_infoPFvPvE', 'type': 'FUNC'}
+{'is_defined': True, 'name': '_ZNSt13exception_ptr31__from_native_exception_pointerEPv', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt13exception_ptrC1ERKS_', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt13exception_ptrC2ERKS_', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt13exception_ptrD1Ev', 'type': 'FUNC'}

>From 2035f1135156fc02aed5ebc1cedf8a4a8a913323 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 6 Oct 2023 18:02:39 +0300
Subject: [PATCH 17/39] some more ifdefs

---
 libcxx/include/__exception/exception_ptr.h              | 4 +++-
 libcxx/src/support/runtime/exception_pointer_cxxabi.ipp | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 0e5310c767cbe92..ecc82d0ba905f54 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -28,7 +28,8 @@ namespace std { // purposefully not using versioning namespace
 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   void* __ptr_;
 
-#  if defined(LIBCXX_BUILDING_LIBCXXABI)
+#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+#    if defined(LIBCXX_BUILDING_LIBCXXABI)
   template <class _Ep>
   static inline void __dest_thunk(void* __x) {
     static_cast<_Ep*>(__x)->~_Ep();
@@ -41,6 +42,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   template <class _Ep>
   friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
 #  endif
+#  endif
 
 public:
   _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}
diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index 93e08dba3cd9bd5..d51031977e2dcb5 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -35,6 +35,7 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
     return *this;
 }
 
+#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
 void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
 {
     void *__ex = __cxa_allocate_exception(size);
@@ -54,6 +55,7 @@ exception_ptr exception_ptr::__from_native_exception_pointer(void *__e) noexcept
 
     return ptr;
 }
+#  endif
 
 nested_exception::nested_exception() noexcept
     : __ptr_(current_exception())

>From 81c5626f035183d48df898d58d5162fcd5ec57c9 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Mon, 9 Oct 2023 02:44:07 +0300
Subject: [PATCH 18/39] + clang-format

---
 libcxx/include/__exception/exception_ptr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index ecc82d0ba905f54..e435da65cc5d2b6 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -41,7 +41,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
 
   template <class _Ep>
   friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
-#  endif
+#    endif
 #  endif
 
 public:

>From 6e8e55daaa90742dd2249ccef767f3236bc26521 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Mon, 9 Oct 2023 03:20:25 +0300
Subject: [PATCH 19/39] + clang-format

---
 libcxx/include/__typeinfo/typeinfo.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__typeinfo/typeinfo.h b/libcxx/include/__typeinfo/typeinfo.h
index 07b53af2b478588..2af157a5bf76f64 100644
--- a/libcxx/include/__typeinfo/typeinfo.h
+++ b/libcxx/include/__typeinfo/typeinfo.h
@@ -297,7 +297,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
 
 } // namespace std
 
-#endif // defined(_LIBCPP_ABI_VCRUNTIME)
+#endif   // defined(_LIBCPP_ABI_VCRUNTIME)
 
 #if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
 

>From d633048ef7fbd55ab7ac5ba3d1d0cf929167325b Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Mon, 9 Oct 2023 03:37:52 +0300
Subject: [PATCH 20/39] + clang-format, 17.0.2

---
 libcxx/include/__typeinfo/typeinfo.h |  2 +-
 libcxxabi/src/cxa_exception.cpp      | 30 ++++++++++++++--------------
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/libcxx/include/__typeinfo/typeinfo.h b/libcxx/include/__typeinfo/typeinfo.h
index 2af157a5bf76f64..07b53af2b478588 100644
--- a/libcxx/include/__typeinfo/typeinfo.h
+++ b/libcxx/include/__typeinfo/typeinfo.h
@@ -297,7 +297,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
 
 } // namespace std
 
-#endif   // defined(_LIBCPP_ABI_VCRUNTIME)
+#endif // defined(_LIBCPP_ABI_VCRUNTIME)
 
 #if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
 
diff --git a/libcxxabi/src/cxa_exception.cpp b/libcxxabi/src/cxa_exception.cpp
index 1e72d7f34f26a21..65e9f4504ddade0 100644
--- a/libcxxabi/src/cxa_exception.cpp
+++ b/libcxxabi/src/cxa_exception.cpp
@@ -208,16 +208,16 @@ void __cxa_free_exception(void *thrown_object) throw() {
 
 __cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
                                               void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
-    __cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
-    exception_header->referenceCount = 0;
-    exception_header->unexpectedHandler = std::get_unexpected();
-    exception_header->terminateHandler = std::get_terminate();
-    exception_header->exceptionType = tinfo;
-    exception_header->exceptionDestructor = dest;
-    setOurExceptionClass(&exception_header->unwindHeader);
-    exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
+  __cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
+  exception_header->referenceCount = 0;
+  exception_header->unexpectedHandler = std::get_unexpected();
+  exception_header->terminateHandler = std::get_terminate();
+  exception_header->exceptionType = tinfo;
+  exception_header->exceptionDestructor = dest;
+  setOurExceptionClass(&exception_header->unwindHeader);
+  exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
 
-    return exception_header;
+  return exception_header;
 }
 
 //  This function shall allocate a __cxa_dependent_exception and
@@ -273,15 +273,15 @@ __cxa_throw(void *thrown_object, std::type_info *tinfo, void *(_LIBCXXABI_DTOR_F
 #else
 __cxa_throw(void *thrown_object, std::type_info *tinfo, void (_LIBCXXABI_DTOR_FUNC *dest)(void *)) {
 #endif
-    __cxa_eh_globals* globals = __cxa_get_globals();
-    globals->uncaughtExceptions += 1;   // Not atomically, since globals are thread-local
+  __cxa_eh_globals* globals = __cxa_get_globals();
+  globals->uncaughtExceptions += 1; // Not atomically, since globals are thread-local
 
-    __cxa_exception* exception_header = __cxa_init_primary_exception(thrown_object, tinfo, dest);
-    exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
+  __cxa_exception* exception_header = __cxa_init_primary_exception(thrown_object, tinfo, dest);
+  exception_header->referenceCount = 1; // This is a newly allocated exception, no need for thread safety.
 
 #if __has_feature(address_sanitizer)
-    // Inform the ASan runtime that now might be a good time to clean stuff up.
-    __asan_handle_no_return();
+  // Inform the ASan runtime that now might be a good time to clean stuff up.
+  __asan_handle_no_return();
 #endif
 
 #ifdef __USING_SJLJ_EXCEPTIONS__

>From 24ac98453b236a1ac929aa4d8cb1c97c65e958a4 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Thu, 12 Oct 2023 05:18:00 +0300
Subject: [PATCH 21/39] code review fixes

---
 libcxx/include/__exception/exception_ptr.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index e435da65cc5d2b6..5c14eab00b00980 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -12,10 +12,10 @@
 #include <__config>
 #include <__exception/operations.h>
 #include <__memory/addressof.h>
+#include <__type_traits/decay.h>
 #include <__typeinfo/typeinfo.h>
 #include <cstddef>
 #include <cstdlib>
-#include <type_traits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -70,7 +70,7 @@ template <class _Ep>
 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
 #    if defined(LIBCXX_BUILDING_LIBCXXABI)
-  using _Ep2 = typename decay<_Ep>::type;
+  using _Ep2 = __decay_t<_Ep>;
   void* __ex = exception_ptr::__init_native_exception(
       sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
   try {

>From b6adc58c4b217394ce4c2c012e1246424c93158f Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Thu, 12 Oct 2023 10:29:02 +0300
Subject: [PATCH 22/39] fix transitive_includes tests

---
 libcxx/test/libcxx/transitive_includes/cxx23.csv | 2 --
 libcxx/test/libcxx/transitive_includes/cxx26.csv | 2 --
 2 files changed, 4 deletions(-)

diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index 408559119c3a02e..64c292b69ce9f43 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -159,7 +159,6 @@ deque version
 exception cstddef
 exception cstdint
 exception cstdlib
-exception type_traits
 exception version
 execution cstddef
 execution version
@@ -291,7 +290,6 @@ future ratio
 future stdexcept
 future string
 future thread
-future type_traits
 future typeinfo
 future version
 initializer_list cstddef
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index 408559119c3a02e..64c292b69ce9f43 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -159,7 +159,6 @@ deque version
 exception cstddef
 exception cstdint
 exception cstdlib
-exception type_traits
 exception version
 execution cstddef
 execution version
@@ -291,7 +290,6 @@ future ratio
 future stdexcept
 future string
 future thread
-future type_traits
 future typeinfo
 future version
 initializer_list cstddef

>From 9c0ce0fc9af97f471cf4a721b031471cc27dba98 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sat, 14 Oct 2023 16:08:33 +0300
Subject: [PATCH 23/39] runtime lookup of __cxa_init_primary_exception

---
 libcxx/include/__exception/exception_ptr.h           |  8 ++++++++
 .../src/support/runtime/exception_pointer_cxxabi.ipp | 12 +++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 5c14eab00b00980..17894eeeed5818a 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -73,6 +73,14 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
   using _Ep2 = __decay_t<_Ep>;
   void* __ex = exception_ptr::__init_native_exception(
       sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
+  if (__ex == nullptr) {
+    try {
+      throw __e;
+    } catch (...) {
+      return current_exception();
+    }
+  }
+
   try {
     ::new (__ex) _Ep2(__e);
     return exception_ptr::__from_native_exception_pointer(__ex);
diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index d51031977e2dcb5..73aa30e6ca893ae 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -11,6 +11,8 @@
 #error this header may only be used with libc++abi or libcxxrt
 #endif
 
+#include <dlfcn.h>
+
 namespace std {
 
 exception_ptr::~exception_ptr() noexcept
@@ -38,8 +40,16 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
 void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
 {
+    using CxaInitPrimaryExceptionPrototype = void *(*)(void *, type_info *, void(*)(void *));
+    static CxaInitPrimaryExceptionPrototype cxa_init_primary_exception_fn = reinterpret_cast<CxaInitPrimaryExceptionPrototype>(
+        dlsym(RTLD_DEFAULT, "__cxa_init_primary_exception"));
+
+    if (cxa_init_primary_exception_fn == nullptr) {
+        return nullptr;
+    }
+
     void *__ex = __cxa_allocate_exception(size);
-    (void)__cxa_init_primary_exception(__ex, tinfo, dest);
+    (void)cxa_init_primary_exception_fn(__ex, tinfo, dest);
     return __ex;
 }
 

>From d0fa02cf2e0119dec9652fc336dbc829cc009e2e Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sat, 14 Oct 2023 18:23:46 +0300
Subject: [PATCH 24/39] regenerate cxx-abilist

---
 ...nknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist b/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
index 71c492deced3c6a..a391ac9be3c5130 100644
--- a/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
+++ b/libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist
@@ -54,7 +54,6 @@
 {'is_defined': False, 'name': '__cxa_guard_acquire', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_guard_release', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_increment_exception_refcount', 'type': 'FUNC'}
-{'is_defined': False, 'name': '__cxa_init_primary_exception', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_pure_virtual', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_rethrow', 'type': 'FUNC'}
 {'is_defined': False, 'name': '__cxa_rethrow_primary_exception', 'type': 'FUNC'}

>From 5f34001afc189b71e39ddf8f319c701a15aa3ab5 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sat, 14 Oct 2023 22:03:35 +0300
Subject: [PATCH 25/39] add exception_ptr.bench

---
 libcxx/benchmarks/CMakeLists.txt          |  1 +
 libcxx/benchmarks/exception_ptr.bench.cpp | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 libcxx/benchmarks/exception_ptr.bench.cpp

diff --git a/libcxx/benchmarks/CMakeLists.txt b/libcxx/benchmarks/CMakeLists.txt
index 7591f34d938bf86..800545a5271d48c 100644
--- a/libcxx/benchmarks/CMakeLists.txt
+++ b/libcxx/benchmarks/CMakeLists.txt
@@ -200,6 +200,7 @@ set(BENCHMARK_TESTS
     allocation.bench.cpp
     deque.bench.cpp
     deque_iterator.bench.cpp
+    exception_ptr.bench.cpp
     filesystem.bench.cpp
     format_to_n.bench.cpp
     format_to.bench.cpp
diff --git a/libcxx/benchmarks/exception_ptr.bench.cpp b/libcxx/benchmarks/exception_ptr.bench.cpp
new file mode 100644
index 000000000000000..c28d11ba0ed3924
--- /dev/null
+++ b/libcxx/benchmarks/exception_ptr.bench.cpp
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include <benchmark/benchmark.h>
+#include <exception>
+#include <stdexcept>
+
+void bm_make_exception_ptr(benchmark::State& state) {
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(std::make_exception_ptr(std::runtime_error{"Some error"}));
+  }
+}
+BENCHMARK(bm_make_exception_ptr);
+
+BENCHMARK_MAIN();

>From 4445b7e0cffbedc40bdb96a4b7d91137beac18cb Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sat, 14 Oct 2023 22:40:47 +0300
Subject: [PATCH 26/39] add threaded benchmark

---
 libcxx/benchmarks/exception_ptr.bench.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/benchmarks/exception_ptr.bench.cpp b/libcxx/benchmarks/exception_ptr.bench.cpp
index c28d11ba0ed3924..4933d4b22babeb1 100644
--- a/libcxx/benchmarks/exception_ptr.bench.cpp
+++ b/libcxx/benchmarks/exception_ptr.bench.cpp
@@ -16,5 +16,6 @@ void bm_make_exception_ptr(benchmark::State& state) {
   }
 }
 BENCHMARK(bm_make_exception_ptr);
+BENCHMARK(bm_make_exception_ptr)->Threads(8)->UseRealTime();
 
 BENCHMARK_MAIN();

>From c49cb3899d6871bfd738ad4ce0a2dd74a794533d Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sat, 14 Oct 2023 23:21:35 +0300
Subject: [PATCH 27/39] better measurements in benchmark

---
 libcxx/benchmarks/exception_ptr.bench.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libcxx/benchmarks/exception_ptr.bench.cpp b/libcxx/benchmarks/exception_ptr.bench.cpp
index 4933d4b22babeb1..95a59e5bd647e0a 100644
--- a/libcxx/benchmarks/exception_ptr.bench.cpp
+++ b/libcxx/benchmarks/exception_ptr.bench.cpp
@@ -15,7 +15,6 @@ void bm_make_exception_ptr(benchmark::State& state) {
     benchmark::DoNotOptimize(std::make_exception_ptr(std::runtime_error{"Some error"}));
   }
 }
-BENCHMARK(bm_make_exception_ptr);
-BENCHMARK(bm_make_exception_ptr)->Threads(8)->UseRealTime();
+BENCHMARK(bm_make_exception_ptr)->ThreadRange(1, 8);
 
 BENCHMARK_MAIN();

>From 139ec39c8ed905152042c8f8029b20cf68edb1ad Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sun, 15 Oct 2023 00:38:26 +0300
Subject: [PATCH 28/39] add cxx-abilist for other architectures, some ifdefs
 for windows

---
 ...c64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist | 3 +++
 ...pple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist | 5 +++++
 ...own-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist | 3 +++
 libcxx/src/support/runtime/exception_pointer_cxxabi.ipp     | 6 ++++++
 4 files changed, 17 insertions(+)

diff --git a/libcxx/lib/abi/powerpc64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist b/libcxx/lib/abi/powerpc64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist
index 8a98d42a2a1aa02..09a1d45ef897010 100644
--- a/libcxx/lib/abi/powerpc64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist
+++ b/libcxx/lib/abi/powerpc64-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist
@@ -249,6 +249,9 @@
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD0Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD1Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD2Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
+{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptr23__free_native_exceptionEPv', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
+{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptr23__init_native_exceptionEmPSt9type_infoPFvPvE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
+{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptr31__from_native_exception_pointerEPv', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptrC1ERKS_', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptrC2ERKS_', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptrD1Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
diff --git a/libcxx/lib/abi/x86_64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist b/libcxx/lib/abi/x86_64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist
index 0c06b5097b83f80..204b3cbfc978288 100644
--- a/libcxx/lib/abi/x86_64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist
+++ b/libcxx/lib/abi/x86_64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist
@@ -300,6 +300,7 @@
 {'is_defined': False, 'name': '___cxa_guard_acquire', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_guard_release', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_increment_exception_refcount', 'type': 'U'}
+{'is_defined': False, 'name': '___cxa_init_primary_exception', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_pure_virtual', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_rethrow', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_rethrow_primary_exception', 'type': 'U'}
@@ -811,6 +812,9 @@
 {'is_defined': True, 'name': '__ZNSt13bad_exceptionD0Ev', 'type': 'I'}
 {'is_defined': True, 'name': '__ZNSt13bad_exceptionD1Ev', 'type': 'I'}
 {'is_defined': True, 'name': '__ZNSt13bad_exceptionD2Ev', 'type': 'I'}
+{'is_defined': True, 'name': '__ZNSt13exception_ptr23__free_native_exceptionEPv', 'type': 'FUNC'}
+{'is_defined': True, 'name': '__ZNSt13exception_ptr23__init_native_exceptionEmPSt9type_infoPFvPvE', 'type': 'FUNC'}
+{'is_defined': True, 'name': '__ZNSt13exception_ptr31__from_native_exception_pointerEPv', 'type': 'FUNC'}
 {'is_defined': True, 'name': '__ZNSt13exception_ptrC1ERKS_', 'type': 'FUNC'}
 {'is_defined': True, 'name': '__ZNSt13exception_ptrC2ERKS_', 'type': 'FUNC'}
 {'is_defined': True, 'name': '__ZNSt13exception_ptrD1Ev', 'type': 'FUNC'}
@@ -2542,6 +2546,7 @@
 {'is_defined': True, 'name': '___cxa_guard_abort', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_guard_acquire', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_guard_release', 'type': 'I'}
+{'is_defined': True, 'name': '___cxa_init_primary_exception', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_pure_virtual', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_rethrow', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_throw', 'type': 'I'}
diff --git a/libcxx/lib/abi/x86_64-unknown-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist b/libcxx/lib/abi/x86_64-unknown-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist
index 16658fdff54932b..7387f6102b2d695 100644
--- a/libcxx/lib/abi/x86_64-unknown-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist
+++ b/libcxx/lib/abi/x86_64-unknown-freebsd.libcxxabi.v1.stable.exceptions.nonew.abilist
@@ -523,6 +523,9 @@
 {'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD0Ev', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD1Ev', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD2Ev', 'type': 'FUNC'}
+{'is_defined': True, 'name': '_ZNSt13exception_ptr23__free_native_exceptionEPv', 'type': 'FUNC'}
+{'is_defined': True, 'name': '_ZNSt13exception_ptr23__init_native_exceptionEmPSt9type_infoPFvPvE', 'type': 'FUNC'}
+{'is_defined': True, 'name': '_ZNSt13exception_ptr31__from_native_exception_pointerEPv', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt13exception_ptrC1ERKS_', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt13exception_ptrC2ERKS_', 'type': 'FUNC'}
 {'is_defined': True, 'name': '_ZNSt13exception_ptrD1Ev', 'type': 'FUNC'}
diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index 73aa30e6ca893ae..dbac19d84516c4f 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -11,7 +11,9 @@
 #error this header may only be used with libc++abi or libcxxrt
 #endif
 
+#if !defined(_WIN32)
 #include <dlfcn.h>
+#endif
 
 namespace std {
 
@@ -40,6 +42,7 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
 void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
 {
+    #if !defined(_WIN32)
     using CxaInitPrimaryExceptionPrototype = void *(*)(void *, type_info *, void(*)(void *));
     static CxaInitPrimaryExceptionPrototype cxa_init_primary_exception_fn = reinterpret_cast<CxaInitPrimaryExceptionPrototype>(
         dlsym(RTLD_DEFAULT, "__cxa_init_primary_exception"));
@@ -51,6 +54,9 @@ void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void
     void *__ex = __cxa_allocate_exception(size);
     (void)cxa_init_primary_exception_fn(__ex, tinfo, dest);
     return __ex;
+    #else
+    return nullptr;
+    #endif
 }
 
 void exception_ptr::__free_native_exception(void *thrown_object) noexcept

>From 652bd3041697390a98f3e9229f816c41943e1955 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sun, 15 Oct 2023 01:09:59 +0300
Subject: [PATCH 29/39] support libcxxrt as well

---
 libcxx/include/__exception/exception_ptr.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 17894eeeed5818a..52affe6c5d360d8 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -29,7 +29,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   void* __ptr_;
 
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-#    if defined(LIBCXX_BUILDING_LIBCXXABI)
+#    if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
   template <class _Ep>
   static inline void __dest_thunk(void* __x) {
     static_cast<_Ep*>(__x)->~_Ep();
@@ -69,7 +69,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
 template <class _Ep>
 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-#    if defined(LIBCXX_BUILDING_LIBCXXABI)
+#    if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
   using _Ep2 = __decay_t<_Ep>;
   void* __ex = exception_ptr::__init_native_exception(
       sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);

>From 40a7f57cc3bebd65cabc0c519e62e4c234f40e88 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sun, 15 Oct 2023 05:00:59 +0300
Subject: [PATCH 30/39] add remaining cxx-abilists

---
 ...apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist | 5 +++++
 ...erpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist b/libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist
index 8daad89f52e6f7c..86c75369306fbac 100644
--- a/libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist
+++ b/libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist
@@ -300,6 +300,7 @@
 {'is_defined': False, 'name': '___cxa_guard_acquire', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_guard_release', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_increment_exception_refcount', 'type': 'U'}
+{'is_defined': False, 'name': '___cxa_init_primary_exception', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_pure_virtual', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_rethrow', 'type': 'U'}
 {'is_defined': False, 'name': '___cxa_rethrow_primary_exception', 'type': 'U'}
@@ -811,6 +812,9 @@
 {'is_defined': True, 'name': '__ZNSt13bad_exceptionD0Ev', 'type': 'I'}
 {'is_defined': True, 'name': '__ZNSt13bad_exceptionD1Ev', 'type': 'I'}
 {'is_defined': True, 'name': '__ZNSt13bad_exceptionD2Ev', 'type': 'I'}
+{'is_defined': True, 'name': '__ZNSt13exception_ptr23__free_native_exceptionEPv', 'type': 'FUNC'}
+{'is_defined': True, 'name': '__ZNSt13exception_ptr23__init_native_exceptionEmPSt9type_infoPFvPvE', 'type': 'FUNC'}
+{'is_defined': True, 'name': '__ZNSt13exception_ptr31__from_native_exception_pointerEPv', 'type': 'FUNC'}
 {'is_defined': True, 'name': '__ZNSt13exception_ptrC1ERKS_', 'type': 'FUNC'}
 {'is_defined': True, 'name': '__ZNSt13exception_ptrC2ERKS_', 'type': 'FUNC'}
 {'is_defined': True, 'name': '__ZNSt13exception_ptrD1Ev', 'type': 'FUNC'}
@@ -2508,6 +2512,7 @@
 {'is_defined': True, 'name': '___cxa_guard_abort', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_guard_acquire', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_guard_release', 'type': 'I'}
+{'is_defined': True, 'name': '___cxa_init_primary_exception', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_pure_virtual', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_rethrow', 'type': 'I'}
 {'is_defined': True, 'name': '___cxa_throw', 'type': 'I'}
diff --git a/libcxx/lib/abi/powerpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist b/libcxx/lib/abi/powerpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist
index 91976f500539daa..a7145024d95e221 100644
--- a/libcxx/lib/abi/powerpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist
+++ b/libcxx/lib/abi/powerpc-ibm-aix.libcxxabi.v1.stable.exceptions.nonew.abilist
@@ -249,6 +249,9 @@
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD0Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD1Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt12experimental19bad_optional_accessD2Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
+{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptr23__free_native_exceptionEPv', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
+{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptr23__init_native_exceptionEmPSt9type_infoPFvPvE', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
+{'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptr31__from_native_exception_pointerEPv', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptrC1ERKS_', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptrC2ERKS_', 'storage_mapping_class': 'DS', 'type': 'FUNC'}
 {'import_export': 'EXP', 'is_defined': True, 'name': '_ZNSt13exception_ptrD1Ev', 'storage_mapping_class': 'DS', 'type': 'FUNC'}

>From afc0df2d49441a60bd975109f0a62cc936b11b50 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sun, 15 Oct 2023 18:28:32 +0300
Subject: [PATCH 31/39] fix the build for older versions of libcxxrt, add some
 comments

---
 libcxx/include/__exception/exception_ptr.h          |  2 ++
 .../support/runtime/exception_pointer_cxxabi.ipp    | 13 +++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 52affe6c5d360d8..8008fc84c727045 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -73,6 +73,8 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
   using _Ep2 = __decay_t<_Ep>;
   void* __ex = exception_ptr::__init_native_exception(
       sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
+  // This could happen with older versions of libcxxrt/libcxxabi,
+  // which don't yet support direct exception initialization.
   if (__ex == nullptr) {
     try {
       throw __e;
diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index dbac19d84516c4f..f759f21787dabe5 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -15,6 +15,15 @@
 #include <dlfcn.h>
 #endif
 
+#if defined(LIBCXXRT)
+extern "C" {
+    // Although libcxxrt defines these two (as an ABI-library should),
+    // it doesn't declare them in some versions.
+    void *__cxa_allocate_exception(size_t thrown_size);
+    void __cxa_free_exception(void* thrown_exception);
+}
+#endif
+
 namespace std {
 
 exception_ptr::~exception_ptr() noexcept
@@ -43,6 +52,10 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
 void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
 {
     #if !defined(_WIN32)
+    // We perform a runtime lookup of __cxa_init_primary_exception to
+    // preserve compability with older versions of libcxxrt/libcxxabi.
+    // If the function is not present we return nullptr because no meaningful work can be done,
+    // and the caller knows how to handle this (it fallbacks to throw + catch).
     using CxaInitPrimaryExceptionPrototype = void *(*)(void *, type_info *, void(*)(void *));
     static CxaInitPrimaryExceptionPrototype cxa_init_primary_exception_fn = reinterpret_cast<CxaInitPrimaryExceptionPrototype>(
         dlsym(RTLD_DEFAULT, "__cxa_init_primary_exception"));

>From f0ecbd3dec54aafc7796137f7f1bd6721a227e98 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Sun, 15 Oct 2023 20:38:20 +0300
Subject: [PATCH 32/39] cleaner benchmark

---
 libcxx/benchmarks/exception_ptr.bench.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libcxx/benchmarks/exception_ptr.bench.cpp b/libcxx/benchmarks/exception_ptr.bench.cpp
index 95a59e5bd647e0a..1292ad7935e3751 100644
--- a/libcxx/benchmarks/exception_ptr.bench.cpp
+++ b/libcxx/benchmarks/exception_ptr.bench.cpp
@@ -8,11 +8,10 @@
 
 #include <benchmark/benchmark.h>
 #include <exception>
-#include <stdexcept>
 
 void bm_make_exception_ptr(benchmark::State& state) {
   for (auto _ : state) {
-    benchmark::DoNotOptimize(std::make_exception_ptr(std::runtime_error{"Some error"}));
+    benchmark::DoNotOptimize(std::make_exception_ptr(42));
   }
 }
 BENCHMARK(bm_make_exception_ptr)->ThreadRange(1, 8);

>From 1894d2921510b43b4587e59e7829d946623c3331 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 20 Oct 2023 01:07:14 +0300
Subject: [PATCH 33/39] revert the typeinfo header split, make exception
 dependent on typeinfo instead

---
 libcxx/include/CMakeLists.txt              |   1 -
 libcxx/include/__exception/exception_ptr.h |   4 +-
 libcxx/include/__typeinfo/typeinfo.h       | 336 -------------------
 libcxx/include/libcxx.imp                  |   1 -
 libcxx/include/module.modulemap.in         |   5 -
 libcxx/include/typeinfo                    | 355 ++++++++++++++++++++-
 libcxx/utils/data/ignore_format.txt        |   1 +
 7 files changed, 356 insertions(+), 347 deletions(-)
 delete mode 100644 libcxx/include/__typeinfo/typeinfo.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 482577069bd36ee..9b03430a87d8338 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -832,7 +832,6 @@ set(files
   __type_traits/underlying_type.h
   __type_traits/unwrap_ref.h
   __type_traits/void_t.h
-  __typeinfo/typeinfo.h
   __undef_macros
   __utility/as_const.h
   __utility/auto_cast.h
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 8008fc84c727045..e6b0876cc8e4f0a 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -13,7 +13,7 @@
 #include <__exception/operations.h>
 #include <__memory/addressof.h>
 #include <__type_traits/decay.h>
-#include <__typeinfo/typeinfo.h>
+#include <typeinfo>
 #include <cstddef>
 #include <cstdlib>
 
@@ -69,7 +69,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
 template <class _Ep>
 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-#    if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
+#    if !defined(_LIBCPP_HAS_NO_RTTI) && (defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI))
   using _Ep2 = __decay_t<_Ep>;
   void* __ex = exception_ptr::__init_native_exception(
       sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
diff --git a/libcxx/include/__typeinfo/typeinfo.h b/libcxx/include/__typeinfo/typeinfo.h
deleted file mode 100644
index 07b53af2b478588..000000000000000
--- a/libcxx/include/__typeinfo/typeinfo.h
+++ /dev/null
@@ -1,336 +0,0 @@
-// -*- 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___TYPEINFO_TYPEINFO_H
-#define _LIBCPP___TYPEINFO_TYPEINFO_H
-
-#include <__availability>
-#include <__config>
-#include <__exception/exception.h>
-#include <__type_traits/is_constant_evaluated.h>
-#include <__verbose_abort>
-#include <cstddef>
-#include <cstdint>
-
-#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-#  pragma GCC system_header
-#endif
-
-#if defined(_LIBCPP_ABI_VCRUNTIME)
-#  include <vcruntime_typeinfo.h>
-#else
-
-namespace std // purposefully not using versioning namespace
-{
-
-#  if defined(_LIBCPP_ABI_MICROSOFT)
-
-class _LIBCPP_EXPORTED_FROM_ABI type_info {
-  type_info& operator=(const type_info&);
-  type_info(const type_info&);
-
-  mutable struct {
-    const char* __undecorated_name;
-    const char __decorated_name[1];
-  } __data;
-
-  int __compare(const type_info& __rhs) const _NOEXCEPT;
-
-public:
-  _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
-  virtual ~type_info();
-
-  const char* name() const _NOEXCEPT;
-
-  _LIBCPP_INLINE_VISIBILITY bool before(const type_info& __arg) const _NOEXCEPT { return __compare(__arg) < 0; }
-
-  size_t hash_code() const _NOEXCEPT;
-
-  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
-    // When evaluated in a constant expression, both type infos simply can't come
-    // from different translation units, so it is sufficient to compare their addresses.
-    if (__libcpp_is_constant_evaluated()) {
-      return this == &__arg;
-    }
-    return __compare(__arg) == 0;
-  }
-
-#    if _LIBCPP_STD_VER <= 17
-  _LIBCPP_INLINE_VISIBILITY bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); }
-#    endif
-};
-
-#  else // !defined(_LIBCPP_ABI_MICROSOFT)
-
-// ========================================================================== //
-//                           Implementations
-// ========================================================================== //
-// ------------------------------------------------------------------------- //
-//                               Unique
-//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
-// ------------------------------------------------------------------------- //
-// This implementation of type_info assumes a unique copy of the RTTI for a
-// given type inside a program. This is a valid assumption when abiding to the
-// Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
-// Under this assumption, we can always compare the addresses of the type names
-// to implement equality-comparison of type_infos instead of having to perform
-// a deep string comparison.
-// -------------------------------------------------------------------------- //
-//                             NonUnique
-//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
-// -------------------------------------------------------------------------- //
-// This implementation of type_info does not assume there is always a unique
-// copy of the RTTI for a given type inside a program. For various reasons
-// the linker may have failed to merge every copy of a types RTTI
-// (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
-// type_infos are equal if their addresses are equal or if a deep string
-// comparison is equal.
-// -------------------------------------------------------------------------- //
-//                          NonUniqueARMRTTIBit
-//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
-// -------------------------------------------------------------------------- //
-// This implementation is specific to ARM64 on Apple platforms.
-//
-// This implementation of type_info does not assume always a unique copy of
-// the RTTI for a given type inside a program. When constructing the type_info,
-// the compiler packs the pointer to the type name into a uintptr_t and reserves
-// the high bit of that pointer, which is assumed to be free for use under that
-// ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
-// to be unique within the program. If the high bit is unset, then the RTTI can
-// be assumed to be unique within the program.
-//
-// When comparing type_infos, if both RTTIs can be assumed to be unique, it
-// suffices to compare their addresses. If both the RTTIs can't be assumed to
-// be unique, we must perform a deep string comparison of the type names.
-// However, if one of the RTTIs is guaranteed unique and the other one isn't,
-// then both RTTIs are necessarily not to be considered equal.
-//
-// The intent of this design is to remove the need for weak symbols. Specifically,
-// if a type would normally have a default-visibility RTTI emitted as a weak
-// symbol, it is given hidden visibility instead and the non-unique bit is set.
-// Otherwise, types declared with hidden visibility are always considered to have
-// a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
-// to be deduplicated by the linker within the linked image. Across linked image
-// boundaries, such types are thus considered different types.
-
-// This value can be overriden in the __config_site. When it's not overriden,
-// we pick a default implementation based on the platform here.
-#    ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
-
-// Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation.
-#      if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)
-#        define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
-
-// On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
-#      elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
-#        define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
-
-// On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
-#      else
-#        define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
-#      endif
-#    endif
-
-struct __type_info_implementations {
-  struct __string_impl_base {
-    typedef const char* __type_name_t;
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR static const char*
-    __type_name_to_string(__type_name_t __v) _NOEXCEPT {
-      return __v;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR static __type_name_t
-    __string_to_type_name(const char* __v) _NOEXCEPT {
-      return __v;
-    }
-  };
-
-  struct __unique_impl : __string_impl_base {
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT {
-      return reinterpret_cast<size_t>(__v);
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
-    __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      return __lhs == __rhs;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
-    __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      return __lhs < __rhs;
-    }
-  };
-
-  struct __non_unique_impl : __string_impl_base {
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
-      size_t __hash = 5381;
-      while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
-        __hash = (__hash * 33) ^ __c;
-      return __hash;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
-    __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
-    __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      return __builtin_strcmp(__lhs, __rhs) < 0;
-    }
-  };
-
-  struct __non_unique_arm_rtti_bit_impl {
-    typedef uintptr_t __type_name_t;
-
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static const char*
-    __type_name_to_string(__type_name_t __v) _NOEXCEPT {
-      return reinterpret_cast<const char*>(__v & ~__non_unique_rtti_bit::value);
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static __type_name_t
-    __string_to_type_name(const char* __v) _NOEXCEPT {
-      return reinterpret_cast<__type_name_t>(__v);
-    }
-
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT {
-      if (__is_type_name_unique(__v))
-        return __v;
-      return __non_unique_impl::__hash(__type_name_to_string(__v));
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
-    __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      if (__lhs == __rhs)
-        return true;
-      if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
-        // Either both are unique and have a different address, or one of them
-        // is unique and the other one isn't. In both cases they are unequal.
-        return false;
-      return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
-    }
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE static bool
-    __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
-      if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
-        return __lhs < __rhs;
-      return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
-    }
-
-  private:
-    // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
-    // this implementation is actually used.
-    typedef integral_constant<__type_name_t, (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))>
-        __non_unique_rtti_bit;
-
-    _LIBCPP_INLINE_VISIBILITY static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
-      return !(__lhs & __non_unique_rtti_bit::value);
-    }
-  };
-
-  typedef
-#    if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
-      __unique_impl
-#    elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
-      __non_unique_impl
-#    elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
-      __non_unique_arm_rtti_bit_impl
-#    else
-#      error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
-#    endif
-          __impl;
-};
-
-class _LIBCPP_EXPORTED_FROM_ABI type_info {
-  type_info& operator=(const type_info&);
-  type_info(const type_info&);
-
-protected:
-  typedef __type_info_implementations::__impl __impl;
-
-  __impl::__type_name_t __type_name;
-
-  _LIBCPP_INLINE_VISIBILITY explicit type_info(const char* __n) : __type_name(__impl::__string_to_type_name(__n)) {}
-
-public:
-  _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
-  virtual ~type_info();
-
-  _LIBCPP_INLINE_VISIBILITY const char* name() const _NOEXCEPT { return __impl::__type_name_to_string(__type_name); }
-
-  _LIBCPP_INLINE_VISIBILITY bool before(const type_info& __arg) const _NOEXCEPT {
-    return __impl::__lt(__type_name, __arg.__type_name);
-  }
-
-  _LIBCPP_INLINE_VISIBILITY size_t hash_code() const _NOEXCEPT { return __impl::__hash(__type_name); }
-
-  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
-    // When evaluated in a constant expression, both type infos simply can't come
-    // from different translation units, so it is sufficient to compare their addresses.
-    if (__libcpp_is_constant_evaluated()) {
-      return this == &__arg;
-    }
-    return __impl::__eq(__type_name, __arg.__type_name);
-  }
-
-#    if _LIBCPP_STD_VER <= 17
-  _LIBCPP_INLINE_VISIBILITY bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); }
-#    endif
-};
-#  endif // defined(_LIBCPP_ABI_MICROSOFT)
-
-class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception {
-public:
-  bad_cast() _NOEXCEPT;
-  _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT            = default;
-  _LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
-  ~bad_cast() _NOEXCEPT override;
-  const char* what() const _NOEXCEPT override;
-};
-
-class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
-public:
-  bad_typeid() _NOEXCEPT;
-  _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT            = default;
-  _LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
-  ~bad_typeid() _NOEXCEPT override;
-  const char* what() const _NOEXCEPT override;
-};
-
-} // namespace std
-
-#endif // defined(_LIBCPP_ABI_VCRUNTIME)
-
-#if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
-
-namespace std {
-
-class bad_cast : public exception {
-public:
-  bad_cast() _NOEXCEPT : exception("bad cast") {}
-
-private:
-  bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {}
-};
-
-class bad_typeid : public exception {
-public:
-  bad_typeid() _NOEXCEPT : exception("bad typeid") {}
-
-private:
-  bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {}
-};
-
-} // namespace std
-
-#endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY void __throw_bad_cast() {
-#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-  throw bad_cast();
-#else
-  _LIBCPP_VERBOSE_ABORT("bad_cast was thrown in -fno-exceptions mode");
-#endif
-}
-_LIBCPP_END_NAMESPACE_STD
-
-#endif // _LIBCPP___TYPEINFO_TYPEINFO_H
diff --git a/libcxx/include/libcxx.imp b/libcxx/include/libcxx.imp
index f7e906e024f33a4..3eb2aa849314824 100644
--- a/libcxx/include/libcxx.imp
+++ b/libcxx/include/libcxx.imp
@@ -48,7 +48,6 @@
   { include: [ "@<__thread/.*>", "private", "<thread>", "public" ] },
   { include: [ "@<__tuple/.*>", "private", "<tuple>", "public" ] },
   { include: [ "@<__type_traits/.*>", "private", "<type_traits>", "public" ] },
-  { include: [ "@<__typeinfo/.*>", "private", "<typeinfo>", "public" ] },
   { include: [ "@<__utility/.*>", "private", "<utility>", "public" ] },
   { include: [ "@<__variant/.*>", "private", "<variant>", "public" ] },
 ]
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 2a72758a2dd1ed7..3e5a8a391b6e69c 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1267,11 +1267,6 @@ module std_private_debug_utils_strict_weak_ordering_check [system] {
   export std_private_type_traits_is_constant_evaluated
 }
 
-module std_private_typeinfo_typeinfo          [system] {
-  header "__typeinfo/typeinfo.h"
-  export *
-}
-
 module std_private_exception_exception        [system] { header "__exception/exception.h" }
 module std_private_exception_exception_ptr    [system] {
   header "__exception/exception_ptr.h"
diff --git a/libcxx/include/typeinfo b/libcxx/include/typeinfo
index e869cd5e6750787..797d815135cb2e5 100644
--- a/libcxx/include/typeinfo
+++ b/libcxx/include/typeinfo
@@ -57,16 +57,367 @@ public:
 */
 
 #include <__assert> // all public C++ headers provide the assertion handler
+#include <__availability>
 #include <__config>
-#include <__typeinfo/typeinfo.h>
+#include <__exception/exception.h>
+#include <__type_traits/is_constant_evaluated.h>
+#include <__verbose_abort>
+#include <cstddef>
+#include <cstdint>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
+#if defined(_LIBCPP_ABI_VCRUNTIME)
+#include <vcruntime_typeinfo.h>
+#else
+
+namespace std  // purposefully not using versioning namespace
+{
+
+
+#if defined(_LIBCPP_ABI_MICROSOFT)
+
+class _LIBCPP_EXPORTED_FROM_ABI type_info
+{
+    type_info& operator=(const type_info&);
+    type_info(const type_info&);
+
+    mutable struct {
+      const char *__undecorated_name;
+      const char __decorated_name[1];
+    } __data;
+
+    int __compare(const type_info &__rhs) const _NOEXCEPT;
+
+public:
+    _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
+    virtual ~type_info();
+
+    const char *name() const _NOEXCEPT;
+
+    _LIBCPP_INLINE_VISIBILITY
+    bool before(const type_info& __arg) const _NOEXCEPT {
+      return __compare(__arg) < 0;
+    }
+
+    size_t hash_code() const _NOEXCEPT;
+
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
+    bool operator==(const type_info& __arg) const _NOEXCEPT {
+      // When evaluated in a constant expression, both type infos simply can't come
+      // from different translation units, so it is sufficient to compare their addresses.
+      if (__libcpp_is_constant_evaluated()) {
+        return this == &__arg;
+      }
+      return __compare(__arg) == 0;
+    }
+
+#if _LIBCPP_STD_VER <= 17
+    _LIBCPP_INLINE_VISIBILITY
+    bool operator!=(const type_info& __arg) const _NOEXCEPT
+    { return !operator==(__arg); }
+#endif
+};
+
+#else // !defined(_LIBCPP_ABI_MICROSOFT)
+
+// ========================================================================== //
+//                           Implementations
+// ========================================================================== //
+// ------------------------------------------------------------------------- //
+//                               Unique
+//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
+// ------------------------------------------------------------------------- //
+// This implementation of type_info assumes a unique copy of the RTTI for a
+// given type inside a program. This is a valid assumption when abiding to the
+// Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
+// Under this assumption, we can always compare the addresses of the type names
+// to implement equality-comparison of type_infos instead of having to perform
+// a deep string comparison.
+// -------------------------------------------------------------------------- //
+//                             NonUnique
+//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
+// -------------------------------------------------------------------------- //
+// This implementation of type_info does not assume there is always a unique
+// copy of the RTTI for a given type inside a program. For various reasons
+// the linker may have failed to merge every copy of a types RTTI
+// (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
+// type_infos are equal if their addresses are equal or if a deep string
+// comparison is equal.
+// -------------------------------------------------------------------------- //
+//                          NonUniqueARMRTTIBit
+//               (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
+// -------------------------------------------------------------------------- //
+// This implementation is specific to ARM64 on Apple platforms.
+//
+// This implementation of type_info does not assume always a unique copy of
+// the RTTI for a given type inside a program. When constructing the type_info,
+// the compiler packs the pointer to the type name into a uintptr_t and reserves
+// the high bit of that pointer, which is assumed to be free for use under that
+// ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
+// to be unique within the program. If the high bit is unset, then the RTTI can
+// be assumed to be unique within the program.
+//
+// When comparing type_infos, if both RTTIs can be assumed to be unique, it
+// suffices to compare their addresses. If both the RTTIs can't be assumed to
+// be unique, we must perform a deep string comparison of the type names.
+// However, if one of the RTTIs is guaranteed unique and the other one isn't,
+// then both RTTIs are necessarily not to be considered equal.
+//
+// The intent of this design is to remove the need for weak symbols. Specifically,
+// if a type would normally have a default-visibility RTTI emitted as a weak
+// symbol, it is given hidden visibility instead and the non-unique bit is set.
+// Otherwise, types declared with hidden visibility are always considered to have
+// a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
+// to be deduplicated by the linker within the linked image. Across linked image
+// boundaries, such types are thus considered different types.
+
+// This value can be overriden in the __config_site. When it's not overriden,
+// we pick a default implementation based on the platform here.
+#ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
+
+  // Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation.
+# if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)
+#   define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
+
+  // On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
+# elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
+#   define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
+
+  // On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
+# else
+#   define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
+# endif
+#endif
+
+struct __type_info_implementations {
+  struct __string_impl_base {
+    typedef const char* __type_name_t;
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    _LIBCPP_CONSTEXPR static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
+      return __v;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    _LIBCPP_CONSTEXPR static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
+      return __v;
+    }
+  };
+
+  struct __unique_impl : __string_impl_base {
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static size_t __hash(__type_name_t __v) _NOEXCEPT {
+      return reinterpret_cast<size_t>(__v);
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      return __lhs == __rhs;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      return __lhs < __rhs;
+    }
+  };
+
+  struct __non_unique_impl : __string_impl_base {
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
+      size_t __hash = 5381;
+      while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
+        __hash = (__hash * 33) ^ __c;
+      return __hash;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      return __builtin_strcmp(__lhs, __rhs) < 0;
+    }
+  };
+
+  struct __non_unique_arm_rtti_bit_impl {
+    typedef uintptr_t __type_name_t;
+
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
+      return reinterpret_cast<const char*>(__v &
+          ~__non_unique_rtti_bit::value);
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
+      return reinterpret_cast<__type_name_t>(__v);
+    }
+
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static size_t __hash(__type_name_t __v) _NOEXCEPT {
+      if (__is_type_name_unique(__v))
+        return __v;
+      return __non_unique_impl::__hash(__type_name_to_string(__v));
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      if (__lhs == __rhs)
+        return true;
+      if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
+        // Either both are unique and have a different address, or one of them
+        // is unique and the other one isn't. In both cases they are unequal.
+        return false;
+      return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
+    }
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
+    static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
+      if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
+        return __lhs < __rhs;
+      return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
+    }
+
+   private:
+    // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
+    // this implementation is actually used.
+    typedef integral_constant<__type_name_t,
+      (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))> __non_unique_rtti_bit;
+
+    _LIBCPP_INLINE_VISIBILITY
+    static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
+      return !(__lhs & __non_unique_rtti_bit::value);
+    }
+  };
+
+  typedef
+#if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
+    __unique_impl
+#elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
+    __non_unique_impl
+#elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
+    __non_unique_arm_rtti_bit_impl
+#else
+#   error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
+#endif
+     __impl;
+};
+
+class _LIBCPP_EXPORTED_FROM_ABI type_info
+{
+  type_info& operator=(const type_info&);
+  type_info(const type_info&);
+
+ protected:
+    typedef __type_info_implementations::__impl __impl;
+
+    __impl::__type_name_t __type_name;
+
+    _LIBCPP_INLINE_VISIBILITY
+    explicit type_info(const char* __n)
+      : __type_name(__impl::__string_to_type_name(__n)) {}
+
+public:
+    _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
+    virtual ~type_info();
+
+    _LIBCPP_INLINE_VISIBILITY
+    const char* name() const _NOEXCEPT
+    {
+      return __impl::__type_name_to_string(__type_name);
+    }
+
+    _LIBCPP_INLINE_VISIBILITY
+    bool before(const type_info& __arg) const _NOEXCEPT
+    {
+      return __impl::__lt(__type_name, __arg.__type_name);
+    }
+
+    _LIBCPP_INLINE_VISIBILITY
+    size_t hash_code() const _NOEXCEPT
+    {
+      return __impl::__hash(__type_name);
+    }
+
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
+    bool operator==(const type_info& __arg) const _NOEXCEPT
+    {
+      // When evaluated in a constant expression, both type infos simply can't come
+      // from different translation units, so it is sufficient to compare their addresses.
+      if (__libcpp_is_constant_evaluated()) {
+        return this == &__arg;
+      }
+      return __impl::__eq(__type_name, __arg.__type_name);
+    }
+
+#if _LIBCPP_STD_VER <= 17
+    _LIBCPP_INLINE_VISIBILITY
+    bool operator!=(const type_info& __arg) const _NOEXCEPT
+    { return !operator==(__arg); }
+#endif
+};
+#endif // defined(_LIBCPP_ABI_MICROSOFT)
+
+class _LIBCPP_EXPORTED_FROM_ABI bad_cast
+    : public exception
+{
+ public:
+  bad_cast() _NOEXCEPT;
+  _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
+  _LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
+  ~bad_cast() _NOEXCEPT override;
+  const char* what() const _NOEXCEPT override;
+};
+
+class _LIBCPP_EXPORTED_FROM_ABI bad_typeid
+    : public exception
+{
+ public:
+  bad_typeid() _NOEXCEPT;
+  _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default;
+  _LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
+  ~bad_typeid() _NOEXCEPT override;
+  const char* what() const _NOEXCEPT override;
+};
+
+} // namespace std
+
+#endif // defined(_LIBCPP_ABI_VCRUNTIME)
+
+#if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
+
+namespace std {
+
+class bad_cast : public exception {
+public:
+  bad_cast() _NOEXCEPT : exception("bad cast") {}
+
+private:
+  bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {}
+};
+
+class bad_typeid : public exception {
+public:
+  bad_typeid() _NOEXCEPT : exception("bad typeid") {}
+
+private:
+  bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {}
+};
+
+} // namespace std
+
+#endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
+void __throw_bad_cast()
+{
+#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+    throw bad_cast();
+#else
+    _LIBCPP_VERBOSE_ABORT("bad_cast was thrown in -fno-exceptions mode");
+#endif
+}
+_LIBCPP_END_NAMESPACE_STD
+
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
 #  include <cstdlib>
-#  include <exception>
 #  include <type_traits>
 #endif
 
diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index be01186536cb506..664e9f51e9ba7fd 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -463,6 +463,7 @@ libcxx/include/__tuple/tuple_like_ext.h
 libcxx/include/__tuple/tuple_size.h
 libcxx/include/__tuple/tuple_types.h
 libcxx/include/typeindex
+libcxx/include/typeinfo
 libcxx/include/uchar.h
 libcxx/include/unordered_map
 libcxx/include/unordered_set

>From 6c77ecf0956931f8d3fb11fa80f9278a40ff2c7d Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 20 Oct 2023 02:22:51 +0300
Subject: [PATCH 34/39] use weak linkage instead of dlsym

---
 .../runtime/exception_pointer_cxxabi.ipp      | 35 ++++++++-----------
 libcxxabi/include/cxxabi.h                    |  2 +-
 2 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index f759f21787dabe5..71a6eae90c7ae09 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -11,16 +11,20 @@
 #error this header may only be used with libc++abi or libcxxrt
 #endif
 
-#if !defined(_WIN32)
-#include <dlfcn.h>
-#endif
-
 #if defined(LIBCXXRT)
 extern "C" {
     // Although libcxxrt defines these two (as an ABI-library should),
     // it doesn't declare them in some versions.
     void *__cxa_allocate_exception(size_t thrown_size);
     void __cxa_free_exception(void* thrown_exception);
+
+    __attribute__((weak)) 
+    __cxa_exception *__cxa_init_primary_exception(void *, std::type_info *, void(*)(void*));
+}
+#else
+extern "C" {
+    __attribute__((weak)) 
+    __cxa_exception *__cxa_init_primary_exception(void *, std::type_info *, void(*)(void*)) throw();
 }
 #endif
 
@@ -51,25 +55,14 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
 void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
 {
-    #if !defined(_WIN32)
-    // We perform a runtime lookup of __cxa_init_primary_exception to
-    // preserve compability with older versions of libcxxrt/libcxxabi.
-    // If the function is not present we return nullptr because no meaningful work can be done,
-    // and the caller knows how to handle this (it fallbacks to throw + catch).
-    using CxaInitPrimaryExceptionPrototype = void *(*)(void *, type_info *, void(*)(void *));
-    static CxaInitPrimaryExceptionPrototype cxa_init_primary_exception_fn = reinterpret_cast<CxaInitPrimaryExceptionPrototype>(
-        dlsym(RTLD_DEFAULT, "__cxa_init_primary_exception"));
-
-    if (cxa_init_primary_exception_fn == nullptr) {
+    if (__cxa_init_primary_exception != nullptr) {
+        void *__ex = __cxa_allocate_exception(size);
+        (void)__cxa_init_primary_exception(__ex, tinfo, dest);
+        return __ex;
+    }
+    else {
         return nullptr;
     }
-
-    void *__ex = __cxa_allocate_exception(size);
-    (void)cxa_init_primary_exception_fn(__ex, tinfo, dest);
-    return __ex;
-    #else
-    return nullptr;
-    #endif
 }
 
 void exception_ptr::__free_native_exception(void *thrown_object) noexcept
diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h
index 03ec7986e209126..0ae9aebcee5c934 100644
--- a/libcxxabi/include/cxxabi.h
+++ b/libcxxabi/include/cxxabi.h
@@ -46,7 +46,7 @@ extern _LIBCXXABI_FUNC_VIS void *
 __cxa_allocate_exception(size_t thrown_size) throw();
 extern _LIBCXXABI_FUNC_VIS void
 __cxa_free_exception(void *thrown_exception) throw();
-// This function is an llvm extension
+// This function is an LLVM extension, which mirros the same extension in libsupc++ and libcxxrt
 extern _LIBCXXABI_FUNC_VIS __cxa_exception*
 __cxa_init_primary_exception(void* object, std::type_info* tinfo, void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
 

>From 48bbdb71bda04e3c3fe9e0c400bb20aa1bdd3cad Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 20 Oct 2023 02:25:07 +0300
Subject: [PATCH 35/39] + clang-format

---
 libcxx/include/__exception/exception_ptr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index e6b0876cc8e4f0a..d9ca69290c9db0e 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -13,9 +13,9 @@
 #include <__exception/operations.h>
 #include <__memory/addressof.h>
 #include <__type_traits/decay.h>
-#include <typeinfo>
 #include <cstddef>
 #include <cstdlib>
+#include <typeinfo>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header

>From 6072c62240aa0e46c98131928c1b73fc07f368db Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 20 Oct 2023 03:23:49 +0300
Subject: [PATCH 36/39] fix transitive_includes

---
 libcxx/test/libcxx/transitive_includes/cxx03.csv | 3 +--
 libcxx/test/libcxx/transitive_includes/cxx11.csv | 3 +--
 libcxx/test/libcxx/transitive_includes/cxx14.csv | 3 +--
 libcxx/test/libcxx/transitive_includes/cxx17.csv | 3 +--
 libcxx/test/libcxx/transitive_includes/cxx20.csv | 3 +--
 libcxx/test/libcxx/transitive_includes/cxx23.csv | 2 +-
 libcxx/test/libcxx/transitive_includes/cxx26.csv | 2 +-
 7 files changed, 7 insertions(+), 12 deletions(-)

diff --git a/libcxx/test/libcxx/transitive_includes/cxx03.csv b/libcxx/test/libcxx/transitive_includes/cxx03.csv
index 04cc0354714493d..cfe9cb04fdd6290 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx03.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx03.csv
@@ -219,9 +219,9 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
-exception cstdint
 exception cstdlib
 exception type_traits
+exception typeinfo
 exception version
 execution cstddef
 execution version
@@ -908,7 +908,6 @@ typeindex version
 typeinfo cstddef
 typeinfo cstdint
 typeinfo cstdlib
-typeinfo exception
 typeinfo type_traits
 unordered_map algorithm
 unordered_map bit
diff --git a/libcxx/test/libcxx/transitive_includes/cxx11.csv b/libcxx/test/libcxx/transitive_includes/cxx11.csv
index 47048bef817749c..a9e60010b54bb87 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx11.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx11.csv
@@ -220,9 +220,9 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
-exception cstdint
 exception cstdlib
 exception type_traits
+exception typeinfo
 exception version
 execution cstddef
 execution version
@@ -914,7 +914,6 @@ typeindex version
 typeinfo cstddef
 typeinfo cstdint
 typeinfo cstdlib
-typeinfo exception
 typeinfo type_traits
 unordered_map algorithm
 unordered_map bit
diff --git a/libcxx/test/libcxx/transitive_includes/cxx14.csv b/libcxx/test/libcxx/transitive_includes/cxx14.csv
index d7c12b65941b5e6..1f991b4ea86fd07 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx14.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx14.csv
@@ -220,9 +220,9 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
-exception cstdint
 exception cstdlib
 exception type_traits
+exception typeinfo
 exception version
 execution cstddef
 execution version
@@ -916,7 +916,6 @@ typeindex version
 typeinfo cstddef
 typeinfo cstdint
 typeinfo cstdlib
-typeinfo exception
 typeinfo type_traits
 unordered_map algorithm
 unordered_map bit
diff --git a/libcxx/test/libcxx/transitive_includes/cxx17.csv b/libcxx/test/libcxx/transitive_includes/cxx17.csv
index d7c12b65941b5e6..1f991b4ea86fd07 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx17.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx17.csv
@@ -220,9 +220,9 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
-exception cstdint
 exception cstdlib
 exception type_traits
+exception typeinfo
 exception version
 execution cstddef
 execution version
@@ -916,7 +916,6 @@ typeindex version
 typeinfo cstddef
 typeinfo cstdint
 typeinfo cstdlib
-typeinfo exception
 typeinfo type_traits
 unordered_map algorithm
 unordered_map bit
diff --git a/libcxx/test/libcxx/transitive_includes/cxx20.csv b/libcxx/test/libcxx/transitive_includes/cxx20.csv
index c7189e616402f73..19006fdabcbf13d 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx20.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx20.csv
@@ -226,9 +226,9 @@ deque type_traits
 deque typeinfo
 deque version
 exception cstddef
-exception cstdint
 exception cstdlib
 exception type_traits
+exception typeinfo
 exception version
 execution cstddef
 execution version
@@ -920,7 +920,6 @@ typeindex version
 typeinfo cstddef
 typeinfo cstdint
 typeinfo cstdlib
-typeinfo exception
 typeinfo type_traits
 unordered_map algorithm
 unordered_map bit
diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index 64c292b69ce9f43..4f2fb9dc0022285 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -157,8 +157,8 @@ deque stdexcept
 deque tuple
 deque version
 exception cstddef
-exception cstdint
 exception cstdlib
+exception typeinfo
 exception version
 execution cstddef
 execution version
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index 64c292b69ce9f43..4f2fb9dc0022285 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -157,8 +157,8 @@ deque stdexcept
 deque tuple
 deque version
 exception cstddef
-exception cstdint
 exception cstdlib
+exception typeinfo
 exception version
 execution cstddef
 execution version

>From 12d27974360c09edb571e9f7f1b0ada5c4787bd6 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 20 Oct 2023 04:35:32 +0300
Subject: [PATCH 37/39] some formatting again

---
 libcxx/src/support/runtime/exception_pointer_cxxabi.ipp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index 71a6eae90c7ae09..580363ccc0833d8 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -18,13 +18,11 @@ extern "C" {
     void *__cxa_allocate_exception(size_t thrown_size);
     void __cxa_free_exception(void* thrown_exception);
 
-    __attribute__((weak)) 
-    __cxa_exception *__cxa_init_primary_exception(void *, std::type_info *, void(*)(void*));
+    __attribute__((weak)) __cxa_exception *__cxa_init_primary_exception(void *, std::type_info *, void(*)(void*));
 }
 #else
 extern "C" {
-    __attribute__((weak)) 
-    __cxa_exception *__cxa_init_primary_exception(void *, std::type_info *, void(*)(void*)) throw();
+    __attribute__((weak)) __cxa_exception *__cxa_init_primary_exception(void *, std::type_info *, void(*)(void*)) throw();
 }
 #endif
 

>From e5b3f0a566ec9c53554558700c2131485de2a5d3 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 20 Oct 2023 06:49:00 +0300
Subject: [PATCH 38/39] add libsupc++ support

---
 libcxx/include/__exception/exception_ptr.h    |  4 +--
 .../runtime/exception_pointer_cxxabi.ipp      |  2 ++
 .../runtime/exception_pointer_glibcxx.ipp     | 31 +++++++++++++++++++
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index d9ca69290c9db0e..25f534c3449fe28 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -29,7 +29,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   void* __ptr_;
 
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-#    if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
+#    if !defined(_LIBCPP_HAS_NO_RTTI)
   template <class _Ep>
   static inline void __dest_thunk(void* __x) {
     static_cast<_Ep*>(__x)->~_Ep();
@@ -69,7 +69,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
 template <class _Ep>
 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-#    if !defined(_LIBCPP_HAS_NO_RTTI) && (defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI))
+#    if !defined(_LIBCPP_HAS_NO_RTTI)
   using _Ep2 = __decay_t<_Ep>;
   void* __ex = exception_ptr::__init_native_exception(
       sizeof(_Ep), const_cast<std::type_info*>(&typeid(_Ep)), exception_ptr::__dest_thunk<_Ep2>);
diff --git a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
index 580363ccc0833d8..dedb5293c188863 100644
--- a/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp
@@ -51,6 +51,7 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
 }
 
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+#    if !defined(_LIBCPP_HAS_NO_RTTI)
 void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
 {
     if (__cxa_init_primary_exception != nullptr) {
@@ -75,6 +76,7 @@ exception_ptr exception_ptr::__from_native_exception_pointer(void *__e) noexcept
 
     return ptr;
 }
+#    endif
 #  endif
 
 nested_exception::nested_exception() noexcept
diff --git a/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp b/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp
index 983a08808dccb7f..225edcd24f968a8 100644
--- a/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp
@@ -16,6 +16,13 @@
 // stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr)
 // function.
 
+extern "C" {
+    void* __cxa_allocate_exception(size_t) throw();
+    void __cxa_free_exception(void*) throw();
+
+    void* __cxa_init_primary_exception(void*, std::type_info*, void(*)(void*)) throw();
+}
+
 namespace std {
 
 namespace __exception_ptr
@@ -25,6 +32,7 @@ struct exception_ptr
 {
     void* __ptr_;
 
+    exception_ptr(void* obj) noexcept;
     exception_ptr(const exception_ptr&) noexcept;
     exception_ptr& operator=(const exception_ptr&) noexcept;
     ~exception_ptr() noexcept;
@@ -53,6 +61,29 @@ exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
     return *this;
 }
 
+#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+#    if !defined(_LIBCPP_HAS_NO_RTTI)
+void *exception_ptr::__init_native_exception(size_t size, type_info *tinfo, void (*dest)(void *)) noexcept
+{
+    void *__ex = __cxa_allocate_exception(size);
+    (void)__cxa_init_primary_exception(__ex, tinfo, dest);
+    return __ex;
+}
+
+void exception_ptr::__free_native_exception(void *thrown_object) noexcept
+{
+    __cxa_free_exception(thrown_object);
+}
+
+exception_ptr exception_ptr::__from_native_exception_pointer(void *__e) noexcept {
+    exception_ptr ptr{};
+    new (reinterpret_cast<void*>(&ptr)) __exception_ptr::exception_ptr(__e);
+
+    return ptr;
+}
+#    endif
+#  endif
+
 nested_exception::nested_exception() noexcept
     : __ptr_(current_exception())
 {

>From b6f1292f5b115970af30dead7d46548aa21644e6 Mon Sep 17 00:00:00 2001
From: Ivan Trofimov <i.trofimow at yandex.ru>
Date: Fri, 20 Oct 2023 21:47:48 +0300
Subject: [PATCH 39/39] make tidy happy: + _LIBCPP_HIDE_FROM_ABI

---
 libcxx/include/__exception/exception_ptr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 25f534c3449fe28..a2264b03559ec08 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -31,7 +31,7 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
 #    if !defined(_LIBCPP_HAS_NO_RTTI)
   template <class _Ep>
-  static inline void __dest_thunk(void* __x) {
+  _LIBCPP_HIDE_FROM_ABI static inline void __dest_thunk(void* __x) {
     static_cast<_Ep*>(__x)->~_Ep();
   }
 



More information about the libcxx-commits mailing list