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