]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/eh_ptr.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / eh_ptr.cc
1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008-2021 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // GCC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 #include <bits/c++config.h>
26 #include "eh_atomics.h"
27
28 #if ! _GLIBCXX_INLINE_VERSION
29 // This macro causes exception_ptr to declare an older API (with corresponding
30 // definitions in this file) and to mark some inline functions as "used" so
31 // that definitions will be emitted in this translation unit.
32 #define _GLIBCXX_EH_PTR_COMPAT
33 #endif
34
35 #include <exception>
36 #include <bits/exception_ptr.h>
37 #include "unwind-cxx.h"
38
39 using namespace __cxxabiv1;
40
41 // Verify assumptions about member layout in exception types
42 namespace
43 {
44 template<typename Ex>
45 constexpr std::size_t unwindhdr()
46 { return offsetof(Ex, unwindHeader); }
47
48 template<typename Ex>
49 constexpr std::size_t termHandler()
50 { return unwindhdr<Ex>() - offsetof(Ex, terminateHandler); }
51
52 static_assert( termHandler<__cxa_exception>()
53 == termHandler<__cxa_dependent_exception>(),
54 "__cxa_dependent_exception::termHandler layout must be"
55 " consistent with __cxa_exception::termHandler" );
56
57 #ifndef __ARM_EABI_UNWINDER__
58 template<typename Ex>
59 constexpr std::ptrdiff_t adjptr()
60 { return unwindhdr<Ex>() - offsetof(Ex, adjustedPtr); }
61
62 static_assert( adjptr<__cxa_exception>()
63 == adjptr<__cxa_dependent_exception>(),
64 "__cxa_dependent_exception::adjustedPtr layout must be"
65 " consistent with __cxa_exception::adjustedPtr" );
66 #endif
67 }
68
69 // Define non-inline functions.
70
71 std::__exception_ptr::exception_ptr::exception_ptr(void* obj) noexcept
72 : _M_exception_object(obj) { _M_addref(); }
73
74
75 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool) noexcept
76 : _M_exception_object(nullptr) { }
77
78
79 void
80 std::__exception_ptr::exception_ptr::_M_addref() noexcept
81 {
82 if (__builtin_expect(_M_exception_object != nullptr, true))
83 {
84 __cxa_refcounted_exception *eh =
85 __get_refcounted_exception_header_from_obj (_M_exception_object);
86 __gnu_cxx::__eh_atomic_inc (&eh->referenceCount);
87 }
88 }
89
90
91 void
92 std::__exception_ptr::exception_ptr::_M_release() noexcept
93 {
94 if (__builtin_expect(_M_exception_object != nullptr, true))
95 {
96 __cxa_refcounted_exception *eh =
97 __get_refcounted_exception_header_from_obj (_M_exception_object);
98 if (__gnu_cxx::__eh_atomic_dec (&eh->referenceCount))
99 {
100 if (eh->exc.exceptionDestructor)
101 eh->exc.exceptionDestructor (_M_exception_object);
102
103 __cxa_free_exception (_M_exception_object);
104 _M_exception_object = nullptr;
105 }
106 }
107 }
108
109
110 void*
111 std::__exception_ptr::exception_ptr::_M_get() const noexcept
112 { return _M_exception_object; }
113
114
115
116 // Retained for compatibility with CXXABI_1.3.
117 void
118 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy() noexcept { }
119
120
121 // Retained for compatibility with CXXABI_1.3.
122 bool
123 std::__exception_ptr::exception_ptr::operator!() const noexcept
124 { return _M_exception_object == nullptr; }
125
126
127 // Retained for compatibility with CXXABI_1.3.
128 std::__exception_ptr::exception_ptr::operator __safe_bool() const noexcept
129 {
130 return _M_exception_object ? &exception_ptr::_M_safe_bool_dummy : nullptr;
131 }
132
133 const std::type_info*
134 std::__exception_ptr::exception_ptr::__cxa_exception_type() const noexcept
135 {
136 __cxa_exception *eh = __get_exception_header_from_obj (_M_exception_object);
137 return eh->exceptionType;
138 }
139
140 std::exception_ptr
141 std::current_exception() noexcept
142 {
143 __cxa_eh_globals *globals = __cxa_get_globals ();
144 __cxa_exception *header = globals->caughtExceptions;
145
146 if (!header)
147 return std::exception_ptr();
148
149 // Since foreign exceptions can't be counted, we can't return them.
150 if (!__is_gxx_exception_class (header->unwindHeader.exception_class))
151 return std::exception_ptr();
152
153 return std::exception_ptr(
154 __get_object_from_ambiguous_exception (header));
155 }
156
157
158 static void
159 __gxx_dependent_exception_cleanup(_Unwind_Reason_Code code,
160 _Unwind_Exception *exc)
161 {
162 // This cleanup is set only for dependents.
163 __cxa_dependent_exception *dep = __get_dependent_exception_from_ue (exc);
164 __cxa_refcounted_exception *header =
165 __get_refcounted_exception_header_from_obj (dep->primaryException);
166
167 // We only want to be called through _Unwind_DeleteException.
168 // _Unwind_DeleteException in the HP-UX IA64 libunwind library
169 // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
170 // like the GCC _Unwind_DeleteException function does.
171 if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
172 __terminate (header->exc.terminateHandler);
173
174 __cxa_free_dependent_exception (dep);
175
176 if (__gnu_cxx::__eh_atomic_dec (&header->referenceCount))
177 {
178 if (header->exc.exceptionDestructor)
179 header->exc.exceptionDestructor (header + 1);
180
181 __cxa_free_exception (header + 1);
182 }
183 }
184
185
186 void
187 std::rethrow_exception(std::exception_ptr ep)
188 {
189 void *obj = ep._M_get();
190 __cxa_refcounted_exception *eh
191 = __get_refcounted_exception_header_from_obj (obj);
192
193 __cxa_dependent_exception *dep = __cxa_allocate_dependent_exception ();
194 dep->primaryException = obj;
195 __gnu_cxx::__eh_atomic_inc (&eh->referenceCount);
196
197 dep->unexpectedHandler = get_unexpected ();
198 dep->terminateHandler = get_terminate ();
199 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep->unwindHeader.exception_class);
200 dep->unwindHeader.exception_cleanup = __gxx_dependent_exception_cleanup;
201
202 __cxa_eh_globals *globals = __cxa_get_globals ();
203 globals->uncaughtExceptions += 1;
204
205 #ifdef __USING_SJLJ_EXCEPTIONS__
206 _Unwind_SjLj_RaiseException (&dep->unwindHeader);
207 #else
208 _Unwind_RaiseException (&dep->unwindHeader);
209 #endif
210
211 // Some sort of unwinding error. Note that terminate is a handler.
212 __cxa_begin_catch (&dep->unwindHeader);
213 std::terminate();
214 }
215
216 #undef _GLIBCXX_EH_PTR_COMPAT