* sv.po: Update.
[official-gcc.git] / libstdc++-v3 / libsupc++ / eh_ptr.cc
blobf3c910b2d1e6ba19ac86291dca430de76005f091
1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008-2016 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.
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.
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.
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/>.
25 #include <bits/c++config.h>
26 #include <bits/atomic_lockfree_defines.h>
28 #if ATOMIC_INT_LOCK_FREE > 1
30 #define _GLIBCXX_EH_PTR_COMPAT
32 #include <exception>
33 #include <bits/exception_ptr.h>
34 #include "unwind-cxx.h"
36 using namespace __cxxabiv1;
38 // Verify assumptions about member layout in exception types
39 namespace
41 template<typename Ex>
42 constexpr std::size_t unwindhdr()
43 { return offsetof(Ex, unwindHeader); }
45 template<typename Ex>
46 constexpr std::size_t termHandler()
47 { return unwindhdr<Ex>() - offsetof(Ex, terminateHandler); }
49 static_assert( termHandler<__cxa_exception>()
50 == termHandler<__cxa_dependent_exception>(),
51 "__cxa_dependent_exception::termHandler layout must be"
52 " consistent with __cxa_exception::termHandler" );
54 #ifndef __ARM_EABI_UNWINDER__
55 template<typename Ex>
56 constexpr std::ptrdiff_t adjptr()
57 { return unwindhdr<Ex>() - offsetof(Ex, adjustedPtr); }
59 static_assert( adjptr<__cxa_exception>()
60 == adjptr<__cxa_dependent_exception>(),
61 "__cxa_dependent_exception::adjustedPtr layout must be"
62 " consistent with __cxa_exception::adjustedPtr" );
63 #endif
66 std::__exception_ptr::exception_ptr::exception_ptr() noexcept
67 : _M_exception_object(0) { }
70 std::__exception_ptr::exception_ptr::exception_ptr(void* obj) noexcept
71 : _M_exception_object(obj) { _M_addref(); }
74 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool) noexcept
75 : _M_exception_object(0) { }
78 std::__exception_ptr::
79 exception_ptr::exception_ptr(const exception_ptr& other) noexcept
80 : _M_exception_object(other._M_exception_object)
81 { _M_addref(); }
84 std::__exception_ptr::exception_ptr::~exception_ptr() noexcept
85 { _M_release(); }
88 std::__exception_ptr::exception_ptr&
89 std::__exception_ptr::
90 exception_ptr::operator=(const exception_ptr& other) noexcept
92 exception_ptr(other).swap(*this);
93 return *this;
97 void
98 std::__exception_ptr::exception_ptr::_M_addref() noexcept
100 if (_M_exception_object)
102 __cxa_refcounted_exception *eh =
103 __get_refcounted_exception_header_from_obj (_M_exception_object);
104 __atomic_add_fetch (&eh->referenceCount, 1, __ATOMIC_ACQ_REL);
109 void
110 std::__exception_ptr::exception_ptr::_M_release() noexcept
112 if (_M_exception_object)
114 __cxa_refcounted_exception *eh =
115 __get_refcounted_exception_header_from_obj (_M_exception_object);
116 if (__atomic_sub_fetch (&eh->referenceCount, 1, __ATOMIC_ACQ_REL) == 0)
118 if (eh->exc.exceptionDestructor)
119 eh->exc.exceptionDestructor (_M_exception_object);
121 __cxa_free_exception (_M_exception_object);
122 _M_exception_object = 0;
128 void*
129 std::__exception_ptr::exception_ptr::_M_get() const noexcept
130 { return _M_exception_object; }
133 void
134 std::__exception_ptr::exception_ptr::swap(exception_ptr &other) noexcept
136 void *tmp = _M_exception_object;
137 _M_exception_object = other._M_exception_object;
138 other._M_exception_object = tmp;
142 // Retained for compatibility with CXXABI_1.3.
143 void
144 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy() noexcept { }
147 // Retained for compatibility with CXXABI_1.3.
148 bool
149 std::__exception_ptr::exception_ptr::operator!() const noexcept
150 { return _M_exception_object == 0; }
153 // Retained for compatibility with CXXABI_1.3.
154 std::__exception_ptr::exception_ptr::operator __safe_bool() const noexcept
156 return _M_exception_object ? &exception_ptr::_M_safe_bool_dummy : 0;
160 const std::type_info*
161 std::__exception_ptr::exception_ptr::__cxa_exception_type() const noexcept
163 __cxa_exception *eh = __get_exception_header_from_obj (_M_exception_object);
164 return eh->exceptionType;
168 bool std::__exception_ptr::operator==(const exception_ptr& lhs,
169 const exception_ptr& rhs) noexcept
170 { return lhs._M_exception_object == rhs._M_exception_object; }
173 bool std::__exception_ptr::operator!=(const exception_ptr& lhs,
174 const exception_ptr& rhs) noexcept
175 { return !(lhs == rhs);}
178 std::exception_ptr
179 std::current_exception() noexcept
181 __cxa_eh_globals *globals = __cxa_get_globals ();
182 __cxa_exception *header = globals->caughtExceptions;
184 if (!header)
185 return std::exception_ptr();
187 // Since foreign exceptions can't be counted, we can't return them.
188 if (!__is_gxx_exception_class (header->unwindHeader.exception_class))
189 return std::exception_ptr();
191 return std::exception_ptr(
192 __get_object_from_ambiguous_exception (header));
196 static void
197 __gxx_dependent_exception_cleanup(_Unwind_Reason_Code code,
198 _Unwind_Exception *exc)
200 // This cleanup is set only for dependents.
201 __cxa_dependent_exception *dep = __get_dependent_exception_from_ue (exc);
202 __cxa_refcounted_exception *header =
203 __get_refcounted_exception_header_from_obj (dep->primaryException);
205 // We only want to be called through _Unwind_DeleteException.
206 // _Unwind_DeleteException in the HP-UX IA64 libunwind library
207 // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
208 // like the GCC _Unwind_DeleteException function does.
209 if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
210 __terminate (header->exc.terminateHandler);
212 __cxa_free_dependent_exception (dep);
214 if (__atomic_sub_fetch (&header->referenceCount, 1, __ATOMIC_ACQ_REL) == 0)
216 if (header->exc.exceptionDestructor)
217 header->exc.exceptionDestructor (header + 1);
219 __cxa_free_exception (header + 1);
224 void
225 std::rethrow_exception(std::exception_ptr ep)
227 void *obj = ep._M_get();
228 __cxa_refcounted_exception *eh
229 = __get_refcounted_exception_header_from_obj (obj);
231 __cxa_dependent_exception *dep = __cxa_allocate_dependent_exception ();
232 dep->primaryException = obj;
233 __atomic_add_fetch (&eh->referenceCount, 1, __ATOMIC_ACQ_REL);
235 dep->unexpectedHandler = get_unexpected ();
236 dep->terminateHandler = get_terminate ();
237 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep->unwindHeader.exception_class);
238 dep->unwindHeader.exception_cleanup = __gxx_dependent_exception_cleanup;
240 __cxa_eh_globals *globals = __cxa_get_globals ();
241 globals->uncaughtExceptions += 1;
243 #ifdef __USING_SJLJ_EXCEPTIONS__
244 _Unwind_SjLj_RaiseException (&dep->unwindHeader);
245 #else
246 _Unwind_RaiseException (&dep->unwindHeader);
247 #endif
249 // Some sort of unwinding error. Note that terminate is a handler.
250 __cxa_begin_catch (&dep->unwindHeader);
251 std::terminate();
254 #undef _GLIBCXX_EH_PTR_COMPAT
256 #endif