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