1 /* Handle exceptional things in C++.
2 Copyright (C) 1989-2024 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann <tiemann@cygnus.com>
4 Rewritten by Mike Stump <mrs@cygnus.com>, based upon an
5 initial re-implementation courtesy Tad Hunt.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
28 #include "stringpool.h"
29 #include "trans-mem.h"
31 #include "tree-iterator.h"
34 static void push_eh_cleanup (tree
);
35 static tree
prepare_eh_type (tree
);
36 static tree
do_begin_catch (void);
37 static int dtor_nothrow (tree
);
38 static tree
do_end_catch (tree
);
39 static void initialize_handler_parm (tree
, tree
);
40 static tree
do_allocate_exception (tree
);
41 static tree
wrap_cleanups_r (tree
*, int *, void *);
42 static bool is_admissible_throw_operand_or_catch_parameter (tree
, bool,
45 /* Sets up all the global eh stuff that needs to be initialized at the
46 start of compilation. */
49 init_exception_processing (void)
53 /* void std::terminate (); */
54 push_nested_namespace (std_node
);
55 tmp
= build_function_type_list (void_type_node
, NULL_TREE
);
56 terminate_fn
= build_cp_library_fn_ptr ("terminate", tmp
,
57 ECF_NOTHROW
| ECF_NORETURN
59 gcc_checking_assert (TREE_THIS_VOLATILE (terminate_fn
)
60 && TREE_NOTHROW (terminate_fn
));
61 pop_nested_namespace (std_node
);
63 /* void __cxa_call_unexpected(void *); */
64 tmp
= build_function_type_list (void_type_node
, ptr_type_node
, NULL_TREE
);
66 = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp
);
68 = push_library_fn (get_identifier ("__cxa_call_terminate"), tmp
, NULL_TREE
,
69 ECF_NORETURN
| ECF_COLD
| ECF_NOTHROW
);
72 /* Returns an expression to be executed if an unhandled exception is
73 propagated out of a cleanup region. */
76 cp_protect_cleanup_actions (void)
80 When the destruction of an object during stack unwinding exits
81 using an exception ... void terminate(); is called. */
82 return call_terminate_fn
;
86 prepare_eh_type (tree type
)
88 if (type
== NULL_TREE
)
90 if (type
== error_mark_node
)
91 return error_mark_node
;
93 /* peel back references, so they match. */
94 type
= non_reference (type
);
96 /* Peel off cv qualifiers. */
97 type
= TYPE_MAIN_VARIANT (type
);
99 /* Functions and arrays decay to pointers. */
100 type
= type_decays_to (type
);
105 /* Return the type info for TYPE as used by EH machinery. */
107 eh_type_info (tree type
)
109 if (type
== NULL_TREE
|| type
== error_mark_node
)
112 return get_tinfo_decl (type
);
115 /* Build the address of a typeinfo decl for use in the runtime
116 matching field of the exception model. */
119 build_eh_type_type (tree type
)
121 tree exp
= eh_type_info (type
);
128 return convert (ptr_type_node
, build_address (exp
));
134 return build_call_n (builtin_decl_explicit (BUILT_IN_EH_POINTER
),
135 1, integer_zero_node
);
138 /* Declare an exception ABI entry point called NAME.
139 ECF are the library flags, RTYPE the return type and ARGS[NARGS]
140 the parameter types. We return the DECL -- which might be one
141 found via the symbol table pushing, if the user already declared
142 it. If we pushed a new decl, the user will see it. */
145 declare_library_fn_1 (const char *name
, int ecf
,
146 tree rtype
, int nargs
, tree args
[])
148 tree ident
= get_identifier (name
);
149 tree except
= ecf
& ECF_NOTHROW
? empty_except_spec
: NULL_TREE
;
151 /* Make a new decl. */
152 tree arg_list
= void_list_node
;
153 for (unsigned ix
= nargs
; ix
--;)
154 arg_list
= tree_cons (NULL_TREE
, args
[ix
], arg_list
);
155 tree fntype
= build_function_type (rtype
, arg_list
);
156 tree res
= push_library_fn (ident
, fntype
, except
, ecf
);
161 /* Find or declare a function NAME, returning RTYPE, taking a single
162 parameter PTYPE, with an empty exception specification. ECF are the
163 library fn flags. If TM_ECF is non-zero, also find or create a
164 transaction variant and record it as a replacement, when flag_tm is
167 Note that the C++ ABI document does not have a throw-specifier on
168 the routines declared below via this function. The declarations
169 are consistent with the actual implementations in libsupc++. */
172 declare_library_fn (const char *name
, tree rtype
, tree ptype
,
175 tree res
= declare_library_fn_1 (name
, ecf
, rtype
, ptype
? 1 : 0, &ptype
);
176 if (res
== error_mark_node
)
179 if (tm_ecf
&& flag_tm
)
181 char *tm_name
= concat ("_ITM_", name
+ 2, NULL_TREE
);
183 tree tm_fn
= declare_library_fn_1 (tm_name
, ecf
| tm_ecf
, rtype
,
184 ptype
? 1 : 0, &ptype
);
186 if (tm_fn
!= error_mark_node
)
187 record_tm_replacement (res
, tm_fn
);
193 /* Build up a call to __cxa_get_exception_ptr so that we can build a
194 copy constructor for the thrown object. */
197 do_get_exception_ptr (void)
199 if (!get_exception_ptr_fn
)
200 /* Declare void* __cxa_get_exception_ptr (void *) throw(). */
202 = declare_library_fn ("__cxa_get_exception_ptr",
203 ptr_type_node
, ptr_type_node
,
204 ECF_NOTHROW
| ECF_PURE
| ECF_LEAF
| ECF_TM_PURE
,
207 return cp_build_function_call_nary (get_exception_ptr_fn
,
209 build_exc_ptr (), NULL_TREE
);
212 /* Build up a call to __cxa_begin_catch, to tell the runtime that the
213 exception has been handled. */
216 do_begin_catch (void)
219 /* Declare void* __cxa_begin_catch (void *) throw(). */
221 = declare_library_fn ("__cxa_begin_catch",
222 ptr_type_node
, ptr_type_node
, ECF_NOTHROW
,
225 return cp_build_function_call_nary (begin_catch_fn
, tf_warning_or_error
,
226 build_exc_ptr (), NULL_TREE
);
229 /* Returns nonzero if cleaning up an exception of type TYPE (which can be
230 NULL_TREE for a ... handler) will not throw an exception. */
233 dtor_nothrow (tree type
)
235 if (type
== NULL_TREE
|| type
== error_mark_node
)
238 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
241 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
242 lazily_declare_fn (sfk_destructor
, type
);
244 return TREE_NOTHROW (CLASSTYPE_DESTRUCTOR (type
));
247 /* Build up a call to __cxa_end_catch, to destroy the exception object
248 for the current catch block if no others are currently using it. */
251 do_end_catch (tree type
)
254 /* Declare void __cxa_end_catch ().
255 This can throw if the destructor for the exception throws. */
257 = declare_library_fn ("__cxa_end_catch", void_type_node
,
258 NULL_TREE
, 0, ECF_TM_PURE
);
260 tree cleanup
= cp_build_function_call_vec (end_catch_fn
,
261 NULL
, tf_warning_or_error
);
262 if (cleanup
!= error_mark_node
)
263 TREE_NOTHROW (cleanup
) = dtor_nothrow (type
);
268 /* This routine creates the cleanup for the current exception. */
271 push_eh_cleanup (tree type
)
273 finish_decl_cleanup (NULL_TREE
, do_end_catch (type
));
276 /* Wrap EXPR in a MUST_NOT_THROW_EXPR expressing that EXPR must
277 not throw any exceptions if COND is true. A condition of
278 NULL_TREE is treated as 'true'. */
281 build_must_not_throw_expr (tree body
, tree cond
)
283 tree type
= body
? TREE_TYPE (body
) : void_type_node
;
285 if (!flag_exceptions
)
289 /* OK, unconditional. */;
292 tree conv
= NULL_TREE
;
293 if (!type_dependent_expression_p (cond
))
294 conv
= perform_implicit_conversion_flags (boolean_type_node
, cond
,
297 if (tree inst
= instantiate_non_dependent_or_null (conv
))
298 cond
= cxx_constant_value (inst
);
300 require_constant_expression (cond
);
301 if (integer_zerop (cond
))
303 else if (integer_onep (cond
))
307 return build2 (MUST_NOT_THROW_EXPR
, type
, body
, cond
);
311 /* Initialize the catch parameter DECL. */
314 initialize_handler_parm (tree decl
, tree exp
)
319 /* Make sure we mark the catch param as used, otherwise we'll get a
320 warning about an unused ((anonymous)). */
321 TREE_USED (decl
) = 1;
322 DECL_READ_P (decl
) = 1;
324 /* Figure out the type that the initializer is. Pointers are returned
325 adjusted by value from __cxa_begin_catch. Others are returned by
327 init_type
= TREE_TYPE (decl
);
328 if (!INDIRECT_TYPE_P (init_type
))
329 init_type
= build_reference_type (init_type
);
331 /* Since pointers are passed by value, initialize a reference to
332 pointer catch parm with the address of the temporary. */
333 if (TYPE_REF_P (init_type
)
334 && TYPE_PTR_P (TREE_TYPE (init_type
)))
335 exp
= cp_build_addr_expr (exp
, tf_warning_or_error
);
337 exp
= ocp_convert (init_type
, exp
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, 0,
338 tf_warning_or_error
);
340 init
= convert_from_reference (exp
);
342 /* If the constructor for the catch parm exits via an exception, we
343 must call terminate. See eh23.C. */
344 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
)))
346 /* Generate the copy constructor call directly so we can wrap it.
347 See also expand_default_init. */
348 init
= ocp_convert (TREE_TYPE (decl
), init
,
349 CONV_IMPLICIT
|CONV_FORCE_TEMP
, 0,
350 tf_warning_or_error
);
351 /* Force cleanups now to avoid nesting problems with the
352 MUST_NOT_THROW_EXPR. */
353 init
= fold_build_cleanup_point_expr (TREE_TYPE (init
), init
);
354 init
= build_must_not_throw_expr (init
, NULL_TREE
);
357 decl
= pushdecl (decl
);
359 start_decl_1 (decl
, true);
360 cp_finish_decl (decl
, init
, /*init_const_expr_p=*/false, NULL_TREE
,
361 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
365 /* Routine to see if exception handling is turned on.
366 DO_WARN is nonzero if we want to inform the user that exception
367 handling is turned off.
369 This is used to ensure that -fexceptions has been specified if the
370 compiler tries to use any exception-specific functions. */
375 if (! flag_exceptions
)
377 static int warned
= 0;
380 error ("exception handling disabled, use %<-fexceptions%> to enable");
388 /* Call this to start a catch block. DECL is the catch parameter. */
391 expand_start_catch_block (tree decl
)
401 if (!is_admissible_throw_operand_or_catch_parameter (decl
, false,
402 tf_warning_or_error
))
403 decl
= error_mark_node
;
405 type
= prepare_eh_type (TREE_TYPE (decl
));
406 mark_used (eh_type_info (type
));
411 /* Call __cxa_end_catch at the end of processing the exception. */
412 push_eh_cleanup (type
);
414 init
= do_begin_catch ();
416 /* If there's no decl at all, then all we need to do is make sure
417 to tell the runtime that we've begun handling the exception. */
418 if (decl
== NULL
|| decl
== error_mark_node
|| init
== error_mark_node
)
419 finish_expr_stmt (init
);
421 /* If the C++ object needs constructing, we need to do that before
422 calling __cxa_begin_catch, so that std::uncaught_exception gets
423 the right value during the copy constructor. */
424 else if (flag_use_cxa_get_exception_ptr
425 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
)))
427 exp
= do_get_exception_ptr ();
428 if (exp
!= error_mark_node
)
429 initialize_handler_parm (decl
, exp
);
430 finish_expr_stmt (init
);
433 /* Otherwise the type uses a bitwise copy, and we don't have to worry
434 about the value of std::uncaught_exception and therefore can do the
435 copy with the return value of __cxa_end_catch instead. */
438 tree init_type
= type
;
440 /* Pointers are passed by values, everything else by reference. */
441 if (!TYPE_PTR_P (type
))
442 init_type
= build_pointer_type (type
);
443 if (init_type
!= TREE_TYPE (init
))
444 init
= build1 (NOP_EXPR
, init_type
, init
);
445 exp
= create_temporary_var (init_type
);
446 cp_finish_decl (exp
, init
, /*init_const_expr=*/false,
447 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
448 DECL_REGISTER (exp
) = 1;
449 initialize_handler_parm (decl
, exp
);
455 /* True if we are in a catch block within a catch block. Assumes that we are
456 in function scope. */
459 in_nested_catch (void)
463 /* Scan through the template parameter scopes. */
464 for (cp_binding_level
*b
= current_binding_level
;
465 b
->kind
!= sk_function_parms
;
467 if (b
->kind
== sk_catch
473 /* Call this to end a catch block. Its responsible for emitting the
474 code to handle jumping back to the correct place, and for emitting
475 the label to jump to if this catch block didn't match. */
478 expand_end_catch_block (void)
483 /* The exception being handled is rethrown if control reaches the end of
484 a handler of the function-try-block of a constructor or destructor. */
485 if (in_function_try_handler
486 && (DECL_CONSTRUCTOR_P (current_function_decl
)
487 || DECL_DESTRUCTOR_P (current_function_decl
))
488 && !in_nested_catch ())
490 tree rethrow
= build_throw (input_location
, NULL_TREE
,
491 tf_warning_or_error
);
492 /* Disable all warnings for the generated rethrow statement. */
493 suppress_warning (rethrow
);
494 finish_expr_stmt (rethrow
);
499 begin_eh_spec_block (void)
502 location_t spec_location
= DECL_SOURCE_LOCATION (current_function_decl
);
504 /* A noexcept specification (or throw() with -fnothrow-opt) is a
505 MUST_NOT_THROW_EXPR. */
506 if (TYPE_NOEXCEPT_P (TREE_TYPE (current_function_decl
)))
508 r
= build_stmt (spec_location
, MUST_NOT_THROW_EXPR
,
509 NULL_TREE
, NULL_TREE
);
510 TREE_SIDE_EFFECTS (r
) = 1;
513 r
= build_stmt (spec_location
, EH_SPEC_BLOCK
, NULL_TREE
, NULL_TREE
);
515 TREE_OPERAND (r
, 0) = push_stmt_list ();
520 finish_eh_spec_block (tree raw_raises
, tree eh_spec_block
)
524 TREE_OPERAND (eh_spec_block
, 0)
525 = pop_stmt_list (TREE_OPERAND (eh_spec_block
, 0));
527 if (TREE_CODE (eh_spec_block
) == MUST_NOT_THROW_EXPR
)
530 /* Strip cv quals, etc, from the specification types. */
531 for (raises
= NULL_TREE
;
532 raw_raises
&& TREE_VALUE (raw_raises
);
533 raw_raises
= TREE_CHAIN (raw_raises
))
535 tree type
= prepare_eh_type (TREE_VALUE (raw_raises
));
536 tree tinfo
= eh_type_info (type
);
539 raises
= tree_cons (NULL_TREE
, type
, raises
);
542 EH_SPEC_RAISES (eh_spec_block
) = raises
;
545 /* Return a pointer to a buffer for an exception object of type TYPE. */
548 do_allocate_exception (tree type
)
550 if (!allocate_exception_fn
)
551 /* Declare void *__cxa_allocate_exception(size_t) throw(). */
552 allocate_exception_fn
553 = declare_library_fn ("__cxa_allocate_exception",
554 ptr_type_node
, size_type_node
,
555 ECF_NOTHROW
| ECF_MALLOC
| ECF_COLD
, ECF_TM_PURE
);
557 return cp_build_function_call_nary (allocate_exception_fn
,
559 size_in_bytes (type
), NULL_TREE
);
562 /* Call __cxa_free_exception from a cleanup. This is never invoked
563 directly, but see the comment for stabilize_throw_expr. */
566 do_free_exception (tree ptr
)
568 if (!free_exception_fn
)
569 /* Declare void __cxa_free_exception (void *) throw(). */
571 = declare_library_fn ("__cxa_free_exception",
572 void_type_node
, ptr_type_node
,
573 ECF_NOTHROW
| ECF_LEAF
, ECF_TM_PURE
);
575 return cp_build_function_call_nary (free_exception_fn
,
576 tf_warning_or_error
, ptr
, NULL_TREE
);
579 /* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
580 Called from build_throw via walk_tree_without_duplicates. */
583 wrap_cleanups_r (tree
*tp
, int *walk_subtrees
, void * /*data*/)
588 /* Don't walk into types. */
594 if (TREE_CODE (exp
) != TARGET_EXPR
)
597 cleanup
= TARGET_EXPR_CLEANUP (exp
);
600 cleanup
= build2 (MUST_NOT_THROW_EXPR
, void_type_node
, cleanup
,
602 TARGET_EXPR_CLEANUP (exp
) = cleanup
;
605 /* Keep iterating. */
609 /* Build a throw expression. */
612 build_throw (location_t loc
, tree exp
, tsubst_flags_t complain
)
614 if (exp
== error_mark_node
)
617 if (processing_template_decl
)
620 current_function_returns_abnormally
= 1;
621 exp
= build_min (THROW_EXPR
, void_type_node
, exp
);
622 SET_EXPR_LOCATION (exp
, loc
);
626 if (exp
&& null_node_p (exp
) && (complain
& tf_warning
))
628 "throwing NULL, which has integral, not pointer type");
630 if (exp
&& !is_admissible_throw_operand_or_catch_parameter (exp
,
633 return error_mark_node
;
636 return error_mark_node
;
646 /* The CLEANUP_TYPE is the internal type of a destructor. */
648 cleanup_type
= get_cxa_atexit_fn_ptr_type ();
652 tree args
[3] = {ptr_type_node
, ptr_type_node
, cleanup_type
};
654 throw_fn
= declare_library_fn_1 ("__cxa_throw",
655 ECF_NORETURN
| ECF_XTHROW
| ECF_COLD
,
656 void_type_node
, 3, args
);
657 if (flag_tm
&& throw_fn
!= error_mark_node
)
659 tree itm_fn
= declare_library_fn_1 ("_ITM_cxa_throw",
660 ECF_NORETURN
| ECF_XTHROW
662 void_type_node
, 3, args
);
663 if (itm_fn
!= error_mark_node
)
665 apply_tm_attr (itm_fn
, get_identifier ("transaction_pure"));
666 record_tm_replacement (throw_fn
, itm_fn
);
673 A throw-expression initializes a temporary object, the type
674 of which is determined by removing any top-level
675 cv-qualifiers from the static type of the operand of throw
676 and adjusting the type from "array of T" or "function return
677 T" to "pointer to T" or "pointer to function returning T"
679 temp_type
= is_bitfield_expr_with_lowered_type (exp
);
681 temp_type
= cv_unqualified (type_decays_to (TREE_TYPE (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 /* Allocate the space for the exception. */
697 allocate_expr
= do_allocate_exception (temp_type
);
698 if (allocate_expr
== error_mark_node
)
699 return error_mark_node
;
700 allocate_expr
= get_target_expr (allocate_expr
);
701 ptr
= TARGET_EXPR_SLOT (allocate_expr
);
702 TARGET_EXPR_CLEANUP (allocate_expr
) = do_free_exception (ptr
);
703 CLEANUP_EH_ONLY (allocate_expr
) = 1;
705 object
= build_nop (build_pointer_type (temp_type
), ptr
);
706 object
= cp_build_fold_indirect_ref (object
);
708 /* And initialize the exception object. */
709 if (CLASS_TYPE_P (temp_type
))
711 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
712 location_t exp_loc
= cp_expr_loc_or_loc (exp
, loc
);
714 /* Under C++0x [12.8/16 class.copy], a thrown lvalue is sometimes
715 treated as an rvalue for the purposes of overload resolution
716 to favor move constructors over copy constructors. */
717 if (tree moved
= treat_lvalue_as_rvalue_p (exp
, /*return*/false))
718 /* In C++20 we treat the return value as an rvalue that
719 can bind to lvalue refs. In C++23, such an expression is just
723 /* Call the copy constructor. */
724 releasing_vec
exp_vec (make_tree_vector_single (exp
));
725 exp
= build_special_member_call (object
, complete_ctor_identifier
,
726 &exp_vec
, TREE_TYPE (object
), flags
,
728 if (exp
== error_mark_node
)
730 if (complain
& tf_error
)
731 inform (exp_loc
, " in thrown expression");
732 return error_mark_node
;
737 tree tmp
= decay_conversion (exp
, complain
);
738 if (tmp
== error_mark_node
)
739 return error_mark_node
;
740 exp
= cp_build_init_expr (object
, tmp
);
743 /* Mark any cleanups from the initialization as MUST_NOT_THROW, since
744 they are run after the exception object is initialized. */
745 cp_walk_tree_without_duplicates (&exp
, wrap_cleanups_r
, 0);
747 /* Prepend the allocation. */
748 exp
= build2 (COMPOUND_EXPR
, TREE_TYPE (exp
), allocate_expr
, exp
);
750 /* Force all the cleanups to be evaluated here so that we don't have
751 to do them during unwinding. */
752 exp
= build1 (CLEANUP_POINT_EXPR
, void_type_node
, exp
);
754 throw_type
= build_eh_type_type (prepare_eh_type (TREE_TYPE (object
)));
757 if (type_build_dtor_call (TREE_TYPE (object
)))
759 tree binfo
= TYPE_BINFO (TREE_TYPE (object
));
760 tree dtor_fn
= lookup_fnfields (binfo
,
761 complete_dtor_identifier
, 0,
763 dtor_fn
= BASELINK_FUNCTIONS (dtor_fn
);
764 if (!mark_used (dtor_fn
)
765 || !perform_or_defer_access_check (binfo
, dtor_fn
,
767 return error_mark_node
;
768 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object
)))
770 cxx_mark_addressable (dtor_fn
);
771 /* Pretend it's a normal function. */
772 cleanup
= build1 (ADDR_EXPR
, cleanup_type
, dtor_fn
);
775 if (cleanup
== NULL_TREE
)
776 cleanup
= build_int_cst (cleanup_type
, 0);
778 /* ??? Indicate that this function call throws throw_type. */
779 tree tmp
= cp_build_function_call_nary (throw_fn
, complain
,
780 ptr
, throw_type
, cleanup
,
783 /* Tack on the initialization stuff. */
784 exp
= build2 (COMPOUND_EXPR
, TREE_TYPE (tmp
), exp
, tmp
);
788 /* Rethrow current exception. */
791 rethrow_fn
= declare_library_fn_1 ("__cxa_rethrow",
792 ECF_NORETURN
| ECF_XTHROW
794 void_type_node
, 0, NULL
);
795 if (flag_tm
&& rethrow_fn
!= error_mark_node
)
796 apply_tm_attr (rethrow_fn
, get_identifier ("transaction_pure"));
799 /* ??? Indicate that this function call allows exceptions of the type
800 of the enclosing catch block (if known). */
801 exp
= cp_build_function_call_vec (rethrow_fn
, NULL
, complain
);
804 exp
= build1_loc (loc
, THROW_EXPR
, void_type_node
, exp
);
809 /* Make sure TYPE is complete, pointer to complete, reference to
810 complete, or pointer to cv void. Issue diagnostic on failure.
811 Return the zero on failure and nonzero on success. FROM can be
812 the expr or decl from whence TYPE came, if available. */
815 complete_ptr_ref_or_void_ptr_p (tree type
, tree from
, tsubst_flags_t complain
)
817 /* Check complete. */
818 type
= complete_type_or_maybe_complain (type
, from
, complain
);
822 /* Or a pointer or ref to one, or cv void *. */
823 const bool is_ptr
= TYPE_PTR_P (type
);
824 if (is_ptr
|| TYPE_REF_P (type
))
826 tree core
= TREE_TYPE (type
);
828 if (is_ptr
&& VOID_TYPE_P (core
))
830 else if (!complete_type_or_maybe_complain (core
, from
, complain
))
836 /* If IS_THROW is true return truth-value if T is an expression admissible
837 in throw-expression, i.e. if it is not of incomplete type or a pointer/
838 reference to such a type or of an abstract class type.
839 If IS_THROW is false, likewise for a catch parameter, same requirements
840 for its type plus rvalue reference type is also not admissible. */
843 is_admissible_throw_operand_or_catch_parameter (tree t
, bool is_throw
,
844 tsubst_flags_t complain
)
846 tree expr
= is_throw
? t
: NULL_TREE
;
847 tree type
= TREE_TYPE (t
);
849 /* C++11 [except.handle] The exception-declaration shall not denote
850 an incomplete type, an abstract class type, or an rvalue reference
853 /* 15.1/4 [...] The type of the throw-expression shall not be an
854 incomplete type, or a pointer or a reference to an incomplete
855 type, other than void*, const void*, volatile void*, or
856 const volatile void*. Except for these restriction and the
857 restrictions on type matching mentioned in 15.3, the operand
858 of throw is treated exactly as a function argument in a call
859 (5.2.2) or the operand of a return statement. */
860 if (!complete_ptr_ref_or_void_ptr_p (type
, expr
, complain
))
863 tree nonref_type
= non_reference (type
);
864 if (!verify_type_context (input_location
, TCTX_EXCEPTIONS
, nonref_type
))
867 /* 10.4/3 An abstract class shall not be used as a parameter type,
868 as a function return type or as type of an explicit
870 else if (abstract_virtuals_error (is_throw
? ACU_THROW
: ACU_CATCH
, type
,
875 && TYPE_REF_IS_RVALUE (type
))
877 if (complain
& tf_error
)
878 error ("cannot declare %<catch%> parameter to be of rvalue "
879 "reference type %qT", type
);
882 else if (variably_modified_type_p (type
, NULL_TREE
))
884 if (complain
& tf_error
)
887 error_at (cp_expr_loc_or_input_loc (expr
),
888 "cannot throw expression of type %qT because it involves "
889 "types of variable size", type
);
891 error ("cannot catch type %qT because it involves types of "
892 "variable size", type
);
900 /* Returns nonzero if FN is a declaration of a standard C library
901 function which is known not to throw.
903 [lib.res.on.exception.handling]: None of the functions from the
904 Standard C library shall report an error by throwing an
905 exception, unless it calls a program-supplied function that
906 throws an exception. */
911 nothrow_libfn_p (const_tree fn
)
916 && DECL_EXTERNAL (fn
)
917 && DECL_NAMESPACE_SCOPE_P (fn
)
918 && DECL_EXTERN_C_P (fn
))
921 /* Can't be a C library function. */
924 /* Being a C library function, DECL_ASSEMBLER_NAME == DECL_NAME
925 unless the system headers are playing rename tricks, and if
926 they are, we don't want to be confused by them. */
928 const struct libc_name_struct
*s
929 = libc_name::libc_name_p (IDENTIFIER_POINTER (id
), IDENTIFIER_LENGTH (id
));
935 case 99: return !flag_iso
|| flag_isoc99
;
936 case 11: return !flag_iso
|| flag_isoc11
;
937 default: gcc_unreachable ();
941 /* Returns nonzero if an exception of type FROM will be caught by a
942 handler for type TO, as per [except.handle]. */
945 can_convert_eh (tree to
, tree from
)
947 to
= non_reference (to
);
948 from
= non_reference (from
);
950 if (same_type_ignoring_top_level_qualifiers_p (to
, from
))
953 if (TYPE_PTR_P (to
) && TYPE_PTR_P (from
))
956 from
= TREE_TYPE (from
);
958 if (! at_least_as_qualified_p (to
, from
))
961 if (VOID_TYPE_P (to
))
964 /* Else fall through. */
967 if (CLASS_TYPE_P (to
) && CLASS_TYPE_P (from
)
968 && publicly_uniquely_derived_p (to
, from
))
974 /* Check whether any of the handlers in I are shadowed by another handler
975 accepting TYPE. Note that the shadowing may not be complete; even if
976 an exception of type B would be caught by a handler for A, there could
977 be a derived class C for which A is an ambiguous base but B is not, so
978 the handler for B would catch an exception of type C. */
981 check_handlers_1 (tree master
, tree_stmt_iterator i
)
983 tree type
= TREE_TYPE (master
);
985 for (; !tsi_end_p (i
); tsi_next (&i
))
987 tree handler
= tsi_stmt (i
);
988 if (TREE_TYPE (handler
) && can_convert_eh (type
, TREE_TYPE (handler
)))
990 auto_diagnostic_group d
;
991 if (warning_at (EXPR_LOCATION (handler
), OPT_Wexceptions
,
992 "exception of type %qT will be caught by earlier "
993 "handler", TREE_TYPE (handler
)))
994 inform (EXPR_LOCATION (master
), "for type %qT", type
);
1000 /* Given a STATEMENT_LIST of HANDLERs, make sure that they're OK. */
1003 check_handlers (tree handlers
)
1005 tree_stmt_iterator i
;
1007 /* If we don't have a STATEMENT_LIST, then we've just got one
1008 handler, and thus nothing to warn about. */
1009 if (TREE_CODE (handlers
) != STATEMENT_LIST
)
1012 i
= tsi_start (handlers
);
1016 tree handler
= tsi_stmt (i
);
1019 /* No more handlers; nothing to shadow. */
1022 if (TREE_TYPE (handler
) == NULL_TREE
)
1023 permerror (EXPR_LOCATION (handler
), "%<...%>"
1024 " handler must be the last handler for its try block");
1026 check_handlers_1 (handler
, i
);
1030 /* walk_tree helper for finish_noexcept_expr. Returns non-null if the
1031 expression *TP causes the noexcept operator to evaluate to false.
1033 5.3.7 [expr.noexcept]: The result of the noexcept operator is false if
1034 in a potentially-evaluated context the expression would contain
1035 * a potentially evaluated call to a function, member function,
1036 function pointer, or member function pointer that does not have a
1037 non-throwing exception-specification (15.4),
1038 * a potentially evaluated throw-expression (15.1),
1039 * a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1040 where T is a reference type, that requires a run-time check (5.2.7), or
1041 * a potentially evaluated typeid expression (5.2.8) applied to a glvalue
1042 expression whose type is a polymorphic class type (10.3). */
1045 check_noexcept_r (tree
*tp
, int *walk_subtrees
, void *)
1048 enum tree_code code
= TREE_CODE (t
);
1050 if (unevaluated_p (code
))
1051 *walk_subtrees
= false;
1052 else if ((code
== CALL_EXPR
&& CALL_EXPR_FN (t
))
1053 || code
== AGGR_INIT_EXPR
)
1055 /* We can only use the exception specification of the called function
1056 for determining the value of a noexcept expression; we can't use
1057 TREE_NOTHROW, as it might have a different value in another
1058 translation unit, creating ODR problems.
1060 We could use TREE_NOTHROW (t) for !TREE_PUBLIC fns, though... */
1061 tree fn
= cp_get_callee (t
);
1062 if (concept_check_p (fn
))
1064 tree type
= TREE_TYPE (fn
);
1065 gcc_assert (INDIRECT_TYPE_P (type
));
1066 type
= TREE_TYPE (type
);
1069 if (TREE_CODE (fn
) == ADDR_EXPR
)
1070 fn
= TREE_OPERAND (fn
, 0);
1071 if (TREE_CODE (fn
) == FUNCTION_DECL
)
1073 /* We do use TREE_NOTHROW for ABI internals like __dynamic_cast,
1074 and for C library functions known not to throw. */
1075 if (DECL_EXTERN_C_P (fn
)
1076 && (DECL_ARTIFICIAL (fn
)
1077 || nothrow_libfn_p (fn
)))
1078 return TREE_NOTHROW (fn
) ? NULL_TREE
: fn
;
1079 /* We used to treat a call to a constexpr function as noexcept if
1080 the call was a constant expression (CWG 1129). This has changed
1081 in P0003 whereby noexcept has no special rule for constant
1082 expressions anymore. Since the current behavior is important for
1083 certain library functionality, we treat this as a DR, therefore
1084 adjusting the behavior for C++11 and C++14. Previously, we had
1085 to evaluate the noexcept-specifier's operand here, but that could
1086 cause instantiations that would fail. */
1088 if (!TYPE_NOTHROW_P (type
))
1095 /* If a function that causes a noexcept-expression to be false isn't
1096 defined yet, remember it and check it for TREE_NOTHROW again at EOF. */
1098 struct GTY(()) pending_noexcept
{
1102 static GTY(()) vec
<pending_noexcept
, va_gc
> *pending_noexcept_checks
;
1104 /* FN is a FUNCTION_DECL that caused a noexcept-expr to be false. Warn if
1107 TODO: Consider extending -Wnoexcept to do something like walk_subtrees in the
1108 case of a defaulted function that obtained a noexcept(false) spec. */
1111 maybe_noexcept_warning (tree fn
)
1113 if (TREE_NOTHROW (fn
)
1114 && (!DECL_IN_SYSTEM_HEADER (fn
)
1115 || global_dc
->m_warn_system_headers
))
1117 auto s
= make_temp_override (global_dc
->m_warn_system_headers
, true);
1118 auto_diagnostic_group d
;
1119 if (warning (OPT_Wnoexcept
, "noexcept-expression evaluates to %<false%> "
1120 "because of a call to %qD", fn
))
1121 inform (DECL_SOURCE_LOCATION (fn
),
1122 "but %qD does not throw; perhaps "
1123 "it should be declared %<noexcept%>", fn
);
1127 /* Check any functions that weren't defined earlier when they caused a
1128 noexcept expression to evaluate to false. */
1131 perform_deferred_noexcept_checks (void)
1134 pending_noexcept
*p
;
1135 location_t saved_loc
= input_location
;
1136 FOR_EACH_VEC_SAFE_ELT (pending_noexcept_checks
, i
, p
)
1138 input_location
= p
->loc
;
1139 maybe_noexcept_warning (p
->fn
);
1141 input_location
= saved_loc
;
1144 /* Evaluate noexcept ( EXPR ). */
1147 finish_noexcept_expr (tree expr
, tsubst_flags_t complain
)
1149 if (expr
== error_mark_node
)
1150 return error_mark_node
;
1152 if (processing_template_decl
)
1153 return build_min (NOEXCEPT_EXPR
, boolean_type_node
, expr
);
1155 return (expr_noexcept_p (expr
, complain
)
1156 ? boolean_true_node
: boolean_false_node
);
1159 /* Returns whether EXPR is noexcept, possibly warning if allowed by
1163 expr_noexcept_p (tree expr
, tsubst_flags_t complain
)
1167 if (expr
== error_mark_node
)
1170 fn
= cp_walk_tree_without_duplicates (&expr
, check_noexcept_r
, 0);
1173 if ((complain
& tf_warning
) && warn_noexcept
1174 && TREE_CODE (fn
) == FUNCTION_DECL
)
1176 if (!DECL_INITIAL (fn
))
1178 /* Not defined yet; check again at EOF. */
1179 pending_noexcept p
= {fn
, input_location
};
1180 vec_safe_push (pending_noexcept_checks
, p
);
1183 maybe_noexcept_warning (fn
);
1191 /* Return true iff SPEC is throw() or noexcept(true). */
1194 nothrow_spec_p (const_tree spec
)
1196 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec
));
1198 if (spec
== empty_except_spec
1199 || spec
== noexcept_true_spec
)
1203 || TREE_VALUE (spec
)
1204 || spec
== noexcept_false_spec
1205 || TREE_PURPOSE (spec
) == error_mark_node
1206 || UNPARSED_NOEXCEPT_SPEC_P (spec
)
1207 || processing_template_decl
);
1212 /* For FUNCTION_TYPE or METHOD_TYPE, true if NODE is noexcept. This is the
1213 case for things declared noexcept(true) and, with -fnothrow-opt, for
1214 throw() functions. */
1217 type_noexcept_p (const_tree type
)
1219 tree spec
= TYPE_RAISES_EXCEPTIONS (type
);
1220 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec
));
1221 if (flag_nothrow_opt
)
1222 return nothrow_spec_p (spec
);
1224 return spec
== noexcept_true_spec
;
1227 /* For FUNCTION_TYPE or METHOD_TYPE, true if NODE can throw any type,
1228 i.e. no exception-specification or noexcept(false). */
1231 type_throw_all_p (const_tree type
)
1233 tree spec
= TYPE_RAISES_EXCEPTIONS (type
);
1234 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec
));
1235 return spec
== NULL_TREE
|| spec
== noexcept_false_spec
;
1238 /* Create a representation of the noexcept-specification with
1239 constant-expression of EXPR. COMPLAIN is as for tsubst. */
1242 build_noexcept_spec (tree expr
, tsubst_flags_t complain
)
1244 if (check_for_bare_parameter_packs (expr
))
1245 return error_mark_node
;
1246 if (TREE_CODE (expr
) != DEFERRED_NOEXCEPT
1247 && !instantiation_dependent_expression_p (expr
))
1249 expr
= build_converted_constant_bool_expr (expr
, complain
);
1250 expr
= instantiate_non_dependent_expr (expr
, complain
);
1251 expr
= cxx_constant_value (expr
, complain
);
1253 if (TREE_CODE (expr
) == INTEGER_CST
)
1255 if (operand_equal_p (expr
, boolean_true_node
, 0))
1256 return noexcept_true_spec
;
1259 gcc_checking_assert (operand_equal_p (expr
, boolean_false_node
, 0));
1260 return noexcept_false_spec
;
1263 else if (expr
== error_mark_node
)
1264 return error_mark_node
;
1267 gcc_assert (processing_template_decl
1268 || TREE_CODE (expr
) == DEFERRED_NOEXCEPT
);
1269 if (TREE_CODE (expr
) != DEFERRED_NOEXCEPT
)
1270 /* Avoid problems with a function type built with a dependent typedef
1271 being reused in another scope (c++/84045). */
1272 expr
= strip_typedefs_expr (expr
);
1273 return build_tree_list (expr
, NULL_TREE
);
1277 /* If the current function has a cleanup that might throw, and the return value
1278 has a non-trivial destructor, return a MODIFY_EXPR to set
1279 current_retval_sentinel so that we know that the return value needs to be
1280 destroyed on throw. Do the same if the current function might use the
1281 named return value optimization, so we don't destroy it on return.
1282 Otherwise, returns NULL_TREE.
1284 The sentinel is set to indicate that we're in the process of returning, and
1285 therefore should destroy a normal return value on throw, and shouldn't
1286 destroy a named return value variable on normal scope exit. It is set on
1287 return, and cleared either by maybe_splice_retval_cleanup, or when an
1288 exception reaches the NRV scope (finalize_nrv_r). Note that once return
1289 passes the NRV scope, it's effectively a normal return value, so cleanup
1290 past that point is handled by maybe_splice_retval_cleanup. */
1293 maybe_set_retval_sentinel ()
1295 if (processing_template_decl
)
1297 tree retval
= DECL_RESULT (current_function_decl
);
1298 if (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (retval
)))
1300 if (!cp_function_chain
->throwing_cleanup
1301 && (current_function_return_value
== error_mark_node
1302 || current_function_return_value
== NULL_TREE
))
1305 if (!current_retval_sentinel
)
1307 /* Just create the temporary now, maybe_splice_retval_cleanup
1308 will do the rest. */
1309 current_retval_sentinel
= create_temporary_var (boolean_type_node
);
1310 DECL_INITIAL (current_retval_sentinel
) = boolean_false_node
;
1311 pushdecl_outermost_localscope (current_retval_sentinel
);
1314 return build2 (MODIFY_EXPR
, boolean_type_node
,
1315 current_retval_sentinel
, boolean_true_node
);
1318 /* COMPOUND_STMT is the STATEMENT_LIST for some block. If COMPOUND_STMT is the
1319 current function body or a try block, and current_retval_sentinel was set in
1320 this function, wrap the block in a CLEANUP_STMT to destroy the return value
1324 maybe_splice_retval_cleanup (tree compound_stmt
, bool is_try
)
1326 if (!current_function_decl
|| !cfun
1327 || DECL_CONSTRUCTOR_P (current_function_decl
)
1328 || DECL_DESTRUCTOR_P (current_function_decl
)
1329 || !current_retval_sentinel
)
1332 /* if we need a cleanup for the return value, add it in at the same level as
1333 pushdecl_outermost_localscope. And also in try blocks. */
1334 cp_binding_level
*b
= current_binding_level
;
1335 const bool function_body
= b
->kind
== sk_function_parms
;
1337 if (function_body
|| is_try
)
1339 location_t loc
= DECL_SOURCE_LOCATION (current_function_decl
);
1340 tree_stmt_iterator iter
= tsi_start (compound_stmt
);
1341 tree retval
= DECL_RESULT (current_function_decl
);
1345 /* Add a DECL_EXPR for current_retval_sentinel. */
1346 tree decl_expr
= build_stmt (loc
, DECL_EXPR
, current_retval_sentinel
);
1347 tsi_link_before (&iter
, decl_expr
, TSI_SAME_STMT
);
1350 if (!cp_function_chain
->throwing_cleanup
)
1351 /* We're only using the sentinel for an NRV. */
1354 /* Skip past other decls, they can't contain a return. */
1355 while (!tsi_end_p (iter
)
1356 && TREE_CODE (tsi_stmt (iter
)) == DECL_EXPR
)
1359 if (tsi_end_p (iter
))
1360 /* Nothing to wrap. */
1363 /* Wrap the rest of the STATEMENT_LIST in a CLEANUP_STMT. */
1364 tree stmts
= NULL_TREE
;
1365 while (!tsi_end_p (iter
))
1367 append_to_statement_list_force (tsi_stmt (iter
), &stmts
);
1370 tree dtor
= build_cleanup (retval
);
1373 /* Clear the sentinel so we don't try to destroy the retval again on
1374 rethrow (c++/112301). */
1375 tree clear
= build2 (MODIFY_EXPR
, boolean_type_node
,
1376 current_retval_sentinel
, boolean_false_node
);
1377 dtor
= build2 (COMPOUND_EXPR
, void_type_node
, clear
, dtor
);
1379 tree cond
= build3 (COND_EXPR
, void_type_node
, current_retval_sentinel
,
1381 tree cleanup
= build_stmt (loc
, CLEANUP_STMT
,
1382 stmts
, cond
, retval
);
1383 CLEANUP_EH_ONLY (cleanup
) = true;
1384 append_to_statement_list_force (cleanup
, &compound_stmt
);
1388 #include "gt-cp-except.h"