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