xref: /llvm-project/libcxx/include/__exception/exception_ptr.h (revision f69585235ec85d54e0f3fc41b2d5700430907f99)
143562287SNikolas Klauser //===----------------------------------------------------------------------===//
243562287SNikolas Klauser //
343562287SNikolas Klauser // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
443562287SNikolas Klauser // See https://llvm.org/LICENSE.txt for license information.
543562287SNikolas Klauser // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
643562287SNikolas Klauser //
743562287SNikolas Klauser //===----------------------------------------------------------------------===//
843562287SNikolas Klauser 
943562287SNikolas Klauser #ifndef _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
1043562287SNikolas Klauser #define _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
1143562287SNikolas Klauser 
1243562287SNikolas Klauser #include <__config>
135acc4a3dSNikolas Klauser #include <__cstddef/nullptr_t.h>
1443562287SNikolas Klauser #include <__exception/operations.h>
15c341d503SHans Wennborg #include <__memory/addressof.h>
1651e91b64Sitrofimow #include <__memory/construct_at.h>
1751e91b64Sitrofimow #include <__type_traits/decay.h>
1843562287SNikolas Klauser #include <cstdlib>
1951e91b64Sitrofimow #include <typeinfo>
2043562287SNikolas Klauser 
2143562287SNikolas Klauser #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2243562287SNikolas Klauser #  pragma GCC system_header
2343562287SNikolas Klauser #endif
2443562287SNikolas Klauser 
2551e91b64Sitrofimow #ifndef _LIBCPP_ABI_MICROSOFT
2651e91b64Sitrofimow 
2763af8584Sitrofimow #  if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION
2863af8584Sitrofimow 
2951e91b64Sitrofimow namespace __cxxabiv1 {
3051e91b64Sitrofimow 
3151e91b64Sitrofimow extern "C" {
3251e91b64Sitrofimow _LIBCPP_OVERRIDABLE_FUNC_VIS void* __cxa_allocate_exception(size_t) throw();
3351e91b64Sitrofimow _LIBCPP_OVERRIDABLE_FUNC_VIS void __cxa_free_exception(void*) throw();
3451e91b64Sitrofimow 
3551e91b64Sitrofimow struct __cxa_exception;
3651e91b64Sitrofimow _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
3751e91b64Sitrofimow     void*,
3851e91b64Sitrofimow     std::type_info*,
3951e91b64Sitrofimow #    if defined(_WIN32)
40271eb068SHeejin Ahn     void(__thiscall*)(void*)) throw();
41271eb068SHeejin Ahn #    elif defined(__wasm__)
42271eb068SHeejin Ahn     // In Wasm, a destructor returns its argument
43271eb068SHeejin Ahn     void* (*)(void*)) throw();
44271eb068SHeejin Ahn #    else
45271eb068SHeejin Ahn     void (*)(void*)) throw();
4651e91b64Sitrofimow #    endif
4751e91b64Sitrofimow }
4851e91b64Sitrofimow 
4951e91b64Sitrofimow } // namespace __cxxabiv1
5051e91b64Sitrofimow 
5151e91b64Sitrofimow #  endif
5251e91b64Sitrofimow 
5363af8584Sitrofimow #endif
5463af8584Sitrofimow 
5543562287SNikolas Klauser namespace std { // purposefully not using versioning namespace
5643562287SNikolas Klauser 
5743562287SNikolas Klauser #ifndef _LIBCPP_ABI_MICROSOFT
5843562287SNikolas Klauser 
59f1ea0b11SNikolas Klauser class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
6043562287SNikolas Klauser   void* __ptr_;
6143562287SNikolas Klauser 
6251e91b64Sitrofimow   static exception_ptr __from_native_exception_pointer(void*) _NOEXCEPT;
6351e91b64Sitrofimow 
6451e91b64Sitrofimow   template <class _Ep>
6551e91b64Sitrofimow   friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
6651e91b64Sitrofimow 
6743562287SNikolas Klauser public:
681ba8ed0cSNikolas Klauser   // exception_ptr is basically a COW string.
69*f6958523SNikolas Klauser   using __trivially_relocatable _LIBCPP_NODEBUG = exception_ptr;
701ba8ed0cSNikolas Klauser 
7143562287SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}
7243562287SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
7343562287SNikolas Klauser 
7443562287SNikolas Klauser   exception_ptr(const exception_ptr&) _NOEXCEPT;
7543562287SNikolas Klauser   exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
7643562287SNikolas Klauser   ~exception_ptr() _NOEXCEPT;
7743562287SNikolas Klauser 
7843562287SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __ptr_ != nullptr; }
7943562287SNikolas Klauser 
8043562287SNikolas Klauser   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
8143562287SNikolas Klauser     return __x.__ptr_ == __y.__ptr_;
8243562287SNikolas Klauser   }
8343562287SNikolas Klauser 
8443562287SNikolas Klauser   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
8543562287SNikolas Klauser     return !(__x == __y);
8643562287SNikolas Klauser   }
8743562287SNikolas Klauser 
88f1ea0b11SNikolas Klauser   friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
89f1ea0b11SNikolas Klauser   friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
9043562287SNikolas Klauser };
9143562287SNikolas Klauser 
9243562287SNikolas Klauser template <class _Ep>
9343562287SNikolas Klauser _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
94ba87515fSNikolas Klauser #  if _LIBCPP_HAS_EXCEPTIONS
9551e91b64Sitrofimow #    if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L
9651e91b64Sitrofimow   using _Ep2 = __decay_t<_Ep>;
9751e91b64Sitrofimow 
9851e91b64Sitrofimow   void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
99271eb068SHeejin Ahn #      ifdef __wasm__
100271eb068SHeejin Ahn   // In Wasm, a destructor returns its argument
101e2c2ffbeSLouis Dionne   (void)__cxxabiv1::__cxa_init_primary_exception(
102e2c2ffbeSLouis Dionne       __ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
103271eb068SHeejin Ahn #      else
10451e91b64Sitrofimow   (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
105271eb068SHeejin Ahn #      endif
10651e91b64Sitrofimow         std::__destroy_at(static_cast<_Ep2*>(__p));
107271eb068SHeejin Ahn #      ifdef __wasm__
108271eb068SHeejin Ahn         return __p;
109271eb068SHeejin Ahn #      endif
11051e91b64Sitrofimow       });
11151e91b64Sitrofimow 
11251e91b64Sitrofimow   try {
11351e91b64Sitrofimow     ::new (__ex) _Ep2(__e);
11451e91b64Sitrofimow     return exception_ptr::__from_native_exception_pointer(__ex);
11551e91b64Sitrofimow   } catch (...) {
11651e91b64Sitrofimow     __cxxabiv1::__cxa_free_exception(__ex);
11751e91b64Sitrofimow     return current_exception();
11851e91b64Sitrofimow   }
11951e91b64Sitrofimow #    else
12043562287SNikolas Klauser   try {
12143562287SNikolas Klauser     throw __e;
12243562287SNikolas Klauser   } catch (...) {
12343562287SNikolas Klauser     return current_exception();
12443562287SNikolas Klauser   }
12551e91b64Sitrofimow #    endif
12643562287SNikolas Klauser #  else
12743562287SNikolas Klauser   ((void)__e);
12843562287SNikolas Klauser   std::abort();
12943562287SNikolas Klauser #  endif
13043562287SNikolas Klauser }
13143562287SNikolas Klauser 
13243562287SNikolas Klauser #else // _LIBCPP_ABI_MICROSOFT
13343562287SNikolas Klauser 
134f1ea0b11SNikolas Klauser class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
13543562287SNikolas Klauser   _LIBCPP_DIAGNOSTIC_PUSH
13643562287SNikolas Klauser   _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field")
13743562287SNikolas Klauser   void* __ptr1_;
13843562287SNikolas Klauser   void* __ptr2_;
13943562287SNikolas Klauser   _LIBCPP_DIAGNOSTIC_POP
14043562287SNikolas Klauser 
14143562287SNikolas Klauser public:
14243562287SNikolas Klauser   exception_ptr() _NOEXCEPT;
14343562287SNikolas Klauser   exception_ptr(nullptr_t) _NOEXCEPT;
14443562287SNikolas Klauser   exception_ptr(const exception_ptr& __other) _NOEXCEPT;
14543562287SNikolas Klauser   exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT;
14643562287SNikolas Klauser   exception_ptr& operator=(nullptr_t) _NOEXCEPT;
14743562287SNikolas Klauser   ~exception_ptr() _NOEXCEPT;
14843562287SNikolas Klauser   explicit operator bool() const _NOEXCEPT;
14943562287SNikolas Klauser };
15043562287SNikolas Klauser 
151f1ea0b11SNikolas Klauser _LIBCPP_EXPORTED_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT;
15243562287SNikolas Klauser 
15343562287SNikolas Klauser inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
15443562287SNikolas Klauser   return !(__x == __y);
15543562287SNikolas Klauser }
15643562287SNikolas Klauser 
157f1ea0b11SNikolas Klauser _LIBCPP_EXPORTED_FROM_ABI void swap(exception_ptr&, exception_ptr&) _NOEXCEPT;
15843562287SNikolas Klauser 
159f1ea0b11SNikolas Klauser _LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void* __except, const void* __ptr);
160f1ea0b11SNikolas Klauser _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
161748023dcSNikolas Klauser [[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
16243562287SNikolas Klauser 
16343562287SNikolas Klauser // This is a built-in template function which automagically extracts the required
16443562287SNikolas Klauser // information.
16543562287SNikolas Klauser template <class _E>
16643562287SNikolas Klauser void* __GetExceptionInfo(_E);
16743562287SNikolas Klauser 
16843562287SNikolas Klauser template <class _Ep>
16943562287SNikolas Klauser _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
17043562287SNikolas Klauser   return __copy_exception_ptr(std::addressof(__e), __GetExceptionInfo(__e));
17143562287SNikolas Klauser }
17243562287SNikolas Klauser 
17343562287SNikolas Klauser #endif // _LIBCPP_ABI_MICROSOFT
17443562287SNikolas Klauser } // namespace std
17543562287SNikolas Klauser 
17643562287SNikolas Klauser #endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
177