OSDN Git Service

PR libstdc++/38732
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / eh_throw.cc
1 // -*- C++ -*- Exception handling routines for throwing.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 // Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 //
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING.  If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 #include <bits/c++config.h>
32 #include "unwind-cxx.h"
33
34 using namespace __cxxabiv1;
35
36
37 static void
38 __gxx_exception_cleanup (_Unwind_Reason_Code code, _Unwind_Exception *exc)
39 {
40   // This cleanup is set only for primaries.
41   __cxa_refcounted_exception *header
42     = __get_refcounted_exception_header_from_ue (exc);
43
44   // We only want to be called through _Unwind_DeleteException.
45   // _Unwind_DeleteException in the HP-UX IA64 libunwind library
46   // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
47   // like the GCC _Unwind_DeleteException function does.
48   if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
49     __terminate (header->exc.terminateHandler);
50
51 #ifdef _GLIBCXX_ATOMIC_BUILTINS_4
52   if (__sync_sub_and_fetch (&header->referenceCount, 1) == 0)
53     {
54 #endif
55       if (header->exc.exceptionDestructor)
56         header->exc.exceptionDestructor (header + 1);
57
58       __cxa_free_exception (header + 1);
59 #ifdef _GLIBCXX_ATOMIC_BUILTINS_4
60     }
61 #endif
62 }
63
64
65 extern "C" void
66 __cxxabiv1::__cxa_throw (void *obj, std::type_info *tinfo, 
67                          void (*dest) (void *))
68 {
69   // Definitely a primary.
70   __cxa_refcounted_exception *header
71     = __get_refcounted_exception_header_from_obj (obj);
72   header->referenceCount = 1;
73   header->exc.exceptionType = tinfo;
74   header->exc.exceptionDestructor = dest;
75   header->exc.unexpectedHandler = __unexpected_handler;
76   header->exc.terminateHandler = __terminate_handler;
77   __GXX_INIT_PRIMARY_EXCEPTION_CLASS(header->exc.unwindHeader.exception_class);
78   header->exc.unwindHeader.exception_cleanup = __gxx_exception_cleanup;
79
80 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
81   _Unwind_SjLj_RaiseException (&header->exc.unwindHeader);
82 #else
83   _Unwind_RaiseException (&header->exc.unwindHeader);
84 #endif
85
86   // Some sort of unwinding error.  Note that terminate is a handler.
87   __cxa_begin_catch (&header->exc.unwindHeader);
88   std::terminate ();
89 }
90
91 extern "C" void
92 __cxxabiv1::__cxa_rethrow ()
93 {
94   __cxa_eh_globals *globals = __cxa_get_globals ();
95   __cxa_exception *header = globals->caughtExceptions;
96
97   globals->uncaughtExceptions += 1;
98
99   // Watch for luser rethrowing with no active exception.
100   if (header)
101     {
102       // Tell __cxa_end_catch this is a rethrow.
103       if (!__is_gxx_exception_class(header->unwindHeader.exception_class))
104         globals->caughtExceptions = 0;
105       else
106         header->handlerCount = -header->handlerCount;
107
108 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
109       _Unwind_SjLj_Resume_or_Rethrow (&header->unwindHeader);
110 #else
111 #if defined(_LIBUNWIND_STD_ABI)
112       _Unwind_RaiseException (&header->unwindHeader);
113 #else
114       _Unwind_Resume_or_Rethrow (&header->unwindHeader);
115 #endif
116 #endif
117   
118       // Some sort of unwinding error.  Note that terminate is a handler.
119       __cxa_begin_catch (&header->unwindHeader);
120     }
121   std::terminate ();
122 }