exception_ptr.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
  9. #define _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
  10. #include <__availability>
  11. #include <__config>
  12. #include <__exception/operations.h>
  13. #include <__memory/addressof.h>
  14. #include <__memory/construct_at.h>
  15. #include <__type_traits/decay.h>
  16. #include <cstddef>
  17. #include <cstdlib>
  18. #include <new>
  19. #include <typeinfo>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. #ifndef _LIBCPP_ABI_MICROSOFT
  24. # if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION
  25. namespace __cxxabiv1 {
  26. extern "C" {
  27. _LIBCPP_OVERRIDABLE_FUNC_VIS void* __cxa_allocate_exception(size_t) throw();
  28. _LIBCPP_OVERRIDABLE_FUNC_VIS void __cxa_free_exception(void*) throw();
  29. struct __cxa_exception;
  30. _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
  31. void*,
  32. std::type_info*,
  33. # if defined(_WIN32)
  34. void(__thiscall*)(void*)) throw();
  35. # elif defined(__wasm__)
  36. // In Wasm, a destructor returns its argument
  37. void* (*)(void*)) throw();
  38. # else
  39. void (*)(void*)) throw();
  40. # endif
  41. }
  42. } // namespace __cxxabiv1
  43. # endif
  44. #endif
  45. namespace std { // purposefully not using versioning namespace
  46. class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
  47. void* __ptr_;
  48. static exception_ptr __from_native_exception_pointer(void*) _NOEXCEPT;
  49. template <class _Ep>
  50. friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
  51. public:
  52. _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}
  53. _LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
  54. exception_ptr(const exception_ptr&) _NOEXCEPT;
  55. exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
  56. ~exception_ptr() _NOEXCEPT;
  57. _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __ptr_ != nullptr; }
  58. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
  59. return __x.__ptr_ == __y.__ptr_;
  60. }
  61. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
  62. return !(__x == __y);
  63. }
  64. friend _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) _NOEXCEPT {
  65. void* __tmp = __x.__ptr_;
  66. __x.__ptr_ = __y.__ptr_;
  67. __y.__ptr_ = __tmp;
  68. }
  69. friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
  70. friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
  71. };
  72. #ifndef _LIBCPP_ABI_MICROSOFT
  73. template <class _Ep>
  74. _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
  75. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  76. # if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L && !defined(__ANDROID__) && !defined(__ARM_NEON__) && !defined(__APPLE__)
  77. using _Ep2 = __decay_t<_Ep>;
  78. void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
  79. # ifdef __wasm__
  80. // In Wasm, a destructor returns its argument
  81. (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
  82. # else
  83. (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
  84. # endif
  85. std::__destroy_at(static_cast<_Ep2*>(__p));
  86. # ifdef __wasm__
  87. return __p;
  88. # endif
  89. });
  90. try {
  91. ::new (__ex) _Ep2(__e);
  92. return exception_ptr::__from_native_exception_pointer(__ex);
  93. } catch (...) {
  94. __cxxabiv1::__cxa_free_exception(__ex);
  95. return current_exception();
  96. }
  97. # else
  98. try {
  99. throw __e;
  100. } catch (...) {
  101. return current_exception();
  102. }
  103. # endif
  104. # else
  105. ((void)__e);
  106. std::abort();
  107. # endif
  108. }
  109. #else // _LIBCPP_ABI_MICROSOFT
  110. _LIBCPP_HIDE_FROM_ABI exception_ptr __copy_exception_ptr(void* __exception, const void* __ptr);
  111. // This is a built-in template function which automagically extracts the required
  112. // information.
  113. template <class _E>
  114. void* __GetExceptionInfo(_E);
  115. template <class _Ep>
  116. exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
  117. return __copy_exception_ptr(std::addressof(__e), __GetExceptionInfo(__e));
  118. }
  119. #endif // _LIBCPP_ABI_MICROSOFT
  120. } // namespace std
  121. #endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H