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