name-lookup.h (get_global_value_if_present): New function.
[official-gcc.git] / gcc / cp / except.c
blob962da966b493ef456bbad70c87c2ca22884e1677
1 /* Handle exceptional things in C++.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
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.
8 This file is part of GCC.
10 GCC 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.
15 GCC 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.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "rtl.h"
32 #include "expr.h"
33 #include "libfuncs.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "except.h"
38 #include "toplev.h"
39 #include "tree-inline.h"
41 static void push_eh_cleanup (tree);
42 static tree prepare_eh_type (tree);
43 static tree build_eh_type_type (tree);
44 static tree do_begin_catch (void);
45 static int dtor_nothrow (tree);
46 static tree do_end_catch (tree);
47 static bool decl_is_java_type (tree decl, int err);
48 static void initialize_handler_parm (tree, tree);
49 static tree do_allocate_exception (tree);
50 static tree stabilize_throw_expr (tree, tree *);
51 static tree wrap_cleanups_r (tree *, int *, void *);
52 static int complete_ptr_ref_or_void_ptr_p (tree, tree);
53 static bool is_admissible_throw_operand (tree);
54 static int can_convert_eh (tree, tree);
55 static void check_handlers_1 (tree, tree);
56 static tree cp_protect_cleanup_actions (void);
58 /* Sets up all the global eh stuff that needs to be initialized at the
59 start of compilation. */
61 void
62 init_exception_processing (void)
64 tree tmp;
66 /* void std::terminate (); */
67 push_namespace (std_identifier);
68 tmp = build_function_type (void_type_node, void_list_node);
69 terminate_node = build_cp_library_fn_ptr ("terminate", tmp);
70 TREE_THIS_VOLATILE (terminate_node) = 1;
71 TREE_NOTHROW (terminate_node) = 1;
72 pop_namespace ();
74 /* void __cxa_call_unexpected(void *); */
75 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
76 tmp = build_function_type (void_type_node, tmp);
77 call_unexpected_node
78 = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
80 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
81 ? "__gxx_personality_sj0"
82 : "__gxx_personality_v0");
84 lang_eh_runtime_type = build_eh_type_type;
85 lang_protect_cleanup_actions = &cp_protect_cleanup_actions;
88 /* Returns an expression to be executed if an unhandled exception is
89 propagated out of a cleanup region. */
91 static tree
92 cp_protect_cleanup_actions (void)
94 /* [except.terminate]
96 When the destruction of an object during stack unwinding exits
97 using an exception ... void terminate(); is called. */
98 return build_call (terminate_node, NULL_TREE);
101 static tree
102 prepare_eh_type (tree type)
104 if (type == NULL_TREE)
105 return type;
106 if (type == error_mark_node)
107 return error_mark_node;
109 /* peel back references, so they match. */
110 type = non_reference (type);
112 /* Peel off cv qualifiers. */
113 type = TYPE_MAIN_VARIANT (type);
115 return type;
118 /* Return the type info for TYPE as used by EH machinery. */
119 tree
120 eh_type_info (tree type)
122 tree exp;
124 if (type == NULL_TREE || type == error_mark_node)
125 return type;
127 if (decl_is_java_type (type, 0))
128 exp = build_java_class_ref (TREE_TYPE (type));
129 else
130 exp = get_tinfo_decl (type);
132 return exp;
135 /* Build the address of a typeinfo decl for use in the runtime
136 matching field of the exception model. */
138 static tree
139 build_eh_type_type (tree type)
141 tree exp = eh_type_info (type);
143 if (!exp)
144 return NULL;
146 return build1 (ADDR_EXPR, ptr_type_node, exp);
149 tree
150 build_exc_ptr (void)
152 return build (EXC_PTR_EXPR, ptr_type_node);
155 /* Build up a call to __cxa_begin_catch, to tell the runtime that the
156 exception has been handled. */
158 static tree
159 do_begin_catch (void)
161 tree fn;
163 fn = get_identifier ("__cxa_begin_catch");
164 if (!get_global_value_if_present (fn, &fn))
166 /* Declare void* __cxa_begin_catch (void *). */
167 tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
168 fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp));
171 return build_function_call (fn, tree_cons (NULL_TREE, build_exc_ptr (),
172 NULL_TREE));
175 /* Returns nonzero if cleaning up an exception of type TYPE (which can be
176 NULL_TREE for a ... handler) will not throw an exception. */
178 static int
179 dtor_nothrow (tree type)
181 if (type == NULL_TREE)
182 return 0;
184 if (! TYPE_HAS_DESTRUCTOR (type))
185 return 1;
187 return TREE_NOTHROW (CLASSTYPE_DESTRUCTORS (type));
190 /* Build up a call to __cxa_end_catch, to destroy the exception object
191 for the current catch block if no others are currently using it. */
193 static tree
194 do_end_catch (tree type)
196 tree fn, cleanup;
198 fn = get_identifier ("__cxa_end_catch");
199 if (!get_global_value_if_present (fn, &fn))
201 /* Declare void __cxa_end_catch (). */
202 fn = push_void_library_fn (fn, void_list_node);
203 /* This can throw if the destructor for the exception throws. */
204 TREE_NOTHROW (fn) = 0;
207 cleanup = build_function_call (fn, NULL_TREE);
208 TREE_NOTHROW (cleanup) = dtor_nothrow (type);
210 return cleanup;
213 /* This routine creates the cleanup for the current exception. */
215 static void
216 push_eh_cleanup (tree type)
218 finish_decl_cleanup (NULL_TREE, do_end_catch (type));
221 /* Return nonzero value if DECL is a Java type suitable for catch or
222 throw. */
224 static bool
225 decl_is_java_type (tree decl, int err)
227 bool r = (TREE_CODE (decl) == POINTER_TYPE
228 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
229 && TYPE_FOR_JAVA (TREE_TYPE (decl)));
231 if (err)
233 if (TREE_CODE (decl) == REFERENCE_TYPE
234 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
235 && TYPE_FOR_JAVA (TREE_TYPE (decl)))
237 /* Can't throw a reference. */
238 error ("type `%T' is disallowed in Java `throw' or `catch'",
239 decl);
242 if (r)
244 tree jthrow_node
245 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jthrowable"));
247 if (jthrow_node == NULL_TREE)
248 fatal_error
249 ("call to Java `catch' or `throw' with `jthrowable' undefined");
251 jthrow_node = TREE_TYPE (TREE_TYPE (jthrow_node));
253 if (! DERIVED_FROM_P (jthrow_node, TREE_TYPE (decl)))
255 /* Thrown object must be a Throwable. */
256 error ("type `%T' is not derived from `java::lang::Throwable'",
257 TREE_TYPE (decl));
262 return r;
265 /* Select the personality routine to be used for exception handling,
266 or issue an error if we need two different ones in the same
267 translation unit.
268 ??? At present eh_personality_libfunc is set to
269 __gxx_personality_(sj|v)0 in init_exception_processing - should it
270 be done here instead? */
271 void
272 choose_personality_routine (enum languages lang)
274 static enum {
275 chose_none,
276 chose_cpp,
277 chose_java,
278 gave_error
279 } state;
281 switch (state)
283 case gave_error:
284 return;
286 case chose_cpp:
287 if (lang != lang_cplusplus)
288 goto give_error;
289 return;
291 case chose_java:
292 if (lang != lang_java)
293 goto give_error;
294 return;
296 case chose_none:
297 ; /* proceed to language selection */
300 switch (lang)
302 case lang_cplusplus:
303 state = chose_cpp;
304 break;
306 case lang_java:
307 state = chose_java;
308 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
309 ? "__gcj_personality_sj0"
310 : "__gcj_personality_v0");
311 break;
313 default:
314 abort ();
316 return;
318 give_error:
319 error ("mixing C++ and Java catches in a single translation unit");
320 state = gave_error;
323 /* Initialize the catch parameter DECL. */
325 static void
326 initialize_handler_parm (tree decl, tree exp)
328 tree init;
329 tree init_type;
331 /* Make sure we mark the catch param as used, otherwise we'll get a
332 warning about an unused ((anonymous)). */
333 TREE_USED (decl) = 1;
335 /* Figure out the type that the initializer is. Pointers are returned
336 adjusted by value from __cxa_begin_catch. Others are returned by
337 reference. */
338 init_type = TREE_TYPE (decl);
339 if (! TYPE_PTR_P (init_type)
340 && TREE_CODE (init_type) != REFERENCE_TYPE)
341 init_type = build_reference_type (init_type);
343 choose_personality_routine (decl_is_java_type (init_type, 0)
344 ? lang_java : lang_cplusplus);
346 /* Since pointers are passed by value, initialize a reference to
347 pointer catch parm with the address of the temporary. */
348 if (TREE_CODE (init_type) == REFERENCE_TYPE
349 && TYPE_PTR_P (TREE_TYPE (init_type)))
350 exp = build_unary_op (ADDR_EXPR, exp, 1);
352 exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
354 init = convert_from_reference (exp);
356 /* If the constructor for the catch parm exits via an exception, we
357 must call terminate. See eh23.C. */
358 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
360 /* Generate the copy constructor call directly so we can wrap it.
361 See also expand_default_init. */
362 init = ocp_convert (TREE_TYPE (decl), init,
363 CONV_IMPLICIT|CONV_FORCE_TEMP, 0);
364 init = build1 (MUST_NOT_THROW_EXPR, TREE_TYPE (init), init);
367 /* Let `cp_finish_decl' know that this initializer is ok. */
368 DECL_INITIAL (decl) = error_mark_node;
369 decl = pushdecl (decl);
371 start_decl_1 (decl);
372 cp_finish_decl (decl, init, NULL_TREE,
373 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
376 /* Call this to start a catch block. DECL is the catch parameter. */
378 tree
379 expand_start_catch_block (tree decl)
381 tree exp = NULL_TREE;
382 tree type;
383 bool is_java;
385 if (! doing_eh (1))
386 return NULL_TREE;
388 /* Make sure this declaration is reasonable. */
389 if (decl && !complete_ptr_ref_or_void_ptr_p (TREE_TYPE (decl), NULL_TREE))
390 decl = NULL_TREE;
392 if (decl)
393 type = prepare_eh_type (TREE_TYPE (decl));
394 else
395 type = NULL_TREE;
397 is_java = false;
398 if (decl)
400 tree init;
402 if (decl_is_java_type (type, 1))
404 /* Java only passes object via pointer and doesn't require
405 adjusting. The java object is immediately before the
406 generic exception header. */
407 init = build_exc_ptr ();
408 init = build1 (NOP_EXPR, build_pointer_type (type), init);
409 init = build (MINUS_EXPR, TREE_TYPE (init), init,
410 TYPE_SIZE_UNIT (TREE_TYPE (init)));
411 init = build_indirect_ref (init, NULL);
412 is_java = true;
414 else
416 /* C++ requires that we call __cxa_begin_catch to get the
417 pointer to the actual object. */
418 init = do_begin_catch ();
421 exp = create_temporary_var (ptr_type_node);
422 DECL_REGISTER (exp) = 1;
423 cp_finish_decl (exp, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
424 finish_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
426 else
427 finish_expr_stmt (do_begin_catch ());
429 /* C++ requires that we call __cxa_end_catch at the end of
430 processing the exception. */
431 if (! is_java)
432 push_eh_cleanup (type);
434 if (decl)
435 initialize_handler_parm (decl, exp);
437 return type;
441 /* Call this to end a catch block. Its responsible for emitting the
442 code to handle jumping back to the correct place, and for emitting
443 the label to jump to if this catch block didn't match. */
445 void
446 expand_end_catch_block (void)
448 if (! doing_eh (1))
449 return;
451 /* The exception being handled is rethrown if control reaches the end of
452 a handler of the function-try-block of a constructor or destructor. */
453 if (in_function_try_handler
454 && (DECL_CONSTRUCTOR_P (current_function_decl)
455 || DECL_DESTRUCTOR_P (current_function_decl)))
456 finish_expr_stmt (build_throw (NULL_TREE));
459 tree
460 begin_eh_spec_block (void)
462 tree r = build_stmt (EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
463 add_stmt (r);
464 return r;
467 void
468 finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
470 tree raises;
472 RECHAIN_STMTS (eh_spec_block, EH_SPEC_STMTS (eh_spec_block));
474 /* Strip cv quals, etc, from the specification types. */
475 for (raises = NULL_TREE;
476 raw_raises && TREE_VALUE (raw_raises);
477 raw_raises = TREE_CHAIN (raw_raises))
479 tree type = prepare_eh_type (TREE_VALUE (raw_raises));
480 tree tinfo = eh_type_info (type);
482 mark_used (tinfo);
483 raises = tree_cons (NULL_TREE, type, raises);
486 EH_SPEC_RAISES (eh_spec_block) = raises;
489 /* Return a pointer to a buffer for an exception object of type TYPE. */
491 static tree
492 do_allocate_exception (tree type)
494 tree fn;
496 fn = get_identifier ("__cxa_allocate_exception");
497 if (!get_global_value_if_present (fn, &fn))
499 /* Declare void *__cxa_allocate_exception(size_t). */
500 tree tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
501 fn = push_library_fn (fn, build_function_type (ptr_type_node, tmp));
504 return build_function_call (fn, tree_cons (NULL_TREE, size_in_bytes (type),
505 NULL_TREE));
508 #if 0
509 /* Call __cxa_free_exception from a cleanup. This is never invoked
510 directly, but see the comment for stabilize_throw_expr. */
512 static tree
513 do_free_exception (tree ptr)
515 tree fn;
517 fn = get_identifier ("__cxa_free_exception");
518 if (!get_global_value_if_present (fn, &fn))
520 /* Declare void __cxa_free_exception (void *). */
521 fn = push_void_library_fn (fn, tree_cons (NULL_TREE, ptr_type_node,
522 void_list_node));
525 return build_function_call (fn, tree_cons (NULL_TREE, ptr, NULL_TREE));
527 #endif
529 /* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
530 Called from build_throw via walk_tree_without_duplicates. */
532 static tree
533 wrap_cleanups_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
534 void *data ATTRIBUTE_UNUSED)
536 tree exp = *tp;
537 tree cleanup;
539 /* Don't walk into types. */
540 if (TYPE_P (exp))
542 *walk_subtrees = 0;
543 return NULL_TREE;
545 if (TREE_CODE (exp) != TARGET_EXPR)
546 return NULL_TREE;
548 cleanup = TARGET_EXPR_CLEANUP (exp);
549 if (cleanup)
551 cleanup = build1 (MUST_NOT_THROW_EXPR, void_type_node, cleanup);
552 TARGET_EXPR_CLEANUP (exp) = cleanup;
555 /* Keep iterating. */
556 return NULL_TREE;
559 /* Like stabilize_expr, but specifically for a thrown expression. When
560 throwing a temporary class object, we want to construct it directly into
561 the thrown exception, so we look past the TARGET_EXPR and stabilize the
562 arguments of the call instead.
564 The case where EXP is a call to a function returning a class is a bit of
565 a grey area in the standard; it's unclear whether or not it should be
566 allowed to throw. I'm going to say no, as that allows us to optimize
567 this case without worrying about deallocating the exception object if it
568 does. The alternatives would be either not optimizing this case, or
569 wrapping the initialization in a TRY_CATCH_EXPR to call do_free_exception
570 rather than in a MUST_NOT_THROW_EXPR, for this case only. */
572 static tree
573 stabilize_throw_expr (tree exp, tree *initp)
575 tree init_expr;
577 if (TREE_CODE (exp) == TARGET_EXPR
578 && TREE_CODE (TARGET_EXPR_INITIAL (exp)) == AGGR_INIT_EXPR
579 && flag_elide_constructors)
581 tree aggr_init = AGGR_INIT_EXPR_CHECK (TARGET_EXPR_INITIAL (exp));
582 tree args = TREE_OPERAND (aggr_init, 1);
583 tree newargs = NULL_TREE;
584 tree *p = &newargs;
586 init_expr = void_zero_node;
587 for (; args; args = TREE_CHAIN (args))
589 tree arg = TREE_VALUE (args);
590 tree arg_init_expr;
592 arg = stabilize_expr (arg, &arg_init_expr);
594 if (TREE_SIDE_EFFECTS (arg_init_expr))
595 init_expr = build (COMPOUND_EXPR, void_type_node, init_expr,
596 arg_init_expr);
597 *p = tree_cons (NULL_TREE, arg, NULL_TREE);
598 p = &TREE_CHAIN (*p);
600 TREE_OPERAND (aggr_init, 1) = newargs;
602 else
604 exp = stabilize_expr (exp, &init_expr);
607 *initp = init_expr;
608 return exp;
611 /* Build a throw expression. */
613 tree
614 build_throw (tree exp)
616 tree fn;
618 if (exp == error_mark_node)
619 return exp;
621 if (processing_template_decl)
622 return build_min (THROW_EXPR, void_type_node, exp);
624 if (exp == null_node)
625 warning ("throwing NULL, which has integral, not pointer type");
627 if (exp != NULL_TREE)
629 if (!is_admissible_throw_operand (exp))
630 return error_mark_node;
633 if (! doing_eh (1))
634 return error_mark_node;
636 if (exp && decl_is_java_type (TREE_TYPE (exp), 1))
638 tree fn = get_identifier ("_Jv_Throw");
639 if (!get_global_value_if_present (fn, &fn))
641 /* Declare void _Jv_Throw (void *). */
642 tree tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
643 tmp = build_function_type (ptr_type_node, tmp);
644 fn = push_throw_library_fn (fn, tmp);
647 exp = build_function_call (fn, tree_cons (NULL_TREE, exp, NULL_TREE));
649 else if (exp)
651 tree throw_type;
652 tree cleanup;
653 tree object, ptr;
654 tree tmp;
655 tree temp_expr, allocate_expr;
657 fn = get_identifier ("__cxa_throw");
658 if (!get_global_value_if_present (fn, &fn))
660 /* The CLEANUP_TYPE is the internal type of a destructor. */
661 if (cleanup_type == NULL_TREE)
663 tmp = void_list_node;
664 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
665 tmp = build_function_type (void_type_node, tmp);
666 cleanup_type = build_pointer_type (tmp);
669 /* Declare void __cxa_throw (void*, void*, void (*)(void*)). */
670 /* ??? Second argument is supposed to be "std::type_info*". */
671 tmp = void_list_node;
672 tmp = tree_cons (NULL_TREE, cleanup_type, tmp);
673 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
674 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
675 tmp = build_function_type (void_type_node, tmp);
676 fn = push_throw_library_fn (fn, tmp);
679 /* throw expression */
680 /* First, decay it. */
681 exp = decay_conversion (exp);
683 /* OK, this is kind of wacky. The standard says that we call
684 terminate when the exception handling mechanism, after
685 completing evaluation of the expression to be thrown but
686 before the exception is caught (_except.throw_), calls a
687 user function that exits via an uncaught exception.
689 So we have to protect the actual initialization of the
690 exception object with terminate(), but evaluate the
691 expression first. Since there could be temps in the
692 expression, we need to handle that, too. We also expand
693 the call to __cxa_allocate_exception first (which doesn't
694 matter, since it can't throw). */
696 /* Pre-evaluate the thrown expression first, since if we allocated
697 the space first we would have to deal with cleaning it up if
698 evaluating this expression throws. */
699 exp = stabilize_throw_expr (exp, &temp_expr);
701 /* Allocate the space for the exception. */
702 allocate_expr = do_allocate_exception (TREE_TYPE (exp));
703 allocate_expr = get_target_expr (allocate_expr);
704 ptr = TARGET_EXPR_SLOT (allocate_expr);
705 object = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (exp)), ptr);
706 object = build_indirect_ref (object, NULL);
708 /* And initialize the exception object. */
709 exp = build_init (object, exp, LOOKUP_ONLYCONVERTING);
710 if (exp == error_mark_node)
712 error (" in thrown expression");
713 return error_mark_node;
716 exp = build1 (MUST_NOT_THROW_EXPR, void_type_node, exp);
717 /* Prepend the allocation. */
718 exp = build (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
719 if (temp_expr != void_zero_node)
721 /* Prepend the calculation of the throw expression. Also, force
722 any cleanups from the expression to be evaluated here so that
723 we don't have to do them during unwinding. But first wrap
724 them in MUST_NOT_THROW_EXPR, since they are run after the
725 exception object is initialized. */
726 walk_tree_without_duplicates (&temp_expr, wrap_cleanups_r, 0);
727 exp = build (COMPOUND_EXPR, TREE_TYPE (exp), temp_expr, exp);
728 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
731 throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
733 if (TYPE_HAS_DESTRUCTOR (TREE_TYPE (object)))
735 cleanup = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
736 complete_dtor_identifier, 0);
737 cleanup = BASELINK_FUNCTIONS (cleanup);
738 mark_used (cleanup);
739 cxx_mark_addressable (cleanup);
740 /* Pretend it's a normal function. */
741 cleanup = build1 (ADDR_EXPR, cleanup_type, cleanup);
743 else
745 cleanup = build_int_2 (0, 0);
746 TREE_TYPE (cleanup) = cleanup_type;
749 tmp = tree_cons (NULL_TREE, cleanup, NULL_TREE);
750 tmp = tree_cons (NULL_TREE, throw_type, tmp);
751 tmp = tree_cons (NULL_TREE, ptr, tmp);
752 /* ??? Indicate that this function call throws throw_type. */
753 tmp = build_function_call (fn, tmp);
755 /* Tack on the initialization stuff. */
756 exp = build (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp);
758 else
760 /* Rethrow current exception. */
762 tree fn = get_identifier ("__cxa_rethrow");
763 if (!get_global_value_if_present (fn, &fn))
765 /* Declare void __cxa_rethrow (void). */
766 fn = push_throw_library_fn
767 (fn, build_function_type (void_type_node, void_list_node));
770 /* ??? Indicate that this function call allows exceptions of the type
771 of the enclosing catch block (if known). */
772 exp = build_function_call (fn, NULL_TREE);
775 exp = build1 (THROW_EXPR, void_type_node, exp);
777 return exp;
780 /* Make sure TYPE is complete, pointer to complete, reference to
781 complete, or pointer to cv void. Issue diagnostic on failure.
782 Return the zero on failure and nonzero on success. FROM can be
783 the expr or decl from whence TYPE came, if available. */
785 static int
786 complete_ptr_ref_or_void_ptr_p (tree type, tree from)
788 int is_ptr;
790 /* Check complete. */
791 type = complete_type_or_else (type, from);
792 if (!type)
793 return 0;
795 /* Or a pointer or ref to one, or cv void *. */
796 is_ptr = TREE_CODE (type) == POINTER_TYPE;
797 if (is_ptr || TREE_CODE (type) == REFERENCE_TYPE)
799 tree core = TREE_TYPE (type);
801 if (is_ptr && VOID_TYPE_P (core))
802 /* OK */;
803 else if (!complete_type_or_else (core, from))
804 return 0;
806 return 1;
809 /* Return truth-value if EXPRESSION is admissible in throw-expression,
810 i.e. if it is not of incomplete type or a pointer/reference to such
811 a type or of an abstract class type. */
813 static bool
814 is_admissible_throw_operand (tree expr)
816 tree type = TREE_TYPE (expr);
818 /* 15.1/4 [...] The type of the throw-expression shall not be an
819 incomplete type, or a pointer or a reference to an incomplete
820 type, other than void*, const void*, volatile void*, or
821 const volatile void*. Except for these restriction and the
822 restrictions on type matching mentioned in 15.3, the operand
823 of throw is treated exactly as a function argument in a call
824 (5.2.2) or the operand of a return statement. */
825 if (!complete_ptr_ref_or_void_ptr_p (type, expr))
826 return false;
828 /* 10.4/3 An abstract class shall not be used as a parameter type,
829 as a function return type or as type of an explicit
830 conversion. */
831 else if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
833 error ("expression '%E' of abstract class type '%T' cannot be used in throw-expression", expr, type);
834 return false;
837 return true;
840 /* Returns nonzero if FN is a declaration of a standard C library
841 function which is known not to throw.
843 [lib.res.on.exception.handling]: None of the functions from the
844 Standard C library shall report an error by throwing an
845 exception, unless it calls a program-supplied function that
846 throws an exception. */
848 #include "cfns.h"
851 nothrow_libfn_p (tree fn)
853 tree id;
855 if (TREE_PUBLIC (fn)
856 && DECL_EXTERNAL (fn)
857 && DECL_NAMESPACE_SCOPE_P (fn)
858 && DECL_EXTERN_C_P (fn))
859 /* OK */;
860 else
861 /* Can't be a C library function. */
862 return 0;
864 id = DECL_ASSEMBLER_NAME (fn);
865 return !!libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
868 /* Returns nonzero if an exception of type FROM will be caught by a
869 handler for type TO, as per [except.handle]. */
871 static int
872 can_convert_eh (tree to, tree from)
874 to = non_reference (to);
875 from = non_reference (from);
877 if (TREE_CODE (to) == POINTER_TYPE && TREE_CODE (from) == POINTER_TYPE)
879 to = TREE_TYPE (to);
880 from = TREE_TYPE (from);
882 if (! at_least_as_qualified_p (to, from))
883 return 0;
885 if (TREE_CODE (to) == VOID_TYPE)
886 return 1;
888 /* else fall through */
891 if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
892 && PUBLICLY_UNIQUELY_DERIVED_P (to, from))
893 return 1;
895 return 0;
898 /* Check whether any of HANDLERS are shadowed by another handler accepting
899 TYPE. Note that the shadowing may not be complete; even if an exception
900 of type B would be caught by a handler for A, there could be a derived
901 class C for which A is an ambiguous base but B is not, so the handler
902 for B would catch an exception of type C. */
904 static void
905 check_handlers_1 (tree master, tree handlers)
907 tree type = TREE_TYPE (master);
908 tree handler;
910 for (handler = handlers; handler; handler = TREE_CHAIN (handler))
911 if (TREE_TYPE (handler)
912 && can_convert_eh (type, TREE_TYPE (handler)))
914 input_line = STMT_LINENO (handler);
915 warning ("exception of type `%T' will be caught",
916 TREE_TYPE (handler));
917 input_line = STMT_LINENO (master);
918 warning (" by earlier handler for `%T'", type);
919 break;
923 /* Given a chain of HANDLERs, make sure that they're OK. */
925 void
926 check_handlers (tree handlers)
928 tree handler;
929 int save_line = input_line;
931 for (handler = handlers; handler; handler = TREE_CHAIN (handler))
933 if (TREE_CHAIN (handler) == NULL_TREE)
934 /* No more handlers; nothing to shadow. */;
935 else if (TREE_TYPE (handler) == NULL_TREE)
937 input_line = STMT_LINENO (handler);
938 pedwarn
939 ("`...' handler must be the last handler for its try block");
941 else
942 check_handlers_1 (handler, TREE_CHAIN (handler));
944 input_line = save_line;