Debian Codegraph @

gcc/cp/except.cc

raw
1 /* Handle exceptional things in C++.
2 Copyright (C) 1989-2025 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann <[email protected]>
4 Rewritten by Mike Stump <[email protected]>, based upon an
5 initial re-implementation courtesy Tad Hunt.
6
7 This file is part of GCC.
8
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)
12 any later version.
13
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.
18
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/>. */
22
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "cp-tree.h"
28 #include "stringpool.h"
29 #include "trans-mem.h"
30 #include "attribs.h"
31 #include "tree-iterator.h"
32 #include "target.h"
33
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,
43 tsubst_flags_t);
44
45 /* Initializes the node to std::terminate, which is used in exception
46 handling and contract handling. */
47
48 void
49 init_terminate_fn (void)
50 {
51 if (terminate_fn)
52 return;
53
54 tree tmp;
55
56 push_nested_namespace (std_node);
57 tmp = build_function_type_list (void_type_node, NULL_TREE);
58 terminate_fn = build_cp_library_fn_ptr ("terminate", tmp,
59 ECF_NOTHROW | ECF_NORETURN
60 | ECF_COLD);
61 gcc_checking_assert (TREE_THIS_VOLATILE (terminate_fn)
62 && TREE_NOTHROW (terminate_fn));
63 pop_nested_namespace (std_node);
64
65 }
66
67 /* Sets up all the global eh stuff that needs to be initialized at the
68 start of compilation. */
69
70 void
71 init_exception_processing (void)
72 {
73 tree tmp;
74
75 /* void std::terminate (); */
76 init_terminate_fn ();
77
78 /* void __cxa_call_unexpected(void *); */
79 tmp = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
80 call_unexpected_fn
81 = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
82 call_terminate_fn
83 = push_library_fn (get_identifier ("__cxa_call_terminate"), tmp, NULL_TREE,
84 ECF_NORETURN | ECF_COLD | ECF_NOTHROW);
85 }
86
87 /* Returns an expression to be executed if an unhandled exception is
88 propagated out of a cleanup region. */
89
90 tree
91 cp_protect_cleanup_actions (void)
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 call_terminate_fn;
98 }
99
100 static tree
101 prepare_eh_type (tree type)
102 {
103 if (type == NULL_TREE)
104 return type;
105 if (type == error_mark_node)
106 return error_mark_node;
107
108 /* peel back references, so they match. */
109 type = non_reference (type);
110
111 /* Peel off cv qualifiers. */
112 type = TYPE_MAIN_VARIANT (type);
113
114 /* Functions and arrays decay to pointers. */
115 type = type_decays_to (type);
116
117 return type;
118 }
119
120 /* Return the type info for TYPE as used by EH machinery. */
121 tree
122 eh_type_info (tree type)
123 {
124 if (type == NULL_TREE || type == error_mark_node)
125 return type;
126
127 return get_tinfo_decl (type);
128 }
129
130 /* Build the address of a typeinfo decl for use in the runtime
131 matching field of the exception model. */
132
133 tree
134 build_eh_type_type (tree type)
135 {
136 tree exp = eh_type_info (type);
137
138 if (!exp)
139 return NULL;
140
141 mark_used (exp);
142
143 return convert (ptr_type_node, build_address (exp));
144 }
145
146 tree
147 build_exc_ptr (void)
148 {
149 return build_call_n (builtin_decl_explicit (BUILT_IN_EH_POINTER),
150 1, integer_zero_node);
151 }
152
153 /* Declare an exception ABI entry point called NAME.
154 ECF are the library flags, RTYPE the return type and ARGS[NARGS]
155 the parameter types. We return the DECL -- which might be one
156 found via the symbol table pushing, if the user already declared
157 it. If we pushed a new decl, the user will see it. */
158
159 static tree
160 declare_library_fn_1 (const char *name, int ecf,
161 tree rtype, int nargs, tree args[])
162 {
163 tree ident = get_identifier (name);
164 tree except = ecf & ECF_NOTHROW ? empty_except_spec : NULL_TREE;
165
166 /* Make a new decl. */
167 tree arg_list = void_list_node;
168 for (unsigned ix = nargs; ix--;)
169 arg_list = tree_cons (NULL_TREE, args[ix], arg_list);
170 tree fntype = build_function_type (rtype, arg_list);
171 tree res = push_library_fn (ident, fntype, except, ecf);
172
173 return res;
174 }
175
176 /* Find or declare a function NAME, returning RTYPE, taking a single
177 parameter PTYPE, with an empty exception specification. ECF are the
178 library fn flags. If TM_ECF is non-zero, also find or create a
179 transaction variant and record it as a replacement, when flag_tm is
180 in effect.
181
182 Note that the C++ ABI document does not have a throw-specifier on
183 the routines declared below via this function. The declarations
184 are consistent with the actual implementations in libsupc++. */
185
186 static tree
187 declare_library_fn (const char *name, tree rtype, tree ptype,
188 int ecf, int tm_ecf)
189 {
190 tree res = declare_library_fn_1 (name, ecf, rtype, ptype ? 1 : 0, &ptype);
191 if (res == error_mark_node)
192 return res;
193
194 if (tm_ecf && flag_tm)
195 {
196 char *tm_name = concat ("_ITM_", name + 2, NULL_TREE);
197
198 tree tm_fn = declare_library_fn_1 (tm_name, ecf | tm_ecf, rtype,
199 ptype ? 1 : 0, &ptype);
200 free (tm_name);
201 if (tm_fn != error_mark_node)
202 record_tm_replacement (res, tm_fn);
203 }
204
205 return res;
206 }
207
208 /* Build up a call to __cxa_get_exception_ptr so that we can build a
209 copy constructor for the thrown object. */
210
211 static tree
212 do_get_exception_ptr (void)
213 {
214 if (!get_exception_ptr_fn)
215 /* Declare void* __cxa_get_exception_ptr (void *) throw(). */
216 get_exception_ptr_fn
217 = declare_library_fn ("__cxa_get_exception_ptr",
218 ptr_type_node, ptr_type_node,
219 ECF_NOTHROW | ECF_PURE | ECF_LEAF | ECF_TM_PURE,
220 0);
221
222 return cp_build_function_call_nary (get_exception_ptr_fn,
223 tf_warning_or_error,
224 build_exc_ptr (), NULL_TREE);
225 }
226
227 /* Build up a call to __cxa_begin_catch, to tell the runtime that the
228 exception has been handled. */
229
230 static tree
231 do_begin_catch (void)
232 {
233 if (!begin_catch_fn)
234 /* Declare void* __cxa_begin_catch (void *) throw(). */
235 begin_catch_fn
236 = declare_library_fn ("__cxa_begin_catch",
237 ptr_type_node, ptr_type_node, ECF_NOTHROW,
238 ECF_TM_PURE);
239
240 return cp_build_function_call_nary (begin_catch_fn, tf_warning_or_error,
241 build_exc_ptr (), NULL_TREE);
242 }
243
244 /* Returns nonzero if cleaning up an exception of type TYPE (which can be
245 NULL_TREE for a ... handler) will not throw an exception. */
246
247 static int
248 dtor_nothrow (tree type)
249 {
250 if (type == NULL_TREE || type == error_mark_node)
251 return 0;
252
253 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
254 return 1;
255
256 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
257 lazily_declare_fn (sfk_destructor, type);
258
259 return TREE_NOTHROW (CLASSTYPE_DESTRUCTOR (type));
260 }
261
262 /* Build up a call to __cxa_end_catch, to destroy the exception object
263 for the current catch block if no others are currently using it. */
264
265 static tree
266 do_end_catch (tree type)
267 {
268 if (!end_catch_fn)
269 /* Declare void __cxa_end_catch ().
270 This can throw if the destructor for the exception throws. */
271 end_catch_fn
272 = declare_library_fn ("__cxa_end_catch", void_type_node,
273 NULL_TREE, 0, ECF_TM_PURE);
274
275 tree cleanup = cp_build_function_call_vec (end_catch_fn,
276 NULL, tf_warning_or_error);
277 if (cleanup != error_mark_node)
278 TREE_NOTHROW (cleanup) = dtor_nothrow (type);
279
280 return cleanup;
281 }
282
283 /* This routine creates the cleanup for the current exception. */
284
285 static void
286 push_eh_cleanup (tree type)
287 {
288 finish_decl_cleanup (NULL_TREE, do_end_catch (type));
289 }
290
291 /* Wrap EXPR in a MUST_NOT_THROW_EXPR expressing that EXPR must
292 not throw any exceptions if COND is true. A condition of
293 NULL_TREE is treated as 'true'. */
294
295 tree
296 build_must_not_throw_expr (tree body, tree cond)
297 {
298 tree type = body ? TREE_TYPE (body) : void_type_node;
299
300 if (!flag_exceptions)
301 return body;
302
303 if (!cond)
304 /* OK, unconditional. */;
305 else
306 {
307 tree conv = NULL_TREE;
308 if (!type_dependent_expression_p (cond))
309 conv = perform_implicit_conversion_flags (boolean_type_node, cond,
310 tf_warning_or_error,
311 LOOKUP_NORMAL);
312 if (tree inst = instantiate_non_dependent_or_null (conv))
313 cond = cxx_constant_value (inst);
314 else
315 require_constant_expression (cond);
316 if (integer_zerop (cond))
317 return body;
318 else if (integer_onep (cond))
319 cond = NULL_TREE;
320 }
321
322 return build2 (MUST_NOT_THROW_EXPR, type, body, cond);
323 }
324
325
326 /* Initialize the catch parameter DECL. */
327
328 static void
329 initialize_handler_parm (tree decl, tree exp)
330 {
331 tree init;
332 tree init_type;
333
334 /* Make sure we mark the catch param as used, otherwise we'll get a
335 warning about an unused ((anonymous)). */
336 TREE_USED (decl) = 1;
337 DECL_READ_P (decl) = 1;
338
339 /* Figure out the type that the initializer is. Pointers are returned
340 adjusted by value from __cxa_begin_catch. Others are returned by
341 reference. */
342 init_type = TREE_TYPE (decl);
343 if (!INDIRECT_TYPE_P (init_type))
344 init_type = build_reference_type (init_type);
345
346 /* Since pointers are passed by value, initialize a reference to
347 pointer catch parm with the address of the temporary. */
348 if (TYPE_REF_P (init_type)
349 && TYPE_PTR_P (TREE_TYPE (init_type)))
350 exp = cp_build_addr_expr (exp, tf_warning_or_error);
351
352 exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0,
353 tf_warning_or_error);
354
355 init = convert_from_reference (exp);
356
357 /* If the constructor for the catch parm exits via an exception, we
358 must call terminate. See eh23.C. */
359 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
360 {
361 /* Generate the copy constructor call directly so we can wrap it.
362 See also expand_default_init. */
363 init = ocp_convert (TREE_TYPE (decl), init,
364 CONV_IMPLICIT|CONV_FORCE_TEMP, 0,
365 tf_warning_or_error);
366 /* Force cleanups now to avoid nesting problems with the
367 MUST_NOT_THROW_EXPR. */
368 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
369 init = build_must_not_throw_expr (init, NULL_TREE);
370 }
371
372 decl = pushdecl (decl);
373
374 start_decl_1 (decl, true);
375 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
376 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
377 }
378
379
380 /* Routine to see if exception handling is turned on.
381 DO_WARN is nonzero if we want to inform the user that exception
382 handling is turned off.
383
384 This is used to ensure that -fexceptions has been specified if the
385 compiler tries to use any exception-specific functions. */
386
387 static inline int
388 doing_eh (void)
389 {
390 if (! flag_exceptions)
391 {
392 static int warned = 0;
393 if (! warned)
394 {
395 error ("exception handling disabled, use %<-fexceptions%> to enable");
396 warned = 1;
397 }
398 return 0;
399 }
400 return 1;
401 }
402
403 /* Call this to start a catch block. DECL is the catch parameter. */
404
405 tree
406 expand_start_catch_block (tree decl)
407 {
408 tree exp;
409 tree type, init;
410
411 if (! doing_eh ())
412 return NULL_TREE;
413
414 if (decl)
415 {
416 if (!is_admissible_throw_operand_or_catch_parameter (decl, false,
417 tf_warning_or_error))
418 decl = error_mark_node;
419
420 type = prepare_eh_type (TREE_TYPE (decl));
421 mark_used (eh_type_info (type));
422 }
423 else
424 type = NULL_TREE;
425
426 /* Call __cxa_end_catch at the end of processing the exception. */
427 push_eh_cleanup (type);
428
429 init = do_begin_catch ();
430
431 /* If there's no decl at all, then all we need to do is make sure
432 to tell the runtime that we've begun handling the exception. */
433 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
434 finish_expr_stmt (init);
435
436 /* If the C++ object needs constructing, we need to do that before
437 calling __cxa_begin_catch, so that std::uncaught_exception gets
438 the right value during the copy constructor. */
439 else if (flag_use_cxa_get_exception_ptr
440 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
441 {
442 exp = do_get_exception_ptr ();
443 if (exp != error_mark_node)
444 initialize_handler_parm (decl, exp);
445 finish_expr_stmt (init);
446 }
447
448 /* Otherwise the type uses a bitwise copy, and we don't have to worry
449 about the value of std::uncaught_exception and therefore can do the
450 copy with the return value of __cxa_end_catch instead. */
451 else
452 {
453 tree init_type = type;
454
455 /* Pointers are passed by values, everything else by reference. */
456 if (!TYPE_PTR_P (type))
457 init_type = build_pointer_type (type);
458 if (init_type != TREE_TYPE (init))
459 init = build1 (NOP_EXPR, init_type, init);
460 exp = create_temporary_var (init_type);
461 cp_finish_decl (exp, init, /*init_const_expr=*/false,
462 NULL_TREE, LOOKUP_ONLYCONVERTING);
463 DECL_REGISTER (exp) = 1;
464 initialize_handler_parm (decl, exp);
465 }
466
467 return type;
468 }
469
470 /* True if we are in a catch block within a catch block. Assumes that we are
471 in function scope. */
472
473 static bool
474 in_nested_catch (void)
475 {
476 int catches = 0;
477
478 /* Scan through the template parameter scopes. */
479 for (cp_binding_level *b = current_binding_level;
480 b->kind != sk_function_parms;
481 b = b->level_chain)
482 if (b->kind == sk_catch
483 && ++catches == 2)
484 return true;
485 return false;
486 }
487
488 /* Call this to end a catch block. Its responsible for emitting the
489 code to handle jumping back to the correct place, and for emitting
490 the label to jump to if this catch block didn't match. */
491
492 void
493 expand_end_catch_block (void)
494 {
495 if (! doing_eh ())
496 return;
497
498 /* The exception being handled is rethrown if control reaches the end of
499 a handler of the function-try-block of a constructor or destructor. */
500 if (in_function_try_handler
501 && (DECL_CONSTRUCTOR_P (current_function_decl)
502 || DECL_DESTRUCTOR_P (current_function_decl))
503 && !in_nested_catch ())
504 {
505 tree rethrow = build_throw (input_location, NULL_TREE,
506 tf_warning_or_error);
507 /* Disable all warnings for the generated rethrow statement. */
508 suppress_warning (rethrow);
509 finish_expr_stmt (rethrow);
510 }
511 }
512
513 tree
514 begin_eh_spec_block (void)
515 {
516 tree r;
517 location_t spec_location = DECL_SOURCE_LOCATION (current_function_decl);
518
519 /* A noexcept specification (or throw() with -fnothrow-opt) is a
520 MUST_NOT_THROW_EXPR. */
521 if (TYPE_NOEXCEPT_P (TREE_TYPE (current_function_decl)))
522 {
523 r = build_stmt (spec_location, MUST_NOT_THROW_EXPR,
524 NULL_TREE, NULL_TREE);
525 TREE_SIDE_EFFECTS (r) = 1;
526 }
527 else
528 r = build_stmt (spec_location, EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
529 add_stmt (r);
530 TREE_OPERAND (r, 0) = push_stmt_list ();
531 return r;
532 }
533
534 void
535 finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
536 {
537 tree raises;
538
539 TREE_OPERAND (eh_spec_block, 0)
540 = pop_stmt_list (TREE_OPERAND (eh_spec_block, 0));
541
542 if (TREE_CODE (eh_spec_block) == MUST_NOT_THROW_EXPR)
543 return;
544
545 /* Strip cv quals, etc, from the specification types. */
546 for (raises = NULL_TREE;
547 raw_raises && TREE_VALUE (raw_raises);
548 raw_raises = TREE_CHAIN (raw_raises))
549 {
550 tree type = prepare_eh_type (TREE_VALUE (raw_raises));
551 tree tinfo = eh_type_info (type);
552
553 mark_used (tinfo);
554 raises = tree_cons (NULL_TREE, type, raises);
555 }
556
557 EH_SPEC_RAISES (eh_spec_block) = raises;
558 }
559
560 /* Return a pointer to a buffer for an exception object of type TYPE. */
561
562 static tree
563 do_allocate_exception (tree type)
564 {
565 if (!allocate_exception_fn)
566 /* Declare void *__cxa_allocate_exception(size_t) throw(). */
567 allocate_exception_fn
568 = declare_library_fn ("__cxa_allocate_exception",
569 ptr_type_node, size_type_node,
570 ECF_NOTHROW | ECF_MALLOC | ECF_COLD, ECF_TM_PURE);
571
572 return cp_build_function_call_nary (allocate_exception_fn,
573 tf_warning_or_error,
574 size_in_bytes (type), NULL_TREE);
575 }
576
577 /* Call __cxa_free_exception from a cleanup. This is never invoked
578 directly, but see the comment for stabilize_throw_expr. */
579
580 static tree
581 do_free_exception (tree ptr)
582 {
583 if (!free_exception_fn)
584 /* Declare void __cxa_free_exception (void *) throw(). */
585 free_exception_fn
586 = declare_library_fn ("__cxa_free_exception",
587 void_type_node, ptr_type_node,
588 ECF_NOTHROW | ECF_LEAF, ECF_TM_PURE);
589
590 return cp_build_function_call_nary (free_exception_fn,
591 tf_warning_or_error, ptr, NULL_TREE);
592 }
593
594 /* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
595 Called from build_throw via walk_tree_without_duplicates. */
596
597 static tree
598 wrap_cleanups_r (tree *tp, int *walk_subtrees, void * /*data*/)
599 {
600 tree exp = *tp;
601 tree cleanup;
602
603 /* Don't walk into types. */
604 if (TYPE_P (exp))
605 {
606 *walk_subtrees = 0;
607 return NULL_TREE;
608 }
609 if (TREE_CODE (exp) != TARGET_EXPR)
610 return NULL_TREE;
611
612 cleanup = TARGET_EXPR_CLEANUP (exp);
613 if (cleanup)
614 {
615 cleanup = build2 (MUST_NOT_THROW_EXPR, void_type_node, cleanup,
616 NULL_TREE);
617 TARGET_EXPR_CLEANUP (exp) = cleanup;
618 }
619
620 /* Keep iterating. */
621 return NULL_TREE;
622 }
623
624 /* Build a throw expression. */
625
626 tree
627 build_throw (location_t loc, tree exp, tsubst_flags_t complain)
628 {
629 if (exp == error_mark_node)
630 return exp;
631
632 if (processing_template_decl)
633 {
634 if (cfun)
635 current_function_returns_abnormally = 1;
636 exp = build_min (THROW_EXPR, void_type_node, exp);
637 SET_EXPR_LOCATION (exp, loc);
638 return exp;
639 }
640
641 if (exp && null_node_p (exp) && (complain & tf_warning))
642 warning_at (loc, 0,
643 "throwing NULL, which has integral, not pointer type");
644
645 if (exp && !is_admissible_throw_operand_or_catch_parameter (exp,
646 /*is_throw=*/true,
647 complain))
648 return error_mark_node;
649
650 if (! doing_eh ())
651 return error_mark_node;
652
653 if (exp)
654 {
655 tree throw_type;
656 tree temp_type;
657 tree cleanup;
658 tree object, ptr;
659 tree allocate_expr;
660
661 /* The CLEANUP_TYPE is the internal type of a destructor. */
662 if (!cleanup_type)
663 cleanup_type = get_cxa_atexit_fn_ptr_type ();
664
665 if (!throw_fn)
666 {
667 tree args[3] = {ptr_type_node, ptr_type_node, cleanup_type};
668
669 throw_fn = declare_library_fn_1 ("__cxa_throw",
670 ECF_NORETURN | ECF_XTHROW | ECF_COLD,
671 void_type_node, 3, args);
672 if (flag_tm && throw_fn != error_mark_node)
673 {
674 tree itm_fn = declare_library_fn_1 ("_ITM_cxa_throw",
675 ECF_NORETURN | ECF_XTHROW
676 | ECF_COLD,
677 void_type_node, 3, args);
678 if (itm_fn != error_mark_node)
679 {
680 apply_tm_attr (itm_fn, get_identifier ("transaction_pure"));
681 record_tm_replacement (throw_fn, itm_fn);
682 }
683 }
684 }
685
686 /* [except.throw]
687
688 A throw-expression initializes a temporary object, the type
689 of which is determined by removing any top-level
690 cv-qualifiers from the static type of the operand of throw
691 and adjusting the type from "array of T" or "function return
692 T" to "pointer to T" or "pointer to function returning T"
693 respectively. */
694 temp_type = is_bitfield_expr_with_lowered_type (exp);
695 if (!temp_type)
696 temp_type = cv_unqualified (type_decays_to (TREE_TYPE (exp)));
697
698 /* OK, this is kind of wacky. The standard says that we call
699 terminate when the exception handling mechanism, after
700 completing evaluation of the expression to be thrown but
701 before the exception is caught (_except.throw_), calls a
702 user function that exits via an uncaught exception.
703
704 So we have to protect the actual initialization of the
705 exception object with terminate(), but evaluate the
706 expression first. Since there could be temps in the
707 expression, we need to handle that, too. We also expand
708 the call to __cxa_allocate_exception first (which doesn't
709 matter, since it can't throw). */
710
711 /* Allocate the space for the exception. */
712 allocate_expr = do_allocate_exception (temp_type);
713 if (allocate_expr == error_mark_node)
714 return error_mark_node;
715 allocate_expr = get_internal_target_expr (allocate_expr);
716 ptr = TARGET_EXPR_SLOT (allocate_expr);
717 TARGET_EXPR_CLEANUP (allocate_expr) = do_free_exception (ptr);
718 CLEANUP_EH_ONLY (allocate_expr) = 1;
719
720 object = build_nop (build_pointer_type (temp_type), ptr);
721 object = cp_build_fold_indirect_ref (object);
722
723 /* And initialize the exception object. */
724 if (CLASS_TYPE_P (temp_type))
725 {
726 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
727 location_t exp_loc = cp_expr_loc_or_loc (exp, loc);
728
729 /* Under C++0x [12.8/16 class.copy], a thrown lvalue is sometimes
730 treated as an rvalue for the purposes of overload resolution
731 to favor move constructors over copy constructors. */
732 if (tree moved = treat_lvalue_as_rvalue_p (exp, /*return*/false))
733 /* In C++20 we treat the return value as an rvalue that
734 can bind to lvalue refs. In C++23, such an expression is just
735 an xvalue. */
736 exp = moved;
737
738 /* Call the copy constructor. */
739 auto_diagnostic_group d;
740 releasing_vec exp_vec (make_tree_vector_single (exp));
741 exp = build_special_member_call (object, complete_ctor_identifier,
742 &exp_vec, TREE_TYPE (object), flags,
743 complain);
744 if (exp == error_mark_node)
745 {
746 if (complain & tf_error)
747 inform (exp_loc, " in thrown expression");
748 return error_mark_node;
749 }
750 }
751 else
752 {
753 tree tmp = decay_conversion (exp, complain);
754 if (tmp == error_mark_node)
755 return error_mark_node;
756 exp = cp_build_init_expr (object, tmp);
757 }
758
759 /* Mark any cleanups from the initialization as MUST_NOT_THROW, since
760 they are run after the exception object is initialized. */
761 cp_walk_tree_without_duplicates (&exp, wrap_cleanups_r, 0);
762
763 /* Prepend the allocation. */
764 exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
765
766 /* Force all the cleanups to be evaluated here so that we don't have
767 to do them during unwinding. */
768 exp = build1 (CLEANUP_POINT_EXPR, void_type_node, exp);
769
770 throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
771
772 cleanup = NULL_TREE;
773 if (type_build_dtor_call (TREE_TYPE (object)))
774 {
775 tree binfo = TYPE_BINFO (TREE_TYPE (object));
776 tree dtor_fn = lookup_fnfields (binfo,
777 complete_dtor_identifier, 0,
778 complain);
779 dtor_fn = BASELINK_FUNCTIONS (dtor_fn);
780 if (!mark_used (dtor_fn)
781 || !perform_or_defer_access_check (binfo, dtor_fn,
782 dtor_fn, complain))
783 return error_mark_node;
784 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
785 {
786 cxx_mark_addressable (dtor_fn);
787 /* Pretend it's a normal function. */
788 cleanup = build1 (ADDR_EXPR, cleanup_type, dtor_fn);
789 }
790 }
791 if (cleanup == NULL_TREE)
792 cleanup = build_int_cst (cleanup_type, 0);
793
794 /* ??? Indicate that this function call throws throw_type. */
795 tree tmp = cp_build_function_call_nary (throw_fn, complain,
796 ptr, throw_type, cleanup,
797 NULL_TREE);
798
799 /* Tack on the initialization stuff. */
800 exp = build2 (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp);
801 }
802 else
803 {
804 /* Rethrow current exception. */
805 if (!rethrow_fn)
806 {
807 rethrow_fn = declare_library_fn_1 ("__cxa_rethrow",
808 ECF_NORETURN | ECF_XTHROW
809 | ECF_COLD,
810 void_type_node, 0, NULL);
811 if (flag_tm && rethrow_fn != error_mark_node)
812 apply_tm_attr (rethrow_fn, get_identifier ("transaction_pure"));
813 }
814
815 /* ??? Indicate that this function call allows exceptions of the type
816 of the enclosing catch block (if known). */
817 exp = cp_build_function_call_vec (rethrow_fn, NULL, complain);
818 }
819
820 exp = build1_loc (loc, THROW_EXPR, void_type_node, exp);
821
822 return exp;
823 }
824
825 /* Make sure TYPE is complete, pointer to complete, reference to
826 complete, or pointer to cv void. Issue diagnostic on failure.
827 Return the zero on failure and nonzero on success. FROM can be
828 the expr or decl from whence TYPE came, if available. */
829
830 static bool
831 complete_ptr_ref_or_void_ptr_p (tree type, tree from, tsubst_flags_t complain)
832 {
833 /* Check complete. */
834 type = complete_type_or_maybe_complain (type, from, complain);
835 if (!type)
836 return false;
837
838 /* Or a pointer or ref to one, or cv void *. */
839 const bool is_ptr = TYPE_PTR_P (type);
840 if (is_ptr || TYPE_REF_P (type))
841 {
842 tree core = TREE_TYPE (type);
843
844 if (is_ptr && VOID_TYPE_P (core))
845 /* OK */;
846 else if (!complete_type_or_maybe_complain (core, from, complain))
847 return false;
848 }
849 return true;
850 }
851
852 /* If IS_THROW is true return truth-value if T is an expression admissible
853 in throw-expression, i.e. if it is not of incomplete type or a pointer/
854 reference to such a type or of an abstract class type.
855 If IS_THROW is false, likewise for a catch parameter, same requirements
856 for its type plus rvalue reference type is also not admissible. */
857
858 static bool
859 is_admissible_throw_operand_or_catch_parameter (tree t, bool is_throw,
860 tsubst_flags_t complain)
861 {
862 tree expr = is_throw ? t : NULL_TREE;
863 tree type = TREE_TYPE (t);
864
865 /* C++11 [except.handle] The exception-declaration shall not denote
866 an incomplete type, an abstract class type, or an rvalue reference
867 type. */
868
869 /* 15.1/4 [...] The type of the throw-expression shall not be an
870 incomplete type, or a pointer or a reference to an incomplete
871 type, other than void*, const void*, volatile void*, or
872 const volatile void*. Except for these restriction and the
873 restrictions on type matching mentioned in 15.3, the operand
874 of throw is treated exactly as a function argument in a call
875 (5.2.2) or the operand of a return statement. */
876 if (!complete_ptr_ref_or_void_ptr_p (type, expr, complain))
877 return false;
878
879 tree nonref_type = non_reference (type);
880 if (!verify_type_context (input_location, TCTX_EXCEPTIONS, nonref_type))
881 return false;
882
883 /* 10.4/3 An abstract class shall not be used as a parameter type,
884 as a function return type or as type of an explicit
885 conversion. */
886 else if (abstract_virtuals_error (is_throw ? ACU_THROW : ACU_CATCH, type,
887 complain))
888 return false;
889 else if (!is_throw
890 && TYPE_REF_P (type)
891 && TYPE_REF_IS_RVALUE (type))
892 {
893 if (complain & tf_error)
894 error ("cannot declare %<catch%> parameter to be of rvalue "
895 "reference type %qT", type);
896 return false;
897 }
898 else if (variably_modified_type_p (type, NULL_TREE))
899 {
900 if (complain & tf_error)
901 {
902 if (is_throw)
903 error_at (cp_expr_loc_or_input_loc (expr),
904 "cannot throw expression of type %qT because it involves "
905 "types of variable size", type);
906 else
907 error ("cannot catch type %qT because it involves types of "
908 "variable size", type);
909 }
910 return false;
911 }
912
913 return true;
914 }
915
916 /* Returns nonzero if FN is a declaration of a standard C library
917 function which is known not to throw.
918
919 [lib.res.on.exception.handling]: None of the functions from the
920 Standard C library shall report an error by throwing an
921 exception, unless it calls a program-supplied function that
922 throws an exception. */
923
924 #include "cfns.h"
925
926 int
927 nothrow_libfn_p (const_tree fn)
928 {
929 tree id;
930
931 if (TREE_PUBLIC (fn)
932 && DECL_EXTERNAL (fn)
933 && DECL_NAMESPACE_SCOPE_P (fn)
934 && DECL_EXTERN_C_P (fn))
935 /* OK */;
936 else
937 /* Can't be a C library function. */
938 return 0;
939
940 /* Being a C library function, DECL_ASSEMBLER_NAME == DECL_NAME
941 unless the system headers are playing rename tricks, and if
942 they are, we don't want to be confused by them. */
943 id = DECL_NAME (fn);
944 const struct libc_name_struct *s
945 = libc_name::libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
946 if (s == NULL)
947 return 0;
948 switch (s->c_ver)
949 {
950 case 89: return 1;
951 case 99: return !flag_iso || flag_isoc99;
952 case 11: return !flag_iso || flag_isoc11;
953 default: gcc_unreachable ();
954 }
955 }
956
957 /* Returns nonzero if an exception of type FROM will be caught by a
958 handler for type TO, as per [except.handle]. */
959
960 static bool
961 can_convert_eh (tree to, tree from)
962 {
963 to = non_reference (to);
964 from = non_reference (from);
965
966 if (same_type_ignoring_top_level_qualifiers_p (to, from))
967 return true;
968
969 if (TYPE_PTR_P (to) && TYPE_PTR_P (from))
970 {
971 to = TREE_TYPE (to);
972 from = TREE_TYPE (from);
973
974 if (! at_least_as_qualified_p (to, from))
975 return false;
976
977 if (VOID_TYPE_P (to))
978 return true;
979
980 /* Else fall through. */
981 }
982
983 if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
984 && publicly_uniquely_derived_p (to, from))
985 return true;
986
987 return false;
988 }
989
990 /* Check whether any of the handlers in I are shadowed by another handler
991 accepting TYPE. Note that the shadowing may not be complete; even if
992 an exception of type B would be caught by a handler for A, there could
993 be a derived class C for which A is an ambiguous base but B is not, so
994 the handler for B would catch an exception of type C. */
995
996 static void
997 check_handlers_1 (tree master, tree_stmt_iterator i)
998 {
999 tree type = TREE_TYPE (master);
1000
1001 for (; !tsi_end_p (i); tsi_next (&i))
1002 {
1003 tree handler = tsi_stmt (i);
1004 if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
1005 {
1006 auto_diagnostic_group d;
1007 if (warning_at (EXPR_LOCATION (handler), OPT_Wexceptions,
1008 "exception of type %qT will be caught by earlier "
1009 "handler", TREE_TYPE (handler)))
1010 inform (EXPR_LOCATION (master), "for type %qT", type);
1011 break;
1012 }
1013 }
1014 }
1015
1016 /* Given a STATEMENT_LIST of HANDLERs, make sure that they're OK. */
1017
1018 void
1019 check_handlers (tree handlers)
1020 {
1021 tree_stmt_iterator i;
1022
1023 /* If we don't have a STATEMENT_LIST, then we've just got one
1024 handler, and thus nothing to warn about. */
1025 if (TREE_CODE (handlers) != STATEMENT_LIST)
1026 return;
1027
1028 i = tsi_start (handlers);
1029 if (!tsi_end_p (i))
1030 while (1)
1031 {
1032 tree handler = tsi_stmt (i);
1033 tsi_next (&i);
1034
1035 /* No more handlers; nothing to shadow. */
1036 if (tsi_end_p (i))
1037 break;
1038 if (TREE_TYPE (handler) == NULL_TREE)
1039 permerror (EXPR_LOCATION (handler), "%<...%>"
1040 " handler must be the last handler for its try block");
1041 else
1042 check_handlers_1 (handler, i);
1043 }
1044 }
1045
1046 /* walk_tree helper for finish_noexcept_expr. Returns non-null if the
1047 expression *TP causes the noexcept operator to evaluate to false.
1048
1049 5.3.7 [expr.noexcept]: The result of the noexcept operator is false if
1050 in a potentially-evaluated context the expression would contain
1051 * a potentially evaluated call to a function, member function,
1052 function pointer, or member function pointer that does not have a
1053 non-throwing exception-specification (15.4),
1054 * a potentially evaluated throw-expression (15.1),
1055 * a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1056 where T is a reference type, that requires a run-time check (5.2.7), or
1057 * a potentially evaluated typeid expression (5.2.8) applied to a glvalue
1058 expression whose type is a polymorphic class type (10.3). */
1059
1060 static tree
1061 check_noexcept_r (tree *tp, int *walk_subtrees, void *)
1062 {
1063 tree t = *tp;
1064 enum tree_code code = TREE_CODE (t);
1065
1066 if (unevaluated_p (code))
1067 *walk_subtrees = false;
1068 else if ((code == CALL_EXPR && CALL_EXPR_FN (t))
1069 || code == AGGR_INIT_EXPR)
1070 {
1071 /* We can only use the exception specification of the called function
1072 for determining the value of a noexcept expression; we can't use
1073 TREE_NOTHROW, as it might have a different value in another
1074 translation unit, creating ODR problems.
1075
1076 We could use TREE_NOTHROW (t) for !TREE_PUBLIC fns, though... */
1077 tree fn = cp_get_callee (t);
1078 tree type = TREE_TYPE (fn);
1079 gcc_assert (INDIRECT_TYPE_P (type));
1080 type = TREE_TYPE (type);
1081
1082 STRIP_NOPS (fn);
1083 if (TREE_CODE (fn) == ADDR_EXPR)
1084 fn = TREE_OPERAND (fn, 0);
1085 if (TREE_CODE (fn) == FUNCTION_DECL)
1086 {
1087 /* We do use TREE_NOTHROW for ABI internals like __dynamic_cast,
1088 and for C library functions known not to throw. */
1089 if (DECL_EXTERN_C_P (fn)
1090 && (DECL_ARTIFICIAL (fn)
1091 || nothrow_libfn_p (fn)))
1092 return TREE_NOTHROW (fn) ? NULL_TREE : fn;
1093 /* We used to treat a call to a constexpr function as noexcept if
1094 the call was a constant expression (CWG 1129). This has changed
1095 in P0003 whereby noexcept has no special rule for constant
1096 expressions anymore. Since the current behavior is important for
1097 certain library functionality, we treat this as a DR, therefore
1098 adjusting the behavior for C++11 and C++14. Previously, we had
1099 to evaluate the noexcept-specifier's operand here, but that could
1100 cause instantiations that would fail. */
1101 }
1102 if (!TYPE_NOTHROW_P (type))
1103 return fn;
1104 }
1105
1106 return NULL_TREE;
1107 }
1108
1109 /* If a function that causes a noexcept-expression to be false isn't
1110 defined yet, remember it and check it for TREE_NOTHROW again at EOF. */
1111
1112 struct GTY(()) pending_noexcept {
1113 tree fn;
1114 location_t loc;
1115 };
1116 static GTY(()) vec<pending_noexcept, va_gc> *pending_noexcept_checks;
1117
1118 /* FN is a FUNCTION_DECL that caused a noexcept-expr to be false. Warn if
1119 it can't throw.
1120
1121 TODO: Consider extending -Wnoexcept to do something like walk_subtrees in the
1122 case of a defaulted function that obtained a noexcept(false) spec. */
1123
1124 static void
1125 maybe_noexcept_warning (tree fn)
1126 {
1127 if (TREE_NOTHROW (fn)
1128 && (!DECL_IN_SYSTEM_HEADER (fn)
1129 || global_dc->m_warn_system_headers))
1130 {
1131 auto s = make_temp_override (global_dc->m_warn_system_headers, true);
1132 auto_diagnostic_group d;
1133 if (warning (OPT_Wnoexcept, "noexcept-expression evaluates to %<false%> "
1134 "because of a call to %qD", fn))
1135 inform (DECL_SOURCE_LOCATION (fn),
1136 "but %qD does not throw; perhaps "
1137 "it should be declared %<noexcept%>", fn);
1138 }
1139 }
1140
1141 /* Check any functions that weren't defined earlier when they caused a
1142 noexcept expression to evaluate to false. */
1143
1144 void
1145 perform_deferred_noexcept_checks (void)
1146 {
1147 int i;
1148 pending_noexcept *p;
1149 location_t saved_loc = input_location;
1150 FOR_EACH_VEC_SAFE_ELT (pending_noexcept_checks, i, p)
1151 {
1152 input_location = p->loc;
1153 maybe_noexcept_warning (p->fn);
1154 }
1155 input_location = saved_loc;
1156 }
1157
1158 /* Evaluate noexcept ( EXPR ). */
1159
1160 tree
1161 finish_noexcept_expr (tree expr, tsubst_flags_t complain)
1162 {
1163 if (expr == error_mark_node)
1164 return error_mark_node;
1165
1166 if (processing_template_decl)
1167 return build_min (NOEXCEPT_EXPR, boolean_type_node, expr);
1168
1169 return (expr_noexcept_p (expr, complain)
1170 ? boolean_true_node : boolean_false_node);
1171 }
1172
1173 /* Returns whether EXPR is noexcept, possibly warning if allowed by
1174 COMPLAIN. */
1175
1176 bool
1177 expr_noexcept_p (tree expr, tsubst_flags_t complain)
1178 {
1179 tree fn;
1180
1181 if (expr == error_mark_node)
1182 return false;
1183
1184 fn = cp_walk_tree_without_duplicates (&expr, check_noexcept_r, 0);
1185 if (fn)
1186 {
1187 if ((complain & tf_warning) && warn_noexcept
1188 && TREE_CODE (fn) == FUNCTION_DECL)
1189 {
1190 if (!DECL_INITIAL (fn))
1191 {
1192 /* Not defined yet; check again at EOF. */
1193 pending_noexcept p = {fn, input_location};
1194 vec_safe_push (pending_noexcept_checks, p);
1195 }
1196 else
1197 maybe_noexcept_warning (fn);
1198 }
1199 return false;
1200 }
1201 else
1202 return true;
1203 }
1204
1205 /* Return true iff SPEC is throw() or noexcept(true). */
1206
1207 bool
1208 nothrow_spec_p (const_tree spec)
1209 {
1210 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
1211
1212 if (spec == empty_except_spec
1213 || spec == noexcept_true_spec)
1214 return true;
1215
1216 gcc_assert (!spec
1217 || TREE_VALUE (spec)
1218 || spec == noexcept_false_spec
1219 || TREE_PURPOSE (spec) == error_mark_node
1220 || UNPARSED_NOEXCEPT_SPEC_P (spec)
1221 || processing_template_decl);
1222
1223 return false;
1224 }
1225
1226 /* For FUNCTION_TYPE or METHOD_TYPE, true if NODE is noexcept. This is the
1227 case for things declared noexcept(true) and, with -fnothrow-opt, for
1228 throw() functions. */
1229
1230 bool
1231 type_noexcept_p (const_tree type)
1232 {
1233 tree spec = TYPE_RAISES_EXCEPTIONS (type);
1234 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
1235 if (flag_nothrow_opt)
1236 return nothrow_spec_p (spec);
1237 else
1238 return spec == noexcept_true_spec;
1239 }
1240
1241 /* For FUNCTION_TYPE or METHOD_TYPE, true if NODE can throw any type,
1242 i.e. no exception-specification or noexcept(false). */
1243
1244 bool
1245 type_throw_all_p (const_tree type)
1246 {
1247 tree spec = TYPE_RAISES_EXCEPTIONS (type);
1248 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
1249 return spec == NULL_TREE || spec == noexcept_false_spec;
1250 }
1251
1252 /* Create a representation of the noexcept-specification with
1253 constant-expression of EXPR. COMPLAIN is as for tsubst. */
1254
1255 tree
1256 build_noexcept_spec (tree expr, tsubst_flags_t complain)
1257 {
1258 if (check_for_bare_parameter_packs (expr))
1259 return error_mark_node;
1260 if (TREE_CODE (expr) != DEFERRED_NOEXCEPT
1261 && !instantiation_dependent_expression_p (expr))
1262 {
1263 expr = build_converted_constant_bool_expr (expr, complain);
1264 expr = instantiate_non_dependent_expr (expr, complain);
1265 expr = cxx_constant_value (expr, complain);
1266 }
1267 if (TREE_CODE (expr) == INTEGER_CST)
1268 {
1269 if (operand_equal_p (expr, boolean_true_node, 0))
1270 return noexcept_true_spec;
1271 else
1272 {
1273 gcc_checking_assert (operand_equal_p (expr, boolean_false_node, 0));
1274 return noexcept_false_spec;
1275 }
1276 }
1277 else if (expr == error_mark_node)
1278 return error_mark_node;
1279 else
1280 {
1281 gcc_assert (processing_template_decl
1282 || TREE_CODE (expr) == DEFERRED_NOEXCEPT);
1283 if (TREE_CODE (expr) != DEFERRED_NOEXCEPT)
1284 /* Avoid problems with a function type built with a dependent typedef
1285 being reused in another scope (c++/84045). */
1286 expr = strip_typedefs_expr (expr);
1287 return build_tree_list (expr, NULL_TREE);
1288 }
1289 }
1290
1291 /* If the current function has a cleanup that might throw, and the return value
1292 has a non-trivial destructor, return a MODIFY_EXPR to set
1293 current_retval_sentinel so that we know that the return value needs to be
1294 destroyed on throw. Do the same if the current function might use the
1295 named return value optimization, so we don't destroy it on return.
1296 Otherwise, returns NULL_TREE.
1297
1298 The sentinel is set to indicate that we're in the process of returning, and
1299 therefore should destroy a normal return value on throw, and shouldn't
1300 destroy a named return value variable on normal scope exit. It is set on
1301 return, and cleared either by maybe_splice_retval_cleanup, or when an
1302 exception reaches the NRV scope (finalize_nrv_r). Note that once return
1303 passes the NRV scope, it's effectively a normal return value, so cleanup
1304 past that point is handled by maybe_splice_retval_cleanup. */
1305
1306 tree
1307 maybe_set_retval_sentinel ()
1308 {
1309 if (processing_template_decl)
1310 return NULL_TREE;
1311 tree retval = DECL_RESULT (current_function_decl);
1312 if (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (retval)))
1313 return NULL_TREE;
1314 if (!cp_function_chain->throwing_cleanup
1315 && (current_function_return_value == error_mark_node
1316 || current_function_return_value == NULL_TREE))
1317 return NULL_TREE;
1318
1319 if (!current_retval_sentinel)
1320 {
1321 /* Just create the temporary now, maybe_splice_retval_cleanup
1322 will do the rest. */
1323 current_retval_sentinel = create_temporary_var (boolean_type_node);
1324 DECL_INITIAL (current_retval_sentinel) = boolean_false_node;
1325 pushdecl_outermost_localscope (current_retval_sentinel);
1326 }
1327
1328 return build2 (MODIFY_EXPR, boolean_type_node,
1329 current_retval_sentinel, boolean_true_node);
1330 }
1331
1332 /* COMPOUND_STMT is the STATEMENT_LIST for some block. If COMPOUND_STMT is the
1333 current function body or a try block, and current_retval_sentinel was set in
1334 this function, wrap the block in a CLEANUP_STMT to destroy the return value
1335 on throw. */
1336
1337 void
1338 maybe_splice_retval_cleanup (tree compound_stmt, bool is_try)
1339 {
1340 if (!current_function_decl || !cfun
1341 || DECL_CONSTRUCTOR_P (current_function_decl)
1342 || DECL_DESTRUCTOR_P (current_function_decl)
1343 || !current_retval_sentinel)
1344 return;
1345
1346 /* if we need a cleanup for the return value, add it in at the same level as
1347 pushdecl_outermost_localscope. And also in try blocks. */
1348 cp_binding_level *b = current_binding_level;
1349 const bool function_body = b->kind == sk_function_parms;
1350
1351 if (function_body || is_try)
1352 {
1353 location_t loc = DECL_SOURCE_LOCATION (current_function_decl);
1354 tree_stmt_iterator iter = tsi_start (compound_stmt);
1355 tree retval = DECL_RESULT (current_function_decl);
1356
1357 if (function_body)
1358 {
1359 /* Add a DECL_EXPR for current_retval_sentinel. */
1360 tree decl_expr = build_stmt (loc, DECL_EXPR, current_retval_sentinel);
1361 tsi_link_before (&iter, decl_expr, TSI_SAME_STMT);
1362 }
1363
1364 if (!cp_function_chain->throwing_cleanup)
1365 /* We're only using the sentinel for an NRV. */
1366 return;
1367
1368 /* Skip past other decls, they can't contain a return. */
1369 while (!tsi_end_p (iter)
1370 && TREE_CODE (tsi_stmt (iter)) == DECL_EXPR)
1371 tsi_next (&iter);
1372
1373 if (tsi_end_p (iter))
1374 /* Nothing to wrap. */
1375 return;
1376
1377 /* Wrap the rest of the STATEMENT_LIST in a CLEANUP_STMT. */
1378 tree stmts = NULL_TREE;
1379 while (!tsi_end_p (iter))
1380 {
1381 append_to_statement_list_force (tsi_stmt (iter), &stmts);
1382 tsi_delink (&iter);
1383 }
1384 tree dtor = build_cleanup (retval);
1385 if (!function_body)
1386 {
1387 /* Clear the sentinel so we don't try to destroy the retval again on
1388 rethrow (c++/112301). */
1389 tree clear = build2 (MODIFY_EXPR, boolean_type_node,
1390 current_retval_sentinel, boolean_false_node);
1391 dtor = build2 (COMPOUND_EXPR, void_type_node, clear, dtor);
1392 }
1393 tree cond = build3 (COND_EXPR, void_type_node, current_retval_sentinel,
1394 dtor, void_node);
1395 tree cleanup = build_stmt (loc, CLEANUP_STMT,
1396 stmts, cond, retval);
1397 CLEANUP_EH_ONLY (cleanup) = true;
1398 append_to_statement_list_force (cleanup, &compound_stmt);
1399 }
1400 }
1401
1402 #include "gt-cp-except.h"