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 <__exception/operations.h> 15 #include <__memory/addressof.h> 16 #include <__memory/construct_at.h> 17 #include <__type_traits/decay.h> 18 #include <cstdlib> 19 #include <typeinfo> 20 21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22 # pragma GCC system_header 23 #endif 24 25 #ifndef _LIBCPP_ABI_MICROSOFT 26 27 # if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION 28 29 namespace __cxxabiv1 { 30 31 extern "C" { 32 _LIBCPP_OVERRIDABLE_FUNC_VIS void* __cxa_allocate_exception(size_t) throw(); 33 _LIBCPP_OVERRIDABLE_FUNC_VIS void __cxa_free_exception(void*) throw(); 34 35 struct __cxa_exception; 36 _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception( 37 void*, 38 std::type_info*, 39 # if defined(_WIN32) 40 void(__thiscall*)(void*)) throw(); 41 # elif defined(__wasm__) 42 // In Wasm, a destructor returns its argument 43 void* (*)(void*)) throw(); 44 # else 45 void (*)(void*)) throw(); 46 # endif 47 } 48 49 } // namespace __cxxabiv1 50 51 # endif 52 53 #endif 54 55 namespace std { // purposefully not using versioning namespace 56 57 #ifndef _LIBCPP_ABI_MICROSOFT 58 59 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr { 60 void* __ptr_; 61 62 static exception_ptr __from_native_exception_pointer(void*) _NOEXCEPT; 63 64 template <class _Ep> 65 friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT; 66 67 public: 68 // exception_ptr is basically a COW string. 69 using __trivially_relocatable _LIBCPP_NODEBUG = exception_ptr; 70 71 _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {} 72 _LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {} 73 74 exception_ptr(const exception_ptr&) _NOEXCEPT; 75 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT; 76 ~exception_ptr() _NOEXCEPT; 77 78 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __ptr_ != nullptr; } 79 80 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { 81 return __x.__ptr_ == __y.__ptr_; 82 } 83 84 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { 85 return !(__x == __y); 86 } 87 88 friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT; 89 friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr); 90 }; 91 92 template <class _Ep> 93 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT { 94 # if _LIBCPP_HAS_EXCEPTIONS 95 # if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L 96 using _Ep2 = __decay_t<_Ep>; 97 98 void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep)); 99 # ifdef __wasm__ 100 // In Wasm, a destructor returns its argument 101 (void)__cxxabiv1::__cxa_init_primary_exception( 102 __ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* { 103 # else 104 (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) { 105 # endif 106 std::__destroy_at(static_cast<_Ep2*>(__p)); 107 # ifdef __wasm__ 108 return __p; 109 # endif 110 }); 111 112 try { 113 ::new (__ex) _Ep2(__e); 114 return exception_ptr::__from_native_exception_pointer(__ex); 115 } catch (...) { 116 __cxxabiv1::__cxa_free_exception(__ex); 117 return current_exception(); 118 } 119 # else 120 try { 121 throw __e; 122 } catch (...) { 123 return current_exception(); 124 } 125 # endif 126 # else 127 ((void)__e); 128 std::abort(); 129 # endif 130 } 131 132 #else // _LIBCPP_ABI_MICROSOFT 133 134 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr { 135 _LIBCPP_DIAGNOSTIC_PUSH 136 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field") 137 void* __ptr1_; 138 void* __ptr2_; 139 _LIBCPP_DIAGNOSTIC_POP 140 141 public: 142 exception_ptr() _NOEXCEPT; 143 exception_ptr(nullptr_t) _NOEXCEPT; 144 exception_ptr(const exception_ptr& __other) _NOEXCEPT; 145 exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT; 146 exception_ptr& operator=(nullptr_t) _NOEXCEPT; 147 ~exception_ptr() _NOEXCEPT; 148 explicit operator bool() const _NOEXCEPT; 149 }; 150 151 _LIBCPP_EXPORTED_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT; 152 153 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { 154 return !(__x == __y); 155 } 156 157 _LIBCPP_EXPORTED_FROM_ABI void swap(exception_ptr&, exception_ptr&) _NOEXCEPT; 158 159 _LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void* __except, const void* __ptr); 160 _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT; 161 [[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr); 162 163 // This is a built-in template function which automagically extracts the required 164 // information. 165 template <class _E> 166 void* __GetExceptionInfo(_E); 167 168 template <class _Ep> 169 _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT { 170 return __copy_exception_ptr(std::addressof(__e), __GetExceptionInfo(__e)); 171 } 172 173 #endif // _LIBCPP_ABI_MICROSOFT 174 } // namespace std 175 176 #endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H 177