]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/exception.cc
[multiple changes]
[gcc.git] / gcc / cp / exception.cc
1 // Functions for Exception Support for -*- C++ -*-
2 // Copyright (C) 1994, 1995, 1996, 1998 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 #include "eh-common.h"
34
35 /* Define terminate, unexpected, set_terminate, set_unexpected as
36 well as the default terminate func and default unexpected func. */
37
38 extern std::terminate_handler __terminate_func __attribute__((__noreturn__));
39 using std::terminate;
40
41 void
42 std::terminate ()
43 {
44 __terminate_func ();
45 }
46
47 void
48 __default_unexpected ()
49 {
50 terminate ();
51 }
52
53 static std::unexpected_handler __unexpected_func __attribute__((__noreturn__))
54 = __default_unexpected;
55
56 std::terminate_handler
57 std::set_terminate (std::terminate_handler func)
58 {
59 std::terminate_handler old = __terminate_func;
60
61 __terminate_func = func;
62 return old;
63 }
64
65 std::unexpected_handler
66 std::set_unexpected (std::unexpected_handler func)
67 {
68 std::unexpected_handler old = __unexpected_func;
69
70 __unexpected_func = func;
71 return old;
72 }
73
74 void
75 std::unexpected ()
76 {
77 __unexpected_func ();
78 }
79
80 /* C++-specific state about the current exception.
81 This must match init_exception_processing().
82
83 Note that handlers and caught are not redundant; when rethrown, an
84 exception can have multiple active handlers and still be considered
85 uncaught. */
86
87 struct cp_eh_info
88 {
89 __eh_info eh_info;
90 void *value;
91 void *type;
92 void (*cleanup)(void *, int);
93 bool caught;
94 cp_eh_info *next;
95 long handlers;
96 void *original_value;
97 };
98
99 /* Language-specific EH info pointer, defined in libgcc2. */
100
101 extern "C" cp_eh_info **__get_eh_info (); // actually void **
102
103 /* Is P the type_info node for a pointer of some kind? */
104
105 extern bool __is_pointer (void *);
106
107
108 /* OLD Compiler hook to return a pointer to the info for the current exception.
109 Used by get_eh_info (). This fudges the actualy returned value to
110 point to the beginning of what USE to be the cp_eh_info structure.
111 THis is so that old code that dereferences this pointer will find
112 things where it expects it to be.*/
113 extern "C" void *
114 __cp_exception_info (void)
115 {
116 return &((*__get_eh_info ())->value);
117 }
118
119 /* Compiler hook to return a pointer to the info for the current exception.
120 Used by get_eh_info (). */
121
122 extern "C" cp_eh_info *
123 __cp_eh_info (void)
124 {
125 return *__get_eh_info ();
126 }
127
128 /* Allocate a buffer for a cp_eh_info and an exception object of size SIZE,
129 and return a pointer to the beginning of the object's space. */
130
131 extern "C" void * malloc (size_t);
132 extern "C" void *
133 __eh_alloc (size_t size)
134 {
135 void *p = malloc (size);
136 if (p == 0)
137 terminate ();
138 return p;
139 }
140
141 /* Free the memory for an cp_eh_info and associated exception, given
142 a pointer to the cp_eh_info. */
143
144 extern "C" void free (void *);
145 extern "C" void
146 __eh_free (void *p)
147 {
148 free (p);
149 }
150
151
152 typedef void * (* rtimetype) (void);
153
154 extern "C" void *
155 __cplus_type_matcher (cp_eh_info *info, rtimetype match_info,
156 exception_descriptor *exception_table)
157 {
158 void *ret;
159
160 if (exception_table->lang.language != EH_LANG_C_plus_plus)
161 return NULL;
162
163 if (match_info == CATCH_ALL_TYPE)
164 return info->value;
165
166 /* we don't worry about version info yet, there is only one version! */
167
168 void *match_type = match_info ();
169 ret = __throw_type_match_rtti (match_type, info->type, info->original_value);
170 /* change value of exception */
171 if (ret)
172 info->value = ret;
173 return ret;
174 }
175
176
177 /* Compiler hook to push a new exception onto the stack.
178 Used by expand_throw(). */
179
180 extern "C" void
181 __cp_push_exception (void *value, void *type, void (*cleanup)(void *, int))
182 {
183 cp_eh_info *p = (cp_eh_info *) __eh_alloc (sizeof (cp_eh_info));
184
185 p->value = value;
186 p->type = type;
187 p->cleanup = cleanup;
188 p->handlers = 0;
189 p->caught = false;
190 p->original_value = value;
191
192 p->eh_info.match_function = __cplus_type_matcher;
193 p->eh_info.language = EH_LANG_C_plus_plus;
194 p->eh_info.version = 1;
195
196 cp_eh_info **q = __get_eh_info ();
197
198 p->next = *q;
199 *q = p;
200 }
201
202 /* Compiler hook to pop an exception that has been finalized. Used by
203 push_eh_cleanup(). P is the info for the exception caught by the
204 current catch block. */
205
206 extern "C" void
207 __cp_pop_exception (cp_eh_info *p)
208 {
209 cp_eh_info **q = __get_eh_info ();
210
211 --p->handlers;
212
213 /* Don't really pop if there are still active handlers for our exception,
214 or if our exception is being rethrown (i.e. if the active exception is
215 our exception and it is uncaught). */
216 if (p->handlers != 0
217 || (p == *q && !p->caught))
218 return;
219
220 for (; *q; q = &((*q)->next))
221 if (*q == p)
222 break;
223
224 if (! *q)
225 terminate ();
226
227 *q = p->next;
228
229 if (p->cleanup)
230 /* 2 is a magic value for destructors; see build_delete(). */
231 p->cleanup (p->value, 2);
232
233 if (! __is_pointer (p->type))
234 __eh_free (p->value);
235
236 __eh_free (p);
237 }
238
239 extern "C" void
240 __uncatch_exception (void)
241 {
242 cp_eh_info *p = __cp_eh_info ();
243 if (p == 0)
244 terminate ();
245 p->caught = false;
246 }
247
248 /* As per [except.unexpected]:
249 If an exception is thrown, we check it against the spec. If it doesn't
250 match, we call unexpected (). If unexpected () throws, we check that
251 exception against the spec. If it doesn't match, if the spec allows
252 bad_exception we throw that; otherwise we call terminate ().
253
254 The compiler treats an exception spec as a try block with a generic
255 handler that just calls this function with a list of the allowed
256 exception types, so we have an active exception that can be rethrown.
257
258 This function does not return. */
259
260 extern "C" void
261 __check_eh_spec (int n, const void **spec)
262 {
263 cp_eh_info *p = __cp_eh_info ();
264
265 for (int i = 0; i < n; ++i)
266 {
267 if (__throw_type_match_rtti (spec[i], p->type, p->value))
268 throw;
269 }
270
271 try
272 {
273 std::unexpected ();
274 }
275 catch (...)
276 {
277 // __exception_info is an artificial var pushed into each catch block.
278 if (p != __exception_info)
279 {
280 p = __exception_info;
281 for (int i = 0; i < n; ++i)
282 {
283 if (__throw_type_match_rtti (spec[i], p->type, p->value))
284 throw;
285 }
286 }
287
288 const std::type_info &bad_exc = typeid (std::bad_exception);
289 for (int i = 0; i < n; ++i)
290 {
291 if (__throw_type_match_rtti (spec[i], &bad_exc, p->value))
292 throw std::bad_exception ();
293 }
294
295 terminate ();
296 }
297 }
298
299 extern "C" void
300 __throw_bad_cast (void)
301 {
302 throw std::bad_cast ();
303 }
304
305 extern "C" void
306 __throw_bad_typeid (void)
307 {
308 throw std::bad_typeid ();
309 }
310
311 /* Has the current exception been caught? */
312
313 bool
314 std::uncaught_exception ()
315 {
316 cp_eh_info *p = __cp_eh_info ();
317 return p && ! p->caught;
318 }
319
320 const char * std::exception::
321 what () const
322 {
323 return typeid (*this).name ();
324 }
This page took 0.051208 seconds and 5 git commands to generate.