Branch data Line data Source code
1 : : /* Handle exceptional things in C++.
2 : : Copyright (C) 1989-2025 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 : 94111 : init_terminate_fn (void)
50 : : {
51 : 94111 : if (terminate_fn)
52 : : return;
53 : :
54 : 93959 : tree tmp;
55 : :
56 : 93959 : push_nested_namespace (std_node);
57 : 93959 : tmp = build_function_type_list (void_type_node, NULL_TREE);
58 : 93959 : terminate_fn = build_cp_library_fn_ptr ("terminate", tmp,
59 : : ECF_NOTHROW | ECF_NORETURN
60 : : | ECF_COLD);
61 : 93959 : gcc_checking_assert (TREE_THIS_VOLATILE (terminate_fn)
62 : : && TREE_NOTHROW (terminate_fn));
63 : 93959 : 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 : 93958 : init_exception_processing (void)
72 : : {
73 : 93958 : tree tmp;
74 : :
75 : : /* void std::terminate (); */
76 : 93958 : init_terminate_fn ();
77 : :
78 : : /* void __cxa_call_unexpected(void *); */
79 : 93958 : tmp = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
80 : 93958 : call_unexpected_fn
81 : 93958 : = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
82 : 93958 : call_terminate_fn
83 : 93958 : = push_library_fn (get_identifier ("__cxa_call_terminate"), tmp, NULL_TREE,
84 : : ECF_NORETURN | ECF_COLD | ECF_NOTHROW);
85 : 93958 : }
86 : :
87 : : /* Returns an expression to be executed if an unhandled exception is
88 : : propagated out of a cleanup region. */
89 : :
90 : : tree
91 : 695075 : 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 : 695075 : return call_terminate_fn;
98 : : }
99 : :
100 : : static tree
101 : 120969 : prepare_eh_type (tree type)
102 : : {
103 : 120969 : if (type == NULL_TREE)
104 : : return type;
105 : 120969 : if (type == error_mark_node)
106 : : return error_mark_node;
107 : :
108 : : /* peel back references, so they match. */
109 : 120905 : type = non_reference (type);
110 : :
111 : : /* Peel off cv qualifiers. */
112 : 120905 : type = TYPE_MAIN_VARIANT (type);
113 : :
114 : : /* Functions and arrays decay to pointers. */
115 : 120905 : type = type_decays_to (type);
116 : :
117 : 120905 : return type;
118 : : }
119 : :
120 : : /* Return the type info for TYPE as used by EH machinery. */
121 : : tree
122 : 123851 : eh_type_info (tree type)
123 : : {
124 : 123851 : if (type == NULL_TREE || type == error_mark_node)
125 : : return type;
126 : :
127 : 123787 : 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 : 115136 : build_eh_type_type (tree type)
135 : : {
136 : 115136 : tree exp = eh_type_info (type);
137 : :
138 : 115136 : if (!exp)
139 : : return NULL;
140 : :
141 : 115136 : mark_used (exp);
142 : :
143 : 115136 : return convert (ptr_type_node, build_address (exp));
144 : : }
145 : :
146 : : tree
147 : 142608 : build_exc_ptr (void)
148 : : {
149 : 142608 : return build_call_n (builtin_decl_explicit (BUILT_IN_EH_POINTER),
150 : 142608 : 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 : 73473 : declare_library_fn_1 (const char *name, int ecf,
161 : : tree rtype, int nargs, tree args[])
162 : : {
163 : 73473 : tree ident = get_identifier (name);
164 : 73473 : tree except = ecf & ECF_NOTHROW ? empty_except_spec : NULL_TREE;
165 : :
166 : : /* Make a new decl. */
167 : 73473 : tree arg_list = void_list_node;
168 : 152375 : for (unsigned ix = nargs; ix--;)
169 : 78902 : arg_list = tree_cons (NULL_TREE, args[ix], arg_list);
170 : 73473 : tree fntype = build_function_type (rtype, arg_list);
171 : 73473 : tree res = push_library_fn (ident, fntype, except, ecf);
172 : :
173 : 73473 : 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 : 55454 : declare_library_fn (const char *name, tree rtype, tree ptype,
188 : : int ecf, int tm_ecf)
189 : : {
190 : 70369 : tree res = declare_library_fn_1 (name, ecf, rtype, ptype ? 1 : 0, &ptype);
191 : 55454 : if (res == error_mark_node)
192 : : return res;
193 : :
194 : 55412 : if (tm_ecf && flag_tm)
195 : : {
196 : 60 : char *tm_name = concat ("_ITM_", name + 2, NULL_TREE);
197 : :
198 : 60 : tree tm_fn = declare_library_fn_1 (tm_name, ecf | tm_ecf, rtype,
199 : : ptype ? 1 : 0, &ptype);
200 : 60 : free (tm_name);
201 : 60 : if (tm_fn != error_mark_node)
202 : 60 : 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 : 256 : do_get_exception_ptr (void)
213 : : {
214 : 256 : if (!get_exception_ptr_fn)
215 : : /* Declare void* __cxa_get_exception_ptr (void *) throw(). */
216 : 123 : get_exception_ptr_fn
217 : 123 : = 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 : 256 : return cp_build_function_call_nary (get_exception_ptr_fn,
223 : : tf_warning_or_error,
224 : 256 : 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 : 137783 : do_begin_catch (void)
232 : : {
233 : 137783 : if (!begin_catch_fn)
234 : : /* Declare void* __cxa_begin_catch (void *) throw(). */
235 : 14915 : begin_catch_fn
236 : 14915 : = declare_library_fn ("__cxa_begin_catch",
237 : : ptr_type_node, ptr_type_node, ECF_NOTHROW,
238 : : ECF_TM_PURE);
239 : :
240 : 137783 : return cp_build_function_call_nary (begin_catch_fn, tf_warning_or_error,
241 : 137783 : 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 : 137774 : dtor_nothrow (tree type)
249 : : {
250 : 137774 : if (type == NULL_TREE || type == error_mark_node)
251 : : return 0;
252 : :
253 : 8435 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
254 : : return 1;
255 : :
256 : 4855 : if (CLASSTYPE_LAZY_DESTRUCTOR (type))
257 : 189 : lazily_declare_fn (sfk_destructor, type);
258 : :
259 : 4855 : 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 : 137783 : do_end_catch (tree type)
267 : : {
268 : 137783 : if (!end_catch_fn)
269 : : /* Declare void __cxa_end_catch ().
270 : : This can throw if the destructor for the exception throws. */
271 : 14915 : end_catch_fn
272 : 14915 : = declare_library_fn ("__cxa_end_catch", void_type_node,
273 : : NULL_TREE, 0, ECF_TM_PURE);
274 : :
275 : 137783 : tree cleanup = cp_build_function_call_vec (end_catch_fn,
276 : : NULL, tf_warning_or_error);
277 : 137783 : if (cleanup != error_mark_node)
278 : 137774 : TREE_NOTHROW (cleanup) = dtor_nothrow (type);
279 : :
280 : 137783 : return cleanup;
281 : : }
282 : :
283 : : /* This routine creates the cleanup for the current exception. */
284 : :
285 : : static void
286 : 137783 : push_eh_cleanup (tree type)
287 : : {
288 : 137783 : finish_decl_cleanup (NULL_TREE, do_end_catch (type));
289 : 137783 : }
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 : 385 : build_must_not_throw_expr (tree body, tree cond)
297 : : {
298 : 385 : tree type = body ? TREE_TYPE (body) : void_type_node;
299 : :
300 : 385 : if (!flag_exceptions)
301 : : return body;
302 : :
303 : 382 : 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 : 307 : cond = NULL_TREE;
320 : : }
321 : :
322 : 352 : return build2 (MUST_NOT_THROW_EXPR, type, body, cond);
323 : : }
324 : :
325 : :
326 : : /* Initialize the catch parameter DECL. */
327 : :
328 : : static void
329 : 8426 : initialize_handler_parm (tree decl, tree exp)
330 : : {
331 : 8426 : tree init;
332 : 8426 : 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 : 8426 : TREE_USED (decl) = 1;
337 : 8426 : 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 : 8426 : init_type = TREE_TYPE (decl);
343 : 8426 : if (!INDIRECT_TYPE_P (init_type))
344 : 1626 : 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 : 8426 : if (TYPE_REF_P (init_type)
349 : 8426 : && TYPE_PTR_P (TREE_TYPE (init_type)))
350 : 51 : exp = cp_build_addr_expr (exp, tf_warning_or_error);
351 : :
352 : 8426 : exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0,
353 : : tf_warning_or_error);
354 : :
355 : 8426 : 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 : 8426 : 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 : 256 : 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 : 256 : init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
369 : 256 : init = build_must_not_throw_expr (init, NULL_TREE);
370 : 256 : if (init && TREE_CODE (init) == MUST_NOT_THROW_EXPR)
371 : 256 : MUST_NOT_THROW_CATCH_P (init) = 1;
372 : : }
373 : :
374 : 8426 : decl = pushdecl (decl);
375 : :
376 : 8426 : start_decl_1 (decl, true);
377 : 8426 : cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
378 : : LOOKUP_ONLYCONVERTING|DIRECT_BIND);
379 : 8426 : }
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 : 401835 : doing_eh (void)
391 : : {
392 : 401835 : 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 : 24 : 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 : 137789 : expand_start_catch_block (tree decl)
409 : : {
410 : 137789 : tree exp;
411 : 137789 : tree type, init;
412 : :
413 : 137789 : if (! doing_eh ())
414 : : return NULL_TREE;
415 : :
416 : 137783 : if (decl)
417 : : {
418 : 8508 : if (!is_admissible_throw_operand_or_catch_parameter (decl, false,
419 : : tf_warning_or_error))
420 : 64 : decl = error_mark_node;
421 : :
422 : 8508 : type = prepare_eh_type (TREE_TYPE (decl));
423 : 8508 : 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 : 137783 : push_eh_cleanup (type);
430 : :
431 : 137783 : 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 : 137783 : if (decl == NULL || decl == error_mark_node || init == error_mark_node)
436 : 129351 : 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 : 8432 : else if (flag_use_cxa_get_exception_ptr
442 : 8432 : && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
443 : : {
444 : 256 : exp = do_get_exception_ptr ();
445 : 256 : if (exp != error_mark_node)
446 : 250 : initialize_handler_parm (decl, exp);
447 : 256 : 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 : 8176 : tree init_type = type;
456 : :
457 : : /* Pointers are passed by values, everything else by reference. */
458 : 8176 : if (!TYPE_PTR_P (type))
459 : 6796 : init_type = build_pointer_type (type);
460 : 8176 : if (init_type != TREE_TYPE (init))
461 : 8128 : init = build1 (NOP_EXPR, init_type, init);
462 : 8176 : exp = create_temporary_var (init_type);
463 : 8176 : cp_finish_decl (exp, init, /*init_const_expr=*/false,
464 : : NULL_TREE, LOOKUP_ONLYCONVERTING);
465 : 8176 : DECL_REGISTER (exp) = 1;
466 : 8176 : 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 : 108 : in_nested_catch (void)
477 : : {
478 : 108 : int catches = 0;
479 : :
480 : : /* Scan through the template parameter scopes. */
481 : 108 : for (cp_binding_level *b = current_binding_level;
482 : 354 : b->kind != sk_function_parms;
483 : 246 : b = b->level_chain)
484 : 249 : if (b->kind == sk_catch
485 : 249 : && ++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 : 137789 : expand_end_catch_block (void)
496 : : {
497 : 137789 : 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 : 137783 : if (in_function_try_handler
503 : 648 : && (DECL_CONSTRUCTOR_P (current_function_decl)
504 : 220 : || DECL_DESTRUCTOR_P (current_function_decl))
505 : 137891 : && !in_nested_catch ())
506 : : {
507 : 105 : tree rethrow = build_throw (input_location, NULL_TREE,
508 : : tf_warning_or_error);
509 : : /* Disable all warnings for the generated rethrow statement. */
510 : 105 : suppress_warning (rethrow);
511 : 105 : finish_expr_stmt (rethrow);
512 : : }
513 : : }
514 : :
515 : : tree
516 : 18605763 : begin_eh_spec_block (void)
517 : : {
518 : 18605763 : tree r;
519 : 18605763 : 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 : 18605763 : if (TYPE_NOEXCEPT_P (TREE_TYPE (current_function_decl)))
524 : : {
525 : 18583833 : r = build_stmt (spec_location, MUST_NOT_THROW_EXPR,
526 : : NULL_TREE, NULL_TREE);
527 : 18583833 : TREE_SIDE_EFFECTS (r) = 1;
528 : 18583833 : MUST_NOT_THROW_NOEXCEPT_P (r) = 1;
529 : : }
530 : : else
531 : 21930 : r = build_stmt (spec_location, EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
532 : 18605763 : add_stmt (r);
533 : 18605763 : TREE_OPERAND (r, 0) = push_stmt_list ();
534 : 18605763 : return r;
535 : : }
536 : :
537 : : void
538 : 18605760 : finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
539 : : {
540 : 18605760 : tree raises;
541 : :
542 : 18605760 : TREE_OPERAND (eh_spec_block, 0)
543 : 18605760 : = pop_stmt_list (TREE_OPERAND (eh_spec_block, 0));
544 : :
545 : 18605760 : 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 : 44092 : 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 : 21930 : 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 : 112301 : do_allocate_exception (tree type)
567 : : {
568 : 112301 : if (!allocate_exception_fn)
569 : : /* Declare void *__cxa_allocate_exception(size_t) throw(). */
570 : 12755 : allocate_exception_fn
571 : 12755 : = 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 : 112301 : return cp_build_function_call_nary (allocate_exception_fn,
576 : : tf_warning_or_error,
577 : 112301 : 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 : 112283 : do_free_exception (tree ptr)
585 : : {
586 : 112283 : if (!free_exception_fn)
587 : : /* Declare void __cxa_free_exception (void *) throw(). */
588 : 12746 : free_exception_fn
589 : 12746 : = declare_library_fn ("__cxa_free_exception",
590 : : void_type_node, ptr_type_node,
591 : : ECF_NOTHROW | ECF_LEAF, ECF_TM_PURE);
592 : :
593 : 112283 : return cp_build_function_call_nary (free_exception_fn,
594 : 112283 : 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 : 2607306 : wrap_cleanups_r (tree *tp, int *walk_subtrees, void * /*data*/)
602 : : {
603 : 2607306 : tree exp = *tp;
604 : 2607306 : tree cleanup;
605 : :
606 : : /* Don't walk into types. */
607 : 2607306 : if (TYPE_P (exp))
608 : : {
609 : 7 : *walk_subtrees = 0;
610 : 7 : return NULL_TREE;
611 : : }
612 : 2607299 : if (TREE_CODE (exp) != TARGET_EXPR)
613 : : return NULL_TREE;
614 : :
615 : 146584 : cleanup = TARGET_EXPR_CLEANUP (exp);
616 : 146584 : if (cleanup)
617 : : {
618 : 131214 : cleanup = build2 (MUST_NOT_THROW_EXPR, void_type_node, cleanup,
619 : : NULL_TREE);
620 : 131214 : MUST_NOT_THROW_THROW_P (cleanup) = 1;
621 : 131214 : 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 : 1068510 : build_throw (location_t loc, tree exp, tsubst_flags_t complain)
632 : : {
633 : 1068510 : if (exp == error_mark_node)
634 : : return exp;
635 : :
636 : 1068486 : if (processing_template_decl)
637 : : {
638 : 942205 : if (cfun)
639 : 942175 : current_function_returns_abnormally = 1;
640 : 942205 : exp = build_min (THROW_EXPR, void_type_node, exp);
641 : 942205 : SET_EXPR_LOCATION (exp, loc);
642 : 942205 : return exp;
643 : : }
644 : :
645 : 126281 : 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 : 126281 : if (exp && !is_admissible_throw_operand_or_catch_parameter (exp,
650 : : /*is_throw=*/true,
651 : : complain))
652 : 24 : return error_mark_node;
653 : :
654 : 126257 : if (! doing_eh ())
655 : 12 : return error_mark_node;
656 : :
657 : 126245 : if (exp)
658 : : {
659 : 112301 : tree throw_type;
660 : 112301 : tree temp_type;
661 : 112301 : tree cleanup;
662 : 112301 : tree object, ptr;
663 : 112301 : tree allocate_expr;
664 : :
665 : : /* The CLEANUP_TYPE is the internal type of a destructor. */
666 : 112301 : if (!cleanup_type)
667 : 12664 : cleanup_type = get_cxa_atexit_fn_ptr_type ();
668 : :
669 : 112301 : if (!throw_fn)
670 : : {
671 : 12755 : tree args[3] = {ptr_type_node, ptr_type_node, cleanup_type};
672 : :
673 : 12755 : throw_fn = declare_library_fn_1 ("__cxa_throw",
674 : : ECF_NORETURN | ECF_XTHROW | ECF_COLD,
675 : : void_type_node, 3, args);
676 : 12755 : if (flag_tm && throw_fn != error_mark_node)
677 : : {
678 : 17 : 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 : 17 : if (itm_fn != error_mark_node)
683 : : {
684 : 17 : apply_tm_attr (itm_fn, get_identifier ("transaction_pure"));
685 : 17 : 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 : 112301 : temp_type = is_bitfield_expr_with_lowered_type (exp);
699 : 112301 : if (!temp_type)
700 : 112301 : 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 : 112301 : allocate_expr = do_allocate_exception (temp_type);
717 : 112301 : 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 : 112283 : tree ptr_copy = get_internal_target_expr (null_pointer_node);
724 : 112283 : allocate_expr = get_internal_target_expr (allocate_expr);
725 : 112283 : ptr = TARGET_EXPR_SLOT (allocate_expr);
726 : 112283 : TARGET_EXPR_CLEANUP (allocate_expr) = do_free_exception (ptr);
727 : 112283 : CLEANUP_EH_ONLY (allocate_expr) = 1;
728 : :
729 : 112283 : object = build_nop (build_pointer_type (temp_type), ptr);
730 : 112283 : object = cp_build_fold_indirect_ref (object);
731 : :
732 : : /* And initialize the exception object. */
733 : 112283 : if (CLASS_TYPE_P (temp_type))
734 : : {
735 : 109339 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
736 : 109339 : 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 : 109339 : 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 : 98 : exp = moved;
746 : :
747 : : /* Call the copy constructor. */
748 : 109339 : auto_diagnostic_group d;
749 : 109339 : releasing_vec exp_vec (make_tree_vector_single (exp));
750 : 109339 : exp = build_special_member_call (object, complete_ctor_identifier,
751 : 109339 : &exp_vec, TREE_TYPE (object), flags,
752 : : complain);
753 : 109339 : if (exp == error_mark_node)
754 : : {
755 : 26 : if (complain & tf_error)
756 : 24 : inform (exp_loc, " in thrown expression");
757 : 26 : return error_mark_node;
758 : : }
759 : 109339 : }
760 : : else
761 : : {
762 : 2944 : tree tmp = decay_conversion (exp, complain);
763 : 2944 : if (tmp == error_mark_node)
764 : : return error_mark_node;
765 : 2941 : 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 : 112254 : cp_walk_tree_without_duplicates (&exp, wrap_cleanups_r, 0);
771 : :
772 : : /* Prepend the allocation. */
773 : 112254 : exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
774 : :
775 : 112254 : exp = build2 (COMPOUND_EXPR, void_type_node, exp,
776 : : build2 (MODIFY_EXPR, void_type_node,
777 : 112254 : TARGET_EXPR_SLOT (ptr_copy), ptr));
778 : 112254 : 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 : 112254 : exp = build1 (CLEANUP_POINT_EXPR, void_type_node, exp);
783 : :
784 : 112254 : exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), ptr_copy, exp);
785 : :
786 : 112254 : throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
787 : :
788 : 112254 : cleanup = NULL_TREE;
789 : 112254 : if (type_build_dtor_call (TREE_TYPE (object)))
790 : : {
791 : 106736 : tree binfo = TYPE_BINFO (TREE_TYPE (object));
792 : 106736 : tree dtor_fn = lookup_fnfields (binfo,
793 : : complete_dtor_identifier, 0,
794 : : complain);
795 : 106736 : dtor_fn = BASELINK_FUNCTIONS (dtor_fn);
796 : 106736 : if (!mark_used (dtor_fn)
797 : 106736 : || !perform_or_defer_access_check (binfo, dtor_fn,
798 : : dtor_fn, complain))
799 : 0 : return error_mark_node;
800 : 106736 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
801 : : {
802 : 106736 : cxx_mark_addressable (dtor_fn);
803 : : /* Pretend it's a normal function. */
804 : 106736 : cleanup = build1 (ADDR_EXPR, cleanup_type, dtor_fn);
805 : : }
806 : : }
807 : 106736 : if (cleanup == NULL_TREE)
808 : 5518 : cleanup = build_int_cst (cleanup_type, 0);
809 : :
810 : : /* ??? Indicate that this function call throws throw_type. */
811 : 112254 : 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 : 112254 : exp = build2 (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp);
817 : : }
818 : : else
819 : : {
820 : : /* Rethrow current exception. */
821 : 13944 : if (!rethrow_fn)
822 : : {
823 : 5187 : rethrow_fn = declare_library_fn_1 ("__cxa_rethrow",
824 : : ECF_NORETURN | ECF_XTHROW
825 : : | ECF_COLD,
826 : : void_type_node, 0, NULL);
827 : 5187 : 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 : 13944 : exp = cp_build_function_call_vec (rethrow_fn, NULL, complain);
834 : : }
835 : :
836 : 126198 : exp = build1_loc (loc, THROW_EXPR, void_type_node, exp);
837 : :
838 : 126198 : 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 : 120842 : complete_ptr_ref_or_void_ptr_p (tree type, tree from, tsubst_flags_t complain)
848 : : {
849 : : /* Check complete. */
850 : 120842 : type = complete_type_or_maybe_complain (type, from, complain);
851 : 120842 : if (!type)
852 : : return false;
853 : :
854 : : /* Or a pointer or ref to one, or cv void *. */
855 : 120781 : const bool is_ptr = TYPE_PTR_P (type);
856 : 120781 : if (is_ptr || TYPE_REF_P (type))
857 : : {
858 : 7122 : tree core = TREE_TYPE (type);
859 : :
860 : 7122 : if (is_ptr && VOID_TYPE_P (core))
861 : : /* OK */;
862 : 7063 : else if (!complete_type_or_maybe_complain (core, from, complain))
863 : : 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 : 120842 : is_admissible_throw_operand_or_catch_parameter (tree t, bool is_throw,
876 : : tsubst_flags_t complain)
877 : : {
878 : 120842 : tree expr = is_throw ? t : NULL_TREE;
879 : 120842 : 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 : 120842 : if (!complete_ptr_ref_or_void_ptr_p (type, expr, complain))
893 : : return false;
894 : :
895 : 120772 : tree nonref_type = non_reference (type);
896 : 120772 : 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 : 129225 : else if (abstract_virtuals_error (is_throw ? ACU_THROW : ACU_CATCH, type,
903 : : complain))
904 : : return false;
905 : 120763 : else if (!is_throw
906 : 8450 : && TYPE_REF_P (type)
907 : 126242 : && 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 : 3 : return false;
913 : : }
914 : 120760 : 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 : 6 : 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 : 107141024 : nothrow_libfn_p (const_tree fn)
944 : : {
945 : 107141024 : tree id;
946 : :
947 : 107141024 : if (TREE_PUBLIC (fn)
948 : 105951153 : && DECL_EXTERNAL (fn)
949 : 105951153 : && DECL_NAMESPACE_SCOPE_P (fn)
950 : 144626003 : && 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 : 2281744 : id = DECL_NAME (fn);
960 : 2281744 : const struct libc_name_struct *s
961 : 2281744 : = libc_name::libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
962 : 2281744 : if (s == NULL)
963 : : return 0;
964 : 27278 : switch (s->c_ver)
965 : : {
966 : : case 89: return 1;
967 : 398 : case 99: return !flag_iso || flag_isoc99;
968 : 26 : case 11: return !flag_iso || flag_isoc11;
969 : 0 : default: gcc_unreachable ();
970 : : }
971 : : }
972 : :
973 : : /* Returns nonzero if an exception of type FROM will be caught by a
974 : : handler for type TO, as per [except.handle]. */
975 : :
976 : : static bool
977 : 5077 : can_convert_eh (tree to, tree from)
978 : : {
979 : 5077 : to = non_reference (to);
980 : 5077 : from = non_reference (from);
981 : :
982 : 5077 : if (same_type_ignoring_top_level_qualifiers_p (to, from))
983 : : return true;
984 : :
985 : 5053 : if (TYPE_PTR_P (to) && TYPE_PTR_P (from))
986 : : {
987 : 527 : to = TREE_TYPE (to);
988 : 527 : from = TREE_TYPE (from);
989 : :
990 : 527 : if (! at_least_as_qualified_p (to, from))
991 : : return false;
992 : :
993 : 460 : if (VOID_TYPE_P (to))
994 : : return true;
995 : :
996 : : /* Else fall through. */
997 : : }
998 : :
999 : 4200 : if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
1000 : 8708 : && publicly_uniquely_derived_p (to, from))
1001 : : return true;
1002 : :
1003 : : return false;
1004 : : }
1005 : :
1006 : : /* Check whether any of the handlers in I are shadowed by another handler
1007 : : accepting TYPE. Note that the shadowing may not be complete; even if
1008 : : an exception of type B would be caught by a handler for A, there could
1009 : : be a derived class C for which A is an ambiguous base but B is not, so
1010 : : the handler for B would catch an exception of type C. */
1011 : :
1012 : : static void
1013 : 463867 : check_handlers_1 (tree master, tree_stmt_iterator i)
1014 : : {
1015 : 463867 : tree type = TREE_TYPE (master);
1016 : :
1017 : 931755 : for (; !tsi_end_p (i); tsi_next (&i))
1018 : : {
1019 : 468149 : tree handler = tsi_stmt (i);
1020 : 468149 : if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
1021 : : {
1022 : 261 : auto_diagnostic_group d;
1023 : 261 : if (warning_at (EXPR_LOCATION (handler), OPT_Wexceptions,
1024 : : "exception of type %qT will be caught by earlier "
1025 : 261 : "handler", TREE_TYPE (handler)))
1026 : 36 : inform (EXPR_LOCATION (master), "for type %qT", type);
1027 : 261 : break;
1028 : 261 : }
1029 : : }
1030 : 463867 : }
1031 : :
1032 : : /* Given a STATEMENT_LIST of HANDLERs, make sure that they're OK. */
1033 : :
1034 : : void
1035 : 1130842 : check_handlers (tree handlers)
1036 : : {
1037 : 1130842 : tree_stmt_iterator i;
1038 : :
1039 : : /* If we don't have a STATEMENT_LIST, then we've just got one
1040 : : handler, and thus nothing to warn about. */
1041 : 1130842 : if (TREE_CODE (handlers) != STATEMENT_LIST)
1042 : 668066 : return;
1043 : :
1044 : 462776 : i = tsi_start (handlers);
1045 : 462776 : if (!tsi_end_p (i))
1046 : 926649 : while (1)
1047 : : {
1048 : 926649 : tree handler = tsi_stmt (i);
1049 : 926649 : tsi_next (&i);
1050 : :
1051 : : /* No more handlers; nothing to shadow. */
1052 : 926649 : if (tsi_end_p (i))
1053 : : break;
1054 : 463873 : if (TREE_TYPE (handler) == NULL_TREE)
1055 : 6 : permerror (EXPR_LOCATION (handler), "%<...%>"
1056 : : " handler must be the last handler for its try block");
1057 : : else
1058 : 463867 : check_handlers_1 (handler, i);
1059 : : }
1060 : : }
1061 : :
1062 : : /* walk_tree helper for finish_noexcept_expr. Returns non-null if the
1063 : : expression *TP causes the noexcept operator to evaluate to false.
1064 : :
1065 : : 5.3.7 [expr.noexcept]: The result of the noexcept operator is false if
1066 : : in a potentially-evaluated context the expression would contain
1067 : : * a potentially evaluated call to a function, member function,
1068 : : function pointer, or member function pointer that does not have a
1069 : : non-throwing exception-specification (15.4),
1070 : : * a potentially evaluated throw-expression (15.1),
1071 : : * a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1072 : : where T is a reference type, that requires a run-time check (5.2.7), or
1073 : : * a potentially evaluated typeid expression (5.2.8) applied to a glvalue
1074 : : expression whose type is a polymorphic class type (10.3). */
1075 : :
1076 : : static tree
1077 : 38175792 : check_noexcept_r (tree *tp, int *walk_subtrees, void *)
1078 : : {
1079 : 38175792 : tree t = *tp;
1080 : 38175792 : enum tree_code code = TREE_CODE (t);
1081 : :
1082 : 38175792 : if (unevaluated_p (code))
1083 : 9 : *walk_subtrees = false;
1084 : 4862792 : else if ((code == CALL_EXPR && CALL_EXPR_FN (t))
1085 : 38175783 : || code == AGGR_INIT_EXPR)
1086 : : {
1087 : : /* We can only use the exception specification of the called function
1088 : : for determining the value of a noexcept expression; we can't use
1089 : : TREE_NOTHROW, as it might have a different value in another
1090 : : translation unit, creating ODR problems.
1091 : :
1092 : : We could use TREE_NOTHROW (t) for !TREE_PUBLIC fns, though... */
1093 : 5022174 : tree fn = cp_get_callee (t);
1094 : 5022174 : tree type = TREE_TYPE (fn);
1095 : 5022174 : gcc_assert (INDIRECT_TYPE_P (type));
1096 : 5022174 : type = TREE_TYPE (type);
1097 : :
1098 : 5022174 : STRIP_NOPS (fn);
1099 : 5022174 : if (TREE_CODE (fn) == ADDR_EXPR)
1100 : 4992653 : fn = TREE_OPERAND (fn, 0);
1101 : 5022174 : if (TREE_CODE (fn) == FUNCTION_DECL)
1102 : : {
1103 : : /* We do use TREE_NOTHROW for ABI internals like __dynamic_cast,
1104 : : and for C library functions known not to throw. */
1105 : 4992653 : if (DECL_EXTERN_C_P (fn)
1106 : 4992653 : && (DECL_ARTIFICIAL (fn)
1107 : 0 : || nothrow_libfn_p (fn)))
1108 : 102 : return TREE_NOTHROW (fn) ? NULL_TREE : fn;
1109 : : /* We used to treat a call to a constexpr function as noexcept if
1110 : : the call was a constant expression (CWG 1129). This has changed
1111 : : in P0003 whereby noexcept has no special rule for constant
1112 : : expressions anymore. Since the current behavior is important for
1113 : : certain library functionality, we treat this as a DR, therefore
1114 : : adjusting the behavior for C++11 and C++14. Previously, we had
1115 : : to evaluate the noexcept-specifier's operand here, but that could
1116 : : cause instantiations that would fail. */
1117 : : }
1118 : 5022109 : if (!TYPE_NOTHROW_P (type))
1119 : : return fn;
1120 : : }
1121 : :
1122 : : return NULL_TREE;
1123 : : }
1124 : :
1125 : : /* If a function that causes a noexcept-expression to be false isn't
1126 : : defined yet, remember it and check it for TREE_NOTHROW again at EOF. */
1127 : :
1128 : : struct GTY(()) pending_noexcept {
1129 : : tree fn;
1130 : : location_t loc;
1131 : : };
1132 : : static GTY(()) vec<pending_noexcept, va_gc> *pending_noexcept_checks;
1133 : :
1134 : : /* FN is a FUNCTION_DECL that caused a noexcept-expr to be false. Warn if
1135 : : it can't throw.
1136 : :
1137 : : TODO: Consider extending -Wnoexcept to do something like walk_subtrees in the
1138 : : case of a defaulted function that obtained a noexcept(false) spec. */
1139 : :
1140 : : static void
1141 : 15 : maybe_noexcept_warning (tree fn)
1142 : : {
1143 : 15 : if (TREE_NOTHROW (fn)
1144 : 15 : && (!DECL_IN_SYSTEM_HEADER (fn)
1145 : 3 : || global_dc->m_warn_system_headers))
1146 : : {
1147 : 9 : auto s = make_temp_override (global_dc->m_warn_system_headers, true);
1148 : 9 : auto_diagnostic_group d;
1149 : 9 : if (warning (OPT_Wnoexcept, "noexcept-expression evaluates to %<false%> "
1150 : : "because of a call to %qD", fn))
1151 : 9 : inform (DECL_SOURCE_LOCATION (fn),
1152 : : "but %qD does not throw; perhaps "
1153 : : "it should be declared %<noexcept%>", fn);
1154 : 9 : }
1155 : 15 : }
1156 : :
1157 : : /* Check any functions that weren't defined earlier when they caused a
1158 : : noexcept expression to evaluate to false. */
1159 : :
1160 : : void
1161 : 93654 : perform_deferred_noexcept_checks (void)
1162 : : {
1163 : 93654 : int i;
1164 : 93654 : pending_noexcept *p;
1165 : 93654 : location_t saved_loc = input_location;
1166 : 93660 : FOR_EACH_VEC_SAFE_ELT (pending_noexcept_checks, i, p)
1167 : : {
1168 : 6 : input_location = p->loc;
1169 : 6 : maybe_noexcept_warning (p->fn);
1170 : : }
1171 : 93654 : input_location = saved_loc;
1172 : 93654 : }
1173 : :
1174 : : /* Evaluate noexcept ( EXPR ). */
1175 : :
1176 : : tree
1177 : 2159653 : finish_noexcept_expr (tree expr, tsubst_flags_t complain)
1178 : : {
1179 : 2159653 : if (expr == error_mark_node)
1180 : : return error_mark_node;
1181 : :
1182 : 2159516 : if (processing_template_decl)
1183 : 1712209 : return build_min (NOEXCEPT_EXPR, boolean_type_node, expr);
1184 : :
1185 : 447307 : return (expr_noexcept_p (expr, complain)
1186 : 447307 : ? boolean_true_node : boolean_false_node);
1187 : : }
1188 : :
1189 : : /* Returns whether EXPR is noexcept, possibly warning if allowed by
1190 : : COMPLAIN. */
1191 : :
1192 : : bool
1193 : 5163673 : expr_noexcept_p (tree expr, tsubst_flags_t complain)
1194 : : {
1195 : 5163673 : tree fn;
1196 : :
1197 : 5163673 : if (expr == error_mark_node)
1198 : : return false;
1199 : :
1200 : 5163589 : fn = cp_walk_tree_without_duplicates (&expr, check_noexcept_r, 0);
1201 : 5163589 : if (fn)
1202 : : {
1203 : 72764 : if ((complain & tf_warning) && warn_noexcept
1204 : 15 : && TREE_CODE (fn) == FUNCTION_DECL)
1205 : : {
1206 : 15 : if (!DECL_INITIAL (fn))
1207 : : {
1208 : : /* Not defined yet; check again at EOF. */
1209 : 6 : pending_noexcept p = {fn, input_location};
1210 : 6 : vec_safe_push (pending_noexcept_checks, p);
1211 : : }
1212 : : else
1213 : 9 : maybe_noexcept_warning (fn);
1214 : : }
1215 : 72764 : return false;
1216 : : }
1217 : : else
1218 : : return true;
1219 : : }
1220 : :
1221 : : /* Return true iff SPEC is throw() or noexcept(true). */
1222 : :
1223 : : bool
1224 : 427454597 : nothrow_spec_p (const_tree spec)
1225 : : {
1226 : 687803656 : gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
1227 : :
1228 : 427454597 : if (spec == empty_except_spec
1229 : 426307840 : || spec == noexcept_true_spec)
1230 : : return true;
1231 : :
1232 : 176293422 : gcc_assert (!spec
1233 : : || TREE_VALUE (spec)
1234 : : || spec == noexcept_false_spec
1235 : : || TREE_PURPOSE (spec) == error_mark_node
1236 : : || UNPARSED_NOEXCEPT_SPEC_P (spec)
1237 : : || processing_template_decl);
1238 : :
1239 : : return false;
1240 : : }
1241 : :
1242 : : /* For FUNCTION_TYPE or METHOD_TYPE, true if NODE is noexcept. This is the
1243 : : case for things declared noexcept(true) and, with -fnothrow-opt, for
1244 : : throw() functions. */
1245 : :
1246 : : bool
1247 : 47683983 : type_noexcept_p (const_tree type)
1248 : : {
1249 : 47683983 : tree spec = TYPE_RAISES_EXCEPTIONS (type);
1250 : 84067462 : gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
1251 : 47683983 : if (flag_nothrow_opt)
1252 : 159 : return nothrow_spec_p (spec);
1253 : : else
1254 : 47683824 : return spec == noexcept_true_spec;
1255 : : }
1256 : :
1257 : : /* For FUNCTION_TYPE or METHOD_TYPE, true if NODE can throw any type,
1258 : : i.e. no exception-specification or noexcept(false). */
1259 : :
1260 : : bool
1261 : 85810255 : type_throw_all_p (const_tree type)
1262 : : {
1263 : 85810255 : tree spec = TYPE_RAISES_EXCEPTIONS (type);
1264 : 125194757 : gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
1265 : 39384502 : return spec == NULL_TREE || spec == noexcept_false_spec;
1266 : : }
1267 : :
1268 : : /* Create a representation of the noexcept-specification with
1269 : : constant-expression of EXPR. COMPLAIN is as for tsubst. */
1270 : :
1271 : : tree
1272 : 114270562 : build_noexcept_spec (tree expr, tsubst_flags_t complain)
1273 : : {
1274 : 114270562 : if (check_for_bare_parameter_packs (expr))
1275 : 3 : return error_mark_node;
1276 : 114270559 : if (TREE_CODE (expr) != DEFERRED_NOEXCEPT
1277 : 114270559 : && !instantiation_dependent_expression_p (expr))
1278 : : {
1279 : 105260934 : expr = build_converted_constant_bool_expr (expr, complain);
1280 : 105260934 : expr = instantiate_non_dependent_expr (expr, complain);
1281 : 105260934 : expr = cxx_constant_value (expr, complain);
1282 : : }
1283 : 114270559 : if (TREE_CODE (expr) == INTEGER_CST)
1284 : : {
1285 : 105260892 : if (operand_equal_p (expr, boolean_true_node, 0))
1286 : 105037590 : return noexcept_true_spec;
1287 : : else
1288 : : {
1289 : 223302 : gcc_checking_assert (operand_equal_p (expr, boolean_false_node, 0));
1290 : 223302 : return noexcept_false_spec;
1291 : : }
1292 : : }
1293 : 9009667 : else if (expr == error_mark_node)
1294 : : return error_mark_node;
1295 : : else
1296 : : {
1297 : 9009625 : gcc_assert (processing_template_decl
1298 : : || TREE_CODE (expr) == DEFERRED_NOEXCEPT);
1299 : 9009625 : if (TREE_CODE (expr) != DEFERRED_NOEXCEPT)
1300 : : /* Avoid problems with a function type built with a dependent typedef
1301 : : being reused in another scope (c++/84045). */
1302 : 3608953 : expr = strip_typedefs_expr (expr);
1303 : 9009625 : return build_tree_list (expr, NULL_TREE);
1304 : : }
1305 : : }
1306 : :
1307 : : /* If the current function has a cleanup that might throw, and the return value
1308 : : has a non-trivial destructor, return a MODIFY_EXPR to set
1309 : : current_retval_sentinel so that we know that the return value needs to be
1310 : : destroyed on throw. Do the same if the current function might use the
1311 : : named return value optimization, so we don't destroy it on return.
1312 : : Otherwise, returns NULL_TREE.
1313 : :
1314 : : The sentinel is set to indicate that we're in the process of returning, and
1315 : : therefore should destroy a normal return value on throw, and shouldn't
1316 : : destroy a named return value variable on normal scope exit. It is set on
1317 : : return, and cleared either by maybe_splice_retval_cleanup, or when an
1318 : : exception reaches the NRV scope (finalize_nrv_r). Note that once return
1319 : : passes the NRV scope, it's effectively a normal return value, so cleanup
1320 : : past that point is handled by maybe_splice_retval_cleanup. */
1321 : :
1322 : : tree
1323 : 35996067 : maybe_set_retval_sentinel ()
1324 : : {
1325 : 35996067 : if (processing_template_decl)
1326 : : return NULL_TREE;
1327 : 35996067 : tree retval = DECL_RESULT (current_function_decl);
1328 : 35996067 : if (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (retval)))
1329 : : return NULL_TREE;
1330 : 1132325 : if (!cp_function_chain->throwing_cleanup
1331 : 1131820 : && (current_function_return_value == error_mark_node
1332 : 136196 : || current_function_return_value == NULL_TREE))
1333 : : return NULL_TREE;
1334 : :
1335 : 136701 : if (!current_retval_sentinel)
1336 : : {
1337 : : /* Just create the temporary now, maybe_splice_retval_cleanup
1338 : : will do the rest. */
1339 : 126261 : current_retval_sentinel = create_temporary_var (boolean_type_node);
1340 : 126261 : DECL_INITIAL (current_retval_sentinel) = boolean_false_node;
1341 : 126261 : pushdecl_outermost_localscope (current_retval_sentinel);
1342 : : }
1343 : :
1344 : 136701 : return build2 (MODIFY_EXPR, boolean_type_node,
1345 : 136701 : current_retval_sentinel, boolean_true_node);
1346 : : }
1347 : :
1348 : : /* COMPOUND_STMT is the STATEMENT_LIST for some block. If COMPOUND_STMT is the
1349 : : current function body or a try block, and current_retval_sentinel was set in
1350 : : this function, wrap the block in a CLEANUP_STMT to destroy the return value
1351 : : on throw. */
1352 : :
1353 : : void
1354 : 305320892 : maybe_splice_retval_cleanup (tree compound_stmt, bool is_try)
1355 : : {
1356 : 305319676 : if (!current_function_decl || !cfun
1357 : 610639034 : || DECL_CONSTRUCTOR_P (current_function_decl)
1358 : 265671184 : || DECL_DESTRUCTOR_P (current_function_decl)
1359 : 562001701 : || !current_retval_sentinel)
1360 : : return;
1361 : :
1362 : : /* if we need a cleanup for the return value, add it in at the same level as
1363 : : pushdecl_outermost_localscope. And also in try blocks. */
1364 : 194973 : cp_binding_level *b = current_binding_level;
1365 : 194973 : const bool function_body = b->kind == sk_function_parms;
1366 : :
1367 : 194973 : if (function_body || is_try)
1368 : : {
1369 : 126560 : location_t loc = DECL_SOURCE_LOCATION (current_function_decl);
1370 : 126560 : tree_stmt_iterator iter = tsi_start (compound_stmt);
1371 : 126560 : tree retval = DECL_RESULT (current_function_decl);
1372 : :
1373 : 126560 : if (function_body)
1374 : : {
1375 : : /* Add a DECL_EXPR for current_retval_sentinel. */
1376 : 126261 : tree decl_expr = build_stmt (loc, DECL_EXPR, current_retval_sentinel);
1377 : 126261 : tsi_link_before (&iter, decl_expr, TSI_SAME_STMT);
1378 : : }
1379 : :
1380 : 126560 : if (!cp_function_chain->throwing_cleanup)
1381 : : /* We're only using the sentinel for an NRV. */
1382 : 126067 : return;
1383 : :
1384 : : /* Skip past other decls, they can't contain a return. */
1385 : 690 : while (!tsi_end_p (iter)
1386 : 690 : && TREE_CODE (tsi_stmt (iter)) == DECL_EXPR)
1387 : 194 : tsi_next (&iter);
1388 : :
1389 : 496 : if (tsi_end_p (iter))
1390 : : /* Nothing to wrap. */
1391 : : return;
1392 : :
1393 : : /* Wrap the rest of the STATEMENT_LIST in a CLEANUP_STMT. */
1394 : 493 : tree stmts = NULL_TREE;
1395 : 1432 : while (!tsi_end_p (iter))
1396 : : {
1397 : 939 : append_to_statement_list_force (tsi_stmt (iter), &stmts);
1398 : 939 : tsi_delink (&iter);
1399 : : }
1400 : 493 : tree dtor = build_cleanup (retval);
1401 : 493 : if (!function_body)
1402 : : {
1403 : : /* Clear the sentinel so we don't try to destroy the retval again on
1404 : : rethrow (c++/112301). */
1405 : 68 : tree clear = build2 (MODIFY_EXPR, boolean_type_node,
1406 : 34 : current_retval_sentinel, boolean_false_node);
1407 : 34 : dtor = build2 (COMPOUND_EXPR, void_type_node, clear, dtor);
1408 : : }
1409 : 493 : tree cond = build3 (COND_EXPR, void_type_node, current_retval_sentinel,
1410 : : dtor, void_node);
1411 : 493 : tree cleanup = build_stmt (loc, CLEANUP_STMT,
1412 : : stmts, cond, retval);
1413 : 493 : CLEANUP_EH_ONLY (cleanup) = true;
1414 : 493 : append_to_statement_list_force (cleanup, &compound_stmt);
1415 : : }
1416 : : }
1417 : :
1418 : : #include "gt-cp-except.h"
|