Debian Codegraph @

gcc/cp/cp-gimplify.cc

raw
1 /* C++-specific tree lowering bits; see also c-gimplify.cc and gimple.cc.
2
3 Copyright (C) 2002-2025 Free Software Foundation, Inc.
4 Contributed by Jason Merrill <[email protected]>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "target.h"
26 #include "basic-block.h"
27 #include "cp-tree.h"
28 #include "gimple.h"
29 #include "predict.h"
30 #include "stor-layout.h"
31 #include "tree-iterator.h"
32 #include "gimplify.h"
33 #include "c-family/c-ubsan.h"
34 #include "stringpool.h"
35 #include "attribs.h"
36 #include "asan.h"
37 #include "gcc-rich-location.h"
38 #include "memmodel.h"
39 #include "tm_p.h"
40 #include "output.h"
41 #include "file-prefix-map.h"
42 #include "cgraph.h"
43 #include "omp-general.h"
44 #include "opts.h"
45 #include "gcc-urlifier.h"
46
47 /* Keep track of forward references to immediate-escalating functions in
48 case they become consteval. This vector contains ADDR_EXPRs and
49 PTRMEM_CSTs; it also stores FUNCTION_DECLs that had an escalating
50 function call in them, to check that they can be evaluated to a constant,
51 and immediate-escalating functions that may become consteval. */
52 static GTY(()) hash_set<tree> *deferred_escalating_exprs;
53
54 static void
55 remember_escalating_expr (tree t)
56 {
57 if (uses_template_parms (t))
58 /* Templates don't escalate, and cp_fold_immediate can get confused by
59 other template trees in the function body (c++/115986). */
60 return;
61 if (!deferred_escalating_exprs)
62 deferred_escalating_exprs = hash_set<tree>::create_ggc (37);
63 deferred_escalating_exprs->add (t);
64 }
65
66 /* Flags for cp_fold and cp_fold_r. */
67
68 enum fold_flags {
69 ff_none = 0,
70 /* Whether we're being called from cp_fold_function. */
71 ff_genericize = 1 << 0,
72 /* Whether we're folding a point where we know we're
73 definitely not in a manifestly constant-evaluated
74 context. */
75 ff_mce_false = 1 << 1,
76 };
77
78 using fold_flags_t = int;
79
80 struct cp_fold_data
81 {
82 hash_set<tree> pset;
83 fold_flags_t flags;
84 cp_fold_data (fold_flags_t flags): flags (flags) {}
85 };
86
87 /* Forward declarations. */
88
89 static tree cp_genericize_r (tree *, int *, void *);
90 static tree cp_fold_r (tree *, int *, void *);
91 static void cp_genericize_tree (tree*, bool);
92 static tree cp_fold (tree, fold_flags_t);
93 static tree cp_fold_immediate_r (tree *, int *, void *);
94
95 /* Genericize a TRY_BLOCK. */
96
97 static void
98 genericize_try_block (tree *stmt_p)
99 {
100 tree body = TRY_STMTS (*stmt_p);
101 tree cleanup = TRY_HANDLERS (*stmt_p);
102
103 *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
104 }
105
106 /* Genericize a HANDLER by converting to a CATCH_EXPR. */
107
108 static void
109 genericize_catch_block (tree *stmt_p)
110 {
111 tree type = HANDLER_TYPE (*stmt_p);
112 tree body = HANDLER_BODY (*stmt_p);
113
114 /* FIXME should the caught type go in TREE_TYPE? */
115 *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
116 }
117
118 /* A terser interface for building a representation of an exception
119 specification. */
120
121 static tree
122 build_gimple_eh_filter_tree (tree body, tree allowed, tree failure)
123 {
124 tree t;
125
126 /* FIXME should the allowed types go in TREE_TYPE? */
127 t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
128 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
129
130 t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
131 append_to_statement_list (body, &TREE_OPERAND (t, 0));
132
133 return t;
134 }
135
136 /* Genericize an EH_SPEC_BLOCK by converting it to a
137 TRY_CATCH_EXPR/EH_FILTER_EXPR pair. */
138
139 static void
140 genericize_eh_spec_block (tree *stmt_p)
141 {
142 tree body = EH_SPEC_STMTS (*stmt_p);
143 tree allowed = EH_SPEC_RAISES (*stmt_p);
144 tree failure = build_call_n (call_unexpected_fn, 1, build_exc_ptr ());
145
146 *stmt_p = build_gimple_eh_filter_tree (body, allowed, failure);
147 suppress_warning (*stmt_p);
148 suppress_warning (TREE_OPERAND (*stmt_p, 1));
149 }
150
151 /* Return the first non-compound statement in STMT. */
152
153 tree
154 first_stmt (tree stmt)
155 {
156 switch (TREE_CODE (stmt))
157 {
158 case STATEMENT_LIST:
159 if (tree_statement_list_node *p = STATEMENT_LIST_HEAD (stmt))
160 return first_stmt (p->stmt);
161 return void_node;
162
163 case BIND_EXPR:
164 return first_stmt (BIND_EXPR_BODY (stmt));
165
166 default:
167 return stmt;
168 }
169 }
170
171 /* Genericize an IF_STMT by turning it into a COND_EXPR. */
172
173 static void
174 genericize_if_stmt (tree *stmt_p)
175 {
176 tree stmt, cond, then_, else_;
177 location_t locus = EXPR_LOCATION (*stmt_p);
178
179 stmt = *stmt_p;
180 cond = IF_COND (stmt);
181 then_ = THEN_CLAUSE (stmt);
182 else_ = ELSE_CLAUSE (stmt);
183
184 if (then_ && else_)
185 {
186 tree ft = first_stmt (then_);
187 tree fe = first_stmt (else_);
188 br_predictor pr;
189 if (TREE_CODE (ft) == PREDICT_EXPR
190 && TREE_CODE (fe) == PREDICT_EXPR
191 && (pr = PREDICT_EXPR_PREDICTOR (ft)) == PREDICT_EXPR_PREDICTOR (fe)
192 && (pr == PRED_HOT_LABEL || pr == PRED_COLD_LABEL))
193 {
194 gcc_rich_location richloc (EXPR_LOC_OR_LOC (ft, locus));
195 richloc.add_range (EXPR_LOC_OR_LOC (fe, locus));
196 warning_at (&richloc, OPT_Wattributes,
197 "both branches of %<if%> statement marked as %qs",
198 pr == PRED_HOT_LABEL ? "likely" : "unlikely");
199 }
200 }
201
202 if (!then_)
203 then_ = build_empty_stmt (locus);
204 if (!else_)
205 else_ = build_empty_stmt (locus);
206
207 /* consteval if has been verified not to have the then_/else_ blocks
208 entered by gotos/case labels from elsewhere, and as then_ block
209 can contain unfolded immediate function calls, we have to discard
210 the then_ block regardless of whether else_ has side-effects or not. */
211 if (IF_STMT_CONSTEVAL_P (stmt))
212 {
213 if (block_may_fallthru (then_))
214 stmt = build3 (COND_EXPR, void_type_node, boolean_false_node,
215 void_node, else_);
216 else
217 stmt = else_;
218 }
219 else if (IF_STMT_CONSTEXPR_P (stmt))
220 stmt = integer_nonzerop (cond) ? then_ : else_;
221 /* ??? This optimization doesn't seem to belong here, but removing it
222 causes -Wreturn-type regressions (e.g. 107310). */
223 else if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
224 stmt = then_;
225 else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
226 stmt = else_;
227 else
228 stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
229 protected_set_expr_location_if_unset (stmt, locus);
230 *stmt_p = stmt;
231 }
232
233 /* Hook into the middle of gimplifying an OMP_FOR node. */
234
235 static enum gimplify_status
236 cp_gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
237 {
238 tree for_stmt = *expr_p;
239 gimple_seq seq = NULL;
240
241 /* Protect ourselves from recursion. */
242 if (OMP_FOR_GIMPLIFYING_P (for_stmt))
243 return GS_UNHANDLED;
244 OMP_FOR_GIMPLIFYING_P (for_stmt) = 1;
245
246 gimplify_and_add (for_stmt, &seq);
247 gimple_seq_add_seq (pre_p, seq);
248
249 OMP_FOR_GIMPLIFYING_P (for_stmt) = 0;
250
251 return GS_ALL_DONE;
252 }
253
254 /* Gimplify an EXPR_STMT node. */
255
256 static void
257 gimplify_expr_stmt (tree *stmt_p)
258 {
259 tree stmt = EXPR_STMT_EXPR (*stmt_p);
260
261 if (stmt == error_mark_node)
262 stmt = NULL;
263
264 /* Gimplification of a statement expression will nullify the
265 statement if all its side effects are moved to *PRE_P and *POST_P.
266
267 In this case we will not want to emit the gimplified statement.
268 However, we may still want to emit a warning, so we do that before
269 gimplification. */
270 if (stmt && warn_unused_value)
271 {
272 if (!TREE_SIDE_EFFECTS (stmt))
273 {
274 if (!IS_EMPTY_STMT (stmt)
275 && !VOID_TYPE_P (TREE_TYPE (stmt))
276 && !warning_suppressed_p (stmt, OPT_Wunused_value))
277 warning (OPT_Wunused_value, "statement with no effect");
278 }
279 else
280 warn_if_unused_value (stmt, input_location);
281 }
282
283 if (stmt == NULL_TREE)
284 stmt = alloc_stmt_list ();
285
286 *stmt_p = stmt;
287 }
288
289 /* Gimplify initialization from an AGGR_INIT_EXPR. */
290
291 static void
292 cp_gimplify_init_expr (tree *expr_p)
293 {
294 tree from = TREE_OPERAND (*expr_p, 1);
295 tree to = TREE_OPERAND (*expr_p, 0);
296 tree t;
297
298 if (TREE_CODE (from) == TARGET_EXPR)
299 if (tree init = TARGET_EXPR_INITIAL (from))
300 {
301 /* Make sure that we expected to elide this temporary. But also allow
302 gimplify_modify_expr_rhs to elide temporaries of trivial type. */
303 gcc_checking_assert (TARGET_EXPR_ELIDING_P (from)
304 || !TREE_ADDRESSABLE (TREE_TYPE (from)));
305 if (target_expr_needs_replace (from))
306 {
307 /* If this was changed by cp_genericize_target_expr, we need to
308 walk into it to replace uses of the slot. */
309 replace_decl (&init, TARGET_EXPR_SLOT (from), to);
310 *expr_p = init;
311 return;
312 }
313 else
314 from = init;
315 }
316
317 /* Look through any COMPOUND_EXPRs, since build_compound_expr pushes them
318 inside the TARGET_EXPR. */
319 for (t = from; t; )
320 {
321 tree sub = TREE_CODE (t) == COMPOUND_EXPR ? TREE_OPERAND (t, 0) : t;
322
323 /* If we are initializing from an AGGR_INIT_EXPR, drop the INIT_EXPR and
324 replace the slot operand with our target.
325
326 Should we add a target parm to gimplify_expr instead? No, as in this
327 case we want to replace the INIT_EXPR. */
328 if (TREE_CODE (sub) == AGGR_INIT_EXPR
329 || TREE_CODE (sub) == VEC_INIT_EXPR)
330 {
331 if (TREE_CODE (sub) == AGGR_INIT_EXPR)
332 AGGR_INIT_EXPR_SLOT (sub) = to;
333 else
334 VEC_INIT_EXPR_SLOT (sub) = to;
335 *expr_p = from;
336
337 /* The initialization is now a side-effect, so the container can
338 become void. */
339 if (from != sub)
340 TREE_TYPE (from) = void_type_node;
341 }
342
343 /* Handle aggregate NSDMI. */
344 replace_placeholders (sub, to);
345
346 if (t == sub)
347 break;
348 else
349 t = TREE_OPERAND (t, 1);
350 }
351
352 }
353
354 /* Gimplify a MUST_NOT_THROW_EXPR. */
355
356 static enum gimplify_status
357 gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
358 {
359 tree stmt = *expr_p;
360 tree temp = voidify_wrapper_expr (stmt, NULL);
361 tree body = TREE_OPERAND (stmt, 0);
362 gimple_seq try_ = NULL;
363 gimple_seq catch_ = NULL;
364 gimple *mnt;
365
366 gimplify_and_add (body, &try_);
367 mnt = gimple_build_eh_must_not_throw (call_terminate_fn);
368 gimple_seq_add_stmt_without_update (&catch_, mnt);
369 mnt = gimple_build_try (try_, catch_, GIMPLE_TRY_CATCH);
370
371 gimple_seq_add_stmt_without_update (pre_p, mnt);
372 if (temp)
373 {
374 *expr_p = temp;
375 return GS_OK;
376 }
377
378 *expr_p = NULL;
379 return GS_ALL_DONE;
380 }
381
382 /* Return TRUE if an operand (OP) of a given TYPE being copied is
383 really just an empty class copy.
384
385 Check that the operand has a simple form so that TARGET_EXPRs and
386 non-empty CONSTRUCTORs get reduced properly, and we leave the
387 return slot optimization alone because it isn't a copy. */
388
389 bool
390 simple_empty_class_p (tree type, tree op, tree_code code)
391 {
392 if (TREE_CODE (op) == COMPOUND_EXPR)
393 return simple_empty_class_p (type, TREE_OPERAND (op, 1), code);
394 if (SIMPLE_TARGET_EXPR_P (op)
395 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
396 /* The TARGET_EXPR is itself a simple copy, look through it. */
397 return simple_empty_class_p (type, TARGET_EXPR_INITIAL (op), code);
398
399 if (TREE_CODE (op) == PARM_DECL
400 && TREE_ADDRESSABLE (TREE_TYPE (op)))
401 {
402 tree fn = DECL_CONTEXT (op);
403 if (DECL_THUNK_P (fn)
404 || lambda_static_thunk_p (fn))
405 /* In a thunk, we pass through invisible reference parms, so this isn't
406 actually a copy. */
407 return false;
408 }
409
410 return
411 (TREE_CODE (op) == EMPTY_CLASS_EXPR
412 || code == MODIFY_EXPR
413 || is_gimple_lvalue (op)
414 || INDIRECT_REF_P (op)
415 || (TREE_CODE (op) == CONSTRUCTOR
416 && CONSTRUCTOR_NELTS (op) == 0)
417 || (TREE_CODE (op) == CALL_EXPR
418 && !CALL_EXPR_RETURN_SLOT_OPT (op)))
419 && !TREE_CLOBBER_P (op)
420 && is_really_empty_class (type, /*ignore_vptr*/true);
421 }
422
423 /* Returns true if evaluating E as an lvalue has side-effects;
424 specifically, a volatile lvalue has TREE_SIDE_EFFECTS, but it doesn't really
425 have side-effects until there is a read or write through it. */
426
427 static bool
428 lvalue_has_side_effects (tree e)
429 {
430 if (!TREE_SIDE_EFFECTS (e))
431 return false;
432 while (handled_component_p (e))
433 {
434 if (TREE_CODE (e) == ARRAY_REF
435 && TREE_SIDE_EFFECTS (TREE_OPERAND (e, 1)))
436 return true;
437 e = TREE_OPERAND (e, 0);
438 }
439 if (DECL_P (e))
440 /* Just naming a variable has no side-effects. */
441 return false;
442 else if (INDIRECT_REF_P (e))
443 /* Similarly, indirection has no side-effects. */
444 return TREE_SIDE_EFFECTS (TREE_OPERAND (e, 0));
445 else
446 /* For anything else, trust TREE_SIDE_EFFECTS. */
447 return TREE_SIDE_EFFECTS (e);
448 }
449
450 /* Return true if FN is an immediate-escalating function. */
451
452 static bool
453 immediate_escalating_function_p (tree fn)
454 {
455 if (!fn || !flag_immediate_escalation)
456 return false;
457
458 gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
459
460 if (DECL_IMMEDIATE_FUNCTION_P (fn))
461 return false;
462
463 /* An immediate-escalating function is
464 -- the call operator of a lambda that is not declared with the consteval
465 specifier */
466 if (LAMBDA_FUNCTION_P (fn))
467 return true;
468 /* -- a defaulted special member function that is not declared with the
469 consteval specifier */
470 special_function_kind sfk = special_memfn_p (fn);
471 if (sfk != sfk_none && DECL_DEFAULTED_FN (fn))
472 return true;
473 /* -- a function that results from the instantiation of a templated entity
474 defined with the constexpr specifier. */
475 return is_instantiation_of_constexpr (fn);
476 }
477
478 /* Return true if FN is an immediate-escalating function that has not been
479 checked for escalating expressions.. */
480
481 static bool
482 unchecked_immediate_escalating_function_p (tree fn)
483 {
484 return (immediate_escalating_function_p (fn)
485 && !DECL_ESCALATION_CHECKED_P (fn));
486 }
487
488 /* Promote FN to an immediate function, including its clones. */
489
490 static void
491 promote_function_to_consteval (tree fn)
492 {
493 SET_DECL_IMMEDIATE_FUNCTION_P (fn);
494 DECL_ESCALATION_CHECKED_P (fn) = true;
495 tree clone;
496 FOR_EACH_CLONE (clone, fn)
497 {
498 SET_DECL_IMMEDIATE_FUNCTION_P (clone);
499 DECL_ESCALATION_CHECKED_P (clone) = true;
500 }
501 }
502
503 /* A wrapper around cp_fold_immediate_r. Return a non-null tree if
504 we found a non-constant immediate function, or taking the address
505 of an immediate function. */
506
507 tree
508 cp_fold_immediate (tree *tp, mce_value manifestly_const_eval,
509 tree decl /*= current_function_decl*/)
510 {
511 if (cxx_dialect <= cxx17)
512 return NULL_TREE;
513
514 temp_override<tree> cfd (current_function_decl, decl);
515
516 fold_flags_t flags = ff_none;
517 if (manifestly_const_eval == mce_false)
518 flags |= ff_mce_false;
519
520 cp_fold_data data (flags);
521 int save_errorcount = errorcount;
522 tree r = cp_walk_tree (tp, cp_fold_immediate_r, &data, NULL);
523 if (errorcount > save_errorcount)
524 return integer_one_node;
525 return r;
526 }
527
528 /* Maybe say that FN (a function decl with DECL_IMMEDIATE_FUNCTION_P set)
529 was initially not an immediate function, but was promoted to one because
530 its body contained an immediate-escalating expression or conversion. */
531
532 static void
533 maybe_explain_promoted_consteval (location_t loc, tree fn)
534 {
535 if (DECL_ESCALATION_CHECKED_P (fn))
536 {
537 /* See if we can figure out what made the function consteval. */
538 tree x = cp_fold_immediate (&DECL_SAVED_TREE (fn), mce_unknown, NULL_TREE);
539 if (x)
540 inform (cp_expr_loc_or_loc (x, loc),
541 "%qD was promoted to an immediate function because its "
542 "body contains an immediate-escalating expression %qE", fn, x);
543 else
544 inform (loc, "%qD was promoted to an immediate function", fn);
545 }
546 }
547
548 /* Gimplify *EXPR_P as rvalue into an expression that can't be modified
549 by expressions with side-effects in other operands. */
550
551 static enum gimplify_status
552 gimplify_to_rvalue (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
553 bool (*gimple_test_f) (tree))
554 {
555 enum gimplify_status t
556 = gimplify_expr (expr_p, pre_p, post_p, gimple_test_f, fb_rvalue);
557 if (t == GS_ERROR)
558 return GS_ERROR;
559 else if (is_gimple_variable (*expr_p) && TREE_CODE (*expr_p) != SSA_NAME)
560 *expr_p = get_initialized_tmp_var (*expr_p, pre_p);
561 return t;
562 }
563
564 /* Like gimplify_arg, but if ORDERED is set (which should be set if
565 any of the arguments this argument is sequenced before has
566 TREE_SIDE_EFFECTS set, make sure expressions with is_gimple_reg_type type
567 are gimplified into SSA_NAME or a fresh temporary and for
568 non-is_gimple_reg_type we don't optimize away TARGET_EXPRs. */
569
570 static enum gimplify_status
571 cp_gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
572 bool ordered)
573 {
574 enum gimplify_status t;
575 if (ordered
576 && !is_gimple_reg_type (TREE_TYPE (*arg_p))
577 && TREE_CODE (*arg_p) == TARGET_EXPR)
578 {
579 /* gimplify_arg would strip away the TARGET_EXPR, but
580 that can mean we don't copy the argument and some following
581 argument with side-effect could modify it. */
582 protected_set_expr_location (*arg_p, call_location);
583 return gimplify_expr (arg_p, pre_p, NULL, is_gimple_lvalue, fb_either);
584 }
585 else
586 {
587 t = gimplify_arg (arg_p, pre_p, call_location);
588 if (t == GS_ERROR)
589 return GS_ERROR;
590 else if (ordered
591 && is_gimple_reg_type (TREE_TYPE (*arg_p))
592 && is_gimple_variable (*arg_p)
593 && TREE_CODE (*arg_p) != SSA_NAME
594 /* No need to force references into register, references
595 can't be modified. */
596 && !TYPE_REF_P (TREE_TYPE (*arg_p))
597 /* And this can't be modified either. */
598 && *arg_p != current_class_ptr)
599 *arg_p = get_initialized_tmp_var (*arg_p, pre_p);
600 return t;
601 }
602
603 }
604
605 /* Do C++-specific gimplification. Args are as for gimplify_expr. */
606
607 int
608 cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
609 {
610 int saved_stmts_are_full_exprs_p = 0;
611 location_t loc = cp_expr_loc_or_input_loc (*expr_p);
612 enum tree_code code = TREE_CODE (*expr_p);
613 enum gimplify_status ret;
614
615 if (STATEMENT_CODE_P (code))
616 {
617 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
618 current_stmt_tree ()->stmts_are_full_exprs_p
619 = STMT_IS_FULL_EXPR_P (*expr_p);
620 }
621
622 switch (code)
623 {
624 case AGGR_INIT_EXPR:
625 simplify_aggr_init_expr (expr_p);
626 ret = GS_OK;
627 break;
628
629 case VEC_INIT_EXPR:
630 {
631 *expr_p = expand_vec_init_expr (NULL_TREE, *expr_p,
632 tf_warning_or_error);
633
634 cp_fold_data data (ff_genericize | ff_mce_false);
635 cp_walk_tree (expr_p, cp_fold_r, &data, NULL);
636 cp_genericize_tree (expr_p, false);
637 copy_if_shared (expr_p);
638 ret = GS_OK;
639 }
640 break;
641
642 case THROW_EXPR:
643 /* FIXME communicate throw type to back end, probably by moving
644 THROW_EXPR into ../tree.def. */
645 *expr_p = TREE_OPERAND (*expr_p, 0);
646 ret = GS_OK;
647 break;
648
649 case MUST_NOT_THROW_EXPR:
650 ret = gimplify_must_not_throw_expr (expr_p, pre_p);
651 break;
652
653 /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the
654 LHS of an assignment might also be involved in the RHS, as in bug
655 25979. */
656 case INIT_EXPR:
657 cp_gimplify_init_expr (expr_p);
658 if (TREE_CODE (*expr_p) != INIT_EXPR)
659 return GS_OK;
660 /* Fall through. */
661 case MODIFY_EXPR:
662 modify_expr_case:
663 {
664 /* If the back end isn't clever enough to know that the lhs and rhs
665 types are the same, add an explicit conversion. */
666 tree op0 = TREE_OPERAND (*expr_p, 0);
667 tree op1 = TREE_OPERAND (*expr_p, 1);
668
669 if (!error_operand_p (op0)
670 && !error_operand_p (op1)
671 && (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op0))
672 || TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op1)))
673 && !useless_type_conversion_p (TREE_TYPE (op1), TREE_TYPE (op0)))
674 TREE_OPERAND (*expr_p, 1) = build1 (VIEW_CONVERT_EXPR,
675 TREE_TYPE (op0), op1);
676
677 else if (simple_empty_class_p (TREE_TYPE (op0), op1, code))
678 {
679 while (TREE_CODE (op1) == TARGET_EXPR)
680 /* We're disconnecting the initializer from its target,
681 don't create a temporary. */
682 op1 = TARGET_EXPR_INITIAL (op1);
683
684 /* Remove any copies of empty classes. Also drop volatile
685 variables on the RHS to avoid infinite recursion from
686 gimplify_expr trying to load the value. */
687 if (TREE_SIDE_EFFECTS (op1))
688 {
689 if (TREE_THIS_VOLATILE (op1)
690 && (REFERENCE_CLASS_P (op1) || DECL_P (op1)))
691 op1 = build_fold_addr_expr (op1);
692
693 gimplify_and_add (op1, pre_p);
694 }
695 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
696 is_gimple_lvalue, fb_lvalue);
697 *expr_p = TREE_OPERAND (*expr_p, 0);
698 if (code == RETURN_EXPR && REFERENCE_CLASS_P (*expr_p))
699 /* Avoid 'return *<retval>;' */
700 *expr_p = TREE_OPERAND (*expr_p, 0);
701 }
702 /* P0145 says that the RHS is sequenced before the LHS.
703 gimplify_modify_expr gimplifies the RHS before the LHS, but that
704 isn't quite strong enough in two cases:
705
706 1) gimplify.cc wants to leave a CALL_EXPR on the RHS, which would
707 mean it's evaluated after the LHS.
708
709 2) the value calculation of the RHS is also sequenced before the
710 LHS, so for scalar assignment we need to preevaluate if the
711 RHS could be affected by LHS side-effects even if it has no
712 side-effects of its own. We don't need this for classes because
713 class assignment takes its RHS by reference. */
714 else if (flag_strong_eval_order > 1
715 && TREE_CODE (*expr_p) == MODIFY_EXPR
716 && lvalue_has_side_effects (op0)
717 && (TREE_CODE (op1) == CALL_EXPR
718 || (SCALAR_TYPE_P (TREE_TYPE (op1))
719 && !TREE_CONSTANT (op1))))
720 TREE_OPERAND (*expr_p, 1) = get_initialized_tmp_var (op1, pre_p);
721 }
722 ret = GS_OK;
723 break;
724
725 case EMPTY_CLASS_EXPR:
726 /* We create an empty CONSTRUCTOR with RECORD_TYPE. */
727 *expr_p = build_constructor (TREE_TYPE (*expr_p), NULL);
728 ret = GS_OK;
729 break;
730
731 case BASELINK:
732 *expr_p = BASELINK_FUNCTIONS (*expr_p);
733 ret = GS_OK;
734 break;
735
736 case TRY_BLOCK:
737 genericize_try_block (expr_p);
738 ret = GS_OK;
739 break;
740
741 case HANDLER:
742 genericize_catch_block (expr_p);
743 ret = GS_OK;
744 break;
745
746 case EH_SPEC_BLOCK:
747 genericize_eh_spec_block (expr_p);
748 ret = GS_OK;
749 break;
750
751 case USING_STMT:
752 gcc_unreachable ();
753
754 case FOR_STMT:
755 case WHILE_STMT:
756 case DO_STMT:
757 case SWITCH_STMT:
758 case CONTINUE_STMT:
759 case BREAK_STMT:
760 gcc_unreachable ();
761
762 case OMP_FOR:
763 case OMP_SIMD:
764 case OMP_DISTRIBUTE:
765 case OMP_LOOP:
766 case OMP_TASKLOOP:
767 case OMP_TILE:
768 case OMP_UNROLL:
769 ret = cp_gimplify_omp_for (expr_p, pre_p);
770 break;
771
772 case EXPR_STMT:
773 gimplify_expr_stmt (expr_p);
774 ret = GS_OK;
775 break;
776
777 case UNARY_PLUS_EXPR:
778 {
779 tree arg = TREE_OPERAND (*expr_p, 0);
780 tree type = TREE_TYPE (*expr_p);
781 *expr_p = (TREE_TYPE (arg) != type) ? fold_convert (type, arg)
782 : arg;
783 ret = GS_OK;
784 }
785 break;
786
787 case CALL_EXPR:
788 ret = GS_OK;
789 if (flag_strong_eval_order == 2
790 && CALL_EXPR_FN (*expr_p)
791 && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p)
792 && cp_get_callee_fndecl_nofold (*expr_p) == NULL_TREE)
793 {
794 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
795 enum gimplify_status t
796 = gimplify_to_rvalue (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
797 is_gimple_call_addr);
798 if (t == GS_ERROR)
799 ret = GS_ERROR;
800 /* GIMPLE considers most pointer conversion useless, but for
801 calls we actually care about the exact function pointer type. */
802 else if (TREE_TYPE (CALL_EXPR_FN (*expr_p)) != fnptrtype)
803 CALL_EXPR_FN (*expr_p)
804 = build1 (NOP_EXPR, fnptrtype, CALL_EXPR_FN (*expr_p));
805 }
806 if (!CALL_EXPR_FN (*expr_p))
807 /* Internal function call. */;
808 else if (CALL_EXPR_REVERSE_ARGS (*expr_p))
809 {
810 /* This is a call to a (compound) assignment operator that used
811 the operator syntax; gimplify the RHS first. */
812 gcc_assert (call_expr_nargs (*expr_p) == 2);
813 gcc_assert (!CALL_EXPR_ORDERED_ARGS (*expr_p));
814 enum gimplify_status t
815 = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, 1), pre_p, loc,
816 TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, 0)));
817 if (t == GS_ERROR)
818 ret = GS_ERROR;
819 }
820 else if (CALL_EXPR_ORDERED_ARGS (*expr_p))
821 {
822 /* Leave the last argument for gimplify_call_expr, to avoid problems
823 with __builtin_va_arg_pack(). */
824 int nargs = call_expr_nargs (*expr_p) - 1;
825 int last_side_effects_arg = -1;
826 for (int i = nargs; i > 0; --i)
827 if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, i)))
828 {
829 last_side_effects_arg = i;
830 break;
831 }
832 for (int i = 0; i < nargs; ++i)
833 {
834 enum gimplify_status t
835 = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p, loc,
836 i < last_side_effects_arg);
837 if (t == GS_ERROR)
838 ret = GS_ERROR;
839 }
840 }
841 else if (flag_strong_eval_order
842 && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p))
843 {
844 /* If flag_strong_eval_order, evaluate the object argument first. */
845 tree fntype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
846 if (INDIRECT_TYPE_P (fntype))
847 fntype = TREE_TYPE (fntype);
848 tree decl = cp_get_callee_fndecl_nofold (*expr_p);
849 /* We can't just rely on 'decl' because virtual function callees
850 are expressed as OBJ_TYPE_REF. Note that the xobj memfn check
851 will also hold for calls of the form (&A::f)(a, ...) which does
852 not require such sequencing, though it's allowed under
853 "indeterminately sequenced". */
854 if (TREE_CODE (fntype) == METHOD_TYPE
855 || (decl && DECL_LANG_SPECIFIC (decl)
856 && DECL_XOBJ_MEMBER_FUNCTION_P (decl)))
857 {
858 int nargs = call_expr_nargs (*expr_p);
859 bool side_effects = false;
860 for (int i = 1; i < nargs; ++i)
861 if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p, i)))
862 {
863 side_effects = true;
864 break;
865 }
866 enum gimplify_status t
867 = cp_gimplify_arg (&CALL_EXPR_ARG (*expr_p, 0), pre_p, loc,
868 side_effects);
869 if (t == GS_ERROR)
870 ret = GS_ERROR;
871 }
872 }
873 if (ret != GS_ERROR)
874 {
875 tree decl = cp_get_callee_fndecl_nofold (*expr_p);
876 if (!decl)
877 break;
878 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
879 switch (DECL_FE_FUNCTION_CODE (decl))
880 {
881 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
882 *expr_p = boolean_false_node;
883 break;
884 case CP_BUILT_IN_SOURCE_LOCATION:
885 *expr_p
886 = fold_builtin_source_location (*expr_p);
887 break;
888 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
889 *expr_p
890 = fold_builtin_is_corresponding_member
891 (EXPR_LOCATION (*expr_p), call_expr_nargs (*expr_p),
892 &CALL_EXPR_ARG (*expr_p, 0));
893 break;
894 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
895 *expr_p
896 = fold_builtin_is_pointer_inverconvertible_with_class
897 (EXPR_LOCATION (*expr_p), call_expr_nargs (*expr_p),
898 &CALL_EXPR_ARG (*expr_p, 0));
899 break;
900 default:
901 break;
902 }
903 else if (fndecl_built_in_p (decl, BUILT_IN_CLZG, BUILT_IN_CTZG))
904 ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p,
905 post_p);
906 else
907 /* All consteval functions should have been processed by now. */
908 gcc_checking_assert (!immediate_invocation_p (decl));
909 }
910 break;
911
912 case TARGET_EXPR:
913 /* A TARGET_EXPR that expresses direct-initialization should have been
914 elided by cp_gimplify_init_expr. */
915 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (*expr_p));
916 /* Likewise, but allow extra temps of trivial type so that
917 gimplify_init_ctor_preeval can materialize subobjects of a CONSTRUCTOR
918 on the rhs of an assignment, as in constexpr-aggr1.C. */
919 gcc_checking_assert (!TARGET_EXPR_ELIDING_P (*expr_p)
920 || !TREE_ADDRESSABLE (TREE_TYPE (*expr_p)));
921 ret = GS_UNHANDLED;
922 break;
923
924 case PTRMEM_CST:
925 *expr_p = cplus_expand_constant (*expr_p);
926 if (TREE_CODE (*expr_p) == PTRMEM_CST)
927 ret = GS_ERROR;
928 else
929 ret = GS_OK;
930 break;
931
932 case RETURN_EXPR:
933 if (TREE_OPERAND (*expr_p, 0)
934 && (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == INIT_EXPR
935 || TREE_CODE (TREE_OPERAND (*expr_p, 0)) == MODIFY_EXPR))
936 {
937 expr_p = &TREE_OPERAND (*expr_p, 0);
938 /* Avoid going through the INIT_EXPR case, which can
939 degrade INIT_EXPRs into AGGR_INIT_EXPRs. */
940 goto modify_expr_case;
941 }
942 /* Fall through. */
943
944 default:
945 ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
946 break;
947 }
948
949 /* Restore saved state. */
950 if (STATEMENT_CODE_P (code))
951 current_stmt_tree ()->stmts_are_full_exprs_p
952 = saved_stmts_are_full_exprs_p;
953
954 return ret;
955 }
956
957 static inline bool
958 is_invisiref_parm (const_tree t)
959 {
960 return ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
961 && DECL_BY_REFERENCE (t));
962 }
963
964 /* A stable comparison routine for use with splay trees and DECLs. */
965
966 static int
967 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
968 {
969 tree a = (tree) xa;
970 tree b = (tree) xb;
971
972 return DECL_UID (a) - DECL_UID (b);
973 }
974
975 /* OpenMP context during genericization. */
976
977 struct cp_genericize_omp_taskreg
978 {
979 bool is_parallel;
980 bool default_shared;
981 struct cp_genericize_omp_taskreg *outer;
982 splay_tree variables;
983 };
984
985 /* Return true if genericization should try to determine if
986 DECL is firstprivate or shared within task regions. */
987
988 static bool
989 omp_var_to_track (tree decl)
990 {
991 tree type = TREE_TYPE (decl);
992 if (is_invisiref_parm (decl))
993 type = TREE_TYPE (type);
994 else if (TYPE_REF_P (type))
995 type = TREE_TYPE (type);
996 while (TREE_CODE (type) == ARRAY_TYPE)
997 type = TREE_TYPE (type);
998 if (type == error_mark_node || !CLASS_TYPE_P (type))
999 return false;
1000 if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
1001 return false;
1002 if (cxx_omp_predetermined_sharing (decl) != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
1003 return false;
1004 return true;
1005 }
1006
1007 /* Note DECL use in OpenMP region OMP_CTX during genericization. */
1008
1009 static void
1010 omp_cxx_notice_variable (struct cp_genericize_omp_taskreg *omp_ctx, tree decl)
1011 {
1012 splay_tree_node n = splay_tree_lookup (omp_ctx->variables,
1013 (splay_tree_key) decl);
1014 if (n == NULL)
1015 {
1016 int flags = OMP_CLAUSE_DEFAULT_SHARED;
1017 if (omp_ctx->outer)
1018 omp_cxx_notice_variable (omp_ctx->outer, decl);
1019 if (!omp_ctx->default_shared)
1020 {
1021 struct cp_genericize_omp_taskreg *octx;
1022
1023 for (octx = omp_ctx->outer; octx; octx = octx->outer)
1024 {
1025 n = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
1026 if (n && n->value != OMP_CLAUSE_DEFAULT_SHARED)
1027 {
1028 flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
1029 break;
1030 }
1031 if (octx->is_parallel)
1032 break;
1033 }
1034 if (octx == NULL
1035 && (TREE_CODE (decl) == PARM_DECL
1036 || (!(TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1037 && DECL_CONTEXT (decl) == current_function_decl)))
1038 flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
1039 if (flags == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
1040 {
1041 /* DECL is implicitly determined firstprivate in
1042 the current task construct. Ensure copy ctor and
1043 dtor are instantiated, because during gimplification
1044 it will be already too late. */
1045 tree type = TREE_TYPE (decl);
1046 if (is_invisiref_parm (decl))
1047 type = TREE_TYPE (type);
1048 else if (TYPE_REF_P (type))
1049 type = TREE_TYPE (type);
1050 while (TREE_CODE (type) == ARRAY_TYPE)
1051 type = TREE_TYPE (type);
1052 get_copy_ctor (type, tf_none);
1053 get_dtor (type, tf_none);
1054 }
1055 }
1056 splay_tree_insert (omp_ctx->variables, (splay_tree_key) decl, flags);
1057 }
1058 }
1059
1060 /* True if any of the element initializers in CTOR are TARGET_EXPRs that are
1061 not expected to elide, e.g. because unsafe_copy_elision_p is true. */
1062
1063 static bool
1064 any_non_eliding_target_exprs (tree ctor)
1065 {
1066 for (const constructor_elt &e : *CONSTRUCTOR_ELTS (ctor))
1067 {
1068 if (TREE_CODE (e.value) == TARGET_EXPR
1069 && !TARGET_EXPR_ELIDING_P (e.value))
1070 return true;
1071 }
1072 return false;
1073 }
1074
1075 /* If we might need to clean up a partially constructed object, break down the
1076 CONSTRUCTOR with split_nonconstant_init. Also expand VEC_INIT_EXPR at this
1077 point. If initializing TO with FROM is non-trivial, overwrite *REPLACE with
1078 the result. */
1079
1080 static void
1081 cp_genericize_init (tree *replace, tree from, tree to, vec<tree,va_gc>** flags)
1082 {
1083 tree init = NULL_TREE;
1084 if (TREE_CODE (from) == VEC_INIT_EXPR)
1085 init = expand_vec_init_expr (to, from, tf_warning_or_error, flags);
1086 else if (TREE_CODE (from) == CONSTRUCTOR
1087 && TREE_SIDE_EFFECTS (from)
1088 && ((flag_exceptions
1089 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (from)))
1090 || any_non_eliding_target_exprs (from)))
1091 {
1092 to = cp_stabilize_reference (to);
1093 replace_placeholders (from, to);
1094 init = split_nonconstant_init (to, from);
1095 }
1096
1097 if (init)
1098 {
1099 if (*replace == from)
1100 /* Make cp_gimplify_init_expr call replace_decl on this
1101 TARGET_EXPR_INITIAL. */
1102 init = fold_convert (void_type_node, init);
1103 *replace = init;
1104 }
1105 }
1106
1107 /* For an INIT_EXPR, replace the INIT_EXPR itself. */
1108
1109 static void
1110 cp_genericize_init_expr (tree *stmt_p)
1111 {
1112 iloc_sentinel ils = EXPR_LOCATION (*stmt_p);
1113 tree to = TREE_OPERAND (*stmt_p, 0);
1114 tree from = TREE_OPERAND (*stmt_p, 1);
1115 if (SIMPLE_TARGET_EXPR_P (from)
1116 /* Return gets confused if we clobber its INIT_EXPR this soon. */
1117 && TREE_CODE (to) != RESULT_DECL)
1118 from = TARGET_EXPR_INITIAL (from);
1119 cp_genericize_init (stmt_p, from, to, nullptr);
1120 }
1121
1122 /* For a TARGET_EXPR, change the TARGET_EXPR_INITIAL. We will need to use
1123 replace_decl later when we know what we're initializing. */
1124
1125 static void
1126 cp_genericize_target_expr (tree *stmt_p)
1127 {
1128 iloc_sentinel ils = EXPR_LOCATION (*stmt_p);
1129 tree slot = TARGET_EXPR_SLOT (*stmt_p);
1130 vec<tree, va_gc> *flags = make_tree_vector ();
1131 cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p),
1132 TARGET_EXPR_INITIAL (*stmt_p), slot, &flags);
1133 gcc_assert (!DECL_INITIAL (slot));
1134 for (tree f : flags)
1135 {
1136 /* Once initialization is complete TARGET_EXPR_CLEANUP becomes active, so
1137 disable any subobject cleanups. */
1138 tree d = build_disable_temp_cleanup (f);
1139 auto &r = TARGET_EXPR_INITIAL (*stmt_p);
1140 r = add_stmt_to_compound (r, d);
1141 }
1142 release_tree_vector (flags);
1143 }
1144
1145 /* Similar to if (target_expr_needs_replace) replace_decl, but TP is the
1146 TARGET_EXPR_INITIAL, and this also updates *_SLOT. We need this extra
1147 replacement when cp_folding TARGET_EXPR to preserve the invariant that
1148 AGGR_INIT_EXPR_SLOT agrees with the enclosing TARGET_EXPR_SLOT. */
1149
1150 static bool
1151 maybe_replace_decl (tree *tp, tree decl, tree replacement)
1152 {
1153 if (!*tp || !VOID_TYPE_P (TREE_TYPE (*tp)))
1154 return false;
1155 tree t = *tp;
1156 while (TREE_CODE (t) == COMPOUND_EXPR)
1157 t = TREE_OPERAND (t, 1);
1158 if (TREE_CODE (t) == AGGR_INIT_EXPR)
1159 replace_decl (&AGGR_INIT_EXPR_SLOT (t), decl, replacement);
1160 else if (TREE_CODE (t) == VEC_INIT_EXPR)
1161 replace_decl (&VEC_INIT_EXPR_SLOT (t), decl, replacement);
1162 else
1163 replace_decl (tp, decl, replacement);
1164 return true;
1165 }
1166
1167 /* Genericization context. */
1168
1169 struct cp_genericize_data
1170 {
1171 hash_set<tree> *p_set;
1172 auto_vec<tree> bind_expr_stack;
1173 struct cp_genericize_omp_taskreg *omp_ctx;
1174 tree try_block;
1175 bool no_sanitize_p;
1176 bool handle_invisiref_parm_p;
1177 };
1178
1179 /* Emit an error about taking the address of an immediate function.
1180 EXPR is the whole expression; DECL is the immediate function. */
1181
1182 static void
1183 taking_address_of_imm_fn_error (tree expr, tree decl)
1184 {
1185 auto_diagnostic_group d;
1186 const location_t loc = (TREE_CODE (expr) == PTRMEM_CST
1187 ? PTRMEM_CST_LOCATION (expr)
1188 : EXPR_LOCATION (expr));
1189 error_at (loc, "taking address of an immediate function %qD", decl);
1190 maybe_explain_promoted_consteval (loc, decl);
1191 }
1192
1193 /* Build up an INIT_EXPR to initialize the object of a constructor call that
1194 has been folded to a constant value. CALL is the CALL_EXPR for the
1195 constructor call; INIT is the value. */
1196
1197 static tree
1198 cp_build_init_expr_for_ctor (tree call, tree init)
1199 {
1200 tree a = CALL_EXPR_ARG (call, 0);
1201 if (is_dummy_object (a))
1202 return init;
1203 const bool return_this = targetm.cxx.cdtor_returns_this ();
1204 const location_t loc = EXPR_LOCATION (call);
1205 if (return_this)
1206 a = cp_save_expr (a);
1207 tree s = build_fold_indirect_ref_loc (loc, a);
1208 init = cp_build_init_expr (s, init);
1209 if (return_this)
1210 {
1211 init = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (call), init,
1212 fold_convert_loc (loc, TREE_TYPE (call), a));
1213 suppress_warning (init);
1214 }
1215 return init;
1216 }
1217
1218 /* A walk_tree callback for cp_fold_function and cp_fully_fold_init to handle
1219 immediate functions. */
1220
1221 static tree
1222 cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_)
1223 {
1224 auto data = static_cast<cp_fold_data *>(data_);
1225 tree stmt = *stmt_p;
1226 /* The purpose of this is not to emit errors for mce_unknown. */
1227 const tsubst_flags_t complain = (data->flags & ff_mce_false
1228 ? tf_error : tf_none);
1229 const tree_code code = TREE_CODE (stmt);
1230
1231 /* No need to look into types or unevaluated operands.
1232 NB: This affects cp_fold_r as well. */
1233 if (TYPE_P (stmt)
1234 || unevaluated_p (code)
1235 /* We do not use in_immediate_context here because it checks
1236 more than is desirable, e.g., sk_template_parms. */
1237 || cp_unevaluated_operand
1238 || (current_function_decl
1239 && DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
1240 {
1241 *walk_subtrees = 0;
1242 return NULL_TREE;
1243 }
1244
1245 tree decl = NULL_TREE;
1246 bool call_p = false;
1247
1248 /* We are looking for &fn or fn(). */
1249 switch (code)
1250 {
1251 case CALL_EXPR:
1252 case AGGR_INIT_EXPR:
1253 if (tree fn = cp_get_callee (stmt))
1254 if (TREE_CODE (fn) != ADDR_EXPR || ADDR_EXPR_DENOTES_CALL_P (fn))
1255 decl = cp_get_fndecl_from_callee (fn, /*fold*/false);
1256 call_p = true;
1257 break;
1258 case PTRMEM_CST:
1259 decl = PTRMEM_CST_MEMBER (stmt);
1260 break;
1261 case ADDR_EXPR:
1262 if (!ADDR_EXPR_DENOTES_CALL_P (stmt))
1263 decl = TREE_OPERAND (stmt, 0);
1264 break;
1265 case IF_STMT:
1266 if (IF_STMT_CONSTEVAL_P (stmt))
1267 {
1268 if (!data->pset.add (stmt))
1269 cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_immediate_r, data_,
1270 NULL);
1271 *walk_subtrees = 0;
1272 return NULL_TREE;
1273 }
1274 /* FALLTHRU */
1275 default:
1276 if (data->pset.add (stmt))
1277 *walk_subtrees = 0;
1278 return NULL_TREE;
1279 }
1280
1281 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
1282 return NULL_TREE;
1283
1284 /* Fully escalate once all templates have been instantiated. What we're
1285 calling is not a consteval function but it may become one. This
1286 requires recursing; DECL may be promoted to consteval because it
1287 contains an escalating expression E, but E itself may have to be
1288 promoted first, etc. */
1289 if (at_eof > 1 && unchecked_immediate_escalating_function_p (decl))
1290 {
1291 /* Set before the actual walk to avoid endless recursion. */
1292 DECL_ESCALATION_CHECKED_P (decl) = true;
1293 /* We're only looking for the first escalating expression. Let us not
1294 walk more trees than necessary, hence mce_unknown. */
1295 cp_fold_immediate (&DECL_SAVED_TREE (decl), mce_unknown, decl);
1296 }
1297
1298 /* [expr.const]p16 "An expression or conversion is immediate-escalating if
1299 it is not initially in an immediate function context and it is either
1300 -- an immediate invocation that is not a constant expression and is not
1301 a subexpression of an immediate invocation."
1302
1303 If we are in an immediate-escalating function, the immediate-escalating
1304 expression or conversion makes it an immediate function. So STMT does
1305 not need to produce a constant expression. */
1306 if (DECL_IMMEDIATE_FUNCTION_P (decl))
1307 {
1308 tree e = cxx_constant_value (stmt, tf_none);
1309 if (e == error_mark_node)
1310 {
1311 /* This takes care of, e.g.,
1312 template <typename T>
1313 constexpr int f(T t)
1314 {
1315 return id(t);
1316 }
1317 where id (consteval) causes f<int> to be promoted. */
1318 if (immediate_escalating_function_p (current_function_decl))
1319 promote_function_to_consteval (current_function_decl);
1320 else if (complain & tf_error)
1321 {
1322 if (call_p)
1323 {
1324 auto_diagnostic_group d;
1325 location_t loc = cp_expr_loc_or_input_loc (stmt);
1326 error_at (loc, "call to consteval function %qE is "
1327 "not a constant expression", stmt);
1328 /* Explain why it's not a constant expression. */
1329 *stmt_p = cxx_constant_value (stmt, complain);
1330 maybe_explain_promoted_consteval (loc, decl);
1331 }
1332 else if (!data->pset.add (stmt))
1333 {
1334 taking_address_of_imm_fn_error (stmt, decl);
1335 *stmt_p = build_zero_cst (TREE_TYPE (stmt));
1336 }
1337 /* If we're giving hard errors, continue the walk rather than
1338 bailing out after the first error. */
1339 return NULL_TREE;
1340 }
1341 *walk_subtrees = 0;
1342 return stmt;
1343 }
1344 /* We've evaluated the consteval function call. */
1345 if (call_p)
1346 {
1347 if (code == CALL_EXPR && DECL_CONSTRUCTOR_P (decl))
1348 *stmt_p = cp_build_init_expr_for_ctor (stmt, e);
1349 else
1350 *stmt_p = e;
1351 }
1352 }
1353 /* We've encountered a function call that may turn out to be consteval
1354 later. Store its caller so that we can ensure that the call is
1355 a constant expression. */
1356 else if (unchecked_immediate_escalating_function_p (decl))
1357 {
1358 /* Make sure we're not inserting new elements while walking
1359 the deferred_escalating_exprs hash table; if we are, it's
1360 likely that a function wasn't properly marked checked for
1361 i-e expressions. */
1362 gcc_checking_assert (at_eof <= 1);
1363 if (current_function_decl)
1364 remember_escalating_expr (current_function_decl);
1365 /* auto p = &f<int>; in the global scope won't be ensconced in
1366 a function we could store for later at this point. (If there's
1367 no c_f_d at this point and we're dealing with a call, we should
1368 see the call when cp_fold_function __static_i_and_d.) */
1369 else if (!call_p)
1370 remember_escalating_expr (stmt);
1371 }
1372
1373 return NULL_TREE;
1374 }
1375
1376 /* Perform any pre-gimplification folding of C++ front end trees to
1377 GENERIC.
1378 Note: The folding of non-omp cases is something to move into
1379 the middle-end. As for now we have most foldings only on GENERIC
1380 in fold-const, we need to perform this before transformation to
1381 GIMPLE-form.
1382
1383 ??? This is algorithmically weird because walk_tree works in pre-order, so
1384 we see outer expressions before inner expressions. This isn't as much of an
1385 issue because cp_fold recurses into subexpressions in many cases, but then
1386 walk_tree walks back into those subexpressions again. We avoid the
1387 resulting complexity problem by caching the result of cp_fold, but it's
1388 inelegant. */
1389
1390 static tree
1391 cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
1392 {
1393 cp_fold_data *data = (cp_fold_data*)data_;
1394 tree stmt = *stmt_p;
1395 enum tree_code code = TREE_CODE (stmt);
1396
1397 *stmt_p = stmt = cp_fold (*stmt_p, data->flags);
1398
1399 if (data->pset.add (stmt))
1400 {
1401 /* Don't walk subtrees of stmts we've already walked once, otherwise
1402 we can have exponential complexity with e.g. lots of nested
1403 SAVE_EXPRs or TARGET_EXPRs. cp_fold uses a cache and will return
1404 always the same tree, which the first time cp_fold_r has been
1405 called on it had the subtrees walked. */
1406 *walk_subtrees = 0;
1407 return NULL_TREE;
1408 }
1409
1410 code = TREE_CODE (stmt);
1411 switch (code)
1412 {
1413 tree x;
1414 int i, n;
1415 case OMP_FOR:
1416 case OMP_SIMD:
1417 case OMP_DISTRIBUTE:
1418 case OMP_LOOP:
1419 case OMP_TASKLOOP:
1420 case OMP_TILE:
1421 case OMP_UNROLL:
1422 case OACC_LOOP:
1423 cp_walk_tree (&OMP_FOR_BODY (stmt), cp_fold_r, data, NULL);
1424 cp_walk_tree (&OMP_FOR_CLAUSES (stmt), cp_fold_r, data, NULL);
1425 cp_walk_tree (&OMP_FOR_INIT (stmt), cp_fold_r, data, NULL);
1426 x = OMP_FOR_COND (stmt);
1427 if (x && TREE_CODE_CLASS (TREE_CODE (x)) == tcc_comparison)
1428 {
1429 cp_walk_tree (&TREE_OPERAND (x, 0), cp_fold_r, data, NULL);
1430 cp_walk_tree (&TREE_OPERAND (x, 1), cp_fold_r, data, NULL);
1431 }
1432 else if (x && TREE_CODE (x) == TREE_VEC)
1433 {
1434 n = TREE_VEC_LENGTH (x);
1435 for (i = 0; i < n; i++)
1436 {
1437 tree o = TREE_VEC_ELT (x, i);
1438 if (o && TREE_CODE_CLASS (TREE_CODE (o)) == tcc_comparison)
1439 cp_walk_tree (&TREE_OPERAND (o, 1), cp_fold_r, data, NULL);
1440 }
1441 }
1442 x = OMP_FOR_INCR (stmt);
1443 if (x && TREE_CODE (x) == TREE_VEC)
1444 {
1445 n = TREE_VEC_LENGTH (x);
1446 for (i = 0; i < n; i++)
1447 {
1448 tree o = TREE_VEC_ELT (x, i);
1449 if (o && TREE_CODE (o) == MODIFY_EXPR)
1450 o = TREE_OPERAND (o, 1);
1451 if (o && (TREE_CODE (o) == PLUS_EXPR || TREE_CODE (o) == MINUS_EXPR
1452 || TREE_CODE (o) == POINTER_PLUS_EXPR))
1453 {
1454 cp_walk_tree (&TREE_OPERAND (o, 0), cp_fold_r, data, NULL);
1455 cp_walk_tree (&TREE_OPERAND (o, 1), cp_fold_r, data, NULL);
1456 }
1457 }
1458 }
1459 cp_walk_tree (&OMP_FOR_PRE_BODY (stmt), cp_fold_r, data, NULL);
1460 *walk_subtrees = 0;
1461 return NULL_TREE;
1462
1463 case IF_STMT:
1464 if (IF_STMT_CONSTEVAL_P (stmt))
1465 {
1466 /* Don't walk THEN_CLAUSE (stmt) for consteval if. IF_COND is always
1467 boolean_false_node. */
1468 cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_r, data, NULL);
1469 cp_walk_tree (&IF_SCOPE (stmt), cp_fold_r, data, NULL);
1470 *walk_subtrees = 0;
1471 return NULL_TREE;
1472 }
1473 break;
1474
1475 /* cp_genericize_{init,target}_expr are only for genericize time; they're
1476 here rather than in cp_genericize to avoid problems with the invisible
1477 reference transition. */
1478 case INIT_EXPR:
1479 if (data->flags & ff_genericize)
1480 cp_genericize_init_expr (stmt_p);
1481 break;
1482
1483 case TARGET_EXPR:
1484 if (!flag_no_inline)
1485 if (tree &init = TARGET_EXPR_INITIAL (stmt))
1486 {
1487 tree folded = maybe_constant_init (init, TARGET_EXPR_SLOT (stmt),
1488 (data->flags & ff_mce_false
1489 ? mce_false : mce_unknown));
1490 if (folded != init && TREE_CONSTANT (folded))
1491 init = folded;
1492 }
1493
1494 /* This needs to happen between the constexpr evaluation (which wants
1495 pre-generic trees) and fold (which wants the cp_genericize_init
1496 transformations). */
1497 if (data->flags & ff_genericize)
1498 cp_genericize_target_expr (stmt_p);
1499
1500 if (tree &init = TARGET_EXPR_INITIAL (stmt))
1501 {
1502 cp_walk_tree (&init, cp_fold_r, data, NULL);
1503 cp_walk_tree (&TARGET_EXPR_CLEANUP (stmt), cp_fold_r, data, NULL);
1504 *walk_subtrees = 0;
1505 /* Folding might replace e.g. a COND_EXPR with a TARGET_EXPR; in
1506 that case, strip it in favor of this one. */
1507 if (TREE_CODE (init) == TARGET_EXPR)
1508 {
1509 tree sub = TARGET_EXPR_INITIAL (init);
1510 maybe_replace_decl (&sub, TARGET_EXPR_SLOT (init),
1511 TARGET_EXPR_SLOT (stmt));
1512 init = sub;
1513 }
1514 }
1515 break;
1516
1517 default:
1518 break;
1519 }
1520
1521 return NULL_TREE;
1522 }
1523
1524 /* Fold ALL the trees! FIXME we should be able to remove this, but
1525 apparently that still causes optimization regressions. */
1526
1527 void
1528 cp_fold_function (tree fndecl)
1529 {
1530 /* By now all manifestly-constant-evaluated expressions will have
1531 been constant-evaluated already if possible, so we can safely
1532 pass ff_mce_false. */
1533 cp_fold_data data (ff_genericize | ff_mce_false);
1534 /* Do cp_fold_immediate_r in separate whole IL walk instead of during
1535 cp_fold_r, as otherwise expressions using results of immediate functions
1536 might not be folded as cp_fold is called on those before cp_fold_r is
1537 called on their argument. */
1538 if (cxx_dialect >= cxx20)
1539 {
1540 cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_immediate_r,
1541 &data, NULL);
1542 data.pset.empty ();
1543 }
1544 cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_r, &data, NULL);
1545
1546 /* This is merely an optimization: if FNDECL has no i-e expressions,
1547 we'll not save c_f_d, and we can safely say that FNDECL will not
1548 be promoted to consteval. */
1549 if (deferred_escalating_exprs
1550 && !deferred_escalating_exprs->contains (current_function_decl))
1551 DECL_ESCALATION_CHECKED_P (fndecl) = true;
1552 }
1553
1554 /* We've stashed immediate-escalating functions. Now see if they indeed
1555 ought to be promoted to consteval. */
1556
1557 void
1558 process_and_check_pending_immediate_escalating_fns ()
1559 {
1560 /* This will be null for -fno-immediate-escalation. */
1561 if (!deferred_escalating_exprs)
1562 return;
1563
1564 for (auto e : *deferred_escalating_exprs)
1565 if (TREE_CODE (e) == FUNCTION_DECL && !DECL_ESCALATION_CHECKED_P (e))
1566 cp_fold_immediate (&DECL_SAVED_TREE (e), mce_false, e);
1567
1568 /* We've escalated every function that could have been promoted to
1569 consteval. Check that we are not taking the address of a consteval
1570 function. */
1571 for (auto e : *deferred_escalating_exprs)
1572 {
1573 if (TREE_CODE (e) == FUNCTION_DECL)
1574 continue;
1575 tree decl = (TREE_CODE (e) == PTRMEM_CST
1576 ? PTRMEM_CST_MEMBER (e)
1577 : TREE_OPERAND (e, 0));
1578 if (DECL_IMMEDIATE_FUNCTION_P (decl))
1579 taking_address_of_imm_fn_error (e, decl);
1580 }
1581
1582 deferred_escalating_exprs = nullptr;
1583 }
1584
1585 /* Turn SPACESHIP_EXPR EXPR into GENERIC. */
1586
1587 static tree genericize_spaceship (tree expr)
1588 {
1589 iloc_sentinel s (cp_expr_location (expr));
1590 tree type = TREE_TYPE (expr);
1591 tree op0 = TREE_OPERAND (expr, 0);
1592 tree op1 = TREE_OPERAND (expr, 1);
1593 return genericize_spaceship (input_location, type, op0, op1);
1594 }
1595
1596 /* If EXPR involves an anonymous VLA type, prepend a DECL_EXPR for that type
1597 to trigger gimplify_type_sizes; otherwise a cast to pointer-to-VLA confuses
1598 the middle-end (c++/88256). If EXPR is a DECL, use add_stmt and return
1599 NULL_TREE; otherwise return a COMPOUND_STMT of the DECL_EXPR and EXPR. */
1600
1601 tree
1602 predeclare_vla (tree expr)
1603 {
1604 tree type = TREE_TYPE (expr);
1605 if (type == error_mark_node)
1606 return expr;
1607 if (is_typedef_decl (expr))
1608 type = DECL_ORIGINAL_TYPE (expr);
1609
1610 /* We need to strip pointers for gimplify_type_sizes. */
1611 tree vla = type;
1612 while (POINTER_TYPE_P (vla))
1613 {
1614 if (TYPE_NAME (vla))
1615 return expr;
1616 vla = TREE_TYPE (vla);
1617 }
1618 if (vla == type || TYPE_NAME (vla)
1619 || !variably_modified_type_p (vla, NULL_TREE))
1620 return expr;
1621
1622 tree decl = build_decl (input_location, TYPE_DECL, NULL_TREE, vla);
1623 DECL_ARTIFICIAL (decl) = 1;
1624 TYPE_NAME (vla) = decl;
1625 tree dexp = build_stmt (input_location, DECL_EXPR, decl);
1626 if (DECL_P (expr))
1627 {
1628 add_stmt (dexp);
1629 return NULL_TREE;
1630 }
1631 else
1632 {
1633 expr = build2 (COMPOUND_EXPR, type, dexp, expr);
1634 return expr;
1635 }
1636 }
1637
1638 /* Perform any pre-gimplification lowering of C++ front end trees to
1639 GENERIC. */
1640
1641 static tree
1642 cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
1643 {
1644 tree stmt = *stmt_p;
1645 struct cp_genericize_data *wtd = (struct cp_genericize_data *) data;
1646 hash_set<tree> *p_set = wtd->p_set;
1647
1648 /* If in an OpenMP context, note var uses. */
1649 if (UNLIKELY (wtd->omp_ctx != NULL)
1650 && (VAR_P (stmt)
1651 || TREE_CODE (stmt) == PARM_DECL
1652 || TREE_CODE (stmt) == RESULT_DECL)
1653 && omp_var_to_track (stmt))
1654 omp_cxx_notice_variable (wtd->omp_ctx, stmt);
1655
1656 /* Don't dereference parms in a thunk, pass the references through. */
1657 if ((TREE_CODE (stmt) == CALL_EXPR && call_from_lambda_thunk_p (stmt))
1658 || (TREE_CODE (stmt) == AGGR_INIT_EXPR && AGGR_INIT_FROM_THUNK_P (stmt)))
1659 {
1660 *walk_subtrees = 0;
1661 return NULL;
1662 }
1663
1664 /* Dereference invisible reference parms. */
1665 if (wtd->handle_invisiref_parm_p && is_invisiref_parm (stmt))
1666 {
1667 *stmt_p = convert_from_reference (stmt);
1668 p_set->add (*stmt_p);
1669 *walk_subtrees = 0;
1670 return NULL;
1671 }
1672
1673 /* Map block scope extern declarations to visible declarations with the
1674 same name and type in outer scopes if any. */
1675 if (VAR_OR_FUNCTION_DECL_P (stmt) && DECL_LOCAL_DECL_P (stmt))
1676 if (tree alias = DECL_LOCAL_DECL_ALIAS (stmt))
1677 {
1678 if (alias != error_mark_node)
1679 {
1680 *stmt_p = alias;
1681 TREE_USED (alias) |= TREE_USED (stmt);
1682 }
1683 *walk_subtrees = 0;
1684 return NULL;
1685 }
1686
1687 if (TREE_CODE (stmt) == INTEGER_CST
1688 && TYPE_REF_P (TREE_TYPE (stmt))
1689 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))
1690 && !wtd->no_sanitize_p)
1691 {
1692 ubsan_maybe_instrument_reference (stmt_p);
1693 if (*stmt_p != stmt)
1694 {
1695 *walk_subtrees = 0;
1696 return NULL_TREE;
1697 }
1698 }
1699
1700 /* Other than invisiref parms, don't walk the same tree twice. */
1701 if (p_set->contains (stmt))
1702 {
1703 *walk_subtrees = 0;
1704 return NULL_TREE;
1705 }
1706
1707 switch (TREE_CODE (stmt))
1708 {
1709 case ADDR_EXPR:
1710 if (is_invisiref_parm (TREE_OPERAND (stmt, 0)))
1711 {
1712 /* If in an OpenMP context, note var uses. */
1713 if (UNLIKELY (wtd->omp_ctx != NULL)
1714 && omp_var_to_track (TREE_OPERAND (stmt, 0)))
1715 omp_cxx_notice_variable (wtd->omp_ctx, TREE_OPERAND (stmt, 0));
1716 *stmt_p = fold_convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
1717 *walk_subtrees = 0;
1718 }
1719 break;
1720
1721 case RETURN_EXPR:
1722 if (TREE_OPERAND (stmt, 0))
1723 {
1724 if (error_operand_p (TREE_OPERAND (stmt, 0))
1725 && warn_return_type)
1726 /* Suppress -Wreturn-type for this function. */
1727 suppress_warning (current_function_decl, OPT_Wreturn_type);
1728
1729 if (is_invisiref_parm (TREE_OPERAND (stmt, 0)))
1730 /* Don't dereference an invisiref RESULT_DECL inside a
1731 RETURN_EXPR. */
1732 *walk_subtrees = 0;
1733 if (RETURN_EXPR_LOCAL_ADDR_P (stmt))
1734 {
1735 /* Don't return the address of a local variable. */
1736 tree *p = &TREE_OPERAND (stmt, 0);
1737 while (TREE_CODE (*p) == COMPOUND_EXPR)
1738 p = &TREE_OPERAND (*p, 0);
1739 if (TREE_CODE (*p) == INIT_EXPR)
1740 {
1741 tree op = TREE_OPERAND (*p, 1);
1742 tree new_op = build2 (COMPOUND_EXPR, TREE_TYPE (op), op,
1743 build_zero_cst (TREE_TYPE (op)));
1744 TREE_OPERAND (*p, 1) = new_op;
1745 }
1746 }
1747 }
1748 break;
1749
1750 case OMP_CLAUSE:
1751 switch (OMP_CLAUSE_CODE (stmt))
1752 {
1753 case OMP_CLAUSE_LASTPRIVATE:
1754 /* Don't dereference an invisiref in OpenMP clauses. */
1755 if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1756 {
1757 *walk_subtrees = 0;
1758 if (OMP_CLAUSE_LASTPRIVATE_STMT (stmt))
1759 cp_walk_tree (&OMP_CLAUSE_LASTPRIVATE_STMT (stmt),
1760 cp_genericize_r, data, NULL);
1761 }
1762 break;
1763 case OMP_CLAUSE_PRIVATE:
1764 /* Don't dereference an invisiref in OpenMP clauses. */
1765 if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1766 *walk_subtrees = 0;
1767 else if (wtd->omp_ctx != NULL)
1768 {
1769 /* Private clause doesn't cause any references to the
1770 var in outer contexts, avoid calling
1771 omp_cxx_notice_variable for it. */
1772 struct cp_genericize_omp_taskreg *old = wtd->omp_ctx;
1773 wtd->omp_ctx = NULL;
1774 cp_walk_tree (&OMP_CLAUSE_DECL (stmt), cp_genericize_r,
1775 data, NULL);
1776 wtd->omp_ctx = old;
1777 *walk_subtrees = 0;
1778 }
1779 break;
1780 case OMP_CLAUSE_SHARED:
1781 case OMP_CLAUSE_FIRSTPRIVATE:
1782 case OMP_CLAUSE_COPYIN:
1783 case OMP_CLAUSE_COPYPRIVATE:
1784 case OMP_CLAUSE_INCLUSIVE:
1785 case OMP_CLAUSE_EXCLUSIVE:
1786 /* Don't dereference an invisiref in OpenMP clauses. */
1787 if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1788 *walk_subtrees = 0;
1789 break;
1790 case OMP_CLAUSE_REDUCTION:
1791 case OMP_CLAUSE_IN_REDUCTION:
1792 case OMP_CLAUSE_TASK_REDUCTION:
1793 /* Don't dereference an invisiref in reduction clause's
1794 OMP_CLAUSE_DECL either. OMP_CLAUSE_REDUCTION_{INIT,MERGE}
1795 still needs to be genericized. */
1796 if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
1797 {
1798 *walk_subtrees = 0;
1799 if (OMP_CLAUSE_REDUCTION_INIT (stmt))
1800 cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (stmt),
1801 cp_genericize_r, data, NULL);
1802 if (OMP_CLAUSE_REDUCTION_MERGE (stmt))
1803 cp_walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (stmt),
1804 cp_genericize_r, data, NULL);
1805 }
1806 break;
1807 default:
1808 break;
1809 }
1810 break;
1811
1812 /* Due to the way voidify_wrapper_expr is written, we don't get a chance
1813 to lower this construct before scanning it, so we need to lower these
1814 before doing anything else. */
1815 case CLEANUP_STMT:
1816 *stmt_p = build2_loc (EXPR_LOCATION (stmt),
1817 CLEANUP_EH_ONLY (stmt) ? TRY_CATCH_EXPR
1818 : TRY_FINALLY_EXPR,
1819 void_type_node,
1820 CLEANUP_BODY (stmt),
1821 CLEANUP_EXPR (stmt));
1822 break;
1823
1824 case IF_STMT:
1825 genericize_if_stmt (stmt_p);
1826 /* *stmt_p has changed, tail recurse to handle it again. */
1827 return cp_genericize_r (stmt_p, walk_subtrees, data);
1828
1829 /* COND_EXPR might have incompatible types in branches if one or both
1830 arms are bitfields. Fix it up now. */
1831 case COND_EXPR:
1832 {
1833 tree type_left
1834 = (TREE_OPERAND (stmt, 1)
1835 ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 1))
1836 : NULL_TREE);
1837 tree type_right
1838 = (TREE_OPERAND (stmt, 2)
1839 ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 2))
1840 : NULL_TREE);
1841 if (type_left
1842 && !useless_type_conversion_p (TREE_TYPE (stmt),
1843 TREE_TYPE (TREE_OPERAND (stmt, 1))))
1844 {
1845 TREE_OPERAND (stmt, 1)
1846 = fold_convert (type_left, TREE_OPERAND (stmt, 1));
1847 gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
1848 type_left));
1849 }
1850 if (type_right
1851 && !useless_type_conversion_p (TREE_TYPE (stmt),
1852 TREE_TYPE (TREE_OPERAND (stmt, 2))))
1853 {
1854 TREE_OPERAND (stmt, 2)
1855 = fold_convert (type_right, TREE_OPERAND (stmt, 2));
1856 gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
1857 type_right));
1858 }
1859 }
1860 break;
1861
1862 case BIND_EXPR:
1863 if (UNLIKELY (wtd->omp_ctx != NULL))
1864 {
1865 tree decl;
1866 for (decl = BIND_EXPR_VARS (stmt); decl; decl = DECL_CHAIN (decl))
1867 if (VAR_P (decl)
1868 && !DECL_EXTERNAL (decl)
1869 && omp_var_to_track (decl))
1870 {
1871 splay_tree_node n
1872 = splay_tree_lookup (wtd->omp_ctx->variables,
1873 (splay_tree_key) decl);
1874 if (n == NULL)
1875 splay_tree_insert (wtd->omp_ctx->variables,
1876 (splay_tree_key) decl,
1877 TREE_STATIC (decl)
1878 ? OMP_CLAUSE_DEFAULT_SHARED
1879 : OMP_CLAUSE_DEFAULT_PRIVATE);
1880 }
1881 }
1882 if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT | SANITIZE_VPTR))
1883 {
1884 /* The point here is to not sanitize static initializers. */
1885 bool no_sanitize_p = wtd->no_sanitize_p;
1886 wtd->no_sanitize_p = true;
1887 for (tree decl = BIND_EXPR_VARS (stmt);
1888 decl;
1889 decl = DECL_CHAIN (decl))
1890 if (VAR_P (decl)
1891 && TREE_STATIC (decl)
1892 && DECL_INITIAL (decl))
1893 cp_walk_tree (&DECL_INITIAL (decl), cp_genericize_r, data, NULL);
1894 wtd->no_sanitize_p = no_sanitize_p;
1895 }
1896 wtd->bind_expr_stack.safe_push (stmt);
1897 cp_walk_tree (&BIND_EXPR_BODY (stmt),
1898 cp_genericize_r, data, NULL);
1899 wtd->bind_expr_stack.pop ();
1900 break;
1901
1902 case ASSERTION_STMT:
1903 case PRECONDITION_STMT:
1904 case POSTCONDITION_STMT:
1905 {
1906 if (tree check = build_contract_check (stmt))
1907 {
1908 *stmt_p = check;
1909 return cp_genericize_r (stmt_p, walk_subtrees, data);
1910 }
1911
1912 /* If we didn't build a check, replace it with void_node so we don't
1913 leak contracts into GENERIC. */
1914 *stmt_p = void_node;
1915 *walk_subtrees = 0;
1916 }
1917 break;
1918
1919 case USING_STMT:
1920 {
1921 tree block = NULL_TREE;
1922
1923 /* Get the innermost inclosing GIMPLE_BIND that has a non NULL
1924 BLOCK, and append an IMPORTED_DECL to its
1925 BLOCK_VARS chained list. */
1926 if (wtd->bind_expr_stack.exists ())
1927 {
1928 int i;
1929 for (i = wtd->bind_expr_stack.length () - 1; i >= 0; i--)
1930 if ((block = BIND_EXPR_BLOCK (wtd->bind_expr_stack[i])))
1931 break;
1932 }
1933 if (block)
1934 {
1935 tree decl = TREE_OPERAND (stmt, 0);
1936 gcc_assert (decl);
1937
1938 if (undeduced_auto_decl (decl))
1939 /* Omit from the GENERIC, the back-end can't handle it. */;
1940 else
1941 {
1942 tree using_directive = make_node (IMPORTED_DECL);
1943 TREE_TYPE (using_directive) = void_type_node;
1944 DECL_CONTEXT (using_directive) = current_function_decl;
1945 DECL_SOURCE_LOCATION (using_directive)
1946 = cp_expr_loc_or_input_loc (stmt);
1947
1948 IMPORTED_DECL_ASSOCIATED_DECL (using_directive) = decl;
1949 DECL_CHAIN (using_directive) = BLOCK_VARS (block);
1950 BLOCK_VARS (block) = using_directive;
1951 }
1952 }
1953 /* The USING_STMT won't appear in GENERIC. */
1954 *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1955 *walk_subtrees = 0;
1956 }
1957 break;
1958
1959 case DECL_EXPR:
1960 if (TREE_CODE (DECL_EXPR_DECL (stmt)) == USING_DECL)
1961 {
1962 /* Using decls inside DECL_EXPRs are just dropped on the floor. */
1963 *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1964 *walk_subtrees = 0;
1965 }
1966 else
1967 {
1968 tree d = DECL_EXPR_DECL (stmt);
1969 if (VAR_P (d))
1970 gcc_assert (CP_DECL_THREAD_LOCAL_P (d) == DECL_THREAD_LOCAL_P (d));
1971 }
1972 break;
1973
1974 case OMP_PARALLEL:
1975 case OMP_TASK:
1976 case OMP_TASKLOOP:
1977 {
1978 struct cp_genericize_omp_taskreg omp_ctx;
1979 tree c, decl;
1980 splay_tree_node n;
1981
1982 *walk_subtrees = 0;
1983 cp_walk_tree (&OMP_CLAUSES (stmt), cp_genericize_r, data, NULL);
1984 omp_ctx.is_parallel = TREE_CODE (stmt) == OMP_PARALLEL;
1985 omp_ctx.default_shared = omp_ctx.is_parallel;
1986 omp_ctx.outer = wtd->omp_ctx;
1987 omp_ctx.variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
1988 wtd->omp_ctx = &omp_ctx;
1989 for (c = OMP_CLAUSES (stmt); c; c = OMP_CLAUSE_CHAIN (c))
1990 switch (OMP_CLAUSE_CODE (c))
1991 {
1992 case OMP_CLAUSE_SHARED:
1993 case OMP_CLAUSE_PRIVATE:
1994 case OMP_CLAUSE_FIRSTPRIVATE:
1995 case OMP_CLAUSE_LASTPRIVATE:
1996 decl = OMP_CLAUSE_DECL (c);
1997 if (decl == error_mark_node || !omp_var_to_track (decl))
1998 break;
1999 n = splay_tree_lookup (omp_ctx.variables, (splay_tree_key) decl);
2000 if (n != NULL)
2001 break;
2002 splay_tree_insert (omp_ctx.variables, (splay_tree_key) decl,
2003 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
2004 ? OMP_CLAUSE_DEFAULT_SHARED
2005 : OMP_CLAUSE_DEFAULT_PRIVATE);
2006 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE && omp_ctx.outer)
2007 omp_cxx_notice_variable (omp_ctx.outer, decl);
2008 break;
2009 case OMP_CLAUSE_DEFAULT:
2010 if (OMP_CLAUSE_DEFAULT_KIND (c) == OMP_CLAUSE_DEFAULT_SHARED)
2011 omp_ctx.default_shared = true;
2012 default:
2013 break;
2014 }
2015 if (TREE_CODE (stmt) == OMP_TASKLOOP)
2016 c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2017 cp_genericize_r, cp_walk_subtrees);
2018 else
2019 cp_walk_tree (&OMP_BODY (stmt), cp_genericize_r, data, NULL);
2020 wtd->omp_ctx = omp_ctx.outer;
2021 splay_tree_delete (omp_ctx.variables);
2022 }
2023 break;
2024
2025 case OMP_TARGET:
2026 cfun->has_omp_target = true;
2027 break;
2028
2029 case TRY_BLOCK:
2030 {
2031 *walk_subtrees = 0;
2032 tree try_block = wtd->try_block;
2033 wtd->try_block = stmt;
2034 cp_walk_tree (&TRY_STMTS (stmt), cp_genericize_r, data, NULL);
2035 wtd->try_block = try_block;
2036 cp_walk_tree (&TRY_HANDLERS (stmt), cp_genericize_r, data, NULL);
2037 }
2038 break;
2039
2040 case MUST_NOT_THROW_EXPR:
2041 /* MUST_NOT_THROW_COND might be something else with TM. */
2042 if (MUST_NOT_THROW_COND (stmt) == NULL_TREE)
2043 {
2044 *walk_subtrees = 0;
2045 tree try_block = wtd->try_block;
2046 wtd->try_block = stmt;
2047 cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_genericize_r, data, NULL);
2048 wtd->try_block = try_block;
2049 }
2050 break;
2051
2052 case THROW_EXPR:
2053 {
2054 location_t loc = location_of (stmt);
2055 if (warning_suppressed_p (stmt /* What warning? */))
2056 /* Never mind. */;
2057 else if (wtd->try_block)
2058 {
2059 if (TREE_CODE (wtd->try_block) == MUST_NOT_THROW_EXPR)
2060 {
2061 auto_diagnostic_group d;
2062 if (warning_at (loc, OPT_Wterminate,
2063 "%<throw%> will always call %<terminate%>")
2064 && cxx_dialect >= cxx11
2065 && DECL_DESTRUCTOR_P (current_function_decl))
2066 inform (loc, "in C++11 destructors default to %<noexcept%>");
2067 }
2068 }
2069 else
2070 {
2071 if (warn_cxx11_compat && cxx_dialect < cxx11
2072 && DECL_DESTRUCTOR_P (current_function_decl)
2073 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))
2074 == NULL_TREE)
2075 && (get_defaulted_eh_spec (current_function_decl)
2076 == empty_except_spec))
2077 warning_at (loc, OPT_Wc__11_compat,
2078 "in C++11 this %<throw%> will call %<terminate%> "
2079 "because destructors default to %<noexcept%>");
2080 }
2081 }
2082 break;
2083
2084 case CONVERT_EXPR:
2085 gcc_checking_assert (!AGGREGATE_TYPE_P (TREE_TYPE (stmt)));
2086 gcc_assert (!CONVERT_EXPR_VBASE_PATH (stmt));
2087 break;
2088
2089 case SPACESHIP_EXPR:
2090 *stmt_p = genericize_spaceship (*stmt_p);
2091 break;
2092
2093 case PTRMEM_CST:
2094 /* By the time we get here we're handing off to the back end, so we don't
2095 need or want to preserve PTRMEM_CST anymore. */
2096 *stmt_p = cplus_expand_constant (stmt);
2097 *walk_subtrees = 0;
2098 break;
2099
2100 case MEM_REF:
2101 /* For MEM_REF, make sure not to sanitize the second operand even
2102 if it has reference type. It is just an offset with a type
2103 holding other information. There is no other processing we
2104 need to do for INTEGER_CSTs, so just ignore the second argument
2105 unconditionally. */
2106 cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_genericize_r, data, NULL);
2107 *walk_subtrees = 0;
2108 break;
2109
2110 case NOP_EXPR:
2111 *stmt_p = predeclare_vla (*stmt_p);
2112
2113 /* Warn of new allocations that are not big enough for the target
2114 type. */
2115 if (warn_alloc_size
2116 && TREE_CODE (TREE_OPERAND (stmt, 0)) == CALL_EXPR
2117 && POINTER_TYPE_P (TREE_TYPE (stmt)))
2118 {
2119 if (tree fndecl = get_callee_fndecl (TREE_OPERAND (stmt, 0)))
2120 if (DECL_IS_MALLOC (fndecl))
2121 {
2122 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
2123 tree alloc_size = lookup_attribute ("alloc_size", attrs);
2124 if (alloc_size)
2125 warn_for_alloc_size (EXPR_LOCATION (stmt),
2126 TREE_TYPE (TREE_TYPE (stmt)),
2127 TREE_OPERAND (stmt, 0), alloc_size);
2128 }
2129 }
2130
2131 if (!wtd->no_sanitize_p
2132 && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT)
2133 && TYPE_REF_P (TREE_TYPE (stmt)))
2134 ubsan_maybe_instrument_reference (stmt_p);
2135 break;
2136
2137 case CALL_EXPR:
2138 if (!wtd->no_sanitize_p
2139 && sanitize_flags_p ((SANITIZE_NULL
2140 | SANITIZE_ALIGNMENT | SANITIZE_VPTR)))
2141 {
2142 tree fn = CALL_EXPR_FN (stmt);
2143 if (fn != NULL_TREE
2144 && !error_operand_p (fn)
2145 && INDIRECT_TYPE_P (TREE_TYPE (fn))
2146 && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) == METHOD_TYPE)
2147 {
2148 bool is_ctor
2149 = TREE_CODE (fn) == ADDR_EXPR
2150 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
2151 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0));
2152 if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT))
2153 ubsan_maybe_instrument_member_call (stmt, is_ctor);
2154 if (sanitize_flags_p (SANITIZE_VPTR) && !is_ctor)
2155 cp_ubsan_maybe_instrument_member_call (stmt);
2156 }
2157 else if (fn == NULL_TREE
2158 && CALL_EXPR_IFN (stmt) == IFN_UBSAN_NULL
2159 && TREE_CODE (CALL_EXPR_ARG (stmt, 0)) == INTEGER_CST
2160 && TYPE_REF_P (TREE_TYPE (CALL_EXPR_ARG (stmt, 0))))
2161 *walk_subtrees = 0;
2162 }
2163 /* Fall through. */
2164 case AGGR_INIT_EXPR:
2165 /* For calls to a multi-versioned function, overload resolution
2166 returns the function with the highest target priority, that is,
2167 the version that will checked for dispatching first. If this
2168 version is inlinable, a direct call to this version can be made
2169 otherwise the call should go through the dispatcher. */
2170 {
2171 tree fn = cp_get_callee_fndecl_nofold (stmt);
2172 if (fn && DECL_FUNCTION_VERSIONED (fn)
2173 && (current_function_decl == NULL
2174 || !targetm.target_option.can_inline_p (current_function_decl,
2175 fn)))
2176 if (tree dis = get_function_version_dispatcher (fn))
2177 {
2178 mark_versions_used (dis);
2179 dis = build_address (dis);
2180 if (TREE_CODE (stmt) == CALL_EXPR)
2181 CALL_EXPR_FN (stmt) = dis;
2182 else
2183 AGGR_INIT_EXPR_FN (stmt) = dis;
2184 }
2185 }
2186 break;
2187
2188 case TARGET_EXPR:
2189 if (TARGET_EXPR_INITIAL (stmt)
2190 && TREE_CODE (TARGET_EXPR_INITIAL (stmt)) == CONSTRUCTOR
2191 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (TARGET_EXPR_INITIAL (stmt)))
2192 TARGET_EXPR_NO_ELIDE (stmt) = 1;
2193 break;
2194
2195 case TEMPLATE_ID_EXPR:
2196 gcc_assert (concept_check_p (stmt));
2197 /* Emit the value of the concept check. */
2198 *stmt_p = evaluate_concept_check (stmt);
2199 walk_subtrees = 0;
2200 break;
2201
2202 case OMP_DISTRIBUTE:
2203 /* Need to explicitly instantiate copy ctors on class iterators of
2204 composite distribute parallel for. */
2205 if (OMP_FOR_INIT (*stmt_p) == NULL_TREE)
2206 {
2207 tree *data[4] = { NULL, NULL, NULL, NULL };
2208 tree inner = walk_tree (&OMP_FOR_BODY (*stmt_p),
2209 find_combined_omp_for, data, NULL);
2210 if (inner != NULL_TREE
2211 && TREE_CODE (inner) == OMP_FOR)
2212 {
2213 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner)); i++)
2214 if (TREE_VEC_ELT (OMP_FOR_INIT (inner), i)
2215 && OMP_FOR_ORIG_DECLS (inner)
2216 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner),
2217 i)) == TREE_LIST
2218 && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner),
2219 i)))
2220 {
2221 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner), i);
2222 /* Class iterators aren't allowed on OMP_SIMD, so the only
2223 case we need to solve is distribute parallel for. */
2224 gcc_assert (TREE_CODE (inner) == OMP_FOR
2225 && data[1]);
2226 tree orig_decl = TREE_PURPOSE (orig);
2227 tree c, cl = NULL_TREE;
2228 for (c = OMP_FOR_CLAUSES (inner);
2229 c; c = OMP_CLAUSE_CHAIN (c))
2230 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
2231 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
2232 && OMP_CLAUSE_DECL (c) == orig_decl)
2233 {
2234 cl = c;
2235 break;
2236 }
2237 if (cl == NULL_TREE)
2238 {
2239 for (c = OMP_PARALLEL_CLAUSES (*data[1]);
2240 c; c = OMP_CLAUSE_CHAIN (c))
2241 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
2242 && OMP_CLAUSE_DECL (c) == orig_decl)
2243 {
2244 cl = c;
2245 break;
2246 }
2247 }
2248 if (cl)
2249 {
2250 orig_decl = require_complete_type (orig_decl);
2251 tree inner_type = TREE_TYPE (orig_decl);
2252 if (orig_decl == error_mark_node)
2253 continue;
2254 if (TYPE_REF_P (TREE_TYPE (orig_decl)))
2255 inner_type = TREE_TYPE (inner_type);
2256
2257 while (TREE_CODE (inner_type) == ARRAY_TYPE)
2258 inner_type = TREE_TYPE (inner_type);
2259 get_copy_ctor (inner_type, tf_warning_or_error);
2260 }
2261 }
2262 }
2263 }
2264 /* FALLTHRU */
2265
2266 case FOR_STMT:
2267 case WHILE_STMT:
2268 case DO_STMT:
2269 case SWITCH_STMT:
2270 case CONTINUE_STMT:
2271 case BREAK_STMT:
2272 case OMP_FOR:
2273 case OMP_SIMD:
2274 case OMP_LOOP:
2275 case OMP_TILE:
2276 case OMP_UNROLL:
2277 case OACC_LOOP:
2278 case STATEMENT_LIST:
2279 /* These cases are handled by shared code. */
2280 c_genericize_control_stmt (stmt_p, walk_subtrees, data,
2281 cp_genericize_r, cp_walk_subtrees);
2282 break;
2283
2284 case BIT_CAST_EXPR:
2285 *stmt_p = build1_loc (EXPR_LOCATION (stmt), VIEW_CONVERT_EXPR,
2286 TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
2287 break;
2288
2289 case MODIFY_EXPR:
2290 /* Mark stores to parts of complex automatic non-addressable
2291 variables as DECL_NOT_GIMPLE_REG_P for -O0. This can't be
2292 done during gimplification. See PR119120. */
2293 if ((TREE_CODE (TREE_OPERAND (stmt, 0)) == REALPART_EXPR
2294 || TREE_CODE (TREE_OPERAND (stmt, 0)) == IMAGPART_EXPR)
2295 && !optimize
2296 && DECL_P (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0))
2297 && is_gimple_reg (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0)))
2298 DECL_NOT_GIMPLE_REG_P (TREE_OPERAND (TREE_OPERAND (stmt, 0), 0)) = 1;
2299 break;
2300
2301 default:
2302 if (IS_TYPE_OR_DECL_P (stmt))
2303 *walk_subtrees = 0;
2304 break;
2305 }
2306
2307 p_set->add (*stmt_p);
2308
2309 return NULL;
2310 }
2311
2312 /* Lower C++ front end trees to GENERIC in T_P. */
2313
2314 static void
2315 cp_genericize_tree (tree* t_p, bool handle_invisiref_parm_p)
2316 {
2317 struct cp_genericize_data wtd;
2318
2319 wtd.p_set = new hash_set<tree>;
2320 wtd.bind_expr_stack.create (0);
2321 wtd.omp_ctx = NULL;
2322 wtd.try_block = NULL_TREE;
2323 wtd.no_sanitize_p = false;
2324 wtd.handle_invisiref_parm_p = handle_invisiref_parm_p;
2325 cp_walk_tree (t_p, cp_genericize_r, &wtd, NULL);
2326 delete wtd.p_set;
2327 if (sanitize_flags_p (SANITIZE_VPTR))
2328 cp_ubsan_instrument_member_accesses (t_p);
2329 }
2330
2331 /* If a function that should end with a return in non-void
2332 function doesn't obviously end with return, add ubsan
2333 instrumentation code to verify it at runtime. If -fsanitize=return
2334 is not enabled, instrument __builtin_unreachable. */
2335
2336 static void
2337 cp_maybe_instrument_return (tree fndecl)
2338 {
2339 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
2340 || DECL_CONSTRUCTOR_P (fndecl)
2341 || DECL_DESTRUCTOR_P (fndecl)
2342 || !targetm.warn_func_return (fndecl))
2343 return;
2344
2345 if (!sanitize_flags_p (SANITIZE_RETURN, fndecl)
2346 /* Don't add __builtin_unreachable () if not optimizing, it will not
2347 improve any optimizations in that case, just break UB code.
2348 Don't add it if -fsanitize=unreachable -fno-sanitize=return either,
2349 UBSan covers this with ubsan_instrument_return above where sufficient
2350 information is provided, while the __builtin_unreachable () below
2351 if return sanitization is disabled will just result in hard to
2352 understand runtime error without location. */
2353 && ((!optimize && !flag_unreachable_traps)
2354 || sanitize_flags_p (SANITIZE_UNREACHABLE, fndecl)))
2355 return;
2356
2357 tree t = DECL_SAVED_TREE (fndecl);
2358 while (t)
2359 {
2360 switch (TREE_CODE (t))
2361 {
2362 case BIND_EXPR:
2363 t = BIND_EXPR_BODY (t);
2364 continue;
2365 case TRY_FINALLY_EXPR:
2366 case CLEANUP_POINT_EXPR:
2367 t = TREE_OPERAND (t, 0);
2368 continue;
2369 case STATEMENT_LIST:
2370 {
2371 tree_stmt_iterator i = tsi_last (t);
2372 while (!tsi_end_p (i))
2373 {
2374 tree p = tsi_stmt (i);
2375 if (TREE_CODE (p) != DEBUG_BEGIN_STMT)
2376 break;
2377 tsi_prev (&i);
2378 }
2379 if (!tsi_end_p (i))
2380 {
2381 t = tsi_stmt (i);
2382 continue;
2383 }
2384 }
2385 break;
2386 case RETURN_EXPR:
2387 return;
2388 default:
2389 break;
2390 }
2391 break;
2392 }
2393 if (t == NULL_TREE)
2394 return;
2395 tree *p = &DECL_SAVED_TREE (fndecl);
2396 if (TREE_CODE (*p) == BIND_EXPR)
2397 p = &BIND_EXPR_BODY (*p);
2398
2399 location_t loc = DECL_SOURCE_LOCATION (fndecl);
2400 if (sanitize_flags_p (SANITIZE_RETURN, fndecl))
2401 t = ubsan_instrument_return (loc);
2402 else
2403 t = build_builtin_unreachable (BUILTINS_LOCATION);
2404
2405 append_to_statement_list (t, p);
2406 }
2407
2408 void
2409 cp_genericize (tree fndecl)
2410 {
2411 tree t;
2412
2413 /* Fix up the types of parms passed by invisible reference. */
2414 for (t = DECL_ARGUMENTS (fndecl); t; t = DECL_CHAIN (t))
2415 if (TREE_ADDRESSABLE (TREE_TYPE (t)))
2416 {
2417 /* If a function's arguments are copied to create a thunk,
2418 then DECL_BY_REFERENCE will be set -- but the type of the
2419 argument will be a pointer type, so we will never get
2420 here. */
2421 gcc_assert (!DECL_BY_REFERENCE (t));
2422 gcc_assert (DECL_ARG_TYPE (t) != TREE_TYPE (t));
2423 TREE_TYPE (t) = DECL_ARG_TYPE (t);
2424 DECL_BY_REFERENCE (t) = 1;
2425 TREE_ADDRESSABLE (t) = 0;
2426 relayout_decl (t);
2427 }
2428
2429 /* Do the same for the return value. */
2430 if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (fndecl))))
2431 {
2432 t = DECL_RESULT (fndecl);
2433 TREE_TYPE (t) = build_reference_type (TREE_TYPE (t));
2434 DECL_BY_REFERENCE (t) = 1;
2435 TREE_ADDRESSABLE (t) = 0;
2436 relayout_decl (t);
2437 if (DECL_NAME (t))
2438 {
2439 /* Adjust DECL_VALUE_EXPR of the original var. */
2440 tree outer = outer_curly_brace_block (current_function_decl);
2441 tree var;
2442
2443 if (outer)
2444 for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
2445 if (VAR_P (var)
2446 && DECL_NAME (t) == DECL_NAME (var)
2447 && DECL_HAS_VALUE_EXPR_P (var)
2448 && DECL_VALUE_EXPR (var) == t)
2449 {
2450 tree val = convert_from_reference (t);
2451 SET_DECL_VALUE_EXPR (var, val);
2452 break;
2453 }
2454 }
2455 }
2456
2457 /* If we're a clone, the body is already GIMPLE. */
2458 if (DECL_CLONED_FUNCTION_P (fndecl))
2459 return;
2460
2461 /* Allow cp_genericize calls to be nested. */
2462 bc_state_t save_state;
2463 save_bc_state (&save_state);
2464
2465 /* We do want to see every occurrence of the parms, so we can't just use
2466 walk_tree's hash functionality. */
2467 cp_genericize_tree (&DECL_SAVED_TREE (fndecl), true);
2468
2469 cp_maybe_instrument_return (fndecl);
2470
2471 /* Do everything else. */
2472 c_genericize (fndecl);
2473 restore_bc_state (&save_state);
2474 }
2475
2476 /* Build code to apply FN to each member of ARG1 and ARG2. FN may be
2477 NULL if there is in fact nothing to do. ARG2 may be null if FN
2478 actually only takes one argument. */
2479
2480 static tree
2481 cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
2482 {
2483 tree defparm, parm, t;
2484 int i = 0;
2485 int nargs;
2486 tree *argarray;
2487
2488 if (fn == NULL)
2489 return NULL;
2490
2491 nargs = list_length (DECL_ARGUMENTS (fn));
2492 argarray = XALLOCAVEC (tree, nargs);
2493
2494 defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
2495 if (arg2)
2496 defparm = TREE_CHAIN (defparm);
2497
2498 bool is_method = TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE;
2499 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2500 {
2501 tree inner_type = TREE_TYPE (arg1);
2502 tree start1, end1, p1;
2503 tree start2 = NULL, p2 = NULL;
2504 tree ret = NULL, lab;
2505
2506 start1 = arg1;
2507 start2 = arg2;
2508 do
2509 {
2510 inner_type = TREE_TYPE (inner_type);
2511 start1 = build4 (ARRAY_REF, inner_type, start1,
2512 size_zero_node, NULL, NULL);
2513 if (arg2)
2514 start2 = build4 (ARRAY_REF, inner_type, start2,
2515 size_zero_node, NULL, NULL);
2516 }
2517 while (TREE_CODE (inner_type) == ARRAY_TYPE);
2518 start1 = build_fold_addr_expr_loc (input_location, start1);
2519 if (arg2)
2520 start2 = build_fold_addr_expr_loc (input_location, start2);
2521
2522 end1 = TYPE_SIZE_UNIT (TREE_TYPE (arg1));
2523 end1 = fold_build_pointer_plus (start1, end1);
2524
2525 p1 = create_tmp_var (TREE_TYPE (start1));
2526 t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, start1);
2527 append_to_statement_list (t, &ret);
2528
2529 if (arg2)
2530 {
2531 p2 = create_tmp_var (TREE_TYPE (start2));
2532 t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, start2);
2533 append_to_statement_list (t, &ret);
2534 }
2535
2536 lab = create_artificial_label (input_location);
2537 t = build1 (LABEL_EXPR, void_type_node, lab);
2538 append_to_statement_list (t, &ret);
2539
2540 argarray[i++] = p1;
2541 if (arg2)
2542 argarray[i++] = p2;
2543 /* Handle default arguments. */
2544 for (parm = defparm; parm && parm != void_list_node;
2545 parm = TREE_CHAIN (parm), i++)
2546 argarray[i] = convert_default_arg (TREE_VALUE (parm),
2547 TREE_PURPOSE (parm), fn,
2548 i - is_method, tf_warning_or_error);
2549 t = build_call_a (fn, i, argarray);
2550 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
2551 t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
2552 t = fold_convert (void_type_node, t);
2553 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
2554 append_to_statement_list (t, &ret);
2555
2556 t = fold_build_pointer_plus (p1, TYPE_SIZE_UNIT (inner_type));
2557 t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, t);
2558 append_to_statement_list (t, &ret);
2559
2560 if (arg2)
2561 {
2562 t = fold_build_pointer_plus (p2, TYPE_SIZE_UNIT (inner_type));
2563 t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, t);
2564 append_to_statement_list (t, &ret);
2565 }
2566
2567 t = build2 (NE_EXPR, boolean_type_node, p1, end1);
2568 t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&lab), NULL);
2569 append_to_statement_list (t, &ret);
2570
2571 return ret;
2572 }
2573 else
2574 {
2575 argarray[i++] = build_fold_addr_expr_loc (input_location, arg1);
2576 if (arg2)
2577 argarray[i++] = build_fold_addr_expr_loc (input_location, arg2);
2578 /* Handle default arguments. */
2579 for (parm = defparm; parm && parm != void_list_node;
2580 parm = TREE_CHAIN (parm), i++)
2581 argarray[i] = convert_default_arg (TREE_VALUE (parm),
2582 TREE_PURPOSE (parm), fn,
2583 i - is_method, tf_warning_or_error);
2584 t = build_call_a (fn, i, argarray);
2585 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
2586 t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
2587 t = fold_convert (void_type_node, t);
2588 return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
2589 }
2590 }
2591
2592 /* Return code to initialize DECL with its default constructor, or
2593 NULL if there's nothing to do. */
2594
2595 tree
2596 cxx_omp_clause_default_ctor (tree clause, tree decl, tree /*outer*/)
2597 {
2598 tree info = CP_OMP_CLAUSE_INFO (clause);
2599 tree ret = NULL;
2600
2601 if (info)
2602 ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), decl, NULL);
2603
2604 return ret;
2605 }
2606
2607 /* Return code to initialize DST with a copy constructor from SRC. */
2608
2609 tree
2610 cxx_omp_clause_copy_ctor (tree clause, tree dst, tree src)
2611 {
2612 tree info = CP_OMP_CLAUSE_INFO (clause);
2613 tree ret = NULL;
2614
2615 if (info)
2616 ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), dst, src);
2617 if (ret == NULL)
2618 ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
2619
2620 return ret;
2621 }
2622
2623 /* Similarly, except use an assignment operator instead. */
2624
2625 tree
2626 cxx_omp_clause_assign_op (tree clause, tree dst, tree src)
2627 {
2628 tree info = CP_OMP_CLAUSE_INFO (clause);
2629 tree ret = NULL;
2630
2631 if (info)
2632 ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 2), dst, src);
2633 if (ret == NULL)
2634 ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
2635
2636 return ret;
2637 }
2638
2639 /* Return code to destroy DECL. */
2640
2641 tree
2642 cxx_omp_clause_dtor (tree clause, tree decl)
2643 {
2644 tree info = CP_OMP_CLAUSE_INFO (clause);
2645 tree ret = NULL;
2646
2647 if (info)
2648 ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 1), decl, NULL);
2649
2650 return ret;
2651 }
2652
2653 /* True if OpenMP should privatize what this DECL points to rather
2654 than the DECL itself. */
2655
2656 bool
2657 cxx_omp_privatize_by_reference (const_tree decl)
2658 {
2659 return (TYPE_REF_P (TREE_TYPE (decl))
2660 || is_invisiref_parm (decl));
2661 }
2662
2663 /* Return true if DECL is const qualified var having no mutable member. */
2664 bool
2665 cxx_omp_const_qual_no_mutable (tree decl)
2666 {
2667 tree type = TREE_TYPE (decl);
2668 if (TYPE_REF_P (type))
2669 {
2670 if (!is_invisiref_parm (decl))
2671 return false;
2672 type = TREE_TYPE (type);
2673
2674 if (TREE_CODE (decl) == RESULT_DECL && DECL_NAME (decl))
2675 {
2676 /* NVR doesn't preserve const qualification of the
2677 variable's type. */
2678 tree outer = outer_curly_brace_block (current_function_decl);
2679 tree var;
2680
2681 if (outer)
2682 for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
2683 if (VAR_P (var)
2684 && DECL_NAME (decl) == DECL_NAME (var)
2685 && (TYPE_MAIN_VARIANT (type)
2686 == TYPE_MAIN_VARIANT (TREE_TYPE (var))))
2687 {
2688 if (TYPE_READONLY (TREE_TYPE (var)))
2689 type = TREE_TYPE (var);
2690 break;
2691 }
2692 }
2693 }
2694
2695 if (type == error_mark_node)
2696 return false;
2697
2698 /* Variables with const-qualified type having no mutable member
2699 are predetermined shared. */
2700 if (TYPE_READONLY (type) && !cp_has_mutable_p (type))
2701 return true;
2702
2703 return false;
2704 }
2705
2706 /* OMP_CLAUSE_DEFAULT_UNSPECIFIED unless OpenMP sharing attribute
2707 of DECL is predetermined. */
2708
2709 enum omp_clause_default_kind
2710 cxx_omp_predetermined_sharing_1 (tree decl)
2711 {
2712 /* Static data members are predetermined shared. */
2713 if (TREE_STATIC (decl))
2714 {
2715 tree ctx = CP_DECL_CONTEXT (decl);
2716 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
2717 return OMP_CLAUSE_DEFAULT_SHARED;
2718
2719 if (c_omp_predefined_variable (decl))
2720 return OMP_CLAUSE_DEFAULT_SHARED;
2721 }
2722
2723 /* this may not be specified in data-sharing clauses, still we need
2724 to predetermined it firstprivate. */
2725 if (decl == current_class_ptr)
2726 return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
2727
2728 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
2729 }
2730
2731 /* Likewise, but also include the artificial vars. We don't want to
2732 disallow the artificial vars being mentioned in explicit clauses,
2733 as we use artificial vars e.g. for loop constructs with random
2734 access iterators other than pointers, but during gimplification
2735 we want to treat them as predetermined. */
2736
2737 enum omp_clause_default_kind
2738 cxx_omp_predetermined_sharing (tree decl)
2739 {
2740 enum omp_clause_default_kind ret = cxx_omp_predetermined_sharing_1 (decl);
2741 if (ret != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
2742 return ret;
2743
2744 /* Predetermine artificial variables holding integral values, those
2745 are usually result of gimplify_one_sizepos or SAVE_EXPR
2746 gimplification. */
2747 if (VAR_P (decl)
2748 && DECL_ARTIFICIAL (decl)
2749 && INTEGRAL_TYPE_P (TREE_TYPE (decl))
2750 && !(DECL_LANG_SPECIFIC (decl)
2751 && DECL_OMP_PRIVATIZED_MEMBER (decl)))
2752 return OMP_CLAUSE_DEFAULT_SHARED;
2753
2754 /* Similarly for typeinfo symbols. */
2755 if (VAR_P (decl) && DECL_ARTIFICIAL (decl) && DECL_TINFO_P (decl))
2756 return OMP_CLAUSE_DEFAULT_SHARED;
2757
2758 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
2759 }
2760
2761 enum omp_clause_defaultmap_kind
2762 cxx_omp_predetermined_mapping (tree decl)
2763 {
2764 /* Predetermine artificial variables holding integral values, those
2765 are usually result of gimplify_one_sizepos or SAVE_EXPR
2766 gimplification. */
2767 if (VAR_P (decl)
2768 && DECL_ARTIFICIAL (decl)
2769 && INTEGRAL_TYPE_P (TREE_TYPE (decl))
2770 && !(DECL_LANG_SPECIFIC (decl)
2771 && DECL_OMP_PRIVATIZED_MEMBER (decl)))
2772 return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
2773
2774 if (c_omp_predefined_variable (decl))
2775 return OMP_CLAUSE_DEFAULTMAP_TO;
2776
2777 return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
2778 }
2779
2780 /* Finalize an implicitly determined clause. */
2781
2782 void
2783 cxx_omp_finish_clause (tree c, gimple_seq *, bool /* openacc */)
2784 {
2785 tree decl, inner_type;
2786 bool make_shared = false;
2787
2788 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
2789 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE
2790 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LASTPRIVATE
2791 || !OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)))
2792 return;
2793
2794 decl = OMP_CLAUSE_DECL (c);
2795 decl = require_complete_type (decl);
2796 inner_type = TREE_TYPE (decl);
2797 if (decl == error_mark_node)
2798 make_shared = true;
2799 else if (TYPE_REF_P (TREE_TYPE (decl)))
2800 inner_type = TREE_TYPE (inner_type);
2801
2802 /* We're interested in the base element, not arrays. */
2803 while (TREE_CODE (inner_type) == ARRAY_TYPE)
2804 inner_type = TREE_TYPE (inner_type);
2805
2806 /* Check for special function availability by building a call to one.
2807 Save the results, because later we won't be in the right context
2808 for making these queries. */
2809 bool first = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE;
2810 bool last = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE;
2811 if (!make_shared
2812 && CLASS_TYPE_P (inner_type)
2813 && cxx_omp_create_clause_info (c, inner_type, !first, first, last,
2814 true))
2815 make_shared = true;
2816
2817 if (make_shared)
2818 {
2819 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_SHARED;
2820 OMP_CLAUSE_SHARED_FIRSTPRIVATE (c) = 0;
2821 OMP_CLAUSE_SHARED_READONLY (c) = 0;
2822 }
2823 }
2824
2825 /* Return true if DECL's DECL_VALUE_EXPR (if any) should be
2826 disregarded in OpenMP construct, because it is going to be
2827 remapped during OpenMP lowering. SHARED is true if DECL
2828 is going to be shared, false if it is going to be privatized. */
2829
2830 bool
2831 cxx_omp_disregard_value_expr (tree decl, bool shared)
2832 {
2833 if (shared)
2834 return false;
2835 if (VAR_P (decl)
2836 && DECL_HAS_VALUE_EXPR_P (decl)
2837 && DECL_ARTIFICIAL (decl)
2838 && DECL_LANG_SPECIFIC (decl)
2839 && DECL_OMP_PRIVATIZED_MEMBER (decl))
2840 return true;
2841 if (VAR_P (decl) && DECL_CONTEXT (decl) && is_capture_proxy (decl))
2842 return true;
2843 return false;
2844 }
2845
2846 /* Fold expression X which is used as an rvalue if RVAL is true. */
2847
2848 static tree
2849 cp_fold_maybe_rvalue (tree x, bool rval, fold_flags_t flags)
2850 {
2851 while (true)
2852 {
2853 x = cp_fold (x, flags);
2854 if (rval)
2855 x = mark_rvalue_use (x);
2856 if (rval && DECL_P (x)
2857 && !TYPE_REF_P (TREE_TYPE (x)))
2858 {
2859 tree v = decl_constant_value (x);
2860 if (v != x && v != error_mark_node)
2861 {
2862 x = v;
2863 continue;
2864 }
2865 }
2866 break;
2867 }
2868 return x;
2869 }
2870
2871 tree
2872 cp_fold_maybe_rvalue (tree x, bool rval)
2873 {
2874 return cp_fold_maybe_rvalue (x, rval, ff_none);
2875 }
2876
2877 /* Fold expression X which is used as an rvalue. */
2878
2879 static tree
2880 cp_fold_rvalue (tree x, fold_flags_t flags)
2881 {
2882 return cp_fold_maybe_rvalue (x, true, flags);
2883 }
2884
2885 tree
2886 cp_fold_rvalue (tree x)
2887 {
2888 return cp_fold_rvalue (x, ff_none);
2889 }
2890
2891 /* Perform folding on expression X. */
2892
2893 static tree
2894 cp_fully_fold (tree x, mce_value manifestly_const_eval)
2895 {
2896 if (processing_template_decl)
2897 return x;
2898 /* FIXME cp_fold ought to be a superset of maybe_constant_value so we don't
2899 have to call both. */
2900 if (cxx_dialect >= cxx11)
2901 {
2902 x = maybe_constant_value (x, /*decl=*/NULL_TREE, manifestly_const_eval);
2903 /* Sometimes we are given a CONSTRUCTOR but the call above wraps it into
2904 a TARGET_EXPR; undo that here. */
2905 if (TREE_CODE (x) == TARGET_EXPR)
2906 x = TARGET_EXPR_INITIAL (x);
2907 else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
2908 && TREE_CODE (TREE_OPERAND (x, 0)) == CONSTRUCTOR
2909 && TREE_TYPE (TREE_OPERAND (x, 0)) == TREE_TYPE (x))
2910 x = TREE_OPERAND (x, 0);
2911 }
2912 fold_flags_t flags = ff_none;
2913 if (manifestly_const_eval == mce_false)
2914 flags |= ff_mce_false;
2915 return cp_fold_rvalue (x, flags);
2916 }
2917
2918 tree
2919 cp_fully_fold (tree x)
2920 {
2921 return cp_fully_fold (x, mce_unknown);
2922 }
2923
2924 /* Likewise, but also fold recursively, which cp_fully_fold doesn't perform
2925 in some cases. */
2926
2927 tree
2928 cp_fully_fold_init (tree x)
2929 {
2930 if (processing_template_decl)
2931 return x;
2932 x = cp_fully_fold (x, mce_false);
2933 cp_fold_data data (ff_mce_false);
2934 if (cxx_dialect >= cxx20)
2935 {
2936 cp_walk_tree (&x, cp_fold_immediate_r, &data, NULL);
2937 data.pset.empty ();
2938 }
2939 cp_walk_tree (&x, cp_fold_r, &data, NULL);
2940 return x;
2941 }
2942
2943 /* c-common interface to cp_fold. If IN_INIT, this is in a static initializer
2944 and certain changes are made to the folding done. Or should be (FIXME). We
2945 never touch maybe_const, as it is only used for the C front-end
2946 C_MAYBE_CONST_EXPR. */
2947
2948 tree
2949 c_fully_fold (tree x, bool /*in_init*/, bool */*maybe_const*/, bool lval)
2950 {
2951 return cp_fold_maybe_rvalue (x, !lval);
2952 }
2953
2954 static GTY((deletable)) hash_map<tree, tree> *fold_caches[2];
2955
2956 /* Subroutine of cp_fold. Returns which fold cache to use according
2957 to the given flags. We need multiple caches since the result of
2958 folding may depend on which flags are used. */
2959
2960 static hash_map<tree, tree> *&
2961 get_fold_cache (fold_flags_t flags)
2962 {
2963 if (flags & ff_mce_false)
2964 return fold_caches[1];
2965 else
2966 return fold_caches[0];
2967 }
2968
2969 /* Dispose of the whole FOLD_CACHE. */
2970
2971 void
2972 clear_fold_cache (void)
2973 {
2974 for (auto& fold_cache : fold_caches)
2975 if (fold_cache != NULL)
2976 fold_cache->empty ();
2977 }
2978
2979 /* This function tries to fold an expression X.
2980 To avoid combinatorial explosion, folding results are kept in fold_cache.
2981 If X is invalid, we don't fold at all.
2982 For performance reasons we don't cache expressions representing a
2983 declaration or constant.
2984 Function returns X or its folded variant. */
2985
2986 static tree
2987 cp_fold (tree x, fold_flags_t flags)
2988 {
2989 tree op0, op1, op2, op3;
2990 tree org_x = x, r = NULL_TREE;
2991 enum tree_code code;
2992 location_t loc;
2993 bool rval_ops = true;
2994
2995 if (!x || x == error_mark_node)
2996 return x;
2997
2998 if (EXPR_P (x) && (!TREE_TYPE (x) || TREE_TYPE (x) == error_mark_node))
2999 return x;
3000
3001 /* Don't bother to cache DECLs or constants. */
3002 if (DECL_P (x) || CONSTANT_CLASS_P (x))
3003 return x;
3004
3005 auto& fold_cache = get_fold_cache (flags);
3006 if (fold_cache == NULL)
3007 fold_cache = hash_map<tree, tree>::create_ggc (101);
3008
3009 if (tree *cached = fold_cache->get (x))
3010 {
3011 /* unshare_expr doesn't recurse into SAVE_EXPRs. If SAVE_EXPR's
3012 argument has been folded into a tree invariant, make sure it is
3013 unshared. See PR112727. */
3014 if (TREE_CODE (x) == SAVE_EXPR && *cached != x)
3015 return unshare_expr (*cached);
3016 return *cached;
3017 }
3018
3019 uid_sensitive_constexpr_evaluation_checker c;
3020
3021 code = TREE_CODE (x);
3022 switch (code)
3023 {
3024 case CLEANUP_POINT_EXPR:
3025 /* Strip CLEANUP_POINT_EXPR if the expression doesn't have side
3026 effects. */
3027 r = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3028 if (!TREE_SIDE_EFFECTS (r))
3029 x = r;
3030 break;
3031
3032 case SIZEOF_EXPR:
3033 x = fold_sizeof_expr (x);
3034 break;
3035
3036 case VIEW_CONVERT_EXPR:
3037 rval_ops = false;
3038 /* FALLTHRU */
3039 case NON_LVALUE_EXPR:
3040 CASE_CONVERT:
3041
3042 if (VOID_TYPE_P (TREE_TYPE (x)))
3043 {
3044 /* This is just to make sure we don't end up with casts to
3045 void from error_mark_node. If we just return x, then
3046 cp_fold_r might fold the operand into error_mark_node and
3047 leave the conversion in the IR. STRIP_USELESS_TYPE_CONVERSION
3048 during gimplification doesn't like such casts.
3049 Don't create a new tree if op0 != TREE_OPERAND (x, 0), the
3050 folding of the operand should be in the caches and if in cp_fold_r
3051 it will modify it in place. */
3052 op0 = cp_fold (TREE_OPERAND (x, 0), flags);
3053 if (op0 == error_mark_node)
3054 x = error_mark_node;
3055 break;
3056 }
3057
3058 loc = EXPR_LOCATION (x);
3059 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3060
3061 if (op0 == error_mark_node)
3062 x = error_mark_node;
3063 else if (code == CONVERT_EXPR
3064 && SCALAR_TYPE_P (TREE_TYPE (x))
3065 && op0 != void_node)
3066 /* During parsing we used convert_to_*_nofold; re-convert now using the
3067 folding variants, since fold() doesn't do those transformations. */
3068 x = fold (convert (TREE_TYPE (x), op0));
3069 else if (op0 != TREE_OPERAND (x, 0))
3070 x = fold_build1_loc (loc, code, TREE_TYPE (x), op0);
3071 else
3072 x = fold (x);
3073
3074 /* Conversion of an out-of-range value has implementation-defined
3075 behavior; the language considers it different from arithmetic
3076 overflow, which is undefined. */
3077 if (TREE_CODE (op0) == INTEGER_CST
3078 && TREE_OVERFLOW_P (x) && !TREE_OVERFLOW_P (op0))
3079 TREE_OVERFLOW (x) = false;
3080
3081 break;
3082
3083 case EXCESS_PRECISION_EXPR:
3084 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3085 x = fold_convert_loc (EXPR_LOCATION (x), TREE_TYPE (x), op0);
3086 break;
3087
3088 case INDIRECT_REF:
3089 /* We don't need the decltype(auto) obfuscation anymore. */
3090 if (REF_PARENTHESIZED_P (x))
3091 {
3092 tree p = maybe_undo_parenthesized_ref (x);
3093 if (p != x)
3094 return cp_fold (p, flags);
3095 }
3096 goto unary;
3097
3098 case ADDR_EXPR:
3099 loc = EXPR_LOCATION (x);
3100 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), false, flags);
3101
3102 /* Cope with user tricks that amount to offsetof. */
3103 if (op0 != error_mark_node
3104 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (op0)))
3105 {
3106 tree val = get_base_address (op0);
3107 if (val
3108 && INDIRECT_REF_P (val)
3109 && COMPLETE_TYPE_P (TREE_TYPE (val))
3110 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3111 {
3112 val = TREE_OPERAND (val, 0);
3113 STRIP_NOPS (val);
3114 val = maybe_constant_value (val);
3115 if (TREE_CODE (val) == INTEGER_CST)
3116 return fold_offsetof (op0, TREE_TYPE (x));
3117 }
3118 }
3119 goto finish_unary;
3120
3121 case REALPART_EXPR:
3122 case IMAGPART_EXPR:
3123 rval_ops = false;
3124 /* FALLTHRU */
3125 case CONJ_EXPR:
3126 case FIX_TRUNC_EXPR:
3127 case FLOAT_EXPR:
3128 case NEGATE_EXPR:
3129 case ABS_EXPR:
3130 case ABSU_EXPR:
3131 case BIT_NOT_EXPR:
3132 case TRUTH_NOT_EXPR:
3133 case FIXED_CONVERT_EXPR:
3134 unary:
3135
3136 loc = EXPR_LOCATION (x);
3137 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3138
3139 finish_unary:
3140 if (op0 == error_mark_node)
3141 x = error_mark_node;
3142 else if (op0 != TREE_OPERAND (x, 0))
3143 {
3144 x = fold_build1_loc (loc, code, TREE_TYPE (x), op0);
3145 if (code == INDIRECT_REF
3146 && (INDIRECT_REF_P (x) || TREE_CODE (x) == MEM_REF))
3147 {
3148 TREE_READONLY (x) = TREE_READONLY (org_x);
3149 TREE_SIDE_EFFECTS (x) = TREE_SIDE_EFFECTS (org_x);
3150 TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3151 }
3152 }
3153 else
3154 x = fold (x);
3155
3156 gcc_assert (TREE_CODE (x) != COND_EXPR
3157 || !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))));
3158 break;
3159
3160 case UNARY_PLUS_EXPR:
3161 op0 = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3162 if (op0 == error_mark_node)
3163 x = error_mark_node;
3164 else
3165 x = fold_convert (TREE_TYPE (x), op0);
3166 break;
3167
3168 case POSTDECREMENT_EXPR:
3169 case POSTINCREMENT_EXPR:
3170 case INIT_EXPR:
3171 case PREDECREMENT_EXPR:
3172 case PREINCREMENT_EXPR:
3173 case COMPOUND_EXPR:
3174 case MODIFY_EXPR:
3175 rval_ops = false;
3176 /* FALLTHRU */
3177 case POINTER_PLUS_EXPR:
3178 case PLUS_EXPR:
3179 case POINTER_DIFF_EXPR:
3180 case MINUS_EXPR:
3181 case MULT_EXPR:
3182 case TRUNC_DIV_EXPR:
3183 case CEIL_DIV_EXPR:
3184 case FLOOR_DIV_EXPR:
3185 case ROUND_DIV_EXPR:
3186 case TRUNC_MOD_EXPR:
3187 case CEIL_MOD_EXPR:
3188 case ROUND_MOD_EXPR:
3189 case RDIV_EXPR:
3190 case EXACT_DIV_EXPR:
3191 case MIN_EXPR:
3192 case MAX_EXPR:
3193 case LSHIFT_EXPR:
3194 case RSHIFT_EXPR:
3195 case LROTATE_EXPR:
3196 case RROTATE_EXPR:
3197 case BIT_AND_EXPR:
3198 case BIT_IOR_EXPR:
3199 case BIT_XOR_EXPR:
3200 case TRUTH_AND_EXPR:
3201 case TRUTH_ANDIF_EXPR:
3202 case TRUTH_OR_EXPR:
3203 case TRUTH_ORIF_EXPR:
3204 case TRUTH_XOR_EXPR:
3205 case LT_EXPR: case LE_EXPR:
3206 case GT_EXPR: case GE_EXPR:
3207 case EQ_EXPR: case NE_EXPR:
3208 case UNORDERED_EXPR: case ORDERED_EXPR:
3209 case UNLT_EXPR: case UNLE_EXPR:
3210 case UNGT_EXPR: case UNGE_EXPR:
3211 case UNEQ_EXPR: case LTGT_EXPR:
3212 case RANGE_EXPR: case COMPLEX_EXPR:
3213
3214 loc = EXPR_LOCATION (x);
3215 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops, flags);
3216 op1 = cp_fold_rvalue (TREE_OPERAND (x, 1), flags);
3217
3218 /* decltype(nullptr) has only one value, so optimize away all comparisons
3219 with that type right away, keeping them in the IL causes troubles for
3220 various optimizations. */
3221 if (COMPARISON_CLASS_P (org_x)
3222 && TREE_CODE (TREE_TYPE (op0)) == NULLPTR_TYPE
3223 && TREE_CODE (TREE_TYPE (op1)) == NULLPTR_TYPE)
3224 {
3225 switch (code)
3226 {
3227 case EQ_EXPR:
3228 x = constant_boolean_node (true, TREE_TYPE (x));
3229 break;
3230 case NE_EXPR:
3231 x = constant_boolean_node (false, TREE_TYPE (x));
3232 break;
3233 default:
3234 gcc_unreachable ();
3235 }
3236 return omit_two_operands_loc (loc, TREE_TYPE (x), x,
3237 op0, op1);
3238 }
3239
3240 if (op0 == error_mark_node || op1 == error_mark_node)
3241 x = error_mark_node;
3242 else if (op0 != TREE_OPERAND (x, 0) || op1 != TREE_OPERAND (x, 1))
3243 x = fold_build2_loc (loc, code, TREE_TYPE (x), op0, op1);
3244 else
3245 x = fold (x);
3246
3247 /* This is only needed for -Wnonnull-compare and only if
3248 TREE_NO_WARNING (org_x), but to avoid that option affecting code
3249 generation, we do it always. */
3250 if (COMPARISON_CLASS_P (org_x))
3251 {
3252 if (x == error_mark_node || TREE_CODE (x) == INTEGER_CST)
3253 ;
3254 else if (COMPARISON_CLASS_P (x))
3255 {
3256 if (warn_nonnull_compare
3257 && warning_suppressed_p (org_x, OPT_Wnonnull_compare))
3258 suppress_warning (x, OPT_Wnonnull_compare);
3259 }
3260 /* Otherwise give up on optimizing these, let GIMPLE folders
3261 optimize those later on. */
3262 else if (op0 != TREE_OPERAND (org_x, 0)
3263 || op1 != TREE_OPERAND (org_x, 1))
3264 {
3265 x = build2_loc (loc, code, TREE_TYPE (org_x), op0, op1);
3266 if (warn_nonnull_compare
3267 && warning_suppressed_p (org_x, OPT_Wnonnull_compare))
3268 suppress_warning (x, OPT_Wnonnull_compare);
3269 }
3270 else
3271 x = org_x;
3272 }
3273
3274 break;
3275
3276 case VEC_COND_EXPR:
3277 case COND_EXPR:
3278 loc = EXPR_LOCATION (x);
3279 op0 = cp_fold_rvalue (TREE_OPERAND (x, 0), flags);
3280 op1 = cp_fold (TREE_OPERAND (x, 1), flags);
3281 op2 = cp_fold (TREE_OPERAND (x, 2), flags);
3282
3283 if (TREE_CODE (TREE_TYPE (x)) == BOOLEAN_TYPE)
3284 {
3285 warning_sentinel s (warn_int_in_bool_context);
3286 if (!VOID_TYPE_P (TREE_TYPE (op1)))
3287 op1 = cp_truthvalue_conversion (op1, tf_warning_or_error);
3288 if (!VOID_TYPE_P (TREE_TYPE (op2)))
3289 op2 = cp_truthvalue_conversion (op2, tf_warning_or_error);
3290 }
3291 else if (VOID_TYPE_P (TREE_TYPE (x)))
3292 {
3293 if (TREE_CODE (op0) == INTEGER_CST)
3294 {
3295 /* If the condition is constant, fold can fold away
3296 the COND_EXPR. If some statement-level uses of COND_EXPR
3297 have one of the branches NULL, avoid folding crash. */
3298 if (!op1)
3299 op1 = build_empty_stmt (loc);
3300 if (!op2)
3301 op2 = build_empty_stmt (loc);
3302 }
3303 else
3304 {
3305 /* Otherwise, don't bother folding a void condition, since
3306 it can't produce a constant value. */
3307 if (op0 != TREE_OPERAND (x, 0)
3308 || op1 != TREE_OPERAND (x, 1)
3309 || op2 != TREE_OPERAND (x, 2))
3310 x = build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3311 break;
3312 }
3313 }
3314
3315 if (op0 == error_mark_node
3316 || op1 == error_mark_node
3317 || op2 == error_mark_node)
3318 x = error_mark_node;
3319 else if (op0 != TREE_OPERAND (x, 0)
3320 || op1 != TREE_OPERAND (x, 1)
3321 || op2 != TREE_OPERAND (x, 2))
3322 x = fold_build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
3323 else
3324 x = fold (x);
3325
3326 /* A COND_EXPR might have incompatible types in branches if one or both
3327 arms are bitfields. If folding exposed such a branch, fix it up. */
3328 if (TREE_CODE (x) != code
3329 && x != error_mark_node
3330 && !useless_type_conversion_p (TREE_TYPE (org_x), TREE_TYPE (x)))
3331 x = fold_convert (TREE_TYPE (org_x), x);
3332
3333 break;
3334
3335 case CALL_EXPR:
3336 {
3337 tree callee = get_callee_fndecl (x);
3338
3339 /* "Inline" calls to std::move/forward and other cast-like functions
3340 by simply folding them into a corresponding cast to their return
3341 type. This is cheaper than relying on the middle end to do so, and
3342 also means we avoid generating useless debug info for them at all.
3343
3344 At this point the argument has already been converted into a
3345 reference, so it suffices to use a NOP_EXPR to express the
3346 cast. */
3347 if ((OPTION_SET_P (flag_fold_simple_inlines)
3348 ? flag_fold_simple_inlines
3349 : !flag_no_inline)
3350 && call_expr_nargs (x) == 1
3351 && decl_in_std_namespace_p (callee)
3352 && DECL_NAME (callee) != NULL_TREE
3353 && (id_equal (DECL_NAME (callee), "move")
3354 || id_equal (DECL_NAME (callee), "forward")
3355 || id_equal (DECL_NAME (callee), "forward_like")
3356 || id_equal (DECL_NAME (callee), "addressof")
3357 /* This addressof equivalent is used heavily in libstdc++. */
3358 || id_equal (DECL_NAME (callee), "__addressof")
3359 || id_equal (DECL_NAME (callee), "as_const")))
3360 {
3361 r = CALL_EXPR_ARG (x, 0);
3362 /* Check that the return and argument types are sane before
3363 folding. */
3364 if (INDIRECT_TYPE_P (TREE_TYPE (x))
3365 && INDIRECT_TYPE_P (TREE_TYPE (r)))
3366 {
3367 if (!same_type_p (TREE_TYPE (x), TREE_TYPE (r)))
3368 r = build_nop (TREE_TYPE (x), r);
3369 x = cp_fold (r, flags);
3370 break;
3371 }
3372 }
3373
3374 int sv = optimize, nw = sv;
3375
3376 /* Some built-in function calls will be evaluated at compile-time in
3377 fold (). Set optimize to 1 when folding __builtin_constant_p inside
3378 a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
3379 if (callee && fndecl_built_in_p (callee) && !optimize
3380 && DECL_IS_BUILTIN_CONSTANT_P (callee)
3381 && current_function_decl
3382 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
3383 nw = 1;
3384
3385 if (callee && fndecl_built_in_p (callee, BUILT_IN_FRONTEND))
3386 {
3387 iloc_sentinel ils (EXPR_LOCATION (x));
3388 switch (DECL_FE_FUNCTION_CODE (callee))
3389 {
3390 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
3391 /* Defer folding __builtin_is_constant_evaluated unless
3392 we know this isn't a manifestly constant-evaluated
3393 context. */
3394 if (flags & ff_mce_false)
3395 x = boolean_false_node;
3396 break;
3397 case CP_BUILT_IN_SOURCE_LOCATION:
3398 x = fold_builtin_source_location (x);
3399 break;
3400 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
3401 x = fold_builtin_is_corresponding_member
3402 (EXPR_LOCATION (x), call_expr_nargs (x),
3403 &CALL_EXPR_ARG (x, 0));
3404 break;
3405 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
3406 x = fold_builtin_is_pointer_inverconvertible_with_class
3407 (EXPR_LOCATION (x), call_expr_nargs (x),
3408 &CALL_EXPR_ARG (x, 0));
3409 break;
3410 default:
3411 break;
3412 }
3413 break;
3414 }
3415
3416 if (callee
3417 && fndecl_built_in_p (callee, CP_BUILT_IN_SOURCE_LOCATION,
3418 BUILT_IN_FRONTEND))
3419 {
3420 x = fold_builtin_source_location (x);
3421 break;
3422 }
3423
3424 bool changed = false;
3425 int m = call_expr_nargs (x);
3426 for (int i = 0; i < m; i++)
3427 {
3428 r = cp_fold (CALL_EXPR_ARG (x, i), flags);
3429 if (r != CALL_EXPR_ARG (x, i))
3430 {
3431 if (r == error_mark_node)
3432 {
3433 x = error_mark_node;
3434 break;
3435 }
3436 if (!changed)
3437 x = copy_node (x);
3438 CALL_EXPR_ARG (x, i) = r;
3439 changed = true;
3440 }
3441 }
3442 if (x == error_mark_node)
3443 break;
3444
3445 optimize = nw;
3446 r = fold (x);
3447 optimize = sv;
3448
3449 if (TREE_CODE (r) != CALL_EXPR)
3450 {
3451 x = cp_fold (r, flags);
3452 break;
3453 }
3454
3455 optimize = nw;
3456
3457 /* Invoke maybe_constant_value for functions declared
3458 constexpr and not called with AGGR_INIT_EXPRs.
3459 TODO:
3460 Do constexpr expansion of expressions where the call itself is not
3461 constant, but the call followed by an INDIRECT_REF is. */
3462 if (callee && DECL_DECLARED_CONSTEXPR_P (callee)
3463 && !flag_no_inline)
3464 {
3465 mce_value manifestly_const_eval = mce_unknown;
3466 if (flags & ff_mce_false)
3467 /* Allow folding __builtin_is_constant_evaluated to false during
3468 constexpr evaluation of this call. */
3469 manifestly_const_eval = mce_false;
3470 r = maybe_constant_value (x, /*decl=*/NULL_TREE,
3471 manifestly_const_eval);
3472 }
3473 optimize = sv;
3474
3475 if (TREE_CODE (r) != CALL_EXPR)
3476 {
3477 if (DECL_CONSTRUCTOR_P (callee))
3478 r = cp_build_init_expr_for_ctor (x, r);
3479 x = r;
3480 break;
3481 }
3482
3483 break;
3484 }
3485
3486 case CONSTRUCTOR:
3487 {
3488 unsigned i;
3489 constructor_elt *p;
3490 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (x);
3491 vec<constructor_elt, va_gc> *nelts = NULL;
3492 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
3493 {
3494 tree op = cp_fold (p->value, flags);
3495 if (op == error_mark_node)
3496 {
3497 x = error_mark_node;
3498 vec_free (nelts);
3499 break;
3500 }
3501 else if (op != p->value)
3502 {
3503 if (nelts == NULL)
3504 nelts = elts->copy ();
3505 (*nelts)[i].value = op;
3506 }
3507 }
3508 if (nelts)
3509 {
3510 x = build_constructor (TREE_TYPE (x), nelts);
3511 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (x)
3512 = CONSTRUCTOR_PLACEHOLDER_BOUNDARY (org_x);
3513 CONSTRUCTOR_MUTABLE_POISON (x)
3514 = CONSTRUCTOR_MUTABLE_POISON (org_x);
3515 }
3516 if (VECTOR_TYPE_P (TREE_TYPE (x)))
3517 x = fold (x);
3518 break;
3519 }
3520 case TREE_VEC:
3521 {
3522 bool changed = false;
3523 int n = TREE_VEC_LENGTH (x);
3524
3525 for (int i = 0; i < n; i++)
3526 {
3527 tree op = cp_fold (TREE_VEC_ELT (x, i), flags);
3528 if (op != TREE_VEC_ELT (x, i))
3529 {
3530 if (!changed)
3531 x = copy_node (x);
3532 TREE_VEC_ELT (x, i) = op;
3533 changed = true;
3534 }
3535 }
3536 }
3537
3538 break;
3539
3540 case ARRAY_REF:
3541 case ARRAY_RANGE_REF:
3542
3543 loc = EXPR_LOCATION (x);
3544 op0 = cp_fold (TREE_OPERAND (x, 0), flags);
3545 op1 = cp_fold (TREE_OPERAND (x, 1), flags);
3546 op2 = cp_fold (TREE_OPERAND (x, 2), flags);
3547 op3 = cp_fold (TREE_OPERAND (x, 3), flags);
3548
3549 if (op0 == error_mark_node
3550 || op1 == error_mark_node
3551 || op2 == error_mark_node
3552 || op3 == error_mark_node)
3553 x = error_mark_node;
3554 else if (op0 != TREE_OPERAND (x, 0)
3555 || op1 != TREE_OPERAND (x, 1)
3556 || op2 != TREE_OPERAND (x, 2)
3557 || op3 != TREE_OPERAND (x, 3))
3558 {
3559 x = build4_loc (loc, code, TREE_TYPE (x), op0, op1, op2, op3);
3560 TREE_READONLY (x) = TREE_READONLY (org_x);
3561 TREE_SIDE_EFFECTS (x) = TREE_SIDE_EFFECTS (org_x);
3562 TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3563 }
3564
3565 x = fold (x);
3566 break;
3567
3568 case SAVE_EXPR:
3569 /* A SAVE_EXPR might contain e.g. (0 * i) + (0 * j), which, after
3570 folding, evaluates to an invariant. In that case no need to wrap
3571 this folded tree with a SAVE_EXPR. */
3572 r = cp_fold (TREE_OPERAND (x, 0), flags);
3573 if (tree_invariant_p (r))
3574 x = r;
3575 break;
3576
3577 case REQUIRES_EXPR:
3578 x = evaluate_requires_expr (x);
3579 break;
3580
3581 default:
3582 return org_x;
3583 }
3584
3585 if (EXPR_P (x) && TREE_CODE (x) == code)
3586 {
3587 TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
3588 copy_warning (x, org_x);
3589 }
3590
3591 if (!c.evaluation_restricted_p ())
3592 {
3593 fold_cache->put (org_x, x);
3594 /* Prevent that we try to fold an already folded result again. */
3595 if (x != org_x)
3596 fold_cache->put (x, x);
3597 }
3598
3599 return x;
3600 }
3601
3602 /* Look up "hot", "cold", "likely" or "unlikely" in attribute list LIST. */
3603
3604 tree
3605 lookup_hotness_attribute (tree list)
3606 {
3607 for (; list; list = TREE_CHAIN (list))
3608 {
3609 tree name = get_attribute_name (list);
3610 if ((is_attribute_p ("hot", name)
3611 || is_attribute_p ("cold", name)
3612 || is_attribute_p ("likely", name)
3613 || is_attribute_p ("unlikely", name))
3614 && is_attribute_namespace_p ("", list))
3615 break;
3616 }
3617 return list;
3618 }
3619
3620 /* Remove "hot", "cold", "likely" and "unlikely" attributes from LIST. */
3621
3622 static tree
3623 remove_hotness_attribute (tree list)
3624 {
3625 for (tree *p = &list; *p; )
3626 {
3627 tree l = *p;
3628 tree name = get_attribute_name (l);
3629 if ((is_attribute_p ("hot", name)
3630 || is_attribute_p ("cold", name)
3631 || is_attribute_p ("likely", name)
3632 || is_attribute_p ("unlikely", name))
3633 && is_attribute_namespace_p ("", l))
3634 {
3635 *p = TREE_CHAIN (l);
3636 continue;
3637 }
3638 p = &TREE_CHAIN (l);
3639 }
3640 return list;
3641 }
3642
3643 /* If [[likely]] or [[unlikely]] appear on this statement, turn it into a
3644 PREDICT_EXPR. */
3645
3646 tree
3647 process_stmt_hotness_attribute (tree std_attrs, location_t attrs_loc)
3648 {
3649 if (std_attrs == error_mark_node)
3650 return std_attrs;
3651 if (tree attr = lookup_hotness_attribute (std_attrs))
3652 {
3653 tree name = get_attribute_name (attr);
3654 bool hot = (is_attribute_p ("hot", name)
3655 || is_attribute_p ("likely", name));
3656 tree pred = build_predict_expr (hot ? PRED_HOT_LABEL : PRED_COLD_LABEL,
3657 hot ? TAKEN : NOT_TAKEN);
3658 SET_EXPR_LOCATION (pred, attrs_loc);
3659 add_stmt (pred);
3660 if (tree other = lookup_hotness_attribute (TREE_CHAIN (attr)))
3661 {
3662 auto_urlify_attributes sentinel;
3663 warning (OPT_Wattributes, "ignoring attribute %qE after earlier %qE",
3664 get_attribute_name (other), name);
3665 }
3666 std_attrs = remove_hotness_attribute (std_attrs);
3667 }
3668 return std_attrs;
3669 }
3670
3671 /* Build IFN_ASSUME internal call for assume condition ARG. */
3672
3673 tree
3674 build_assume_call (location_t loc, tree arg)
3675 {
3676 if (!processing_template_decl)
3677 arg = fold_build_cleanup_point_expr (TREE_TYPE (arg), arg);
3678 return build_call_expr_internal_loc (loc, IFN_ASSUME, void_type_node,
3679 1, arg);
3680 }
3681
3682 /* If [[assume (cond)]] appears on this statement, handle it. */
3683
3684 tree
3685 process_stmt_assume_attribute (tree std_attrs, tree statement,
3686 location_t attrs_loc)
3687 {
3688 if (std_attrs == error_mark_node)
3689 return std_attrs;
3690 tree attr = lookup_attribute ("gnu", "assume", std_attrs);
3691 if (!attr)
3692 return std_attrs;
3693 /* The next token after the assume attribute is not ';'. */
3694 if (statement)
3695 {
3696 warning_at (attrs_loc, OPT_Wattributes,
3697 "%<assume%> attribute not followed by %<;%>");
3698 attr = NULL_TREE;
3699 }
3700 for (; attr; attr = lookup_attribute ("gnu", "assume", TREE_CHAIN (attr)))
3701 {
3702 tree args = TREE_VALUE (attr);
3703 if (args && PACK_EXPANSION_P (args))
3704 {
3705 auto_diagnostic_group d;
3706 error_at (attrs_loc, "pack expansion of %qE attribute",
3707 get_attribute_name (attr));
3708 if (cxx_dialect >= cxx17)
3709 inform (attrs_loc, "use fold expression in the attribute "
3710 "argument instead");
3711 continue;
3712 }
3713 int nargs = list_length (args);
3714 if (nargs != 1)
3715 {
3716 auto_diagnostic_group d;
3717 error_at (attrs_loc, "wrong number of arguments specified for "
3718 "%qE attribute", get_attribute_name (attr));
3719 inform (attrs_loc, "expected %i, found %i", 1, nargs);
3720 }
3721 else
3722 {
3723 tree arg = TREE_VALUE (args);
3724 if (!type_dependent_expression_p (arg))
3725 arg = contextual_conv_bool (arg, tf_warning_or_error);
3726 if (error_operand_p (arg))
3727 continue;
3728 finish_expr_stmt (build_assume_call (attrs_loc, arg));
3729 }
3730 }
3731 return remove_attribute ("gnu", "assume", std_attrs);
3732 }
3733
3734 /* Return the type std::source_location::__impl after performing
3735 verification on it. */
3736
3737 tree
3738 get_source_location_impl_type ()
3739 {
3740 tree name = get_identifier ("source_location");
3741 tree decl = lookup_qualified_name (std_node, name);
3742 if (TREE_CODE (decl) != TYPE_DECL)
3743 {
3744 auto_diagnostic_group d;
3745 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
3746 qualified_name_lookup_error (std_node, name, decl, input_location);
3747 else
3748 error ("%qD is not a type", decl);
3749 return error_mark_node;
3750 }
3751 name = get_identifier ("__impl");
3752 tree type = TREE_TYPE (decl);
3753 decl = lookup_qualified_name (type, name);
3754 if (TREE_CODE (decl) != TYPE_DECL)
3755 {
3756 auto_diagnostic_group d;
3757 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
3758 qualified_name_lookup_error (type, name, decl, input_location);
3759 else
3760 error ("%qD is not a type", decl);
3761 return error_mark_node;
3762 }
3763 type = TREE_TYPE (decl);
3764 if (TREE_CODE (type) != RECORD_TYPE)
3765 {
3766 error ("%qD is not a class type", decl);
3767 return error_mark_node;
3768 }
3769
3770 int cnt = 0;
3771 for (tree field = TYPE_FIELDS (type);
3772 (field = next_aggregate_field (field)) != NULL_TREE;
3773 field = DECL_CHAIN (field))
3774 {
3775 if (DECL_NAME (field) != NULL_TREE)
3776 {
3777 const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
3778 if (strcmp (n, "_M_file_name") == 0
3779 || strcmp (n, "_M_function_name") == 0)
3780 {
3781 if (TREE_TYPE (field) != const_string_type_node)
3782 {
3783 error ("%qD does not have %<const char *%> type", field);
3784 return error_mark_node;
3785 }
3786 cnt++;
3787 continue;
3788 }
3789 else if (strcmp (n, "_M_line") == 0 || strcmp (n, "_M_column") == 0)
3790 {
3791 if (TREE_CODE (TREE_TYPE (field)) != INTEGER_TYPE)
3792 {
3793 error ("%qD does not have integral type", field);
3794 return error_mark_node;
3795 }
3796 cnt++;
3797 continue;
3798 }
3799 }
3800 cnt = 0;
3801 break;
3802 }
3803 if (cnt != 4)
3804 {
3805 error ("%<std::source_location::__impl%> does not contain only "
3806 "non-static data members %<_M_file_name%>, "
3807 "%<_M_function_name%>, %<_M_line%> and %<_M_column%>");
3808 return error_mark_node;
3809 }
3810 return build_qualified_type (type, TYPE_QUAL_CONST);
3811 }
3812
3813 /* Type for source_location_table hash_set. */
3814 struct GTY((for_user)) source_location_table_entry {
3815 location_t loc;
3816 unsigned uid;
3817 tree var;
3818 };
3819
3820 /* Traits class for function start hash maps below. */
3821
3822 struct source_location_table_entry_hash
3823 : ggc_remove <source_location_table_entry>
3824 {
3825 typedef source_location_table_entry value_type;
3826 typedef source_location_table_entry compare_type;
3827
3828 static hashval_t
3829 hash (const source_location_table_entry &ref)
3830 {
3831 inchash::hash hstate (0);
3832 hstate.add_int (ref.loc);
3833 hstate.add_int (ref.uid);
3834 return hstate.end ();
3835 }
3836
3837 static bool
3838 equal (const source_location_table_entry &ref1,
3839 const source_location_table_entry &ref2)
3840 {
3841 return ref1.loc == ref2.loc && ref1.uid == ref2.uid;
3842 }
3843
3844 static void
3845 mark_deleted (source_location_table_entry &ref)
3846 {
3847 ref.loc = UNKNOWN_LOCATION;
3848 ref.uid = -1U;
3849 ref.var = NULL_TREE;
3850 }
3851
3852 static const bool empty_zero_p = true;
3853
3854 static void
3855 mark_empty (source_location_table_entry &ref)
3856 {
3857 ref.loc = UNKNOWN_LOCATION;
3858 ref.uid = 0;
3859 ref.var = NULL_TREE;
3860 }
3861
3862 static bool
3863 is_deleted (const source_location_table_entry &ref)
3864 {
3865 return (ref.loc == UNKNOWN_LOCATION
3866 && ref.uid == -1U
3867 && ref.var == NULL_TREE);
3868 }
3869
3870 static bool
3871 is_empty (const source_location_table_entry &ref)
3872 {
3873 return (ref.loc == UNKNOWN_LOCATION
3874 && ref.uid == 0
3875 && ref.var == NULL_TREE);
3876 }
3877
3878 static void
3879 pch_nx (source_location_table_entry &p)
3880 {
3881 extern void gt_pch_nx (source_location_table_entry &);
3882 gt_pch_nx (p);
3883 }
3884
3885 static void
3886 pch_nx (source_location_table_entry &p, gt_pointer_operator op, void *cookie)
3887 {
3888 extern void gt_pch_nx (source_location_table_entry *, gt_pointer_operator,
3889 void *);
3890 gt_pch_nx (&p, op, cookie);
3891 }
3892 };
3893
3894 static GTY(()) hash_table <source_location_table_entry_hash>
3895 *source_location_table;
3896 static GTY(()) unsigned int source_location_id;
3897
3898 /* Fold the __builtin_source_location () call T. */
3899
3900 tree
3901 fold_builtin_source_location (const_tree t)
3902 {
3903 gcc_assert (TREE_CODE (t) == CALL_EXPR);
3904 /* TREE_TYPE (t) is const std::source_location::__impl* */
3905 tree source_location_impl = TREE_TYPE (TREE_TYPE (t));
3906 if (source_location_impl == error_mark_node)
3907 return build_zero_cst (const_ptr_type_node);
3908 gcc_assert (CLASS_TYPE_P (source_location_impl)
3909 && id_equal (TYPE_IDENTIFIER (source_location_impl), "__impl"));
3910
3911 location_t loc = EXPR_LOCATION (t);
3912 if (source_location_table == NULL)
3913 source_location_table
3914 = hash_table <source_location_table_entry_hash>::create_ggc (64);
3915 const line_map_ordinary *map;
3916 source_location_table_entry entry;
3917 entry.loc
3918 = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
3919 &map);
3920 entry.uid = current_function_decl ? DECL_UID (current_function_decl) : -1;
3921 entry.var = error_mark_node;
3922 source_location_table_entry *entryp
3923 = source_location_table->find_slot (entry, INSERT);
3924 tree var;
3925 if (entryp->var)
3926 var = entryp->var;
3927 else
3928 {
3929 char tmp_name[32];
3930 ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lsrc_loc", source_location_id++);
3931 var = build_decl (loc, VAR_DECL, get_identifier (tmp_name),
3932 source_location_impl);
3933 TREE_STATIC (var) = 1;
3934 TREE_PUBLIC (var) = 0;
3935 DECL_ARTIFICIAL (var) = 1;
3936 DECL_IGNORED_P (var) = 1;
3937 DECL_EXTERNAL (var) = 0;
3938 DECL_DECLARED_CONSTEXPR_P (var) = 1;
3939 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = 1;
3940 layout_decl (var, 0);
3941
3942 vec<constructor_elt, va_gc> *v = NULL;
3943 vec_alloc (v, 4);
3944 for (tree field = TYPE_FIELDS (source_location_impl);
3945 (field = next_aggregate_field (field)) != NULL_TREE;
3946 field = DECL_CHAIN (field))
3947 {
3948 const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
3949 tree val = NULL_TREE;
3950 if (strcmp (n, "_M_file_name") == 0)
3951 {
3952 if (const char *fname = LOCATION_FILE (loc))
3953 {
3954 fname = remap_macro_filename (fname);
3955 val = build_string_literal (fname);
3956 }
3957 else
3958 val = build_string_literal ("");
3959 }
3960 else if (strcmp (n, "_M_function_name") == 0)
3961 {
3962 const char *name = "";
3963
3964 if (current_function_decl)
3965 {
3966 /* If this is a coroutine, we should get the name of the user
3967 function rather than the actor we generate. */
3968 if (tree ramp = DECL_RAMP_FN (current_function_decl))
3969 name = cxx_printable_name (ramp, 2);
3970 else
3971 name = cxx_printable_name (current_function_decl, 2);
3972 }
3973
3974 val = build_string_literal (name);
3975 }
3976 else if (strcmp (n, "_M_line") == 0)
3977 val = build_int_cst (TREE_TYPE (field), LOCATION_LINE (loc));
3978 else if (strcmp (n, "_M_column") == 0)
3979 val = build_int_cst (TREE_TYPE (field), LOCATION_COLUMN (loc));
3980 else
3981 gcc_unreachable ();
3982 CONSTRUCTOR_APPEND_ELT (v, field, val);
3983 }
3984
3985 tree ctor = build_constructor (source_location_impl, v);
3986 TREE_CONSTANT (ctor) = 1;
3987 TREE_STATIC (ctor) = 1;
3988 DECL_INITIAL (var) = ctor;
3989 varpool_node::finalize_decl (var);
3990 *entryp = entry;
3991 entryp->var = var;
3992 }
3993
3994 return build_fold_addr_expr_with_type_loc (loc, var, TREE_TYPE (t));
3995 }
3996
3997 #include "gt-cp-cp-gimplify.h"