]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle exceptional things in C++. |
c913b6f1 KG |
2 | Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
3 | 2000, 2001 Free Software Foundation, Inc. | |
8d2733ca MS |
4 | Contributed by Michael Tiemann <tiemann@cygnus.com> |
5 | Rewritten by Mike Stump <mrs@cygnus.com>, based upon an | |
6 | initial re-implementation courtesy Tad Hunt. | |
8d08fdba MS |
7 | |
8 | This file is part of GNU CC. | |
9 | ||
10 | GNU CC is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2, or (at your option) | |
13 | any later version. | |
14 | ||
15 | GNU CC is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with GNU CC; see the file COPYING. If not, write to | |
e9fa0c7c RK |
22 | the Free Software Foundation, 59 Temple Place - Suite 330, |
23 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
24 | |
25 | ||
8d08fdba | 26 | #include "config.h" |
8d052bc7 | 27 | #include "system.h" |
8d08fdba MS |
28 | #include "tree.h" |
29 | #include "rtl.h" | |
8f17b5c5 | 30 | #include "expr.h" |
e78d8e51 | 31 | #include "libfuncs.h" |
8d08fdba MS |
32 | #include "cp-tree.h" |
33 | #include "flags.h" | |
21451173 | 34 | #include "output.h" |
6467930b | 35 | #include "except.h" |
54f92bfb | 36 | #include "toplev.h" |
6f30f1f1 | 37 | #include "tree-inline.h" |
8d08fdba | 38 | |
93ca4ba7 | 39 | static void push_eh_cleanup PARAMS ((tree)); |
52a11cbf | 40 | static tree prepare_eh_type PARAMS ((tree)); |
158991b7 | 41 | static tree build_eh_type_type PARAMS ((tree)); |
52a11cbf | 42 | static tree do_begin_catch PARAMS ((void)); |
93ca4ba7 | 43 | static int dtor_nothrow PARAMS ((tree)); |
52a11cbf RH |
44 | static tree do_end_catch PARAMS ((tree)); |
45 | static void push_eh_cleanup PARAMS ((tree)); | |
46 | static bool decl_is_java_type PARAMS ((tree decl, int err)); | |
52a11cbf RH |
47 | static void initialize_handler_parm PARAMS ((tree, tree)); |
48 | static tree do_allocate_exception PARAMS ((tree)); | |
6f30f1f1 JM |
49 | static tree stabilize_throw_expr PARAMS ((tree, tree *)); |
50 | static tree wrap_cleanups_r PARAMS ((tree *, int *, void *)); | |
158991b7 | 51 | static int complete_ptr_ref_or_void_ptr_p PARAMS ((tree, tree)); |
d064d75a | 52 | static bool is_admissible_throw_operand PARAMS ((tree)); |
fbd40359 ZW |
53 | static int can_convert_eh PARAMS ((tree, tree)); |
54 | static void check_handlers_1 PARAMS ((tree, tree)); | |
e6855a2d | 55 | static tree cp_protect_cleanup_actions PARAMS ((void)); |
8d2733ca | 56 | |
52a11cbf | 57 | /* Sets up all the global eh stuff that needs to be initialized at the |
bbd0d54a | 58 | start of compilation. */ |
8d08fdba | 59 | |
8d08fdba | 60 | void |
8d2733ca | 61 | init_exception_processing () |
8d08fdba | 62 | { |
52a11cbf RH |
63 | tree tmp; |
64 | ||
52a11cbf | 65 | /* void std::terminate (); */ |
1dbb6023 | 66 | push_namespace (std_identifier); |
52a11cbf RH |
67 | tmp = build_function_type (void_type_node, void_list_node); |
68 | terminate_node = build_cp_library_fn_ptr ("terminate", tmp); | |
9cd64686 | 69 | TREE_THIS_VOLATILE (terminate_node) = 1; |
0c11ada6 | 70 | TREE_NOTHROW (terminate_node) = 1; |
1dbb6023 | 71 | pop_namespace (); |
8ccc31eb | 72 | |
52a11cbf RH |
73 | /* void __cxa_call_unexpected(void *); */ |
74 | tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
75 | tmp = build_function_type (void_type_node, tmp); | |
76 | call_unexpected_node | |
77 | = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp); | |
6874c264 | 78 | |
52a11cbf RH |
79 | eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS |
80 | ? "__gxx_personality_sj0" | |
81 | : "__gxx_personality_v0"); | |
6874c264 | 82 | |
52a11cbf | 83 | lang_eh_runtime_type = build_eh_type_type; |
e6855a2d | 84 | lang_protect_cleanup_actions = &cp_protect_cleanup_actions; |
6874c264 JM |
85 | } |
86 | ||
e6855a2d | 87 | /* Returns an expression to be executed if an unhandled exception is |
aba649ba | 88 | propagated out of a cleanup region. */ |
e6855a2d MM |
89 | |
90 | static tree | |
91 | cp_protect_cleanup_actions () | |
92 | { | |
93 | /* [except.terminate] | |
94 | ||
95 | When the destruction of an object during stack unwinding exits | |
96 | using an exception ... void terminate(); is called. */ | |
97 | return build_call (terminate_node, NULL_TREE); | |
98 | } | |
99 | ||
6874c264 | 100 | static tree |
52a11cbf | 101 | prepare_eh_type (type) |
f30432d7 | 102 | tree type; |
8d08fdba | 103 | { |
52a11cbf RH |
104 | if (type == NULL_TREE) |
105 | return type; | |
f30432d7 MS |
106 | if (type == error_mark_node) |
107 | return error_mark_node; | |
8d2733ca | 108 | |
e92cc029 | 109 | /* peel back references, so they match. */ |
f30432d7 MS |
110 | if (TREE_CODE (type) == REFERENCE_TYPE) |
111 | type = TREE_TYPE (type); | |
8d08fdba | 112 | |
e92cc029 | 113 | /* Peel off cv qualifiers. */ |
f30432d7 | 114 | type = TYPE_MAIN_VARIANT (type); |
8d2733ca | 115 | |
52a11cbf | 116 | return type; |
8d08fdba | 117 | } |
8d08fdba | 118 | |
e5f614d7 | 119 | /* Build the address of a typeinfo decl for use in the runtime |
c6002625 | 120 | matching field of the exception model. */ |
5816cb14 AM |
121 | |
122 | static tree | |
52a11cbf | 123 | build_eh_type_type (type) |
5816cb14 AM |
124 | tree type; |
125 | { | |
5816cb14 | 126 | tree exp; |
5816cb14 | 127 | |
93ca4ba7 JM |
128 | if (type == NULL_TREE || type == error_mark_node) |
129 | return type; | |
5816cb14 | 130 | |
52a11cbf RH |
131 | if (decl_is_java_type (type, 0)) |
132 | exp = build_java_class_ref (TREE_TYPE (type)); | |
133 | else | |
134 | exp = get_tinfo_decl (type); | |
5816cb14 | 135 | |
e5f614d7 | 136 | mark_used (exp); |
db48b831 | 137 | exp = build1 (ADDR_EXPR, ptr_type_node, exp); |
5816cb14 | 138 | |
52a11cbf | 139 | return exp; |
5816cb14 AM |
140 | } |
141 | ||
52a11cbf RH |
142 | tree |
143 | build_exc_ptr () | |
144 | { | |
145 | return build (EXC_PTR_EXPR, ptr_type_node); | |
146 | } | |
147 | ||
148 | /* Build up a call to __cxa_begin_catch, to tell the runtime that the | |
149 | exception has been handled. */ | |
93ca4ba7 | 150 | |
52a11cbf RH |
151 | static tree |
152 | do_begin_catch () | |
9c606f69 | 153 | { |
52a11cbf RH |
154 | tree fn; |
155 | ||
156 | fn = get_identifier ("__cxa_begin_catch"); | |
157 | if (IDENTIFIER_GLOBAL_VALUE (fn)) | |
158 | fn = IDENTIFIER_GLOBAL_VALUE (fn); | |
159 | else | |
9c606f69 | 160 | { |
52a11cbf RH |
161 | /* Declare void* __cxa_begin_catch (void *). */ |
162 | tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
163 | fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp)); | |
9c606f69 | 164 | } |
52a11cbf RH |
165 | |
166 | return build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (), | |
167 | NULL_TREE)); | |
9c606f69 AM |
168 | } |
169 | ||
93ca4ba7 JM |
170 | /* Returns nonzero if cleaning up an exception of type TYPE (which can be |
171 | NULL_TREE for a ... handler) will not throw an exception. */ | |
172 | ||
173 | static int | |
174 | dtor_nothrow (type) | |
175 | tree type; | |
176 | { | |
93ca4ba7 JM |
177 | if (type == NULL_TREE) |
178 | return 0; | |
179 | ||
180 | if (! TYPE_HAS_DESTRUCTOR (type)) | |
181 | return 1; | |
182 | ||
50ad9642 | 183 | return TREE_NOTHROW (CLASSTYPE_DESTRUCTORS (type)); |
93ca4ba7 JM |
184 | } |
185 | ||
52a11cbf | 186 | /* Build up a call to __cxa_end_catch, to destroy the exception object |
93ca4ba7 | 187 | for the current catch block if no others are currently using it. */ |
6467930b | 188 | |
c7ae64f2 | 189 | static tree |
52a11cbf | 190 | do_end_catch (type) |
93ca4ba7 | 191 | tree type; |
72b7eeff | 192 | { |
6874c264 | 193 | tree fn, cleanup; |
52a11cbf RH |
194 | |
195 | fn = get_identifier ("__cxa_end_catch"); | |
6874c264 JM |
196 | if (IDENTIFIER_GLOBAL_VALUE (fn)) |
197 | fn = IDENTIFIER_GLOBAL_VALUE (fn); | |
198 | else | |
199 | { | |
52a11cbf RH |
200 | /* Declare void __cxa_end_catch (). */ |
201 | fn = push_void_library_fn (fn, void_list_node); | |
0c11ada6 JM |
202 | /* This can throw if the destructor for the exception throws. */ |
203 | TREE_NOTHROW (fn) = 0; | |
6874c264 | 204 | } |
72b7eeff | 205 | |
52a11cbf | 206 | cleanup = build_function_call (fn, NULL_TREE); |
93ca4ba7 | 207 | TREE_NOTHROW (cleanup) = dtor_nothrow (type); |
52a11cbf | 208 | |
c0700ea5 | 209 | return cleanup; |
c7ae64f2 JM |
210 | } |
211 | ||
212 | /* This routine creates the cleanup for the current exception. */ | |
72b7eeff | 213 | |
c7ae64f2 | 214 | static void |
93ca4ba7 JM |
215 | push_eh_cleanup (type) |
216 | tree type; | |
c7ae64f2 | 217 | { |
52a11cbf | 218 | finish_decl_cleanup (NULL_TREE, do_end_catch (type)); |
f4a23343 JM |
219 | } |
220 | ||
e97f22c9 TT |
221 | /* Return nonzero value if DECL is a Java type suitable for catch or |
222 | throw. */ | |
223 | ||
52a11cbf | 224 | static bool |
e97f22c9 TT |
225 | decl_is_java_type (decl, err) |
226 | tree decl; | |
227 | int err; | |
228 | { | |
52a11cbf RH |
229 | bool r = (TREE_CODE (decl) == POINTER_TYPE |
230 | && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
231 | && TYPE_FOR_JAVA (TREE_TYPE (decl))); | |
e97f22c9 TT |
232 | |
233 | if (err) | |
234 | { | |
235 | if (TREE_CODE (decl) == REFERENCE_TYPE | |
236 | && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
237 | && TYPE_FOR_JAVA (TREE_TYPE (decl))) | |
238 | { | |
239 | /* Can't throw a reference. */ | |
33bd39a2 | 240 | error ("type `%T' is disallowed in Java `throw' or `catch'", |
e97f22c9 TT |
241 | decl); |
242 | } | |
243 | ||
244 | if (r) | |
245 | { | |
246 | tree jthrow_node | |
247 | = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jthrowable")); | |
400500c4 | 248 | |
e97f22c9 | 249 | if (jthrow_node == NULL_TREE) |
400500c4 RK |
250 | fatal_error |
251 | ("call to Java `catch' or `throw' with `jthrowable' undefined"); | |
252 | ||
e97f22c9 TT |
253 | jthrow_node = TREE_TYPE (TREE_TYPE (jthrow_node)); |
254 | ||
255 | if (! DERIVED_FROM_P (jthrow_node, TREE_TYPE (decl))) | |
256 | { | |
257 | /* Thrown object must be a Throwable. */ | |
33bd39a2 | 258 | error ("type `%T' is not derived from `java::lang::Throwable'", |
e97f22c9 TT |
259 | TREE_TYPE (decl)); |
260 | } | |
261 | } | |
262 | } | |
263 | ||
264 | return r; | |
265 | } | |
266 | ||
1f730ff7 ZW |
267 | /* Select the personality routine to be used for exception handling, |
268 | or issue an error if we need two different ones in the same | |
269 | translation unit. | |
270 | ??? At present eh_personality_libfunc is set to | |
271 | __gxx_personality_(sj|v)0 in init_exception_processing - should it | |
272 | be done here instead? */ | |
273 | void | |
274 | choose_personality_routine (lang) | |
275 | enum languages lang; | |
52a11cbf RH |
276 | { |
277 | static enum { | |
278 | chose_none, | |
279 | chose_cpp, | |
280 | chose_java, | |
281 | gave_error | |
282 | } state; | |
283 | ||
284 | switch (state) | |
285 | { | |
1f730ff7 ZW |
286 | case gave_error: |
287 | return; | |
52a11cbf RH |
288 | |
289 | case chose_cpp: | |
1f730ff7 ZW |
290 | if (lang != lang_cplusplus) |
291 | goto give_error; | |
292 | return; | |
293 | ||
52a11cbf | 294 | case chose_java: |
1f730ff7 ZW |
295 | if (lang != lang_java) |
296 | goto give_error; | |
297 | return; | |
298 | ||
299 | case chose_none: | |
300 | ; /* proceed to language selection */ | |
301 | } | |
302 | ||
303 | switch (lang) | |
304 | { | |
305 | case lang_cplusplus: | |
306 | state = chose_cpp; | |
52a11cbf RH |
307 | break; |
308 | ||
1f730ff7 ZW |
309 | case lang_java: |
310 | state = chose_java; | |
311 | eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS | |
312 | ? "__gcj_personality_sj0" | |
313 | : "__gcj_personality_v0"); | |
52a11cbf | 314 | break; |
1f730ff7 ZW |
315 | |
316 | default: | |
317 | abort (); | |
52a11cbf | 318 | } |
1f730ff7 ZW |
319 | return; |
320 | ||
321 | give_error: | |
322 | error ("mixing C++ and Java catches in a single translation unit"); | |
323 | state = gave_error; | |
52a11cbf RH |
324 | } |
325 | ||
b35d4555 | 326 | /* Initialize the catch parameter DECL. */ |
6467930b | 327 | |
b35d4555 | 328 | static void |
52a11cbf | 329 | initialize_handler_parm (decl, exp) |
3c5c0849 | 330 | tree decl; |
52a11cbf | 331 | tree exp; |
8d08fdba | 332 | { |
b35d4555 MM |
333 | tree init; |
334 | tree init_type; | |
335 | ||
336 | /* Make sure we mark the catch param as used, otherwise we'll get a | |
337 | warning about an unused ((anonymous)). */ | |
338 | TREE_USED (decl) = 1; | |
339 | ||
52a11cbf RH |
340 | /* Figure out the type that the initializer is. Pointers are returned |
341 | adjusted by value from __cxa_begin_catch. Others are returned by | |
342 | reference. */ | |
b35d4555 | 343 | init_type = TREE_TYPE (decl); |
c93586fa | 344 | if (! TYPE_PTR_P (init_type) |
52a11cbf | 345 | && TREE_CODE (init_type) != REFERENCE_TYPE) |
b35d4555 MM |
346 | init_type = build_reference_type (init_type); |
347 | ||
1f730ff7 ZW |
348 | choose_personality_routine (decl_is_java_type (init_type, 0) |
349 | ? lang_java : lang_cplusplus); | |
b35d4555 MM |
350 | |
351 | /* Since pointers are passed by value, initialize a reference to | |
52a11cbf | 352 | pointer catch parm with the address of the temporary. */ |
c93586fa RS |
353 | if (TREE_CODE (init_type) == REFERENCE_TYPE |
354 | && TYPE_PTR_P (TREE_TYPE (init_type))) | |
b35d4555 MM |
355 | exp = build_unary_op (ADDR_EXPR, exp, 1); |
356 | ||
52a11cbf | 357 | exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0); |
b35d4555 MM |
358 | |
359 | init = convert_from_reference (exp); | |
360 | ||
361 | /* If the constructor for the catch parm exits via an exception, we | |
362 | must call terminate. See eh23.C. */ | |
363 | if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))) | |
faf5394a | 364 | { |
b35d4555 MM |
365 | /* Generate the copy constructor call directly so we can wrap it. |
366 | See also expand_default_init. */ | |
367 | init = ocp_convert (TREE_TYPE (decl), init, | |
368 | CONV_IMPLICIT|CONV_FORCE_TEMP, 0); | |
52a11cbf | 369 | init = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (init), init); |
faf5394a MS |
370 | } |
371 | ||
b35d4555 MM |
372 | /* Let `cp_finish_decl' know that this initializer is ok. */ |
373 | DECL_INITIAL (decl) = error_mark_node; | |
374 | decl = pushdecl (decl); | |
8d2733ca | 375 | |
b35d4555 | 376 | start_decl_1 (decl); |
cd9f6678 | 377 | cp_finish_decl (decl, init, NULL_TREE, |
b35d4555 | 378 | LOOKUP_ONLYCONVERTING|DIRECT_BIND); |
5816cb14 AM |
379 | } |
380 | ||
b35d4555 | 381 | /* Call this to start a catch block. DECL is the catch parameter. */ |
5816cb14 | 382 | |
b35d4555 MM |
383 | tree |
384 | expand_start_catch_block (decl) | |
3c5c0849 | 385 | tree decl; |
5816cb14 | 386 | { |
52a11cbf RH |
387 | tree exp = NULL_TREE; |
388 | tree type; | |
389 | bool is_java; | |
5816cb14 | 390 | |
b35d4555 MM |
391 | if (! doing_eh (1)) |
392 | return NULL_TREE; | |
5816cb14 | 393 | |
b35d4555 | 394 | /* Make sure this declaration is reasonable. */ |
3c5c0849 MM |
395 | if (decl && !complete_ptr_ref_or_void_ptr_p (TREE_TYPE (decl), NULL_TREE)) |
396 | decl = NULL_TREE; | |
5816cb14 | 397 | |
52a11cbf RH |
398 | if (decl) |
399 | type = prepare_eh_type (TREE_TYPE (decl)); | |
400 | else | |
401 | type = NULL_TREE; | |
52a11cbf RH |
402 | |
403 | is_java = false; | |
404 | if (decl) | |
e97f22c9 | 405 | { |
52a11cbf | 406 | tree init; |
5816cb14 | 407 | |
52a11cbf RH |
408 | if (decl_is_java_type (type, 1)) |
409 | { | |
410 | /* Java only passes object via pointer and doesn't require | |
411 | adjusting. The java object is immediately before the | |
412 | generic exception header. */ | |
413 | init = build_exc_ptr (); | |
414 | init = build1 (NOP_EXPR, build_pointer_type (type), init); | |
415 | init = build (MINUS_EXPR, TREE_TYPE (init), init, | |
416 | TYPE_SIZE_UNIT (TREE_TYPE (init))); | |
3e411c3f | 417 | init = build_indirect_ref (init, NULL); |
52a11cbf RH |
418 | is_java = true; |
419 | } | |
e97f22c9 | 420 | else |
52a11cbf RH |
421 | { |
422 | /* C++ requires that we call __cxa_begin_catch to get the | |
423 | pointer to the actual object. */ | |
424 | init = do_begin_catch (); | |
425 | } | |
426 | ||
427 | exp = create_temporary_var (ptr_type_node); | |
428 | DECL_REGISTER (exp) = 1; | |
429 | cp_finish_decl (exp, init, NULL_TREE, LOOKUP_ONLYCONVERTING); | |
430 | finish_expr_stmt (build_modify_expr (exp, INIT_EXPR, init)); | |
e97f22c9 TT |
431 | } |
432 | else | |
52a11cbf RH |
433 | finish_expr_stmt (do_begin_catch ()); |
434 | ||
435 | /* C++ requires that we call __cxa_end_catch at the end of | |
436 | processing the exception. */ | |
437 | if (! is_java) | |
438 | push_eh_cleanup (type); | |
5816cb14 | 439 | |
b35d4555 | 440 | if (decl) |
52a11cbf | 441 | initialize_handler_parm (decl, exp); |
5816cb14 | 442 | |
1a6025b4 | 443 | return type; |
5816cb14 AM |
444 | } |
445 | ||
f30432d7 | 446 | |
8d2733ca MS |
447 | /* Call this to end a catch block. Its responsible for emitting the |
448 | code to handle jumping back to the correct place, and for emitting | |
449 | the label to jump to if this catch block didn't match. */ | |
6467930b | 450 | |
824b9a4c | 451 | void |
1a6025b4 | 452 | expand_end_catch_block () |
8d08fdba | 453 | { |
f30432d7 MS |
454 | if (! doing_eh (1)) |
455 | return; | |
8d2733ca | 456 | |
0dde4175 JM |
457 | /* The exception being handled is rethrown if control reaches the end of |
458 | a handler of the function-try-block of a constructor or destructor. */ | |
459 | if (in_function_try_handler | |
460 | && (DECL_CONSTRUCTOR_P (current_function_decl) | |
461 | || DECL_DESTRUCTOR_P (current_function_decl))) | |
b35d4555 | 462 | finish_expr_stmt (build_throw (NULL_TREE)); |
8d2733ca | 463 | } |
8d08fdba | 464 | |
b35d4555 | 465 | tree |
52a11cbf | 466 | begin_eh_spec_block () |
f30432d7 | 467 | { |
52a11cbf RH |
468 | tree r = build_stmt (EH_SPEC_BLOCK, NULL_TREE, NULL_TREE); |
469 | add_stmt (r); | |
470 | return r; | |
f30432d7 MS |
471 | } |
472 | ||
b35d4555 | 473 | void |
52a11cbf RH |
474 | finish_eh_spec_block (raw_raises, eh_spec_block) |
475 | tree raw_raises; | |
476 | tree eh_spec_block; | |
f30432d7 | 477 | { |
52a11cbf | 478 | tree raises; |
0c11ada6 | 479 | |
52a11cbf | 480 | RECHAIN_STMTS (eh_spec_block, EH_SPEC_STMTS (eh_spec_block)); |
6c20b7e9 | 481 | |
52a11cbf RH |
482 | /* Strip cv quals, etc, from the specification types. */ |
483 | for (raises = NULL_TREE; | |
484 | raw_raises && TREE_VALUE (raw_raises); | |
485 | raw_raises = TREE_CHAIN (raw_raises)) | |
486 | raises = tree_cons (NULL_TREE, prepare_eh_type (TREE_VALUE (raw_raises)), | |
487 | raises); | |
6c20b7e9 | 488 | |
52a11cbf | 489 | EH_SPEC_RAISES (eh_spec_block) = raises; |
f30432d7 MS |
490 | } |
491 | ||
52a11cbf | 492 | /* Return a pointer to a buffer for an exception object of type TYPE. */ |
6467930b | 493 | |
52a11cbf RH |
494 | static tree |
495 | do_allocate_exception (type) | |
496 | tree type; | |
8d08fdba | 497 | { |
52a11cbf | 498 | tree fn; |
eb448459 | 499 | |
52a11cbf RH |
500 | fn = get_identifier ("__cxa_allocate_exception"); |
501 | if (IDENTIFIER_GLOBAL_VALUE (fn)) | |
502 | fn = IDENTIFIER_GLOBAL_VALUE (fn); | |
503 | else | |
f30432d7 | 504 | { |
52a11cbf | 505 | /* Declare void *__cxa_allocate_exception(size_t). */ |
c9f8536c | 506 | tree tmp = tree_cons (NULL_TREE, size_type_node, void_list_node); |
52a11cbf | 507 | fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp)); |
e00737d2 | 508 | } |
52a11cbf RH |
509 | |
510 | return build_function_call (fn, tree_cons (NULL_TREE, size_in_bytes (type), | |
511 | NULL_TREE)); | |
8d08fdba MS |
512 | } |
513 | ||
2ac8a0f9 JM |
514 | #if 0 |
515 | /* Call __cxa_free_exception from a cleanup. This is never invoked | |
6f30f1f1 | 516 | directly, but see the comment for stabilize_throw_expr. */ |
f4a23343 | 517 | |
c6160f8f | 518 | static tree |
52a11cbf RH |
519 | do_free_exception (ptr) |
520 | tree ptr; | |
f4a23343 | 521 | { |
52a11cbf | 522 | tree fn; |
f4a23343 | 523 | |
52a11cbf | 524 | fn = get_identifier ("__cxa_free_exception"); |
f4a23343 JM |
525 | if (IDENTIFIER_GLOBAL_VALUE (fn)) |
526 | fn = IDENTIFIER_GLOBAL_VALUE (fn); | |
527 | else | |
528 | { | |
52a11cbf RH |
529 | /* Declare void __cxa_free_exception (void *). */ |
530 | fn = push_void_library_fn (fn, tree_cons (NULL_TREE, ptr_type_node, | |
531 | void_list_node)); | |
f4a23343 JM |
532 | } |
533 | ||
52a11cbf | 534 | return build_function_call (fn, tree_cons (NULL_TREE, ptr, NULL_TREE)); |
f4a23343 | 535 | } |
2ac8a0f9 | 536 | #endif |
f4a23343 | 537 | |
6f30f1f1 JM |
538 | /* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR. |
539 | Called from build_throw via walk_tree_without_duplicates. */ | |
540 | ||
541 | static tree | |
542 | wrap_cleanups_r (tp, walk_subtrees, data) | |
543 | tree *tp; | |
544 | int *walk_subtrees ATTRIBUTE_UNUSED; | |
545 | void *data ATTRIBUTE_UNUSED; | |
546 | { | |
547 | tree exp = *tp; | |
548 | tree cleanup; | |
549 | ||
550 | /* Don't walk into types. */ | |
551 | if (TYPE_P (exp)) | |
552 | { | |
553 | *walk_subtrees = 0; | |
554 | return NULL_TREE; | |
555 | } | |
556 | if (TREE_CODE (exp) != TARGET_EXPR) | |
557 | return NULL_TREE; | |
558 | ||
559 | cleanup = TARGET_EXPR_CLEANUP (exp); | |
560 | if (cleanup) | |
561 | { | |
562 | cleanup = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (cleanup), cleanup); | |
563 | TARGET_EXPR_CLEANUP (exp) = cleanup; | |
564 | } | |
565 | ||
566 | /* Keep iterating. */ | |
567 | return NULL_TREE; | |
568 | } | |
569 | ||
570 | /* Like stabilize_expr, but specifically for a thrown expression. When | |
571 | throwing a temporary class object, we want to construct it directly into | |
572 | the thrown exception, so we look past the TARGET_EXPR and stabilize the | |
573 | arguments of the call instead. | |
574 | ||
575 | The case where EXP is a call to a function returning a class is a bit of | |
576 | a grey area in the standard; it's unclear whether or not it should be | |
577 | allowed to throw. I'm going to say no, as that allows us to optimize | |
578 | this case without worrying about deallocating the exception object if it | |
579 | does. The alternatives would be either not optimizing this case, or | |
580 | wrapping the initialization in a TRY_CATCH_EXPR to call do_free_exception | |
581 | rather than in a MUST_NOT_THROW_EXPR, for this case only. */ | |
582 | ||
583 | static tree | |
584 | stabilize_throw_expr (exp, initp) | |
585 | tree exp; | |
586 | tree *initp; | |
587 | { | |
588 | tree init_expr; | |
589 | ||
590 | if (TREE_CODE (exp) == TARGET_EXPR | |
591 | && TREE_CODE (TARGET_EXPR_INITIAL (exp)) == AGGR_INIT_EXPR | |
592 | && flag_elide_constructors) | |
593 | { | |
594 | tree aggr_init = AGGR_INIT_EXPR_CHECK (TARGET_EXPR_INITIAL (exp)); | |
595 | tree args = TREE_OPERAND (aggr_init, 1); | |
596 | tree newargs = NULL_TREE; | |
597 | tree *p = &newargs; | |
598 | ||
599 | init_expr = void_zero_node; | |
600 | for (; args; args = TREE_CHAIN (args)) | |
601 | { | |
bab076f7 | 602 | tree arg = TREE_VALUE (args); |
6f30f1f1 | 603 | tree arg_init_expr; |
6f30f1f1 | 604 | |
c246c65d JM |
605 | arg = stabilize_expr (arg, &arg_init_expr); |
606 | ||
607 | if (TREE_SIDE_EFFECTS (arg_init_expr)) | |
608 | init_expr = build (COMPOUND_EXPR, void_type_node, init_expr, | |
609 | arg_init_expr); | |
bab076f7 | 610 | *p = tree_cons (NULL_TREE, arg, NULL_TREE); |
6f30f1f1 JM |
611 | p = &TREE_CHAIN (*p); |
612 | } | |
613 | TREE_OPERAND (aggr_init, 1) = newargs; | |
614 | } | |
615 | else | |
616 | { | |
617 | exp = stabilize_expr (exp, &init_expr); | |
618 | } | |
619 | ||
620 | *initp = init_expr; | |
621 | return exp; | |
622 | } | |
623 | ||
52a11cbf | 624 | /* Build a throw expression. */ |
6467930b | 625 | |
52a11cbf RH |
626 | tree |
627 | build_throw (exp) | |
8d2733ca | 628 | tree exp; |
8d08fdba | 629 | { |
6874c264 | 630 | tree fn; |
8d08fdba | 631 | |
52a11cbf RH |
632 | if (exp == error_mark_node) |
633 | return exp; | |
634 | ||
635 | if (processing_template_decl) | |
636 | return build_min (THROW_EXPR, void_type_node, exp); | |
637 | ||
638 | if (exp == null_node) | |
33bd39a2 | 639 | warning ("throwing NULL, which has integral, not pointer type"); |
52a11cbf RH |
640 | |
641 | if (exp != NULL_TREE) | |
642 | { | |
643 | if (!is_admissible_throw_operand (exp)) | |
644 | return error_mark_node; | |
645 | } | |
646 | ||
8d2733ca | 647 | if (! doing_eh (1)) |
59ccf49d | 648 | return error_mark_node; |
8d08fdba | 649 | |
52a11cbf | 650 | if (exp && decl_is_java_type (TREE_TYPE (exp), 1)) |
e97f22c9 | 651 | { |
52a11cbf | 652 | tree fn = get_identifier ("_Jv_Throw"); |
e97f22c9 TT |
653 | if (IDENTIFIER_GLOBAL_VALUE (fn)) |
654 | fn = IDENTIFIER_GLOBAL_VALUE (fn); | |
655 | else | |
656 | { | |
52a11cbf | 657 | /* Declare void _Jv_Throw (void *). */ |
0c11ada6 JM |
658 | tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node); |
659 | tmp = build_function_type (ptr_type_node, tmp); | |
c00996a3 | 660 | fn = push_throw_library_fn (fn, tmp); |
e97f22c9 TT |
661 | } |
662 | ||
52a11cbf | 663 | exp = build_function_call (fn, tree_cons (NULL_TREE, exp, NULL_TREE)); |
e97f22c9 TT |
664 | } |
665 | else if (exp) | |
8d2733ca | 666 | { |
faae18ab | 667 | tree throw_type; |
52a11cbf | 668 | tree cleanup; |
52a11cbf RH |
669 | tree object, ptr; |
670 | tree tmp; | |
6f30f1f1 | 671 | tree temp_expr, allocate_expr; |
52a11cbf RH |
672 | |
673 | fn = get_identifier ("__cxa_throw"); | |
674 | if (IDENTIFIER_GLOBAL_VALUE (fn)) | |
675 | fn = IDENTIFIER_GLOBAL_VALUE (fn); | |
676 | else | |
677 | { | |
678 | /* The CLEANUP_TYPE is the internal type of a destructor. */ | |
679 | if (cleanup_type == NULL_TREE) | |
680 | { | |
681 | tmp = void_list_node; | |
682 | tmp = tree_cons (NULL_TREE, ptr_type_node, tmp); | |
683 | tmp = build_function_type (void_type_node, tmp); | |
684 | cleanup_type = build_pointer_type (tmp); | |
685 | } | |
686 | ||
687 | /* Declare void __cxa_throw (void*, void*, void (*)(void*)). */ | |
688 | /* ??? Second argument is supposed to be "std::type_info*". */ | |
689 | tmp = void_list_node; | |
690 | tmp = tree_cons (NULL_TREE, cleanup_type, tmp); | |
691 | tmp = tree_cons (NULL_TREE, ptr_type_node, tmp); | |
692 | tmp = tree_cons (NULL_TREE, ptr_type_node, tmp); | |
693 | tmp = build_function_type (void_type_node, tmp); | |
694 | fn = push_throw_library_fn (fn, tmp); | |
695 | } | |
59ccf49d | 696 | |
a3b49ccd | 697 | /* throw expression */ |
e92cc029 | 698 | /* First, decay it. */ |
f30432d7 | 699 | exp = decay_conversion (exp); |
a3b49ccd | 700 | |
52a11cbf RH |
701 | /* OK, this is kind of wacky. The standard says that we call |
702 | terminate when the exception handling mechanism, after | |
703 | completing evaluation of the expression to be thrown but | |
704 | before the exception is caught (_except.throw_), calls a | |
705 | user function that exits via an uncaught exception. | |
706 | ||
707 | So we have to protect the actual initialization of the | |
708 | exception object with terminate(), but evaluate the | |
709 | expression first. Since there could be temps in the | |
710 | expression, we need to handle that, too. We also expand | |
711 | the call to __cxa_allocate_exception first (which doesn't | |
712 | matter, since it can't throw). */ | |
713 | ||
6f30f1f1 JM |
714 | /* Pre-evaluate the thrown expression first, since if we allocated |
715 | the space first we would have to deal with cleaning it up if | |
716 | evaluating this expression throws. */ | |
717 | exp = stabilize_throw_expr (exp, &temp_expr); | |
6874c264 | 718 | |
52a11cbf | 719 | /* Allocate the space for the exception. */ |
6f30f1f1 JM |
720 | allocate_expr = do_allocate_exception (TREE_TYPE (exp)); |
721 | allocate_expr = get_target_expr (allocate_expr); | |
722 | ptr = TARGET_EXPR_SLOT (allocate_expr); | |
52a11cbf | 723 | object = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (exp)), ptr); |
3e411c3f | 724 | object = build_indirect_ref (object, NULL); |
faae18ab | 725 | |
6f30f1f1 JM |
726 | /* And initialize the exception object. */ |
727 | exp = build_init (object, exp, LOOKUP_ONLYCONVERTING); | |
52a11cbf | 728 | if (exp == error_mark_node) |
6f30f1f1 JM |
729 | { |
730 | error (" in thrown expression"); | |
731 | return error_mark_node; | |
732 | } | |
f4a23343 | 733 | |
2ac8a0f9 | 734 | exp = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (exp), exp); |
6f30f1f1 JM |
735 | /* Prepend the allocation. */ |
736 | exp = build (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp); | |
737 | if (temp_expr != void_zero_node) | |
738 | { | |
739 | /* Prepend the calculation of the throw expression. Also, force | |
740 | any cleanups from the expression to be evaluated here so that | |
741 | we don't have to do them during unwinding. But first wrap | |
742 | them in MUST_NOT_THROW_EXPR, since they are run after the | |
743 | exception object is initialized. */ | |
744 | walk_tree_without_duplicates (&temp_expr, wrap_cleanups_r, 0); | |
745 | exp = build (COMPOUND_EXPR, TREE_TYPE (exp), temp_expr, exp); | |
746 | exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp); | |
747 | } | |
72b7eeff | 748 | |
52a11cbf | 749 | throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object))); |
f4a23343 | 750 | |
52a11cbf RH |
751 | if (TYPE_HAS_DESTRUCTOR (TREE_TYPE (object))) |
752 | { | |
753 | cleanup = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)), | |
754 | complete_dtor_identifier, 0); | |
50ad9642 | 755 | cleanup = BASELINK_FUNCTIONS (cleanup); |
52a11cbf | 756 | mark_used (cleanup); |
dffd7eb6 | 757 | cxx_mark_addressable (cleanup); |
52a11cbf RH |
758 | /* Pretend it's a normal function. */ |
759 | cleanup = build1 (ADDR_EXPR, cleanup_type, cleanup); | |
f30432d7 | 760 | } |
52a11cbf | 761 | else |
6874c264 JM |
762 | { |
763 | cleanup = build_int_2 (0, 0); | |
764 | TREE_TYPE (cleanup) = cleanup_type; | |
765 | } | |
72b7eeff | 766 | |
52a11cbf RH |
767 | tmp = tree_cons (NULL_TREE, cleanup, NULL_TREE); |
768 | tmp = tree_cons (NULL_TREE, throw_type, tmp); | |
769 | tmp = tree_cons (NULL_TREE, ptr, tmp); | |
52a11cbf | 770 | /* ??? Indicate that this function call throws throw_type. */ |
6f30f1f1 | 771 | tmp = build_function_call (fn, tmp); |
72b7eeff | 772 | |
6f30f1f1 JM |
773 | /* Tack on the initialization stuff. */ |
774 | exp = build (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp); | |
8d2733ca MS |
775 | } |
776 | else | |
a3b49ccd | 777 | { |
52a11cbf | 778 | /* Rethrow current exception. */ |
6874c264 | 779 | |
52a11cbf | 780 | tree fn = get_identifier ("__cxa_rethrow"); |
6874c264 JM |
781 | if (IDENTIFIER_GLOBAL_VALUE (fn)) |
782 | fn = IDENTIFIER_GLOBAL_VALUE (fn); | |
783 | else | |
52a11cbf RH |
784 | { |
785 | /* Declare void __cxa_rethrow (void). */ | |
786 | fn = push_throw_library_fn | |
787 | (fn, build_function_type (void_type_node, void_list_node)); | |
788 | } | |
6874c264 | 789 | |
6f30f1f1 JM |
790 | /* ??? Indicate that this function call allows exceptions of the type |
791 | of the enclosing catch block (if known). */ | |
6874c264 | 792 | exp = build_function_call (fn, NULL_TREE); |
a3b49ccd | 793 | } |
8d2733ca | 794 | |
52a11cbf | 795 | exp = build1 (THROW_EXPR, void_type_node, exp); |
02020185 | 796 | |
52a11cbf | 797 | return exp; |
8d08fdba | 798 | } |
4cfbc546 NS |
799 | |
800 | /* Make sure TYPE is complete, pointer to complete, reference to | |
801 | complete, or pointer to cv void. Issue diagnostic on failure. | |
838dfd8a | 802 | Return the zero on failure and nonzero on success. FROM can be |
4cfbc546 NS |
803 | the expr or decl from whence TYPE came, if available. */ |
804 | ||
805 | static int | |
806 | complete_ptr_ref_or_void_ptr_p (type, from) | |
807 | tree type; | |
808 | tree from; | |
809 | { | |
810 | int is_ptr; | |
811 | ||
812 | /* Check complete. */ | |
813 | type = complete_type_or_else (type, from); | |
814 | if (!type) | |
815 | return 0; | |
816 | ||
817 | /* Or a pointer or ref to one, or cv void *. */ | |
818 | is_ptr = TREE_CODE (type) == POINTER_TYPE; | |
819 | if (is_ptr || TREE_CODE (type) == REFERENCE_TYPE) | |
820 | { | |
821 | tree core = TREE_TYPE (type); | |
822 | ||
b72801e2 | 823 | if (is_ptr && VOID_TYPE_P (core)) |
4cfbc546 NS |
824 | /* OK */; |
825 | else if (!complete_type_or_else (core, from)) | |
826 | return 0; | |
827 | } | |
828 | return 1; | |
829 | } | |
830 | ||
d064d75a GDR |
831 | /* Return truth-value if EXPRESSION is admissible in throw-expression, |
832 | i.e. if it is not of incomplete type or a pointer/reference to such | |
833 | a type or of an abstract class type. */ | |
834 | ||
835 | static bool | |
836 | is_admissible_throw_operand (expr) | |
837 | tree expr; | |
838 | { | |
839 | tree type = TREE_TYPE (expr); | |
840 | ||
841 | /* 15.1/4 [...] The type of the throw-expression shall not be an | |
842 | incomplete type, or a pointer or a reference to an incomplete | |
843 | type, other than void*, const void*, volatile void*, or | |
844 | const volatile void*. Except for these restriction and the | |
845 | restrictions on type matching mentioned in 15.3, the operand | |
846 | of throw is treated exactly as a function argument in a call | |
847 | (5.2.2) or the operand of a return statement. */ | |
848 | if (!complete_ptr_ref_or_void_ptr_p (type, expr)) | |
849 | return false; | |
850 | ||
851 | /* 10.4/3 An abstract class shall not be used as a parameter type, | |
852 | as a function return type or as type of an explicit | |
853 | conversion. */ | |
854 | else if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) | |
855 | { | |
33bd39a2 | 856 | error ("expression '%E' of abstract class type '%T' cannot be used in throw-expression", expr, type); |
d064d75a GDR |
857 | return false; |
858 | } | |
859 | ||
860 | return true; | |
861 | } | |
862 | ||
1660cb3a JM |
863 | /* Returns nonzero if FN is a declaration of a standard C library |
864 | function which is known not to throw. | |
865 | ||
866 | [lib.res.on.exception.handling]: None of the functions from the | |
867 | Standard C library shall report an error by throwing an | |
868 | exception, unless it calls a program-supplied function that | |
869 | throws an exception. */ | |
870 | ||
871 | #include "cfns.h" | |
872 | ||
873 | int | |
874 | nothrow_libfn_p (fn) | |
875 | tree fn; | |
876 | { | |
877 | tree id; | |
878 | ||
879 | if (TREE_PUBLIC (fn) | |
880 | && DECL_EXTERNAL (fn) | |
92643fea | 881 | && DECL_NAMESPACE_SCOPE_P (fn) |
eb68cb58 | 882 | && DECL_EXTERN_C_P (fn)) |
1660cb3a JM |
883 | /* OK */; |
884 | else | |
885 | /* Can't be a C library function. */ | |
886 | return 0; | |
887 | ||
888 | id = DECL_ASSEMBLER_NAME (fn); | |
889 | return !!libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id)); | |
890 | } | |
2bc9f1d1 JM |
891 | |
892 | /* Returns nonzero if an exception of type FROM will be caught by a | |
893 | handler for type TO, as per [except.handle]. */ | |
894 | ||
895 | static int | |
896 | can_convert_eh (to, from) | |
897 | tree to, from; | |
898 | { | |
899 | if (TREE_CODE (to) == REFERENCE_TYPE) | |
900 | to = TREE_TYPE (to); | |
901 | if (TREE_CODE (from) == REFERENCE_TYPE) | |
902 | from = TREE_TYPE (from); | |
903 | ||
904 | if (TREE_CODE (to) == POINTER_TYPE && TREE_CODE (from) == POINTER_TYPE) | |
905 | { | |
906 | to = TREE_TYPE (to); | |
907 | from = TREE_TYPE (from); | |
908 | ||
909 | if (! at_least_as_qualified_p (to, from)) | |
910 | return 0; | |
911 | ||
912 | if (TREE_CODE (to) == VOID_TYPE) | |
913 | return 1; | |
914 | ||
915 | /* else fall through */ | |
916 | } | |
917 | ||
2d46ec83 | 918 | if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from) |
2bc9f1d1 JM |
919 | && PUBLICLY_UNIQUELY_DERIVED_P (to, from)) |
920 | return 1; | |
921 | ||
922 | return 0; | |
923 | } | |
924 | ||
925 | /* Check whether any of HANDLERS are shadowed by another handler accepting | |
926 | TYPE. Note that the shadowing may not be complete; even if an exception | |
927 | of type B would be caught by a handler for A, there could be a derived | |
928 | class C for which A is an ambiguous base but B is not, so the handler | |
929 | for B would catch an exception of type C. */ | |
930 | ||
931 | static void | |
932 | check_handlers_1 (master, handlers) | |
933 | tree master; | |
934 | tree handlers; | |
935 | { | |
936 | tree type = TREE_TYPE (master); | |
937 | tree handler; | |
938 | ||
939 | for (handler = handlers; handler; handler = TREE_CHAIN (handler)) | |
940 | if (TREE_TYPE (handler) | |
941 | && can_convert_eh (type, TREE_TYPE (handler))) | |
942 | { | |
943 | lineno = STMT_LINENO (handler); | |
33bd39a2 | 944 | warning ("exception of type `%T' will be caught", |
2bc9f1d1 JM |
945 | TREE_TYPE (handler)); |
946 | lineno = STMT_LINENO (master); | |
33bd39a2 | 947 | warning (" by earlier handler for `%T'", type); |
2bc9f1d1 JM |
948 | break; |
949 | } | |
950 | } | |
951 | ||
952 | /* Given a chain of HANDLERs, make sure that they're OK. */ | |
953 | ||
954 | void | |
955 | check_handlers (handlers) | |
956 | tree handlers; | |
957 | { | |
958 | tree handler; | |
959 | int save_line = lineno; | |
960 | for (handler = handlers; handler; handler = TREE_CHAIN (handler)) | |
961 | { | |
962 | if (TREE_CHAIN (handler) == NULL_TREE) | |
963 | /* No more handlers; nothing to shadow. */; | |
964 | else if (TREE_TYPE (handler) == NULL_TREE) | |
965 | { | |
966 | lineno = STMT_LINENO (handler); | |
33bd39a2 | 967 | pedwarn |
2bc9f1d1 JM |
968 | ("`...' handler must be the last handler for its try block"); |
969 | } | |
970 | else | |
971 | check_handlers_1 (handler, TREE_CHAIN (handler)); | |
972 | } | |
973 | lineno = save_line; | |
974 | } |