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