| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___EXCEPTION_EXCEPTION_PTR_H |
| 10 | #define _LIBCPP___EXCEPTION_EXCEPTION_PTR_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__cstddef/nullptr_t.h> |
| 14 | #include <__cstddef/size_t.h> |
| 15 | #include <__exception/operations.h> |
| 16 | #include <__memory/addressof.h> |
| 17 | #include <__memory/construct_at.h> |
| 18 | #include <__type_traits/decay.h> |
| 19 | #include <__type_traits/is_pointer.h> |
| 20 | #include <__utility/move.h> |
| 21 | #include <__utility/swap.h> |
| 22 | #include <__verbose_abort> |
| 23 | #include <typeinfo> |
| 24 | |
| 25 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 26 | # pragma GCC system_header |
| 27 | #endif |
| 28 | |
| 29 | _LIBCPP_PUSH_MACROS |
| 30 | #include <__undef_macros> |
| 31 | |
| 32 | #ifndef _LIBCPP_ABI_MICROSOFT |
| 33 | |
| 34 | # if _LIBCPP_HAS_EXCEPTIONS && _LIBCPP_HAS_RTTI && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION |
| 35 | |
| 36 | namespace __cxxabiv1 { |
| 37 | |
| 38 | extern "C" { |
| 39 | _LIBCPP_OVERRIDABLE_FUNC_VIS void* __cxa_allocate_exception(std::size_t) throw(); |
| 40 | _LIBCPP_OVERRIDABLE_FUNC_VIS void __cxa_free_exception(void*) throw(); |
| 41 | |
| 42 | struct __cxa_exception; |
| 43 | _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception( |
| 44 | void*, |
| 45 | std::type_info*, |
| 46 | # if defined(_WIN32) |
| 47 | void(__thiscall*)(void*)) throw(); |
| 48 | # elif defined(__wasm__) |
| 49 | // In Wasm, a destructor returns its argument |
| 50 | void* (*)(void*)) throw(); |
| 51 | # else |
| 52 | void (*)(void*)) throw(); |
| 53 | # endif |
| 54 | } |
| 55 | |
| 56 | } // namespace __cxxabiv1 |
| 57 | |
| 58 | # endif // _LIBCPP_HAS_EXCEPTIONS && _LIBCPP_HAS_RTTI && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION |
| 59 | |
| 60 | #endif // !defined(_LIBCPP_ABI_MICROSOFT) |
| 61 | |
| 62 | _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD |
| 63 | _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS |
| 64 | |
| 65 | inline _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) _NOEXCEPT; |
| 66 | |
| 67 | class _LIBCPP_EXPORTED_FROM_ABI exception_ptr { |
| 68 | void* __ptr_; |
| 69 | |
| 70 | static exception_ptr __from_native_exception_pointer(void*) _NOEXCEPT; |
| 71 | |
| 72 | template <class _Ep> |
| 73 | friend _LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_explicit(_Ep&) _NOEXCEPT; |
| 74 | |
| 75 | friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void*, const void*); |
| 76 | |
| 77 | public: |
| 78 | // exception_ptr is basically a COW string so it is trivially relocatable. |
| 79 | using __trivially_relocatable _LIBCPP_NODEBUG = exception_ptr; |
| 80 | |
| 81 | _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {} |
| 82 | _LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {} |
| 83 | |
| 84 | exception_ptr(const exception_ptr&) _NOEXCEPT; |
| 85 | _LIBCPP_HIDE_FROM_ABI exception_ptr(exception_ptr&& __other) _NOEXCEPT : __ptr_(__other.__ptr_) { |
| 86 | __other.__ptr_ = nullptr; |
| 87 | } |
| 88 | exception_ptr& operator=(const exception_ptr&) _NOEXCEPT; |
| 89 | _LIBCPP_HIDE_FROM_ABI exception_ptr& operator=(exception_ptr&& __other) _NOEXCEPT { |
| 90 | exception_ptr __tmp(std::move(__other)); |
| 91 | std::swap(x&: __tmp, y&: *this); |
| 92 | return *this; |
| 93 | } |
| 94 | ~exception_ptr() _NOEXCEPT; |
| 95 | |
| 96 | _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __ptr_ != nullptr; } |
| 97 | |
| 98 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { |
| 99 | return __x.__ptr_ == __y.__ptr_; |
| 100 | } |
| 101 | |
| 102 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { |
| 103 | return !(__x == __y); |
| 104 | } |
| 105 | |
| 106 | friend _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) _NOEXCEPT; |
| 107 | |
| 108 | friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT; |
| 109 | friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr); |
| 110 | }; |
| 111 | |
| 112 | inline _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) _NOEXCEPT { |
| 113 | std::swap(x&: __x.__ptr_, y&: __y.__ptr_); |
| 114 | } |
| 115 | |
| 116 | #ifndef _LIBCPP_ABI_MICROSOFT |
| 117 | |
| 118 | # if _LIBCPP_HAS_EXCEPTIONS |
| 119 | # if _LIBCPP_HAS_RTTI && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION |
| 120 | template <class _Ep> |
| 121 | _LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_explicit(_Ep& __e) _NOEXCEPT { |
| 122 | using _Ep2 = __decay_t<_Ep>; |
| 123 | void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep)); |
| 124 | # ifdef __wasm__ |
| 125 | auto __cleanup = [](void* __p) -> void* { |
| 126 | std::__destroy_at(static_cast<_Ep2*>(__p)); |
| 127 | return __p; |
| 128 | }; |
| 129 | # else |
| 130 | auto __cleanup = [](void* __p) { std::__destroy_at(static_cast<_Ep2*>(__p)); }; |
| 131 | # endif |
| 132 | (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), __cleanup); |
| 133 | |
| 134 | try { |
| 135 | ::new (__ex) _Ep2(__e); |
| 136 | return exception_ptr::__from_native_exception_pointer(__ex); |
| 137 | } catch (...) { |
| 138 | __cxxabiv1::__cxa_free_exception(__ex); |
| 139 | return current_exception(); |
| 140 | } |
| 141 | } |
| 142 | # endif // _LIBCPP_HAS_RTTI && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION |
| 143 | |
| 144 | template <class _Ep> |
| 145 | _LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_via_throw(_Ep& __e) _NOEXCEPT { |
| 146 | try { |
| 147 | throw __e; |
| 148 | } catch (...) { |
| 149 | return current_exception(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | template <class _Ep> |
| 154 | _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT { |
| 155 | // Objective-C exceptions are thrown via pointer. When throwing an Objective-C exception, |
| 156 | // Clang generates a call to `objc_exception_throw` instead of the usual `__cxa_throw`. |
| 157 | // That function creates an exception with a special Objective-C typeinfo instead of |
| 158 | // the usual C++ typeinfo, since that is needed to implement the behavior documented |
| 159 | // at [1]). |
| 160 | // |
| 161 | // Because of this special behavior, we can't create an exception via `__cxa_init_primary_exception` |
| 162 | // for Objective-C exceptions, otherwise we'd bypass `objc_exception_throw`. See https://llvm.org/PR135089. |
| 163 | // |
| 164 | // [1]: |
| 165 | // https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Exceptions/Articles/Exceptions64Bit.html |
| 166 | if _LIBCPP_CONSTEXPR (is_pointer<_Ep>::value) { |
| 167 | return std::__make_exception_ptr_via_throw(__e); |
| 168 | } |
| 169 | |
| 170 | # if _LIBCPP_HAS_RTTI && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && !defined(_LIBCPP_CXX03_LANG) |
| 171 | return std::__make_exception_ptr_explicit(__e); |
| 172 | # else |
| 173 | return std::__make_exception_ptr_via_throw(__e); |
| 174 | # endif |
| 175 | } |
| 176 | # else // !_LIBCPP_HAS_EXCEPTIONS |
| 177 | template <class _Ep> |
| 178 | _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT { |
| 179 | _LIBCPP_VERBOSE_ABORT("make_exception_ptr was called in -fno-exceptions mode" ); |
| 180 | } |
| 181 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 182 | |
| 183 | #else // defined(_LIBCPP_ABI_MICROSOFT) |
| 184 | |
| 185 | _LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void* __except, const void* __ptr); |
| 186 | |
| 187 | // This is a built-in template function which automagically extracts the required |
| 188 | // information. |
| 189 | template <class _E> |
| 190 | void* __GetExceptionInfo(_E); |
| 191 | |
| 192 | template <class _Ep> |
| 193 | _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT { |
| 194 | return __copy_exception_ptr(std::addressof(__e), __GetExceptionInfo(__e)); |
| 195 | } |
| 196 | |
| 197 | #endif // defined(_LIBCPP_ABI_MICROSOFT) |
| 198 | |
| 199 | _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS |
| 200 | _LIBCPP_END_UNVERSIONED_NAMESPACE_STD |
| 201 | |
| 202 | _LIBCPP_POP_MACROS |
| 203 | |
| 204 | #endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H |
| 205 | |