1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008 Free Software Foundation, Inc.
4 // This file is part of GCC.
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 2, or (at your option)
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 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 #include <bits/c++config.h>
32 #include <exception_ptr.h>
33 #include "unwind-cxx.h"
35 using namespace __cxxabiv1
;
38 std::__exception_ptr::exception_ptr::exception_ptr() throw()
39 : _M_exception_object(0)
44 std::__exception_ptr::exception_ptr::exception_ptr(void* obj
) throw()
45 : _M_exception_object(obj
)
51 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool
) throw()
52 : _M_exception_object(0)
57 std::__exception_ptr::exception_ptr::exception_ptr(
58 const exception_ptr
& other
) throw()
59 : _M_exception_object(other
._M_exception_object
)
65 std::__exception_ptr::exception_ptr::~exception_ptr() throw()
71 std::__exception_ptr::exception_ptr
&
72 std::__exception_ptr::exception_ptr::operator=(
73 const exception_ptr
& other
) throw()
75 exception_ptr(other
).swap(*this);
81 std::__exception_ptr::exception_ptr::_M_addref() throw()
83 if (_M_exception_object
)
86 __get_exception_header_from_obj (_M_exception_object
);
87 __gnu_cxx::__atomic_add_dispatch (&eh
->referenceCount
, 1);
93 std::__exception_ptr::exception_ptr::_M_release() throw()
95 if (_M_exception_object
)
98 __get_exception_header_from_obj (_M_exception_object
);
99 if (__gnu_cxx::__exchange_and_add_dispatch (&eh
->referenceCount
, -1) == 0)
101 if (eh
->exceptionDestructor
)
102 eh
->exceptionDestructor (_M_exception_object
);
104 __cxa_free_exception (_M_exception_object
);
105 _M_exception_object
= 0;
112 std::__exception_ptr::exception_ptr::_M_get() const throw()
114 return _M_exception_object
;
119 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy()
125 std::__exception_ptr::exception_ptr::swap(exception_ptr
&other
) throw()
127 void *tmp
= _M_exception_object
;
128 _M_exception_object
= other
._M_exception_object
;
129 other
._M_exception_object
= tmp
;
134 std::__exception_ptr::exception_ptr::operator!() const throw()
136 return _M_exception_object
== 0;
140 std::__exception_ptr::exception_ptr::operator __safe_bool() const throw()
142 return _M_exception_object
? &exception_ptr::_M_safe_bool_dummy
: 0;
146 const std::type_info
*
147 std::__exception_ptr::exception_ptr::__cxa_exception_type() const throw()
149 __cxa_exception
*eh
= __get_exception_header_from_obj (_M_exception_object
);
150 return eh
->exceptionType
;
154 bool std::__exception_ptr::operator==(const exception_ptr
& lhs
,
155 const exception_ptr
& rhs
) throw()
157 return lhs
._M_exception_object
== rhs
._M_exception_object
;
161 bool std::__exception_ptr::operator!=(const exception_ptr
& lhs
,
162 const exception_ptr
& rhs
) throw()
164 return !(lhs
== rhs
);
169 std::current_exception() throw()
171 __cxa_eh_globals
*globals
= __cxa_get_globals ();
172 __cxa_exception
*header
= globals
->caughtExceptions
;
175 return std::exception_ptr();
177 // Since foreign exceptions can't be counted, we can't return them.
178 if (!__is_gxx_exception_class (header
->unwindHeader
.exception_class
))
179 return std::exception_ptr();
181 return std::exception_ptr(
182 __get_object_from_ambiguous_exception (header
));
187 __gxx_dependent_exception_cleanup (_Unwind_Reason_Code code
,
188 _Unwind_Exception
*exc
)
190 // This cleanup is set only for dependents.
191 __cxa_dependent_exception
*dep
= __get_dependent_exception_from_ue (exc
);
192 __cxa_exception
*header
=
193 __get_exception_header_from_obj (dep
->primaryException
);
195 // We only want to be called through _Unwind_DeleteException.
196 // _Unwind_DeleteException in the HP-UX IA64 libunwind library
197 // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
198 // like the GCC _Unwind_DeleteException function does.
199 if (code
!= _URC_FOREIGN_EXCEPTION_CAUGHT
&& code
!= _URC_NO_REASON
)
200 __terminate (header
->terminateHandler
);
202 __cxa_free_dependent_exception (dep
);
204 if (__gnu_cxx::__exchange_and_add_dispatch (&header
->referenceCount
, -1) == 0)
206 if (header
->exceptionDestructor
)
207 header
->exceptionDestructor (header
+ 1);
209 __cxa_free_exception (header
+ 1);
215 std::rethrow_exception(std::exception_ptr ep
)
217 void *obj
= ep
._M_get();
218 __cxa_exception
*eh
= __get_exception_header_from_obj (obj
);
220 __cxa_dependent_exception
*dep
= __cxa_allocate_dependent_exception ();
221 dep
->primaryException
= obj
;
222 __gnu_cxx::__atomic_add_dispatch (&eh
->referenceCount
, 1);
224 dep
->unexpectedHandler
= __unexpected_handler
;
225 dep
->terminateHandler
= __terminate_handler
;
226 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep
->unwindHeader
.exception_class
);
227 dep
->unwindHeader
.exception_cleanup
= __gxx_dependent_exception_cleanup
;
229 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
230 _Unwind_SjLj_RaiseException (&dep
->unwindHeader
);
232 _Unwind_RaiseException (&dep
->unwindHeader
);
235 // Some sort of unwinding error. Note that terminate is a handler.
236 __cxa_begin_catch (&dep
->unwindHeader
);