0276ed7dad8b1b38d607e5a6fe3bdadda2a4b940
[gcc.git] / gcc / cp / exception.cc
1 // Functions for Exception Support for -*- C++ -*-
2 // Copyright (C) 1994, 1995, 1996 Free Software Foundation
3
4 // This file is part of GNU CC.
5
6 // GNU CC 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)
9 // any later version.
10
11 // GNU CC 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 // You should have received a copy of the GNU General Public License
17 // along with GNU CC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
20
21 // As a special exception, if you link this library with other files,
22 // some of which are compiled with GCC, to produce an executable,
23 // this library does not by itself cause the resulting executable
24 // to be covered by the GNU General Public License.
25 // This exception does not however invalidate any other reasons why
26 // the executable file might be covered by the GNU General Public License.
27
28 #pragma implementation "exception"
29
30 #include "typeinfo"
31 #include "exception"
32 #include <stddef.h>
33
34 /* Define terminate, unexpected, set_terminate, set_unexpected as
35 well as the default terminate func and default unexpected func. */
36
37 extern terminate_handler __terminate_func __attribute__((__noreturn__));
38
39 void
40 terminate ()
41 {
42 __terminate_func ();
43 }
44
45 void
46 __default_unexpected ()
47 {
48 terminate ();
49 }
50
51 static unexpected_handler __unexpected_func __attribute__((__noreturn__))
52 = __default_unexpected;
53
54 terminate_handler
55 set_terminate (terminate_handler func)
56 {
57 terminate_handler old = __terminate_func;
58
59 __terminate_func = func;
60 return old;
61 }
62
63 unexpected_handler
64 set_unexpected (unexpected_handler func)
65 {
66 unexpected_handler old = __unexpected_func;
67
68 __unexpected_func = func;
69 return old;
70 }
71
72 void
73 unexpected ()
74 {
75 __unexpected_func ();
76 }
77
78 /* C++-specific state about the current exception.
79 This must match init_exception_processing().
80
81 Note that handlers and caught are not redundant; when rethrown, an
82 exception can have multiple active handlers and still be considered
83 uncaught. */
84
85 struct cp_eh_info
86 {
87 void *value;
88 void *type;
89 void (*cleanup)(void *, int);
90 bool caught;
91 cp_eh_info *next;
92 long handlers;
93 };
94
95 /* Language-specific EH info pointer, defined in libgcc2. */
96
97 extern "C" cp_eh_info **__get_eh_info (); // actually void **
98
99 /* Is P the type_info node for a pointer of some kind? */
100
101 extern bool __is_pointer (void *);
102
103 /* Compiler hook to return a pointer to the info for the current exception.
104 Used by get_eh_info (). */
105
106 extern "C" cp_eh_info *
107 __cp_exception_info (void)
108 {
109 return *__get_eh_info ();
110 }
111
112 /* Allocate a buffer for a cp_eh_info and an exception object of size SIZE,
113 and return a pointer to the beginning of the object's space. */
114
115 extern "C" void * malloc (size_t);
116 extern "C" void *
117 __eh_alloc (size_t size)
118 {
119 void *p = malloc (size);
120 if (p == 0)
121 terminate ();
122 return p;
123 }
124
125 /* Free the memory for an cp_eh_info and associated exception, given
126 a pointer to the cp_eh_info. */
127
128 extern "C" void free (void *);
129 extern "C" void
130 __eh_free (void *p)
131 {
132 free (p);
133 }
134
135 /* Compiler hook to push a new exception onto the stack.
136 Used by expand_throw(). */
137
138 extern "C" void
139 __cp_push_exception (void *value, void *type, void (*cleanup)(void *, int))
140 {
141 cp_eh_info *p = (cp_eh_info *) __eh_alloc (sizeof (cp_eh_info));
142
143 p->value = value;
144 p->type = type;
145 p->cleanup = cleanup;
146 p->handlers = 0;
147 p->caught = false;
148
149 cp_eh_info **q = __get_eh_info ();
150
151 p->next = *q;
152 *q = p;
153 }
154
155 /* Compiler hook to pop an exception that has been finalized. Used by
156 push_eh_cleanup(). P is the info for the exception caught by the
157 current catch block. */
158
159 extern "C" void
160 __cp_pop_exception (cp_eh_info *p)
161 {
162 cp_eh_info **q = __get_eh_info ();
163
164 --p->handlers;
165
166 /* Don't really pop if there are still active handlers for our exception,
167 or if our exception is being rethrown (i.e. if the active exception is
168 our exception and it is uncaught). */
169 if (p->handlers != 0
170 || (p == *q && !p->caught))
171 return;
172
173 for (; *q; q = &((*q)->next))
174 if (*q == p)
175 break;
176
177 if (! *q)
178 terminate ();
179
180 *q = p->next;
181
182 if (p->cleanup)
183 /* 2 is a magic value for destructors; see build_delete(). */
184 p->cleanup (p->value, 2);
185
186 if (! __is_pointer (p->type))
187 __eh_free (p->value);
188
189 __eh_free (p);
190 }
191
192 extern "C" void
193 __uncatch_exception (void)
194 {
195 cp_eh_info *p = __cp_exception_info ();
196 if (p == 0)
197 terminate ();
198 p->caught = false;
199 }
200
201 /* As per [except.unexpected]:
202 If an exception is thrown, we check it against the spec. If it doesn't
203 match, we call unexpected (). If unexpected () throws, we check that
204 exception against the spec. If it doesn't match, if the spec allows
205 bad_exception we throw that; otherwise we call terminate ().
206
207 The compiler treats an exception spec as a try block with a generic
208 handler that just calls this function with a list of the allowed
209 exception types, so we have an active exception that can be rethrown.
210
211 This function does not return. */
212
213 extern "C" void
214 __check_eh_spec (int n, const void **spec)
215 {
216 cp_eh_info *p = __cp_exception_info ();
217
218 for (int i = 0; i < n; ++i)
219 {
220 if (__throw_type_match_rtti (spec[i], p->type, p->value))
221 throw;
222 }
223
224 try
225 {
226 unexpected ();
227 }
228 catch (...)
229 {
230 // __exception_info is an artificial var pushed into each catch block.
231 if (p != __exception_info)
232 {
233 p = __exception_info;
234 for (int i = 0; i < n; ++i)
235 {
236 if (__throw_type_match_rtti (spec[i], p->type, p->value))
237 throw;
238 }
239 }
240
241 const type_info &bad_exc = typeid (bad_exception);
242 for (int i = 0; i < n; ++i)
243 {
244 if (__throw_type_match_rtti (spec[i], &bad_exc, p->value))
245 throw bad_exception ();
246 }
247
248 terminate ();
249 }
250 }
251
252 extern "C" void
253 __throw_bad_cast (void)
254 {
255 throw bad_cast ();
256 }
257
258 extern "C" void
259 __throw_bad_typeid (void)
260 {
261 throw bad_typeid ();
262 }
263
264 /* Has the current exception been caught? */
265
266 bool
267 uncaught_exception ()
268 {
269 cp_eh_info *p = __cp_exception_info ();
270 return p && ! p->caught;
271 }
272
273 const char * exception::
274 what () const
275 {
276 return typeid (*this).name ();
277 }