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 | _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD |
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 so it is trivially relocatable. |
69 | // It is also replaceable because assignment has normal value semantics. |
70 | using __trivially_relocatable _LIBCPP_NODEBUG = exception_ptr; |
71 | using __replaceable _LIBCPP_NODEBUG = exception_ptr; |
72 | |
73 | _LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {} |
74 | _LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {} |
75 | |
76 | exception_ptr(const exception_ptr&) _NOEXCEPT; |
77 | exception_ptr& operator=(const exception_ptr&) _NOEXCEPT; |
78 | ~exception_ptr() _NOEXCEPT; |
79 | |
80 | _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __ptr_ != nullptr; } |
81 | |
82 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { |
83 | return __x.__ptr_ == __y.__ptr_; |
84 | } |
85 | |
86 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { |
87 | return !(__x == __y); |
88 | } |
89 | |
90 | friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT; |
91 | friend _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr); |
92 | }; |
93 | |
94 | template <class _Ep> |
95 | _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT { |
96 | # if _LIBCPP_HAS_EXCEPTIONS |
97 | # if _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION && __cplusplus >= 201103L |
98 | using _Ep2 = __decay_t<_Ep>; |
99 | |
100 | void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep)); |
101 | # ifdef __wasm__ |
102 | // In Wasm, a destructor returns its argument |
103 | (void)__cxxabiv1::__cxa_init_primary_exception( |
104 | __ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* { |
105 | # else |
106 | (void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) { |
107 | # endif |
108 | std::__destroy_at(static_cast<_Ep2*>(__p)); |
109 | # ifdef __wasm__ |
110 | return __p; |
111 | # endif |
112 | }); |
113 | |
114 | try { |
115 | ::new (__ex) _Ep2(__e); |
116 | return exception_ptr::__from_native_exception_pointer(__ex); |
117 | } catch (...) { |
118 | __cxxabiv1::__cxa_free_exception(__ex); |
119 | return current_exception(); |
120 | } |
121 | # else |
122 | try { |
123 | throw __e; |
124 | } catch (...) { |
125 | return current_exception(); |
126 | } |
127 | # endif |
128 | # else |
129 | ((void)__e); |
130 | std::abort(); |
131 | # endif |
132 | } |
133 | |
134 | #else // _LIBCPP_ABI_MICROSOFT |
135 | |
136 | class _LIBCPP_EXPORTED_FROM_ABI exception_ptr { |
137 | _LIBCPP_DIAGNOSTIC_PUSH |
138 | _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field" ) |
139 | void* __ptr1_; |
140 | void* __ptr2_; |
141 | _LIBCPP_DIAGNOSTIC_POP |
142 | |
143 | public: |
144 | exception_ptr() _NOEXCEPT; |
145 | exception_ptr(nullptr_t) _NOEXCEPT; |
146 | exception_ptr(const exception_ptr& __other) _NOEXCEPT; |
147 | exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT; |
148 | exception_ptr& operator=(nullptr_t) _NOEXCEPT; |
149 | ~exception_ptr() _NOEXCEPT; |
150 | explicit operator bool() const _NOEXCEPT; |
151 | }; |
152 | |
153 | _LIBCPP_EXPORTED_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT; |
154 | |
155 | inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { |
156 | return !(__x == __y); |
157 | } |
158 | |
159 | _LIBCPP_EXPORTED_FROM_ABI void swap(exception_ptr&, exception_ptr&) _NOEXCEPT; |
160 | |
161 | _LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void* __except, const void* __ptr); |
162 | _LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT; |
163 | [[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr); |
164 | |
165 | // This is a built-in template function which automagically extracts the required |
166 | // information. |
167 | template <class _E> |
168 | void* __GetExceptionInfo(_E); |
169 | |
170 | template <class _Ep> |
171 | _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT { |
172 | return __copy_exception_ptr(std::addressof(__e), __GetExceptionInfo(__e)); |
173 | } |
174 | |
175 | #endif // _LIBCPP_ABI_MICROSOFT |
176 | _LIBCPP_END_UNVERSIONED_NAMESPACE_STD |
177 | |
178 | #endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H |
179 | |