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