1 /* Perform -*- C++ -*- constant expression evaluation, including calls to 2 constexpr functions. These routines are used both during actual parsing 3 and during the instantiation of template functions. 4 5 Copyright (C) 1998-2022 Free Software Foundation, Inc. 6 7 This file is part of GCC. 8 9 GCC is free software; you can redistribute it and/or modify it 10 under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3, or (at your option) 12 any later version. 13 14 GCC is distributed in the hope that it will be useful, but 15 WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with GCC; see the file COPYING3. If not see 21 <http://www.gnu.org/licenses/>. */ 22 23 #include "config.h" 24 #include "system.h" 25 #include "coretypes.h" 26 #include "cp-tree.h" 27 #include "varasm.h" 28 #include "c-family/c-objc.h" 29 #include "tree-iterator.h" 30 #include "gimplify.h" 31 #include "builtins.h" 32 #include "tree-inline.h" 33 #include "ubsan.h" 34 #include "gimple-fold.h" 35 #include "timevar.h" 36 #include "fold-const-call.h" 37 #include "stor-layout.h" 38 #include "cgraph.h" 39 #include "opts.h" 40 #include "stringpool.h" 41 #include "attribs.h" 42 43 static bool verify_constant (tree, bool, bool *, bool *); 44 #define VERIFY_CONSTANT(X) \ 45 do { \ 46 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ 47 return t; \ 48 } while (0) 49 50 static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, 51 bool insert = false); 52 static int array_index_cmp (tree key, tree index); 53 54 /* Returns true iff FUN is an instantiation of a constexpr function 55 template or a defaulted constexpr function. */ 56 57 bool 58 is_instantiation_of_constexpr (tree fun) 59 { 60 return ((DECL_TEMPLOID_INSTANTIATION (fun) 61 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) 62 || (DECL_DEFAULTED_FN (fun) 63 && DECL_DECLARED_CONSTEXPR_P (fun))); 64 } 65 66 /* Return true if T is a literal type. */ 67 68 bool 69 literal_type_p (tree t) 70 { 71 if (SCALAR_TYPE_P (t) 72 || VECTOR_TYPE_P (t) 73 || TYPE_REF_P (t) 74 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) 75 return true; 76 if (CLASS_TYPE_P (t)) 77 { 78 t = complete_type (t); 79 gcc_assert (COMPLETE_TYPE_P (t) || errorcount); 80 return CLASSTYPE_LITERAL_P (t); 81 } 82 if (TREE_CODE (t) == ARRAY_TYPE) 83 return literal_type_p (strip_array_types (t)); 84 return false; 85 } 86 87 /* If DECL is a variable declared `constexpr', require its type 88 be literal. Return error_mark_node if we give an error, the 89 DECL otherwise. */ 90 91 tree 92 ensure_literal_type_for_constexpr_object (tree decl) 93 { 94 tree type = TREE_TYPE (decl); 95 if (VAR_P (decl) 96 && (DECL_DECLARED_CONSTEXPR_P (decl) 97 || var_in_constexpr_fn (decl)) 98 && !processing_template_decl) 99 { 100 tree stype = strip_array_types (type); 101 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) 102 /* Don't complain here, we'll complain about incompleteness 103 when we try to initialize the variable. */; 104 else if (!literal_type_p (type)) 105 { 106 if (DECL_DECLARED_CONSTEXPR_P (decl)) 107 { 108 auto_diagnostic_group d; 109 error_at (DECL_SOURCE_LOCATION (decl), 110 "the type %qT of %<constexpr%> variable %qD " 111 "is not literal", type, decl); 112 explain_non_literal_class (type); 113 decl = error_mark_node; 114 } 115 else if (cxx_dialect < cxx23) 116 { 117 if (!is_instantiation_of_constexpr (current_function_decl)) 118 { 119 auto_diagnostic_group d; 120 error_at (DECL_SOURCE_LOCATION (decl), 121 "variable %qD of non-literal type %qT in " 122 "%<constexpr%> function only available with " 123 "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type); 124 explain_non_literal_class (type); 125 decl = error_mark_node; 126 } 127 cp_function_chain->invalid_constexpr = true; 128 } 129 } 130 else if (DECL_DECLARED_CONSTEXPR_P (decl) 131 && variably_modified_type_p (type, NULL_TREE)) 132 { 133 error_at (DECL_SOURCE_LOCATION (decl), 134 "%<constexpr%> variable %qD has variably-modified " 135 "type %qT", decl, type); 136 decl = error_mark_node; 137 } 138 } 139 return decl; 140 } 141 142 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> 143 { 144 static hashval_t hash (const constexpr_fundef *); 145 static bool equal (const constexpr_fundef *, const constexpr_fundef *); 146 }; 147 148 /* This table holds all constexpr function definitions seen in 149 the current translation unit. */ 150 151 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; 152 153 /* Utility function used for managing the constexpr function table. 154 Return true if the entries pointed to by P and Q are for the 155 same constexpr function. */ 156 157 inline bool 158 constexpr_fundef_hasher::equal (const constexpr_fundef *lhs, 159 const constexpr_fundef *rhs) 160 { 161 return lhs->decl == rhs->decl; 162 } 163 164 /* Utility function used for managing the constexpr function table. 165 Return a hash value for the entry pointed to by Q. */ 166 167 inline hashval_t 168 constexpr_fundef_hasher::hash (const constexpr_fundef *fundef) 169 { 170 return DECL_UID (fundef->decl); 171 } 172 173 /* Return a previously saved definition of function FUN. */ 174 175 constexpr_fundef * 176 retrieve_constexpr_fundef (tree fun) 177 { 178 if (constexpr_fundef_table == NULL) 179 return NULL; 180 181 constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE }; 182 return constexpr_fundef_table->find (&fundef); 183 } 184 185 /* Check whether the parameter and return types of FUN are valid for a 186 constexpr function, and complain if COMPLAIN. */ 187 188 bool 189 is_valid_constexpr_fn (tree fun, bool complain) 190 { 191 bool ret = true; 192 193 if (DECL_INHERITED_CTOR (fun) 194 && TREE_CODE (fun) == TEMPLATE_DECL) 195 { 196 ret = false; 197 if (complain) 198 error ("inherited constructor %qD is not %<constexpr%>", 199 DECL_INHERITED_CTOR (fun)); 200 } 201 else 202 { 203 for (tree parm = FUNCTION_FIRST_USER_PARM (fun); 204 parm != NULL_TREE; parm = TREE_CHAIN (parm)) 205 if (!literal_type_p (TREE_TYPE (parm))) 206 { 207 ret = false; 208 if (complain) 209 { 210 auto_diagnostic_group d; 211 error ("invalid type for parameter %d of %<constexpr%> " 212 "function %q+#D", DECL_PARM_INDEX (parm), fun); 213 explain_non_literal_class (TREE_TYPE (parm)); 214 } 215 } 216 } 217 218 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) 219 { 220 ret = false; 221 if (complain) 222 inform (DECL_SOURCE_LOCATION (fun), 223 "lambdas are implicitly %<constexpr%> only in C++17 and later"); 224 } 225 else if (DECL_DESTRUCTOR_P (fun)) 226 { 227 if (cxx_dialect < cxx20) 228 { 229 ret = false; 230 if (complain) 231 error_at (DECL_SOURCE_LOCATION (fun), 232 "%<constexpr%> destructors only available" 233 " with %<-std=c++20%> or %<-std=gnu++20%>"); 234 } 235 } 236 else if (!DECL_CONSTRUCTOR_P (fun)) 237 { 238 tree rettype = TREE_TYPE (TREE_TYPE (fun)); 239 if (!literal_type_p (rettype)) 240 { 241 ret = false; 242 if (complain) 243 { 244 auto_diagnostic_group d; 245 error ("invalid return type %qT of %<constexpr%> function %q+D", 246 rettype, fun); 247 explain_non_literal_class (rettype); 248 } 249 } 250 251 /* C++14 DR 1684 removed this restriction. */ 252 if (cxx_dialect < cxx14 253 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) 254 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) 255 { 256 ret = false; 257 if (complain) 258 { 259 auto_diagnostic_group d; 260 if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, 261 "enclosing class of %<constexpr%> non-static" 262 " member function %q+#D is not a literal type", 263 fun)) 264 explain_non_literal_class (DECL_CONTEXT (fun)); 265 } 266 } 267 } 268 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) 269 { 270 ret = false; 271 if (complain) 272 error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); 273 } 274 275 return ret; 276 } 277 278 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF 279 for a member of an anonymous aggregate, INIT is the initializer for that 280 member, and VEC_OUTER is the vector of constructor elements for the class 281 whose constructor we are processing. Add the initializer to the vector 282 and return true to indicate success. */ 283 284 static bool 285 build_anon_member_initialization (tree member, tree init, 286 vec<constructor_elt, va_gc> **vec_outer) 287 { 288 /* MEMBER presents the relevant fields from the inside out, but we need 289 to build up the initializer from the outside in so that we can reuse 290 previously built CONSTRUCTORs if this is, say, the second field in an 291 anonymous struct. So we use a vec as a stack. */ 292 auto_vec<tree, 2> fields; 293 do 294 { 295 fields.safe_push (TREE_OPERAND (member, 1)); 296 member = TREE_OPERAND (member, 0); 297 } 298 while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) 299 && TREE_CODE (member) == COMPONENT_REF); 300 301 /* VEC has the constructor elements vector for the context of FIELD. 302 If FIELD is an anonymous aggregate, we will push inside it. */ 303 vec<constructor_elt, va_gc> **vec = vec_outer; 304 tree field; 305 while (field = fields.pop(), 306 ANON_AGGR_TYPE_P (TREE_TYPE (field))) 307 { 308 tree ctor; 309 /* If there is already an outer constructor entry for the anonymous 310 aggregate FIELD, use it; otherwise, insert one. */ 311 if (vec_safe_is_empty (*vec) 312 || (*vec)->last().index != field) 313 { 314 ctor = build_constructor (TREE_TYPE (field), NULL); 315 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); 316 } 317 else 318 ctor = (*vec)->last().value; 319 vec = &CONSTRUCTOR_ELTS (ctor); 320 } 321 322 /* Now we're at the innermost field, the one that isn't an anonymous 323 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ 324 gcc_assert (fields.is_empty()); 325 CONSTRUCTOR_APPEND_ELT (*vec, field, init); 326 327 return true; 328 } 329 330 /* Subroutine of build_constexpr_constructor_member_initializers. 331 The expression tree T represents a data member initialization 332 in a (constexpr) constructor definition. Build a pairing of 333 the data member with its initializer, and prepend that pair 334 to the existing initialization pair INITS. */ 335 336 static bool 337 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) 338 { 339 tree member, init; 340 if (TREE_CODE (t) == CLEANUP_POINT_EXPR) 341 t = TREE_OPERAND (t, 0); 342 if (TREE_CODE (t) == EXPR_STMT) 343 t = TREE_OPERAND (t, 0); 344 if (t == error_mark_node) 345 return false; 346 if (TREE_CODE (t) == STATEMENT_LIST) 347 { 348 for (tree stmt : tsi_range (t)) 349 if (! build_data_member_initialization (stmt, vec)) 350 return false; 351 return true; 352 } 353 if (TREE_CODE (t) == CLEANUP_STMT) 354 { 355 /* We can't see a CLEANUP_STMT in a constructor for a literal class, 356 but we can in a constexpr constructor for a non-literal class. Just 357 ignore it; either all the initialization will be constant, in which 358 case the cleanup can't run, or it can't be constexpr. 359 Still recurse into CLEANUP_BODY. */ 360 return build_data_member_initialization (CLEANUP_BODY (t), vec); 361 } 362 if (TREE_CODE (t) == CONVERT_EXPR) 363 t = TREE_OPERAND (t, 0); 364 if (TREE_CODE (t) == INIT_EXPR 365 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only 366 use what this function builds for cx_check_missing_mem_inits, and 367 assignment in the ctor body doesn't count. */ 368 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) 369 { 370 member = TREE_OPERAND (t, 0); 371 init = break_out_target_exprs (TREE_OPERAND (t, 1)); 372 } 373 else if (TREE_CODE (t) == CALL_EXPR) 374 { 375 tree fn = get_callee_fndecl (t); 376 if (!fn || !DECL_CONSTRUCTOR_P (fn)) 377 /* We're only interested in calls to subobject constructors. */ 378 return true; 379 member = CALL_EXPR_ARG (t, 0); 380 /* We don't use build_cplus_new here because it complains about 381 abstract bases. Leaving the call unwrapped means that it has the 382 wrong type, but cxx_eval_constant_expression doesn't care. */ 383 init = break_out_target_exprs (t); 384 } 385 else if (TREE_CODE (t) == BIND_EXPR) 386 return build_data_member_initialization (BIND_EXPR_BODY (t), vec); 387 else 388 /* Don't add anything else to the CONSTRUCTOR. */ 389 return true; 390 if (INDIRECT_REF_P (member)) 391 member = TREE_OPERAND (member, 0); 392 if (TREE_CODE (member) == NOP_EXPR) 393 { 394 tree op = member; 395 STRIP_NOPS (op); 396 if (TREE_CODE (op) == ADDR_EXPR) 397 { 398 gcc_assert (same_type_ignoring_top_level_qualifiers_p 399 (TREE_TYPE (TREE_TYPE (op)), 400 TREE_TYPE (TREE_TYPE (member)))); 401 /* Initializing a cv-qualified member; we need to look through 402 the const_cast. */ 403 member = op; 404 } 405 else if (op == current_class_ptr 406 && (same_type_ignoring_top_level_qualifiers_p 407 (TREE_TYPE (TREE_TYPE (member)), 408 current_class_type))) 409 /* Delegating constructor. */ 410 member = op; 411 else 412 { 413 /* This is an initializer for an empty base; keep it for now so 414 we can check it in cxx_eval_bare_aggregate. */ 415 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); 416 } 417 } 418 if (TREE_CODE (member) == ADDR_EXPR) 419 member = TREE_OPERAND (member, 0); 420 if (TREE_CODE (member) == COMPONENT_REF) 421 { 422 tree aggr = TREE_OPERAND (member, 0); 423 if (TREE_CODE (aggr) == VAR_DECL) 424 /* Initializing a local variable, don't add anything. */ 425 return true; 426 if (TREE_CODE (aggr) != COMPONENT_REF) 427 /* Normal member initialization. */ 428 member = TREE_OPERAND (member, 1); 429 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) 430 /* Initializing a member of an anonymous union. */ 431 return build_anon_member_initialization (member, init, vec); 432 else 433 /* We're initializing a vtable pointer in a base. Leave it as 434 COMPONENT_REF so we remember the path to get to the vfield. */ 435 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); 436 } 437 438 /* Value-initialization can produce multiple initializers for the 439 same field; use the last one. */ 440 if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) 441 (*vec)->last().value = init; 442 else 443 CONSTRUCTOR_APPEND_ELT (*vec, member, init); 444 return true; 445 } 446 447 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. 448 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a 449 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ 450 451 static bool 452 check_constexpr_bind_expr_vars (tree t) 453 { 454 gcc_assert (TREE_CODE (t) == BIND_EXPR); 455 456 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) 457 if (TREE_CODE (var) == TYPE_DECL 458 && DECL_IMPLICIT_TYPEDEF_P (var) 459 && !LAMBDA_TYPE_P (TREE_TYPE (var))) 460 return false; 461 return true; 462 } 463 464 /* Subroutine of check_constexpr_ctor_body. */ 465 466 static bool 467 check_constexpr_ctor_body_1 (tree last, tree list) 468 { 469 switch (TREE_CODE (list)) 470 { 471 case DECL_EXPR: 472 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL 473 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) 474 return true; 475 return false; 476 477 case CLEANUP_POINT_EXPR: 478 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), 479 /*complain=*/false); 480 481 case BIND_EXPR: 482 if (!check_constexpr_bind_expr_vars (list) 483 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), 484 /*complain=*/false)) 485 return false; 486 return true; 487 488 case USING_STMT: 489 case STATIC_ASSERT: 490 case DEBUG_BEGIN_STMT: 491 return true; 492 493 default: 494 return false; 495 } 496 } 497 498 /* Make sure that there are no statements after LAST in the constructor 499 body represented by LIST. */ 500 501 bool 502 check_constexpr_ctor_body (tree last, tree list, bool complain) 503 { 504 /* C++14 doesn't require a constexpr ctor to have an empty body. */ 505 if (cxx_dialect >= cxx14) 506 return true; 507 508 bool ok = true; 509 if (TREE_CODE (list) == STATEMENT_LIST) 510 { 511 tree_stmt_iterator i = tsi_last (list); 512 for (; !tsi_end_p (i); tsi_prev (&i)) 513 { 514 tree t = tsi_stmt (i); 515 if (t == last) 516 break; 517 if (!check_constexpr_ctor_body_1 (last, t)) 518 { 519 ok = false; 520 break; 521 } 522 } 523 } 524 else if (list != last 525 && !check_constexpr_ctor_body_1 (last, list)) 526 ok = false; 527 if (!ok) 528 { 529 if (complain) 530 error ("%<constexpr%> constructor does not have empty body"); 531 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; 532 } 533 return ok; 534 } 535 536 /* V is a vector of constructor elements built up for the base and member 537 initializers of a constructor for TYPE. They need to be in increasing 538 offset order, which they might not be yet if TYPE has a primary base 539 which is not first in the base-clause or a vptr and at least one base 540 all of which are non-primary. */ 541 542 static vec<constructor_elt, va_gc> * 543 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) 544 { 545 tree pri = CLASSTYPE_PRIMARY_BINFO (type); 546 tree field_type; 547 unsigned i; 548 constructor_elt *ce; 549 550 if (pri) 551 field_type = BINFO_TYPE (pri); 552 else if (TYPE_CONTAINS_VPTR_P (type)) 553 field_type = vtbl_ptr_type_node; 554 else 555 return v; 556 557 /* Find the element for the primary base or vptr and move it to the 558 beginning of the vec. */ 559 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 560 if (TREE_TYPE (ce->index) == field_type) 561 break; 562 563 if (i > 0 && i < vec_safe_length (v)) 564 { 565 vec<constructor_elt, va_gc> &vref = *v; 566 constructor_elt elt = vref[i]; 567 for (; i > 0; --i) 568 vref[i] = vref[i-1]; 569 vref[0] = elt; 570 } 571 572 return v; 573 } 574 575 /* Build compile-time evalable representations of member-initializer list 576 for a constexpr constructor. */ 577 578 static tree 579 build_constexpr_constructor_member_initializers (tree type, tree body) 580 { 581 vec<constructor_elt, va_gc> *vec = NULL; 582 bool ok = true; 583 while (true) 584 switch (TREE_CODE (body)) 585 { 586 case MUST_NOT_THROW_EXPR: 587 case EH_SPEC_BLOCK: 588 body = TREE_OPERAND (body, 0); 589 break; 590 591 case STATEMENT_LIST: 592 for (tree stmt : tsi_range (body)) 593 { 594 body = stmt; 595 if (TREE_CODE (body) == BIND_EXPR) 596 break; 597 } 598 break; 599 600 case BIND_EXPR: 601 body = BIND_EXPR_BODY (body); 602 goto found; 603 604 default: 605 gcc_unreachable (); 606 } 607 found: 608 if (TREE_CODE (body) == TRY_BLOCK) 609 { 610 body = TREE_OPERAND (body, 0); 611 if (TREE_CODE (body) == BIND_EXPR) 612 body = BIND_EXPR_BODY (body); 613 } 614 if (TREE_CODE (body) == CLEANUP_POINT_EXPR) 615 { 616 body = TREE_OPERAND (body, 0); 617 if (TREE_CODE (body) == EXPR_STMT) 618 body = TREE_OPERAND (body, 0); 619 if (TREE_CODE (body) == INIT_EXPR 620 && (same_type_ignoring_top_level_qualifiers_p 621 (TREE_TYPE (TREE_OPERAND (body, 0)), 622 current_class_type))) 623 { 624 /* Trivial copy. */ 625 return TREE_OPERAND (body, 1); 626 } 627 ok = build_data_member_initialization (body, &vec); 628 } 629 else if (TREE_CODE (body) == STATEMENT_LIST) 630 { 631 for (tree stmt : tsi_range (body)) 632 { 633 ok = build_data_member_initialization (stmt, &vec); 634 if (!ok) 635 break; 636 } 637 } 638 else if (EXPR_P (body)) 639 ok = build_data_member_initialization (body, &vec); 640 else 641 gcc_assert (errorcount > 0); 642 if (ok) 643 { 644 if (vec_safe_length (vec) > 0) 645 { 646 /* In a delegating constructor, return the target. */ 647 constructor_elt *ce = &(*vec)[0]; 648 if (ce->index == current_class_ptr) 649 { 650 body = ce->value; 651 vec_free (vec); 652 return body; 653 } 654 } 655 vec = sort_constexpr_mem_initializers (type, vec); 656 return build_constructor (type, vec); 657 } 658 else 659 return error_mark_node; 660 } 661 662 /* We have an expression tree T that represents a call, either CALL_EXPR 663 or AGGR_INIT_EXPR. If the call is lexically to a named function, 664 retrun the _DECL for that function. */ 665 666 static tree 667 get_function_named_in_call (tree t) 668 { 669 tree fun = cp_get_callee (t); 670 if (fun && TREE_CODE (fun) == ADDR_EXPR 671 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) 672 fun = TREE_OPERAND (fun, 0); 673 return fun; 674 } 675 676 /* Subroutine of check_constexpr_fundef. BODY is the body of a function 677 declared to be constexpr, or a sub-statement thereof. Returns the 678 return value if suitable, error_mark_node for a statement not allowed in 679 a constexpr function, or NULL_TREE if no return value was found. */ 680 681 tree 682 constexpr_fn_retval (tree body) 683 { 684 switch (TREE_CODE (body)) 685 { 686 case STATEMENT_LIST: 687 { 688 tree expr = NULL_TREE; 689 for (tree stmt : tsi_range (body)) 690 { 691 tree s = constexpr_fn_retval (stmt); 692 if (s == error_mark_node) 693 return error_mark_node; 694 else if (s == NULL_TREE) 695 /* Keep iterating. */; 696 else if (expr) 697 /* Multiple return statements. */ 698 return error_mark_node; 699 else 700 expr = s; 701 } 702 return expr; 703 } 704 705 case RETURN_EXPR: 706 return break_out_target_exprs (TREE_OPERAND (body, 0)); 707 708 case DECL_EXPR: 709 { 710 tree decl = DECL_EXPR_DECL (body); 711 if (TREE_CODE (decl) == USING_DECL 712 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ 713 || DECL_ARTIFICIAL (decl)) 714 return NULL_TREE; 715 return error_mark_node; 716 } 717 718 case CLEANUP_POINT_EXPR: 719 return constexpr_fn_retval (TREE_OPERAND (body, 0)); 720 721 case BIND_EXPR: 722 if (!check_constexpr_bind_expr_vars (body)) 723 return error_mark_node; 724 return constexpr_fn_retval (BIND_EXPR_BODY (body)); 725 726 case USING_STMT: 727 case DEBUG_BEGIN_STMT: 728 return NULL_TREE; 729 730 case CALL_EXPR: 731 { 732 tree fun = get_function_named_in_call (body); 733 if (fun != NULL_TREE 734 && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) 735 return NULL_TREE; 736 } 737 /* Fallthru. */ 738 739 default: 740 return error_mark_node; 741 } 742 } 743 744 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of 745 FUN; do the necessary transformations to turn it into a single expression 746 that we can store in the hash table. */ 747 748 static tree 749 massage_constexpr_body (tree fun, tree body) 750 { 751 if (DECL_CONSTRUCTOR_P (fun)) 752 body = build_constexpr_constructor_member_initializers 753 (DECL_CONTEXT (fun), body); 754 else if (cxx_dialect < cxx14) 755 { 756 if (TREE_CODE (body) == EH_SPEC_BLOCK) 757 body = EH_SPEC_STMTS (body); 758 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) 759 body = TREE_OPERAND (body, 0); 760 body = constexpr_fn_retval (body); 761 } 762 return body; 763 } 764 765 /* CTYPE is a type constructed from BODY. Return true if some 766 bases/fields are uninitialized, and complain if COMPLAIN. */ 767 768 static bool 769 cx_check_missing_mem_inits (tree ctype, tree body, bool complain) 770 { 771 /* We allow uninitialized bases/fields in C++20. */ 772 if (cxx_dialect >= cxx20) 773 return false; 774 775 unsigned nelts = 0; 776 777 if (body) 778 { 779 if (TREE_CODE (body) != CONSTRUCTOR) 780 return false; 781 nelts = CONSTRUCTOR_NELTS (body); 782 } 783 tree field = TYPE_FIELDS (ctype); 784 785 if (TREE_CODE (ctype) == UNION_TYPE) 786 { 787 if (nelts == 0 && next_initializable_field (field)) 788 { 789 if (complain) 790 error ("%<constexpr%> constructor for union %qT must " 791 "initialize exactly one non-static data member", ctype); 792 return true; 793 } 794 return false; 795 } 796 797 /* Iterate over the CONSTRUCTOR, checking any missing fields don't 798 need an explicit initialization. */ 799 bool bad = false; 800 for (unsigned i = 0; i <= nelts; ++i) 801 { 802 tree index = NULL_TREE; 803 if (i < nelts) 804 { 805 index = CONSTRUCTOR_ELT (body, i)->index; 806 /* Skip base and vtable inits. */ 807 if (TREE_CODE (index) != FIELD_DECL 808 || DECL_ARTIFICIAL (index)) 809 continue; 810 } 811 812 for (; field != index; field = DECL_CHAIN (field)) 813 { 814 tree ftype; 815 if (TREE_CODE (field) != FIELD_DECL) 816 continue; 817 if (DECL_UNNAMED_BIT_FIELD (field)) 818 continue; 819 if (DECL_ARTIFICIAL (field)) 820 continue; 821 if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 822 { 823 /* Recurse to check the anonymous aggregate member. */ 824 bad |= cx_check_missing_mem_inits 825 (TREE_TYPE (field), NULL_TREE, complain); 826 if (bad && !complain) 827 return true; 828 continue; 829 } 830 ftype = TREE_TYPE (field); 831 if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype)) 832 /* A flexible array can't be intialized here, so don't complain 833 that it isn't. */ 834 continue; 835 if (is_empty_field (field)) 836 /* An empty field doesn't need an initializer. */ 837 continue; 838 ftype = strip_array_types (ftype); 839 if (type_has_constexpr_default_constructor (ftype)) 840 { 841 /* It's OK to skip a member with a trivial constexpr ctor. 842 A constexpr ctor that isn't trivial should have been 843 added in by now. */ 844 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) 845 || errorcount != 0); 846 continue; 847 } 848 if (!complain) 849 return true; 850 auto_diagnostic_group d; 851 error ("member %qD must be initialized by mem-initializer " 852 "in %<constexpr%> constructor", field); 853 inform (DECL_SOURCE_LOCATION (field), "declared here"); 854 bad = true; 855 } 856 if (field == NULL_TREE) 857 break; 858 859 if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) 860 { 861 /* Check the anonymous aggregate initializer is valid. */ 862 bad |= cx_check_missing_mem_inits 863 (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); 864 if (bad && !complain) 865 return true; 866 } 867 field = DECL_CHAIN (field); 868 } 869 870 return bad; 871 } 872 873 /* We are processing the definition of the constexpr function FUN. 874 Check that its body fulfills the apropriate requirements and 875 enter it in the constexpr function definition table. */ 876 877 void 878 maybe_save_constexpr_fundef (tree fun) 879 { 880 if (processing_template_decl 881 || cp_function_chain->invalid_constexpr 882 || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) 883 return; 884 885 /* With -fimplicit-constexpr, try to make inlines constexpr. We'll 886 actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */ 887 bool implicit = false; 888 if (flag_implicit_constexpr) 889 { 890 if (DECL_DELETING_DESTRUCTOR_P (fun) 891 && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun))) 892 /* Don't inherit implicit constexpr from the non-deleting 893 destructor. */ 894 DECL_DECLARED_CONSTEXPR_P (fun) = false; 895 896 if (!DECL_DECLARED_CONSTEXPR_P (fun) 897 && DECL_DECLARED_INLINE_P (fun) 898 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun))) 899 implicit = true; 900 } 901 902 if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit) 903 return; 904 905 bool complain = !DECL_GENERATED_P (fun) && !implicit; 906 907 if (!is_valid_constexpr_fn (fun, complain)) 908 return; 909 910 tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); 911 if (massaged == NULL_TREE || massaged == error_mark_node) 912 { 913 if (!DECL_CONSTRUCTOR_P (fun) && complain) 914 error ("body of %<constexpr%> function %qD not a return-statement", 915 fun); 916 return; 917 } 918 919 bool potential = potential_rvalue_constant_expression (massaged); 920 if (!potential && complain) 921 require_potential_rvalue_constant_expression (massaged); 922 923 if (DECL_CONSTRUCTOR_P (fun) && potential 924 && !DECL_DEFAULTED_FN (fun)) 925 { 926 if (cx_check_missing_mem_inits (DECL_CONTEXT (fun), 927 massaged, complain)) 928 potential = false; 929 else if (cxx_dialect > cxx11) 930 { 931 /* What we got from massage_constexpr_body is pretty much just the 932 ctor-initializer, also check the body. */ 933 massaged = DECL_SAVED_TREE (fun); 934 potential = potential_rvalue_constant_expression (massaged); 935 if (!potential && complain) 936 require_potential_rvalue_constant_expression (massaged); 937 } 938 } 939 940 if (!potential && complain) 941 return; 942 943 if (implicit) 944 { 945 if (potential) 946 { 947 DECL_DECLARED_CONSTEXPR_P (fun) = true; 948 DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true; 949 if (DECL_CONSTRUCTOR_P (fun)) 950 TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true; 951 } 952 else 953 /* Don't bother keeping the pre-generic body of unsuitable functions 954 not explicitly declared constexpr. */ 955 return; 956 } 957 958 constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE}; 959 bool clear_ctx = false; 960 if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) 961 { 962 clear_ctx = true; 963 DECL_CONTEXT (DECL_RESULT (fun)) = fun; 964 } 965 tree saved_fn = current_function_decl; 966 current_function_decl = fun; 967 entry.body = copy_fn (entry.decl, entry.parms, entry.result); 968 current_function_decl = saved_fn; 969 if (clear_ctx) 970 DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE; 971 if (!potential) 972 /* For a template instantiation, we want to remember the pre-generic body 973 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression 974 that it doesn't need to bother trying to expand the function. */ 975 entry.result = error_mark_node; 976 977 register_constexpr_fundef (entry); 978 } 979 980 /* BODY is a validated and massaged definition of a constexpr 981 function. Register it in the hash table. */ 982 983 void 984 register_constexpr_fundef (const constexpr_fundef &value) 985 { 986 /* Create the constexpr function table if necessary. */ 987 if (constexpr_fundef_table == NULL) 988 constexpr_fundef_table 989 = hash_table<constexpr_fundef_hasher>::create_ggc (101); 990 991 constexpr_fundef **slot = constexpr_fundef_table->find_slot 992 (const_cast<constexpr_fundef *> (&value), INSERT); 993 994 gcc_assert (*slot == NULL); 995 *slot = ggc_alloc<constexpr_fundef> (); 996 **slot = value; 997 } 998 999 /* FUN is a non-constexpr function called in a context that requires a 1000 constant expression. If it comes from a constexpr template, explain why 1001 the instantiation isn't constexpr. */ 1002 1003 void 1004 explain_invalid_constexpr_fn (tree fun) 1005 { 1006 static hash_set<tree> *diagnosed; 1007 tree body; 1008 /* Only diagnose defaulted functions, lambdas, or instantiations. */ 1009 if (!DECL_DEFAULTED_FN (fun) 1010 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) 1011 && !is_instantiation_of_constexpr (fun)) 1012 { 1013 inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun); 1014 return; 1015 } 1016 if (diagnosed == NULL) 1017 diagnosed = new hash_set<tree>; 1018 if (diagnosed->add (fun)) 1019 /* Already explained. */ 1020 return; 1021 1022 iloc_sentinel ils = input_location; 1023 if (!lambda_static_thunk_p (fun)) 1024 { 1025 /* Diagnostics should completely ignore the static thunk, so leave 1026 input_location set to our caller's location. */ 1027 input_location = DECL_SOURCE_LOCATION (fun); 1028 inform (input_location, 1029 "%qD is not usable as a %<constexpr%> function because:", fun); 1030 } 1031 /* First check the declaration. */ 1032 if (is_valid_constexpr_fn (fun, true)) 1033 { 1034 /* Then if it's OK, the body. */ 1035 if (!DECL_DECLARED_CONSTEXPR_P (fun) 1036 && DECL_DEFAULTED_FN (fun)) 1037 explain_implicit_non_constexpr (fun); 1038 else 1039 { 1040 if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun)) 1041 body = fd->body; 1042 else 1043 body = DECL_SAVED_TREE (fun); 1044 body = massage_constexpr_body (fun, body); 1045 require_potential_rvalue_constant_expression (body); 1046 if (DECL_CONSTRUCTOR_P (fun)) 1047 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); 1048 } 1049 } 1050 } 1051 1052 /* Objects of this type represent calls to constexpr functions 1053 along with the bindings of parameters to their arguments, for 1054 the purpose of compile time evaluation. */ 1055 1056 struct GTY((for_user)) constexpr_call { 1057 /* Description of the constexpr function definition. */ 1058 constexpr_fundef *fundef; 1059 /* Parameter bindings environment. A TREE_VEC of arguments. */ 1060 tree bindings; 1061 /* Result of the call. 1062 NULL means the call is being evaluated. 1063 error_mark_node means that the evaluation was erroneous; 1064 otherwise, the actuall value of the call. */ 1065 tree result; 1066 /* The hash of this call; we remember it here to avoid having to 1067 recalculate it when expanding the hash table. */ 1068 hashval_t hash; 1069 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */ 1070 bool manifestly_const_eval; 1071 }; 1072 1073 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> 1074 { 1075 static hashval_t hash (constexpr_call *); 1076 static bool equal (constexpr_call *, constexpr_call *); 1077 }; 1078 1079 enum constexpr_switch_state { 1080 /* Used when processing a switch for the first time by cxx_eval_switch_expr 1081 and default: label for that switch has not been seen yet. */ 1082 css_default_not_seen, 1083 /* Used when processing a switch for the first time by cxx_eval_switch_expr 1084 and default: label for that switch has been seen already. */ 1085 css_default_seen, 1086 /* Used when processing a switch for the second time by 1087 cxx_eval_switch_expr, where default: label should match. */ 1088 css_default_processing 1089 }; 1090 1091 /* The constexpr expansion context part which needs one instance per 1092 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of 1093 variables initialized within the expression. */ 1094 1095 struct constexpr_global_ctx { 1096 /* Values for any temporaries or local variables within the 1097 constant-expression. */ 1098 hash_map<tree,tree> values; 1099 /* Number of cxx_eval_constant_expression calls (except skipped ones, 1100 on simple constants or location wrappers) encountered during current 1101 cxx_eval_outermost_constant_expr call. */ 1102 HOST_WIDE_INT constexpr_ops_count; 1103 /* Heap VAR_DECLs created during the evaluation of the outermost constant 1104 expression. */ 1105 auto_vec<tree, 16> heap_vars; 1106 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */ 1107 vec<tree> *cleanups; 1108 /* Number of heap VAR_DECL deallocations. */ 1109 unsigned heap_dealloc_count; 1110 /* Constructor. */ 1111 constexpr_global_ctx () 1112 : constexpr_ops_count (0), cleanups (NULL), heap_dealloc_count (0) {} 1113 }; 1114 1115 /* The constexpr expansion context. CALL is the current function 1116 expansion, CTOR is the current aggregate initializer, OBJECT is the 1117 object being initialized by CTOR, either a VAR_DECL or a _REF. */ 1118 1119 struct constexpr_ctx { 1120 /* The part of the context that needs to be unique to the whole 1121 cxx_eval_outermost_constant_expr invocation. */ 1122 constexpr_global_ctx *global; 1123 /* The innermost call we're evaluating. */ 1124 constexpr_call *call; 1125 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen 1126 within the current LOOP_EXPR. NULL if we aren't inside a loop. */ 1127 vec<tree> *save_exprs; 1128 /* The CONSTRUCTOR we're currently building up for an aggregate 1129 initializer. */ 1130 tree ctor; 1131 /* The object we're building the CONSTRUCTOR for. */ 1132 tree object; 1133 /* If inside SWITCH_EXPR. */ 1134 constexpr_switch_state *css_state; 1135 /* The aggregate initialization context inside which this one is nested. This 1136 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */ 1137 const constexpr_ctx *parent; 1138 1139 /* Whether we should error on a non-constant expression or fail quietly. 1140 This flag needs to be here, but some of the others could move to global 1141 if they get larger than a word. */ 1142 bool quiet; 1143 /* Whether we are strictly conforming to constant expression rules or 1144 trying harder to get a constant value. */ 1145 bool strict; 1146 /* Whether __builtin_is_constant_evaluated () should be true. */ 1147 bool manifestly_const_eval; 1148 }; 1149 1150 /* This internal flag controls whether we should avoid doing anything during 1151 constexpr evaluation that would cause extra DECL_UID generation, such as 1152 template instantiation and function body copying. */ 1153 1154 static bool uid_sensitive_constexpr_evaluation_value; 1155 1156 /* An internal counter that keeps track of the number of times 1157 uid_sensitive_constexpr_evaluation_p returned true. */ 1158 1159 static unsigned uid_sensitive_constexpr_evaluation_true_counter; 1160 1161 /* The accessor for uid_sensitive_constexpr_evaluation_value which also 1162 increments the corresponding counter. */ 1163 1164 static bool 1165 uid_sensitive_constexpr_evaluation_p () 1166 { 1167 if (uid_sensitive_constexpr_evaluation_value) 1168 { 1169 ++uid_sensitive_constexpr_evaluation_true_counter; 1170 return true; 1171 } 1172 else 1173 return false; 1174 } 1175 1176 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel 1177 enables the internal flag for uid_sensitive_constexpr_evaluation_p 1178 during the lifetime of the sentinel object. Upon its destruction, the 1179 previous value of uid_sensitive_constexpr_evaluation_p is restored. */ 1180 1181 uid_sensitive_constexpr_evaluation_sentinel 1182 ::uid_sensitive_constexpr_evaluation_sentinel () 1183 : ovr (uid_sensitive_constexpr_evaluation_value, true) 1184 { 1185 } 1186 1187 /* The default constructor for uid_sensitive_constexpr_evaluation_checker 1188 records the current number of times that uid_sensitive_constexpr_evaluation_p 1189 has been called and returned true. */ 1190 1191 uid_sensitive_constexpr_evaluation_checker 1192 ::uid_sensitive_constexpr_evaluation_checker () 1193 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter) 1194 { 1195 } 1196 1197 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and 1198 some constexpr evaluation was restricted due to u_s_c_e_p being called 1199 and returning true during the lifetime of this checker object. */ 1200 1201 bool 1202 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const 1203 { 1204 return (uid_sensitive_constexpr_evaluation_value 1205 && saved_counter != uid_sensitive_constexpr_evaluation_true_counter); 1206 } 1207 1208 1209 /* A table of all constexpr calls that have been evaluated by the 1210 compiler in this translation unit. */ 1211 1212 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; 1213 1214 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, 1215 bool, bool *, bool *, tree * = NULL); 1216 1217 /* Compute a hash value for a constexpr call representation. */ 1218 1219 inline hashval_t 1220 constexpr_call_hasher::hash (constexpr_call *info) 1221 { 1222 return info->hash; 1223 } 1224 1225 /* Return true if the objects pointed to by P and Q represent calls 1226 to the same constexpr function with the same arguments. 1227 Otherwise, return false. */ 1228 1229 bool 1230 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) 1231 { 1232 if (lhs == rhs) 1233 return true; 1234 if (lhs->hash != rhs->hash) 1235 return false; 1236 if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) 1237 return false; 1238 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) 1239 return false; 1240 return cp_tree_equal (lhs->bindings, rhs->bindings); 1241 } 1242 1243 /* Initialize the constexpr call table, if needed. */ 1244 1245 static void 1246 maybe_initialize_constexpr_call_table (void) 1247 { 1248 if (constexpr_call_table == NULL) 1249 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); 1250 } 1251 1252 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when 1253 a function happens to get called recursively, we unshare the callee 1254 function's body and evaluate this unshared copy instead of evaluating the 1255 original body. 1256 1257 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function 1258 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map 1259 that's keyed off of the original FUNCTION_DECL and whose value is a 1260 TREE_LIST of this function's unused copies awaiting reuse. 1261 1262 This is not GC-deletable to avoid GC affecting UID generation. */ 1263 1264 static GTY(()) decl_tree_map *fundef_copies_table; 1265 1266 /* Reuse a copy or create a new unshared copy of the function FUN. 1267 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE 1268 is parms, TYPE is result. */ 1269 1270 static tree 1271 get_fundef_copy (constexpr_fundef *fundef) 1272 { 1273 tree copy; 1274 bool existed; 1275 tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> 1276 (fundef_copies_table, fundef->decl, &existed, 127)); 1277 1278 if (!existed) 1279 { 1280 /* There is no cached function available, or in use. We can use 1281 the function directly. That the slot is now created records 1282 that this function is now in use. */ 1283 copy = build_tree_list (fundef->body, fundef->parms); 1284 TREE_TYPE (copy) = fundef->result; 1285 } 1286 else if (*slot == NULL_TREE) 1287 { 1288 if (uid_sensitive_constexpr_evaluation_p ()) 1289 return NULL_TREE; 1290 1291 /* We've already used the function itself, so make a copy. */ 1292 copy = build_tree_list (NULL, NULL); 1293 tree saved_body = DECL_SAVED_TREE (fundef->decl); 1294 tree saved_parms = DECL_ARGUMENTS (fundef->decl); 1295 tree saved_result = DECL_RESULT (fundef->decl); 1296 tree saved_fn = current_function_decl; 1297 DECL_SAVED_TREE (fundef->decl) = fundef->body; 1298 DECL_ARGUMENTS (fundef->decl) = fundef->parms; 1299 DECL_RESULT (fundef->decl) = fundef->result; 1300 current_function_decl = fundef->decl; 1301 TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), 1302 TREE_TYPE (copy)); 1303 current_function_decl = saved_fn; 1304 DECL_RESULT (fundef->decl) = saved_result; 1305 DECL_ARGUMENTS (fundef->decl) = saved_parms; 1306 DECL_SAVED_TREE (fundef->decl) = saved_body; 1307 } 1308 else 1309 { 1310 /* We have a cached function available. */ 1311 copy = *slot; 1312 *slot = TREE_CHAIN (copy); 1313 } 1314 1315 return copy; 1316 } 1317 1318 /* Save the copy COPY of function FUN for later reuse by 1319 get_fundef_copy(). By construction, there will always be an entry 1320 to find. */ 1321 1322 static void 1323 save_fundef_copy (tree fun, tree copy) 1324 { 1325 tree *slot = fundef_copies_table->get (fun); 1326 TREE_CHAIN (copy) = *slot; 1327 *slot = copy; 1328 } 1329 1330 /* We have an expression tree T that represents a call, either CALL_EXPR 1331 or AGGR_INIT_EXPR. Return the Nth argument. */ 1332 1333 static inline tree 1334 get_nth_callarg (tree t, int n) 1335 { 1336 switch (TREE_CODE (t)) 1337 { 1338 case CALL_EXPR: 1339 return CALL_EXPR_ARG (t, n); 1340 1341 case AGGR_INIT_EXPR: 1342 return AGGR_INIT_EXPR_ARG (t, n); 1343 1344 default: 1345 gcc_unreachable (); 1346 return NULL; 1347 } 1348 } 1349 1350 /* Attempt to evaluate T which represents a call to a builtin function. 1351 We assume here that all builtin functions evaluate to scalar types 1352 represented by _CST nodes. */ 1353 1354 static tree 1355 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, 1356 bool lval, 1357 bool *non_constant_p, bool *overflow_p) 1358 { 1359 const int nargs = call_expr_nargs (t); 1360 tree *args = (tree *) alloca (nargs * sizeof (tree)); 1361 tree new_call; 1362 int i; 1363 1364 /* Don't fold __builtin_constant_p within a constexpr function. */ 1365 bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); 1366 1367 /* If we aren't requiring a constant expression, defer __builtin_constant_p 1368 in a constexpr function until we have values for the parameters. */ 1369 if (bi_const_p 1370 && !ctx->manifestly_const_eval 1371 && current_function_decl 1372 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) 1373 { 1374 *non_constant_p = true; 1375 return t; 1376 } 1377 1378 /* For __builtin_is_constant_evaluated, defer it if not 1379 ctx->manifestly_const_eval (as sometimes we try to constant evaluate 1380 without manifestly_const_eval even expressions or parts thereof which 1381 will later be manifestly const_eval evaluated), otherwise fold it to 1382 true. */ 1383 if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, 1384 BUILT_IN_FRONTEND)) 1385 { 1386 if (!ctx->manifestly_const_eval) 1387 { 1388 *non_constant_p = true; 1389 return t; 1390 } 1391 return boolean_true_node; 1392 } 1393 1394 if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND)) 1395 { 1396 temp_override<tree> ovr (current_function_decl); 1397 if (ctx->call && ctx->call->fundef) 1398 current_function_decl = ctx->call->fundef->decl; 1399 return fold_builtin_source_location (EXPR_LOCATION (t)); 1400 } 1401 1402 int strops = 0; 1403 int strret = 0; 1404 if (fndecl_built_in_p (fun, BUILT_IN_NORMAL)) 1405 switch (DECL_FUNCTION_CODE (fun)) 1406 { 1407 case BUILT_IN_STRLEN: 1408 case BUILT_IN_STRNLEN: 1409 strops = 1; 1410 break; 1411 case BUILT_IN_MEMCHR: 1412 case BUILT_IN_STRCHR: 1413 case BUILT_IN_STRRCHR: 1414 strops = 1; 1415 strret = 1; 1416 break; 1417 case BUILT_IN_MEMCMP: 1418 case BUILT_IN_STRCMP: 1419 strops = 2; 1420 break; 1421 case BUILT_IN_STRSTR: 1422 strops = 2; 1423 strret = 1; 1424 break; 1425 case BUILT_IN_ASAN_POINTER_COMPARE: 1426 case BUILT_IN_ASAN_POINTER_SUBTRACT: 1427 /* These builtins shall be ignored during constant expression 1428 evaluation. */ 1429 return void_node; 1430 default: 1431 break; 1432 } 1433 1434 /* Be permissive for arguments to built-ins; __builtin_constant_p should 1435 return constant false for a non-constant argument. */ 1436 constexpr_ctx new_ctx = *ctx; 1437 new_ctx.quiet = true; 1438 for (i = 0; i < nargs; ++i) 1439 { 1440 tree arg = CALL_EXPR_ARG (t, i); 1441 tree oarg = arg; 1442 1443 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since 1444 expand_builtin doesn't know how to look in the values table. */ 1445 bool strop = i < strops; 1446 if (strop) 1447 { 1448 STRIP_NOPS (arg); 1449 if (TREE_CODE (arg) == ADDR_EXPR) 1450 arg = TREE_OPERAND (arg, 0); 1451 else 1452 strop = false; 1453 } 1454 1455 /* If builtin_valid_in_constant_expr_p is true, 1456 potential_constant_expression_1 has not recursed into the arguments 1457 of the builtin, verify it here. */ 1458 if (!builtin_valid_in_constant_expr_p (fun) 1459 || potential_constant_expression (arg)) 1460 { 1461 bool dummy1 = false, dummy2 = false; 1462 arg = cxx_eval_constant_expression (&new_ctx, arg, false, 1463 &dummy1, &dummy2); 1464 } 1465 1466 if (bi_const_p) 1467 /* For __builtin_constant_p, fold all expressions with constant values 1468 even if they aren't C++ constant-expressions. */ 1469 arg = cp_fold_rvalue (arg); 1470 else if (strop) 1471 { 1472 if (TREE_CODE (arg) == CONSTRUCTOR) 1473 arg = braced_lists_to_strings (TREE_TYPE (arg), arg); 1474 if (TREE_CODE (arg) == STRING_CST) 1475 arg = build_address (arg); 1476 else 1477 arg = oarg; 1478 } 1479 1480 args[i] = arg; 1481 } 1482 1483 bool save_ffbcp = force_folding_builtin_constant_p; 1484 force_folding_builtin_constant_p |= ctx->manifestly_const_eval; 1485 tree save_cur_fn = current_function_decl; 1486 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ 1487 if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) 1488 && ctx->call 1489 && ctx->call->fundef) 1490 current_function_decl = ctx->call->fundef->decl; 1491 if (fndecl_built_in_p (fun, 1492 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, 1493 BUILT_IN_FRONTEND)) 1494 { 1495 location_t loc = EXPR_LOCATION (t); 1496 if (nargs >= 1) 1497 VERIFY_CONSTANT (args[0]); 1498 new_call 1499 = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs, 1500 args); 1501 } 1502 else if (fndecl_built_in_p (fun, 1503 CP_BUILT_IN_IS_CORRESPONDING_MEMBER, 1504 BUILT_IN_FRONTEND)) 1505 { 1506 location_t loc = EXPR_LOCATION (t); 1507 if (nargs >= 2) 1508 { 1509 VERIFY_CONSTANT (args[0]); 1510 VERIFY_CONSTANT (args[1]); 1511 } 1512 new_call = fold_builtin_is_corresponding_member (loc, nargs, args); 1513 } 1514 else 1515 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), 1516 CALL_EXPR_FN (t), nargs, args); 1517 current_function_decl = save_cur_fn; 1518 force_folding_builtin_constant_p = save_ffbcp; 1519 if (new_call == NULL) 1520 { 1521 if (!*non_constant_p && !ctx->quiet) 1522 { 1523 /* Do not allow__builtin_unreachable in constexpr function. 1524 The __builtin_unreachable call with BUILTINS_LOCATION 1525 comes from cp_maybe_instrument_return. */ 1526 if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE) 1527 && EXPR_LOCATION (t) == BUILTINS_LOCATION) 1528 error ("%<constexpr%> call flows off the end of the function"); 1529 else 1530 { 1531 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), 1532 CALL_EXPR_FN (t), nargs, args); 1533 error ("%q+E is not a constant expression", new_call); 1534 } 1535 } 1536 *non_constant_p = true; 1537 return t; 1538 } 1539 1540 if (!potential_constant_expression (new_call)) 1541 { 1542 if (!*non_constant_p && !ctx->quiet) 1543 error ("%q+E is not a constant expression", new_call); 1544 *non_constant_p = true; 1545 return t; 1546 } 1547 1548 if (strret) 1549 { 1550 /* memchr returns a pointer into the first argument, but we replaced the 1551 argument above with a STRING_CST; put it back it now. */ 1552 tree op = CALL_EXPR_ARG (t, strret-1); 1553 STRIP_NOPS (new_call); 1554 if (TREE_CODE (new_call) == POINTER_PLUS_EXPR) 1555 TREE_OPERAND (new_call, 0) = op; 1556 else if (TREE_CODE (new_call) == ADDR_EXPR) 1557 new_call = op; 1558 } 1559 1560 return cxx_eval_constant_expression (&new_ctx, new_call, lval, 1561 non_constant_p, overflow_p); 1562 } 1563 1564 /* TEMP is the constant value of a temporary object of type TYPE. Adjust 1565 the type of the value to match. */ 1566 1567 static tree 1568 adjust_temp_type (tree type, tree temp) 1569 { 1570 if (same_type_p (TREE_TYPE (temp), type)) 1571 return temp; 1572 /* Avoid wrapping an aggregate value in a NOP_EXPR. */ 1573 if (TREE_CODE (temp) == CONSTRUCTOR) 1574 { 1575 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ 1576 tree t = copy_node (temp); 1577 TREE_TYPE (t) = type; 1578 return t; 1579 } 1580 if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) 1581 return build0 (EMPTY_CLASS_EXPR, type); 1582 gcc_assert (scalarish_type_p (type)); 1583 /* Now we know we're dealing with a scalar, and a prvalue of non-class 1584 type is cv-unqualified. */ 1585 return cp_fold_convert (cv_unqualified (type), temp); 1586 } 1587 1588 /* If T is a CONSTRUCTOR, return an unshared copy of T and any 1589 sub-CONSTRUCTORs. Otherwise return T. 1590 1591 We use this whenever we initialize an object as a whole, whether it's a 1592 parameter, a local variable, or a subobject, so that subsequent 1593 modifications don't affect other places where it was used. */ 1594 1595 tree 1596 unshare_constructor (tree t MEM_STAT_DECL) 1597 { 1598 if (!t || TREE_CODE (t) != CONSTRUCTOR) 1599 return t; 1600 auto_vec <tree*, 4> ptrs; 1601 ptrs.safe_push (&t); 1602 while (!ptrs.is_empty ()) 1603 { 1604 tree *p = ptrs.pop (); 1605 tree n = copy_node (*p PASS_MEM_STAT); 1606 CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); 1607 *p = n; 1608 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n); 1609 constructor_elt *ce; 1610 for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i) 1611 if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) 1612 ptrs.safe_push (&ce->value); 1613 } 1614 return t; 1615 } 1616 1617 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */ 1618 1619 static void 1620 free_constructor (tree t) 1621 { 1622 if (!t || TREE_CODE (t) != CONSTRUCTOR) 1623 return; 1624 releasing_vec ctors; 1625 vec_safe_push (ctors, t); 1626 while (!ctors->is_empty ()) 1627 { 1628 tree c = ctors->pop (); 1629 if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c)) 1630 { 1631 constructor_elt *ce; 1632 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) 1633 if (TREE_CODE (ce->value) == CONSTRUCTOR) 1634 vec_safe_push (ctors, ce->value); 1635 ggc_free (elts); 1636 } 1637 ggc_free (c); 1638 } 1639 } 1640 1641 /* Helper function of cxx_bind_parameters_in_call. Return non-NULL 1642 if *TP is address of a static variable (or part of it) currently being 1643 constructed or of a heap artificial variable. */ 1644 1645 static tree 1646 addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data) 1647 { 1648 if (TREE_CODE (*tp) == ADDR_EXPR) 1649 if (tree var = get_base_address (TREE_OPERAND (*tp, 0))) 1650 if (VAR_P (var) && TREE_STATIC (var)) 1651 { 1652 if (DECL_NAME (var) == heap_uninit_identifier 1653 || DECL_NAME (var) == heap_identifier 1654 || DECL_NAME (var) == heap_vec_uninit_identifier 1655 || DECL_NAME (var) == heap_vec_identifier) 1656 return var; 1657 1658 constexpr_global_ctx *global = (constexpr_global_ctx *) data; 1659 if (global->values.get (var)) 1660 return var; 1661 } 1662 if (TYPE_P (*tp)) 1663 *walk_subtrees = false; 1664 return NULL_TREE; 1665 } 1666 1667 /* Subroutine of cxx_eval_call_expression. 1668 We are processing a call expression (either CALL_EXPR or 1669 AGGR_INIT_EXPR) in the context of CTX. Evaluate 1670 all arguments and bind their values to correspondings 1671 parameters, making up the NEW_CALL context. */ 1672 1673 static tree 1674 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun, 1675 bool *non_constant_p, bool *overflow_p, 1676 bool *non_constant_args) 1677 { 1678 const int nargs = call_expr_nargs (t); 1679 tree parms = DECL_ARGUMENTS (fun); 1680 int i; 1681 /* We don't record ellipsis args below. */ 1682 int nparms = list_length (parms); 1683 int nbinds = nargs < nparms ? nargs : nparms; 1684 tree binds = make_tree_vec (nbinds); 1685 for (i = 0; i < nargs; ++i) 1686 { 1687 tree x, arg; 1688 tree type = parms ? TREE_TYPE (parms) : void_type_node; 1689 if (parms && DECL_BY_REFERENCE (parms)) 1690 type = TREE_TYPE (type); 1691 x = get_nth_callarg (t, i); 1692 /* For member function, the first argument is a pointer to the implied 1693 object. For a constructor, it might still be a dummy object, in 1694 which case we get the real argument from ctx. */ 1695 if (i == 0 && DECL_CONSTRUCTOR_P (fun) 1696 && is_dummy_object (x)) 1697 { 1698 x = ctx->object; 1699 x = build_address (x); 1700 } 1701 if (TREE_ADDRESSABLE (type)) 1702 /* Undo convert_for_arg_passing work here. */ 1703 x = convert_from_reference (x); 1704 /* Normally we would strip a TARGET_EXPR in an initialization context 1705 such as this, but here we do the elision differently: we keep the 1706 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ 1707 arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false, 1708 non_constant_p, overflow_p); 1709 /* Don't VERIFY_CONSTANT here. */ 1710 if (*non_constant_p && ctx->quiet) 1711 break; 1712 /* Just discard ellipsis args after checking their constantitude. */ 1713 if (!parms) 1714 continue; 1715 1716 if (!*non_constant_p) 1717 { 1718 /* Make sure the binding has the same type as the parm. But 1719 only for constant args. */ 1720 if (!TYPE_REF_P (type)) 1721 arg = adjust_temp_type (type, arg); 1722 if (!TREE_CONSTANT (arg)) 1723 *non_constant_args = true; 1724 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 1725 /* The destructor needs to see any modifications the callee makes 1726 to the argument. */ 1727 *non_constant_args = true; 1728 /* If arg is or contains address of a heap artificial variable or 1729 of a static variable being constructed, avoid caching the 1730 function call, as those variables might be modified by the 1731 function, or might be modified by the callers in between 1732 the cached function and just read by the function. */ 1733 else if (!*non_constant_args 1734 && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global, 1735 NULL)) 1736 *non_constant_args = true; 1737 1738 /* For virtual calls, adjust the this argument, so that it is 1739 the object on which the method is called, rather than 1740 one of its bases. */ 1741 if (i == 0 && DECL_VIRTUAL_P (fun)) 1742 { 1743 tree addr = arg; 1744 STRIP_NOPS (addr); 1745 if (TREE_CODE (addr) == ADDR_EXPR) 1746 { 1747 tree obj = TREE_OPERAND (addr, 0); 1748 while (TREE_CODE (obj) == COMPONENT_REF 1749 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) 1750 && !same_type_ignoring_top_level_qualifiers_p 1751 (TREE_TYPE (obj), DECL_CONTEXT (fun))) 1752 obj = TREE_OPERAND (obj, 0); 1753 if (obj != TREE_OPERAND (addr, 0)) 1754 arg = build_fold_addr_expr_with_type (obj, 1755 TREE_TYPE (arg)); 1756 } 1757 } 1758 TREE_VEC_ELT (binds, i) = arg; 1759 } 1760 parms = TREE_CHAIN (parms); 1761 } 1762 1763 return binds; 1764 } 1765 1766 /* Variables and functions to manage constexpr call expansion context. 1767 These do not need to be marked for PCH or GC. */ 1768 1769 /* FIXME remember and print actual constant arguments. */ 1770 static vec<tree> call_stack; 1771 static int call_stack_tick; 1772 static int last_cx_error_tick; 1773 1774 static int 1775 push_cx_call_context (tree call) 1776 { 1777 ++call_stack_tick; 1778 if (!EXPR_HAS_LOCATION (call)) 1779 SET_EXPR_LOCATION (call, input_location); 1780 call_stack.safe_push (call); 1781 int len = call_stack.length (); 1782 if (len > max_constexpr_depth) 1783 return false; 1784 return len; 1785 } 1786 1787 static void 1788 pop_cx_call_context (void) 1789 { 1790 ++call_stack_tick; 1791 call_stack.pop (); 1792 } 1793 1794 vec<tree> 1795 cx_error_context (void) 1796 { 1797 vec<tree> r = vNULL; 1798 if (call_stack_tick != last_cx_error_tick 1799 && !call_stack.is_empty ()) 1800 r = call_stack; 1801 last_cx_error_tick = call_stack_tick; 1802 return r; 1803 } 1804 1805 /* Evaluate a call T to a GCC internal function when possible and return 1806 the evaluated result or, under the control of CTX, give an error, set 1807 NON_CONSTANT_P, and return the unevaluated call T otherwise. */ 1808 1809 static tree 1810 cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, 1811 bool lval, 1812 bool *non_constant_p, bool *overflow_p) 1813 { 1814 enum tree_code opcode = ERROR_MARK; 1815 1816 switch (CALL_EXPR_IFN (t)) 1817 { 1818 case IFN_UBSAN_NULL: 1819 case IFN_UBSAN_BOUNDS: 1820 case IFN_UBSAN_VPTR: 1821 case IFN_FALLTHROUGH: 1822 return void_node; 1823 1824 case IFN_ADD_OVERFLOW: 1825 opcode = PLUS_EXPR; 1826 break; 1827 case IFN_SUB_OVERFLOW: 1828 opcode = MINUS_EXPR; 1829 break; 1830 case IFN_MUL_OVERFLOW: 1831 opcode = MULT_EXPR; 1832 break; 1833 1834 case IFN_LAUNDER: 1835 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), 1836 false, non_constant_p, overflow_p); 1837 1838 case IFN_VEC_CONVERT: 1839 { 1840 tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), 1841 false, non_constant_p, 1842 overflow_p); 1843 if (TREE_CODE (arg) == VECTOR_CST) 1844 if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg)) 1845 return r; 1846 } 1847 /* FALLTHRU */ 1848 1849 default: 1850 if (!ctx->quiet) 1851 error_at (cp_expr_loc_or_input_loc (t), 1852 "call to internal function %qE", t); 1853 *non_constant_p = true; 1854 return t; 1855 } 1856 1857 /* Evaluate constant arguments using OPCODE and return a complex 1858 number containing the result and the overflow bit. */ 1859 tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, 1860 non_constant_p, overflow_p); 1861 tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, 1862 non_constant_p, overflow_p); 1863 1864 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) 1865 { 1866 location_t loc = cp_expr_loc_or_input_loc (t); 1867 tree type = TREE_TYPE (TREE_TYPE (t)); 1868 tree result = fold_binary_loc (loc, opcode, type, 1869 fold_convert_loc (loc, type, arg0), 1870 fold_convert_loc (loc, type, arg1)); 1871 tree ovf 1872 = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); 1873 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ 1874 if (TREE_OVERFLOW (result)) 1875 TREE_OVERFLOW (result) = 0; 1876 1877 return build_complex (TREE_TYPE (t), result, ovf); 1878 } 1879 1880 *non_constant_p = true; 1881 return t; 1882 } 1883 1884 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ 1885 1886 static void 1887 clear_no_implicit_zero (tree ctor) 1888 { 1889 if (CONSTRUCTOR_NO_CLEARING (ctor)) 1890 { 1891 CONSTRUCTOR_NO_CLEARING (ctor) = false; 1892 for (auto &e: CONSTRUCTOR_ELTS (ctor)) 1893 if (TREE_CODE (e.value) == CONSTRUCTOR) 1894 clear_no_implicit_zero (e.value); 1895 } 1896 } 1897 1898 /* Complain about a const object OBJ being modified in a constant expression. 1899 EXPR is the MODIFY_EXPR expression performing the modification. */ 1900 1901 static void 1902 modifying_const_object_error (tree expr, tree obj) 1903 { 1904 location_t loc = cp_expr_loc_or_input_loc (expr); 1905 auto_diagnostic_group d; 1906 error_at (loc, "modifying a const object %qE is not allowed in " 1907 "a constant expression", TREE_OPERAND (expr, 0)); 1908 inform (location_of (obj), "originally declared %<const%> here"); 1909 } 1910 1911 /* Return true if FNDECL is a replaceable global allocation function that 1912 should be useable during constant expression evaluation. */ 1913 1914 static inline bool 1915 cxx_replaceable_global_alloc_fn (tree fndecl) 1916 { 1917 return (cxx_dialect >= cxx20 1918 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl)) 1919 && CP_DECL_CONTEXT (fndecl) == global_namespace 1920 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) 1921 || DECL_IS_OPERATOR_DELETE_P (fndecl))); 1922 } 1923 1924 /* Return true if FNDECL is a placement new function that should be 1925 useable during constant expression evaluation of std::construct_at. */ 1926 1927 static inline bool 1928 cxx_placement_new_fn (tree fndecl) 1929 { 1930 if (cxx_dialect >= cxx20 1931 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl)) 1932 && CP_DECL_CONTEXT (fndecl) == global_namespace 1933 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) 1934 && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE) 1935 { 1936 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); 1937 if (TREE_VALUE (first_arg) == ptr_type_node 1938 && TREE_CHAIN (first_arg) == void_list_node) 1939 return true; 1940 } 1941 return false; 1942 } 1943 1944 /* Return true if FNDECL is std::construct_at. */ 1945 1946 static inline bool 1947 is_std_construct_at (tree fndecl) 1948 { 1949 if (!decl_in_std_namespace_p (fndecl)) 1950 return false; 1951 1952 tree name = DECL_NAME (fndecl); 1953 return name && id_equal (name, "construct_at"); 1954 } 1955 1956 /* Overload for the above taking constexpr_call*. */ 1957 1958 static inline bool 1959 is_std_construct_at (const constexpr_call *call) 1960 { 1961 return (call 1962 && call->fundef 1963 && is_std_construct_at (call->fundef->decl)); 1964 } 1965 1966 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */ 1967 1968 static inline bool 1969 is_std_allocator_allocate (tree fndecl) 1970 { 1971 tree name = DECL_NAME (fndecl); 1972 if (name == NULL_TREE 1973 || !(id_equal (name, "allocate") || id_equal (name, "deallocate"))) 1974 return false; 1975 1976 tree ctx = DECL_CONTEXT (fndecl); 1977 if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) 1978 return false; 1979 1980 tree decl = TYPE_MAIN_DECL (ctx); 1981 name = DECL_NAME (decl); 1982 if (name == NULL_TREE || !id_equal (name, "allocator")) 1983 return false; 1984 1985 return decl_in_std_namespace_p (decl); 1986 } 1987 1988 /* Overload for the above taking constexpr_call*. */ 1989 1990 static inline bool 1991 is_std_allocator_allocate (const constexpr_call *call) 1992 { 1993 return (call 1994 && call->fundef 1995 && is_std_allocator_allocate (call->fundef->decl)); 1996 } 1997 1998 /* Return true if FNDECL is __dynamic_cast. */ 1999 2000 static inline bool 2001 cxx_dynamic_cast_fn_p (tree fndecl) 2002 { 2003 return (cxx_dialect >= cxx20 2004 && id_equal (DECL_NAME (fndecl), "__dynamic_cast") 2005 && CP_DECL_CONTEXT (fndecl) == global_namespace); 2006 } 2007 2008 /* Often, we have an expression in the form of address + offset, e.g. 2009 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */ 2010 2011 static tree 2012 extract_obj_from_addr_offset (tree expr) 2013 { 2014 if (TREE_CODE (expr) == POINTER_PLUS_EXPR) 2015 expr = TREE_OPERAND (expr, 0); 2016 STRIP_NOPS (expr); 2017 if (TREE_CODE (expr) == ADDR_EXPR) 2018 expr = TREE_OPERAND (expr, 0); 2019 return expr; 2020 } 2021 2022 /* Given a PATH like 2023 2024 g.D.2181.D.2154.D.2102.D.2093 2025 2026 find a component with type TYPE. Return NULL_TREE if not found, and 2027 error_mark_node if the component is not accessible. If STOP is non-null, 2028 this function will return NULL_TREE if STOP is found before TYPE. */ 2029 2030 static tree 2031 get_component_with_type (tree path, tree type, tree stop) 2032 { 2033 while (true) 2034 { 2035 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type)) 2036 /* Found it. */ 2037 return path; 2038 else if (stop 2039 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), 2040 stop))) 2041 return NULL_TREE; 2042 else if (TREE_CODE (path) == COMPONENT_REF 2043 && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1))) 2044 { 2045 /* We need to check that the component we're accessing is in fact 2046 accessible. */ 2047 if (TREE_PRIVATE (TREE_OPERAND (path, 1)) 2048 || TREE_PROTECTED (TREE_OPERAND (path, 1))) 2049 return error_mark_node; 2050 path = TREE_OPERAND (path, 0); 2051 } 2052 else 2053 return NULL_TREE; 2054 } 2055 } 2056 2057 /* Evaluate a call to __dynamic_cast (permitted by P1327R1). 2058 2059 The declaration of __dynamic_cast is: 2060 2061 void* __dynamic_cast (const void* __src_ptr, 2062 const __class_type_info* __src_type, 2063 const __class_type_info* __dst_type, 2064 ptrdiff_t __src2dst); 2065 2066 where src2dst has the following possible values 2067 2068 >-1: src_type is a unique public non-virtual base of dst_type 2069 dst_ptr + src2dst == src_ptr 2070 -1: unspecified relationship 2071 -2: src_type is not a public base of dst_type 2072 -3: src_type is a multiple public non-virtual base of dst_type 2073 2074 Since literal types can't have virtual bases, we only expect hint >=0, 2075 -2, or -3. */ 2076 2077 static tree 2078 cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call, 2079 bool *non_constant_p, bool *overflow_p) 2080 { 2081 /* T will be something like 2082 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8) 2083 dismantle it. */ 2084 gcc_assert (call_expr_nargs (call) == 4); 2085 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; 2086 tree obj = CALL_EXPR_ARG (call, 0); 2087 tree type = CALL_EXPR_ARG (call, 2); 2088 HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3)); 2089 location_t loc = cp_expr_loc_or_input_loc (call); 2090 2091 /* Get the target type of the dynamic_cast. */ 2092 gcc_assert (TREE_CODE (type) == ADDR_EXPR); 2093 type = TREE_OPERAND (type, 0); 2094 type = TREE_TYPE (DECL_NAME (type)); 2095 2096 /* TYPE can only be either T* or T&. We can't know which of these it 2097 is by looking at TYPE, but OBJ will be "(T*) x" in the first case, 2098 and something like "(T*)(T&)(T*) x" in the second case. */ 2099 bool reference_p = false; 2100 while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR) 2101 { 2102 reference_p |= TYPE_REF_P (TREE_TYPE (obj)); 2103 obj = TREE_OPERAND (obj, 0); 2104 } 2105 2106 /* Evaluate the object so that we know its dynamic type. */ 2107 obj = cxx_eval_constant_expression (ctx, obj, /*lval*/false, non_constant_p, 2108 overflow_p); 2109 if (*non_constant_p) 2110 return call; 2111 2112 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0, 2113 but when HINT is > 0, it can also be something like 2114 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */ 2115 obj = extract_obj_from_addr_offset (obj); 2116 const tree objtype = TREE_TYPE (obj); 2117 /* If OBJ doesn't refer to a base field, we're done. */ 2118 if (tree t = (TREE_CODE (obj) == COMPONENT_REF 2119 ? TREE_OPERAND (obj, 1) : obj)) 2120 if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t)) 2121 { 2122 if (reference_p) 2123 { 2124 if (!ctx->quiet) 2125 { 2126 error_at (loc, "reference %<dynamic_cast%> failed"); 2127 inform (loc, "dynamic type %qT of its operand does " 2128 "not have a base class of type %qT", 2129 objtype, type); 2130 } 2131 *non_constant_p = true; 2132 } 2133 return integer_zero_node; 2134 } 2135 2136 /* [class.cdtor] When a dynamic_cast is used in a constructor ... 2137 or in a destructor ... if the operand of the dynamic_cast refers 2138 to the object under construction or destruction, this object is 2139 considered to be a most derived object that has the type of the 2140 constructor or destructor's class. */ 2141 tree vtable = build_vfield_ref (obj, objtype); 2142 vtable = cxx_eval_constant_expression (ctx, vtable, /*lval*/false, 2143 non_constant_p, overflow_p); 2144 if (*non_constant_p) 2145 return call; 2146 /* With -fsanitize=vptr, we initialize all vtable pointers to null, 2147 so it's possible that we got a null pointer now. */ 2148 if (integer_zerop (vtable)) 2149 { 2150 if (!ctx->quiet) 2151 error_at (loc, "virtual table pointer is used uninitialized"); 2152 *non_constant_p = true; 2153 return integer_zero_node; 2154 } 2155 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */ 2156 vtable = extract_obj_from_addr_offset (vtable); 2157 const tree mdtype = DECL_CONTEXT (vtable); 2158 2159 /* Given dynamic_cast<T>(v), 2160 2161 [expr.dynamic.cast] If C is the class type to which T points or refers, 2162 the runtime check logically executes as follows: 2163 2164 If, in the most derived object pointed (referred) to by v, v points 2165 (refers) to a public base class subobject of a C object, and if only 2166 one object of type C is derived from the subobject pointed (referred) 2167 to by v the result points (refers) to that C object. 2168 2169 In this case, HINT >= 0 or -3. */ 2170 if (hint >= 0 || hint == -3) 2171 { 2172 /* Look for a component with type TYPE. */ 2173 tree t = get_component_with_type (obj, type, mdtype); 2174 /* If not accessible, give an error. */ 2175 if (t == error_mark_node) 2176 { 2177 if (reference_p) 2178 { 2179 if (!ctx->quiet) 2180 { 2181 error_at (loc, "reference %<dynamic_cast%> failed"); 2182 inform (loc, "static type %qT of its operand is a " 2183 "non-public base class of dynamic type %qT", 2184 objtype, type); 2185 2186 } 2187 *non_constant_p = true; 2188 } 2189 return integer_zero_node; 2190 } 2191 else if (t) 2192 /* The result points to the TYPE object. */ 2193 return cp_build_addr_expr (t, complain); 2194 /* Else, TYPE was not found, because the HINT turned out to be wrong. 2195 Fall through to the normal processing. */ 2196 } 2197 2198 /* Otherwise, if v points (refers) to a public base class subobject of the 2199 most derived object, and the type of the most derived object has a base 2200 class, of type C, that is unambiguous and public, the result points 2201 (refers) to the C subobject of the most derived object. 2202 2203 But it can also be an invalid case. */ 2204 2205 /* Get the most derived object. */ 2206 obj = get_component_with_type (obj, mdtype, NULL_TREE); 2207 if (obj == error_mark_node) 2208 { 2209 if (reference_p) 2210 { 2211 if (!ctx->quiet) 2212 { 2213 error_at (loc, "reference %<dynamic_cast%> failed"); 2214 inform (loc, "static type %qT of its operand is a non-public" 2215 " base class of dynamic type %qT", objtype, mdtype); 2216 } 2217 *non_constant_p = true; 2218 } 2219 return integer_zero_node; 2220 } 2221 else 2222 gcc_assert (obj); 2223 2224 /* Check that the type of the most derived object has a base class 2225 of type TYPE that is unambiguous and public. */ 2226 base_kind b_kind; 2227 tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none); 2228 if (!binfo || binfo == error_mark_node) 2229 { 2230 if (reference_p) 2231 { 2232 if (!ctx->quiet) 2233 { 2234 error_at (loc, "reference %<dynamic_cast%> failed"); 2235 if (b_kind == bk_ambig) 2236 inform (loc, "%qT is an ambiguous base class of dynamic " 2237 "type %qT of its operand", type, mdtype); 2238 else 2239 inform (loc, "dynamic type %qT of its operand does not " 2240 "have an unambiguous public base class %qT", 2241 mdtype, type); 2242 } 2243 *non_constant_p = true; 2244 } 2245 return integer_zero_node; 2246 } 2247 /* If so, return the TYPE subobject of the most derived object. */ 2248 obj = convert_to_base_statically (obj, binfo); 2249 return cp_build_addr_expr (obj, complain); 2250 } 2251 2252 /* Data structure used by replace_decl and replace_decl_r. */ 2253 2254 struct replace_decl_data 2255 { 2256 /* The _DECL we want to replace. */ 2257 tree decl; 2258 /* The replacement for DECL. */ 2259 tree replacement; 2260 /* Trees we've visited. */ 2261 hash_set<tree> *pset; 2262 /* Whether we've performed any replacements. */ 2263 bool changed; 2264 }; 2265 2266 /* Helper function for replace_decl, called through cp_walk_tree. */ 2267 2268 static tree 2269 replace_decl_r (tree *tp, int *walk_subtrees, void *data) 2270 { 2271 replace_decl_data *d = (replace_decl_data *) data; 2272 2273 if (*tp == d->decl) 2274 { 2275 *tp = unshare_expr (d->replacement); 2276 d->changed = true; 2277 *walk_subtrees = 0; 2278 } 2279 else if (TYPE_P (*tp) 2280 || d->pset->add (*tp)) 2281 *walk_subtrees = 0; 2282 2283 return NULL_TREE; 2284 } 2285 2286 /* Replace every occurrence of DECL with (an unshared copy of) 2287 REPLACEMENT within the expression *TP. Returns true iff a 2288 replacement was performed. */ 2289 2290 bool 2291 replace_decl (tree *tp, tree decl, tree replacement) 2292 { 2293 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p 2294 (TREE_TYPE (decl), TREE_TYPE (replacement))); 2295 hash_set<tree> pset; 2296 replace_decl_data data = { decl, replacement, &pset, false }; 2297 cp_walk_tree (tp, replace_decl_r, &data, NULL); 2298 return data.changed; 2299 } 2300 2301 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */ 2302 2303 static tree 2304 cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl, 2305 bool lval, 2306 bool *non_constant_p, bool *overflow_p) 2307 { 2308 tree function = THUNK_TARGET (thunk_fndecl); 2309 2310 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) 2311 { 2312 if (!ctx->quiet) 2313 { 2314 if (!DECL_DECLARED_CONSTEXPR_P (function)) 2315 { 2316 error ("call to non-%<constexpr%> function %qD", function); 2317 explain_invalid_constexpr_fn (function); 2318 } 2319 else 2320 /* virtual_offset is only set for virtual bases, which make the 2321 class non-literal, so we don't need to handle it here. */ 2322 error ("calling constexpr member function %qD through virtual " 2323 "base subobject", function); 2324 } 2325 *non_constant_p = true; 2326 return t; 2327 } 2328 2329 tree new_call = copy_node (t); 2330 CALL_EXPR_FN (new_call) = function; 2331 TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function)); 2332 2333 tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl)); 2334 2335 if (DECL_THIS_THUNK_P (thunk_fndecl)) 2336 { 2337 /* 'this'-adjusting thunk. */ 2338 tree this_arg = CALL_EXPR_ARG (t, 0); 2339 this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg), 2340 this_arg, offset); 2341 CALL_EXPR_ARG (new_call, 0) = this_arg; 2342 } 2343 else 2344 /* Return-adjusting thunk. */ 2345 new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call), 2346 new_call, offset); 2347 2348 return cxx_eval_constant_expression (ctx, new_call, lval, 2349 non_constant_p, overflow_p); 2350 } 2351 2352 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set 2353 its TREE_READONLY flag according to READONLY_P. Used for constexpr 2354 'tors to detect modifying const objects in a constexpr context. */ 2355 2356 static void 2357 cxx_set_object_constness (const constexpr_ctx *ctx, tree object, 2358 bool readonly_p, bool *non_constant_p, 2359 bool *overflow_p) 2360 { 2361 if (CLASS_TYPE_P (TREE_TYPE (object)) 2362 && CP_TYPE_CONST_P (TREE_TYPE (object))) 2363 { 2364 /* Subobjects might not be stored in ctx->global->values but we 2365 can get its CONSTRUCTOR by evaluating *this. */ 2366 tree e = cxx_eval_constant_expression (ctx, object, /*lval*/false, 2367 non_constant_p, overflow_p); 2368 if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) 2369 TREE_READONLY (e) = readonly_p; 2370 } 2371 } 2372 2373 /* Subroutine of cxx_eval_constant_expression. 2374 Evaluate the call expression tree T in the context of OLD_CALL expression 2375 evaluation. */ 2376 2377 static tree 2378 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, 2379 bool lval, 2380 bool *non_constant_p, bool *overflow_p) 2381 { 2382 /* Handle concept checks separately. */ 2383 if (concept_check_p (t)) 2384 return evaluate_concept_check (t); 2385 2386 location_t loc = cp_expr_loc_or_input_loc (t); 2387 tree fun = get_function_named_in_call (t); 2388 constexpr_call new_call 2389 = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; 2390 int depth_ok; 2391 2392 if (fun == NULL_TREE) 2393 return cxx_eval_internal_function (ctx, t, lval, 2394 non_constant_p, overflow_p); 2395 2396 if (TREE_CODE (fun) != FUNCTION_DECL) 2397 { 2398 /* Might be a constexpr function pointer. */ 2399 fun = cxx_eval_constant_expression (ctx, fun, 2400 /*lval*/false, non_constant_p, 2401 overflow_p); 2402 STRIP_NOPS (fun); 2403 if (TREE_CODE (fun) == ADDR_EXPR) 2404 fun = TREE_OPERAND (fun, 0); 2405 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no 2406 indirection, the called expression is a pointer into the 2407 virtual table which should contain FDESC_EXPR. Extract the 2408 FUNCTION_DECL from there. */ 2409 else if (TARGET_VTABLE_USES_DESCRIPTORS 2410 && TREE_CODE (fun) == POINTER_PLUS_EXPR 2411 && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR 2412 && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) 2413 { 2414 tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); 2415 if (VAR_P (d) 2416 && DECL_VTABLE_OR_VTT_P (d) 2417 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE 2418 && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type 2419 && DECL_INITIAL (d) 2420 && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) 2421 { 2422 tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), 2423 TYPE_SIZE_UNIT (vtable_entry_type)); 2424 HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); 2425 if (idx >= 0) 2426 { 2427 tree fdesc 2428 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; 2429 if (TREE_CODE (fdesc) == FDESC_EXPR 2430 && integer_zerop (TREE_OPERAND (fdesc, 1))) 2431 fun = TREE_OPERAND (fdesc, 0); 2432 } 2433 } 2434 } 2435 } 2436 if (TREE_CODE (fun) != FUNCTION_DECL) 2437 { 2438 if (!ctx->quiet && !*non_constant_p) 2439 error_at (loc, "expression %qE does not designate a %<constexpr%> " 2440 "function", fun); 2441 *non_constant_p = true; 2442 return t; 2443 } 2444 if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) 2445 fun = DECL_CLONED_FUNCTION (fun); 2446 2447 if (is_ubsan_builtin_p (fun)) 2448 return void_node; 2449 2450 if (fndecl_built_in_p (fun)) 2451 return cxx_eval_builtin_function_call (ctx, t, fun, 2452 lval, non_constant_p, overflow_p); 2453 if (DECL_THUNK_P (fun)) 2454 return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p); 2455 if (!maybe_constexpr_fn (fun)) 2456 { 2457 if (TREE_CODE (t) == CALL_EXPR 2458 && cxx_replaceable_global_alloc_fn (fun) 2459 && (CALL_FROM_NEW_OR_DELETE_P (t) 2460 || is_std_allocator_allocate (ctx->call))) 2461 { 2462 const int nargs = call_expr_nargs (t); 2463 tree arg0 = NULL_TREE; 2464 for (int i = 0; i < nargs; ++i) 2465 { 2466 tree arg = CALL_EXPR_ARG (t, i); 2467 arg = cxx_eval_constant_expression (ctx, arg, false, 2468 non_constant_p, overflow_p); 2469 VERIFY_CONSTANT (arg); 2470 if (i == 0) 2471 arg0 = arg; 2472 } 2473 gcc_assert (arg0); 2474 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) 2475 { 2476 tree type = build_array_type_nelts (char_type_node, 2477 tree_to_uhwi (arg0)); 2478 tree var = build_decl (loc, VAR_DECL, 2479 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) 2480 & OVL_OP_FLAG_VEC) 2481 ? heap_vec_uninit_identifier 2482 : heap_uninit_identifier, 2483 type); 2484 DECL_ARTIFICIAL (var) = 1; 2485 TREE_STATIC (var) = 1; 2486 // Temporarily register the artificial var in varpool, 2487 // so that comparisons of its address against NULL are folded 2488 // through nonzero_address even with 2489 // -fno-delete-null-pointer-checks or that comparison of 2490 // addresses of different heap artificial vars is folded too. 2491 // See PR98988 and PR99031. 2492 varpool_node::finalize_decl (var); 2493 ctx->global->heap_vars.safe_push (var); 2494 ctx->global->values.put (var, NULL_TREE); 2495 return fold_convert (ptr_type_node, build_address (var)); 2496 } 2497 else 2498 { 2499 STRIP_NOPS (arg0); 2500 if (TREE_CODE (arg0) == ADDR_EXPR 2501 && VAR_P (TREE_OPERAND (arg0, 0))) 2502 { 2503 tree var = TREE_OPERAND (arg0, 0); 2504 if (DECL_NAME (var) == heap_uninit_identifier 2505 || DECL_NAME (var) == heap_identifier) 2506 { 2507 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) 2508 & OVL_OP_FLAG_VEC) 2509 { 2510 if (!ctx->quiet) 2511 { 2512 error_at (loc, "array deallocation of object " 2513 "allocated with non-array " 2514 "allocation"); 2515 inform (DECL_SOURCE_LOCATION (var), 2516 "allocation performed here"); 2517 } 2518 *non_constant_p = true; 2519 return t; 2520 } 2521 DECL_NAME (var) = heap_deleted_identifier; 2522 ctx->global->values.remove (var); 2523 ctx->global->heap_dealloc_count++; 2524 return void_node; 2525 } 2526 else if (DECL_NAME (var) == heap_vec_uninit_identifier 2527 || DECL_NAME (var) == heap_vec_identifier) 2528 { 2529 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) 2530 & OVL_OP_FLAG_VEC) == 0) 2531 { 2532 if (!ctx->quiet) 2533 { 2534 error_at (loc, "non-array deallocation of " 2535 "object allocated with array " 2536 "allocation"); 2537 inform (DECL_SOURCE_LOCATION (var), 2538 "allocation performed here"); 2539 } 2540 *non_constant_p = true; 2541 return t; 2542 } 2543 DECL_NAME (var) = heap_deleted_identifier; 2544 ctx->global->values.remove (var); 2545 ctx->global->heap_dealloc_count++; 2546 return void_node; 2547 } 2548 else if (DECL_NAME (var) == heap_deleted_identifier) 2549 { 2550 if (!ctx->quiet) 2551 error_at (loc, "deallocation of already deallocated " 2552 "storage"); 2553 *non_constant_p = true; 2554 return t; 2555 } 2556 } 2557 if (!ctx->quiet) 2558 error_at (loc, "deallocation of storage that was " 2559 "not previously allocated"); 2560 *non_constant_p = true; 2561 return t; 2562 } 2563 } 2564 /* Allow placement new in std::construct_at, just return the second 2565 argument. */ 2566 if (TREE_CODE (t) == CALL_EXPR 2567 && cxx_placement_new_fn (fun) 2568 && is_std_construct_at (ctx->call)) 2569 { 2570 const int nargs = call_expr_nargs (t); 2571 tree arg1 = NULL_TREE; 2572 for (int i = 0; i < nargs; ++i) 2573 { 2574 tree arg = CALL_EXPR_ARG (t, i); 2575 arg = cxx_eval_constant_expression (ctx, arg, false, 2576 non_constant_p, overflow_p); 2577 if (i == 1) 2578 arg1 = arg; 2579 else 2580 VERIFY_CONSTANT (arg); 2581 } 2582 gcc_assert (arg1); 2583 return arg1; 2584 } 2585 else if (cxx_dynamic_cast_fn_p (fun)) 2586 return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p); 2587 2588 if (!ctx->quiet) 2589 { 2590 if (!lambda_static_thunk_p (fun)) 2591 error_at (loc, "call to non-%<constexpr%> function %qD", fun); 2592 explain_invalid_constexpr_fn (fun); 2593 } 2594 *non_constant_p = true; 2595 return t; 2596 } 2597 2598 constexpr_ctx new_ctx = *ctx; 2599 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object 2600 && TREE_CODE (t) == AGGR_INIT_EXPR) 2601 { 2602 /* We want to have an initialization target for an AGGR_INIT_EXPR. 2603 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ 2604 new_ctx.object = AGGR_INIT_EXPR_SLOT (t); 2605 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); 2606 CONSTRUCTOR_NO_CLEARING (ctor) = true; 2607 ctx->global->values.put (new_ctx.object, ctor); 2608 ctx = &new_ctx; 2609 } 2610 2611 /* Shortcut trivial constructor/op=. */ 2612 if (trivial_fn_p (fun)) 2613 { 2614 tree init = NULL_TREE; 2615 if (call_expr_nargs (t) == 2) 2616 init = convert_from_reference (get_nth_callarg (t, 1)); 2617 else if (TREE_CODE (t) == AGGR_INIT_EXPR 2618 && AGGR_INIT_ZERO_FIRST (t)) 2619 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); 2620 if (init) 2621 { 2622 tree op = get_nth_callarg (t, 0); 2623 if (is_dummy_object (op)) 2624 op = ctx->object; 2625 else 2626 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); 2627 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); 2628 new_ctx.call = &new_call; 2629 return cxx_eval_constant_expression (&new_ctx, set, lval, 2630 non_constant_p, overflow_p); 2631 } 2632 } 2633 2634 bool non_constant_args = false; 2635 new_call.bindings 2636 = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p, 2637 overflow_p, &non_constant_args); 2638 2639 /* We build up the bindings list before we know whether we already have this 2640 call cached. If we don't end up saving these bindings, ggc_free them when 2641 this function exits. */ 2642 class free_bindings 2643 { 2644 tree *bindings; 2645 public: 2646 free_bindings (tree &b): bindings (&b) { } 2647 ~free_bindings () { if (bindings) ggc_free (*bindings); } 2648 void preserve () { bindings = NULL; } 2649 } fb (new_call.bindings); 2650 2651 if (*non_constant_p) 2652 return t; 2653 2654 /* We can't defer instantiating the function any longer. */ 2655 if (!DECL_INITIAL (fun) 2656 && DECL_TEMPLOID_INSTANTIATION (fun) 2657 && !uid_sensitive_constexpr_evaluation_p ()) 2658 { 2659 location_t save_loc = input_location; 2660 input_location = loc; 2661 ++function_depth; 2662 if (ctx->manifestly_const_eval) 2663 FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; 2664 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); 2665 --function_depth; 2666 input_location = save_loc; 2667 } 2668 2669 /* If in direct recursive call, optimize definition search. */ 2670 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) 2671 new_call.fundef = ctx->call->fundef; 2672 else 2673 { 2674 new_call.fundef = retrieve_constexpr_fundef (fun); 2675 if (new_call.fundef == NULL || new_call.fundef->body == NULL 2676 || new_call.fundef->result == error_mark_node 2677 || fun == current_function_decl) 2678 { 2679 if (!ctx->quiet) 2680 { 2681 /* We need to check for current_function_decl here in case we're 2682 being called during cp_fold_function, because at that point 2683 DECL_INITIAL is set properly and we have a fundef but we 2684 haven't lowered invisirefs yet (c++/70344). */ 2685 if (DECL_INITIAL (fun) == error_mark_node 2686 || fun == current_function_decl) 2687 error_at (loc, "%qD called in a constant expression before its " 2688 "definition is complete", fun); 2689 else if (DECL_INITIAL (fun)) 2690 { 2691 /* The definition of fun was somehow unsuitable. But pretend 2692 that lambda static thunks don't exist. */ 2693 if (!lambda_static_thunk_p (fun)) 2694 error_at (loc, "%qD called in a constant expression", fun); 2695 explain_invalid_constexpr_fn (fun); 2696 } 2697 else 2698 error_at (loc, "%qD used before its definition", fun); 2699 } 2700 *non_constant_p = true; 2701 return t; 2702 } 2703 } 2704 2705 depth_ok = push_cx_call_context (t); 2706 2707 /* Remember the object we are constructing or destructing. */ 2708 tree new_obj = NULL_TREE; 2709 if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)) 2710 { 2711 /* In a cdtor, it should be the first `this' argument. 2712 At this point it has already been evaluated in the call 2713 to cxx_bind_parameters_in_call. */ 2714 new_obj = TREE_VEC_ELT (new_call.bindings, 0); 2715 STRIP_NOPS (new_obj); 2716 if (TREE_CODE (new_obj) == ADDR_EXPR) 2717 new_obj = TREE_OPERAND (new_obj, 0); 2718 2719 if (ctx->call && ctx->call->fundef 2720 && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl)) 2721 { 2722 tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0); 2723 STRIP_NOPS (cur_obj); 2724 if (TREE_CODE (cur_obj) == ADDR_EXPR) 2725 cur_obj = TREE_OPERAND (cur_obj, 0); 2726 if (new_obj == cur_obj) 2727 /* We're calling the target constructor of a delegating 2728 constructor, or accessing a base subobject through a 2729 NOP_EXPR as part of a call to a base constructor, so 2730 there is no new (sub)object. */ 2731 new_obj = NULL_TREE; 2732 } 2733 } 2734 2735 tree result = NULL_TREE; 2736 2737 constexpr_call *entry = NULL; 2738 if (depth_ok && !non_constant_args && ctx->strict) 2739 { 2740 new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); 2741 new_call.hash 2742 = iterative_hash_template_arg (new_call.bindings, new_call.hash); 2743 new_call.hash 2744 = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); 2745 2746 /* If we have seen this call before, we are done. */ 2747 maybe_initialize_constexpr_call_table (); 2748 constexpr_call **slot 2749 = constexpr_call_table->find_slot (&new_call, INSERT); 2750 entry = *slot; 2751 if (entry == NULL) 2752 { 2753 /* Only cache up to constexpr_cache_depth to limit memory use. */ 2754 if (depth_ok < constexpr_cache_depth) 2755 { 2756 /* We need to keep a pointer to the entry, not just the slot, as 2757 the slot can move during evaluation of the body. */ 2758 *slot = entry = ggc_alloc<constexpr_call> (); 2759 *entry = new_call; 2760 fb.preserve (); 2761 } 2762 } 2763 /* Calls that are in progress have their result set to NULL, so that we 2764 can detect circular dependencies. Now that we only cache up to 2765 constexpr_cache_depth this won't catch circular dependencies that 2766 start deeper, but they'll hit the recursion or ops limit. */ 2767 else if (entry->result == NULL) 2768 { 2769 if (!ctx->quiet) 2770 error ("call has circular dependency"); 2771 *non_constant_p = true; 2772 entry->result = result = error_mark_node; 2773 } 2774 else 2775 result = entry->result; 2776 } 2777 2778 if (!depth_ok) 2779 { 2780 if (!ctx->quiet) 2781 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " 2782 "%<-fconstexpr-depth=%> to increase the maximum)", 2783 max_constexpr_depth); 2784 *non_constant_p = true; 2785 result = error_mark_node; 2786 } 2787 else 2788 { 2789 bool cacheable = true; 2790 if (result && result != error_mark_node) 2791 /* OK */; 2792 else if (!DECL_SAVED_TREE (fun)) 2793 { 2794 /* When at_eof >= 2, cgraph has started throwing away 2795 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of 2796 late code generation for VEC_INIT_EXPR, which needs to be 2797 completely reconsidered. */ 2798 gcc_assert (at_eof >= 2 && ctx->quiet); 2799 *non_constant_p = true; 2800 } 2801 else if (tree copy = get_fundef_copy (new_call.fundef)) 2802 { 2803 tree body, parms, res; 2804 releasing_vec ctors; 2805 2806 /* Reuse or create a new unshared copy of this function's body. */ 2807 body = TREE_PURPOSE (copy); 2808 parms = TREE_VALUE (copy); 2809 res = TREE_TYPE (copy); 2810 2811 /* Associate the bindings with the remapped parms. */ 2812 tree bound = new_call.bindings; 2813 tree remapped = parms; 2814 for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) 2815 { 2816 tree arg = TREE_VEC_ELT (bound, i); 2817 if (entry) 2818 { 2819 /* Unshare args going into the hash table to separate them 2820 from the caller's context, for better GC and to avoid 2821 problems with verify_gimple. */ 2822 arg = unshare_expr_without_location (arg); 2823 TREE_VEC_ELT (bound, i) = arg; 2824 2825 /* And then unshare again so the callee doesn't change the 2826 argument values in the hash table. XXX Could we unshare 2827 lazily in cxx_eval_store_expression? */ 2828 arg = unshare_constructor (arg); 2829 if (TREE_CODE (arg) == CONSTRUCTOR) 2830 vec_safe_push (ctors, arg); 2831 } 2832 ctx->global->values.put (remapped, arg); 2833 remapped = DECL_CHAIN (remapped); 2834 } 2835 /* Add the RESULT_DECL to the values map, too. */ 2836 gcc_assert (!DECL_BY_REFERENCE (res)); 2837 ctx->global->values.put (res, NULL_TREE); 2838 2839 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that 2840 we can forget their values after the call. */ 2841 constexpr_ctx ctx_with_save_exprs = *ctx; 2842 auto_vec<tree, 10> save_exprs; 2843 ctx_with_save_exprs.save_exprs = &save_exprs; 2844 ctx_with_save_exprs.call = &new_call; 2845 unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); 2846 unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; 2847 2848 /* If this is a constexpr destructor, the object's const and volatile 2849 semantics are no longer in effect; see [class.dtor]p5. */ 2850 if (new_obj && DECL_DESTRUCTOR_P (fun)) 2851 cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false, 2852 non_constant_p, overflow_p); 2853 2854 tree jump_target = NULL_TREE; 2855 cxx_eval_constant_expression (&ctx_with_save_exprs, body, 2856 lval, non_constant_p, overflow_p, 2857 &jump_target); 2858 2859 if (DECL_CONSTRUCTOR_P (fun)) 2860 { 2861 /* This can be null for a subobject constructor call, in 2862 which case what we care about is the initialization 2863 side-effects rather than the value. We could get at the 2864 value by evaluating *this, but we don't bother; there's 2865 no need to put such a call in the hash table. */ 2866 result = lval ? ctx->object : ctx->ctor; 2867 2868 /* If we've just evaluated a subobject constructor call for an 2869 empty union member, it might not have produced a side effect 2870 that actually activated the union member. So produce such a 2871 side effect now to ensure the union appears initialized. */ 2872 if (!result && new_obj 2873 && TREE_CODE (new_obj) == COMPONENT_REF 2874 && TREE_CODE (TREE_TYPE 2875 (TREE_OPERAND (new_obj, 0))) == UNION_TYPE 2876 && is_really_empty_class (TREE_TYPE (new_obj), 2877 /*ignore_vptr*/false)) 2878 { 2879 tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj), 2880 new_obj, 2881 build_constructor (TREE_TYPE (new_obj), 2882 NULL)); 2883 cxx_eval_constant_expression (ctx, activate, lval, 2884 non_constant_p, overflow_p); 2885 ggc_free (activate); 2886 } 2887 } 2888 else if (VOID_TYPE_P (TREE_TYPE (res))) 2889 result = void_node; 2890 else 2891 { 2892 result = *ctx->global->values.get (res); 2893 if (result == NULL_TREE && !*non_constant_p 2894 && !DECL_DESTRUCTOR_P (fun)) 2895 { 2896 if (!ctx->quiet) 2897 error ("%<constexpr%> call flows off the end " 2898 "of the function"); 2899 *non_constant_p = true; 2900 } 2901 } 2902 2903 /* At this point, the object's constructor will have run, so 2904 the object is no longer under construction, and its possible 2905 'const' semantics now apply. Make a note of this fact by 2906 marking the CONSTRUCTOR TREE_READONLY. */ 2907 if (new_obj && DECL_CONSTRUCTOR_P (fun)) 2908 cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true, 2909 non_constant_p, overflow_p); 2910 2911 /* Forget the saved values of the callee's SAVE_EXPRs and 2912 TARGET_EXPRs. */ 2913 for (tree save_expr : save_exprs) 2914 ctx->global->values.remove (save_expr); 2915 2916 /* Remove the parms/result from the values map. Is it worth 2917 bothering to do this when the map itself is only live for 2918 one constexpr evaluation? If so, maybe also clear out 2919 other vars from call, maybe in BIND_EXPR handling? */ 2920 ctx->global->values.remove (res); 2921 for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) 2922 ctx->global->values.remove (parm); 2923 2924 /* Free any parameter CONSTRUCTORs we aren't returning directly. */ 2925 while (!ctors->is_empty ()) 2926 { 2927 tree c = ctors->pop (); 2928 if (c != result) 2929 free_constructor (c); 2930 } 2931 2932 /* Make the unshared function copy we used available for re-use. */ 2933 save_fundef_copy (fun, copy); 2934 2935 /* If the call allocated some heap object that hasn't been 2936 deallocated during the call, or if it deallocated some heap 2937 object it has not allocated, the call isn't really stateless 2938 for the constexpr evaluation and should not be cached. 2939 It is fine if the call allocates something and deallocates it 2940 too. */ 2941 if (entry 2942 && (save_heap_alloc_count != ctx->global->heap_vars.length () 2943 || (save_heap_dealloc_count 2944 != ctx->global->heap_dealloc_count))) 2945 { 2946 tree heap_var; 2947 unsigned int i; 2948 if ((ctx->global->heap_vars.length () 2949 - ctx->global->heap_dealloc_count) 2950 != save_heap_alloc_count - save_heap_dealloc_count) 2951 cacheable = false; 2952 else 2953 FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var, 2954 save_heap_alloc_count) 2955 if (DECL_NAME (heap_var) != heap_deleted_identifier) 2956 { 2957 cacheable = false; 2958 break; 2959 } 2960 } 2961 2962 /* Rewrite all occurrences of the function's RESULT_DECL with the 2963 current object under construction. */ 2964 if (!*non_constant_p && ctx->object 2965 && CLASS_TYPE_P (TREE_TYPE (res)) 2966 && !is_empty_class (TREE_TYPE (res))) 2967 if (replace_decl (&result, res, ctx->object)) 2968 cacheable = false; 2969 } 2970 else 2971 /* Couldn't get a function copy to evaluate. */ 2972 *non_constant_p = true; 2973 2974 if (result == error_mark_node) 2975 *non_constant_p = true; 2976 if (*non_constant_p || *overflow_p) 2977 result = error_mark_node; 2978 else if (!result) 2979 result = void_node; 2980 if (entry) 2981 entry->result = cacheable ? result : error_mark_node; 2982 } 2983 2984 /* The result of a constexpr function must be completely initialized. 2985 2986 However, in C++20, a constexpr constructor doesn't necessarily have 2987 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING 2988 in order to detect reading an unitialized object in constexpr instead 2989 of value-initializing it. (reduced_constant_expression_p is expected to 2990 take care of clearing the flag.) */ 2991 if (TREE_CODE (result) == CONSTRUCTOR 2992 && (cxx_dialect < cxx20 2993 || !DECL_CONSTRUCTOR_P (fun))) 2994 clear_no_implicit_zero (result); 2995 2996 pop_cx_call_context (); 2997 return result; 2998 } 2999 3000 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR 3001 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be 3002 cleared. 3003 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ 3004 3005 bool 3006 reduced_constant_expression_p (tree t) 3007 { 3008 if (t == NULL_TREE) 3009 return false; 3010 3011 switch (TREE_CODE (t)) 3012 { 3013 case PTRMEM_CST: 3014 /* Even if we can't lower this yet, it's constant. */ 3015 return true; 3016 3017 case CONSTRUCTOR: 3018 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ 3019 tree field; 3020 if (CONSTRUCTOR_NO_CLEARING (t)) 3021 { 3022 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) 3023 /* An initialized vector would have a VECTOR_CST. */ 3024 return false; 3025 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 3026 { 3027 /* There must be a valid constant initializer at every array 3028 index. */ 3029 tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); 3030 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); 3031 tree cursor = min; 3032 for (auto &e: CONSTRUCTOR_ELTS (t)) 3033 { 3034 if (!reduced_constant_expression_p (e.value)) 3035 return false; 3036 if (array_index_cmp (cursor, e.index) != 0) 3037 return false; 3038 if (TREE_CODE (e.index) == RANGE_EXPR) 3039 cursor = TREE_OPERAND (e.index, 1); 3040 cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); 3041 } 3042 if (find_array_ctor_elt (t, max) == -1) 3043 return false; 3044 goto ok; 3045 } 3046 else if (cxx_dialect >= cxx20 3047 && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) 3048 { 3049 if (CONSTRUCTOR_NELTS (t) == 0) 3050 /* An initialized union has a constructor element. */ 3051 return false; 3052 /* And it only initializes one member. */ 3053 field = NULL_TREE; 3054 } 3055 else 3056 field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t))); 3057 } 3058 else 3059 field = NULL_TREE; 3060 for (auto &e: CONSTRUCTOR_ELTS (t)) 3061 { 3062 /* If VAL is null, we're in the middle of initializing this 3063 element. */ 3064 if (!reduced_constant_expression_p (e.value)) 3065 return false; 3066 /* We want to remove initializers for empty fields in a struct to 3067 avoid confusing output_constructor. */ 3068 if (is_empty_field (e.index) 3069 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) 3070 return false; 3071 /* Check for non-empty fields between initialized fields when 3072 CONSTRUCTOR_NO_CLEARING. */ 3073 for (; field && e.index != field; 3074 field = next_subobject_field (DECL_CHAIN (field))) 3075 if (!is_really_empty_class (TREE_TYPE (field), 3076 /*ignore_vptr*/false)) 3077 return false; 3078 if (field) 3079 field = next_subobject_field (DECL_CHAIN (field)); 3080 } 3081 /* There could be a non-empty field at the end. */ 3082 for (; field; field = next_subobject_field (DECL_CHAIN (field))) 3083 if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) 3084 return false; 3085 ok: 3086 if (CONSTRUCTOR_NO_CLEARING (t)) 3087 /* All the fields are initialized. */ 3088 CONSTRUCTOR_NO_CLEARING (t) = false; 3089 return true; 3090 3091 default: 3092 /* FIXME are we calling this too much? */ 3093 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; 3094 } 3095 } 3096 3097 /* Some expressions may have constant operands but are not constant 3098 themselves, such as 1/0. Call this function to check for that 3099 condition. 3100 3101 We only call this in places that require an arithmetic constant, not in 3102 places where we might have a non-constant expression that can be a 3103 component of a constant expression, such as the address of a constexpr 3104 variable that might be dereferenced later. */ 3105 3106 static bool 3107 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, 3108 bool *overflow_p) 3109 { 3110 if (!*non_constant_p && !reduced_constant_expression_p (t) 3111 && t != void_node) 3112 { 3113 if (!allow_non_constant) 3114 error ("%q+E is not a constant expression", t); 3115 *non_constant_p = true; 3116 } 3117 if (TREE_OVERFLOW_P (t)) 3118 { 3119 if (!allow_non_constant) 3120 { 3121 permerror (input_location, "overflow in constant expression"); 3122 /* If we're being permissive (and are in an enforcing 3123 context), ignore the overflow. */ 3124 if (flag_permissive) 3125 return *non_constant_p; 3126 } 3127 *overflow_p = true; 3128 } 3129 return *non_constant_p; 3130 } 3131 3132 /* Check whether the shift operation with code CODE and type TYPE on LHS 3133 and RHS is undefined. If it is, give an error with an explanation, 3134 and return true; return false otherwise. */ 3135 3136 static bool 3137 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, 3138 enum tree_code code, tree type, tree lhs, tree rhs) 3139 { 3140 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) 3141 || TREE_CODE (lhs) != INTEGER_CST 3142 || TREE_CODE (rhs) != INTEGER_CST) 3143 return false; 3144 3145 tree lhstype = TREE_TYPE (lhs); 3146 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); 3147 3148 /* [expr.shift] The behavior is undefined if the right operand 3149 is negative, or greater than or equal to the length in bits 3150 of the promoted left operand. */ 3151 if (tree_int_cst_sgn (rhs) == -1) 3152 { 3153 if (!ctx->quiet) 3154 permerror (loc, "right operand of shift expression %q+E is negative", 3155 build2_loc (loc, code, type, lhs, rhs)); 3156 return (!flag_permissive || ctx->quiet); 3157 } 3158 if (compare_tree_int (rhs, uprec) >= 0) 3159 { 3160 if (!ctx->quiet) 3161 permerror (loc, "right operand of shift expression %q+E is greater " 3162 "than or equal to the precision %wu of the left operand", 3163 build2_loc (loc, code, type, lhs, rhs), uprec); 3164 return (!flag_permissive || ctx->quiet); 3165 } 3166 3167 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] 3168 if E1 has a signed type and non-negative value, and E1x2^E2 is 3169 representable in the corresponding unsigned type of the result type, 3170 then that value, converted to the result type, is the resulting value; 3171 otherwise, the behavior is undefined. 3172 For C++20: 3173 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo 3174 2^N, where N is the range exponent of the type of the result. */ 3175 if (code == LSHIFT_EXPR 3176 && !TYPE_OVERFLOW_WRAPS (lhstype) 3177 && cxx_dialect >= cxx11 3178 && cxx_dialect < cxx20) 3179 { 3180 if (tree_int_cst_sgn (lhs) == -1) 3181 { 3182 if (!ctx->quiet) 3183 permerror (loc, 3184 "left operand of shift expression %q+E is negative", 3185 build2_loc (loc, code, type, lhs, rhs)); 3186 return (!flag_permissive || ctx->quiet); 3187 } 3188 /* For signed x << y the following: 3189 (unsigned) x >> ((prec (lhs) - 1) - y) 3190 if > 1, is undefined. The right-hand side of this formula 3191 is the highest bit of the LHS that can be set (starting from 0), 3192 so that the shift doesn't overflow. We then right-shift the LHS 3193 to see whether any other bit is set making the original shift 3194 undefined -- the result is not representable in the corresponding 3195 unsigned type. */ 3196 tree t = build_int_cst (unsigned_type_node, uprec - 1); 3197 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); 3198 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); 3199 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); 3200 if (tree_int_cst_lt (integer_one_node, t)) 3201 { 3202 if (!ctx->quiet) 3203 permerror (loc, "shift expression %q+E overflows", 3204 build2_loc (loc, code, type, lhs, rhs)); 3205 return (!flag_permissive || ctx->quiet); 3206 } 3207 } 3208 return false; 3209 } 3210 3211 /* Subroutine of cxx_eval_constant_expression. 3212 Attempt to reduce the unary expression tree T to a compile time value. 3213 If successful, return the value. Otherwise issue a diagnostic 3214 and return error_mark_node. */ 3215 3216 static tree 3217 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, 3218 bool /*lval*/, 3219 bool *non_constant_p, bool *overflow_p) 3220 { 3221 tree r; 3222 tree orig_arg = TREE_OPERAND (t, 0); 3223 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false, 3224 non_constant_p, overflow_p); 3225 VERIFY_CONSTANT (arg); 3226 location_t loc = EXPR_LOCATION (t); 3227 enum tree_code code = TREE_CODE (t); 3228 tree type = TREE_TYPE (t); 3229 r = fold_unary_loc (loc, code, type, arg); 3230 if (r == NULL_TREE) 3231 { 3232 if (arg == orig_arg) 3233 r = t; 3234 else 3235 r = build1_loc (loc, code, type, arg); 3236 } 3237 VERIFY_CONSTANT (r); 3238 return r; 3239 } 3240 3241 /* Helper function for cxx_eval_binary_expression. Try to optimize 3242 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the 3243 generic folding should be used. */ 3244 3245 static tree 3246 cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, 3247 tree lhs, tree rhs, bool *non_constant_p, 3248 bool *overflow_p) 3249 { 3250 STRIP_NOPS (lhs); 3251 if (TREE_CODE (lhs) != ADDR_EXPR) 3252 return NULL_TREE; 3253 3254 lhs = TREE_OPERAND (lhs, 0); 3255 3256 /* &A[i] p+ j => &A[i + j] */ 3257 if (TREE_CODE (lhs) == ARRAY_REF 3258 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST 3259 && TREE_CODE (rhs) == INTEGER_CST 3260 && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) 3261 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) 3262 { 3263 tree orig_type = TREE_TYPE (t); 3264 location_t loc = EXPR_LOCATION (t); 3265 tree type = TREE_TYPE (lhs); 3266 3267 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); 3268 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); 3269 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, 3270 overflow_p); 3271 if (*non_constant_p) 3272 return NULL_TREE; 3273 /* Don't fold an out-of-bound access. */ 3274 if (!tree_int_cst_le (t, nelts)) 3275 return NULL_TREE; 3276 rhs = cp_fold_convert (ssizetype, rhs); 3277 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. 3278 constexpr int A[1]; ... (char *)&A[0] + 1 */ 3279 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, 3280 rhs, TYPE_SIZE_UNIT (type)))) 3281 return NULL_TREE; 3282 /* Make sure to treat the second operand of POINTER_PLUS_EXPR 3283 as signed. */ 3284 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, 3285 TYPE_SIZE_UNIT (type)); 3286 t = size_binop_loc (loc, PLUS_EXPR, rhs, t); 3287 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), 3288 t, NULL_TREE, NULL_TREE); 3289 t = cp_build_addr_expr (t, tf_warning_or_error); 3290 t = cp_fold_convert (orig_type, t); 3291 return cxx_eval_constant_expression (ctx, t, /*lval*/false, 3292 non_constant_p, overflow_p); 3293 } 3294 3295 return NULL_TREE; 3296 } 3297 3298 /* Try to fold expressions like 3299 (struct S *) (&a[0].D.2378 + 12) 3300 into 3301 &MEM <struct T> [(void *)&a + 12B] 3302 This is something normally done by gimple_fold_stmt_to_constant_1 3303 on GIMPLE, but is undesirable on GENERIC if we are e.g. going to 3304 dereference the address because some details are lost. 3305 For pointer comparisons we want such folding though so that 3306 match.pd address_compare optimization works. */ 3307 3308 static tree 3309 cxx_maybe_fold_addr_pointer_plus (tree t) 3310 { 3311 while (CONVERT_EXPR_P (t) 3312 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))) 3313 t = TREE_OPERAND (t, 0); 3314 if (TREE_CODE (t) != POINTER_PLUS_EXPR) 3315 return NULL_TREE; 3316 tree op0 = TREE_OPERAND (t, 0); 3317 tree op1 = TREE_OPERAND (t, 1); 3318 if (TREE_CODE (op1) != INTEGER_CST) 3319 return NULL_TREE; 3320 while (CONVERT_EXPR_P (op0) 3321 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))) 3322 op0 = TREE_OPERAND (op0, 0); 3323 if (TREE_CODE (op0) != ADDR_EXPR) 3324 return NULL_TREE; 3325 op1 = fold_convert (ptr_type_node, op1); 3326 tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1); 3327 return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r); 3328 } 3329 3330 /* Subroutine of cxx_eval_constant_expression. 3331 Like cxx_eval_unary_expression, except for binary expressions. */ 3332 3333 static tree 3334 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, 3335 bool lval, 3336 bool *non_constant_p, bool *overflow_p) 3337 { 3338 tree r = NULL_TREE; 3339 tree orig_lhs = TREE_OPERAND (t, 0); 3340 tree orig_rhs = TREE_OPERAND (t, 1); 3341 tree lhs, rhs; 3342 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false, 3343 non_constant_p, overflow_p); 3344 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer 3345 subtraction. */ 3346 if (*non_constant_p) 3347 return t; 3348 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false, 3349 non_constant_p, overflow_p); 3350 if (*non_constant_p) 3351 return t; 3352 3353 location_t loc = EXPR_LOCATION (t); 3354 enum tree_code code = TREE_CODE (t); 3355 tree type = TREE_TYPE (t); 3356 3357 if (code == EQ_EXPR || code == NE_EXPR) 3358 { 3359 bool is_code_eq = (code == EQ_EXPR); 3360 3361 if (TREE_CODE (lhs) == PTRMEM_CST 3362 && TREE_CODE (rhs) == PTRMEM_CST) 3363 { 3364 tree lmem = PTRMEM_CST_MEMBER (lhs); 3365 tree rmem = PTRMEM_CST_MEMBER (rhs); 3366 bool eq; 3367 if (TREE_CODE (lmem) == TREE_CODE (rmem) 3368 && TREE_CODE (lmem) == FIELD_DECL 3369 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE 3370 && same_type_p (DECL_CONTEXT (lmem), 3371 DECL_CONTEXT (rmem))) 3372 /* If both refer to (possibly different) members of the same union 3373 (12.3), they compare equal. */ 3374 eq = true; 3375 else 3376 eq = cp_tree_equal (lhs, rhs); 3377 r = constant_boolean_node (eq == is_code_eq, type); 3378 } 3379 else if ((TREE_CODE (lhs) == PTRMEM_CST 3380 || TREE_CODE (rhs) == PTRMEM_CST) 3381 && (null_member_pointer_value_p (lhs) 3382 || null_member_pointer_value_p (rhs))) 3383 r = constant_boolean_node (!is_code_eq, type); 3384 else if (TREE_CODE (lhs) == PTRMEM_CST) 3385 lhs = cplus_expand_constant (lhs); 3386 else if (TREE_CODE (rhs) == PTRMEM_CST) 3387 rhs = cplus_expand_constant (rhs); 3388 } 3389 if (r == NULL_TREE 3390 && TREE_CODE_CLASS (code) == tcc_comparison 3391 && POINTER_TYPE_P (TREE_TYPE (lhs))) 3392 { 3393 if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs)) 3394 lhs = fold_convert (TREE_TYPE (lhs), lhso); 3395 if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs)) 3396 rhs = fold_convert (TREE_TYPE (rhs), rhso); 3397 } 3398 if (code == POINTER_PLUS_EXPR && !*non_constant_p 3399 && integer_zerop (lhs) && !integer_zerop (rhs)) 3400 { 3401 if (!ctx->quiet) 3402 error ("arithmetic involving a null pointer in %qE", lhs); 3403 *non_constant_p = true; 3404 return t; 3405 } 3406 else if (code == POINTER_PLUS_EXPR) 3407 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, 3408 overflow_p); 3409 else if (code == SPACESHIP_EXPR) 3410 { 3411 r = genericize_spaceship (loc, type, lhs, rhs); 3412 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, 3413 overflow_p); 3414 } 3415 3416 if (r == NULL_TREE) 3417 { 3418 if (ctx->manifestly_const_eval 3419 && (flag_constexpr_fp_except 3420 || TREE_CODE (type) != REAL_TYPE)) 3421 { 3422 auto ofcc = make_temp_override (folding_cxx_constexpr, true); 3423 r = fold_binary_initializer_loc (loc, code, type, lhs, rhs); 3424 } 3425 else 3426 r = fold_binary_loc (loc, code, type, lhs, rhs); 3427 } 3428 3429 if (r == NULL_TREE 3430 && (code == LSHIFT_EXPR || code == RSHIFT_EXPR) 3431 && TREE_CODE (lhs) == INTEGER_CST 3432 && TREE_CODE (rhs) == INTEGER_CST 3433 && wi::neg_p (wi::to_wide (rhs))) 3434 { 3435 /* For diagnostics and -fpermissive emulate previous behavior of 3436 handling shifts by negative amount. */ 3437 tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs); 3438 if (nrhs) 3439 r = fold_binary_loc (loc, 3440 code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR, 3441 type, lhs, nrhs); 3442 } 3443 3444 if (r == NULL_TREE) 3445 { 3446 if (lhs == orig_lhs && rhs == orig_rhs) 3447 r = t; 3448 else 3449 r = build2_loc (loc, code, type, lhs, rhs); 3450 } 3451 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) 3452 *non_constant_p = true; 3453 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to 3454 a local array in a constexpr function. */ 3455 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); 3456 if (!ptr) 3457 VERIFY_CONSTANT (r); 3458 return r; 3459 } 3460 3461 /* Subroutine of cxx_eval_constant_expression. 3462 Attempt to evaluate condition expressions. Dead branches are not 3463 looked into. */ 3464 3465 static tree 3466 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, 3467 bool lval, 3468 bool *non_constant_p, bool *overflow_p, 3469 tree *jump_target) 3470 { 3471 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 3472 /*lval*/false, 3473 non_constant_p, overflow_p); 3474 VERIFY_CONSTANT (val); 3475 if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t)) 3476 { 3477 /* Evaluate the condition as if it was 3478 if (__builtin_is_constant_evaluated ()), i.e. defer it if not 3479 ctx->manifestly_const_eval (as sometimes we try to constant evaluate 3480 without manifestly_const_eval even expressions or parts thereof which 3481 will later be manifestly const_eval evaluated), otherwise fold it to 3482 true. */ 3483 if (ctx->manifestly_const_eval) 3484 val = boolean_true_node; 3485 else 3486 { 3487 *non_constant_p = true; 3488 return t; 3489 } 3490 } 3491 /* Don't VERIFY_CONSTANT the other operands. */ 3492 if (integer_zerop (val)) 3493 val = TREE_OPERAND (t, 2); 3494 else 3495 val = TREE_OPERAND (t, 1); 3496 if (TREE_CODE (t) == IF_STMT && !val) 3497 val = void_node; 3498 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, 3499 overflow_p, jump_target); 3500 } 3501 3502 /* Subroutine of cxx_eval_constant_expression. 3503 Attempt to evaluate vector condition expressions. Unlike 3504 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal 3505 ternary arithmetics operation, where all 3 arguments have to be 3506 evaluated as constants and then folding computes the result from 3507 them. */ 3508 3509 static tree 3510 cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, 3511 bool *non_constant_p, bool *overflow_p) 3512 { 3513 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 3514 /*lval*/false, 3515 non_constant_p, overflow_p); 3516 VERIFY_CONSTANT (arg1); 3517 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 3518 /*lval*/false, 3519 non_constant_p, overflow_p); 3520 VERIFY_CONSTANT (arg2); 3521 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), 3522 /*lval*/false, 3523 non_constant_p, overflow_p); 3524 VERIFY_CONSTANT (arg3); 3525 location_t loc = EXPR_LOCATION (t); 3526 tree type = TREE_TYPE (t); 3527 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); 3528 if (r == NULL_TREE) 3529 { 3530 if (arg1 == TREE_OPERAND (t, 0) 3531 && arg2 == TREE_OPERAND (t, 1) 3532 && arg3 == TREE_OPERAND (t, 2)) 3533 r = t; 3534 else 3535 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); 3536 } 3537 VERIFY_CONSTANT (r); 3538 return r; 3539 } 3540 3541 /* Returns less than, equal to, or greater than zero if KEY is found to be 3542 less than, to match, or to be greater than the constructor_elt's INDEX. */ 3543 3544 static int 3545 array_index_cmp (tree key, tree index) 3546 { 3547 gcc_assert (TREE_CODE (key) == INTEGER_CST); 3548 3549 switch (TREE_CODE (index)) 3550 { 3551 case INTEGER_CST: 3552 return tree_int_cst_compare (key, index); 3553 case RANGE_EXPR: 3554 { 3555 tree lo = TREE_OPERAND (index, 0); 3556 tree hi = TREE_OPERAND (index, 1); 3557 if (tree_int_cst_lt (key, lo)) 3558 return -1; 3559 else if (tree_int_cst_lt (hi, key)) 3560 return 1; 3561 else 3562 return 0; 3563 } 3564 default: 3565 gcc_unreachable (); 3566 } 3567 } 3568 3569 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 3570 if none. If INSERT is true, insert a matching element rather than fail. */ 3571 3572 static HOST_WIDE_INT 3573 find_array_ctor_elt (tree ary, tree dindex, bool insert) 3574 { 3575 if (tree_int_cst_sgn (dindex) < 0) 3576 return -1; 3577 3578 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); 3579 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); 3580 unsigned HOST_WIDE_INT len = vec_safe_length (elts); 3581 3582 unsigned HOST_WIDE_INT end = len; 3583 unsigned HOST_WIDE_INT begin = 0; 3584 3585 /* If the last element of the CONSTRUCTOR has its own index, we can assume 3586 that the same is true of the other elements and index directly. */ 3587 if (end > 0) 3588 { 3589 tree cindex = (*elts)[end - 1].index; 3590 if (cindex == NULL_TREE) 3591 { 3592 /* Verify that if the last index is missing, all indexes 3593 are missing. */ 3594 if (flag_checking) 3595 for (unsigned int j = 0; j < len - 1; ++j) 3596 gcc_assert ((*elts)[j].index == NULL_TREE); 3597 if (i < end) 3598 return i; 3599 else 3600 { 3601 begin = end; 3602 if (i == end) 3603 /* If the element is to be added right at the end, 3604 make sure it is added with cleared index too. */ 3605 dindex = NULL_TREE; 3606 else if (insert) 3607 /* Otherwise, in order not to break the assumption 3608 that CONSTRUCTOR either has all indexes or none, 3609 we need to add indexes to all elements. */ 3610 for (unsigned int j = 0; j < len; ++j) 3611 (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j); 3612 } 3613 } 3614 else if (TREE_CODE (cindex) == INTEGER_CST 3615 && compare_tree_int (cindex, end - 1) == 0) 3616 { 3617 if (i < end) 3618 return i; 3619 else 3620 begin = end; 3621 } 3622 } 3623 3624 /* Otherwise, find a matching index by means of a binary search. */ 3625 while (begin != end) 3626 { 3627 unsigned HOST_WIDE_INT middle = (begin + end) / 2; 3628 constructor_elt &elt = (*elts)[middle]; 3629 tree idx = elt.index; 3630 3631 int cmp = array_index_cmp (dindex, idx); 3632 if (cmp < 0) 3633 end = middle; 3634 else if (cmp > 0) 3635 begin = middle + 1; 3636 else 3637 { 3638 if (insert && TREE_CODE (idx) == RANGE_EXPR) 3639 { 3640 /* We need to split the range. */ 3641 constructor_elt e; 3642 tree lo = TREE_OPERAND (idx, 0); 3643 tree hi = TREE_OPERAND (idx, 1); 3644 tree value = elt.value; 3645 dindex = fold_convert (sizetype, dindex); 3646 if (tree_int_cst_lt (lo, dindex)) 3647 { 3648 /* There are still some lower elts; shorten the range. */ 3649 tree new_hi = int_const_binop (MINUS_EXPR, dindex, 3650 size_one_node); 3651 if (tree_int_cst_equal (lo, new_hi)) 3652 /* Only one element left, no longer a range. */ 3653 elt.index = lo; 3654 else 3655 TREE_OPERAND (idx, 1) = new_hi; 3656 /* Append the element we want to insert. */ 3657 ++middle; 3658 e.index = dindex; 3659 e.value = unshare_constructor (value); 3660 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); 3661 } 3662 else 3663 /* No lower elts, the range elt is now ours. */ 3664 elt.index = dindex; 3665 3666 if (tree_int_cst_lt (dindex, hi)) 3667 { 3668 /* There are still some higher elts; append a range. */ 3669 tree new_lo = int_const_binop (PLUS_EXPR, dindex, 3670 size_one_node); 3671 if (tree_int_cst_equal (new_lo, hi)) 3672 e.index = hi; 3673 else 3674 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); 3675 e.value = unshare_constructor (value); 3676 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); 3677 } 3678 } 3679 return middle; 3680 } 3681 } 3682 3683 if (insert) 3684 { 3685 constructor_elt e = { dindex, NULL_TREE }; 3686 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); 3687 return end; 3688 } 3689 3690 return -1; 3691 } 3692 3693 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no 3694 matching constructor_elt exists, then add one to CTOR. 3695 3696 As an optimization, if POS_HINT is non-negative then it is used as a guess 3697 for the (integer) index of the matching constructor_elt within CTOR. */ 3698 3699 static constructor_elt * 3700 get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) 3701 { 3702 /* Check the hint first. */ 3703 if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor) 3704 && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index) 3705 return CONSTRUCTOR_ELT (ctor, pos_hint); 3706 3707 tree type = TREE_TYPE (ctor); 3708 if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE) 3709 { 3710 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE); 3711 return &CONSTRUCTOR_ELTS (ctor)->last(); 3712 } 3713 else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) 3714 { 3715 if (TREE_CODE (index) == RANGE_EXPR) 3716 { 3717 /* Support for RANGE_EXPR index lookups is currently limited to 3718 accessing an existing element via POS_HINT, or appending a new 3719 element to the end of CTOR. ??? Support for other access 3720 patterns may also be needed. */ 3721 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); 3722 if (vec_safe_length (elts)) 3723 { 3724 tree lo = TREE_OPERAND (index, 0); 3725 gcc_assert (array_index_cmp (elts->last().index, lo) < 0); 3726 } 3727 CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE); 3728 return &elts->last(); 3729 } 3730 3731 HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true); 3732 gcc_assert (i >= 0); 3733 constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i); 3734 gcc_assert (cep->index == NULL_TREE 3735 || TREE_CODE (cep->index) != RANGE_EXPR); 3736 return cep; 3737 } 3738 else 3739 { 3740 gcc_assert (TREE_CODE (index) == FIELD_DECL 3741 && (same_type_ignoring_top_level_qualifiers_p 3742 (DECL_CONTEXT (index), TREE_TYPE (ctor)))); 3743 3744 /* We must keep the CONSTRUCTOR's ELTS in FIELD order. 3745 Usually we meet initializers in that order, but it is 3746 possible for base types to be placed not in program 3747 order. */ 3748 tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); 3749 unsigned HOST_WIDE_INT idx = 0; 3750 constructor_elt *cep = NULL; 3751 3752 /* Check if we're changing the active member of a union. */ 3753 if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor) 3754 && CONSTRUCTOR_ELT (ctor, 0)->index != index) 3755 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0); 3756 /* If the bit offset of INDEX is larger than that of the last 3757 constructor_elt, then we can just immediately append a new 3758 constructor_elt to the end of CTOR. */ 3759 else if (CONSTRUCTOR_NELTS (ctor) 3760 && tree_int_cst_compare (bit_position (index), 3761 bit_position (CONSTRUCTOR_ELTS (ctor) 3762 ->last().index)) > 0) 3763 { 3764 idx = CONSTRUCTOR_NELTS (ctor); 3765 goto insert; 3766 } 3767 3768 /* Otherwise, we need to iterate over CTOR to find or insert INDEX 3769 appropriately. */ 3770 3771 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep); 3772 idx++, fields = DECL_CHAIN (fields)) 3773 { 3774 if (index == cep->index) 3775 goto found; 3776 3777 /* The field we're initializing must be on the field 3778 list. Look to see if it is present before the 3779 field the current ELT initializes. */ 3780 for (; fields != cep->index; fields = DECL_CHAIN (fields)) 3781 if (index == fields) 3782 goto insert; 3783 } 3784 /* We fell off the end of the CONSTRUCTOR, so insert a new 3785 entry at the end. */ 3786 3787 insert: 3788 { 3789 constructor_elt ce = { index, NULL_TREE }; 3790 3791 vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce); 3792 cep = CONSTRUCTOR_ELT (ctor, idx); 3793 } 3794 found:; 3795 3796 return cep; 3797 } 3798 } 3799 3800 /* Under the control of CTX, issue a detailed diagnostic for 3801 an out-of-bounds subscript INDEX into the expression ARRAY. */ 3802 3803 static void 3804 diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) 3805 { 3806 if (!ctx->quiet) 3807 { 3808 tree arraytype = TREE_TYPE (array); 3809 3810 /* Convert the unsigned array subscript to a signed integer to avoid 3811 printing huge numbers for small negative values. */ 3812 tree sidx = fold_convert (ssizetype, index); 3813 STRIP_ANY_LOCATION_WRAPPER (array); 3814 if (DECL_P (array)) 3815 { 3816 if (TYPE_DOMAIN (arraytype)) 3817 error_at (loc, "array subscript value %qE is outside the bounds " 3818 "of array %qD of type %qT", sidx, array, arraytype); 3819 else 3820 error_at (loc, "nonzero array subscript %qE is used with array %qD of " 3821 "type %qT with unknown bounds", sidx, array, arraytype); 3822 inform (DECL_SOURCE_LOCATION (array), "declared here"); 3823 } 3824 else if (TYPE_DOMAIN (arraytype)) 3825 error_at (loc, "array subscript value %qE is outside the bounds " 3826 "of array type %qT", sidx, arraytype); 3827 else 3828 error_at (loc, "nonzero array subscript %qE is used with array of type %qT " 3829 "with unknown bounds", sidx, arraytype); 3830 } 3831 } 3832 3833 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or 3834 a VECTOR_TYPE). */ 3835 3836 static tree 3837 get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, 3838 bool *non_constant_p, bool *overflow_p) 3839 { 3840 tree nelts; 3841 if (TREE_CODE (type) == ARRAY_TYPE) 3842 { 3843 if (TYPE_DOMAIN (type)) 3844 nelts = array_type_nelts_top (type); 3845 else 3846 nelts = size_zero_node; 3847 } 3848 else if (VECTOR_TYPE_P (type)) 3849 nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); 3850 else 3851 gcc_unreachable (); 3852 3853 /* For VLAs, the number of elements won't be an integer constant. */ 3854 nelts = cxx_eval_constant_expression (ctx, nelts, false, 3855 non_constant_p, overflow_p); 3856 return nelts; 3857 } 3858 3859 /* Extract element INDEX consisting of CHARS_PER_ELT chars from 3860 STRING_CST STRING. */ 3861 3862 static tree 3863 extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) 3864 { 3865 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); 3866 tree r; 3867 3868 if (chars_per_elt == 1) 3869 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); 3870 else 3871 { 3872 const unsigned char *ptr 3873 = ((const unsigned char *)TREE_STRING_POINTER (string) 3874 + index * chars_per_elt); 3875 r = native_interpret_expr (type, ptr, chars_per_elt); 3876 } 3877 return r; 3878 } 3879 3880 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the 3881 subscript, diagnose any problems with it, and return the result. */ 3882 3883 static tree 3884 eval_and_check_array_index (const constexpr_ctx *ctx, 3885 tree t, bool allow_one_past, 3886 bool *non_constant_p, bool *overflow_p) 3887 { 3888 location_t loc = cp_expr_loc_or_input_loc (t); 3889 tree ary = TREE_OPERAND (t, 0); 3890 t = TREE_OPERAND (t, 1); 3891 tree index = cxx_eval_constant_expression (ctx, t, false, 3892 non_constant_p, overflow_p); 3893 VERIFY_CONSTANT (index); 3894 3895 if (!tree_fits_shwi_p (index) 3896 || tree_int_cst_sgn (index) < 0) 3897 { 3898 diag_array_subscript (loc, ctx, ary, index); 3899 *non_constant_p = true; 3900 return t; 3901 } 3902 3903 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, 3904 overflow_p); 3905 VERIFY_CONSTANT (nelts); 3906 if (allow_one_past 3907 ? !tree_int_cst_le (index, nelts) 3908 : !tree_int_cst_lt (index, nelts)) 3909 { 3910 diag_array_subscript (loc, ctx, ary, index); 3911 *non_constant_p = true; 3912 return t; 3913 } 3914 3915 return index; 3916 } 3917 3918 /* Subroutine of cxx_eval_constant_expression. 3919 Attempt to reduce a reference to an array slot. */ 3920 3921 static tree 3922 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, 3923 bool lval, 3924 bool *non_constant_p, bool *overflow_p) 3925 { 3926 tree oldary = TREE_OPERAND (t, 0); 3927 tree ary = cxx_eval_constant_expression (ctx, oldary, 3928 lval, 3929 non_constant_p, overflow_p); 3930 if (*non_constant_p) 3931 return t; 3932 if (!lval 3933 && TREE_CODE (ary) == VIEW_CONVERT_EXPR 3934 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) 3935 && (TYPE_MAIN_VARIANT (TREE_TYPE (t)) 3936 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))))) 3937 ary = TREE_OPERAND (ary, 0); 3938 3939 tree oldidx = TREE_OPERAND (t, 1); 3940 tree index = eval_and_check_array_index (ctx, t, lval, 3941 non_constant_p, overflow_p); 3942 if (*non_constant_p) 3943 return t; 3944 3945 if (lval && ary == oldary && index == oldidx) 3946 return t; 3947 else if (lval) 3948 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); 3949 3950 unsigned len = 0, elem_nchars = 1; 3951 tree elem_type = TREE_TYPE (TREE_TYPE (ary)); 3952 if (TREE_CODE (ary) == CONSTRUCTOR) 3953 len = CONSTRUCTOR_NELTS (ary); 3954 else if (TREE_CODE (ary) == STRING_CST) 3955 { 3956 elem_nchars = (TYPE_PRECISION (elem_type) 3957 / TYPE_PRECISION (char_type_node)); 3958 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; 3959 } 3960 else if (TREE_CODE (ary) == VECTOR_CST) 3961 /* We don't create variable-length VECTOR_CSTs. */ 3962 len = VECTOR_CST_NELTS (ary).to_constant (); 3963 else 3964 { 3965 /* We can't do anything with other tree codes, so use 3966 VERIFY_CONSTANT to complain and fail. */ 3967 VERIFY_CONSTANT (ary); 3968 gcc_unreachable (); 3969 } 3970 3971 bool found; 3972 HOST_WIDE_INT i = 0; 3973 if (TREE_CODE (ary) == CONSTRUCTOR) 3974 { 3975 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); 3976 found = (ix >= 0); 3977 if (found) 3978 i = ix; 3979 } 3980 else 3981 { 3982 i = tree_to_shwi (index); 3983 found = (i < len); 3984 } 3985 3986 if (found) 3987 { 3988 tree r; 3989 if (TREE_CODE (ary) == CONSTRUCTOR) 3990 r = (*CONSTRUCTOR_ELTS (ary))[i].value; 3991 else if (TREE_CODE (ary) == VECTOR_CST) 3992 r = VECTOR_CST_ELT (ary, i); 3993 else 3994 r = extract_string_elt (ary, elem_nchars, i); 3995 3996 if (r) 3997 /* Don't VERIFY_CONSTANT here. */ 3998 return r; 3999 4000 /* Otherwise the element doesn't have a value yet. */ 4001 } 4002 4003 /* Not found. */ 4004 4005 if (TREE_CODE (ary) == CONSTRUCTOR 4006 && CONSTRUCTOR_NO_CLEARING (ary)) 4007 { 4008 /* 'ary' is part of the aggregate initializer we're currently 4009 building; if there's no initializer for this element yet, 4010 that's an error. */ 4011 if (!ctx->quiet) 4012 error ("accessing uninitialized array element"); 4013 *non_constant_p = true; 4014 return t; 4015 } 4016 4017 /* If it's within the array bounds but doesn't have an explicit 4018 initializer, it's initialized from {}. But use build_value_init 4019 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ 4020 tree val; 4021 constexpr_ctx new_ctx; 4022 if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) 4023 return build_constructor (elem_type, NULL); 4024 else if (CP_AGGREGATE_TYPE_P (elem_type)) 4025 { 4026 tree empty_ctor = build_constructor (init_list_type_node, NULL); 4027 val = digest_init (elem_type, empty_ctor, tf_warning_or_error); 4028 } 4029 else 4030 val = build_value_init (elem_type, tf_warning_or_error); 4031 4032 if (!SCALAR_TYPE_P (elem_type)) 4033 { 4034 new_ctx = *ctx; 4035 if (ctx->object) 4036 /* If there was no object, don't add one: it could confuse us 4037 into thinking we're modifying a const object. */ 4038 new_ctx.object = t; 4039 new_ctx.ctor = build_constructor (elem_type, NULL); 4040 ctx = &new_ctx; 4041 } 4042 t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, 4043 overflow_p); 4044 if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor) 4045 free_constructor (ctx->ctor); 4046 return t; 4047 } 4048 4049 /* Subroutine of cxx_eval_constant_expression. 4050 Attempt to reduce a field access of a value of class type. */ 4051 4052 static tree 4053 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, 4054 bool lval, 4055 bool *non_constant_p, bool *overflow_p) 4056 { 4057 unsigned HOST_WIDE_INT i; 4058 tree field; 4059 tree value; 4060 tree part = TREE_OPERAND (t, 1); 4061 tree orig_whole = TREE_OPERAND (t, 0); 4062 tree whole = cxx_eval_constant_expression (ctx, orig_whole, 4063 lval, 4064 non_constant_p, overflow_p); 4065 if (INDIRECT_REF_P (whole) 4066 && integer_zerop (TREE_OPERAND (whole, 0))) 4067 { 4068 if (!ctx->quiet) 4069 error ("dereferencing a null pointer in %qE", orig_whole); 4070 *non_constant_p = true; 4071 return t; 4072 } 4073 4074 if (TREE_CODE (whole) == PTRMEM_CST) 4075 whole = cplus_expand_constant (whole); 4076 if (whole == orig_whole) 4077 return t; 4078 if (lval) 4079 return fold_build3 (COMPONENT_REF, TREE_TYPE (t), 4080 whole, part, NULL_TREE); 4081 /* Don't VERIFY_CONSTANT here; we only want to check that we got a 4082 CONSTRUCTOR. */ 4083 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) 4084 { 4085 if (!ctx->quiet) 4086 error ("%qE is not a constant expression", orig_whole); 4087 *non_constant_p = true; 4088 } 4089 if (DECL_MUTABLE_P (part)) 4090 { 4091 if (!ctx->quiet) 4092 error ("mutable %qD is not usable in a constant expression", part); 4093 *non_constant_p = true; 4094 } 4095 if (*non_constant_p) 4096 return t; 4097 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); 4098 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) 4099 { 4100 /* Use name match for PMF fields, as a variant will have a 4101 different FIELD_DECL with a different type. */ 4102 if (pmf ? DECL_NAME (field) == DECL_NAME (part) 4103 : field == part) 4104 { 4105 if (value) 4106 { 4107 STRIP_ANY_LOCATION_WRAPPER (value); 4108 return value; 4109 } 4110 else 4111 /* We're in the middle of initializing it. */ 4112 break; 4113 } 4114 } 4115 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE 4116 && CONSTRUCTOR_NELTS (whole) > 0) 4117 { 4118 /* DR 1188 says we don't have to deal with this. */ 4119 if (!ctx->quiet) 4120 { 4121 constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); 4122 if (cep->value == NULL_TREE) 4123 error ("accessing uninitialized member %qD", part); 4124 else 4125 error ("accessing %qD member instead of initialized %qD member in " 4126 "constant expression", part, cep->index); 4127 } 4128 *non_constant_p = true; 4129 return t; 4130 } 4131 4132 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty 4133 classes never get represented; throw together a value now. */ 4134 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 4135 return build_constructor (TREE_TYPE (t), NULL); 4136 4137 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); 4138 4139 if (CONSTRUCTOR_NO_CLEARING (whole)) 4140 { 4141 /* 'whole' is part of the aggregate initializer we're currently 4142 building; if there's no initializer for this member yet, that's an 4143 error. */ 4144 if (!ctx->quiet) 4145 error ("accessing uninitialized member %qD", part); 4146 *non_constant_p = true; 4147 return t; 4148 } 4149 4150 /* If there's no explicit init for this field, it's value-initialized. */ 4151 value = build_value_init (TREE_TYPE (t), tf_warning_or_error); 4152 return cxx_eval_constant_expression (ctx, value, 4153 lval, 4154 non_constant_p, overflow_p); 4155 } 4156 4157 /* Subroutine of cxx_eval_constant_expression. 4158 Attempt to reduce a field access of a value of class type that is 4159 expressed as a BIT_FIELD_REF. */ 4160 4161 static tree 4162 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, 4163 bool lval, 4164 bool *non_constant_p, bool *overflow_p) 4165 { 4166 tree orig_whole = TREE_OPERAND (t, 0); 4167 tree retval, fldval, utype, mask; 4168 bool fld_seen = false; 4169 HOST_WIDE_INT istart, isize; 4170 tree whole = cxx_eval_constant_expression (ctx, orig_whole, 4171 lval, 4172 non_constant_p, overflow_p); 4173 tree start, field, value; 4174 unsigned HOST_WIDE_INT i; 4175 4176 if (whole == orig_whole) 4177 return t; 4178 /* Don't VERIFY_CONSTANT here; we only want to check that we got a 4179 CONSTRUCTOR. */ 4180 if (!*non_constant_p 4181 && TREE_CODE (whole) != VECTOR_CST 4182 && TREE_CODE (whole) != CONSTRUCTOR) 4183 { 4184 if (!ctx->quiet) 4185 error ("%qE is not a constant expression", orig_whole); 4186 *non_constant_p = true; 4187 } 4188 if (*non_constant_p) 4189 return t; 4190 4191 if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t))) 4192 { 4193 if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, 4194 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2))) 4195 return r; 4196 if (!ctx->quiet) 4197 error ("%qE is not a constant expression", orig_whole); 4198 *non_constant_p = true; 4199 return t; 4200 } 4201 4202 start = TREE_OPERAND (t, 2); 4203 istart = tree_to_shwi (start); 4204 isize = tree_to_shwi (TREE_OPERAND (t, 1)); 4205 utype = TREE_TYPE (t); 4206 if (!TYPE_UNSIGNED (utype)) 4207 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); 4208 retval = build_int_cst (utype, 0); 4209 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) 4210 { 4211 tree bitpos = bit_position (field); 4212 STRIP_ANY_LOCATION_WRAPPER (value); 4213 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) 4214 return value; 4215 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE 4216 && TREE_CODE (value) == INTEGER_CST 4217 && tree_fits_shwi_p (bitpos) 4218 && tree_fits_shwi_p (DECL_SIZE (field))) 4219 { 4220 HOST_WIDE_INT bit = tree_to_shwi (bitpos); 4221 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); 4222 HOST_WIDE_INT shift; 4223 if (bit >= istart && bit + sz <= istart + isize) 4224 { 4225 fldval = fold_convert (utype, value); 4226 mask = build_int_cst_type (utype, -1); 4227 mask = fold_build2 (LSHIFT_EXPR, utype, mask, 4228 size_int (TYPE_PRECISION (utype) - sz)); 4229 mask = fold_build2 (RSHIFT_EXPR, utype, mask, 4230 size_int (TYPE_PRECISION (utype) - sz)); 4231 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); 4232 shift = bit - istart; 4233 if (BYTES_BIG_ENDIAN) 4234 shift = TYPE_PRECISION (utype) - shift - sz; 4235 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, 4236 size_int (shift)); 4237 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); 4238 fld_seen = true; 4239 } 4240 } 4241 } 4242 if (fld_seen) 4243 return fold_convert (TREE_TYPE (t), retval); 4244 gcc_unreachable (); 4245 return error_mark_node; 4246 } 4247 4248 /* Helper for cxx_eval_bit_cast. 4249 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From 4250 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>, 4251 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static 4252 data members of reference type. */ 4253 4254 static bool 4255 check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, 4256 tree orig_type) 4257 { 4258 if (TREE_CODE (type) == UNION_TYPE) 4259 { 4260 if (!ctx->quiet) 4261 { 4262 if (type == orig_type) 4263 error_at (loc, "%qs is not a constant expression because %qT is " 4264 "a union type", "__builtin_bit_cast", type); 4265 else 4266 error_at (loc, "%qs is not a constant expression because %qT " 4267 "contains a union type", "__builtin_bit_cast", 4268 orig_type); 4269 } 4270 return true; 4271 } 4272 if (TREE_CODE (type) == POINTER_TYPE) 4273 { 4274 if (!ctx->quiet) 4275 { 4276 if (type == orig_type) 4277 error_at (loc, "%qs is not a constant expression because %qT is " 4278 "a pointer type", "__builtin_bit_cast", type); 4279 else 4280 error_at (loc, "%qs is not a constant expression because %qT " 4281 "contains a pointer type", "__builtin_bit_cast", 4282 orig_type); 4283 } 4284 return true; 4285 } 4286 if (TREE_CODE (type) == REFERENCE_TYPE) 4287 { 4288 if (!ctx->quiet) 4289 { 4290 if (type == orig_type) 4291 error_at (loc, "%qs is not a constant expression because %qT is " 4292 "a reference type", "__builtin_bit_cast", type); 4293 else 4294 error_at (loc, "%qs is not a constant expression because %qT " 4295 "contains a reference type", "__builtin_bit_cast", 4296 orig_type); 4297 } 4298 return true; 4299 } 4300 if (TYPE_PTRMEM_P (type)) 4301 { 4302 if (!ctx->quiet) 4303 { 4304 if (type == orig_type) 4305 error_at (loc, "%qs is not a constant expression because %qT is " 4306 "a pointer to member type", "__builtin_bit_cast", 4307 type); 4308 else 4309 error_at (loc, "%qs is not a constant expression because %qT " 4310 "contains a pointer to member type", 4311 "__builtin_bit_cast", orig_type); 4312 } 4313 return true; 4314 } 4315 if (TYPE_VOLATILE (type)) 4316 { 4317 if (!ctx->quiet) 4318 { 4319 if (type == orig_type) 4320 error_at (loc, "%qs is not a constant expression because %qT is " 4321 "volatile", "__builtin_bit_cast", type); 4322 else 4323 error_at (loc, "%qs is not a constant expression because %qT " 4324 "contains a volatile subobject", 4325 "__builtin_bit_cast", orig_type); 4326 } 4327 return true; 4328 } 4329 if (TREE_CODE (type) == RECORD_TYPE) 4330 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 4331 if (TREE_CODE (field) == FIELD_DECL 4332 && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) 4333 return true; 4334 return false; 4335 } 4336 4337 /* Helper function for cxx_eval_bit_cast. For unsigned char or 4338 std::byte members of CONSTRUCTOR (recursively) if they contain 4339 some indeterminate bits (as set in MASK), remove the ctor elts, 4340 mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the 4341 bits in MASK. */ 4342 4343 static void 4344 clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask) 4345 { 4346 if (TREE_CODE (t) != CONSTRUCTOR) 4347 return; 4348 4349 unsigned i, j = 0; 4350 tree index, value; 4351 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value) 4352 { 4353 tree type = TREE_TYPE (value); 4354 if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE 4355 && DECL_BIT_FIELD_TYPE (index) != NULL_TREE) 4356 { 4357 if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index))) 4358 { 4359 HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index)); 4360 gcc_assert (fldsz != 0); 4361 HOST_WIDE_INT pos = int_byte_position (index); 4362 HOST_WIDE_INT bpos 4363 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index)); 4364 bpos %= BITS_PER_UNIT; 4365 HOST_WIDE_INT end 4366 = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; 4367 gcc_assert (end == 1 || end == 2); 4368 unsigned char *p = mask + pos; 4369 unsigned char mask_save[2]; 4370 mask_save[0] = mask[pos]; 4371 mask_save[1] = end == 2 ? mask[pos + 1] : 0; 4372 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) 4373 sorry_at (loc, "PDP11 bit-field handling unsupported" 4374 " in %qs", "__builtin_bit_cast"); 4375 else if (BYTES_BIG_ENDIAN) 4376 { 4377 /* Big endian. */ 4378 if (bpos + fldsz <= BITS_PER_UNIT) 4379 *p &= ~(((1 << fldsz) - 1) 4380 << (BITS_PER_UNIT - bpos - fldsz)); 4381 else 4382 { 4383 gcc_assert (bpos); 4384 *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos); 4385 p++; 4386 fldsz -= BITS_PER_UNIT - bpos; 4387 gcc_assert (fldsz && fldsz < BITS_PER_UNIT); 4388 *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; 4389 } 4390 } 4391 else 4392 { 4393 /* Little endian. */ 4394 if (bpos + fldsz <= BITS_PER_UNIT) 4395 *p &= ~(((1 << fldsz) - 1) << bpos); 4396 else 4397 { 4398 gcc_assert (bpos); 4399 *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); 4400 p++; 4401 fldsz -= BITS_PER_UNIT - bpos; 4402 gcc_assert (fldsz && fldsz < BITS_PER_UNIT); 4403 *p &= ~((1 << fldsz) - 1); 4404 } 4405 } 4406 if (mask_save[0] != mask[pos] 4407 || (end == 2 && mask_save[1] != mask[pos + 1])) 4408 { 4409 CONSTRUCTOR_NO_CLEARING (t) = 1; 4410 continue; 4411 } 4412 } 4413 } 4414 else if (is_byte_access_type_not_plain_char (type)) 4415 { 4416 HOST_WIDE_INT pos; 4417 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 4418 pos = tree_to_shwi (index); 4419 else 4420 pos = int_byte_position (index); 4421 if (mask[pos]) 4422 { 4423 CONSTRUCTOR_NO_CLEARING (t) = 1; 4424 mask[pos] = 0; 4425 continue; 4426 } 4427 } 4428 if (TREE_CODE (value) == CONSTRUCTOR) 4429 { 4430 HOST_WIDE_INT pos; 4431 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 4432 pos = tree_to_shwi (index) 4433 * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))); 4434 else 4435 pos = int_byte_position (index); 4436 clear_uchar_or_std_byte_in_mask (loc, value, mask + pos); 4437 } 4438 if (i != j) 4439 { 4440 CONSTRUCTOR_ELT (t, j)->index = index; 4441 CONSTRUCTOR_ELT (t, j)->value = value; 4442 } 4443 ++j; 4444 } 4445 if (CONSTRUCTOR_NELTS (t) != j) 4446 vec_safe_truncate (CONSTRUCTOR_ELTS (t), j); 4447 } 4448 4449 /* Subroutine of cxx_eval_constant_expression. 4450 Attempt to evaluate a BIT_CAST_EXPR. */ 4451 4452 static tree 4453 cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, 4454 bool *overflow_p) 4455 { 4456 if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), 4457 TREE_TYPE (t)) 4458 || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0), 4459 EXPR_LOCATION (t)), 4460 TREE_TYPE (TREE_OPERAND (t, 0)), 4461 TREE_TYPE (TREE_OPERAND (t, 0)))) 4462 { 4463 *non_constant_p = true; 4464 return t; 4465 } 4466 4467 tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, 4468 non_constant_p, overflow_p); 4469 if (*non_constant_p) 4470 return t; 4471 4472 location_t loc = EXPR_LOCATION (t); 4473 if (BITS_PER_UNIT != 8 || CHAR_BIT != 8) 4474 { 4475 if (!ctx->quiet) 4476 sorry_at (loc, "%qs cannot be constant evaluated on the target", 4477 "__builtin_bit_cast"); 4478 *non_constant_p = true; 4479 return t; 4480 } 4481 4482 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) 4483 { 4484 if (!ctx->quiet) 4485 sorry_at (loc, "%qs cannot be constant evaluated because the " 4486 "type is too large", "__builtin_bit_cast"); 4487 *non_constant_p = true; 4488 return t; 4489 } 4490 4491 HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); 4492 if (len < 0 || (int) len != len) 4493 { 4494 if (!ctx->quiet) 4495 sorry_at (loc, "%qs cannot be constant evaluated because the " 4496 "type is too large", "__builtin_bit_cast"); 4497 *non_constant_p = true; 4498 return t; 4499 } 4500 4501 unsigned char buf[64]; 4502 unsigned char *ptr, *mask; 4503 size_t alen = (size_t) len * 2; 4504 if (alen <= sizeof (buf)) 4505 ptr = buf; 4506 else 4507 ptr = XNEWVEC (unsigned char, alen); 4508 mask = ptr + (size_t) len; 4509 /* At the beginning consider everything indeterminate. */ 4510 memset (mask, ~0, (size_t) len); 4511 4512 if (native_encode_initializer (op, ptr, len, 0, mask) != len) 4513 { 4514 if (!ctx->quiet) 4515 sorry_at (loc, "%qs cannot be constant evaluated because the " 4516 "argument cannot be encoded", "__builtin_bit_cast"); 4517 *non_constant_p = true; 4518 if (ptr != buf) 4519 XDELETE (ptr); 4520 return t; 4521 } 4522 4523 tree r = NULL_TREE; 4524 if (can_native_interpret_type_p (TREE_TYPE (t))) 4525 { 4526 r = native_interpret_expr (TREE_TYPE (t), ptr, len); 4527 if (is_byte_access_type_not_plain_char (TREE_TYPE (t))) 4528 { 4529 gcc_assert (len == 1); 4530 if (mask[0]) 4531 { 4532 memset (mask, 0, len); 4533 r = build_constructor (TREE_TYPE (r), NULL); 4534 CONSTRUCTOR_NO_CLEARING (r) = 1; 4535 } 4536 } 4537 } 4538 else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) 4539 { 4540 r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); 4541 if (r != NULL_TREE) 4542 { 4543 clear_type_padding_in_mask (TREE_TYPE (t), mask); 4544 clear_uchar_or_std_byte_in_mask (loc, r, mask); 4545 } 4546 } 4547 4548 if (r != NULL_TREE) 4549 { 4550 for (int i = 0; i < len; i++) 4551 if (mask[i]) 4552 { 4553 if (!ctx->quiet) 4554 error_at (loc, "%qs accessing uninitialized byte at offset %d", 4555 "__builtin_bit_cast", i); 4556 *non_constant_p = true; 4557 r = t; 4558 break; 4559 } 4560 if (ptr != buf) 4561 XDELETE (ptr); 4562 return r; 4563 } 4564 4565 if (!ctx->quiet) 4566 sorry_at (loc, "%qs cannot be constant evaluated because the " 4567 "argument cannot be interpreted", "__builtin_bit_cast"); 4568 *non_constant_p = true; 4569 if (ptr != buf) 4570 XDELETE (ptr); 4571 return t; 4572 } 4573 4574 /* Subroutine of cxx_eval_constant_expression. 4575 Evaluate a short-circuited logical expression T in the context 4576 of a given constexpr CALL. BAILOUT_VALUE is the value for 4577 early return. CONTINUE_VALUE is used here purely for 4578 sanity check purposes. */ 4579 4580 static tree 4581 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, 4582 tree bailout_value, tree continue_value, 4583 bool *non_constant_p, bool *overflow_p) 4584 { 4585 tree r; 4586 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 4587 /*lval*/false, non_constant_p, 4588 overflow_p); 4589 VERIFY_CONSTANT (lhs); 4590 if (tree_int_cst_equal (lhs, bailout_value)) 4591 return lhs; 4592 gcc_assert (tree_int_cst_equal (lhs, continue_value)); 4593 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 4594 /*lval*/false, non_constant_p, 4595 overflow_p); 4596 VERIFY_CONSTANT (r); 4597 return r; 4598 } 4599 4600 /* REF is a COMPONENT_REF designating a particular field. V is a vector of 4601 CONSTRUCTOR elements to initialize (part of) an object containing that 4602 field. Return a pointer to the constructor_elt corresponding to the 4603 initialization of the field. */ 4604 4605 static constructor_elt * 4606 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) 4607 { 4608 tree aggr = TREE_OPERAND (ref, 0); 4609 tree field = TREE_OPERAND (ref, 1); 4610 HOST_WIDE_INT i; 4611 constructor_elt *ce; 4612 4613 gcc_assert (TREE_CODE (ref) == COMPONENT_REF); 4614 4615 if (TREE_CODE (aggr) == COMPONENT_REF) 4616 { 4617 constructor_elt *base_ce 4618 = base_field_constructor_elt (v, aggr); 4619 v = CONSTRUCTOR_ELTS (base_ce->value); 4620 } 4621 4622 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 4623 if (ce->index == field) 4624 return ce; 4625 4626 gcc_unreachable (); 4627 return NULL; 4628 } 4629 4630 /* Some of the expressions fed to the constexpr mechanism are calls to 4631 constructors, which have type void. In that case, return the type being 4632 initialized by the constructor. */ 4633 4634 static tree 4635 initialized_type (tree t) 4636 { 4637 if (TYPE_P (t)) 4638 return t; 4639 tree type = TREE_TYPE (t); 4640 if (TREE_CODE (t) == CALL_EXPR) 4641 { 4642 /* A constructor call has void type, so we need to look deeper. */ 4643 tree fn = get_function_named_in_call (t); 4644 if (fn && TREE_CODE (fn) == FUNCTION_DECL 4645 && DECL_CXX_CONSTRUCTOR_P (fn)) 4646 type = DECL_CONTEXT (fn); 4647 } 4648 else if (TREE_CODE (t) == COMPOUND_EXPR) 4649 return initialized_type (TREE_OPERAND (t, 1)); 4650 else if (TREE_CODE (t) == AGGR_INIT_EXPR) 4651 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); 4652 return cv_unqualified (type); 4653 } 4654 4655 /* We're about to initialize element INDEX of an array or class from VALUE. 4656 Set up NEW_CTX appropriately by adjusting .object to refer to the 4657 subobject and creating a new CONSTRUCTOR if the element is itself 4658 a class or array. */ 4659 4660 static void 4661 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, 4662 tree index, tree &value) 4663 { 4664 new_ctx = *ctx; 4665 4666 if (index && TREE_CODE (index) != INTEGER_CST 4667 && TREE_CODE (index) != FIELD_DECL 4668 && TREE_CODE (index) != RANGE_EXPR) 4669 /* This won't have an element in the new CONSTRUCTOR. */ 4670 return; 4671 4672 tree type = initialized_type (value); 4673 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) 4674 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ 4675 return; 4676 if (VECTOR_TYPE_P (type) 4677 && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor)) 4678 && index == NULL_TREE) 4679 /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger 4680 vector is constructed from smaller vectors, doesn't get its own 4681 CONSTRUCTOR either. */ 4682 return; 4683 4684 /* The sub-aggregate initializer might contain a placeholder; 4685 update object to refer to the subobject and ctor to refer to 4686 the (newly created) sub-initializer. */ 4687 if (ctx->object) 4688 { 4689 if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) 4690 /* There's no well-defined subobject for this index. */ 4691 new_ctx.object = NULL_TREE; 4692 else 4693 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); 4694 } 4695 tree elt = build_constructor (type, NULL); 4696 CONSTRUCTOR_NO_CLEARING (elt) = true; 4697 new_ctx.ctor = elt; 4698 4699 if (TREE_CODE (value) == TARGET_EXPR) 4700 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ 4701 value = TARGET_EXPR_INITIAL (value); 4702 } 4703 4704 /* We're about to process an initializer for a class or array TYPE. Make 4705 sure that CTX is set up appropriately. */ 4706 4707 static void 4708 verify_ctor_sanity (const constexpr_ctx *ctx, tree type) 4709 { 4710 /* We don't bother building a ctor for an empty base subobject. */ 4711 if (is_empty_class (type)) 4712 return; 4713 4714 /* We're in the middle of an initializer that might involve placeholders; 4715 our caller should have created a CONSTRUCTOR for us to put the 4716 initializer into. We will either return that constructor or T. */ 4717 gcc_assert (ctx->ctor); 4718 gcc_assert (same_type_ignoring_top_level_qualifiers_p 4719 (type, TREE_TYPE (ctx->ctor))); 4720 /* We used to check that ctx->ctor was empty, but that isn't the case when 4721 the object is zero-initialized before calling the constructor. */ 4722 if (ctx->object) 4723 { 4724 tree otype = TREE_TYPE (ctx->object); 4725 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) 4726 /* Handle flexible array members. */ 4727 || (TREE_CODE (otype) == ARRAY_TYPE 4728 && TYPE_DOMAIN (otype) == NULL_TREE 4729 && TREE_CODE (type) == ARRAY_TYPE 4730 && (same_type_ignoring_top_level_qualifiers_p 4731 (TREE_TYPE (type), TREE_TYPE (otype))))); 4732 } 4733 gcc_assert (!ctx->object || !DECL_P (ctx->object) 4734 || *(ctx->global->values.get (ctx->object)) == ctx->ctor); 4735 } 4736 4737 /* Subroutine of cxx_eval_constant_expression. 4738 The expression tree T denotes a C-style array or a C-style 4739 aggregate. Reduce it to a constant expression. */ 4740 4741 static tree 4742 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, 4743 bool lval, 4744 bool *non_constant_p, bool *overflow_p) 4745 { 4746 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); 4747 bool changed = false; 4748 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); 4749 tree type = TREE_TYPE (t); 4750 4751 constexpr_ctx new_ctx; 4752 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) 4753 { 4754 /* We don't really need the ctx->ctor business for a PMF or 4755 vector, but it's simpler to use the same code. */ 4756 new_ctx = *ctx; 4757 new_ctx.ctor = build_constructor (type, NULL); 4758 new_ctx.object = NULL_TREE; 4759 ctx = &new_ctx; 4760 }; 4761 verify_ctor_sanity (ctx, type); 4762 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); 4763 vec_alloc (*p, vec_safe_length (v)); 4764 4765 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) 4766 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; 4767 4768 unsigned i; 4769 tree index, value; 4770 bool constant_p = true; 4771 bool side_effects_p = false; 4772 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) 4773 { 4774 tree orig_value = value; 4775 /* Like in cxx_eval_store_expression, omit entries for empty fields. */ 4776 bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index); 4777 init_subob_ctx (ctx, new_ctx, index, value); 4778 int pos_hint = -1; 4779 if (new_ctx.ctor != ctx->ctor && !no_slot) 4780 { 4781 /* If we built a new CONSTRUCTOR, attach it now so that other 4782 initializers can refer to it. */ 4783 constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); 4784 cep->value = new_ctx.ctor; 4785 pos_hint = cep - (*p)->begin(); 4786 } 4787 else if (TREE_CODE (type) == UNION_TYPE) 4788 /* Otherwise if we're constructing a non-aggregate union member, set 4789 the active union member now so that we can later detect and diagnose 4790 if its initializer attempts to activate another member. */ 4791 get_or_insert_ctor_field (ctx->ctor, index); 4792 tree elt = cxx_eval_constant_expression (&new_ctx, value, 4793 lval, 4794 non_constant_p, overflow_p); 4795 /* Don't VERIFY_CONSTANT here. */ 4796 if (ctx->quiet && *non_constant_p) 4797 break; 4798 if (elt != orig_value) 4799 changed = true; 4800 4801 if (!TREE_CONSTANT (elt)) 4802 constant_p = false; 4803 if (TREE_SIDE_EFFECTS (elt)) 4804 side_effects_p = true; 4805 if (index && TREE_CODE (index) == COMPONENT_REF) 4806 { 4807 /* This is an initialization of a vfield inside a base 4808 subaggregate that we already initialized; push this 4809 initialization into the previous initialization. */ 4810 constructor_elt *inner = base_field_constructor_elt (*p, index); 4811 inner->value = elt; 4812 changed = true; 4813 } 4814 else if (index 4815 && (TREE_CODE (index) == NOP_EXPR 4816 || TREE_CODE (index) == POINTER_PLUS_EXPR)) 4817 { 4818 /* This is an initializer for an empty base; now that we've 4819 checked that it's constant, we can ignore it. */ 4820 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); 4821 changed = true; 4822 } 4823 else if (no_slot) 4824 changed = true; 4825 else 4826 { 4827 if (TREE_CODE (type) == UNION_TYPE 4828 && (*p)->last().index != index) 4829 /* The initializer erroneously changed the active union member that 4830 we're initializing. */ 4831 gcc_assert (*non_constant_p); 4832 else 4833 { 4834 /* The initializer might have mutated the underlying CONSTRUCTOR, 4835 so recompute the location of the target constructer_elt. */ 4836 constructor_elt *cep 4837 = get_or_insert_ctor_field (ctx->ctor, index, pos_hint); 4838 cep->value = elt; 4839 } 4840 4841 /* Adding or replacing an element might change the ctor's flags. */ 4842 TREE_CONSTANT (ctx->ctor) = constant_p; 4843 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; 4844 } 4845 } 4846 if (*non_constant_p || !changed) 4847 return t; 4848 t = ctx->ctor; 4849 /* We're done building this CONSTRUCTOR, so now we can interpret an 4850 element without an explicit initializer as value-initialized. */ 4851 CONSTRUCTOR_NO_CLEARING (t) = false; 4852 TREE_CONSTANT (t) = constant_p; 4853 TREE_SIDE_EFFECTS (t) = side_effects_p; 4854 if (VECTOR_TYPE_P (type)) 4855 t = fold (t); 4856 return t; 4857 } 4858 4859 /* Subroutine of cxx_eval_constant_expression. 4860 The expression tree T is a VEC_INIT_EXPR which denotes the desired 4861 initialization of a non-static data member of array type. Reduce it to a 4862 CONSTRUCTOR. 4863 4864 Note that apart from value-initialization (when VALUE_INIT is true), 4865 this is only intended to support value-initialization and the 4866 initializations done by defaulted constructors for classes with 4867 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT 4868 will either be NULL_TREE for the default constructor, or a COMPONENT_REF 4869 for the copy/move constructor. */ 4870 4871 static tree 4872 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, 4873 bool value_init, bool lval, 4874 bool *non_constant_p, bool *overflow_p) 4875 { 4876 tree elttype = TREE_TYPE (atype); 4877 verify_ctor_sanity (ctx, atype); 4878 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); 4879 bool pre_init = false; 4880 unsigned HOST_WIDE_INT i; 4881 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; 4882 4883 if (init && TREE_CODE (init) == CONSTRUCTOR) 4884 return cxx_eval_bare_aggregate (ctx, init, lval, 4885 non_constant_p, overflow_p); 4886 4887 /* For the default constructor, build up a call to the default 4888 constructor of the element type. We only need to handle class types 4889 here, as for a constructor to be constexpr, all members must be 4890 initialized, which for a defaulted default constructor means they must 4891 be of a class type with a constexpr default constructor. */ 4892 if (TREE_CODE (elttype) == ARRAY_TYPE) 4893 /* We only do this at the lowest level. */; 4894 else if (value_init) 4895 { 4896 init = build_value_init (elttype, complain); 4897 pre_init = true; 4898 } 4899 else if (!init) 4900 { 4901 releasing_vec argvec; 4902 init = build_special_member_call (NULL_TREE, complete_ctor_identifier, 4903 &argvec, elttype, LOOKUP_NORMAL, 4904 complain); 4905 init = build_aggr_init_expr (elttype, init); 4906 pre_init = true; 4907 } 4908 4909 bool zeroed_out = false; 4910 if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor)) 4911 { 4912 /* We're initializing an array object that had been zero-initialized 4913 earlier. Truncate ctx->ctor, and propagate its zeroed state by 4914 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element 4915 initializers we append to it. */ 4916 gcc_checking_assert (initializer_zerop (ctx->ctor)); 4917 zeroed_out = true; 4918 vec_safe_truncate (*p, 0); 4919 } 4920 4921 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, 4922 overflow_p); 4923 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); 4924 for (i = 0; i < max; ++i) 4925 { 4926 tree idx = build_int_cst (size_type_node, i); 4927 tree eltinit; 4928 bool reuse = false; 4929 constexpr_ctx new_ctx; 4930 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); 4931 if (new_ctx.ctor != ctx->ctor) 4932 { 4933 if (zeroed_out) 4934 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; 4935 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); 4936 } 4937 if (TREE_CODE (elttype) == ARRAY_TYPE) 4938 { 4939 /* A multidimensional array; recurse. */ 4940 if (value_init || init == NULL_TREE) 4941 { 4942 eltinit = NULL_TREE; 4943 reuse = i == 0; 4944 } 4945 else 4946 eltinit = cp_build_array_ref (input_location, init, idx, complain); 4947 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, 4948 lval, 4949 non_constant_p, overflow_p); 4950 } 4951 else if (pre_init) 4952 { 4953 /* Initializing an element using value or default initialization 4954 we just pre-built above. */ 4955 if (init == void_node) 4956 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ 4957 return ctx->ctor; 4958 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, 4959 non_constant_p, overflow_p); 4960 reuse = i == 0; 4961 } 4962 else 4963 { 4964 /* Copying an element. */ 4965 gcc_assert (same_type_ignoring_top_level_qualifiers_p 4966 (atype, TREE_TYPE (init))); 4967 eltinit = cp_build_array_ref (input_location, init, idx, complain); 4968 if (!lvalue_p (init)) 4969 eltinit = move (eltinit); 4970 eltinit = force_rvalue (eltinit, complain); 4971 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, 4972 non_constant_p, overflow_p); 4973 } 4974 if (*non_constant_p) 4975 break; 4976 if (new_ctx.ctor != ctx->ctor) 4977 { 4978 /* We appended this element above; update the value. */ 4979 gcc_assert ((*p)->last().index == idx); 4980 (*p)->last().value = eltinit; 4981 } 4982 else 4983 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); 4984 /* Reuse the result of cxx_eval_constant_expression call 4985 from the first iteration to all others if it is a constant 4986 initializer that doesn't require relocations. */ 4987 if (reuse 4988 && max > 1 4989 && (eltinit == NULL_TREE 4990 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) 4991 == null_pointer_node))) 4992 { 4993 if (new_ctx.ctor != ctx->ctor) 4994 eltinit = new_ctx.ctor; 4995 tree range = build2 (RANGE_EXPR, size_type_node, 4996 build_int_cst (size_type_node, 1), 4997 build_int_cst (size_type_node, max - 1)); 4998 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); 4999 break; 5000 } 5001 else if (i == 0) 5002 vec_safe_reserve (*p, max); 5003 } 5004 5005 if (!*non_constant_p) 5006 { 5007 init = ctx->ctor; 5008 CONSTRUCTOR_NO_CLEARING (init) = false; 5009 } 5010 return init; 5011 } 5012 5013 static tree 5014 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, 5015 bool lval, 5016 bool *non_constant_p, bool *overflow_p) 5017 { 5018 tree atype = TREE_TYPE (t); 5019 tree init = VEC_INIT_EXPR_INIT (t); 5020 bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); 5021 if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) 5022 ; 5023 else if (CONSTRUCTOR_NELTS (init) == 0 5024 && !CP_AGGREGATE_TYPE_P (strip_array_types (atype))) 5025 { 5026 /* Handle {} as value-init. */ 5027 init = NULL_TREE; 5028 value_init = true; 5029 } 5030 else 5031 { 5032 /* This is a more complicated case, like needing to loop over trailing 5033 elements; call build_vec_init and evaluate the result. */ 5034 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; 5035 constexpr_ctx new_ctx = *ctx; 5036 if (!ctx->object) 5037 { 5038 /* We want to have an initialization target for an VEC_INIT_EXPR. 5039 If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */ 5040 new_ctx.object = VEC_INIT_EXPR_SLOT (t); 5041 tree ctor = new_ctx.ctor = build_constructor (atype, NULL); 5042 CONSTRUCTOR_NO_CLEARING (ctor) = true; 5043 ctx->global->values.put (new_ctx.object, ctor); 5044 ctx = &new_ctx; 5045 } 5046 init = expand_vec_init_expr (ctx->object, t, complain); 5047 return cxx_eval_constant_expression (ctx, init, lval, non_constant_p, 5048 overflow_p); 5049 } 5050 tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init, 5051 lval, non_constant_p, overflow_p); 5052 if (*non_constant_p) 5053 return t; 5054 else 5055 return r; 5056 } 5057 5058 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case 5059 where the desired type is an array of unknown bounds because the variable 5060 has had its bounds deduced since the wrapping expression was created. */ 5061 5062 static bool 5063 same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) 5064 { 5065 while (TREE_CODE (type1) == ARRAY_TYPE 5066 && TREE_CODE (type2) == ARRAY_TYPE 5067 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) 5068 { 5069 type1 = TREE_TYPE (type1); 5070 type2 = TREE_TYPE (type2); 5071 } 5072 return same_type_ignoring_top_level_qualifiers_p (type1, type2); 5073 } 5074 5075 /* Try to determine the currently active union member for an expression 5076 with UNION_TYPE. If it can be determined, return the FIELD_DECL, 5077 otherwise return NULL_TREE. */ 5078 5079 static tree 5080 cxx_union_active_member (const constexpr_ctx *ctx, tree t) 5081 { 5082 constexpr_ctx new_ctx = *ctx; 5083 new_ctx.quiet = true; 5084 bool non_constant_p = false, overflow_p = false; 5085 tree ctor = cxx_eval_constant_expression (&new_ctx, t, false, 5086 &non_constant_p, 5087 &overflow_p); 5088 if (TREE_CODE (ctor) == CONSTRUCTOR 5089 && CONSTRUCTOR_NELTS (ctor) == 1 5090 && CONSTRUCTOR_ELT (ctor, 0)->index 5091 && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL) 5092 return CONSTRUCTOR_ELT (ctor, 0)->index; 5093 return NULL_TREE; 5094 } 5095 5096 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */ 5097 5098 static tree 5099 cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, 5100 tree op, unsigned HOST_WIDE_INT off, bool *empty_base) 5101 { 5102 tree optype = TREE_TYPE (op); 5103 unsigned HOST_WIDE_INT const_nunits; 5104 if (off == 0 && similar_type_p (optype, type)) 5105 return op; 5106 else if (TREE_CODE (optype) == COMPLEX_TYPE 5107 && similar_type_p (type, TREE_TYPE (optype))) 5108 { 5109 /* *(foo *)&complexfoo => __real__ complexfoo */ 5110 if (off == 0) 5111 return build1_loc (loc, REALPART_EXPR, type, op); 5112 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ 5113 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) 5114 return build1_loc (loc, IMAGPART_EXPR, type, op); 5115 } 5116 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ 5117 else if (VECTOR_TYPE_P (optype) 5118 && similar_type_p (type, TREE_TYPE (optype)) 5119 && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits)) 5120 { 5121 unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); 5122 unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; 5123 if (off < max_offset && off % part_width == 0) 5124 { 5125 tree index = bitsize_int (off * BITS_PER_UNIT); 5126 return build3_loc (loc, BIT_FIELD_REF, type, op, 5127 TYPE_SIZE (type), index); 5128 } 5129 } 5130 /* ((foo *)&fooarray)[x] => fooarray[x] */ 5131 else if (TREE_CODE (optype) == ARRAY_TYPE 5132 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) 5133 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) 5134 { 5135 tree type_domain = TYPE_DOMAIN (optype); 5136 tree min_val = size_zero_node; 5137 if (type_domain && TYPE_MIN_VALUE (type_domain)) 5138 min_val = TYPE_MIN_VALUE (type_domain); 5139 unsigned HOST_WIDE_INT el_sz 5140 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); 5141 unsigned HOST_WIDE_INT idx = off / el_sz; 5142 unsigned HOST_WIDE_INT rem = off % el_sz; 5143 if (tree_fits_uhwi_p (min_val)) 5144 { 5145 tree index = size_int (idx + tree_to_uhwi (min_val)); 5146 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index, 5147 NULL_TREE, NULL_TREE); 5148 return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, 5149 empty_base); 5150 } 5151 } 5152 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ 5153 else if (TREE_CODE (optype) == RECORD_TYPE 5154 || TREE_CODE (optype) == UNION_TYPE) 5155 { 5156 if (TREE_CODE (optype) == UNION_TYPE) 5157 /* For unions prefer the currently active member. */ 5158 if (tree field = cxx_union_active_member (ctx, op)) 5159 { 5160 unsigned HOST_WIDE_INT el_sz 5161 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); 5162 if (off < el_sz) 5163 { 5164 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), 5165 op, field, NULL_TREE); 5166 if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, 5167 off, empty_base)) 5168 return ret; 5169 } 5170 } 5171 for (tree field = TYPE_FIELDS (optype); 5172 field; field = DECL_CHAIN (field)) 5173 if (TREE_CODE (field) == FIELD_DECL 5174 && TREE_TYPE (field) != error_mark_node 5175 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) 5176 { 5177 tree pos = byte_position (field); 5178 if (!tree_fits_uhwi_p (pos)) 5179 continue; 5180 unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); 5181 unsigned HOST_WIDE_INT el_sz 5182 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); 5183 if (upos <= off && off < upos + el_sz) 5184 { 5185 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), 5186 op, field, NULL_TREE); 5187 if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, 5188 off - upos, 5189 empty_base)) 5190 return ret; 5191 } 5192 } 5193 /* Also handle conversion to an empty base class, which 5194 is represented with a NOP_EXPR. */ 5195 if (is_empty_class (type) 5196 && CLASS_TYPE_P (optype) 5197 && DERIVED_FROM_P (type, optype)) 5198 { 5199 *empty_base = true; 5200 return op; 5201 } 5202 } 5203 5204 return NULL_TREE; 5205 } 5206 5207 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to 5208 match. We want to be less strict for simple *& folding; if we have a 5209 non-const temporary that we access through a const pointer, that should 5210 work. We handle this here rather than change fold_indirect_ref_1 5211 because we're dealing with things like ADDR_EXPR of INTEGER_CST which 5212 don't really make sense outside of constant expression evaluation. Also 5213 we want to allow folding to COMPONENT_REF, which could cause trouble 5214 with TBAA in fold_indirect_ref_1. */ 5215 5216 static tree 5217 cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, 5218 tree op0, bool *empty_base) 5219 { 5220 tree sub = op0; 5221 tree subtype; 5222 poly_uint64 const_op01; 5223 5224 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ 5225 while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR 5226 || TREE_CODE (sub) == VIEW_CONVERT_EXPR) 5227 { 5228 if (TREE_CODE (sub) == NOP_EXPR 5229 && REINTERPRET_CAST_P (sub)) 5230 return NULL_TREE; 5231 sub = TREE_OPERAND (sub, 0); 5232 } 5233 5234 subtype = TREE_TYPE (sub); 5235 if (!INDIRECT_TYPE_P (subtype)) 5236 return NULL_TREE; 5237 5238 /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing 5239 the innermost component into the offset until it would make the 5240 offset positive, so that cxx_fold_indirect_ref_1 can identify 5241 more folding opportunities. */ 5242 auto canonicalize_obj_off = [] (tree& obj, tree& off) { 5243 while (TREE_CODE (obj) == COMPONENT_REF 5244 && (tree_int_cst_sign_bit (off) || integer_zerop (off))) 5245 { 5246 tree field = TREE_OPERAND (obj, 1); 5247 tree pos = byte_position (field); 5248 if (integer_zerop (off) && integer_nonzerop (pos)) 5249 /* If the offset is already 0, keep going as long as the 5250 component is at position 0. */ 5251 break; 5252 off = int_const_binop (PLUS_EXPR, off, pos); 5253 obj = TREE_OPERAND (obj, 0); 5254 } 5255 }; 5256 5257 if (TREE_CODE (sub) == ADDR_EXPR) 5258 { 5259 tree op = TREE_OPERAND (sub, 0); 5260 tree optype = TREE_TYPE (op); 5261 5262 /* *&CONST_DECL -> to the value of the const decl. */ 5263 if (TREE_CODE (op) == CONST_DECL) 5264 return DECL_INITIAL (op); 5265 /* *&p => p; make sure to handle *&"str"[cst] here. */ 5266 if (similar_type_p (optype, type)) 5267 { 5268 tree fop = fold_read_from_constant_string (op); 5269 if (fop) 5270 return fop; 5271 else 5272 return op; 5273 } 5274 else 5275 { 5276 tree off = integer_zero_node; 5277 canonicalize_obj_off (op, off); 5278 gcc_assert (integer_zerop (off)); 5279 return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); 5280 } 5281 } 5282 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR 5283 && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) 5284 { 5285 tree op00 = TREE_OPERAND (sub, 0); 5286 tree off = TREE_OPERAND (sub, 1); 5287 5288 STRIP_NOPS (op00); 5289 if (TREE_CODE (op00) == ADDR_EXPR) 5290 { 5291 tree obj = TREE_OPERAND (op00, 0); 5292 canonicalize_obj_off (obj, off); 5293 return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, 5294 tree_to_uhwi (off), empty_base); 5295 } 5296 } 5297 /* *(foo *)fooarrptr => (*fooarrptr)[0] */ 5298 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE 5299 && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) 5300 { 5301 tree type_domain; 5302 tree min_val = size_zero_node; 5303 tree newsub 5304 = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); 5305 if (newsub) 5306 sub = newsub; 5307 else 5308 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); 5309 type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); 5310 if (type_domain && TYPE_MIN_VALUE (type_domain)) 5311 min_val = TYPE_MIN_VALUE (type_domain); 5312 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, 5313 NULL_TREE); 5314 } 5315 5316 return NULL_TREE; 5317 } 5318 5319 static tree 5320 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, 5321 bool lval, 5322 bool *non_constant_p, bool *overflow_p) 5323 { 5324 tree orig_op0 = TREE_OPERAND (t, 0); 5325 bool empty_base = false; 5326 5327 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second 5328 operand is an integer-zero. Otherwise reject the MEM_REF for now. */ 5329 5330 if (TREE_CODE (t) == MEM_REF 5331 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) 5332 { 5333 gcc_assert (ctx->quiet); 5334 *non_constant_p = true; 5335 return t; 5336 } 5337 5338 /* First try to simplify it directly. */ 5339 tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), 5340 orig_op0, &empty_base); 5341 if (!r) 5342 { 5343 /* If that didn't work, evaluate the operand first. */ 5344 tree op0 = cxx_eval_constant_expression (ctx, orig_op0, 5345 /*lval*/false, non_constant_p, 5346 overflow_p); 5347 /* Don't VERIFY_CONSTANT here. */ 5348 if (*non_constant_p) 5349 return t; 5350 5351 if (!lval && integer_zerop (op0)) 5352 { 5353 if (!ctx->quiet) 5354 error ("dereferencing a null pointer"); 5355 *non_constant_p = true; 5356 return t; 5357 } 5358 5359 r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, 5360 &empty_base); 5361 if (r == NULL_TREE) 5362 { 5363 /* We couldn't fold to a constant value. Make sure it's not 5364 something we should have been able to fold. */ 5365 tree sub = op0; 5366 STRIP_NOPS (sub); 5367 if (TREE_CODE (sub) == ADDR_EXPR) 5368 { 5369 gcc_assert (!similar_type_p 5370 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); 5371 /* DR 1188 says we don't have to deal with this. */ 5372 if (!ctx->quiet) 5373 error_at (cp_expr_loc_or_input_loc (t), 5374 "accessing value of %qE through a %qT glvalue in a " 5375 "constant expression", build_fold_indirect_ref (sub), 5376 TREE_TYPE (t)); 5377 *non_constant_p = true; 5378 return t; 5379 } 5380 5381 if (lval && op0 != orig_op0) 5382 return build1 (INDIRECT_REF, TREE_TYPE (t), op0); 5383 if (!lval) 5384 VERIFY_CONSTANT (t); 5385 return t; 5386 } 5387 } 5388 5389 r = cxx_eval_constant_expression (ctx, r, 5390 lval, non_constant_p, overflow_p); 5391 if (*non_constant_p) 5392 return t; 5393 5394 /* If we're pulling out the value of an empty base, just return an empty 5395 CONSTRUCTOR. */ 5396 if (empty_base && !lval) 5397 { 5398 r = build_constructor (TREE_TYPE (t), NULL); 5399 TREE_CONSTANT (r) = true; 5400 } 5401 5402 return r; 5403 } 5404 5405 /* Complain about R, a VAR_DECL, not being usable in a constant expression. 5406 Shared between potential_constant_expression and 5407 cxx_eval_constant_expression. */ 5408 5409 static void 5410 non_const_var_error (location_t loc, tree r) 5411 { 5412 auto_diagnostic_group d; 5413 tree type = TREE_TYPE (r); 5414 if (DECL_NAME (r) == heap_uninit_identifier 5415 || DECL_NAME (r) == heap_identifier 5416 || DECL_NAME (r) == heap_vec_uninit_identifier 5417 || DECL_NAME (r) == heap_vec_identifier) 5418 { 5419 error_at (loc, "the content of uninitialized storage is not usable " 5420 "in a constant expression"); 5421 inform (DECL_SOURCE_LOCATION (r), "allocated here"); 5422 return; 5423 } 5424 if (DECL_NAME (r) == heap_deleted_identifier) 5425 { 5426 error_at (loc, "use of allocated storage after deallocation in a " 5427 "constant expression"); 5428 inform (DECL_SOURCE_LOCATION (r), "allocated here"); 5429 return; 5430 } 5431 error_at (loc, "the value of %qD is not usable in a constant " 5432 "expression", r); 5433 /* Avoid error cascade. */ 5434 if (DECL_INITIAL (r) == error_mark_node) 5435 return; 5436 if (DECL_DECLARED_CONSTEXPR_P (r)) 5437 inform (DECL_SOURCE_LOCATION (r), 5438 "%qD used in its own initializer", r); 5439 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 5440 { 5441 if (!CP_TYPE_CONST_P (type)) 5442 inform (DECL_SOURCE_LOCATION (r), 5443 "%q#D is not const", r); 5444 else if (CP_TYPE_VOLATILE_P (type)) 5445 inform (DECL_SOURCE_LOCATION (r), 5446 "%q#D is volatile", r); 5447 else if (!DECL_INITIAL (r) 5448 || !TREE_CONSTANT (DECL_INITIAL (r)) 5449 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) 5450 inform (DECL_SOURCE_LOCATION (r), 5451 "%qD was not initialized with a constant " 5452 "expression", r); 5453 else 5454 gcc_unreachable (); 5455 } 5456 else if (TYPE_REF_P (type)) 5457 inform (DECL_SOURCE_LOCATION (r), 5458 "%qD was not initialized with a constant " 5459 "expression", r); 5460 else 5461 { 5462 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) 5463 inform (DECL_SOURCE_LOCATION (r), 5464 "%qD was not declared %<constexpr%>", r); 5465 else 5466 inform (DECL_SOURCE_LOCATION (r), 5467 "%qD does not have integral or enumeration type", 5468 r); 5469 } 5470 } 5471 5472 /* Subroutine of cxx_eval_constant_expression. 5473 Like cxx_eval_unary_expression, except for trinary expressions. */ 5474 5475 static tree 5476 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, 5477 bool lval, 5478 bool *non_constant_p, bool *overflow_p) 5479 { 5480 int i; 5481 tree args[3]; 5482 tree val; 5483 5484 for (i = 0; i < 3; i++) 5485 { 5486 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), 5487 lval, 5488 non_constant_p, overflow_p); 5489 VERIFY_CONSTANT (args[i]); 5490 } 5491 5492 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), 5493 args[0], args[1], args[2]); 5494 if (val == NULL_TREE) 5495 return t; 5496 VERIFY_CONSTANT (val); 5497 return val; 5498 } 5499 5500 /* True if T was declared in a function declared to be constexpr, and 5501 therefore potentially constant in C++14. */ 5502 5503 bool 5504 var_in_constexpr_fn (tree t) 5505 { 5506 tree ctx = DECL_CONTEXT (t); 5507 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL 5508 && DECL_DECLARED_CONSTEXPR_P (ctx)); 5509 } 5510 5511 /* True if a function might be constexpr: either a function that was 5512 declared constexpr, or a C++17 lambda op(). */ 5513 5514 bool 5515 maybe_constexpr_fn (tree t) 5516 { 5517 return (DECL_DECLARED_CONSTEXPR_P (t) 5518 || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t)) 5519 || (flag_implicit_constexpr 5520 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t)))); 5521 } 5522 5523 /* True if T was declared in a function that might be constexpr: either a 5524 function that was declared constexpr, or a C++17 lambda op(). */ 5525 5526 bool 5527 var_in_maybe_constexpr_fn (tree t) 5528 { 5529 return (DECL_FUNCTION_SCOPE_P (t) 5530 && maybe_constexpr_fn (DECL_CONTEXT (t))); 5531 } 5532 5533 /* We're assigning INIT to TARGET. In do_build_copy_constructor and 5534 build_over_call we implement trivial copy of a class with tail padding using 5535 assignment of character arrays, which is valid in normal code, but not in 5536 constexpr evaluation. We don't need to worry about clobbering tail padding 5537 in constexpr evaluation, so strip the type punning. */ 5538 5539 static void 5540 maybe_simplify_trivial_copy (tree &target, tree &init) 5541 { 5542 if (TREE_CODE (target) == MEM_REF 5543 && TREE_CODE (init) == MEM_REF 5544 && TREE_TYPE (target) == TREE_TYPE (init) 5545 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE 5546 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) 5547 { 5548 target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); 5549 init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); 5550 } 5551 } 5552 5553 /* Returns true if REF, which is a COMPONENT_REF, has any fields 5554 of constant type. This does not check for 'mutable', so the 5555 caller is expected to be mindful of that. */ 5556 5557 static bool 5558 cref_has_const_field (tree ref) 5559 { 5560 while (TREE_CODE (ref) == COMPONENT_REF) 5561 { 5562 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) 5563 return true; 5564 ref = TREE_OPERAND (ref, 0); 5565 } 5566 return false; 5567 } 5568 5569 /* Return true if we are modifying something that is const during constant 5570 expression evaluation. CODE is the code of the statement, OBJ is the 5571 object in question, MUTABLE_P is true if one of the subobjects were 5572 declared mutable. */ 5573 5574 static bool 5575 modifying_const_object_p (tree_code code, tree obj, bool mutable_p) 5576 { 5577 /* If this is initialization, there's no problem. */ 5578 if (code != MODIFY_EXPR) 5579 return false; 5580 5581 /* [basic.type.qualifier] "A const object is an object of type 5582 const T or a non-mutable subobject of a const object." */ 5583 if (mutable_p) 5584 return false; 5585 5586 if (TREE_READONLY (obj)) 5587 return true; 5588 5589 if (CP_TYPE_CONST_P (TREE_TYPE (obj))) 5590 { 5591 /* Although a COMPONENT_REF may have a const type, we should 5592 only consider it modifying a const object when any of the 5593 field components is const. This can happen when using 5594 constructs such as const_cast<const T &>(m), making something 5595 const even though it wasn't declared const. */ 5596 if (TREE_CODE (obj) == COMPONENT_REF) 5597 return cref_has_const_field (obj); 5598 else 5599 return true; 5600 } 5601 5602 return false; 5603 } 5604 5605 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ 5606 5607 static tree 5608 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, 5609 bool lval, 5610 bool *non_constant_p, bool *overflow_p) 5611 { 5612 constexpr_ctx new_ctx = *ctx; 5613 5614 tree init = TREE_OPERAND (t, 1); 5615 if (TREE_CLOBBER_P (init)) 5616 /* Just ignore clobbers. */ 5617 return void_node; 5618 5619 /* First we figure out where we're storing to. */ 5620 tree target = TREE_OPERAND (t, 0); 5621 5622 maybe_simplify_trivial_copy (target, init); 5623 5624 tree type = TREE_TYPE (target); 5625 bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; 5626 if (preeval) 5627 { 5628 /* Evaluate the value to be stored without knowing what object it will be 5629 stored in, so that any side-effects happen first. */ 5630 if (!SCALAR_TYPE_P (type)) 5631 new_ctx.ctor = new_ctx.object = NULL_TREE; 5632 init = cxx_eval_constant_expression (&new_ctx, init, false, 5633 non_constant_p, overflow_p); 5634 if (*non_constant_p) 5635 return t; 5636 } 5637 5638 bool evaluated = false; 5639 if (lval) 5640 { 5641 /* If we want to return a reference to the target, we need to evaluate it 5642 as a whole; otherwise, only evaluate the innermost piece to avoid 5643 building up unnecessary *_REFs. */ 5644 target = cxx_eval_constant_expression (ctx, target, true, 5645 non_constant_p, overflow_p); 5646 evaluated = true; 5647 if (*non_constant_p) 5648 return t; 5649 } 5650 5651 /* Find the underlying variable. */ 5652 releasing_vec refs; 5653 tree object = NULL_TREE; 5654 /* If we're modifying a const object, save it. */ 5655 tree const_object_being_modified = NULL_TREE; 5656 bool mutable_p = false; 5657 for (tree probe = target; object == NULL_TREE; ) 5658 { 5659 switch (TREE_CODE (probe)) 5660 { 5661 case BIT_FIELD_REF: 5662 case COMPONENT_REF: 5663 case ARRAY_REF: 5664 { 5665 tree ob = TREE_OPERAND (probe, 0); 5666 tree elt = TREE_OPERAND (probe, 1); 5667 if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) 5668 mutable_p = true; 5669 if (TREE_CODE (probe) == ARRAY_REF) 5670 { 5671 elt = eval_and_check_array_index (ctx, probe, false, 5672 non_constant_p, overflow_p); 5673 if (*non_constant_p) 5674 return t; 5675 } 5676 /* We don't check modifying_const_object_p for ARRAY_REFs. Given 5677 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though 5678 the array isn't const. Instead, check "a" in the next iteration; 5679 that will detect modifying "const int a[10]". */ 5680 else if (evaluated 5681 && modifying_const_object_p (TREE_CODE (t), probe, 5682 mutable_p) 5683 && const_object_being_modified == NULL_TREE) 5684 const_object_being_modified = probe; 5685 vec_safe_push (refs, elt); 5686 vec_safe_push (refs, TREE_TYPE (probe)); 5687 probe = ob; 5688 } 5689 break; 5690 5691 default: 5692 if (evaluated) 5693 object = probe; 5694 else 5695 { 5696 probe = cxx_eval_constant_expression (ctx, probe, true, 5697 non_constant_p, overflow_p); 5698 evaluated = true; 5699 if (*non_constant_p) 5700 return t; 5701 } 5702 break; 5703 } 5704 } 5705 5706 if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) 5707 && const_object_being_modified == NULL_TREE) 5708 const_object_being_modified = object; 5709 5710 /* And then find/build up our initializer for the path to the subobject 5711 we're initializing. */ 5712 tree *valp; 5713 if (DECL_P (object)) 5714 valp = ctx->global->values.get (object); 5715 else 5716 valp = NULL; 5717 if (!valp) 5718 { 5719 /* A constant-expression cannot modify objects from outside the 5720 constant-expression. */ 5721 if (!ctx->quiet) 5722 error ("modification of %qE is not a constant expression", object); 5723 *non_constant_p = true; 5724 return t; 5725 } 5726 type = TREE_TYPE (object); 5727 bool no_zero_init = true; 5728 5729 releasing_vec ctors, indexes; 5730 auto_vec<int> index_pos_hints; 5731 bool activated_union_member_p = false; 5732 while (!refs->is_empty ()) 5733 { 5734 if (*valp == NULL_TREE) 5735 { 5736 *valp = build_constructor (type, NULL); 5737 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; 5738 } 5739 else if (STRIP_ANY_LOCATION_WRAPPER (*valp), 5740 TREE_CODE (*valp) == STRING_CST) 5741 { 5742 /* An array was initialized with a string constant, and now 5743 we're writing into one of its elements. Explode the 5744 single initialization into a set of element 5745 initializations. */ 5746 gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 5747 5748 tree string = *valp; 5749 tree elt_type = TREE_TYPE (type); 5750 unsigned chars_per_elt = (TYPE_PRECISION (elt_type) 5751 / TYPE_PRECISION (char_type_node)); 5752 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; 5753 tree ary_ctor = build_constructor (type, NULL); 5754 5755 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); 5756 for (unsigned ix = 0; ix != num_elts; ix++) 5757 { 5758 constructor_elt elt = 5759 { 5760 build_int_cst (size_type_node, ix), 5761 extract_string_elt (string, chars_per_elt, ix) 5762 }; 5763 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); 5764 } 5765 5766 *valp = ary_ctor; 5767 } 5768 5769 /* If the value of object is already zero-initialized, any new ctors for 5770 subobjects will also be zero-initialized. */ 5771 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); 5772 5773 enum tree_code code = TREE_CODE (type); 5774 type = refs->pop(); 5775 tree index = refs->pop(); 5776 5777 if (code == RECORD_TYPE && is_empty_field (index)) 5778 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they 5779 have no data and might have an offset lower than previously declared 5780 fields, which confuses the middle-end. The code below will notice 5781 that we don't have a CONSTRUCTOR for our inner target and just 5782 return init. */ 5783 break; 5784 5785 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) 5786 && CONSTRUCTOR_ELT (*valp, 0)->index != index) 5787 { 5788 if (cxx_dialect < cxx20) 5789 { 5790 if (!ctx->quiet) 5791 error_at (cp_expr_loc_or_input_loc (t), 5792 "change of the active member of a union " 5793 "from %qD to %qD", 5794 CONSTRUCTOR_ELT (*valp, 0)->index, 5795 index); 5796 *non_constant_p = true; 5797 } 5798 else if (TREE_CODE (t) == MODIFY_EXPR 5799 && CONSTRUCTOR_NO_CLEARING (*valp)) 5800 { 5801 /* Diagnose changing the active union member while the union 5802 is in the process of being initialized. */ 5803 if (!ctx->quiet) 5804 error_at (cp_expr_loc_or_input_loc (t), 5805 "change of the active member of a union " 5806 "from %qD to %qD during initialization", 5807 CONSTRUCTOR_ELT (*valp, 0)->index, 5808 index); 5809 *non_constant_p = true; 5810 } 5811 no_zero_init = true; 5812 } 5813 5814 vec_safe_push (ctors, *valp); 5815 vec_safe_push (indexes, index); 5816 5817 constructor_elt *cep 5818 = get_or_insert_ctor_field (*valp, index); 5819 index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); 5820 5821 if (code == UNION_TYPE) 5822 activated_union_member_p = true; 5823 5824 valp = &cep->value; 5825 } 5826 5827 /* Detect modifying a constant object in constexpr evaluation. 5828 We have found a const object that is being modified. Figure out 5829 if we need to issue an error. Consider 5830 5831 struct A { 5832 int n; 5833 constexpr A() : n(1) { n = 2; } // #1 5834 }; 5835 struct B { 5836 const A a; 5837 constexpr B() { a.n = 3; } // #2 5838 }; 5839 constexpr B b{}; 5840 5841 #1 is OK, since we're modifying an object under construction, but 5842 #2 is wrong, since "a" is const and has been fully constructed. 5843 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR 5844 which means that the object is read-only. For the example above, the 5845 *ctors stack at the point of #2 will look like: 5846 5847 ctors[0] = {.a={.n=2}} TREE_READONLY = 0 5848 ctors[1] = {.n=2} TREE_READONLY = 1 5849 5850 and we're modifying "b.a", so we search the stack and see if the 5851 constructor for "b.a" has already run. */ 5852 if (const_object_being_modified) 5853 { 5854 bool fail = false; 5855 tree const_objtype 5856 = strip_array_types (TREE_TYPE (const_object_being_modified)); 5857 if (!CLASS_TYPE_P (const_objtype)) 5858 fail = true; 5859 else 5860 { 5861 /* [class.ctor]p5 "A constructor can be invoked for a const, 5862 volatile, or const volatile object. const and volatile 5863 semantics are not applied on an object under construction. 5864 They come into effect when the constructor for the most 5865 derived object ends." */ 5866 for (tree elt : *ctors) 5867 if (same_type_ignoring_top_level_qualifiers_p 5868 (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) 5869 { 5870 fail = TREE_READONLY (elt); 5871 break; 5872 } 5873 } 5874 if (fail) 5875 { 5876 if (!ctx->quiet) 5877 modifying_const_object_error (t, const_object_being_modified); 5878 *non_constant_p = true; 5879 return t; 5880 } 5881 } 5882 5883 if (!preeval) 5884 { 5885 /* We're handling an INIT_EXPR of class type, so the value of the 5886 initializer can depend on the object it's initializing. */ 5887 5888 /* Create a new CONSTRUCTOR in case evaluation of the initializer 5889 wants to modify it. */ 5890 if (*valp == NULL_TREE) 5891 { 5892 *valp = build_constructor (type, NULL); 5893 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; 5894 } 5895 new_ctx.ctor = *valp; 5896 new_ctx.object = target; 5897 /* Avoid temporary materialization when initializing from a TARGET_EXPR. 5898 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because 5899 expansion of those trees uses ctx instead. */ 5900 if (TREE_CODE (init) == TARGET_EXPR) 5901 if (tree tinit = TARGET_EXPR_INITIAL (init)) 5902 init = tinit; 5903 init = cxx_eval_constant_expression (&new_ctx, init, false, 5904 non_constant_p, overflow_p); 5905 /* The hash table might have moved since the get earlier, and the 5906 initializer might have mutated the underlying CONSTRUCTORs, so we must 5907 recompute VALP. */ 5908 valp = ctx->global->values.get (object); 5909 for (unsigned i = 0; i < vec_safe_length (indexes); i++) 5910 { 5911 constructor_elt *cep 5912 = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); 5913 valp = &cep->value; 5914 } 5915 } 5916 5917 /* Don't share a CONSTRUCTOR that might be changed later. */ 5918 init = unshare_constructor (init); 5919 5920 if (*valp && TREE_CODE (*valp) == CONSTRUCTOR 5921 && TREE_CODE (init) == CONSTRUCTOR) 5922 { 5923 /* An outer ctx->ctor might be pointing to *valp, so replace 5924 its contents. */ 5925 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), 5926 TREE_TYPE (*valp))) 5927 { 5928 /* For initialization of an empty base, the original target will be 5929 *(base*)this, evaluation of which resolves to the object 5930 argument, which has the derived type rather than the base type. In 5931 this situation, just evaluate the initializer and return, since 5932 there's no actual data to store. */ 5933 gcc_assert (is_empty_class (TREE_TYPE (init))); 5934 return lval ? target : init; 5935 } 5936 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); 5937 TREE_CONSTANT (*valp) = TREE_CONSTANT (init); 5938 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); 5939 CONSTRUCTOR_NO_CLEARING (*valp) 5940 = CONSTRUCTOR_NO_CLEARING (init); 5941 } 5942 else if (TREE_CODE (init) == CONSTRUCTOR 5943 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), 5944 type)) 5945 { 5946 /* See above on initialization of empty bases. */ 5947 gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval); 5948 if (!*valp) 5949 { 5950 /* But do make sure we have something in *valp. */ 5951 *valp = build_constructor (type, nullptr); 5952 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; 5953 } 5954 return init; 5955 } 5956 else 5957 *valp = init; 5958 5959 /* After initialization, 'const' semantics apply to the value of the 5960 object. Make a note of this fact by marking the CONSTRUCTOR 5961 TREE_READONLY. */ 5962 if (TREE_CODE (t) == INIT_EXPR 5963 && TREE_CODE (*valp) == CONSTRUCTOR 5964 && TYPE_READONLY (type)) 5965 { 5966 if (INDIRECT_REF_P (target) 5967 && (is_this_parameter 5968 (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) 5969 /* We've just initialized '*this' (perhaps via the target 5970 constructor of a delegating constructor). Leave it up to the 5971 caller that set 'this' to set TREE_READONLY appropriately. */ 5972 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p 5973 (TREE_TYPE (target), type)); 5974 else 5975 TREE_READONLY (*valp) = true; 5976 } 5977 5978 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing 5979 CONSTRUCTORs, if any. */ 5980 bool c = TREE_CONSTANT (init); 5981 bool s = TREE_SIDE_EFFECTS (init); 5982 if (!c || s || activated_union_member_p) 5983 for (tree elt : *ctors) 5984 { 5985 if (!c) 5986 TREE_CONSTANT (elt) = false; 5987 if (s) 5988 TREE_SIDE_EFFECTS (elt) = true; 5989 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of 5990 this union. */ 5991 if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE) 5992 CONSTRUCTOR_NO_CLEARING (elt) = false; 5993 } 5994 5995 if (*non_constant_p) 5996 return t; 5997 else if (lval) 5998 return target; 5999 else 6000 return init; 6001 } 6002 6003 /* Evaluate a ++ or -- expression. */ 6004 6005 static tree 6006 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, 6007 bool lval, 6008 bool *non_constant_p, bool *overflow_p) 6009 { 6010 enum tree_code code = TREE_CODE (t); 6011 tree type = TREE_TYPE (t); 6012 tree op = TREE_OPERAND (t, 0); 6013 tree offset = TREE_OPERAND (t, 1); 6014 gcc_assert (TREE_CONSTANT (offset)); 6015 6016 /* OFFSET is constant, but perhaps not constant enough. We need to 6017 e.g. bash FLOAT_EXPRs to REAL_CSTs. */ 6018 offset = fold_simple (offset); 6019 6020 /* The operand as an lvalue. */ 6021 op = cxx_eval_constant_expression (ctx, op, true, 6022 non_constant_p, overflow_p); 6023 6024 /* The operand as an rvalue. */ 6025 tree val 6026 = cxx_eval_constant_expression (ctx, op, false, 6027 non_constant_p, overflow_p); 6028 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to 6029 a local array in a constexpr function. */ 6030 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); 6031 if (!ptr) 6032 VERIFY_CONSTANT (val); 6033 6034 /* The modified value. */ 6035 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); 6036 tree mod; 6037 if (INDIRECT_TYPE_P (type)) 6038 { 6039 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ 6040 offset = convert_to_ptrofftype (offset); 6041 if (!inc) 6042 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); 6043 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); 6044 } 6045 else if (c_promoting_integer_type_p (type) 6046 && !TYPE_UNSIGNED (type) 6047 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6048 { 6049 offset = fold_convert (integer_type_node, offset); 6050 mod = fold_convert (integer_type_node, val); 6051 tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node, 6052 mod, offset); 6053 mod = fold_convert (type, t); 6054 if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t)) 6055 TREE_OVERFLOW (mod) = false; 6056 } 6057 else 6058 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); 6059 if (!ptr) 6060 VERIFY_CONSTANT (mod); 6061 6062 /* Storing the modified value. */ 6063 tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), 6064 MODIFY_EXPR, type, op, mod); 6065 mod = cxx_eval_constant_expression (ctx, store, lval, 6066 non_constant_p, overflow_p); 6067 ggc_free (store); 6068 if (*non_constant_p) 6069 return t; 6070 6071 /* And the value of the expression. */ 6072 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) 6073 /* Prefix ops are lvalues, but the caller might want an rvalue; 6074 lval has already been taken into account in the store above. */ 6075 return mod; 6076 else 6077 /* Postfix ops are rvalues. */ 6078 return val; 6079 } 6080 6081 /* Predicates for the meaning of *jump_target. */ 6082 6083 static bool 6084 returns (tree *jump_target) 6085 { 6086 return *jump_target 6087 && (TREE_CODE (*jump_target) == RETURN_EXPR 6088 || (TREE_CODE (*jump_target) == LABEL_DECL 6089 && LABEL_DECL_CDTOR (*jump_target))); 6090 } 6091 6092 static bool 6093 breaks (tree *jump_target) 6094 { 6095 return *jump_target 6096 && ((TREE_CODE (*jump_target) == LABEL_DECL 6097 && LABEL_DECL_BREAK (*jump_target)) 6098 || TREE_CODE (*jump_target) == BREAK_STMT 6099 || TREE_CODE (*jump_target) == EXIT_EXPR); 6100 } 6101 6102 static bool 6103 continues (tree *jump_target) 6104 { 6105 return *jump_target 6106 && ((TREE_CODE (*jump_target) == LABEL_DECL 6107 && LABEL_DECL_CONTINUE (*jump_target)) 6108 || TREE_CODE (*jump_target) == CONTINUE_STMT); 6109 6110 } 6111 6112 static bool 6113 switches (tree *jump_target) 6114 { 6115 return *jump_target 6116 && TREE_CODE (*jump_target) == INTEGER_CST; 6117 } 6118 6119 /* Subroutine of cxx_eval_statement_list. Determine whether the statement 6120 STMT matches *jump_target. If we're looking for a case label and we see 6121 the default label, note it in ctx->css_state. */ 6122 6123 static bool 6124 label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) 6125 { 6126 switch (TREE_CODE (*jump_target)) 6127 { 6128 case LABEL_DECL: 6129 if (TREE_CODE (stmt) == LABEL_EXPR 6130 && LABEL_EXPR_LABEL (stmt) == *jump_target) 6131 return true; 6132 break; 6133 6134 case INTEGER_CST: 6135 if (TREE_CODE (stmt) == CASE_LABEL_EXPR) 6136 { 6137 gcc_assert (ctx->css_state != NULL); 6138 if (!CASE_LOW (stmt)) 6139 { 6140 /* default: should appear just once in a SWITCH_EXPR 6141 body (excluding nested SWITCH_EXPR). */ 6142 gcc_assert (*ctx->css_state != css_default_seen); 6143 /* When evaluating SWITCH_EXPR body for the second time, 6144 return true for the default: label. */ 6145 if (*ctx->css_state == css_default_processing) 6146 return true; 6147 *ctx->css_state = css_default_seen; 6148 } 6149 else if (CASE_HIGH (stmt)) 6150 { 6151 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) 6152 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) 6153 return true; 6154 } 6155 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) 6156 return true; 6157 } 6158 break; 6159 6160 case BREAK_STMT: 6161 case CONTINUE_STMT: 6162 /* These two are handled directly in cxx_eval_loop_expr by testing 6163 breaks (jump_target) or continues (jump_target). */ 6164 break; 6165 6166 default: 6167 gcc_unreachable (); 6168 } 6169 return false; 6170 } 6171 6172 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump 6173 semantics, for switch, break, continue, and return. */ 6174 6175 static tree 6176 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, 6177 bool *non_constant_p, bool *overflow_p, 6178 tree *jump_target) 6179 { 6180 tree local_target; 6181 /* In a statement-expression we want to return the last value. 6182 For empty statement expression return void_node. */ 6183 tree r = void_node; 6184 if (!jump_target) 6185 { 6186 local_target = NULL_TREE; 6187 jump_target = &local_target; 6188 } 6189 for (tree stmt : tsi_range (t)) 6190 { 6191 /* We've found a continue, so skip everything until we reach 6192 the label its jumping to. */ 6193 if (continues (jump_target)) 6194 { 6195 if (label_matches (ctx, jump_target, stmt)) 6196 /* Found it. */ 6197 *jump_target = NULL_TREE; 6198 else 6199 continue; 6200 } 6201 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) 6202 continue; 6203 r = cxx_eval_constant_expression (ctx, stmt, false, 6204 non_constant_p, overflow_p, 6205 jump_target); 6206 if (*non_constant_p) 6207 break; 6208 if (returns (jump_target) || breaks (jump_target)) 6209 break; 6210 } 6211 if (*jump_target && jump_target == &local_target) 6212 { 6213 /* We aren't communicating the jump to our caller, so give up. We don't 6214 need to support evaluation of jumps out of statement-exprs. */ 6215 if (!ctx->quiet) 6216 error_at (cp_expr_loc_or_input_loc (r), 6217 "statement is not a constant expression"); 6218 *non_constant_p = true; 6219 } 6220 return r; 6221 } 6222 6223 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return 6224 semantics; continue semantics are covered by cxx_eval_statement_list. */ 6225 6226 static tree 6227 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, 6228 bool *non_constant_p, bool *overflow_p, 6229 tree *jump_target) 6230 { 6231 constexpr_ctx new_ctx = *ctx; 6232 tree local_target; 6233 if (!jump_target) 6234 { 6235 local_target = NULL_TREE; 6236 jump_target = &local_target; 6237 } 6238 6239 tree body, cond = NULL_TREE, expr = NULL_TREE; 6240 int count = 0; 6241 switch (TREE_CODE (t)) 6242 { 6243 case LOOP_EXPR: 6244 body = LOOP_EXPR_BODY (t); 6245 break; 6246 case DO_STMT: 6247 body = DO_BODY (t); 6248 cond = DO_COND (t); 6249 break; 6250 case WHILE_STMT: 6251 body = WHILE_BODY (t); 6252 cond = WHILE_COND (t); 6253 count = -1; 6254 break; 6255 case FOR_STMT: 6256 if (FOR_INIT_STMT (t)) 6257 cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false, 6258 non_constant_p, overflow_p, jump_target); 6259 if (*non_constant_p) 6260 return NULL_TREE; 6261 body = FOR_BODY (t); 6262 cond = FOR_COND (t); 6263 expr = FOR_EXPR (t); 6264 count = -1; 6265 break; 6266 default: 6267 gcc_unreachable (); 6268 } 6269 auto_vec<tree, 10> save_exprs; 6270 new_ctx.save_exprs = &save_exprs; 6271 do 6272 { 6273 if (count != -1) 6274 { 6275 if (body) 6276 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false, 6277 non_constant_p, overflow_p, 6278 jump_target); 6279 if (breaks (jump_target)) 6280 { 6281 *jump_target = NULL_TREE; 6282 break; 6283 } 6284 6285 if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) 6286 *jump_target = NULL_TREE; 6287 6288 if (expr) 6289 cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false, 6290 non_constant_p, overflow_p, 6291 jump_target); 6292 } 6293 6294 if (cond) 6295 { 6296 tree res 6297 = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false, 6298 non_constant_p, overflow_p, 6299 jump_target); 6300 if (res) 6301 { 6302 if (verify_constant (res, ctx->quiet, non_constant_p, 6303 overflow_p)) 6304 break; 6305 if (integer_zerop (res)) 6306 break; 6307 } 6308 else 6309 gcc_assert (*jump_target); 6310 } 6311 6312 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ 6313 for (tree save_expr : save_exprs) 6314 ctx->global->values.remove (save_expr); 6315 save_exprs.truncate (0); 6316 6317 if (++count >= constexpr_loop_limit) 6318 { 6319 if (!ctx->quiet) 6320 error_at (cp_expr_loc_or_input_loc (t), 6321 "%<constexpr%> loop iteration count exceeds limit of %d " 6322 "(use %<-fconstexpr-loop-limit=%> to increase the limit)", 6323 constexpr_loop_limit); 6324 *non_constant_p = true; 6325 break; 6326 } 6327 } 6328 while (!returns (jump_target) 6329 && !breaks (jump_target) 6330 && !continues (jump_target) 6331 && (!switches (jump_target) || count == 0) 6332 && !*non_constant_p); 6333 6334 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ 6335 for (tree save_expr : save_exprs) 6336 ctx->global->values.remove (save_expr); 6337 6338 return NULL_TREE; 6339 } 6340 6341 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump 6342 semantics. */ 6343 6344 static tree 6345 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, 6346 bool *non_constant_p, bool *overflow_p, 6347 tree *jump_target) 6348 { 6349 tree cond 6350 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); 6351 cond = cxx_eval_constant_expression (ctx, cond, false, 6352 non_constant_p, overflow_p); 6353 VERIFY_CONSTANT (cond); 6354 *jump_target = cond; 6355 6356 tree body 6357 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); 6358 constexpr_ctx new_ctx = *ctx; 6359 constexpr_switch_state css = css_default_not_seen; 6360 new_ctx.css_state = &css; 6361 cxx_eval_constant_expression (&new_ctx, body, false, 6362 non_constant_p, overflow_p, jump_target); 6363 if (switches (jump_target) && css == css_default_seen) 6364 { 6365 /* If the SWITCH_EXPR body has default: label, process it once again, 6366 this time instructing label_matches to return true for default: 6367 label on switches (jump_target). */ 6368 css = css_default_processing; 6369 cxx_eval_constant_expression (&new_ctx, body, false, 6370 non_constant_p, overflow_p, jump_target); 6371 } 6372 if (breaks (jump_target) || switches (jump_target)) 6373 *jump_target = NULL_TREE; 6374 return NULL_TREE; 6375 } 6376 6377 /* Find the object of TYPE under initialization in CTX. */ 6378 6379 static tree 6380 lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) 6381 { 6382 if (!ctx) 6383 return NULL_TREE; 6384 6385 /* Prefer the outermost matching object, but don't cross 6386 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ 6387 if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) 6388 if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) 6389 return outer_ob; 6390 6391 /* We could use ctx->object unconditionally, but using ctx->ctor when we 6392 can is a minor optimization. */ 6393 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) 6394 return ctx->ctor; 6395 6396 if (!ctx->object) 6397 return NULL_TREE; 6398 6399 /* Since an object cannot have a field of its own type, we can search outward 6400 from ctx->object to find the unique containing object of TYPE. */ 6401 tree ob = ctx->object; 6402 while (ob) 6403 { 6404 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) 6405 break; 6406 if (handled_component_p (ob)) 6407 ob = TREE_OPERAND (ob, 0); 6408 else 6409 ob = NULL_TREE; 6410 } 6411 6412 return ob; 6413 } 6414 6415 /* Complain about an attempt to evaluate inline assembly. */ 6416 6417 static void 6418 inline_asm_in_constexpr_error (location_t loc) 6419 { 6420 auto_diagnostic_group d; 6421 error_at (loc, "inline assembly is not a constant expression"); 6422 inform (loc, "only unevaluated inline assembly is allowed in a " 6423 "%<constexpr%> function in C++20"); 6424 } 6425 6426 /* We're getting the constant value of DECL in a manifestly constant-evaluated 6427 context; maybe complain about that. */ 6428 6429 static void 6430 maybe_warn_about_constant_value (location_t loc, tree decl) 6431 { 6432 static bool explained = false; 6433 if (cxx_dialect >= cxx17 6434 && warn_interference_size 6435 && !OPTION_SET_P (param_destruct_interfere_size) 6436 && DECL_CONTEXT (decl) == std_node 6437 && DECL_NAME (decl) 6438 && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") 6439 && (LOCATION_FILE (input_location) != main_input_filename 6440 || module_exporting_p ()) 6441 && warning_at (loc, OPT_Winterference_size, "use of %qD", decl) 6442 && !explained) 6443 { 6444 explained = true; 6445 inform (loc, "its value can vary between compiler versions or " 6446 "with different %<-mtune%> or %<-mcpu%> flags"); 6447 inform (loc, "if this use is part of a public ABI, change it to " 6448 "instead use a constant variable you define"); 6449 inform (loc, "the default value for the current CPU tuning " 6450 "is %d bytes", param_destruct_interfere_size); 6451 inform (loc, "you can stabilize this value with %<--param " 6452 "hardware_destructive_interference_size=%d%>, or disable " 6453 "this warning with %<-Wno-interference-size%>", 6454 param_destruct_interfere_size); 6455 } 6456 } 6457 6458 /* For element type ELT_TYPE, return the appropriate type of the heap object 6459 containing such element(s). COOKIE_SIZE is NULL or the size of cookie 6460 in bytes. If COOKIE_SIZE is NULL, return array type 6461 ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return 6462 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; } 6463 where N is is computed such that the size of the struct fits into FULL_SIZE. 6464 If ARG_SIZE is non-NULL, it is the first argument to the new operator. 6465 It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE 6466 will be also 0 and so it is not possible to determine the actual array 6467 size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant 6468 expression evaluation of subexpressions of ARG_SIZE. */ 6469 6470 static tree 6471 build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type, 6472 tree cookie_size, tree full_size, tree arg_size, 6473 bool *non_constant_p, bool *overflow_p) 6474 { 6475 gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size)); 6476 gcc_assert (tree_fits_uhwi_p (full_size)); 6477 unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0; 6478 if (arg_size) 6479 { 6480 STRIP_NOPS (arg_size); 6481 if (cookie_size) 6482 { 6483 if (TREE_CODE (arg_size) != PLUS_EXPR) 6484 arg_size = NULL_TREE; 6485 else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST 6486 && tree_int_cst_equal (cookie_size, 6487 TREE_OPERAND (arg_size, 0))) 6488 { 6489 arg_size = TREE_OPERAND (arg_size, 1); 6490 STRIP_NOPS (arg_size); 6491 } 6492 else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST 6493 && tree_int_cst_equal (cookie_size, 6494 TREE_OPERAND (arg_size, 1))) 6495 { 6496 arg_size = TREE_OPERAND (arg_size, 0); 6497 STRIP_NOPS (arg_size); 6498 } 6499 else 6500 arg_size = NULL_TREE; 6501 } 6502 if (arg_size && TREE_CODE (arg_size) == MULT_EXPR) 6503 { 6504 tree op0 = TREE_OPERAND (arg_size, 0); 6505 tree op1 = TREE_OPERAND (arg_size, 1); 6506 if (integer_zerop (op0)) 6507 arg_size 6508 = cxx_eval_constant_expression (ctx, op1, false, non_constant_p, 6509 overflow_p); 6510 else if (integer_zerop (op1)) 6511 arg_size 6512 = cxx_eval_constant_expression (ctx, op0, false, non_constant_p, 6513 overflow_p); 6514 else 6515 arg_size = NULL_TREE; 6516 } 6517 else 6518 arg_size = NULL_TREE; 6519 } 6520 6521 unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size); 6522 if (!arg_size) 6523 { 6524 unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type); 6525 gcc_assert (fsz >= csz); 6526 fsz -= csz; 6527 if (esz) 6528 fsz /= esz; 6529 } 6530 tree itype2 = build_index_type (size_int (fsz - 1)); 6531 if (!cookie_size) 6532 return build_cplus_array_type (elt_type, itype2); 6533 return build_new_constexpr_heap_type (elt_type, cookie_size, itype2); 6534 } 6535 6536 /* Attempt to reduce the expression T to a constant value. 6537 On failure, issue diagnostic and return error_mark_node. */ 6538 /* FIXME unify with c_fully_fold */ 6539 /* FIXME overflow_p is too global */ 6540 6541 static tree 6542 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, 6543 bool lval, 6544 bool *non_constant_p, bool *overflow_p, 6545 tree *jump_target /* = NULL */) 6546 { 6547 if (jump_target && *jump_target) 6548 { 6549 /* If we are jumping, ignore all statements/expressions except those 6550 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ 6551 switch (TREE_CODE (t)) 6552 { 6553 case BIND_EXPR: 6554 case STATEMENT_LIST: 6555 case LOOP_EXPR: 6556 case COND_EXPR: 6557 case IF_STMT: 6558 case DO_STMT: 6559 case WHILE_STMT: 6560 case FOR_STMT: 6561 break; 6562 case LABEL_EXPR: 6563 case CASE_LABEL_EXPR: 6564 if (label_matches (ctx, jump_target, t)) 6565 /* Found it. */ 6566 *jump_target = NULL_TREE; 6567 return NULL_TREE; 6568 default: 6569 return NULL_TREE; 6570 } 6571 } 6572 if (error_operand_p (t)) 6573 { 6574 *non_constant_p = true; 6575 return t; 6576 } 6577 6578 location_t loc = cp_expr_loc_or_input_loc (t); 6579 6580 STRIP_ANY_LOCATION_WRAPPER (t); 6581 6582 if (CONSTANT_CLASS_P (t)) 6583 { 6584 if (TREE_OVERFLOW (t)) 6585 { 6586 if (!ctx->quiet) 6587 permerror (input_location, "overflow in constant expression"); 6588 if (!flag_permissive || ctx->quiet) 6589 *overflow_p = true; 6590 } 6591 6592 if (TREE_CODE (t) == INTEGER_CST 6593 && TYPE_PTR_P (TREE_TYPE (t)) 6594 /* INTEGER_CST with pointer-to-method type is only used 6595 for a virtual method in a pointer to member function. 6596 Don't reject those. */ 6597 && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE 6598 && !integer_zerop (t)) 6599 { 6600 if (!ctx->quiet) 6601 error ("value %qE of type %qT is not a constant expression", 6602 t, TREE_TYPE (t)); 6603 *non_constant_p = true; 6604 } 6605 6606 return t; 6607 } 6608 6609 /* Avoid excessively long constexpr evaluations. */ 6610 if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) 6611 { 6612 if (!ctx->quiet) 6613 error_at (loc, 6614 "%<constexpr%> evaluation operation count exceeds limit of " 6615 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", 6616 constexpr_ops_limit); 6617 ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); 6618 *non_constant_p = true; 6619 return t; 6620 } 6621 6622 constexpr_ctx new_ctx; 6623 tree r = t; 6624 6625 tree_code tcode = TREE_CODE (t); 6626 switch (tcode) 6627 { 6628 case RESULT_DECL: 6629 if (lval) 6630 return t; 6631 /* We ask for an rvalue for the RESULT_DECL when indirecting 6632 through an invisible reference, or in named return value 6633 optimization. */ 6634 if (tree *p = ctx->global->values.get (t)) 6635 return *p; 6636 else 6637 { 6638 if (!ctx->quiet) 6639 error ("%qE is not a constant expression", t); 6640 *non_constant_p = true; 6641 } 6642 break; 6643 6644 case VAR_DECL: 6645 if (DECL_HAS_VALUE_EXPR_P (t)) 6646 { 6647 if (is_normal_capture_proxy (t) 6648 && current_function_decl == DECL_CONTEXT (t)) 6649 { 6650 /* Function parms aren't constexpr within the function 6651 definition, so don't try to look at the closure. But if the 6652 captured variable is constant, try to evaluate it directly. */ 6653 r = DECL_CAPTURED_VARIABLE (t); 6654 tree type = TREE_TYPE (t); 6655 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) 6656 { 6657 /* Adjust r to match the reference-ness of t. */ 6658 if (TYPE_REF_P (type)) 6659 r = build_address (r); 6660 else 6661 r = convert_from_reference (r); 6662 } 6663 } 6664 else 6665 r = DECL_VALUE_EXPR (t); 6666 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, 6667 overflow_p); 6668 } 6669 /* fall through */ 6670 case CONST_DECL: 6671 /* We used to not check lval for CONST_DECL, but darwin.cc uses 6672 CONST_DECL for aggregate constants. */ 6673 if (lval) 6674 return t; 6675 else if (t == ctx->object) 6676 return ctx->ctor; 6677 if (VAR_P (t)) 6678 if (tree *p = ctx->global->values.get (t)) 6679 if (*p != NULL_TREE) 6680 { 6681 r = *p; 6682 break; 6683 } 6684 if (ctx->manifestly_const_eval) 6685 maybe_warn_about_constant_value (loc, t); 6686 if (COMPLETE_TYPE_P (TREE_TYPE (t)) 6687 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 6688 { 6689 /* If the class is empty, we aren't actually loading anything. */ 6690 r = build_constructor (TREE_TYPE (t), NULL); 6691 TREE_CONSTANT (r) = true; 6692 } 6693 else if (ctx->strict) 6694 r = decl_really_constant_value (t, /*unshare_p=*/false); 6695 else 6696 r = decl_constant_value (t, /*unshare_p=*/false); 6697 if (TREE_CODE (r) == TARGET_EXPR 6698 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) 6699 r = TARGET_EXPR_INITIAL (r); 6700 if (DECL_P (r)) 6701 { 6702 if (!ctx->quiet) 6703 non_const_var_error (loc, r); 6704 *non_constant_p = true; 6705 } 6706 break; 6707 6708 case DEBUG_BEGIN_STMT: 6709 /* ??? It might be nice to retain this information somehow, so 6710 as to be able to step into a constexpr function call. */ 6711 /* Fall through. */ 6712 6713 case FUNCTION_DECL: 6714 case TEMPLATE_DECL: 6715 case LABEL_DECL: 6716 case LABEL_EXPR: 6717 case CASE_LABEL_EXPR: 6718 case PREDICT_EXPR: 6719 return t; 6720 6721 case PARM_DECL: 6722 if (lval && !TYPE_REF_P (TREE_TYPE (t))) 6723 /* glvalue use. */; 6724 else if (tree *p = ctx->global->values.get (r)) 6725 r = *p; 6726 else if (lval) 6727 /* Defer in case this is only used for its type. */; 6728 else if (COMPLETE_TYPE_P (TREE_TYPE (t)) 6729 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 6730 { 6731 /* If the class is empty, we aren't actually loading anything. */ 6732 r = build_constructor (TREE_TYPE (t), NULL); 6733 TREE_CONSTANT (r) = true; 6734 } 6735 else 6736 { 6737 if (!ctx->quiet) 6738 error ("%qE is not a constant expression", t); 6739 *non_constant_p = true; 6740 } 6741 break; 6742 6743 case CALL_EXPR: 6744 case AGGR_INIT_EXPR: 6745 r = cxx_eval_call_expression (ctx, t, lval, 6746 non_constant_p, overflow_p); 6747 break; 6748 6749 case DECL_EXPR: 6750 { 6751 r = DECL_EXPR_DECL (t); 6752 if (TREE_CODE (r) == USING_DECL) 6753 { 6754 r = void_node; 6755 break; 6756 } 6757 6758 if (VAR_P (r) 6759 && (TREE_STATIC (r) 6760 || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r))) 6761 /* Allow __FUNCTION__ etc. */ 6762 && !DECL_ARTIFICIAL (r)) 6763 { 6764 if (!ctx->quiet) 6765 { 6766 if (CP_DECL_THREAD_LOCAL_P (r)) 6767 error_at (loc, "control passes through definition of %qD " 6768 "with thread storage duration", r); 6769 else 6770 error_at (loc, "control passes through definition of %qD " 6771 "with static storage duration", r); 6772 } 6773 *non_constant_p = true; 6774 break; 6775 } 6776 6777 if (AGGREGATE_TYPE_P (TREE_TYPE (r)) 6778 || VECTOR_TYPE_P (TREE_TYPE (r))) 6779 { 6780 new_ctx = *ctx; 6781 new_ctx.object = r; 6782 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); 6783 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; 6784 ctx->global->values.put (r, new_ctx.ctor); 6785 ctx = &new_ctx; 6786 } 6787 6788 if (tree init = DECL_INITIAL (r)) 6789 { 6790 init = cxx_eval_constant_expression (ctx, init, 6791 false, 6792 non_constant_p, overflow_p); 6793 /* Don't share a CONSTRUCTOR that might be changed. */ 6794 init = unshare_constructor (init); 6795 /* Remember that a constant object's constructor has already 6796 run. */ 6797 if (CLASS_TYPE_P (TREE_TYPE (r)) 6798 && CP_TYPE_CONST_P (TREE_TYPE (r))) 6799 TREE_READONLY (init) = true; 6800 ctx->global->values.put (r, init); 6801 } 6802 else if (ctx == &new_ctx) 6803 /* We gave it a CONSTRUCTOR above. */; 6804 else 6805 ctx->global->values.put (r, NULL_TREE); 6806 } 6807 break; 6808 6809 case TARGET_EXPR: 6810 { 6811 tree type = TREE_TYPE (t); 6812 6813 if (!literal_type_p (type)) 6814 { 6815 if (!ctx->quiet) 6816 { 6817 auto_diagnostic_group d; 6818 error ("temporary of non-literal type %qT in a " 6819 "constant expression", type); 6820 explain_non_literal_class (type); 6821 } 6822 *non_constant_p = true; 6823 break; 6824 } 6825 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); 6826 /* Avoid evaluating a TARGET_EXPR more than once. */ 6827 tree slot = TARGET_EXPR_SLOT (t); 6828 if (tree *p = ctx->global->values.get (slot)) 6829 { 6830 if (lval) 6831 return slot; 6832 r = *p; 6833 break; 6834 } 6835 if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) 6836 { 6837 /* We're being expanded without an explicit target, so start 6838 initializing a new object; expansion with an explicit target 6839 strips the TARGET_EXPR before we get here. */ 6840 new_ctx = *ctx; 6841 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve 6842 any PLACEHOLDER_EXPR within the initializer that refers to the 6843 former object under construction. */ 6844 new_ctx.parent = ctx; 6845 new_ctx.ctor = build_constructor (type, NULL); 6846 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; 6847 new_ctx.object = slot; 6848 ctx->global->values.put (new_ctx.object, new_ctx.ctor); 6849 ctx = &new_ctx; 6850 } 6851 /* Pass false for 'lval' because this indicates 6852 initialization of a temporary. */ 6853 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 6854 false, 6855 non_constant_p, overflow_p); 6856 if (*non_constant_p) 6857 break; 6858 /* If the initializer is complex, evaluate it to initialize slot. */ 6859 bool is_complex = target_expr_needs_replace (t); 6860 if (!is_complex) 6861 { 6862 r = unshare_constructor (r); 6863 /* Adjust the type of the result to the type of the temporary. */ 6864 r = adjust_temp_type (type, r); 6865 ctx->global->values.put (slot, r); 6866 } 6867 if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) 6868 ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); 6869 if (ctx->save_exprs) 6870 ctx->save_exprs->safe_push (slot); 6871 if (lval) 6872 return slot; 6873 if (is_complex) 6874 r = *ctx->global->values.get (slot); 6875 } 6876 break; 6877 6878 case INIT_EXPR: 6879 case MODIFY_EXPR: 6880 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); 6881 r = cxx_eval_store_expression (ctx, t, lval, 6882 non_constant_p, overflow_p); 6883 break; 6884 6885 case SCOPE_REF: 6886 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 6887 lval, 6888 non_constant_p, overflow_p); 6889 break; 6890 6891 case RETURN_EXPR: 6892 if (TREE_OPERAND (t, 0) != NULL_TREE) 6893 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 6894 lval, 6895 non_constant_p, overflow_p); 6896 /* FALLTHRU */ 6897 case BREAK_STMT: 6898 case CONTINUE_STMT: 6899 if (jump_target) 6900 *jump_target = t; 6901 else 6902 { 6903 /* Can happen with ({ return true; }) && false; passed to 6904 maybe_constant_value. There is nothing to jump over in this 6905 case, and the bug will be diagnosed later. */ 6906 gcc_assert (ctx->quiet); 6907 *non_constant_p = true; 6908 } 6909 break; 6910 6911 case SAVE_EXPR: 6912 /* Avoid evaluating a SAVE_EXPR more than once. */ 6913 if (tree *p = ctx->global->values.get (t)) 6914 r = *p; 6915 else 6916 { 6917 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, 6918 non_constant_p, overflow_p); 6919 if (*non_constant_p) 6920 break; 6921 ctx->global->values.put (t, r); 6922 if (ctx->save_exprs) 6923 ctx->save_exprs->safe_push (t); 6924 } 6925 break; 6926 6927 case TRY_CATCH_EXPR: 6928 if (TREE_OPERAND (t, 0) == NULL_TREE) 6929 { 6930 r = void_node; 6931 break; 6932 } 6933 /* FALLTHRU */ 6934 case NON_LVALUE_EXPR: 6935 case TRY_BLOCK: 6936 case MUST_NOT_THROW_EXPR: 6937 case EXPR_STMT: 6938 case EH_SPEC_BLOCK: 6939 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 6940 lval, 6941 non_constant_p, overflow_p, 6942 jump_target); 6943 break; 6944 6945 case CLEANUP_POINT_EXPR: 6946 { 6947 auto_vec<tree, 2> cleanups; 6948 vec<tree> *prev_cleanups = ctx->global->cleanups; 6949 ctx->global->cleanups = &cleanups; 6950 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 6951 lval, 6952 non_constant_p, overflow_p, 6953 jump_target); 6954 ctx->global->cleanups = prev_cleanups; 6955 unsigned int i; 6956 tree cleanup; 6957 /* Evaluate the cleanups. */ 6958 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) 6959 cxx_eval_constant_expression (ctx, cleanup, false, 6960 non_constant_p, overflow_p); 6961 } 6962 break; 6963 6964 case TRY_FINALLY_EXPR: 6965 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, 6966 non_constant_p, overflow_p, 6967 jump_target); 6968 if (!*non_constant_p) 6969 /* Also evaluate the cleanup. */ 6970 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true, 6971 non_constant_p, overflow_p); 6972 break; 6973 6974 case CLEANUP_STMT: 6975 r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, 6976 non_constant_p, overflow_p, 6977 jump_target); 6978 if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) 6979 { 6980 iloc_sentinel ils (loc); 6981 /* Also evaluate the cleanup. */ 6982 cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true, 6983 non_constant_p, overflow_p); 6984 } 6985 break; 6986 6987 /* These differ from cxx_eval_unary_expression in that this doesn't 6988 check for a constant operand or result; an address can be 6989 constant without its operand being, and vice versa. */ 6990 case MEM_REF: 6991 case INDIRECT_REF: 6992 r = cxx_eval_indirect_ref (ctx, t, lval, 6993 non_constant_p, overflow_p); 6994 break; 6995 6996 case ADDR_EXPR: 6997 { 6998 tree oldop = TREE_OPERAND (t, 0); 6999 tree op = cxx_eval_constant_expression (ctx, oldop, 7000 /*lval*/true, 7001 non_constant_p, overflow_p); 7002 /* Don't VERIFY_CONSTANT here. */ 7003 if (*non_constant_p) 7004 return t; 7005 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); 7006 /* This function does more aggressive folding than fold itself. */ 7007 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); 7008 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) 7009 { 7010 ggc_free (r); 7011 return t; 7012 } 7013 break; 7014 } 7015 7016 case REALPART_EXPR: 7017 case IMAGPART_EXPR: 7018 if (lval) 7019 { 7020 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, 7021 non_constant_p, overflow_p); 7022 if (r == error_mark_node) 7023 ; 7024 else if (r == TREE_OPERAND (t, 0)) 7025 r = t; 7026 else 7027 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); 7028 break; 7029 } 7030 /* FALLTHRU */ 7031 case CONJ_EXPR: 7032 case FIX_TRUNC_EXPR: 7033 case FLOAT_EXPR: 7034 case NEGATE_EXPR: 7035 case ABS_EXPR: 7036 case ABSU_EXPR: 7037 case BIT_NOT_EXPR: 7038 case TRUTH_NOT_EXPR: 7039 case FIXED_CONVERT_EXPR: 7040 r = cxx_eval_unary_expression (ctx, t, lval, 7041 non_constant_p, overflow_p); 7042 break; 7043 7044 case SIZEOF_EXPR: 7045 r = fold_sizeof_expr (t); 7046 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, 7047 which could lead to an infinite recursion. */ 7048 if (TREE_CODE (r) != SIZEOF_EXPR) 7049 r = cxx_eval_constant_expression (ctx, r, lval, 7050 non_constant_p, overflow_p, 7051 jump_target); 7052 else 7053 { 7054 *non_constant_p = true; 7055 gcc_assert (ctx->quiet); 7056 } 7057 7058 break; 7059 7060 case COMPOUND_EXPR: 7061 { 7062 /* check_return_expr sometimes wraps a TARGET_EXPR in a 7063 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR 7064 introduced by build_call_a. */ 7065 tree op0 = TREE_OPERAND (t, 0); 7066 tree op1 = TREE_OPERAND (t, 1); 7067 STRIP_NOPS (op1); 7068 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) 7069 || TREE_CODE (op1) == EMPTY_CLASS_EXPR) 7070 r = cxx_eval_constant_expression (ctx, op0, 7071 lval, non_constant_p, overflow_p, 7072 jump_target); 7073 else 7074 { 7075 /* Check that the LHS is constant and then discard it. */ 7076 cxx_eval_constant_expression (ctx, op0, 7077 true, non_constant_p, overflow_p, 7078 jump_target); 7079 if (*non_constant_p) 7080 return t; 7081 op1 = TREE_OPERAND (t, 1); 7082 r = cxx_eval_constant_expression (ctx, op1, 7083 lval, non_constant_p, overflow_p, 7084 jump_target); 7085 } 7086 } 7087 break; 7088 7089 case POINTER_PLUS_EXPR: 7090 case POINTER_DIFF_EXPR: 7091 case PLUS_EXPR: 7092 case MINUS_EXPR: 7093 case MULT_EXPR: 7094 case TRUNC_DIV_EXPR: 7095 case CEIL_DIV_EXPR: 7096 case FLOOR_DIV_EXPR: 7097 case ROUND_DIV_EXPR: 7098 case TRUNC_MOD_EXPR: 7099 case CEIL_MOD_EXPR: 7100 case ROUND_MOD_EXPR: 7101 case RDIV_EXPR: 7102 case EXACT_DIV_EXPR: 7103 case MIN_EXPR: 7104 case MAX_EXPR: 7105 case LSHIFT_EXPR: 7106 case RSHIFT_EXPR: 7107 case LROTATE_EXPR: 7108 case RROTATE_EXPR: 7109 case BIT_IOR_EXPR: 7110 case BIT_XOR_EXPR: 7111 case BIT_AND_EXPR: 7112 case TRUTH_XOR_EXPR: 7113 case LT_EXPR: 7114 case LE_EXPR: 7115 case GT_EXPR: 7116 case GE_EXPR: 7117 case EQ_EXPR: 7118 case NE_EXPR: 7119 case SPACESHIP_EXPR: 7120 case UNORDERED_EXPR: 7121 case ORDERED_EXPR: 7122 case UNLT_EXPR: 7123 case UNLE_EXPR: 7124 case UNGT_EXPR: 7125 case UNGE_EXPR: 7126 case UNEQ_EXPR: 7127 case LTGT_EXPR: 7128 case RANGE_EXPR: 7129 case COMPLEX_EXPR: 7130 r = cxx_eval_binary_expression (ctx, t, lval, 7131 non_constant_p, overflow_p); 7132 break; 7133 7134 /* fold can introduce non-IF versions of these; still treat them as 7135 short-circuiting. */ 7136 case TRUTH_AND_EXPR: 7137 case TRUTH_ANDIF_EXPR: 7138 r = cxx_eval_logical_expression (ctx, t, boolean_false_node, 7139 boolean_true_node, 7140 non_constant_p, overflow_p); 7141 break; 7142 7143 case TRUTH_OR_EXPR: 7144 case TRUTH_ORIF_EXPR: 7145 r = cxx_eval_logical_expression (ctx, t, boolean_true_node, 7146 boolean_false_node, 7147 non_constant_p, overflow_p); 7148 break; 7149 7150 case ARRAY_REF: 7151 r = cxx_eval_array_reference (ctx, t, lval, 7152 non_constant_p, overflow_p); 7153 break; 7154 7155 case COMPONENT_REF: 7156 if (is_overloaded_fn (t)) 7157 { 7158 /* We can only get here in checking mode via 7159 build_non_dependent_expr, because any expression that 7160 calls or takes the address of the function will have 7161 pulled a FUNCTION_DECL out of the COMPONENT_REF. */ 7162 gcc_checking_assert (ctx->quiet || errorcount); 7163 *non_constant_p = true; 7164 return t; 7165 } 7166 r = cxx_eval_component_reference (ctx, t, lval, 7167 non_constant_p, overflow_p); 7168 break; 7169 7170 case BIT_FIELD_REF: 7171 r = cxx_eval_bit_field_ref (ctx, t, lval, 7172 non_constant_p, overflow_p); 7173 break; 7174 7175 case COND_EXPR: 7176 case IF_STMT: 7177 if (jump_target && *jump_target) 7178 { 7179 tree orig_jump = *jump_target; 7180 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) 7181 ? TREE_OPERAND (t, 1) : void_node); 7182 /* When jumping to a label, the label might be either in the 7183 then or else blocks, so process then block first in skipping 7184 mode first, and if we are still in the skipping mode at its end, 7185 process the else block too. */ 7186 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, 7187 overflow_p, jump_target); 7188 /* It's possible that we found the label in the then block. But 7189 it could have been followed by another jumping statement, e.g. 7190 say we're looking for case 1: 7191 if (cond) 7192 { 7193 // skipped statements 7194 case 1:; // clears up *jump_target 7195 return 1; // and sets it to a RETURN_EXPR 7196 } 7197 else { ... } 7198 in which case we need not go looking to the else block. 7199 (goto is not allowed in a constexpr function.) */ 7200 if (*jump_target == orig_jump) 7201 { 7202 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) 7203 ? TREE_OPERAND (t, 2) : void_node); 7204 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, 7205 overflow_p, jump_target); 7206 } 7207 break; 7208 } 7209 r = cxx_eval_conditional_expression (ctx, t, lval, 7210 non_constant_p, overflow_p, 7211 jump_target); 7212 break; 7213 case VEC_COND_EXPR: 7214 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, 7215 overflow_p); 7216 break; 7217 7218 case CONSTRUCTOR: 7219 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) 7220 { 7221 /* Don't re-process a constant CONSTRUCTOR, but do fold it to 7222 VECTOR_CST if applicable. */ 7223 verify_constructor_flags (t); 7224 if (TREE_CONSTANT (t)) 7225 return fold (t); 7226 } 7227 r = cxx_eval_bare_aggregate (ctx, t, lval, 7228 non_constant_p, overflow_p); 7229 break; 7230 7231 case VEC_INIT_EXPR: 7232 /* We can get this in a defaulted constructor for a class with a 7233 non-static data member of array type. Either the initializer will 7234 be NULL, meaning default-initialization, or it will be an lvalue 7235 or xvalue of the same type, meaning direct-initialization from the 7236 corresponding member. */ 7237 r = cxx_eval_vec_init (ctx, t, lval, 7238 non_constant_p, overflow_p); 7239 break; 7240 7241 case VEC_PERM_EXPR: 7242 r = cxx_eval_trinary_expression (ctx, t, lval, 7243 non_constant_p, overflow_p); 7244 break; 7245 7246 case PAREN_EXPR: 7247 gcc_assert (!REF_PARENTHESIZED_P (t)); 7248 /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in 7249 constant expressions since it's unaffected by -fassociative-math. */ 7250 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, 7251 non_constant_p, overflow_p); 7252 break; 7253 7254 case NOP_EXPR: 7255 if (REINTERPRET_CAST_P (t)) 7256 { 7257 if (!ctx->quiet) 7258 error_at (loc, 7259 "%<reinterpret_cast%> is not a constant expression"); 7260 *non_constant_p = true; 7261 return t; 7262 } 7263 /* FALLTHROUGH. */ 7264 case CONVERT_EXPR: 7265 case VIEW_CONVERT_EXPR: 7266 case UNARY_PLUS_EXPR: 7267 { 7268 tree oldop = TREE_OPERAND (t, 0); 7269 7270 tree op = cxx_eval_constant_expression (ctx, oldop, 7271 lval, 7272 non_constant_p, overflow_p); 7273 if (*non_constant_p) 7274 return t; 7275 tree type = TREE_TYPE (t); 7276 7277 if (VOID_TYPE_P (type)) 7278 return void_node; 7279 7280 if (TREE_CODE (t) == CONVERT_EXPR 7281 && ARITHMETIC_TYPE_P (type) 7282 && INDIRECT_TYPE_P (TREE_TYPE (op)) 7283 && ctx->manifestly_const_eval) 7284 { 7285 if (!ctx->quiet) 7286 error_at (loc, 7287 "conversion from pointer type %qT to arithmetic type " 7288 "%qT in a constant expression", TREE_TYPE (op), type); 7289 *non_constant_p = true; 7290 return t; 7291 } 7292 7293 /* [expr.const]: a conversion from type cv void* to a pointer-to-object 7294 type cannot be part of a core constant expression as a resolution to 7295 DR 1312. */ 7296 if (TYPE_PTROB_P (type) 7297 && TYPE_PTR_P (TREE_TYPE (op)) 7298 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) 7299 /* Inside a call to std::construct_at or to 7300 std::allocator<T>::{,de}allocate, we permit casting from void* 7301 because that is compiler-generated code. */ 7302 && !is_std_construct_at (ctx->call) 7303 && !is_std_allocator_allocate (ctx->call)) 7304 { 7305 /* Likewise, don't error when casting from void* when OP is 7306 &heap uninit and similar. */ 7307 tree sop = tree_strip_nop_conversions (op); 7308 if (TREE_CODE (sop) == ADDR_EXPR 7309 && VAR_P (TREE_OPERAND (sop, 0)) 7310 && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0))) 7311 /* OK */; 7312 else 7313 { 7314 if (!ctx->quiet) 7315 error_at (loc, "cast from %qT is not allowed", 7316 TREE_TYPE (op)); 7317 *non_constant_p = true; 7318 return t; 7319 } 7320 } 7321 7322 if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) 7323 { 7324 op = cplus_expand_constant (op); 7325 if (TREE_CODE (op) == PTRMEM_CST) 7326 { 7327 if (!ctx->quiet) 7328 error_at (loc, "%qE is not a constant expression when the " 7329 "class %qT is still incomplete", op, 7330 PTRMEM_CST_CLASS (op)); 7331 *non_constant_p = true; 7332 return t; 7333 } 7334 } 7335 7336 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) 7337 { 7338 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) 7339 && !can_convert_qual (type, op)) 7340 op = cplus_expand_constant (op); 7341 return cp_fold_convert (type, op); 7342 } 7343 7344 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) 7345 { 7346 if (integer_zerop (op)) 7347 { 7348 if (TYPE_REF_P (type)) 7349 { 7350 if (!ctx->quiet) 7351 error_at (loc, "dereferencing a null pointer"); 7352 *non_constant_p = true; 7353 return t; 7354 } 7355 } 7356 else if (TYPE_PTR_P (type) 7357 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE) 7358 /* INTEGER_CST with pointer-to-method type is only used 7359 for a virtual method in a pointer to member function. 7360 Don't reject those. */ 7361 ; 7362 else 7363 { 7364 /* This detects for example: 7365 reinterpret_cast<void*>(sizeof 0) 7366 */ 7367 if (!ctx->quiet) 7368 error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " 7369 "a constant expression", 7370 type, op); 7371 *non_constant_p = true; 7372 return t; 7373 } 7374 } 7375 7376 if (INDIRECT_TYPE_P (type) 7377 && TREE_CODE (op) == NOP_EXPR 7378 && TREE_TYPE (op) == ptr_type_node 7379 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR 7380 && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) 7381 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), 7382 0)) == heap_uninit_identifier 7383 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), 7384 0)) == heap_vec_uninit_identifier)) 7385 { 7386 tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); 7387 tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); 7388 tree elt_type = TREE_TYPE (type); 7389 tree cookie_size = NULL_TREE; 7390 tree arg_size = NULL_TREE; 7391 if (TREE_CODE (elt_type) == RECORD_TYPE 7392 && TYPE_NAME (elt_type) == heap_identifier) 7393 { 7394 tree fld1 = TYPE_FIELDS (elt_type); 7395 tree fld2 = DECL_CHAIN (fld1); 7396 elt_type = TREE_TYPE (TREE_TYPE (fld2)); 7397 cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); 7398 } 7399 DECL_NAME (var) 7400 = (DECL_NAME (var) == heap_uninit_identifier 7401 ? heap_identifier : heap_vec_identifier); 7402 /* For zero sized elt_type, try to recover how many outer_nelts 7403 it should have. */ 7404 if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size) 7405 : integer_zerop (var_size)) 7406 && !int_size_in_bytes (elt_type) 7407 && TREE_CODE (oldop) == CALL_EXPR 7408 && call_expr_nargs (oldop) >= 1) 7409 if (tree fun = get_function_named_in_call (oldop)) 7410 if (cxx_replaceable_global_alloc_fn (fun) 7411 && IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) 7412 arg_size = CALL_EXPR_ARG (oldop, 0); 7413 TREE_TYPE (var) 7414 = build_new_constexpr_heap_type (ctx, elt_type, cookie_size, 7415 var_size, arg_size, 7416 non_constant_p, overflow_p); 7417 TREE_TYPE (TREE_OPERAND (op, 0)) 7418 = build_pointer_type (TREE_TYPE (var)); 7419 } 7420 7421 if (op == oldop && tcode != UNARY_PLUS_EXPR) 7422 /* We didn't fold at the top so we could check for ptr-int 7423 conversion. */ 7424 return fold (t); 7425 7426 tree sop; 7427 7428 /* Handle an array's bounds having been deduced after we built 7429 the wrapping expression. */ 7430 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) 7431 r = op; 7432 else if (sop = tree_strip_nop_conversions (op), 7433 sop != op && (same_type_ignoring_tlq_and_bounds_p 7434 (type, TREE_TYPE (sop)))) 7435 r = sop; 7436 else if (tcode == UNARY_PLUS_EXPR) 7437 r = fold_convert (TREE_TYPE (t), op); 7438 else 7439 r = fold_build1 (tcode, type, op); 7440 7441 /* Conversion of an out-of-range value has implementation-defined 7442 behavior; the language considers it different from arithmetic 7443 overflow, which is undefined. */ 7444 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) 7445 TREE_OVERFLOW (r) = false; 7446 } 7447 break; 7448 7449 case EMPTY_CLASS_EXPR: 7450 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering 7451 it to an appropriate CONSTRUCTOR. */ 7452 return build_constructor (TREE_TYPE (t), NULL); 7453 7454 case STATEMENT_LIST: 7455 new_ctx = *ctx; 7456 new_ctx.ctor = new_ctx.object = NULL_TREE; 7457 return cxx_eval_statement_list (&new_ctx, t, 7458 non_constant_p, overflow_p, jump_target); 7459 7460 case BIND_EXPR: 7461 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), 7462 lval, 7463 non_constant_p, overflow_p, 7464 jump_target); 7465 7466 case PREINCREMENT_EXPR: 7467 case POSTINCREMENT_EXPR: 7468 case PREDECREMENT_EXPR: 7469 case POSTDECREMENT_EXPR: 7470 return cxx_eval_increment_expression (ctx, t, 7471 lval, non_constant_p, overflow_p); 7472 7473 case LAMBDA_EXPR: 7474 case NEW_EXPR: 7475 case VEC_NEW_EXPR: 7476 case DELETE_EXPR: 7477 case VEC_DELETE_EXPR: 7478 case THROW_EXPR: 7479 case MODOP_EXPR: 7480 /* GCC internal stuff. */ 7481 case VA_ARG_EXPR: 7482 case NON_DEPENDENT_EXPR: 7483 case BASELINK: 7484 case OFFSET_REF: 7485 if (!ctx->quiet) 7486 error_at (loc, "expression %qE is not a constant expression", t); 7487 *non_constant_p = true; 7488 break; 7489 7490 case OBJ_TYPE_REF: 7491 /* Virtual function lookup. We don't need to do anything fancy. */ 7492 return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), 7493 lval, non_constant_p, overflow_p); 7494 7495 case PLACEHOLDER_EXPR: 7496 /* Use of the value or address of the current object. */ 7497 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) 7498 { 7499 if (TREE_CODE (ctor) == CONSTRUCTOR) 7500 return ctor; 7501 else 7502 return cxx_eval_constant_expression (ctx, ctor, lval, 7503 non_constant_p, overflow_p); 7504 } 7505 /* A placeholder without a referent. We can get here when 7506 checking whether NSDMIs are noexcept, or in massage_init_elt; 7507 just say it's non-constant for now. */ 7508 gcc_assert (ctx->quiet); 7509 *non_constant_p = true; 7510 break; 7511 7512 case EXIT_EXPR: 7513 { 7514 tree cond = TREE_OPERAND (t, 0); 7515 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false, 7516 non_constant_p, overflow_p); 7517 VERIFY_CONSTANT (cond); 7518 if (integer_nonzerop (cond)) 7519 *jump_target = t; 7520 } 7521 break; 7522 7523 case GOTO_EXPR: 7524 if (breaks (&TREE_OPERAND (t, 0)) 7525 || continues (&TREE_OPERAND (t, 0)) 7526 /* Allow for jumping to a cdtor_label. */ 7527 || returns (&TREE_OPERAND (t, 0))) 7528 *jump_target = TREE_OPERAND (t, 0); 7529 else 7530 { 7531 gcc_assert (cxx_dialect >= cxx23); 7532 if (!ctx->quiet) 7533 error_at (loc, "%<goto%> is not a constant expression"); 7534 *non_constant_p = true; 7535 } 7536 break; 7537 7538 case LOOP_EXPR: 7539 case DO_STMT: 7540 case WHILE_STMT: 7541 case FOR_STMT: 7542 cxx_eval_loop_expr (ctx, t, 7543 non_constant_p, overflow_p, jump_target); 7544 break; 7545 7546 case SWITCH_EXPR: 7547 case SWITCH_STMT: 7548 cxx_eval_switch_expr (ctx, t, 7549 non_constant_p, overflow_p, jump_target); 7550 break; 7551 7552 case REQUIRES_EXPR: 7553 /* It's possible to get a requires-expression in a constant 7554 expression. For example: 7555 7556 template<typename T> concept bool C() { 7557 return requires (T t) { t; }; 7558 } 7559 7560 template<typename T> requires !C<T>() void f(T); 7561 7562 Normalization leaves f with the associated constraint 7563 '!requires (T t) { ... }' which is not transformed into 7564 a constraint. */ 7565 if (!processing_template_decl) 7566 return evaluate_requires_expr (t); 7567 else 7568 *non_constant_p = true; 7569 return t; 7570 7571 case ANNOTATE_EXPR: 7572 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 7573 lval, 7574 non_constant_p, overflow_p, 7575 jump_target); 7576 break; 7577 7578 case USING_STMT: 7579 r = void_node; 7580 break; 7581 7582 case TEMPLATE_ID_EXPR: 7583 { 7584 /* We can evaluate template-id that refers to a concept only if 7585 the template arguments are non-dependent. */ 7586 tree id = unpack_concept_check (t); 7587 tree tmpl = TREE_OPERAND (id, 0); 7588 if (!concept_definition_p (tmpl)) 7589 internal_error ("unexpected template-id %qE", t); 7590 7591 if (function_concept_p (tmpl)) 7592 { 7593 if (!ctx->quiet) 7594 error_at (cp_expr_loc_or_input_loc (t), 7595 "function concept must be called"); 7596 r = error_mark_node; 7597 break; 7598 } 7599 7600 if (!value_dependent_expression_p (t) 7601 && !uid_sensitive_constexpr_evaluation_p ()) 7602 r = evaluate_concept_check (t); 7603 else 7604 *non_constant_p = true; 7605 7606 break; 7607 } 7608 7609 case ASM_EXPR: 7610 if (!ctx->quiet) 7611 inline_asm_in_constexpr_error (loc); 7612 *non_constant_p = true; 7613 return t; 7614 7615 case BIT_CAST_EXPR: 7616 if (lval) 7617 { 7618 if (!ctx->quiet) 7619 error_at (EXPR_LOCATION (t), 7620 "address of a call to %qs is not a constant expression", 7621 "__builtin_bit_cast"); 7622 *non_constant_p = true; 7623 return t; 7624 } 7625 r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); 7626 break; 7627 7628 case OMP_PARALLEL: 7629 case OMP_TASK: 7630 case OMP_FOR: 7631 case OMP_SIMD: 7632 case OMP_DISTRIBUTE: 7633 case OMP_TASKLOOP: 7634 case OMP_LOOP: 7635 case OMP_TEAMS: 7636 case OMP_TARGET_DATA: 7637 case OMP_TARGET: 7638 case OMP_SECTIONS: 7639 case OMP_ORDERED: 7640 case OMP_CRITICAL: 7641 case OMP_SINGLE: 7642 case OMP_SCAN: 7643 case OMP_SCOPE: 7644 case OMP_SECTION: 7645 case OMP_MASTER: 7646 case OMP_MASKED: 7647 case OMP_TASKGROUP: 7648 case OMP_TARGET_UPDATE: 7649 case OMP_TARGET_ENTER_DATA: 7650 case OMP_TARGET_EXIT_DATA: 7651 case OMP_ATOMIC: 7652 case OMP_ATOMIC_READ: 7653 case OMP_ATOMIC_CAPTURE_OLD: 7654 case OMP_ATOMIC_CAPTURE_NEW: 7655 case OMP_DEPOBJ: 7656 case OACC_PARALLEL: 7657 case OACC_KERNELS: 7658 case OACC_SERIAL: 7659 case OACC_DATA: 7660 case OACC_HOST_DATA: 7661 case OACC_LOOP: 7662 case OACC_CACHE: 7663 case OACC_DECLARE: 7664 case OACC_ENTER_DATA: 7665 case OACC_EXIT_DATA: 7666 case OACC_UPDATE: 7667 if (!ctx->quiet) 7668 error_at (EXPR_LOCATION (t), 7669 "statement is not a constant expression"); 7670 *non_constant_p = true; 7671 break; 7672 7673 default: 7674 if (STATEMENT_CODE_P (TREE_CODE (t))) 7675 { 7676 /* This function doesn't know how to deal with pre-genericize 7677 statements; this can only happen with statement-expressions, 7678 so for now just fail. */ 7679 if (!ctx->quiet) 7680 error_at (EXPR_LOCATION (t), 7681 "statement is not a constant expression"); 7682 } 7683 else 7684 internal_error ("unexpected expression %qE of kind %s", t, 7685 get_tree_code_name (TREE_CODE (t))); 7686 *non_constant_p = true; 7687 break; 7688 } 7689 7690 if (r == error_mark_node) 7691 *non_constant_p = true; 7692 7693 if (*non_constant_p) 7694 return t; 7695 else 7696 return r; 7697 } 7698 7699 /* P0859: A function is needed for constant evaluation if it is a constexpr 7700 function that is named by an expression ([basic.def.odr]) that is 7701 potentially constant evaluated. 7702 7703 So we need to instantiate any constexpr functions mentioned by the 7704 expression even if the definition isn't needed for evaluating the 7705 expression. */ 7706 7707 static tree 7708 instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) 7709 { 7710 if (TREE_CODE (*tp) == FUNCTION_DECL 7711 && DECL_DECLARED_CONSTEXPR_P (*tp) 7712 && !DECL_INITIAL (*tp) 7713 && !trivial_fn_p (*tp) 7714 && DECL_TEMPLOID_INSTANTIATION (*tp) 7715 && !uid_sensitive_constexpr_evaluation_p ()) 7716 { 7717 ++function_depth; 7718 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); 7719 --function_depth; 7720 } 7721 else if (TREE_CODE (*tp) == CALL_EXPR 7722 || TREE_CODE (*tp) == AGGR_INIT_EXPR) 7723 { 7724 if (EXPR_HAS_LOCATION (*tp)) 7725 input_location = EXPR_LOCATION (*tp); 7726 } 7727 7728 if (!EXPR_P (*tp)) 7729 *walk_subtrees = 0; 7730 7731 return NULL_TREE; 7732 } 7733 7734 static void 7735 instantiate_constexpr_fns (tree t) 7736 { 7737 location_t loc = input_location; 7738 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); 7739 input_location = loc; 7740 } 7741 7742 /* Look for heap variables in the expression *TP. */ 7743 7744 static tree 7745 find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) 7746 { 7747 if (VAR_P (*tp) 7748 && (DECL_NAME (*tp) == heap_uninit_identifier 7749 || DECL_NAME (*tp) == heap_identifier 7750 || DECL_NAME (*tp) == heap_vec_uninit_identifier 7751 || DECL_NAME (*tp) == heap_vec_identifier 7752 || DECL_NAME (*tp) == heap_deleted_identifier)) 7753 return *tp; 7754 7755 if (TYPE_P (*tp)) 7756 *walk_subtrees = 0; 7757 return NULL_TREE; 7758 } 7759 7760 /* Find immediate function decls in *TP if any. */ 7761 7762 static tree 7763 find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) 7764 { 7765 if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) 7766 return *tp; 7767 if (TREE_CODE (*tp) == PTRMEM_CST 7768 && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL 7769 && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) 7770 return PTRMEM_CST_MEMBER (*tp); 7771 return NULL_TREE; 7772 } 7773 7774 /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant 7775 expression. Return a version of T that has TREE_CONSTANT cleared. */ 7776 7777 static tree 7778 mark_non_constant (tree t) 7779 { 7780 gcc_checking_assert (TREE_CONSTANT (t)); 7781 7782 /* This isn't actually constant, so unset TREE_CONSTANT. 7783 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires 7784 it to be set if it is invariant address, even when it is not 7785 a valid C++ constant expression. Wrap it with a NOP_EXPR 7786 instead. */ 7787 if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR) 7788 t = copy_node (t); 7789 else if (TREE_CODE (t) == CONSTRUCTOR) 7790 t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t); 7791 else 7792 t = build_nop (TREE_TYPE (t), t); 7793 TREE_CONSTANT (t) = false; 7794 return t; 7795 } 7796 7797 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. 7798 STRICT has the same sense as for constant_value_1: true if we only allow 7799 conforming C++ constant expressions, or false if we want a constant value 7800 even if it doesn't conform. 7801 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as 7802 per P0595 even when ALLOW_NON_CONSTANT is true. 7803 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. 7804 OBJECT must be non-NULL in that case. */ 7805 7806 static tree 7807 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, 7808 bool strict = true, 7809 bool manifestly_const_eval = false, 7810 bool constexpr_dtor = false, 7811 tree object = NULL_TREE) 7812 { 7813 auto_timevar time (TV_CONSTEXPR); 7814 7815 bool non_constant_p = false; 7816 bool overflow_p = false; 7817 7818 if (BRACE_ENCLOSED_INITIALIZER_P (t)) 7819 { 7820 gcc_checking_assert (allow_non_constant); 7821 return t; 7822 } 7823 7824 constexpr_global_ctx global_ctx; 7825 constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, 7826 allow_non_constant, strict, 7827 manifestly_const_eval || !allow_non_constant }; 7828 7829 /* Turn off -frounding-math for manifestly constant evaluation. */ 7830 warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval); 7831 tree type = initialized_type (t); 7832 tree r = t; 7833 bool is_consteval = false; 7834 if (VOID_TYPE_P (type)) 7835 { 7836 if (constexpr_dtor) 7837 /* Used for destructors of array elements. */ 7838 type = TREE_TYPE (object); 7839 else 7840 { 7841 if (cxx_dialect < cxx20) 7842 return t; 7843 if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) 7844 return t; 7845 /* Calls to immediate functions returning void need to be 7846 evaluated. */ 7847 tree fndecl = cp_get_callee_fndecl_nofold (t); 7848 if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) 7849 return t; 7850 else 7851 is_consteval = true; 7852 } 7853 } 7854 else if (cxx_dialect >= cxx20 7855 && (TREE_CODE (t) == CALL_EXPR 7856 || TREE_CODE (t) == AGGR_INIT_EXPR 7857 || TREE_CODE (t) == TARGET_EXPR)) 7858 { 7859 /* For non-concept checks, determine if it is consteval. */ 7860 if (!concept_check_p (t)) 7861 { 7862 tree x = t; 7863 if (TREE_CODE (x) == TARGET_EXPR) 7864 x = TARGET_EXPR_INITIAL (x); 7865 tree fndecl = cp_get_callee_fndecl_nofold (x); 7866 if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) 7867 is_consteval = true; 7868 } 7869 } 7870 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) 7871 { 7872 /* In C++14 an NSDMI can participate in aggregate initialization, 7873 and can refer to the address of the object being initialized, so 7874 we need to pass in the relevant VAR_DECL if we want to do the 7875 evaluation in a single pass. The evaluation will dynamically 7876 update ctx.values for the VAR_DECL. We use the same strategy 7877 for C++11 constexpr constructors that refer to the object being 7878 initialized. */ 7879 if (constexpr_dtor) 7880 { 7881 gcc_assert (object && VAR_P (object)); 7882 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); 7883 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); 7884 if (error_operand_p (DECL_INITIAL (object))) 7885 return t; 7886 ctx.ctor = unshare_expr (DECL_INITIAL (object)); 7887 TREE_READONLY (ctx.ctor) = false; 7888 /* Temporarily force decl_really_constant_value to return false 7889 for it, we want to use ctx.ctor for the current value instead. */ 7890 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; 7891 } 7892 else 7893 { 7894 ctx.ctor = build_constructor (type, NULL); 7895 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; 7896 } 7897 if (!object) 7898 { 7899 if (TREE_CODE (t) == TARGET_EXPR) 7900 object = TARGET_EXPR_SLOT (t); 7901 else if (TREE_CODE (t) == AGGR_INIT_EXPR) 7902 object = AGGR_INIT_EXPR_SLOT (t); 7903 } 7904 ctx.object = object; 7905 if (object) 7906 gcc_assert (same_type_ignoring_top_level_qualifiers_p 7907 (type, TREE_TYPE (object))); 7908 if (object && DECL_P (object)) 7909 global_ctx.values.put (object, ctx.ctor); 7910 if (TREE_CODE (r) == TARGET_EXPR) 7911 /* Avoid creating another CONSTRUCTOR when we expand the 7912 TARGET_EXPR. */ 7913 r = TARGET_EXPR_INITIAL (r); 7914 } 7915 7916 auto_vec<tree, 16> cleanups; 7917 global_ctx.cleanups = &cleanups; 7918 7919 if (manifestly_const_eval) 7920 instantiate_constexpr_fns (r); 7921 r = cxx_eval_constant_expression (&ctx, r, 7922 false, &non_constant_p, &overflow_p); 7923 7924 if (!constexpr_dtor) 7925 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); 7926 else 7927 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; 7928 7929 unsigned int i; 7930 tree cleanup; 7931 /* Evaluate the cleanups. */ 7932 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) 7933 cxx_eval_constant_expression (&ctx, cleanup, false, 7934 &non_constant_p, &overflow_p); 7935 7936 /* Mutable logic is a bit tricky: we want to allow initialization of 7937 constexpr variables with mutable members, but we can't copy those 7938 members to another constexpr variable. */ 7939 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) 7940 { 7941 if (!allow_non_constant) 7942 error ("%qE is not a constant expression because it refers to " 7943 "mutable subobjects of %qT", t, type); 7944 non_constant_p = true; 7945 } 7946 7947 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) 7948 { 7949 if (!allow_non_constant) 7950 error ("%qE is not a constant expression because it refers to " 7951 "an incompletely initialized variable", t); 7952 TREE_CONSTANT (r) = false; 7953 non_constant_p = true; 7954 } 7955 7956 if (!global_ctx.heap_vars.is_empty ()) 7957 { 7958 tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, 7959 NULL); 7960 unsigned int i; 7961 if (heap_var) 7962 { 7963 if (!allow_non_constant && !non_constant_p) 7964 error_at (DECL_SOURCE_LOCATION (heap_var), 7965 "%qE is not a constant expression because it refers to " 7966 "a result of %<operator new%>", t); 7967 r = t; 7968 non_constant_p = true; 7969 } 7970 FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) 7971 { 7972 if (DECL_NAME (heap_var) != heap_deleted_identifier) 7973 { 7974 if (!allow_non_constant && !non_constant_p) 7975 error_at (DECL_SOURCE_LOCATION (heap_var), 7976 "%qE is not a constant expression because allocated " 7977 "storage has not been deallocated", t); 7978 r = t; 7979 non_constant_p = true; 7980 } 7981 varpool_node::get (heap_var)->remove (); 7982 } 7983 } 7984 7985 /* Check that immediate invocation does not return an expression referencing 7986 any immediate function decls. */ 7987 if (is_consteval || in_immediate_context ()) 7988 if (tree immediate_fndecl 7989 = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, 7990 NULL)) 7991 { 7992 if (!allow_non_constant && !non_constant_p) 7993 error_at (cp_expr_loc_or_input_loc (t), 7994 "immediate evaluation returns address of immediate " 7995 "function %qD", immediate_fndecl); 7996 r = t; 7997 non_constant_p = true; 7998 } 7999 8000 if (non_constant_p) 8001 /* If we saw something bad, go back to our argument. The wrapping below is 8002 only for the cases of TREE_CONSTANT argument or overflow. */ 8003 r = t; 8004 8005 if (!non_constant_p && overflow_p) 8006 non_constant_p = true; 8007 8008 /* Unshare the result. */ 8009 bool should_unshare = true; 8010 if (r == t || (TREE_CODE (t) == TARGET_EXPR 8011 && TARGET_EXPR_INITIAL (t) == r)) 8012 should_unshare = false; 8013 8014 if (non_constant_p && !allow_non_constant) 8015 return error_mark_node; 8016 else if (constexpr_dtor) 8017 return r; 8018 else if (non_constant_p && TREE_CONSTANT (r)) 8019 r = mark_non_constant (r); 8020 else if (non_constant_p) 8021 return t; 8022 8023 if (should_unshare) 8024 r = unshare_expr (r); 8025 8026 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) 8027 { 8028 r = adjust_temp_type (type, r); 8029 if (TREE_CODE (t) == TARGET_EXPR 8030 && TARGET_EXPR_INITIAL (t) == r) 8031 return t; 8032 else if (TREE_CODE (t) == CONSTRUCTOR) 8033 ; 8034 else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t)) 8035 r = get_target_expr (r); 8036 else 8037 { 8038 r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); 8039 TREE_CONSTANT (r) = true; 8040 } 8041 } 8042 8043 /* Remember the original location if that wouldn't need a wrapper. */ 8044 if (location_t loc = EXPR_LOCATION (t)) 8045 protected_set_expr_location (r, loc); 8046 8047 return r; 8048 } 8049 8050 /* If T represents a constant expression returns its reduced value. 8051 Otherwise return error_mark_node. If T is dependent, then 8052 return NULL. */ 8053 8054 tree 8055 cxx_constant_value (tree t, tree decl) 8056 { 8057 return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl); 8058 } 8059 8060 /* As above, but respect SFINAE. */ 8061 8062 tree 8063 cxx_constant_value_sfinae (tree t, tree decl, tsubst_flags_t complain) 8064 { 8065 bool sfinae = !(complain & tf_error); 8066 tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true, false, decl); 8067 if (sfinae && !TREE_CONSTANT (r)) 8068 r = error_mark_node; 8069 return r; 8070 } 8071 8072 /* Like cxx_constant_value, but used for evaluation of constexpr destructors 8073 of constexpr variables. The actual initializer of DECL is not modified. */ 8074 8075 void 8076 cxx_constant_dtor (tree t, tree decl) 8077 { 8078 cxx_eval_outermost_constant_expr (t, false, true, true, true, decl); 8079 } 8080 8081 /* Helper routine for fold_simple function. Either return simplified 8082 expression T, otherwise NULL_TREE. 8083 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold 8084 even if we are within template-declaration. So be careful on call, as in 8085 such case types can be undefined. */ 8086 8087 static tree 8088 fold_simple_1 (tree t) 8089 { 8090 tree op1; 8091 enum tree_code code = TREE_CODE (t); 8092 8093 switch (code) 8094 { 8095 case INTEGER_CST: 8096 case REAL_CST: 8097 case VECTOR_CST: 8098 case FIXED_CST: 8099 case COMPLEX_CST: 8100 return t; 8101 8102 case SIZEOF_EXPR: 8103 return fold_sizeof_expr (t); 8104 8105 case ABS_EXPR: 8106 case ABSU_EXPR: 8107 case CONJ_EXPR: 8108 case REALPART_EXPR: 8109 case IMAGPART_EXPR: 8110 case NEGATE_EXPR: 8111 case BIT_NOT_EXPR: 8112 case TRUTH_NOT_EXPR: 8113 case NOP_EXPR: 8114 case VIEW_CONVERT_EXPR: 8115 case CONVERT_EXPR: 8116 case FLOAT_EXPR: 8117 case FIX_TRUNC_EXPR: 8118 case FIXED_CONVERT_EXPR: 8119 case ADDR_SPACE_CONVERT_EXPR: 8120 8121 op1 = TREE_OPERAND (t, 0); 8122 8123 t = const_unop (code, TREE_TYPE (t), op1); 8124 if (!t) 8125 return NULL_TREE; 8126 8127 if (CONVERT_EXPR_CODE_P (code) 8128 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) 8129 TREE_OVERFLOW (t) = false; 8130 return t; 8131 8132 default: 8133 return NULL_TREE; 8134 } 8135 } 8136 8137 /* If T is a simple constant expression, returns its simplified value. 8138 Otherwise returns T. In contrast to maybe_constant_value we 8139 simplify only few operations on constant-expressions, and we don't 8140 try to simplify constexpressions. */ 8141 8142 tree 8143 fold_simple (tree t) 8144 { 8145 if (processing_template_decl) 8146 return t; 8147 8148 tree r = fold_simple_1 (t); 8149 if (r) 8150 return r; 8151 8152 return t; 8153 } 8154 8155 /* Try folding the expression T to a simple constant. 8156 Returns that constant, otherwise returns T. */ 8157 8158 tree 8159 fold_to_constant (tree t) 8160 { 8161 tree r = fold (t); 8162 if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r)) 8163 return r; 8164 else 8165 return t; 8166 } 8167 8168 /* If T is a constant expression, returns its reduced value. 8169 Otherwise, if T does not have TREE_CONSTANT set, returns T. 8170 Otherwise, returns a version of T without TREE_CONSTANT. 8171 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated 8172 as per P0595. */ 8173 8174 static GTY((deletable)) hash_map<tree, tree> *cv_cache; 8175 8176 tree 8177 maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) 8178 { 8179 tree r; 8180 8181 if (!is_nondependent_constant_expression (t)) 8182 { 8183 if (TREE_OVERFLOW_P (t) 8184 || (!processing_template_decl && TREE_CONSTANT (t))) 8185 t = mark_non_constant (t); 8186 return t; 8187 } 8188 else if (CONSTANT_CLASS_P (t)) 8189 /* No caching or evaluation needed. */ 8190 return t; 8191 8192 if (manifestly_const_eval) 8193 return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl); 8194 8195 if (cv_cache == NULL) 8196 cv_cache = hash_map<tree, tree>::create_ggc (101); 8197 if (tree *cached = cv_cache->get (t)) 8198 { 8199 r = *cached; 8200 if (r != t) 8201 { 8202 /* Clear processing_template_decl for sake of break_out_target_exprs; 8203 entries in the cv_cache are non-templated. */ 8204 processing_template_decl_sentinel ptds; 8205 8206 r = break_out_target_exprs (r, /*clear_loc*/true); 8207 protected_set_expr_location (r, EXPR_LOCATION (t)); 8208 } 8209 return r; 8210 } 8211 8212 /* Don't constant evaluate an unevaluated non-manifestly-constant operand, 8213 but at least try folding it to a simple constant. */ 8214 if (cp_unevaluated_operand) 8215 return fold_to_constant (t); 8216 8217 uid_sensitive_constexpr_evaluation_checker c; 8218 r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl); 8219 gcc_checking_assert (r == t 8220 || CONVERT_EXPR_P (t) 8221 || TREE_CODE (t) == VIEW_CONVERT_EXPR 8222 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) 8223 || !cp_tree_equal (r, t)); 8224 if (!c.evaluation_restricted_p ()) 8225 cv_cache->put (t, r); 8226 return r; 8227 } 8228 8229 /* Dispose of the whole CV_CACHE. */ 8230 8231 static void 8232 clear_cv_cache (void) 8233 { 8234 if (cv_cache != NULL) 8235 cv_cache->empty (); 8236 } 8237 8238 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ 8239 8240 void 8241 clear_cv_and_fold_caches () 8242 { 8243 clear_cv_cache (); 8244 clear_fold_cache (); 8245 } 8246 8247 /* Internal function handling expressions in templates for 8248 fold_non_dependent_expr and fold_non_dependent_init. 8249 8250 If we're in a template, but T isn't value dependent, simplify 8251 it. We're supposed to treat: 8252 8253 template <typename T> void f(T[1 + 1]); 8254 template <typename T> void f(T[2]); 8255 8256 as two declarations of the same function, for example. */ 8257 8258 static tree 8259 fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, 8260 bool manifestly_const_eval, 8261 tree object) 8262 { 8263 gcc_assert (processing_template_decl); 8264 8265 if (is_nondependent_constant_expression (t)) 8266 { 8267 processing_template_decl_sentinel s; 8268 t = instantiate_non_dependent_expr_internal (t, complain); 8269 8270 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) 8271 { 8272 if (TREE_OVERFLOW_P (t)) 8273 { 8274 t = build_nop (TREE_TYPE (t), t); 8275 TREE_CONSTANT (t) = false; 8276 } 8277 return t; 8278 } 8279 else if (CONSTANT_CLASS_P (t)) 8280 /* No evaluation needed. */ 8281 return t; 8282 8283 /* Don't constant evaluate an unevaluated non-manifestly-constant operand, 8284 but at least try folding it to a simple constant. */ 8285 if (cp_unevaluated_operand && !manifestly_const_eval) 8286 return fold_to_constant (t); 8287 8288 tree r = cxx_eval_outermost_constant_expr (t, true, true, 8289 manifestly_const_eval, 8290 false, object); 8291 /* cp_tree_equal looks through NOPs, so allow them. */ 8292 gcc_checking_assert (r == t 8293 || CONVERT_EXPR_P (t) 8294 || TREE_CODE (t) == VIEW_CONVERT_EXPR 8295 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) 8296 || !cp_tree_equal (r, t)); 8297 return r; 8298 } 8299 else if (TREE_OVERFLOW_P (t)) 8300 { 8301 t = build_nop (TREE_TYPE (t), t); 8302 TREE_CONSTANT (t) = false; 8303 } 8304 8305 return t; 8306 } 8307 8308 /* Like maybe_constant_value but first fully instantiate the argument. 8309 8310 Note: this is equivalent to instantiate_non_dependent_expr_sfinae 8311 (t, complain) followed by maybe_constant_value but is more efficient, 8312 because it calls instantiation_dependent_expression_p and 8313 potential_constant_expression at most once. 8314 The manifestly_const_eval argument is passed to maybe_constant_value. 8315 8316 Callers should generally pass their active complain, or if they are in a 8317 non-template, diagnosing context, they can use the default of 8318 tf_warning_or_error. Callers that might be within a template context, don't 8319 have a complain parameter, and aren't going to remember the result for long 8320 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node 8321 appropriately. */ 8322 8323 tree 8324 fold_non_dependent_expr (tree t, 8325 tsubst_flags_t complain /* = tf_warning_or_error */, 8326 bool manifestly_const_eval /* = false */, 8327 tree object /* = NULL_TREE */) 8328 { 8329 if (t == NULL_TREE) 8330 return NULL_TREE; 8331 8332 if (processing_template_decl) 8333 return fold_non_dependent_expr_template (t, complain, 8334 manifestly_const_eval, object); 8335 8336 return maybe_constant_value (t, object, manifestly_const_eval); 8337 } 8338 8339 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, 8340 return the original expression. */ 8341 8342 tree 8343 maybe_fold_non_dependent_expr (tree expr, 8344 tsubst_flags_t complain/*=tf_warning_or_error*/) 8345 { 8346 tree t = fold_non_dependent_expr (expr, complain); 8347 if (t && TREE_CONSTANT (t)) 8348 return t; 8349 8350 return expr; 8351 } 8352 8353 /* Like maybe_constant_init but first fully instantiate the argument. */ 8354 8355 tree 8356 fold_non_dependent_init (tree t, 8357 tsubst_flags_t complain /*=tf_warning_or_error*/, 8358 bool manifestly_const_eval /*=false*/, 8359 tree object /* = NULL_TREE */) 8360 { 8361 if (t == NULL_TREE) 8362 return NULL_TREE; 8363 8364 if (processing_template_decl) 8365 { 8366 t = fold_non_dependent_expr_template (t, complain, 8367 manifestly_const_eval, object); 8368 /* maybe_constant_init does this stripping, so do it here too. */ 8369 if (TREE_CODE (t) == TARGET_EXPR) 8370 { 8371 tree init = TARGET_EXPR_INITIAL (t); 8372 if (TREE_CODE (init) == CONSTRUCTOR) 8373 t = init; 8374 } 8375 return t; 8376 } 8377 8378 return maybe_constant_init (t, object, manifestly_const_eval); 8379 } 8380 8381 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather 8382 than wrapped in a TARGET_EXPR. 8383 ALLOW_NON_CONSTANT is false if T is required to be a constant expression. 8384 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as 8385 per P0595 even when ALLOW_NON_CONSTANT is true. */ 8386 8387 static tree 8388 maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, 8389 bool manifestly_const_eval) 8390 { 8391 if (!t) 8392 return t; 8393 if (TREE_CODE (t) == EXPR_STMT) 8394 t = TREE_OPERAND (t, 0); 8395 if (TREE_CODE (t) == CONVERT_EXPR 8396 && VOID_TYPE_P (TREE_TYPE (t))) 8397 t = TREE_OPERAND (t, 0); 8398 if (TREE_CODE (t) == INIT_EXPR) 8399 t = TREE_OPERAND (t, 1); 8400 if (TREE_CODE (t) == TARGET_EXPR) 8401 t = TARGET_EXPR_INITIAL (t); 8402 if (!is_nondependent_static_init_expression (t)) 8403 /* Don't try to evaluate it. */; 8404 else if (CONSTANT_CLASS_P (t) && allow_non_constant) 8405 /* No evaluation needed. */; 8406 else 8407 t = cxx_eval_outermost_constant_expr (t, allow_non_constant, 8408 /*strict*/false, 8409 manifestly_const_eval, false, decl); 8410 if (TREE_CODE (t) == TARGET_EXPR) 8411 { 8412 tree init = TARGET_EXPR_INITIAL (t); 8413 if (TREE_CODE (init) == CONSTRUCTOR) 8414 t = init; 8415 } 8416 return t; 8417 } 8418 8419 /* Wrapper for maybe_constant_init_1 which permits non constants. */ 8420 8421 tree 8422 maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) 8423 { 8424 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); 8425 } 8426 8427 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ 8428 8429 tree 8430 cxx_constant_init (tree t, tree decl) 8431 { 8432 return maybe_constant_init_1 (t, decl, false, true); 8433 } 8434 8435 #if 0 8436 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ 8437 /* Return true if the object referred to by REF has automatic or thread 8438 local storage. */ 8439 8440 enum { ck_ok, ck_bad, ck_unknown }; 8441 static int 8442 check_automatic_or_tls (tree ref) 8443 { 8444 machine_mode mode; 8445 poly_int64 bitsize, bitpos; 8446 tree offset; 8447 int volatilep = 0, unsignedp = 0; 8448 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, 8449 &mode, &unsignedp, &volatilep, false); 8450 duration_kind dk; 8451 8452 /* If there isn't a decl in the middle, we don't know the linkage here, 8453 and this isn't a constant expression anyway. */ 8454 if (!DECL_P (decl)) 8455 return ck_unknown; 8456 dk = decl_storage_duration (decl); 8457 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; 8458 } 8459 #endif 8460 8461 /* Data structure for passing data from potential_constant_expression_1 8462 to check_for_return_continue via cp_walk_tree. */ 8463 struct check_for_return_continue_data { 8464 hash_set<tree> *pset; 8465 tree continue_stmt; 8466 tree break_stmt; 8467 }; 8468 8469 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, 8470 called through cp_walk_tree. Return the first RETURN_EXPR found, or note 8471 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ 8472 static tree 8473 check_for_return_continue (tree *tp, int *walk_subtrees, void *data) 8474 { 8475 tree t = *tp, s, b; 8476 check_for_return_continue_data *d = (check_for_return_continue_data *) data; 8477 switch (TREE_CODE (t)) 8478 { 8479 case RETURN_EXPR: 8480 return t; 8481 8482 case CONTINUE_STMT: 8483 if (d->continue_stmt == NULL_TREE) 8484 d->continue_stmt = t; 8485 break; 8486 8487 case BREAK_STMT: 8488 if (d->break_stmt == NULL_TREE) 8489 d->break_stmt = t; 8490 break; 8491 8492 #define RECUR(x) \ 8493 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ 8494 d->pset)) \ 8495 return r 8496 8497 /* For loops, walk subtrees manually, so that continue stmts found 8498 inside of the bodies of the loops are ignored. */ 8499 case DO_STMT: 8500 *walk_subtrees = 0; 8501 RECUR (DO_COND (t)); 8502 s = d->continue_stmt; 8503 b = d->break_stmt; 8504 RECUR (DO_BODY (t)); 8505 d->continue_stmt = s; 8506 d->break_stmt = b; 8507 break; 8508 8509 case WHILE_STMT: 8510 *walk_subtrees = 0; 8511 RECUR (WHILE_COND (t)); 8512 s = d->continue_stmt; 8513 b = d->break_stmt; 8514 RECUR (WHILE_BODY (t)); 8515 d->continue_stmt = s; 8516 d->break_stmt = b; 8517 break; 8518 8519 case FOR_STMT: 8520 *walk_subtrees = 0; 8521 RECUR (FOR_INIT_STMT (t)); 8522 RECUR (FOR_COND (t)); 8523 RECUR (FOR_EXPR (t)); 8524 s = d->continue_stmt; 8525 b = d->break_stmt; 8526 RECUR (FOR_BODY (t)); 8527 d->continue_stmt = s; 8528 d->break_stmt = b; 8529 break; 8530 8531 case RANGE_FOR_STMT: 8532 *walk_subtrees = 0; 8533 RECUR (RANGE_FOR_EXPR (t)); 8534 s = d->continue_stmt; 8535 b = d->break_stmt; 8536 RECUR (RANGE_FOR_BODY (t)); 8537 d->continue_stmt = s; 8538 d->break_stmt = b; 8539 break; 8540 8541 case SWITCH_STMT: 8542 *walk_subtrees = 0; 8543 RECUR (SWITCH_STMT_COND (t)); 8544 b = d->break_stmt; 8545 RECUR (SWITCH_STMT_BODY (t)); 8546 d->break_stmt = b; 8547 break; 8548 #undef RECUR 8549 8550 case STATEMENT_LIST: 8551 case CONSTRUCTOR: 8552 break; 8553 8554 default: 8555 if (!EXPR_P (t)) 8556 *walk_subtrees = 0; 8557 break; 8558 } 8559 8560 return NULL_TREE; 8561 } 8562 8563 /* Return true if T denotes a potentially constant expression. Issue 8564 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, 8565 an lvalue-rvalue conversion is implied. If NOW is true, we want to 8566 consider the expression in the current context, independent of constexpr 8567 substitution. 8568 8569 C++0x [expr.const] used to say 8570 8571 6 An expression is a potential constant expression if it is 8572 a constant expression where all occurrences of function 8573 parameters are replaced by arbitrary constant expressions 8574 of the appropriate type. 8575 8576 2 A conditional expression is a constant expression unless it 8577 involves one of the following as a potentially evaluated 8578 subexpression (3.2), but subexpressions of logical AND (5.14), 8579 logical OR (5.15), and conditional (5.16) operations that are 8580 not evaluated are not considered. */ 8581 8582 static bool 8583 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, 8584 tsubst_flags_t flags, tree *jump_target) 8585 { 8586 #define RECUR(T,RV) \ 8587 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target) 8588 8589 enum { any = false, rval = true }; 8590 int i; 8591 tree tmp; 8592 8593 if (t == error_mark_node) 8594 return false; 8595 if (t == NULL_TREE) 8596 return true; 8597 location_t loc = cp_expr_loc_or_input_loc (t); 8598 8599 if (*jump_target) 8600 /* If we are jumping, ignore everything. This is simpler than the 8601 cxx_eval_constant_expression handling because we only need to be 8602 conservatively correct, and we don't necessarily have a constant value 8603 available, so we don't bother with switch tracking. */ 8604 return true; 8605 8606 if (TREE_THIS_VOLATILE (t) && want_rval 8607 && !NULLPTR_TYPE_P (TREE_TYPE (t))) 8608 { 8609 if (flags & tf_error) 8610 error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " 8611 "%qE with type %qT", t, TREE_TYPE (t)); 8612 return false; 8613 } 8614 if (CONSTANT_CLASS_P (t)) 8615 return true; 8616 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) 8617 && TREE_TYPE (t) == error_mark_node) 8618 return false; 8619 8620 switch (TREE_CODE (t)) 8621 { 8622 case FUNCTION_DECL: 8623 case BASELINK: 8624 case TEMPLATE_DECL: 8625 case OVERLOAD: 8626 case TEMPLATE_ID_EXPR: 8627 case LABEL_DECL: 8628 case CASE_LABEL_EXPR: 8629 case PREDICT_EXPR: 8630 case CONST_DECL: 8631 case SIZEOF_EXPR: 8632 case ALIGNOF_EXPR: 8633 case OFFSETOF_EXPR: 8634 case NOEXCEPT_EXPR: 8635 case TEMPLATE_PARM_INDEX: 8636 case TRAIT_EXPR: 8637 case IDENTIFIER_NODE: 8638 case USERDEF_LITERAL: 8639 /* We can see a FIELD_DECL in a pointer-to-member expression. */ 8640 case FIELD_DECL: 8641 case RESULT_DECL: 8642 case USING_DECL: 8643 case USING_STMT: 8644 case PLACEHOLDER_EXPR: 8645 case REQUIRES_EXPR: 8646 case STATIC_ASSERT: 8647 case DEBUG_BEGIN_STMT: 8648 return true; 8649 8650 case RETURN_EXPR: 8651 if (!RECUR (TREE_OPERAND (t, 0), any)) 8652 return false; 8653 /* FALLTHROUGH */ 8654 8655 case BREAK_STMT: 8656 case CONTINUE_STMT: 8657 *jump_target = t; 8658 return true; 8659 8660 case PARM_DECL: 8661 if (now && want_rval) 8662 { 8663 tree type = TREE_TYPE (t); 8664 if ((processing_template_decl && !COMPLETE_TYPE_P (type)) 8665 || dependent_type_p (type) 8666 || is_really_empty_class (type, /*ignore_vptr*/false)) 8667 /* An empty class has no data to read. */ 8668 return true; 8669 if (flags & tf_error) 8670 error ("%qE is not a constant expression", t); 8671 return false; 8672 } 8673 return true; 8674 8675 case AGGR_INIT_EXPR: 8676 case CALL_EXPR: 8677 /* -- an invocation of a function other than a constexpr function 8678 or a constexpr constructor. */ 8679 { 8680 tree fun = get_function_named_in_call (t); 8681 const int nargs = call_expr_nargs (t); 8682 i = 0; 8683 8684 if (fun == NULL_TREE) 8685 { 8686 /* Reset to allow the function to continue past the end 8687 of the block below. Otherwise return early. */ 8688 bool bail = true; 8689 8690 if (TREE_CODE (t) == CALL_EXPR 8691 && CALL_EXPR_FN (t) == NULL_TREE) 8692 switch (CALL_EXPR_IFN (t)) 8693 { 8694 /* These should be ignored, they are optimized away from 8695 constexpr functions. */ 8696 case IFN_UBSAN_NULL: 8697 case IFN_UBSAN_BOUNDS: 8698 case IFN_UBSAN_VPTR: 8699 case IFN_FALLTHROUGH: 8700 return true; 8701 8702 case IFN_ADD_OVERFLOW: 8703 case IFN_SUB_OVERFLOW: 8704 case IFN_MUL_OVERFLOW: 8705 case IFN_LAUNDER: 8706 case IFN_VEC_CONVERT: 8707 bail = false; 8708 break; 8709 8710 default: 8711 break; 8712 } 8713 8714 if (bail) 8715 { 8716 /* fold_call_expr can't do anything with IFN calls. */ 8717 if (flags & tf_error) 8718 error_at (loc, "call to internal function %qE", t); 8719 return false; 8720 } 8721 } 8722 8723 if (fun && is_overloaded_fn (fun)) 8724 { 8725 if (TREE_CODE (fun) == FUNCTION_DECL) 8726 { 8727 if (builtin_valid_in_constant_expr_p (fun)) 8728 return true; 8729 if (!maybe_constexpr_fn (fun) 8730 /* Allow any built-in function; if the expansion 8731 isn't constant, we'll deal with that then. */ 8732 && !fndecl_built_in_p (fun) 8733 /* In C++20, replaceable global allocation functions 8734 are constant expressions. */ 8735 && (!cxx_replaceable_global_alloc_fn (fun) 8736 || TREE_CODE (t) != CALL_EXPR 8737 || (!CALL_FROM_NEW_OR_DELETE_P (t) 8738 && (current_function_decl == NULL_TREE 8739 || !is_std_allocator_allocate 8740 (current_function_decl)))) 8741 /* Allow placement new in std::construct_at. */ 8742 && (!cxx_placement_new_fn (fun) 8743 || TREE_CODE (t) != CALL_EXPR 8744 || current_function_decl == NULL_TREE 8745 || !is_std_construct_at (current_function_decl)) 8746 && !cxx_dynamic_cast_fn_p (fun)) 8747 { 8748 if (flags & tf_error) 8749 { 8750 error_at (loc, "call to non-%<constexpr%> function %qD", 8751 fun); 8752 explain_invalid_constexpr_fn (fun); 8753 } 8754 return false; 8755 } 8756 /* A call to a non-static member function takes the address 8757 of the object as the first argument. But in a constant 8758 expression the address will be folded away, so look 8759 through it now. */ 8760 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) 8761 && !DECL_CONSTRUCTOR_P (fun)) 8762 { 8763 tree x = get_nth_callarg (t, 0); 8764 if (is_this_parameter (x)) 8765 return true; 8766 /* Don't require an immediately constant value, as 8767 constexpr substitution might not use the value. */ 8768 bool sub_now = false; 8769 if (!potential_constant_expression_1 (x, rval, strict, 8770 sub_now, flags, 8771 jump_target)) 8772 return false; 8773 i = 1; 8774 } 8775 } 8776 else 8777 { 8778 if (!RECUR (fun, true)) 8779 return false; 8780 fun = get_first_fn (fun); 8781 } 8782 /* Skip initial arguments to base constructors. */ 8783 if (DECL_BASE_CONSTRUCTOR_P (fun)) 8784 i = num_artificial_parms_for (fun); 8785 fun = DECL_ORIGIN (fun); 8786 } 8787 else if (fun) 8788 { 8789 if (TREE_TYPE (fun) 8790 && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun))) 8791 want_rval = rval; 8792 else 8793 want_rval = any; 8794 if (RECUR (fun, want_rval)) 8795 /* Might end up being a constant function pointer. But it 8796 could also be a function object with constexpr op(), so 8797 we pass 'any' so that the underlying VAR_DECL is deemed 8798 as potentially-constant even though it wasn't declared 8799 constexpr. */; 8800 else 8801 return false; 8802 } 8803 for (; i < nargs; ++i) 8804 { 8805 tree x = get_nth_callarg (t, i); 8806 /* In a template, reference arguments haven't been converted to 8807 REFERENCE_TYPE and we might not even know if the parameter 8808 is a reference, so accept lvalue constants too. */ 8809 bool rv = processing_template_decl ? any : rval; 8810 /* Don't require an immediately constant value, as constexpr 8811 substitution might not use the value of the argument. */ 8812 bool sub_now = false; 8813 if (!potential_constant_expression_1 (x, rv, strict, 8814 sub_now, flags, jump_target)) 8815 return false; 8816 } 8817 return true; 8818 } 8819 8820 case NON_LVALUE_EXPR: 8821 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to 8822 -- an lvalue of integral type that refers to a non-volatile 8823 const variable or static data member initialized with 8824 constant expressions, or 8825 8826 -- an lvalue of literal type that refers to non-volatile 8827 object defined with constexpr, or that refers to a 8828 sub-object of such an object; */ 8829 return RECUR (TREE_OPERAND (t, 0), rval); 8830 8831 case VAR_DECL: 8832 if (DECL_HAS_VALUE_EXPR_P (t)) 8833 { 8834 if (now && is_normal_capture_proxy (t)) 8835 { 8836 /* -- in a lambda-expression, a reference to this or to a 8837 variable with automatic storage duration defined outside that 8838 lambda-expression, where the reference would be an 8839 odr-use. */ 8840 8841 if (want_rval) 8842 /* Since we're doing an lvalue-rvalue conversion, this might 8843 not be an odr-use, so evaluate the variable directly. */ 8844 return RECUR (DECL_CAPTURED_VARIABLE (t), rval); 8845 8846 if (flags & tf_error) 8847 { 8848 tree cap = DECL_CAPTURED_VARIABLE (t); 8849 error ("lambda capture of %qE is not a constant expression", 8850 cap); 8851 if (decl_constant_var_p (cap)) 8852 inform (input_location, "because it is used as a glvalue"); 8853 } 8854 return false; 8855 } 8856 /* Treat __PRETTY_FUNCTION__ inside a template function as 8857 potentially-constant. */ 8858 else if (DECL_PRETTY_FUNCTION_P (t) 8859 && DECL_VALUE_EXPR (t) == error_mark_node) 8860 return true; 8861 return RECUR (DECL_VALUE_EXPR (t), rval); 8862 } 8863 if (want_rval 8864 && (now || !var_in_maybe_constexpr_fn (t)) 8865 && !type_dependent_expression_p (t) 8866 && !decl_maybe_constant_var_p (t) 8867 && (strict 8868 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) 8869 || (DECL_INITIAL (t) 8870 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) 8871 && COMPLETE_TYPE_P (TREE_TYPE (t)) 8872 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 8873 { 8874 if (flags & tf_error) 8875 non_const_var_error (loc, t); 8876 return false; 8877 } 8878 return true; 8879 8880 case NOP_EXPR: 8881 if (REINTERPRET_CAST_P (t)) 8882 { 8883 if (flags & tf_error) 8884 error_at (loc, "%<reinterpret_cast%> is not a constant expression"); 8885 return false; 8886 } 8887 /* FALLTHRU */ 8888 case CONVERT_EXPR: 8889 case VIEW_CONVERT_EXPR: 8890 /* -- a reinterpret_cast. FIXME not implemented, and this rule 8891 may change to something more specific to type-punning (DR 1312). */ 8892 { 8893 tree from = TREE_OPERAND (t, 0); 8894 if (location_wrapper_p (t)) 8895 return (RECUR (from, want_rval)); 8896 if (INDIRECT_TYPE_P (TREE_TYPE (t))) 8897 { 8898 STRIP_ANY_LOCATION_WRAPPER (from); 8899 if (TREE_CODE (from) == INTEGER_CST 8900 && !integer_zerop (from)) 8901 { 8902 if (flags & tf_error) 8903 error_at (loc, 8904 "%<reinterpret_cast%> from integer to pointer"); 8905 return false; 8906 } 8907 } 8908 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); 8909 } 8910 8911 case ADDRESSOF_EXPR: 8912 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ 8913 t = TREE_OPERAND (t, 0); 8914 goto handle_addr_expr; 8915 8916 case ADDR_EXPR: 8917 /* -- a unary operator & that is applied to an lvalue that 8918 designates an object with thread or automatic storage 8919 duration; */ 8920 t = TREE_OPERAND (t, 0); 8921 8922 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) 8923 /* A pointer-to-member constant. */ 8924 return true; 8925 8926 handle_addr_expr: 8927 #if 0 8928 /* FIXME adjust when issue 1197 is fully resolved. For now don't do 8929 any checking here, as we might dereference the pointer later. If 8930 we remove this code, also remove check_automatic_or_tls. */ 8931 i = check_automatic_or_tls (t); 8932 if (i == ck_ok) 8933 return true; 8934 if (i == ck_bad) 8935 { 8936 if (flags & tf_error) 8937 error ("address-of an object %qE with thread local or " 8938 "automatic storage is not a constant expression", t); 8939 return false; 8940 } 8941 #endif 8942 return RECUR (t, any); 8943 8944 case COMPONENT_REF: 8945 case ARROW_EXPR: 8946 case OFFSET_REF: 8947 /* -- a class member access unless its postfix-expression is 8948 of literal type or of pointer to literal type. */ 8949 /* This test would be redundant, as it follows from the 8950 postfix-expression being a potential constant expression. */ 8951 if (type_unknown_p (t)) 8952 return true; 8953 if (is_overloaded_fn (t)) 8954 /* In a template, a COMPONENT_REF of a function expresses ob.fn(), 8955 which uses ob as an lvalue. */ 8956 want_rval = false; 8957 gcc_fallthrough (); 8958 8959 case REALPART_EXPR: 8960 case IMAGPART_EXPR: 8961 case BIT_FIELD_REF: 8962 return RECUR (TREE_OPERAND (t, 0), want_rval); 8963 8964 case EXPR_PACK_EXPANSION: 8965 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); 8966 8967 case INDIRECT_REF: 8968 { 8969 tree x = TREE_OPERAND (t, 0); 8970 STRIP_NOPS (x); 8971 if (is_this_parameter (x) && !is_capture_proxy (x)) 8972 { 8973 if (now || !var_in_maybe_constexpr_fn (x)) 8974 { 8975 if (flags & tf_error) 8976 error_at (loc, "use of %<this%> in a constant expression"); 8977 return false; 8978 } 8979 return true; 8980 } 8981 return RECUR (x, rval); 8982 } 8983 8984 case STATEMENT_LIST: 8985 for (tree stmt : tsi_range (t)) 8986 if (!RECUR (stmt, any)) 8987 return false; 8988 return true; 8989 8990 case MODIFY_EXPR: 8991 if (cxx_dialect < cxx14) 8992 goto fail; 8993 if (!RECUR (TREE_OPERAND (t, 0), any)) 8994 return false; 8995 /* Just ignore clobbers. */ 8996 if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) 8997 return true; 8998 if (!RECUR (TREE_OPERAND (t, 1), rval)) 8999 return false; 9000 return true; 9001 9002 case MODOP_EXPR: 9003 if (cxx_dialect < cxx14) 9004 goto fail; 9005 if (!RECUR (TREE_OPERAND (t, 0), rval)) 9006 return false; 9007 if (!RECUR (TREE_OPERAND (t, 2), rval)) 9008 return false; 9009 return true; 9010 9011 case DO_STMT: 9012 if (!RECUR (DO_COND (t), rval)) 9013 return false; 9014 if (!RECUR (DO_BODY (t), any)) 9015 return false; 9016 if (breaks (jump_target) || continues (jump_target)) 9017 *jump_target = NULL_TREE; 9018 return true; 9019 9020 case FOR_STMT: 9021 if (!RECUR (FOR_INIT_STMT (t), any)) 9022 return false; 9023 tmp = FOR_COND (t); 9024 if (!RECUR (tmp, rval)) 9025 return false; 9026 if (tmp) 9027 { 9028 if (!processing_template_decl) 9029 tmp = cxx_eval_outermost_constant_expr (tmp, true); 9030 /* If we couldn't evaluate the condition, it might not ever be 9031 true. */ 9032 if (!integer_onep (tmp)) 9033 { 9034 /* Before returning true, check if the for body can contain 9035 a return. */ 9036 hash_set<tree> pset; 9037 check_for_return_continue_data data = { &pset, NULL_TREE, 9038 NULL_TREE }; 9039 if (tree ret_expr 9040 = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, 9041 &data, &pset)) 9042 *jump_target = ret_expr; 9043 return true; 9044 } 9045 } 9046 if (!RECUR (FOR_EXPR (t), any)) 9047 return false; 9048 if (!RECUR (FOR_BODY (t), any)) 9049 return false; 9050 if (breaks (jump_target) || continues (jump_target)) 9051 *jump_target = NULL_TREE; 9052 return true; 9053 9054 case RANGE_FOR_STMT: 9055 if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) 9056 return false; 9057 if (!RECUR (RANGE_FOR_EXPR (t), any)) 9058 return false; 9059 if (!RECUR (RANGE_FOR_BODY (t), any)) 9060 return false; 9061 if (breaks (jump_target) || continues (jump_target)) 9062 *jump_target = NULL_TREE; 9063 return true; 9064 9065 case WHILE_STMT: 9066 tmp = WHILE_COND (t); 9067 if (!RECUR (tmp, rval)) 9068 return false; 9069 if (!processing_template_decl) 9070 tmp = cxx_eval_outermost_constant_expr (tmp, true); 9071 /* If we couldn't evaluate the condition, it might not ever be true. */ 9072 if (!integer_onep (tmp)) 9073 { 9074 /* Before returning true, check if the while body can contain 9075 a return. */ 9076 hash_set<tree> pset; 9077 check_for_return_continue_data data = { &pset, NULL_TREE, 9078 NULL_TREE }; 9079 if (tree ret_expr 9080 = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, 9081 &data, &pset)) 9082 *jump_target = ret_expr; 9083 return true; 9084 } 9085 if (!RECUR (WHILE_BODY (t), any)) 9086 return false; 9087 if (breaks (jump_target) || continues (jump_target)) 9088 *jump_target = NULL_TREE; 9089 return true; 9090 9091 case SWITCH_STMT: 9092 if (!RECUR (SWITCH_STMT_COND (t), rval)) 9093 return false; 9094 /* FIXME we don't check SWITCH_STMT_BODY currently, because even 9095 unreachable labels would be checked and it is enough if there is 9096 a single switch cond value for which it is a valid constant 9097 expression. We need to check if there are any RETURN_EXPRs 9098 or CONTINUE_STMTs inside of the body though, as in that case 9099 we need to set *jump_target. */ 9100 else 9101 { 9102 hash_set<tree> pset; 9103 check_for_return_continue_data data = { &pset, NULL_TREE, 9104 NULL_TREE }; 9105 if (tree ret_expr 9106 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, 9107 &data, &pset)) 9108 /* The switch might return. */ 9109 *jump_target = ret_expr; 9110 else if (data.continue_stmt) 9111 /* The switch can't return, but might continue. */ 9112 *jump_target = data.continue_stmt; 9113 } 9114 return true; 9115 9116 case STMT_EXPR: 9117 return RECUR (STMT_EXPR_STMT (t), rval); 9118 9119 case LAMBDA_EXPR: 9120 if (cxx_dialect >= cxx17) 9121 /* In C++17 lambdas can be constexpr, don't give up yet. */ 9122 return true; 9123 else if (flags & tf_error) 9124 error_at (loc, "lambda-expression is not a constant expression " 9125 "before C++17"); 9126 return false; 9127 9128 case DYNAMIC_CAST_EXPR: 9129 case PSEUDO_DTOR_EXPR: 9130 case NEW_EXPR: 9131 case VEC_NEW_EXPR: 9132 case DELETE_EXPR: 9133 case VEC_DELETE_EXPR: 9134 case THROW_EXPR: 9135 case OMP_PARALLEL: 9136 case OMP_TASK: 9137 case OMP_FOR: 9138 case OMP_SIMD: 9139 case OMP_DISTRIBUTE: 9140 case OMP_TASKLOOP: 9141 case OMP_LOOP: 9142 case OMP_TEAMS: 9143 case OMP_TARGET_DATA: 9144 case OMP_TARGET: 9145 case OMP_SECTIONS: 9146 case OMP_ORDERED: 9147 case OMP_CRITICAL: 9148 case OMP_SINGLE: 9149 case OMP_SCAN: 9150 case OMP_SCOPE: 9151 case OMP_SECTION: 9152 case OMP_MASTER: 9153 case OMP_MASKED: 9154 case OMP_TASKGROUP: 9155 case OMP_TARGET_UPDATE: 9156 case OMP_TARGET_ENTER_DATA: 9157 case OMP_TARGET_EXIT_DATA: 9158 case OMP_ATOMIC: 9159 case OMP_ATOMIC_READ: 9160 case OMP_ATOMIC_CAPTURE_OLD: 9161 case OMP_ATOMIC_CAPTURE_NEW: 9162 case OMP_DEPOBJ: 9163 case OACC_PARALLEL: 9164 case OACC_KERNELS: 9165 case OACC_SERIAL: 9166 case OACC_DATA: 9167 case OACC_HOST_DATA: 9168 case OACC_LOOP: 9169 case OACC_CACHE: 9170 case OACC_DECLARE: 9171 case OACC_ENTER_DATA: 9172 case OACC_EXIT_DATA: 9173 case OACC_UPDATE: 9174 /* GCC internal stuff. */ 9175 case VA_ARG_EXPR: 9176 case TRANSACTION_EXPR: 9177 case AT_ENCODE_EXPR: 9178 fail: 9179 if (flags & tf_error) 9180 error_at (loc, "expression %qE is not a constant expression", t); 9181 return false; 9182 9183 case ASM_EXPR: 9184 if (flags & tf_error) 9185 inline_asm_in_constexpr_error (loc); 9186 return false; 9187 9188 case OBJ_TYPE_REF: 9189 if (cxx_dialect >= cxx20) 9190 /* In C++20 virtual calls can be constexpr, don't give up yet. */ 9191 return true; 9192 else if (flags & tf_error) 9193 error_at (loc, 9194 "virtual functions cannot be %<constexpr%> before C++20"); 9195 return false; 9196 9197 case TYPEID_EXPR: 9198 /* In C++20, a typeid expression whose operand is of polymorphic 9199 class type can be constexpr. */ 9200 { 9201 tree e = TREE_OPERAND (t, 0); 9202 if (cxx_dialect < cxx20 9203 && strict 9204 && !TYPE_P (e) 9205 && !type_dependent_expression_p (e) 9206 && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) 9207 { 9208 if (flags & tf_error) 9209 error_at (loc, "%<typeid%> is not a constant expression " 9210 "because %qE is of polymorphic type", e); 9211 return false; 9212 } 9213 return true; 9214 } 9215 9216 case POINTER_DIFF_EXPR: 9217 case MINUS_EXPR: 9218 want_rval = true; 9219 goto binary; 9220 9221 case LT_EXPR: 9222 case LE_EXPR: 9223 case GT_EXPR: 9224 case GE_EXPR: 9225 case EQ_EXPR: 9226 case NE_EXPR: 9227 case SPACESHIP_EXPR: 9228 want_rval = true; 9229 goto binary; 9230 9231 case PREINCREMENT_EXPR: 9232 case POSTINCREMENT_EXPR: 9233 case PREDECREMENT_EXPR: 9234 case POSTDECREMENT_EXPR: 9235 if (cxx_dialect < cxx14) 9236 goto fail; 9237 goto unary; 9238 9239 case BIT_NOT_EXPR: 9240 /* A destructor. */ 9241 if (TYPE_P (TREE_OPERAND (t, 0))) 9242 return true; 9243 /* fall through. */ 9244 9245 case CONJ_EXPR: 9246 case SAVE_EXPR: 9247 case FIX_TRUNC_EXPR: 9248 case FLOAT_EXPR: 9249 case NEGATE_EXPR: 9250 case ABS_EXPR: 9251 case ABSU_EXPR: 9252 case TRUTH_NOT_EXPR: 9253 case FIXED_CONVERT_EXPR: 9254 case UNARY_PLUS_EXPR: 9255 case UNARY_LEFT_FOLD_EXPR: 9256 case UNARY_RIGHT_FOLD_EXPR: 9257 unary: 9258 return RECUR (TREE_OPERAND (t, 0), rval); 9259 9260 case CAST_EXPR: 9261 case CONST_CAST_EXPR: 9262 case STATIC_CAST_EXPR: 9263 case REINTERPRET_CAST_EXPR: 9264 case IMPLICIT_CONV_EXPR: 9265 if (cxx_dialect < cxx11 9266 && !dependent_type_p (TREE_TYPE (t)) 9267 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) 9268 /* In C++98, a conversion to non-integral type can't be part of a 9269 constant expression. */ 9270 { 9271 if (flags & tf_error) 9272 error_at (loc, 9273 "cast to non-integral type %qT in a constant expression", 9274 TREE_TYPE (t)); 9275 return false; 9276 } 9277 /* This might be a conversion from a class to a (potentially) literal 9278 type. Let's consider it potentially constant since the conversion 9279 might be a constexpr user-defined conversion. */ 9280 else if (cxx_dialect >= cxx11 9281 && (dependent_type_p (TREE_TYPE (t)) 9282 || !COMPLETE_TYPE_P (TREE_TYPE (t)) 9283 || literal_type_p (TREE_TYPE (t))) 9284 && TREE_OPERAND (t, 0)) 9285 { 9286 tree type = TREE_TYPE (TREE_OPERAND (t, 0)); 9287 /* If this is a dependent type, it could end up being a class 9288 with conversions. */ 9289 if (type == NULL_TREE || WILDCARD_TYPE_P (type)) 9290 return true; 9291 /* Or a non-dependent class which has conversions. */ 9292 else if (CLASS_TYPE_P (type) 9293 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) 9294 return true; 9295 } 9296 9297 return (RECUR (TREE_OPERAND (t, 0), 9298 !TYPE_REF_P (TREE_TYPE (t)))); 9299 9300 case BIND_EXPR: 9301 return RECUR (BIND_EXPR_BODY (t), want_rval); 9302 9303 case NON_DEPENDENT_EXPR: 9304 /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by 9305 constexpr evaluation or tsubst, so fold_non_dependent_expr can't 9306 do anything useful with it. And we shouldn't see it in a context 9307 where a constant expression is strictly required, hence the assert. */ 9308 gcc_checking_assert (!(flags & tf_error)); 9309 return false; 9310 9311 case CLEANUP_POINT_EXPR: 9312 case MUST_NOT_THROW_EXPR: 9313 case TRY_CATCH_EXPR: 9314 case TRY_BLOCK: 9315 case EH_SPEC_BLOCK: 9316 case EXPR_STMT: 9317 case PAREN_EXPR: 9318 /* For convenience. */ 9319 case LOOP_EXPR: 9320 case EXIT_EXPR: 9321 return RECUR (TREE_OPERAND (t, 0), want_rval); 9322 9323 case DECL_EXPR: 9324 tmp = DECL_EXPR_DECL (t); 9325 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) 9326 { 9327 if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)) 9328 { 9329 if (flags & tf_error) 9330 error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined " 9331 "%<thread_local%> in %<constexpr%> context", tmp); 9332 return false; 9333 } 9334 else if (TREE_STATIC (tmp)) 9335 { 9336 if (flags & tf_error) 9337 error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined " 9338 "%<static%> in %<constexpr%> context", tmp); 9339 return false; 9340 } 9341 else if (!check_for_uninitialized_const_var 9342 (tmp, /*constexpr_context_p=*/true, flags)) 9343 return false; 9344 } 9345 return RECUR (tmp, want_rval); 9346 9347 case TRY_FINALLY_EXPR: 9348 return (RECUR (TREE_OPERAND (t, 0), want_rval) 9349 && RECUR (TREE_OPERAND (t, 1), any)); 9350 9351 case SCOPE_REF: 9352 return RECUR (TREE_OPERAND (t, 1), want_rval); 9353 9354 case TARGET_EXPR: 9355 if (!TARGET_EXPR_DIRECT_INIT_P (t) 9356 && !literal_type_p (TREE_TYPE (t))) 9357 { 9358 if (flags & tf_error) 9359 { 9360 auto_diagnostic_group d; 9361 error_at (loc, "temporary of non-literal type %qT in a " 9362 "constant expression", TREE_TYPE (t)); 9363 explain_non_literal_class (TREE_TYPE (t)); 9364 } 9365 return false; 9366 } 9367 /* FALLTHRU */ 9368 case INIT_EXPR: 9369 return RECUR (TREE_OPERAND (t, 1), rval); 9370 9371 case CONSTRUCTOR: 9372 { 9373 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); 9374 constructor_elt *ce; 9375 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 9376 if (!RECUR (ce->value, want_rval)) 9377 return false; 9378 return true; 9379 } 9380 9381 case TREE_LIST: 9382 { 9383 gcc_assert (TREE_PURPOSE (t) == NULL_TREE 9384 || DECL_P (TREE_PURPOSE (t))); 9385 if (!RECUR (TREE_VALUE (t), want_rval)) 9386 return false; 9387 if (TREE_CHAIN (t) == NULL_TREE) 9388 return true; 9389 return RECUR (TREE_CHAIN (t), want_rval); 9390 } 9391 9392 case TRUNC_DIV_EXPR: 9393 case CEIL_DIV_EXPR: 9394 case FLOOR_DIV_EXPR: 9395 case ROUND_DIV_EXPR: 9396 case TRUNC_MOD_EXPR: 9397 case CEIL_MOD_EXPR: 9398 case ROUND_MOD_EXPR: 9399 { 9400 tree denom = TREE_OPERAND (t, 1); 9401 if (!RECUR (denom, rval)) 9402 return false; 9403 /* We can't call cxx_eval_outermost_constant_expr on an expression 9404 that hasn't been through instantiate_non_dependent_expr yet. */ 9405 if (!processing_template_decl) 9406 denom = cxx_eval_outermost_constant_expr (denom, true); 9407 if (integer_zerop (denom)) 9408 { 9409 if (flags & tf_error) 9410 error ("division by zero is not a constant expression"); 9411 return false; 9412 } 9413 else 9414 { 9415 want_rval = true; 9416 return RECUR (TREE_OPERAND (t, 0), want_rval); 9417 } 9418 } 9419 9420 case COMPOUND_EXPR: 9421 { 9422 /* check_return_expr sometimes wraps a TARGET_EXPR in a 9423 COMPOUND_EXPR; don't get confused. */ 9424 tree op0 = TREE_OPERAND (t, 0); 9425 tree op1 = TREE_OPERAND (t, 1); 9426 STRIP_NOPS (op1); 9427 if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) 9428 return RECUR (op0, want_rval); 9429 else 9430 goto binary; 9431 } 9432 9433 /* If the first operand is the non-short-circuit constant, look at 9434 the second operand; otherwise we only care about the first one for 9435 potentiality. */ 9436 case TRUTH_AND_EXPR: 9437 case TRUTH_ANDIF_EXPR: 9438 tmp = boolean_true_node; 9439 goto truth; 9440 case TRUTH_OR_EXPR: 9441 case TRUTH_ORIF_EXPR: 9442 tmp = boolean_false_node; 9443 truth: 9444 { 9445 tree op0 = TREE_OPERAND (t, 0); 9446 tree op1 = TREE_OPERAND (t, 1); 9447 if (!RECUR (op0, rval)) 9448 return false; 9449 if (!(flags & tf_error) && RECUR (op1, rval)) 9450 /* When quiet, try to avoid expensive trial evaluation by first 9451 checking potentiality of the second operand. */ 9452 return true; 9453 if (!processing_template_decl) 9454 op0 = cxx_eval_outermost_constant_expr (op0, true); 9455 if (tree_int_cst_equal (op0, tmp)) 9456 return (flags & tf_error) ? RECUR (op1, rval) : false; 9457 else 9458 return true; 9459 } 9460 9461 case PLUS_EXPR: 9462 case MULT_EXPR: 9463 case POINTER_PLUS_EXPR: 9464 case RDIV_EXPR: 9465 case EXACT_DIV_EXPR: 9466 case MIN_EXPR: 9467 case MAX_EXPR: 9468 case LSHIFT_EXPR: 9469 case RSHIFT_EXPR: 9470 case LROTATE_EXPR: 9471 case RROTATE_EXPR: 9472 case BIT_IOR_EXPR: 9473 case BIT_XOR_EXPR: 9474 case BIT_AND_EXPR: 9475 case TRUTH_XOR_EXPR: 9476 case UNORDERED_EXPR: 9477 case ORDERED_EXPR: 9478 case UNLT_EXPR: 9479 case UNLE_EXPR: 9480 case UNGT_EXPR: 9481 case UNGE_EXPR: 9482 case UNEQ_EXPR: 9483 case LTGT_EXPR: 9484 case RANGE_EXPR: 9485 case COMPLEX_EXPR: 9486 want_rval = true; 9487 /* Fall through. */ 9488 case ARRAY_REF: 9489 case ARRAY_RANGE_REF: 9490 case MEMBER_REF: 9491 case DOTSTAR_EXPR: 9492 case MEM_REF: 9493 case BINARY_LEFT_FOLD_EXPR: 9494 case BINARY_RIGHT_FOLD_EXPR: 9495 binary: 9496 for (i = 0; i < 2; ++i) 9497 if (!RECUR (TREE_OPERAND (t, i), want_rval)) 9498 return false; 9499 return true; 9500 9501 case VEC_PERM_EXPR: 9502 for (i = 0; i < 3; ++i) 9503 if (!RECUR (TREE_OPERAND (t, i), true)) 9504 return false; 9505 return true; 9506 9507 case COND_EXPR: 9508 if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) 9509 { 9510 if (flags & tf_error) 9511 error_at (loc, "%<delete[]%> is not a constant expression"); 9512 return false; 9513 } 9514 /* Fall through. */ 9515 case IF_STMT: 9516 case VEC_COND_EXPR: 9517 /* If the condition is a known constant, we know which of the legs we 9518 care about; otherwise we only require that the condition and 9519 either of the legs be potentially constant. */ 9520 tmp = TREE_OPERAND (t, 0); 9521 if (!RECUR (tmp, rval)) 9522 return false; 9523 if (!processing_template_decl) 9524 tmp = cxx_eval_outermost_constant_expr (tmp, true); 9525 /* potential_constant_expression* isn't told if it is called for 9526 manifestly_const_eval or not, so for consteval if always 9527 process both branches as if the condition is not a known 9528 constant. */ 9529 if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) 9530 { 9531 if (integer_zerop (tmp)) 9532 return RECUR (TREE_OPERAND (t, 2), want_rval); 9533 else if (TREE_CODE (tmp) == INTEGER_CST) 9534 return RECUR (TREE_OPERAND (t, 1), want_rval); 9535 } 9536 tmp = *jump_target; 9537 for (i = 1; i < 3; ++i) 9538 { 9539 tree this_jump_target = tmp; 9540 if (potential_constant_expression_1 (TREE_OPERAND (t, i), 9541 want_rval, strict, now, 9542 tf_none, &this_jump_target)) 9543 { 9544 if (returns (&this_jump_target)) 9545 *jump_target = this_jump_target; 9546 else if (!returns (jump_target)) 9547 { 9548 if (breaks (&this_jump_target) 9549 || continues (&this_jump_target)) 9550 *jump_target = this_jump_target; 9551 if (i == 1) 9552 { 9553 /* If the then branch is potentially constant, but 9554 does not return, check if the else branch 9555 couldn't return, break or continue. */ 9556 hash_set<tree> pset; 9557 check_for_return_continue_data data = { &pset, NULL_TREE, 9558 NULL_TREE }; 9559 if (tree ret_expr 9560 = cp_walk_tree (&TREE_OPERAND (t, 2), 9561 check_for_return_continue, &data, 9562 &pset)) 9563 *jump_target = ret_expr; 9564 else if (*jump_target == NULL_TREE) 9565 { 9566 if (data.continue_stmt) 9567 *jump_target = data.continue_stmt; 9568 else if (data.break_stmt) 9569 *jump_target = data.break_stmt; 9570 } 9571 } 9572 } 9573 return true; 9574 } 9575 } 9576 if (flags & tf_error) 9577 { 9578 if (TREE_CODE (t) == IF_STMT) 9579 error_at (loc, "neither branch of %<if%> is a constant expression"); 9580 else 9581 error_at (loc, "expression %qE is not a constant expression", t); 9582 } 9583 return false; 9584 9585 case VEC_INIT_EXPR: 9586 if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) 9587 return true; 9588 if (flags & tf_error) 9589 { 9590 error_at (loc, "non-constant array initialization"); 9591 diagnose_non_constexpr_vec_init (t); 9592 } 9593 return false; 9594 9595 case TYPE_DECL: 9596 case TAG_DEFN: 9597 /* We can see these in statement-expressions. */ 9598 return true; 9599 9600 case CLEANUP_STMT: 9601 if (!RECUR (CLEANUP_BODY (t), any)) 9602 return false; 9603 if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) 9604 return false; 9605 return true; 9606 9607 case EMPTY_CLASS_EXPR: 9608 return true; 9609 9610 case GOTO_EXPR: 9611 { 9612 tree *target = &TREE_OPERAND (t, 0); 9613 /* Gotos representing break, continue and cdtor return are OK. */ 9614 if (breaks (target) || continues (target) || returns (target)) 9615 { 9616 *jump_target = *target; 9617 return true; 9618 } 9619 if (flags & tf_error) 9620 error_at (loc, "%<goto%> is not a constant expression"); 9621 return false; 9622 } 9623 9624 case LABEL_EXPR: 9625 t = LABEL_EXPR_LABEL (t); 9626 if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) 9627 return true; 9628 else if (flags & tf_error) 9629 error_at (loc, "label definition in %<constexpr%> function only " 9630 "available with %<-std=c++2b%> or %<-std=gnu++2b%>"); 9631 return false; 9632 9633 case ANNOTATE_EXPR: 9634 return RECUR (TREE_OPERAND (t, 0), rval); 9635 9636 case BIT_CAST_EXPR: 9637 return RECUR (TREE_OPERAND (t, 0), rval); 9638 9639 /* Coroutine await, yield and return expressions are not. */ 9640 case CO_AWAIT_EXPR: 9641 case CO_YIELD_EXPR: 9642 case CO_RETURN_EXPR: 9643 return false; 9644 9645 case NONTYPE_ARGUMENT_PACK: 9646 { 9647 tree args = ARGUMENT_PACK_ARGS (t); 9648 int len = TREE_VEC_LENGTH (args); 9649 for (int i = 0; i < len; ++i) 9650 if (!RECUR (TREE_VEC_ELT (args, i), any)) 9651 return false; 9652 return true; 9653 } 9654 9655 default: 9656 if (objc_non_constant_expr_p (t)) 9657 return false; 9658 9659 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); 9660 gcc_unreachable (); 9661 return false; 9662 } 9663 #undef RECUR 9664 } 9665 9666 bool 9667 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, 9668 tsubst_flags_t flags) 9669 { 9670 if (flags & tf_error) 9671 { 9672 /* Check potentiality quietly first, as that could be performed more 9673 efficiently in some cases (currently only for TRUTH_*_EXPR). If 9674 that fails, replay the check noisily to give errors. */ 9675 flags &= ~tf_error; 9676 if (potential_constant_expression_1 (t, want_rval, strict, now, flags)) 9677 return true; 9678 flags |= tf_error; 9679 } 9680 9681 tree target = NULL_TREE; 9682 return potential_constant_expression_1 (t, want_rval, strict, now, 9683 flags, &target); 9684 } 9685 9686 /* The main entry point to the above. */ 9687 9688 bool 9689 potential_constant_expression (tree t) 9690 { 9691 return potential_constant_expression_1 (t, false, true, false, tf_none); 9692 } 9693 9694 /* As above, but require a constant rvalue. */ 9695 9696 bool 9697 potential_rvalue_constant_expression (tree t) 9698 { 9699 return potential_constant_expression_1 (t, true, true, false, tf_none); 9700 } 9701 9702 /* Like above, but complain about non-constant expressions. */ 9703 9704 bool 9705 require_potential_constant_expression (tree t) 9706 { 9707 return potential_constant_expression_1 (t, false, true, false, 9708 tf_warning_or_error); 9709 } 9710 9711 /* Cross product of the above. */ 9712 9713 bool 9714 require_potential_rvalue_constant_expression (tree t) 9715 { 9716 return potential_constant_expression_1 (t, true, true, false, 9717 tf_warning_or_error); 9718 } 9719 9720 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ 9721 9722 bool 9723 require_rvalue_constant_expression (tree t) 9724 { 9725 return potential_constant_expression_1 (t, true, true, true, 9726 tf_warning_or_error); 9727 } 9728 9729 /* Like potential_constant_expression, but don't consider possible constexpr 9730 substitution of the current function. That is, PARM_DECL qualifies under 9731 potential_constant_expression, but not here. 9732 9733 This is basically what you can check when any actual constant values might 9734 be value-dependent. */ 9735 9736 bool 9737 is_constant_expression (tree t) 9738 { 9739 return potential_constant_expression_1 (t, false, true, true, tf_none); 9740 } 9741 9742 /* As above, but expect an rvalue. */ 9743 9744 bool 9745 is_rvalue_constant_expression (tree t) 9746 { 9747 return potential_constant_expression_1 (t, true, true, true, tf_none); 9748 } 9749 9750 /* Like above, but complain about non-constant expressions. */ 9751 9752 bool 9753 require_constant_expression (tree t) 9754 { 9755 return potential_constant_expression_1 (t, false, true, true, 9756 tf_warning_or_error); 9757 } 9758 9759 /* Like is_constant_expression, but allow const variables that are not allowed 9760 under constexpr rules. */ 9761 9762 bool 9763 is_static_init_expression (tree t) 9764 { 9765 return potential_constant_expression_1 (t, false, false, true, tf_none); 9766 } 9767 9768 /* Returns true if T is a potential constant expression that is not 9769 instantiation-dependent, and therefore a candidate for constant folding even 9770 in a template. */ 9771 9772 bool 9773 is_nondependent_constant_expression (tree t) 9774 { 9775 return (!type_unknown_p (t) 9776 && is_constant_expression (t) 9777 && !instantiation_dependent_expression_p (t)); 9778 } 9779 9780 /* Returns true if T is a potential static initializer expression that is not 9781 instantiation-dependent. */ 9782 9783 bool 9784 is_nondependent_static_init_expression (tree t) 9785 { 9786 return (!type_unknown_p (t) 9787 && is_static_init_expression (t) 9788 && !instantiation_dependent_expression_p (t)); 9789 } 9790 9791 /* True iff FN is an implicitly constexpr function. */ 9792 9793 bool 9794 decl_implicit_constexpr_p (tree fn) 9795 { 9796 if (!(flag_implicit_constexpr 9797 && TREE_CODE (fn) == FUNCTION_DECL 9798 && DECL_DECLARED_CONSTEXPR_P (fn))) 9799 return false; 9800 9801 if (DECL_CLONED_FUNCTION_P (fn)) 9802 fn = DECL_CLONED_FUNCTION (fn); 9803 9804 return (DECL_LANG_SPECIFIC (fn) 9805 && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr); 9806 } 9807 9808 /* Finalize constexpr processing after parsing. */ 9809 9810 void 9811 fini_constexpr (void) 9812 { 9813 /* The contexpr call and fundef copies tables are no longer needed. */ 9814 constexpr_call_table = NULL; 9815 fundef_copies_table = NULL; 9816 } 9817 9818 #include "gt-cp-constexpr.h" 9819