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 19You should have received a copy of the GNU General Public License 20along 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 43static bool verify_constant (tree, bool, bool *, bool *); 44#define VERIFY_CONSTANT(X) \ 45do { \ 46 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ 47 return t; \ 48 } while (0) 49 50static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, 51 bool insert = false); 52static 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 57bool 58is_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 68bool 69literal_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 91tree 92ensure_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 142struct 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 151static 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 157inline bool 158constexpr_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 167inline hashval_t 168constexpr_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 175constexpr_fundef * 176retrieve_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 188bool 189is_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 284static bool 285build_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 336static bool 337build_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 451static bool 452check_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 466static bool 467check_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 501bool 502check_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 542static vec<constructor_elt, va_gc> * 543sort_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 578static tree 579build_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 666static tree 667get_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 681tree 682constexpr_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 748static tree 749massage_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 768static bool 769cx_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 877void 878maybe_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 983void 984register_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 1003void 1004explain_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 1056struct 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 1073struct 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 1079enum 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 1095struct 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 1119struct 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 1154static 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 1159static 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 1164static bool 1165uid_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 1181uid_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 1191uid_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 1201bool 1202uid_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 1212static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; 1213 1214static 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 1219inline hashval_t 1220constexpr_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 1229bool 1230constexpr_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 1245static void 1246maybe_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 1264static 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 1270static tree 1271get_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 1322static void 1323save_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 1333static inline tree 1334get_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 1354static tree 1355cxx_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 1567static tree 1568adjust_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 1595tree 1596unshare_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 1619static void 1620free_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 1645static tree 1646addr_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 1673static tree 1674cxx_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. */ 1770static vec<tree> call_stack; 1771static int call_stack_tick; 1772static int last_cx_error_tick; 1773 1774static int 1775push_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 1787static void 1788pop_cx_call_context (void) 1789{ 1790 ++call_stack_tick; 1791 call_stack.pop (); 1792} 1793 1794vec<tree> 1795cx_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 1809static tree 1810cxx_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 1886static void 1887clear_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 1901static void 1902modifying_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 1914static inline bool 1915cxx_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 1927static inline bool 1928cxx_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 1946static inline bool 1947is_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 1958static inline bool 1959is_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 1968static inline bool 1969is_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 1990static inline bool 1991is_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 2000static inline bool 2001cxx_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 2011static tree 2012extract_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 2030static tree 2031get_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 2077static tree 2078cxx_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 2254struct 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 2268static tree 2269replace_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 2290bool 2291replace_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 2303static tree 2304cxx_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 2356static void 2357cxx_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 2377static tree 2378cxx_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 3005bool 3006reduced_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; 3085ok: 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 3106static bool 3107verify_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 3136static bool 3137cxx_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 3216static tree 3217cxx_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 3245static tree 3246cxx_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 3308static tree 3309cxx_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 3333static tree 3334cxx_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 3465static tree 3466cxx_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 3509static tree 3510cxx_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 3544static int 3545array_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 3572static HOST_WIDE_INT 3573find_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 3699static constructor_elt * 3700get_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 3803static void 3804diag_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 3836static tree 3837get_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 3862static tree 3863extract_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 3883static tree 3884eval_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 3921static tree 3922cxx_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 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) 3936 ary = TREE_OPERAND (ary, 0); 3937 3938 tree oldidx = TREE_OPERAND (t, 1); 3939 tree index = eval_and_check_array_index (ctx, t, lval, 3940 non_constant_p, overflow_p); 3941 if (*non_constant_p) 3942 return t; 3943 3944 if (lval && ary == oldary && index == oldidx) 3945 return t; 3946 else if (lval) 3947 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); 3948 3949 unsigned len = 0, elem_nchars = 1; 3950 tree elem_type = TREE_TYPE (TREE_TYPE (ary)); 3951 if (TREE_CODE (ary) == CONSTRUCTOR) 3952 len = CONSTRUCTOR_NELTS (ary); 3953 else if (TREE_CODE (ary) == STRING_CST) 3954 { 3955 elem_nchars = (TYPE_PRECISION (elem_type) 3956 / TYPE_PRECISION (char_type_node)); 3957 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; 3958 } 3959 else if (TREE_CODE (ary) == VECTOR_CST) 3960 /* We don't create variable-length VECTOR_CSTs. */ 3961 len = VECTOR_CST_NELTS (ary).to_constant (); 3962 else 3963 { 3964 /* We can't do anything with other tree codes, so use 3965 VERIFY_CONSTANT to complain and fail. */ 3966 VERIFY_CONSTANT (ary); 3967 gcc_unreachable (); 3968 } 3969 3970 bool found; 3971 HOST_WIDE_INT i = 0; 3972 if (TREE_CODE (ary) == CONSTRUCTOR) 3973 { 3974 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); 3975 found = (ix >= 0); 3976 if (found) 3977 i = ix; 3978 } 3979 else 3980 { 3981 i = tree_to_shwi (index); 3982 found = (i < len); 3983 } 3984 3985 if (found) 3986 { 3987 tree r; 3988 if (TREE_CODE (ary) == CONSTRUCTOR) 3989 r = (*CONSTRUCTOR_ELTS (ary))[i].value; 3990 else if (TREE_CODE (ary) == VECTOR_CST) 3991 r = VECTOR_CST_ELT (ary, i); 3992 else 3993 r = extract_string_elt (ary, elem_nchars, i); 3994 3995 if (r) 3996 /* Don't VERIFY_CONSTANT here. */ 3997 return r; 3998 3999 /* Otherwise the element doesn't have a value yet. */ 4000 } 4001 4002 /* Not found. */ 4003 4004 if (TREE_CODE (ary) == CONSTRUCTOR 4005 && CONSTRUCTOR_NO_CLEARING (ary)) 4006 { 4007 /* 'ary' is part of the aggregate initializer we're currently 4008 building; if there's no initializer for this element yet, 4009 that's an error. */ 4010 if (!ctx->quiet) 4011 error ("accessing uninitialized array element"); 4012 *non_constant_p = true; 4013 return t; 4014 } 4015 4016 /* If it's within the array bounds but doesn't have an explicit 4017 initializer, it's initialized from {}. But use build_value_init 4018 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ 4019 tree val; 4020 constexpr_ctx new_ctx; 4021 if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) 4022 return build_constructor (elem_type, NULL); 4023 else if (CP_AGGREGATE_TYPE_P (elem_type)) 4024 { 4025 tree empty_ctor = build_constructor (init_list_type_node, NULL); 4026 val = digest_init (elem_type, empty_ctor, tf_warning_or_error); 4027 } 4028 else 4029 val = build_value_init (elem_type, tf_warning_or_error); 4030 4031 if (!SCALAR_TYPE_P (elem_type)) 4032 { 4033 new_ctx = *ctx; 4034 if (ctx->object) 4035 /* If there was no object, don't add one: it could confuse us 4036 into thinking we're modifying a const object. */ 4037 new_ctx.object = t; 4038 new_ctx.ctor = build_constructor (elem_type, NULL); 4039 ctx = &new_ctx; 4040 } 4041 t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, 4042 overflow_p); 4043 if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor) 4044 free_constructor (ctx->ctor); 4045 return t; 4046} 4047 4048/* Subroutine of cxx_eval_constant_expression. 4049 Attempt to reduce a field access of a value of class type. */ 4050 4051static tree 4052cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, 4053 bool lval, 4054 bool *non_constant_p, bool *overflow_p) 4055{ 4056 unsigned HOST_WIDE_INT i; 4057 tree field; 4058 tree value; 4059 tree part = TREE_OPERAND (t, 1); 4060 tree orig_whole = TREE_OPERAND (t, 0); 4061 tree whole = cxx_eval_constant_expression (ctx, orig_whole, 4062 lval, 4063 non_constant_p, overflow_p); 4064 if (INDIRECT_REF_P (whole) 4065 && integer_zerop (TREE_OPERAND (whole, 0))) 4066 { 4067 if (!ctx->quiet) 4068 error ("dereferencing a null pointer in %qE", orig_whole); 4069 *non_constant_p = true; 4070 return t; 4071 } 4072 4073 if (TREE_CODE (whole) == PTRMEM_CST) 4074 whole = cplus_expand_constant (whole); 4075 if (whole == orig_whole) 4076 return t; 4077 if (lval) 4078 return fold_build3 (COMPONENT_REF, TREE_TYPE (t), 4079 whole, part, NULL_TREE); 4080 /* Don't VERIFY_CONSTANT here; we only want to check that we got a 4081 CONSTRUCTOR. */ 4082 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) 4083 { 4084 if (!ctx->quiet) 4085 error ("%qE is not a constant expression", orig_whole); 4086 *non_constant_p = true; 4087 } 4088 if (DECL_MUTABLE_P (part)) 4089 { 4090 if (!ctx->quiet) 4091 error ("mutable %qD is not usable in a constant expression", part); 4092 *non_constant_p = true; 4093 } 4094 if (*non_constant_p) 4095 return t; 4096 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); 4097 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) 4098 { 4099 /* Use name match for PMF fields, as a variant will have a 4100 different FIELD_DECL with a different type. */ 4101 if (pmf ? DECL_NAME (field) == DECL_NAME (part) 4102 : field == part) 4103 { 4104 if (value) 4105 { 4106 STRIP_ANY_LOCATION_WRAPPER (value); 4107 return value; 4108 } 4109 else 4110 /* We're in the middle of initializing it. */ 4111 break; 4112 } 4113 } 4114 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE 4115 && CONSTRUCTOR_NELTS (whole) > 0) 4116 { 4117 /* DR 1188 says we don't have to deal with this. */ 4118 if (!ctx->quiet) 4119 { 4120 constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); 4121 if (cep->value == NULL_TREE) 4122 error ("accessing uninitialized member %qD", part); 4123 else 4124 error ("accessing %qD member instead of initialized %qD member in " 4125 "constant expression", part, cep->index); 4126 } 4127 *non_constant_p = true; 4128 return t; 4129 } 4130 4131 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty 4132 classes never get represented; throw together a value now. */ 4133 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 4134 return build_constructor (TREE_TYPE (t), NULL); 4135 4136 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); 4137 4138 if (CONSTRUCTOR_NO_CLEARING (whole)) 4139 { 4140 /* 'whole' is part of the aggregate initializer we're currently 4141 building; if there's no initializer for this member yet, that's an 4142 error. */ 4143 if (!ctx->quiet) 4144 error ("accessing uninitialized member %qD", part); 4145 *non_constant_p = true; 4146 return t; 4147 } 4148 4149 /* If there's no explicit init for this field, it's value-initialized. */ 4150 value = build_value_init (TREE_TYPE (t), tf_warning_or_error); 4151 return cxx_eval_constant_expression (ctx, value, 4152 lval, 4153 non_constant_p, overflow_p); 4154} 4155 4156/* Subroutine of cxx_eval_constant_expression. 4157 Attempt to reduce a field access of a value of class type that is 4158 expressed as a BIT_FIELD_REF. */ 4159 4160static tree 4161cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, 4162 bool lval, 4163 bool *non_constant_p, bool *overflow_p) 4164{ 4165 tree orig_whole = TREE_OPERAND (t, 0); 4166 tree retval, fldval, utype, mask; 4167 bool fld_seen = false; 4168 HOST_WIDE_INT istart, isize; 4169 tree whole = cxx_eval_constant_expression (ctx, orig_whole, 4170 lval, 4171 non_constant_p, overflow_p); 4172 tree start, field, value; 4173 unsigned HOST_WIDE_INT i; 4174 4175 if (whole == orig_whole) 4176 return t; 4177 /* Don't VERIFY_CONSTANT here; we only want to check that we got a 4178 CONSTRUCTOR. */ 4179 if (!*non_constant_p 4180 && TREE_CODE (whole) != VECTOR_CST 4181 && TREE_CODE (whole) != CONSTRUCTOR) 4182 { 4183 if (!ctx->quiet) 4184 error ("%qE is not a constant expression", orig_whole); 4185 *non_constant_p = true; 4186 } 4187 if (*non_constant_p) 4188 return t; 4189 4190 if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t))) 4191 { 4192 if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, 4193 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2))) 4194 return r; 4195 if (!ctx->quiet) 4196 error ("%qE is not a constant expression", orig_whole); 4197 *non_constant_p = true; 4198 return t; 4199 } 4200 4201 start = TREE_OPERAND (t, 2); 4202 istart = tree_to_shwi (start); 4203 isize = tree_to_shwi (TREE_OPERAND (t, 1)); 4204 utype = TREE_TYPE (t); 4205 if (!TYPE_UNSIGNED (utype)) 4206 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); 4207 retval = build_int_cst (utype, 0); 4208 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) 4209 { 4210 tree bitpos = bit_position (field); 4211 STRIP_ANY_LOCATION_WRAPPER (value); 4212 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) 4213 return value; 4214 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE 4215 && TREE_CODE (value) == INTEGER_CST 4216 && tree_fits_shwi_p (bitpos) 4217 && tree_fits_shwi_p (DECL_SIZE (field))) 4218 { 4219 HOST_WIDE_INT bit = tree_to_shwi (bitpos); 4220 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); 4221 HOST_WIDE_INT shift; 4222 if (bit >= istart && bit + sz <= istart + isize) 4223 { 4224 fldval = fold_convert (utype, value); 4225 mask = build_int_cst_type (utype, -1); 4226 mask = fold_build2 (LSHIFT_EXPR, utype, mask, 4227 size_int (TYPE_PRECISION (utype) - sz)); 4228 mask = fold_build2 (RSHIFT_EXPR, utype, mask, 4229 size_int (TYPE_PRECISION (utype) - sz)); 4230 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); 4231 shift = bit - istart; 4232 if (BYTES_BIG_ENDIAN) 4233 shift = TYPE_PRECISION (utype) - shift - sz; 4234 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, 4235 size_int (shift)); 4236 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); 4237 fld_seen = true; 4238 } 4239 } 4240 } 4241 if (fld_seen) 4242 return fold_convert (TREE_TYPE (t), retval); 4243 gcc_unreachable (); 4244 return error_mark_node; 4245} 4246 4247/* Helper for cxx_eval_bit_cast. 4248 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From 4249 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>, 4250 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static 4251 data members of reference type. */ 4252 4253static bool 4254check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, 4255 tree orig_type) 4256{ 4257 if (TREE_CODE (type) == UNION_TYPE) 4258 { 4259 if (!ctx->quiet) 4260 { 4261 if (type == orig_type) 4262 error_at (loc, "%qs is not a constant expression because %qT is " 4263 "a union type", "__builtin_bit_cast", type); 4264 else 4265 error_at (loc, "%qs is not a constant expression because %qT " 4266 "contains a union type", "__builtin_bit_cast", 4267 orig_type); 4268 } 4269 return true; 4270 } 4271 if (TREE_CODE (type) == POINTER_TYPE) 4272 { 4273 if (!ctx->quiet) 4274 { 4275 if (type == orig_type) 4276 error_at (loc, "%qs is not a constant expression because %qT is " 4277 "a pointer type", "__builtin_bit_cast", type); 4278 else 4279 error_at (loc, "%qs is not a constant expression because %qT " 4280 "contains a pointer type", "__builtin_bit_cast", 4281 orig_type); 4282 } 4283 return true; 4284 } 4285 if (TREE_CODE (type) == REFERENCE_TYPE) 4286 { 4287 if (!ctx->quiet) 4288 { 4289 if (type == orig_type) 4290 error_at (loc, "%qs is not a constant expression because %qT is " 4291 "a reference type", "__builtin_bit_cast", type); 4292 else 4293 error_at (loc, "%qs is not a constant expression because %qT " 4294 "contains a reference type", "__builtin_bit_cast", 4295 orig_type); 4296 } 4297 return true; 4298 } 4299 if (TYPE_PTRMEM_P (type)) 4300 { 4301 if (!ctx->quiet) 4302 { 4303 if (type == orig_type) 4304 error_at (loc, "%qs is not a constant expression because %qT is " 4305 "a pointer to member type", "__builtin_bit_cast", 4306 type); 4307 else 4308 error_at (loc, "%qs is not a constant expression because %qT " 4309 "contains a pointer to member type", 4310 "__builtin_bit_cast", orig_type); 4311 } 4312 return true; 4313 } 4314 if (TYPE_VOLATILE (type)) 4315 { 4316 if (!ctx->quiet) 4317 { 4318 if (type == orig_type) 4319 error_at (loc, "%qs is not a constant expression because %qT is " 4320 "volatile", "__builtin_bit_cast", type); 4321 else 4322 error_at (loc, "%qs is not a constant expression because %qT " 4323 "contains a volatile subobject", 4324 "__builtin_bit_cast", orig_type); 4325 } 4326 return true; 4327 } 4328 if (TREE_CODE (type) == RECORD_TYPE) 4329 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 4330 if (TREE_CODE (field) == FIELD_DECL 4331 && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) 4332 return true; 4333 return false; 4334} 4335 4336/* Helper function for cxx_eval_bit_cast. For unsigned char or 4337 std::byte members of CONSTRUCTOR (recursively) if they contain 4338 some indeterminate bits (as set in MASK), remove the ctor elts, 4339 mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the 4340 bits in MASK. */ 4341 4342static void 4343clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask) 4344{ 4345 if (TREE_CODE (t) != CONSTRUCTOR) 4346 return; 4347 4348 unsigned i, j = 0; 4349 tree index, value; 4350 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value) 4351 { 4352 tree type = TREE_TYPE (value); 4353 if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE 4354 && DECL_BIT_FIELD_TYPE (index) != NULL_TREE) 4355 { 4356 if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index))) 4357 { 4358 HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index)); 4359 gcc_assert (fldsz != 0); 4360 HOST_WIDE_INT pos = int_byte_position (index); 4361 HOST_WIDE_INT bpos 4362 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index)); 4363 bpos %= BITS_PER_UNIT; 4364 HOST_WIDE_INT end 4365 = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; 4366 gcc_assert (end == 1 || end == 2); 4367 unsigned char *p = mask + pos; 4368 unsigned char mask_save[2]; 4369 mask_save[0] = mask[pos]; 4370 mask_save[1] = end == 2 ? mask[pos + 1] : 0; 4371 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) 4372 sorry_at (loc, "PDP11 bit-field handling unsupported" 4373 " in %qs", "__builtin_bit_cast"); 4374 else if (BYTES_BIG_ENDIAN) 4375 { 4376 /* Big endian. */ 4377 if (bpos + fldsz <= BITS_PER_UNIT) 4378 *p &= ~(((1 << fldsz) - 1) 4379 << (BITS_PER_UNIT - bpos - fldsz)); 4380 else 4381 { 4382 gcc_assert (bpos); 4383 *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos); 4384 p++; 4385 fldsz -= BITS_PER_UNIT - bpos; 4386 gcc_assert (fldsz && fldsz < BITS_PER_UNIT); 4387 *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; 4388 } 4389 } 4390 else 4391 { 4392 /* Little endian. */ 4393 if (bpos + fldsz <= BITS_PER_UNIT) 4394 *p &= ~(((1 << fldsz) - 1) << bpos); 4395 else 4396 { 4397 gcc_assert (bpos); 4398 *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); 4399 p++; 4400 fldsz -= BITS_PER_UNIT - bpos; 4401 gcc_assert (fldsz && fldsz < BITS_PER_UNIT); 4402 *p &= ~((1 << fldsz) - 1); 4403 } 4404 } 4405 if (mask_save[0] != mask[pos] 4406 || (end == 2 && mask_save[1] != mask[pos + 1])) 4407 { 4408 CONSTRUCTOR_NO_CLEARING (t) = 1; 4409 continue; 4410 } 4411 } 4412 } 4413 else if (is_byte_access_type_not_plain_char (type)) 4414 { 4415 HOST_WIDE_INT pos; 4416 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 4417 pos = tree_to_shwi (index); 4418 else 4419 pos = int_byte_position (index); 4420 if (mask[pos]) 4421 { 4422 CONSTRUCTOR_NO_CLEARING (t) = 1; 4423 mask[pos] = 0; 4424 continue; 4425 } 4426 } 4427 if (TREE_CODE (value) == CONSTRUCTOR) 4428 { 4429 HOST_WIDE_INT pos; 4430 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 4431 pos = tree_to_shwi (index) 4432 * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))); 4433 else 4434 pos = int_byte_position (index); 4435 clear_uchar_or_std_byte_in_mask (loc, value, mask + pos); 4436 } 4437 if (i != j) 4438 { 4439 CONSTRUCTOR_ELT (t, j)->index = index; 4440 CONSTRUCTOR_ELT (t, j)->value = value; 4441 } 4442 ++j; 4443 } 4444 if (CONSTRUCTOR_NELTS (t) != j) 4445 vec_safe_truncate (CONSTRUCTOR_ELTS (t), j); 4446} 4447 4448/* Subroutine of cxx_eval_constant_expression. 4449 Attempt to evaluate a BIT_CAST_EXPR. */ 4450 4451static tree 4452cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, 4453 bool *overflow_p) 4454{ 4455 if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), 4456 TREE_TYPE (t)) 4457 || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0), 4458 EXPR_LOCATION (t)), 4459 TREE_TYPE (TREE_OPERAND (t, 0)), 4460 TREE_TYPE (TREE_OPERAND (t, 0)))) 4461 { 4462 *non_constant_p = true; 4463 return t; 4464 } 4465 4466 tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, 4467 non_constant_p, overflow_p); 4468 if (*non_constant_p) 4469 return t; 4470 4471 location_t loc = EXPR_LOCATION (t); 4472 if (BITS_PER_UNIT != 8 || CHAR_BIT != 8) 4473 { 4474 if (!ctx->quiet) 4475 sorry_at (loc, "%qs cannot be constant evaluated on the target", 4476 "__builtin_bit_cast"); 4477 *non_constant_p = true; 4478 return t; 4479 } 4480 4481 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) 4482 { 4483 if (!ctx->quiet) 4484 sorry_at (loc, "%qs cannot be constant evaluated because the " 4485 "type is too large", "__builtin_bit_cast"); 4486 *non_constant_p = true; 4487 return t; 4488 } 4489 4490 HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); 4491 if (len < 0 || (int) len != len) 4492 { 4493 if (!ctx->quiet) 4494 sorry_at (loc, "%qs cannot be constant evaluated because the " 4495 "type is too large", "__builtin_bit_cast"); 4496 *non_constant_p = true; 4497 return t; 4498 } 4499 4500 unsigned char buf[64]; 4501 unsigned char *ptr, *mask; 4502 size_t alen = (size_t) len * 2; 4503 if (alen <= sizeof (buf)) 4504 ptr = buf; 4505 else 4506 ptr = XNEWVEC (unsigned char, alen); 4507 mask = ptr + (size_t) len; 4508 /* At the beginning consider everything indeterminate. */ 4509 memset (mask, ~0, (size_t) len); 4510 4511 if (native_encode_initializer (op, ptr, len, 0, mask) != len) 4512 { 4513 if (!ctx->quiet) 4514 sorry_at (loc, "%qs cannot be constant evaluated because the " 4515 "argument cannot be encoded", "__builtin_bit_cast"); 4516 *non_constant_p = true; 4517 if (ptr != buf) 4518 XDELETE (ptr); 4519 return t; 4520 } 4521 4522 tree r = NULL_TREE; 4523 if (can_native_interpret_type_p (TREE_TYPE (t))) 4524 { 4525 r = native_interpret_expr (TREE_TYPE (t), ptr, len); 4526 if (is_byte_access_type_not_plain_char (TREE_TYPE (t))) 4527 { 4528 gcc_assert (len == 1); 4529 if (mask[0]) 4530 { 4531 memset (mask, 0, len); 4532 r = build_constructor (TREE_TYPE (r), NULL); 4533 CONSTRUCTOR_NO_CLEARING (r) = 1; 4534 } 4535 } 4536 } 4537 else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) 4538 { 4539 r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); 4540 if (r != NULL_TREE) 4541 { 4542 clear_type_padding_in_mask (TREE_TYPE (t), mask); 4543 clear_uchar_or_std_byte_in_mask (loc, r, mask); 4544 } 4545 } 4546 4547 if (r != NULL_TREE) 4548 { 4549 for (int i = 0; i < len; i++) 4550 if (mask[i]) 4551 { 4552 if (!ctx->quiet) 4553 error_at (loc, "%qs accessing uninitialized byte at offset %d", 4554 "__builtin_bit_cast", i); 4555 *non_constant_p = true; 4556 r = t; 4557 break; 4558 } 4559 if (ptr != buf) 4560 XDELETE (ptr); 4561 return r; 4562 } 4563 4564 if (!ctx->quiet) 4565 sorry_at (loc, "%qs cannot be constant evaluated because the " 4566 "argument cannot be interpreted", "__builtin_bit_cast"); 4567 *non_constant_p = true; 4568 if (ptr != buf) 4569 XDELETE (ptr); 4570 return t; 4571} 4572 4573/* Subroutine of cxx_eval_constant_expression. 4574 Evaluate a short-circuited logical expression T in the context 4575 of a given constexpr CALL. BAILOUT_VALUE is the value for 4576 early return. CONTINUE_VALUE is used here purely for 4577 sanity check purposes. */ 4578 4579static tree 4580cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, 4581 tree bailout_value, tree continue_value, 4582 bool *non_constant_p, bool *overflow_p) 4583{ 4584 tree r; 4585 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 4586 /*lval*/false, non_constant_p, 4587 overflow_p); 4588 VERIFY_CONSTANT (lhs); 4589 if (tree_int_cst_equal (lhs, bailout_value)) 4590 return lhs; 4591 gcc_assert (tree_int_cst_equal (lhs, continue_value)); 4592 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 4593 /*lval*/false, non_constant_p, 4594 overflow_p); 4595 VERIFY_CONSTANT (r); 4596 return r; 4597} 4598 4599/* REF is a COMPONENT_REF designating a particular field. V is a vector of 4600 CONSTRUCTOR elements to initialize (part of) an object containing that 4601 field. Return a pointer to the constructor_elt corresponding to the 4602 initialization of the field. */ 4603 4604static constructor_elt * 4605base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) 4606{ 4607 tree aggr = TREE_OPERAND (ref, 0); 4608 tree field = TREE_OPERAND (ref, 1); 4609 HOST_WIDE_INT i; 4610 constructor_elt *ce; 4611 4612 gcc_assert (TREE_CODE (ref) == COMPONENT_REF); 4613 4614 if (TREE_CODE (aggr) == COMPONENT_REF) 4615 { 4616 constructor_elt *base_ce 4617 = base_field_constructor_elt (v, aggr); 4618 v = CONSTRUCTOR_ELTS (base_ce->value); 4619 } 4620 4621 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 4622 if (ce->index == field) 4623 return ce; 4624 4625 gcc_unreachable (); 4626 return NULL; 4627} 4628 4629/* Some of the expressions fed to the constexpr mechanism are calls to 4630 constructors, which have type void. In that case, return the type being 4631 initialized by the constructor. */ 4632 4633static tree 4634initialized_type (tree t) 4635{ 4636 if (TYPE_P (t)) 4637 return t; 4638 tree type = TREE_TYPE (t); 4639 if (TREE_CODE (t) == CALL_EXPR) 4640 { 4641 /* A constructor call has void type, so we need to look deeper. */ 4642 tree fn = get_function_named_in_call (t); 4643 if (fn && TREE_CODE (fn) == FUNCTION_DECL 4644 && DECL_CXX_CONSTRUCTOR_P (fn)) 4645 type = DECL_CONTEXT (fn); 4646 } 4647 else if (TREE_CODE (t) == COMPOUND_EXPR) 4648 return initialized_type (TREE_OPERAND (t, 1)); 4649 else if (TREE_CODE (t) == AGGR_INIT_EXPR) 4650 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); 4651 return cv_unqualified (type); 4652} 4653 4654/* We're about to initialize element INDEX of an array or class from VALUE. 4655 Set up NEW_CTX appropriately by adjusting .object to refer to the 4656 subobject and creating a new CONSTRUCTOR if the element is itself 4657 a class or array. */ 4658 4659static void 4660init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, 4661 tree index, tree &value) 4662{ 4663 new_ctx = *ctx; 4664 4665 if (index && TREE_CODE (index) != INTEGER_CST 4666 && TREE_CODE (index) != FIELD_DECL 4667 && TREE_CODE (index) != RANGE_EXPR) 4668 /* This won't have an element in the new CONSTRUCTOR. */ 4669 return; 4670 4671 tree type = initialized_type (value); 4672 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) 4673 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ 4674 return; 4675 if (VECTOR_TYPE_P (type) 4676 && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor)) 4677 && index == NULL_TREE) 4678 /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger 4679 vector is constructed from smaller vectors, doesn't get its own 4680 CONSTRUCTOR either. */ 4681 return; 4682 4683 /* The sub-aggregate initializer might contain a placeholder; 4684 update object to refer to the subobject and ctor to refer to 4685 the (newly created) sub-initializer. */ 4686 if (ctx->object) 4687 { 4688 if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) 4689 /* There's no well-defined subobject for this index. */ 4690 new_ctx.object = NULL_TREE; 4691 else 4692 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); 4693 } 4694 tree elt = build_constructor (type, NULL); 4695 CONSTRUCTOR_NO_CLEARING (elt) = true; 4696 new_ctx.ctor = elt; 4697 4698 if (TREE_CODE (value) == TARGET_EXPR) 4699 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ 4700 value = TARGET_EXPR_INITIAL (value); 4701} 4702 4703/* We're about to process an initializer for a class or array TYPE. Make 4704 sure that CTX is set up appropriately. */ 4705 4706static void 4707verify_ctor_sanity (const constexpr_ctx *ctx, tree type) 4708{ 4709 /* We don't bother building a ctor for an empty base subobject. */ 4710 if (is_empty_class (type)) 4711 return; 4712 4713 /* We're in the middle of an initializer that might involve placeholders; 4714 our caller should have created a CONSTRUCTOR for us to put the 4715 initializer into. We will either return that constructor or T. */ 4716 gcc_assert (ctx->ctor); 4717 gcc_assert (same_type_ignoring_top_level_qualifiers_p 4718 (type, TREE_TYPE (ctx->ctor))); 4719 /* We used to check that ctx->ctor was empty, but that isn't the case when 4720 the object is zero-initialized before calling the constructor. */ 4721 if (ctx->object) 4722 { 4723 tree otype = TREE_TYPE (ctx->object); 4724 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) 4725 /* Handle flexible array members. */ 4726 || (TREE_CODE (otype) == ARRAY_TYPE 4727 && TYPE_DOMAIN (otype) == NULL_TREE 4728 && TREE_CODE (type) == ARRAY_TYPE 4729 && (same_type_ignoring_top_level_qualifiers_p 4730 (TREE_TYPE (type), TREE_TYPE (otype))))); 4731 } 4732 gcc_assert (!ctx->object || !DECL_P (ctx->object) 4733 || *(ctx->global->values.get (ctx->object)) == ctx->ctor); 4734} 4735 4736/* Subroutine of cxx_eval_constant_expression. 4737 The expression tree T denotes a C-style array or a C-style 4738 aggregate. Reduce it to a constant expression. */ 4739 4740static tree 4741cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, 4742 bool lval, 4743 bool *non_constant_p, bool *overflow_p) 4744{ 4745 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); 4746 bool changed = false; 4747 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); 4748 tree type = TREE_TYPE (t); 4749 4750 constexpr_ctx new_ctx; 4751 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) 4752 { 4753 /* We don't really need the ctx->ctor business for a PMF or 4754 vector, but it's simpler to use the same code. */ 4755 new_ctx = *ctx; 4756 new_ctx.ctor = build_constructor (type, NULL); 4757 new_ctx.object = NULL_TREE; 4758 ctx = &new_ctx; 4759 }; 4760 verify_ctor_sanity (ctx, type); 4761 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); 4762 vec_alloc (*p, vec_safe_length (v)); 4763 4764 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) 4765 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; 4766 4767 unsigned i; 4768 tree index, value; 4769 bool constant_p = true; 4770 bool side_effects_p = false; 4771 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) 4772 { 4773 tree orig_value = value; 4774 /* Like in cxx_eval_store_expression, omit entries for empty fields. */ 4775 bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index); 4776 init_subob_ctx (ctx, new_ctx, index, value); 4777 int pos_hint = -1; 4778 if (new_ctx.ctor != ctx->ctor && !no_slot) 4779 { 4780 /* If we built a new CONSTRUCTOR, attach it now so that other 4781 initializers can refer to it. */ 4782 constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); 4783 cep->value = new_ctx.ctor; 4784 pos_hint = cep - (*p)->begin(); 4785 } 4786 else if (TREE_CODE (type) == UNION_TYPE) 4787 /* Otherwise if we're constructing a non-aggregate union member, set 4788 the active union member now so that we can later detect and diagnose 4789 if its initializer attempts to activate another member. */ 4790 get_or_insert_ctor_field (ctx->ctor, index); 4791 tree elt = cxx_eval_constant_expression (&new_ctx, value, 4792 lval, 4793 non_constant_p, overflow_p); 4794 /* Don't VERIFY_CONSTANT here. */ 4795 if (ctx->quiet && *non_constant_p) 4796 break; 4797 if (elt != orig_value) 4798 changed = true; 4799 4800 if (!TREE_CONSTANT (elt)) 4801 constant_p = false; 4802 if (TREE_SIDE_EFFECTS (elt)) 4803 side_effects_p = true; 4804 if (index && TREE_CODE (index) == COMPONENT_REF) 4805 { 4806 /* This is an initialization of a vfield inside a base 4807 subaggregate that we already initialized; push this 4808 initialization into the previous initialization. */ 4809 constructor_elt *inner = base_field_constructor_elt (*p, index); 4810 inner->value = elt; 4811 changed = true; 4812 } 4813 else if (index 4814 && (TREE_CODE (index) == NOP_EXPR 4815 || TREE_CODE (index) == POINTER_PLUS_EXPR)) 4816 { 4817 /* This is an initializer for an empty base; now that we've 4818 checked that it's constant, we can ignore it. */ 4819 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); 4820 changed = true; 4821 } 4822 else if (no_slot) 4823 changed = true; 4824 else 4825 { 4826 if (TREE_CODE (type) == UNION_TYPE 4827 && (*p)->last().index != index) 4828 /* The initializer erroneously changed the active union member that 4829 we're initializing. */ 4830 gcc_assert (*non_constant_p); 4831 else 4832 { 4833 /* The initializer might have mutated the underlying CONSTRUCTOR, 4834 so recompute the location of the target constructer_elt. */ 4835 constructor_elt *cep 4836 = get_or_insert_ctor_field (ctx->ctor, index, pos_hint); 4837 cep->value = elt; 4838 } 4839 4840 /* Adding or replacing an element might change the ctor's flags. */ 4841 TREE_CONSTANT (ctx->ctor) = constant_p; 4842 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; 4843 } 4844 } 4845 if (*non_constant_p || !changed) 4846 return t; 4847 t = ctx->ctor; 4848 /* We're done building this CONSTRUCTOR, so now we can interpret an 4849 element without an explicit initializer as value-initialized. */ 4850 CONSTRUCTOR_NO_CLEARING (t) = false; 4851 TREE_CONSTANT (t) = constant_p; 4852 TREE_SIDE_EFFECTS (t) = side_effects_p; 4853 if (VECTOR_TYPE_P (type)) 4854 t = fold (t); 4855 return t; 4856} 4857 4858/* Subroutine of cxx_eval_constant_expression. 4859 The expression tree T is a VEC_INIT_EXPR which denotes the desired 4860 initialization of a non-static data member of array type. Reduce it to a 4861 CONSTRUCTOR. 4862 4863 Note that apart from value-initialization (when VALUE_INIT is true), 4864 this is only intended to support value-initialization and the 4865 initializations done by defaulted constructors for classes with 4866 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT 4867 will either be NULL_TREE for the default constructor, or a COMPONENT_REF 4868 for the copy/move constructor. */ 4869 4870static tree 4871cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, 4872 bool value_init, bool lval, 4873 bool *non_constant_p, bool *overflow_p) 4874{ 4875 tree elttype = TREE_TYPE (atype); 4876 verify_ctor_sanity (ctx, atype); 4877 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); 4878 bool pre_init = false; 4879 unsigned HOST_WIDE_INT i; 4880 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; 4881 4882 if (init && TREE_CODE (init) == CONSTRUCTOR) 4883 return cxx_eval_bare_aggregate (ctx, init, lval, 4884 non_constant_p, overflow_p); 4885 4886 /* For the default constructor, build up a call to the default 4887 constructor of the element type. We only need to handle class types 4888 here, as for a constructor to be constexpr, all members must be 4889 initialized, which for a defaulted default constructor means they must 4890 be of a class type with a constexpr default constructor. */ 4891 if (TREE_CODE (elttype) == ARRAY_TYPE) 4892 /* We only do this at the lowest level. */; 4893 else if (value_init) 4894 { 4895 init = build_value_init (elttype, complain); 4896 pre_init = true; 4897 } 4898 else if (!init) 4899 { 4900 releasing_vec argvec; 4901 init = build_special_member_call (NULL_TREE, complete_ctor_identifier, 4902 &argvec, elttype, LOOKUP_NORMAL, 4903 complain); 4904 init = build_aggr_init_expr (elttype, init); 4905 pre_init = true; 4906 } 4907 4908 bool zeroed_out = false; 4909 if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor)) 4910 { 4911 /* We're initializing an array object that had been zero-initialized 4912 earlier. Truncate ctx->ctor, and propagate its zeroed state by 4913 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element 4914 initializers we append to it. */ 4915 gcc_checking_assert (initializer_zerop (ctx->ctor)); 4916 zeroed_out = true; 4917 vec_safe_truncate (*p, 0); 4918 } 4919 4920 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, 4921 overflow_p); 4922 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); 4923 for (i = 0; i < max; ++i) 4924 { 4925 tree idx = build_int_cst (size_type_node, i); 4926 tree eltinit; 4927 bool reuse = false; 4928 constexpr_ctx new_ctx; 4929 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); 4930 if (new_ctx.ctor != ctx->ctor) 4931 { 4932 if (zeroed_out) 4933 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; 4934 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); 4935 } 4936 if (TREE_CODE (elttype) == ARRAY_TYPE) 4937 { 4938 /* A multidimensional array; recurse. */ 4939 if (value_init || init == NULL_TREE) 4940 { 4941 eltinit = NULL_TREE; 4942 reuse = i == 0; 4943 } 4944 else 4945 eltinit = cp_build_array_ref (input_location, init, idx, complain); 4946 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, 4947 lval, 4948 non_constant_p, overflow_p); 4949 } 4950 else if (pre_init) 4951 { 4952 /* Initializing an element using value or default initialization 4953 we just pre-built above. */ 4954 if (init == void_node) 4955 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ 4956 return ctx->ctor; 4957 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, 4958 non_constant_p, overflow_p); 4959 reuse = i == 0; 4960 } 4961 else 4962 { 4963 /* Copying an element. */ 4964 gcc_assert (same_type_ignoring_top_level_qualifiers_p 4965 (atype, TREE_TYPE (init))); 4966 eltinit = cp_build_array_ref (input_location, init, idx, complain); 4967 if (!lvalue_p (init)) 4968 eltinit = move (eltinit); 4969 eltinit = force_rvalue (eltinit, complain); 4970 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, 4971 non_constant_p, overflow_p); 4972 } 4973 if (*non_constant_p) 4974 break; 4975 if (new_ctx.ctor != ctx->ctor) 4976 { 4977 /* We appended this element above; update the value. */ 4978 gcc_assert ((*p)->last().index == idx); 4979 (*p)->last().value = eltinit; 4980 } 4981 else 4982 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); 4983 /* Reuse the result of cxx_eval_constant_expression call 4984 from the first iteration to all others if it is a constant 4985 initializer that doesn't require relocations. */ 4986 if (reuse 4987 && max > 1 4988 && (eltinit == NULL_TREE 4989 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) 4990 == null_pointer_node))) 4991 { 4992 if (new_ctx.ctor != ctx->ctor) 4993 eltinit = new_ctx.ctor; 4994 tree range = build2 (RANGE_EXPR, size_type_node, 4995 build_int_cst (size_type_node, 1), 4996 build_int_cst (size_type_node, max - 1)); 4997 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); 4998 break; 4999 } 5000 else if (i == 0) 5001 vec_safe_reserve (*p, max); 5002 } 5003 5004 if (!*non_constant_p) 5005 { 5006 init = ctx->ctor; 5007 CONSTRUCTOR_NO_CLEARING (init) = false; 5008 } 5009 return init; 5010} 5011 5012static tree 5013cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, 5014 bool lval, 5015 bool *non_constant_p, bool *overflow_p) 5016{ 5017 tree atype = TREE_TYPE (t); 5018 tree init = VEC_INIT_EXPR_INIT (t); 5019 bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); 5020 if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) 5021 ; 5022 else if (CONSTRUCTOR_NELTS (init) == 0 5023 && !CP_AGGREGATE_TYPE_P (strip_array_types (atype))) 5024 { 5025 /* Handle {} as value-init. */ 5026 init = NULL_TREE; 5027 value_init = true; 5028 } 5029 else 5030 { 5031 /* This is a more complicated case, like needing to loop over trailing 5032 elements; call build_vec_init and evaluate the result. */ 5033 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; 5034 constexpr_ctx new_ctx = *ctx; 5035 if (!ctx->object) 5036 { 5037 /* We want to have an initialization target for an VEC_INIT_EXPR. 5038 If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */ 5039 new_ctx.object = VEC_INIT_EXPR_SLOT (t); 5040 tree ctor = new_ctx.ctor = build_constructor (atype, NULL); 5041 CONSTRUCTOR_NO_CLEARING (ctor) = true; 5042 ctx->global->values.put (new_ctx.object, ctor); 5043 ctx = &new_ctx; 5044 } 5045 init = expand_vec_init_expr (ctx->object, t, complain); 5046 return cxx_eval_constant_expression (ctx, init, lval, non_constant_p, 5047 overflow_p); 5048 } 5049 tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init, 5050 lval, non_constant_p, overflow_p); 5051 if (*non_constant_p) 5052 return t; 5053 else 5054 return r; 5055} 5056 5057/* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case 5058 where the desired type is an array of unknown bounds because the variable 5059 has had its bounds deduced since the wrapping expression was created. */ 5060 5061static bool 5062same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) 5063{ 5064 while (TREE_CODE (type1) == ARRAY_TYPE 5065 && TREE_CODE (type2) == ARRAY_TYPE 5066 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) 5067 { 5068 type1 = TREE_TYPE (type1); 5069 type2 = TREE_TYPE (type2); 5070 } 5071 return same_type_ignoring_top_level_qualifiers_p (type1, type2); 5072} 5073 5074/* Try to determine the currently active union member for an expression 5075 with UNION_TYPE. If it can be determined, return the FIELD_DECL, 5076 otherwise return NULL_TREE. */ 5077 5078static tree 5079cxx_union_active_member (const constexpr_ctx *ctx, tree t) 5080{ 5081 constexpr_ctx new_ctx = *ctx; 5082 new_ctx.quiet = true; 5083 bool non_constant_p = false, overflow_p = false; 5084 tree ctor = cxx_eval_constant_expression (&new_ctx, t, false, 5085 &non_constant_p, 5086 &overflow_p); 5087 if (TREE_CODE (ctor) == CONSTRUCTOR 5088 && CONSTRUCTOR_NELTS (ctor) == 1 5089 && CONSTRUCTOR_ELT (ctor, 0)->index 5090 && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL) 5091 return CONSTRUCTOR_ELT (ctor, 0)->index; 5092 return NULL_TREE; 5093} 5094 5095/* Helper function for cxx_fold_indirect_ref_1, called recursively. */ 5096 5097static tree 5098cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, 5099 tree op, unsigned HOST_WIDE_INT off, bool *empty_base) 5100{ 5101 tree optype = TREE_TYPE (op); 5102 unsigned HOST_WIDE_INT const_nunits; 5103 if (off == 0 && similar_type_p (optype, type)) 5104 return op; 5105 else if (TREE_CODE (optype) == COMPLEX_TYPE 5106 && similar_type_p (type, TREE_TYPE (optype))) 5107 { 5108 /* *(foo *)&complexfoo => __real__ complexfoo */ 5109 if (off == 0) 5110 return build1_loc (loc, REALPART_EXPR, type, op); 5111 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ 5112 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) 5113 return build1_loc (loc, IMAGPART_EXPR, type, op); 5114 } 5115 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ 5116 else if (VECTOR_TYPE_P (optype) 5117 && similar_type_p (type, TREE_TYPE (optype)) 5118 && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits)) 5119 { 5120 unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); 5121 unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; 5122 if (off < max_offset && off % part_width == 0) 5123 { 5124 tree index = bitsize_int (off * BITS_PER_UNIT); 5125 return build3_loc (loc, BIT_FIELD_REF, type, op, 5126 TYPE_SIZE (type), index); 5127 } 5128 } 5129 /* ((foo *)&fooarray)[x] => fooarray[x] */ 5130 else if (TREE_CODE (optype) == ARRAY_TYPE 5131 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) 5132 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) 5133 { 5134 tree type_domain = TYPE_DOMAIN (optype); 5135 tree min_val = size_zero_node; 5136 if (type_domain && TYPE_MIN_VALUE (type_domain)) 5137 min_val = TYPE_MIN_VALUE (type_domain); 5138 unsigned HOST_WIDE_INT el_sz 5139 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); 5140 unsigned HOST_WIDE_INT idx = off / el_sz; 5141 unsigned HOST_WIDE_INT rem = off % el_sz; 5142 if (tree_fits_uhwi_p (min_val)) 5143 { 5144 tree index = size_int (idx + tree_to_uhwi (min_val)); 5145 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index, 5146 NULL_TREE, NULL_TREE); 5147 return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, 5148 empty_base); 5149 } 5150 } 5151 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ 5152 else if (TREE_CODE (optype) == RECORD_TYPE 5153 || TREE_CODE (optype) == UNION_TYPE) 5154 { 5155 if (TREE_CODE (optype) == UNION_TYPE) 5156 /* For unions prefer the currently active member. */ 5157 if (tree field = cxx_union_active_member (ctx, op)) 5158 { 5159 unsigned HOST_WIDE_INT el_sz 5160 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); 5161 if (off < el_sz) 5162 { 5163 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), 5164 op, field, NULL_TREE); 5165 if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, 5166 off, empty_base)) 5167 return ret; 5168 } 5169 } 5170 for (tree field = TYPE_FIELDS (optype); 5171 field; field = DECL_CHAIN (field)) 5172 if (TREE_CODE (field) == FIELD_DECL 5173 && TREE_TYPE (field) != error_mark_node 5174 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) 5175 { 5176 tree pos = byte_position (field); 5177 if (!tree_fits_uhwi_p (pos)) 5178 continue; 5179 unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); 5180 unsigned HOST_WIDE_INT el_sz 5181 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); 5182 if (upos <= off && off < upos + el_sz) 5183 { 5184 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), 5185 op, field, NULL_TREE); 5186 if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, 5187 off - upos, 5188 empty_base)) 5189 return ret; 5190 } 5191 } 5192 /* Also handle conversion to an empty base class, which 5193 is represented with a NOP_EXPR. */ 5194 if (is_empty_class (type) 5195 && CLASS_TYPE_P (optype) 5196 && DERIVED_FROM_P (type, optype)) 5197 { 5198 *empty_base = true; 5199 return op; 5200 } 5201 } 5202 5203 return NULL_TREE; 5204} 5205 5206/* A less strict version of fold_indirect_ref_1, which requires cv-quals to 5207 match. We want to be less strict for simple *& folding; if we have a 5208 non-const temporary that we access through a const pointer, that should 5209 work. We handle this here rather than change fold_indirect_ref_1 5210 because we're dealing with things like ADDR_EXPR of INTEGER_CST which 5211 don't really make sense outside of constant expression evaluation. Also 5212 we want to allow folding to COMPONENT_REF, which could cause trouble 5213 with TBAA in fold_indirect_ref_1. */ 5214 5215static tree 5216cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, 5217 tree op0, bool *empty_base) 5218{ 5219 tree sub = op0; 5220 tree subtype; 5221 poly_uint64 const_op01; 5222 5223 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ 5224 while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR 5225 || TREE_CODE (sub) == VIEW_CONVERT_EXPR) 5226 { 5227 if (TREE_CODE (sub) == NOP_EXPR 5228 && REINTERPRET_CAST_P (sub)) 5229 return NULL_TREE; 5230 sub = TREE_OPERAND (sub, 0); 5231 } 5232 5233 subtype = TREE_TYPE (sub); 5234 if (!INDIRECT_TYPE_P (subtype)) 5235 return NULL_TREE; 5236 5237 /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing 5238 the innermost component into the offset until it would make the 5239 offset positive, so that cxx_fold_indirect_ref_1 can identify 5240 more folding opportunities. */ 5241 auto canonicalize_obj_off = [] (tree& obj, tree& off) { 5242 while (TREE_CODE (obj) == COMPONENT_REF 5243 && (tree_int_cst_sign_bit (off) || integer_zerop (off))) 5244 { 5245 tree field = TREE_OPERAND (obj, 1); 5246 tree pos = byte_position (field); 5247 if (integer_zerop (off) && integer_nonzerop (pos)) 5248 /* If the offset is already 0, keep going as long as the 5249 component is at position 0. */ 5250 break; 5251 off = int_const_binop (PLUS_EXPR, off, pos); 5252 obj = TREE_OPERAND (obj, 0); 5253 } 5254 }; 5255 5256 if (TREE_CODE (sub) == ADDR_EXPR) 5257 { 5258 tree op = TREE_OPERAND (sub, 0); 5259 tree optype = TREE_TYPE (op); 5260 5261 /* *&CONST_DECL -> to the value of the const decl. */ 5262 if (TREE_CODE (op) == CONST_DECL) 5263 return DECL_INITIAL (op); 5264 /* *&p => p; make sure to handle *&"str"[cst] here. */ 5265 if (similar_type_p (optype, type)) 5266 { 5267 tree fop = fold_read_from_constant_string (op); 5268 if (fop) 5269 return fop; 5270 else 5271 return op; 5272 } 5273 else 5274 { 5275 tree off = integer_zero_node; 5276 canonicalize_obj_off (op, off); 5277 gcc_assert (integer_zerop (off)); 5278 return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); 5279 } 5280 } 5281 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR 5282 && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) 5283 { 5284 tree op00 = TREE_OPERAND (sub, 0); 5285 tree off = TREE_OPERAND (sub, 1); 5286 5287 STRIP_NOPS (op00); 5288 if (TREE_CODE (op00) == ADDR_EXPR) 5289 { 5290 tree obj = TREE_OPERAND (op00, 0); 5291 canonicalize_obj_off (obj, off); 5292 return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, 5293 tree_to_uhwi (off), empty_base); 5294 } 5295 } 5296 /* *(foo *)fooarrptr => (*fooarrptr)[0] */ 5297 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE 5298 && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) 5299 { 5300 tree type_domain; 5301 tree min_val = size_zero_node; 5302 tree newsub 5303 = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); 5304 if (newsub) 5305 sub = newsub; 5306 else 5307 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); 5308 type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); 5309 if (type_domain && TYPE_MIN_VALUE (type_domain)) 5310 min_val = TYPE_MIN_VALUE (type_domain); 5311 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, 5312 NULL_TREE); 5313 } 5314 5315 return NULL_TREE; 5316} 5317 5318static tree 5319cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, 5320 bool lval, 5321 bool *non_constant_p, bool *overflow_p) 5322{ 5323 tree orig_op0 = TREE_OPERAND (t, 0); 5324 bool empty_base = false; 5325 5326 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second 5327 operand is an integer-zero. Otherwise reject the MEM_REF for now. */ 5328 5329 if (TREE_CODE (t) == MEM_REF 5330 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) 5331 { 5332 gcc_assert (ctx->quiet); 5333 *non_constant_p = true; 5334 return t; 5335 } 5336 5337 /* First try to simplify it directly. */ 5338 tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), 5339 orig_op0, &empty_base); 5340 if (!r) 5341 { 5342 /* If that didn't work, evaluate the operand first. */ 5343 tree op0 = cxx_eval_constant_expression (ctx, orig_op0, 5344 /*lval*/false, non_constant_p, 5345 overflow_p); 5346 /* Don't VERIFY_CONSTANT here. */ 5347 if (*non_constant_p) 5348 return t; 5349 5350 if (!lval && integer_zerop (op0)) 5351 { 5352 if (!ctx->quiet) 5353 error ("dereferencing a null pointer"); 5354 *non_constant_p = true; 5355 return t; 5356 } 5357 5358 r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, 5359 &empty_base); 5360 if (r == NULL_TREE) 5361 { 5362 /* We couldn't fold to a constant value. Make sure it's not 5363 something we should have been able to fold. */ 5364 tree sub = op0; 5365 STRIP_NOPS (sub); 5366 if (TREE_CODE (sub) == ADDR_EXPR) 5367 { 5368 gcc_assert (!similar_type_p 5369 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); 5370 /* DR 1188 says we don't have to deal with this. */ 5371 if (!ctx->quiet) 5372 error_at (cp_expr_loc_or_input_loc (t), 5373 "accessing value of %qE through a %qT glvalue in a " 5374 "constant expression", build_fold_indirect_ref (sub), 5375 TREE_TYPE (t)); 5376 *non_constant_p = true; 5377 return t; 5378 } 5379 5380 if (lval && op0 != orig_op0) 5381 return build1 (INDIRECT_REF, TREE_TYPE (t), op0); 5382 if (!lval) 5383 VERIFY_CONSTANT (t); 5384 return t; 5385 } 5386 } 5387 5388 r = cxx_eval_constant_expression (ctx, r, 5389 lval, non_constant_p, overflow_p); 5390 if (*non_constant_p) 5391 return t; 5392 5393 /* If we're pulling out the value of an empty base, just return an empty 5394 CONSTRUCTOR. */ 5395 if (empty_base && !lval) 5396 { 5397 r = build_constructor (TREE_TYPE (t), NULL); 5398 TREE_CONSTANT (r) = true; 5399 } 5400 5401 return r; 5402} 5403 5404/* Complain about R, a VAR_DECL, not being usable in a constant expression. 5405 Shared between potential_constant_expression and 5406 cxx_eval_constant_expression. */ 5407 5408static void 5409non_const_var_error (location_t loc, tree r) 5410{ 5411 auto_diagnostic_group d; 5412 tree type = TREE_TYPE (r); 5413 if (DECL_NAME (r) == heap_uninit_identifier 5414 || DECL_NAME (r) == heap_identifier 5415 || DECL_NAME (r) == heap_vec_uninit_identifier 5416 || DECL_NAME (r) == heap_vec_identifier) 5417 { 5418 error_at (loc, "the content of uninitialized storage is not usable " 5419 "in a constant expression"); 5420 inform (DECL_SOURCE_LOCATION (r), "allocated here"); 5421 return; 5422 } 5423 if (DECL_NAME (r) == heap_deleted_identifier) 5424 { 5425 error_at (loc, "use of allocated storage after deallocation in a " 5426 "constant expression"); 5427 inform (DECL_SOURCE_LOCATION (r), "allocated here"); 5428 return; 5429 } 5430 error_at (loc, "the value of %qD is not usable in a constant " 5431 "expression", r); 5432 /* Avoid error cascade. */ 5433 if (DECL_INITIAL (r) == error_mark_node) 5434 return; 5435 if (DECL_DECLARED_CONSTEXPR_P (r)) 5436 inform (DECL_SOURCE_LOCATION (r), 5437 "%qD used in its own initializer", r); 5438 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 5439 { 5440 if (!CP_TYPE_CONST_P (type)) 5441 inform (DECL_SOURCE_LOCATION (r), 5442 "%q#D is not const", r); 5443 else if (CP_TYPE_VOLATILE_P (type)) 5444 inform (DECL_SOURCE_LOCATION (r), 5445 "%q#D is volatile", r); 5446 else if (!DECL_INITIAL (r) 5447 || !TREE_CONSTANT (DECL_INITIAL (r)) 5448 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) 5449 inform (DECL_SOURCE_LOCATION (r), 5450 "%qD was not initialized with a constant " 5451 "expression", r); 5452 else 5453 gcc_unreachable (); 5454 } 5455 else if (TYPE_REF_P (type)) 5456 inform (DECL_SOURCE_LOCATION (r), 5457 "%qD was not initialized with a constant " 5458 "expression", r); 5459 else 5460 { 5461 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) 5462 inform (DECL_SOURCE_LOCATION (r), 5463 "%qD was not declared %<constexpr%>", r); 5464 else 5465 inform (DECL_SOURCE_LOCATION (r), 5466 "%qD does not have integral or enumeration type", 5467 r); 5468 } 5469} 5470 5471/* Subroutine of cxx_eval_constant_expression. 5472 Like cxx_eval_unary_expression, except for trinary expressions. */ 5473 5474static tree 5475cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, 5476 bool lval, 5477 bool *non_constant_p, bool *overflow_p) 5478{ 5479 int i; 5480 tree args[3]; 5481 tree val; 5482 5483 for (i = 0; i < 3; i++) 5484 { 5485 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), 5486 lval, 5487 non_constant_p, overflow_p); 5488 VERIFY_CONSTANT (args[i]); 5489 } 5490 5491 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), 5492 args[0], args[1], args[2]); 5493 if (val == NULL_TREE) 5494 return t; 5495 VERIFY_CONSTANT (val); 5496 return val; 5497} 5498 5499/* True if T was declared in a function declared to be constexpr, and 5500 therefore potentially constant in C++14. */ 5501 5502bool 5503var_in_constexpr_fn (tree t) 5504{ 5505 tree ctx = DECL_CONTEXT (t); 5506 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL 5507 && DECL_DECLARED_CONSTEXPR_P (ctx)); 5508} 5509 5510/* True if a function might be constexpr: either a function that was 5511 declared constexpr, or a C++17 lambda op(). */ 5512 5513bool 5514maybe_constexpr_fn (tree t) 5515{ 5516 return (DECL_DECLARED_CONSTEXPR_P (t) 5517 || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t)) 5518 || (flag_implicit_constexpr 5519 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t)))); 5520} 5521 5522/* True if T was declared in a function that might be constexpr: either a 5523 function that was declared constexpr, or a C++17 lambda op(). */ 5524 5525bool 5526var_in_maybe_constexpr_fn (tree t) 5527{ 5528 return (DECL_FUNCTION_SCOPE_P (t) 5529 && maybe_constexpr_fn (DECL_CONTEXT (t))); 5530} 5531 5532/* We're assigning INIT to TARGET. In do_build_copy_constructor and 5533 build_over_call we implement trivial copy of a class with tail padding using 5534 assignment of character arrays, which is valid in normal code, but not in 5535 constexpr evaluation. We don't need to worry about clobbering tail padding 5536 in constexpr evaluation, so strip the type punning. */ 5537 5538static void 5539maybe_simplify_trivial_copy (tree &target, tree &init) 5540{ 5541 if (TREE_CODE (target) == MEM_REF 5542 && TREE_CODE (init) == MEM_REF 5543 && TREE_TYPE (target) == TREE_TYPE (init) 5544 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE 5545 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) 5546 { 5547 target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); 5548 init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); 5549 } 5550} 5551 5552/* Returns true if REF, which is a COMPONENT_REF, has any fields 5553 of constant type. This does not check for 'mutable', so the 5554 caller is expected to be mindful of that. */ 5555 5556static bool 5557cref_has_const_field (tree ref) 5558{ 5559 while (TREE_CODE (ref) == COMPONENT_REF) 5560 { 5561 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) 5562 return true; 5563 ref = TREE_OPERAND (ref, 0); 5564 } 5565 return false; 5566} 5567 5568/* Return true if we are modifying something that is const during constant 5569 expression evaluation. CODE is the code of the statement, OBJ is the 5570 object in question, MUTABLE_P is true if one of the subobjects were 5571 declared mutable. */ 5572 5573static bool 5574modifying_const_object_p (tree_code code, tree obj, bool mutable_p) 5575{ 5576 /* If this is initialization, there's no problem. */ 5577 if (code != MODIFY_EXPR) 5578 return false; 5579 5580 /* [basic.type.qualifier] "A const object is an object of type 5581 const T or a non-mutable subobject of a const object." */ 5582 if (mutable_p) 5583 return false; 5584 5585 if (TREE_READONLY (obj)) 5586 return true; 5587 5588 if (CP_TYPE_CONST_P (TREE_TYPE (obj))) 5589 { 5590 /* Although a COMPONENT_REF may have a const type, we should 5591 only consider it modifying a const object when any of the 5592 field components is const. This can happen when using 5593 constructs such as const_cast<const T &>(m), making something 5594 const even though it wasn't declared const. */ 5595 if (TREE_CODE (obj) == COMPONENT_REF) 5596 return cref_has_const_field (obj); 5597 else 5598 return true; 5599 } 5600 5601 return false; 5602} 5603 5604/* Evaluate an INIT_EXPR or MODIFY_EXPR. */ 5605 5606static tree 5607cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, 5608 bool lval, 5609 bool *non_constant_p, bool *overflow_p) 5610{ 5611 constexpr_ctx new_ctx = *ctx; 5612 5613 tree init = TREE_OPERAND (t, 1); 5614 if (TREE_CLOBBER_P (init)) 5615 /* Just ignore clobbers. */ 5616 return void_node; 5617 5618 /* First we figure out where we're storing to. */ 5619 tree target = TREE_OPERAND (t, 0); 5620 5621 maybe_simplify_trivial_copy (target, init); 5622 5623 tree type = TREE_TYPE (target); 5624 bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; 5625 if (preeval) 5626 { 5627 /* Evaluate the value to be stored without knowing what object it will be 5628 stored in, so that any side-effects happen first. */ 5629 if (!SCALAR_TYPE_P (type)) 5630 new_ctx.ctor = new_ctx.object = NULL_TREE; 5631 init = cxx_eval_constant_expression (&new_ctx, init, false, 5632 non_constant_p, overflow_p); 5633 if (*non_constant_p) 5634 return t; 5635 } 5636 5637 bool evaluated = false; 5638 if (lval) 5639 { 5640 /* If we want to return a reference to the target, we need to evaluate it 5641 as a whole; otherwise, only evaluate the innermost piece to avoid 5642 building up unnecessary *_REFs. */ 5643 target = cxx_eval_constant_expression (ctx, target, true, 5644 non_constant_p, overflow_p); 5645 evaluated = true; 5646 if (*non_constant_p) 5647 return t; 5648 } 5649 5650 /* Find the underlying variable. */ 5651 releasing_vec refs; 5652 tree object = NULL_TREE; 5653 /* If we're modifying a const object, save it. */ 5654 tree const_object_being_modified = NULL_TREE; 5655 bool mutable_p = false; 5656 for (tree probe = target; object == NULL_TREE; ) 5657 { 5658 switch (TREE_CODE (probe)) 5659 { 5660 case BIT_FIELD_REF: 5661 case COMPONENT_REF: 5662 case ARRAY_REF: 5663 { 5664 tree ob = TREE_OPERAND (probe, 0); 5665 tree elt = TREE_OPERAND (probe, 1); 5666 if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) 5667 mutable_p = true; 5668 if (TREE_CODE (probe) == ARRAY_REF) 5669 { 5670 elt = eval_and_check_array_index (ctx, probe, false, 5671 non_constant_p, overflow_p); 5672 if (*non_constant_p) 5673 return t; 5674 } 5675 /* We don't check modifying_const_object_p for ARRAY_REFs. Given 5676 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though 5677 the array isn't const. Instead, check "a" in the next iteration; 5678 that will detect modifying "const int a[10]". */ 5679 else if (evaluated 5680 && modifying_const_object_p (TREE_CODE (t), probe, 5681 mutable_p) 5682 && const_object_being_modified == NULL_TREE) 5683 const_object_being_modified = probe; 5684 vec_safe_push (refs, elt); 5685 vec_safe_push (refs, TREE_TYPE (probe)); 5686 probe = ob; 5687 } 5688 break; 5689 5690 default: 5691 if (evaluated) 5692 object = probe; 5693 else 5694 { 5695 probe = cxx_eval_constant_expression (ctx, probe, true, 5696 non_constant_p, overflow_p); 5697 evaluated = true; 5698 if (*non_constant_p) 5699 return t; 5700 } 5701 break; 5702 } 5703 } 5704 5705 if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) 5706 && const_object_being_modified == NULL_TREE) 5707 const_object_being_modified = object; 5708 5709 /* And then find/build up our initializer for the path to the subobject 5710 we're initializing. */ 5711 tree *valp; 5712 if (DECL_P (object)) 5713 valp = ctx->global->values.get (object); 5714 else 5715 valp = NULL; 5716 if (!valp) 5717 { 5718 /* A constant-expression cannot modify objects from outside the 5719 constant-expression. */ 5720 if (!ctx->quiet) 5721 error ("modification of %qE is not a constant expression", object); 5722 *non_constant_p = true; 5723 return t; 5724 } 5725 type = TREE_TYPE (object); 5726 bool no_zero_init = true; 5727 5728 releasing_vec ctors, indexes; 5729 auto_vec<int> index_pos_hints; 5730 bool activated_union_member_p = false; 5731 while (!refs->is_empty ()) 5732 { 5733 if (*valp == NULL_TREE) 5734 { 5735 *valp = build_constructor (type, NULL); 5736 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; 5737 } 5738 else if (STRIP_ANY_LOCATION_WRAPPER (*valp), 5739 TREE_CODE (*valp) == STRING_CST) 5740 { 5741 /* An array was initialized with a string constant, and now 5742 we're writing into one of its elements. Explode the 5743 single initialization into a set of element 5744 initializations. */ 5745 gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 5746 5747 tree string = *valp; 5748 tree elt_type = TREE_TYPE (type); 5749 unsigned chars_per_elt = (TYPE_PRECISION (elt_type) 5750 / TYPE_PRECISION (char_type_node)); 5751 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; 5752 tree ary_ctor = build_constructor (type, NULL); 5753 5754 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); 5755 for (unsigned ix = 0; ix != num_elts; ix++) 5756 { 5757 constructor_elt elt = 5758 { 5759 build_int_cst (size_type_node, ix), 5760 extract_string_elt (string, chars_per_elt, ix) 5761 }; 5762 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); 5763 } 5764 5765 *valp = ary_ctor; 5766 } 5767 5768 /* If the value of object is already zero-initialized, any new ctors for 5769 subobjects will also be zero-initialized. */ 5770 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); 5771 5772 enum tree_code code = TREE_CODE (type); 5773 type = refs->pop(); 5774 tree index = refs->pop(); 5775 5776 if (code == RECORD_TYPE && is_empty_field (index)) 5777 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they 5778 have no data and might have an offset lower than previously declared 5779 fields, which confuses the middle-end. The code below will notice 5780 that we don't have a CONSTRUCTOR for our inner target and just 5781 return init. */ 5782 break; 5783 5784 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) 5785 && CONSTRUCTOR_ELT (*valp, 0)->index != index) 5786 { 5787 if (cxx_dialect < cxx20) 5788 { 5789 if (!ctx->quiet) 5790 error_at (cp_expr_loc_or_input_loc (t), 5791 "change of the active member of a union " 5792 "from %qD to %qD", 5793 CONSTRUCTOR_ELT (*valp, 0)->index, 5794 index); 5795 *non_constant_p = true; 5796 } 5797 else if (TREE_CODE (t) == MODIFY_EXPR 5798 && CONSTRUCTOR_NO_CLEARING (*valp)) 5799 { 5800 /* Diagnose changing the active union member while the union 5801 is in the process of being initialized. */ 5802 if (!ctx->quiet) 5803 error_at (cp_expr_loc_or_input_loc (t), 5804 "change of the active member of a union " 5805 "from %qD to %qD during initialization", 5806 CONSTRUCTOR_ELT (*valp, 0)->index, 5807 index); 5808 *non_constant_p = true; 5809 } 5810 no_zero_init = true; 5811 } 5812 5813 vec_safe_push (ctors, *valp); 5814 vec_safe_push (indexes, index); 5815 5816 constructor_elt *cep 5817 = get_or_insert_ctor_field (*valp, index); 5818 index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); 5819 5820 if (code == UNION_TYPE) 5821 activated_union_member_p = true; 5822 5823 valp = &cep->value; 5824 } 5825 5826 /* Detect modifying a constant object in constexpr evaluation. 5827 We have found a const object that is being modified. Figure out 5828 if we need to issue an error. Consider 5829 5830 struct A { 5831 int n; 5832 constexpr A() : n(1) { n = 2; } // #1 5833 }; 5834 struct B { 5835 const A a; 5836 constexpr B() { a.n = 3; } // #2 5837 }; 5838 constexpr B b{}; 5839 5840 #1 is OK, since we're modifying an object under construction, but 5841 #2 is wrong, since "a" is const and has been fully constructed. 5842 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR 5843 which means that the object is read-only. For the example above, the 5844 *ctors stack at the point of #2 will look like: 5845 5846 ctors[0] = {.a={.n=2}} TREE_READONLY = 0 5847 ctors[1] = {.n=2} TREE_READONLY = 1 5848 5849 and we're modifying "b.a", so we search the stack and see if the 5850 constructor for "b.a" has already run. */ 5851 if (const_object_being_modified) 5852 { 5853 bool fail = false; 5854 tree const_objtype 5855 = strip_array_types (TREE_TYPE (const_object_being_modified)); 5856 if (!CLASS_TYPE_P (const_objtype)) 5857 fail = true; 5858 else 5859 { 5860 /* [class.ctor]p5 "A constructor can be invoked for a const, 5861 volatile, or const volatile object. const and volatile 5862 semantics are not applied on an object under construction. 5863 They come into effect when the constructor for the most 5864 derived object ends." */ 5865 for (tree elt : *ctors) 5866 if (same_type_ignoring_top_level_qualifiers_p 5867 (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) 5868 { 5869 fail = TREE_READONLY (elt); 5870 break; 5871 } 5872 } 5873 if (fail) 5874 { 5875 if (!ctx->quiet) 5876 modifying_const_object_error (t, const_object_being_modified); 5877 *non_constant_p = true; 5878 return t; 5879 } 5880 } 5881 5882 if (!preeval) 5883 { 5884 /* We're handling an INIT_EXPR of class type, so the value of the 5885 initializer can depend on the object it's initializing. */ 5886 5887 /* Create a new CONSTRUCTOR in case evaluation of the initializer 5888 wants to modify it. */ 5889 if (*valp == NULL_TREE) 5890 { 5891 *valp = build_constructor (type, NULL); 5892 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; 5893 } 5894 new_ctx.ctor = *valp; 5895 new_ctx.object = target; 5896 /* Avoid temporary materialization when initializing from a TARGET_EXPR. 5897 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because 5898 expansion of those trees uses ctx instead. */ 5899 if (TREE_CODE (init) == TARGET_EXPR) 5900 if (tree tinit = TARGET_EXPR_INITIAL (init)) 5901 init = tinit; 5902 init = cxx_eval_constant_expression (&new_ctx, init, false, 5903 non_constant_p, overflow_p); 5904 /* The hash table might have moved since the get earlier, and the 5905 initializer might have mutated the underlying CONSTRUCTORs, so we must 5906 recompute VALP. */ 5907 valp = ctx->global->values.get (object); 5908 for (unsigned i = 0; i < vec_safe_length (indexes); i++) 5909 { 5910 constructor_elt *cep 5911 = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); 5912 valp = &cep->value; 5913 } 5914 } 5915 5916 /* Don't share a CONSTRUCTOR that might be changed later. */ 5917 init = unshare_constructor (init); 5918 5919 if (*valp && TREE_CODE (*valp) == CONSTRUCTOR 5920 && TREE_CODE (init) == CONSTRUCTOR) 5921 { 5922 /* An outer ctx->ctor might be pointing to *valp, so replace 5923 its contents. */ 5924 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), 5925 TREE_TYPE (*valp))) 5926 { 5927 /* For initialization of an empty base, the original target will be 5928 *(base*)this, evaluation of which resolves to the object 5929 argument, which has the derived type rather than the base type. In 5930 this situation, just evaluate the initializer and return, since 5931 there's no actual data to store. */ 5932 gcc_assert (is_empty_class (TREE_TYPE (init))); 5933 return lval ? target : init; 5934 } 5935 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); 5936 TREE_CONSTANT (*valp) = TREE_CONSTANT (init); 5937 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); 5938 CONSTRUCTOR_NO_CLEARING (*valp) 5939 = CONSTRUCTOR_NO_CLEARING (init); 5940 } 5941 else if (TREE_CODE (init) == CONSTRUCTOR 5942 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), 5943 type)) 5944 { 5945 /* See above on initialization of empty bases. */ 5946 gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval); 5947 if (!*valp) 5948 { 5949 /* But do make sure we have something in *valp. */ 5950 *valp = build_constructor (type, nullptr); 5951 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; 5952 } 5953 return init; 5954 } 5955 else 5956 *valp = init; 5957 5958 /* After initialization, 'const' semantics apply to the value of the 5959 object. Make a note of this fact by marking the CONSTRUCTOR 5960 TREE_READONLY. */ 5961 if (TREE_CODE (t) == INIT_EXPR 5962 && TREE_CODE (*valp) == CONSTRUCTOR 5963 && TYPE_READONLY (type)) 5964 { 5965 if (INDIRECT_REF_P (target) 5966 && (is_this_parameter 5967 (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) 5968 /* We've just initialized '*this' (perhaps via the target 5969 constructor of a delegating constructor). Leave it up to the 5970 caller that set 'this' to set TREE_READONLY appropriately. */ 5971 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p 5972 (TREE_TYPE (target), type)); 5973 else 5974 TREE_READONLY (*valp) = true; 5975 } 5976 5977 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing 5978 CONSTRUCTORs, if any. */ 5979 bool c = TREE_CONSTANT (init); 5980 bool s = TREE_SIDE_EFFECTS (init); 5981 if (!c || s || activated_union_member_p) 5982 for (tree elt : *ctors) 5983 { 5984 if (!c) 5985 TREE_CONSTANT (elt) = false; 5986 if (s) 5987 TREE_SIDE_EFFECTS (elt) = true; 5988 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of 5989 this union. */ 5990 if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE) 5991 CONSTRUCTOR_NO_CLEARING (elt) = false; 5992 } 5993 5994 if (*non_constant_p) 5995 return t; 5996 else if (lval) 5997 return target; 5998 else 5999 return init; 6000} 6001 6002/* Evaluate a ++ or -- expression. */ 6003 6004static tree 6005cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, 6006 bool lval, 6007 bool *non_constant_p, bool *overflow_p) 6008{ 6009 enum tree_code code = TREE_CODE (t); 6010 tree type = TREE_TYPE (t); 6011 tree op = TREE_OPERAND (t, 0); 6012 tree offset = TREE_OPERAND (t, 1); 6013 gcc_assert (TREE_CONSTANT (offset)); 6014 6015 /* OFFSET is constant, but perhaps not constant enough. We need to 6016 e.g. bash FLOAT_EXPRs to REAL_CSTs. */ 6017 offset = fold_simple (offset); 6018 6019 /* The operand as an lvalue. */ 6020 op = cxx_eval_constant_expression (ctx, op, true, 6021 non_constant_p, overflow_p); 6022 6023 /* The operand as an rvalue. */ 6024 tree val 6025 = cxx_eval_constant_expression (ctx, op, false, 6026 non_constant_p, overflow_p); 6027 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to 6028 a local array in a constexpr function. */ 6029 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); 6030 if (!ptr) 6031 VERIFY_CONSTANT (val); 6032 6033 /* The modified value. */ 6034 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); 6035 tree mod; 6036 if (INDIRECT_TYPE_P (type)) 6037 { 6038 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ 6039 offset = convert_to_ptrofftype (offset); 6040 if (!inc) 6041 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); 6042 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); 6043 } 6044 else if (c_promoting_integer_type_p (type) 6045 && !TYPE_UNSIGNED (type) 6046 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) 6047 { 6048 offset = fold_convert (integer_type_node, offset); 6049 mod = fold_convert (integer_type_node, val); 6050 tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node, 6051 mod, offset); 6052 mod = fold_convert (type, t); 6053 if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t)) 6054 TREE_OVERFLOW (mod) = false; 6055 } 6056 else 6057 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); 6058 if (!ptr) 6059 VERIFY_CONSTANT (mod); 6060 6061 /* Storing the modified value. */ 6062 tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), 6063 MODIFY_EXPR, type, op, mod); 6064 mod = cxx_eval_constant_expression (ctx, store, lval, 6065 non_constant_p, overflow_p); 6066 ggc_free (store); 6067 if (*non_constant_p) 6068 return t; 6069 6070 /* And the value of the expression. */ 6071 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) 6072 /* Prefix ops are lvalues, but the caller might want an rvalue; 6073 lval has already been taken into account in the store above. */ 6074 return mod; 6075 else 6076 /* Postfix ops are rvalues. */ 6077 return val; 6078} 6079 6080/* Predicates for the meaning of *jump_target. */ 6081 6082static bool 6083returns (tree *jump_target) 6084{ 6085 return *jump_target 6086 && (TREE_CODE (*jump_target) == RETURN_EXPR 6087 || (TREE_CODE (*jump_target) == LABEL_DECL 6088 && LABEL_DECL_CDTOR (*jump_target))); 6089} 6090 6091static bool 6092breaks (tree *jump_target) 6093{ 6094 return *jump_target 6095 && ((TREE_CODE (*jump_target) == LABEL_DECL 6096 && LABEL_DECL_BREAK (*jump_target)) 6097 || TREE_CODE (*jump_target) == BREAK_STMT 6098 || TREE_CODE (*jump_target) == EXIT_EXPR); 6099} 6100 6101static bool 6102continues (tree *jump_target) 6103{ 6104 return *jump_target 6105 && ((TREE_CODE (*jump_target) == LABEL_DECL 6106 && LABEL_DECL_CONTINUE (*jump_target)) 6107 || TREE_CODE (*jump_target) == CONTINUE_STMT); 6108 6109} 6110 6111static bool 6112switches (tree *jump_target) 6113{ 6114 return *jump_target 6115 && TREE_CODE (*jump_target) == INTEGER_CST; 6116} 6117 6118/* Subroutine of cxx_eval_statement_list. Determine whether the statement 6119 STMT matches *jump_target. If we're looking for a case label and we see 6120 the default label, note it in ctx->css_state. */ 6121 6122static bool 6123label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) 6124{ 6125 switch (TREE_CODE (*jump_target)) 6126 { 6127 case LABEL_DECL: 6128 if (TREE_CODE (stmt) == LABEL_EXPR 6129 && LABEL_EXPR_LABEL (stmt) == *jump_target) 6130 return true; 6131 break; 6132 6133 case INTEGER_CST: 6134 if (TREE_CODE (stmt) == CASE_LABEL_EXPR) 6135 { 6136 gcc_assert (ctx->css_state != NULL); 6137 if (!CASE_LOW (stmt)) 6138 { 6139 /* default: should appear just once in a SWITCH_EXPR 6140 body (excluding nested SWITCH_EXPR). */ 6141 gcc_assert (*ctx->css_state != css_default_seen); 6142 /* When evaluating SWITCH_EXPR body for the second time, 6143 return true for the default: label. */ 6144 if (*ctx->css_state == css_default_processing) 6145 return true; 6146 *ctx->css_state = css_default_seen; 6147 } 6148 else if (CASE_HIGH (stmt)) 6149 { 6150 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) 6151 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) 6152 return true; 6153 } 6154 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) 6155 return true; 6156 } 6157 break; 6158 6159 case BREAK_STMT: 6160 case CONTINUE_STMT: 6161 /* These two are handled directly in cxx_eval_loop_expr by testing 6162 breaks (jump_target) or continues (jump_target). */ 6163 break; 6164 6165 default: 6166 gcc_unreachable (); 6167 } 6168 return false; 6169} 6170 6171/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump 6172 semantics, for switch, break, continue, and return. */ 6173 6174static tree 6175cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, 6176 bool *non_constant_p, bool *overflow_p, 6177 tree *jump_target) 6178{ 6179 tree local_target; 6180 /* In a statement-expression we want to return the last value. 6181 For empty statement expression return void_node. */ 6182 tree r = void_node; 6183 if (!jump_target) 6184 { 6185 local_target = NULL_TREE; 6186 jump_target = &local_target; 6187 } 6188 for (tree stmt : tsi_range (t)) 6189 { 6190 /* We've found a continue, so skip everything until we reach 6191 the label its jumping to. */ 6192 if (continues (jump_target)) 6193 { 6194 if (label_matches (ctx, jump_target, stmt)) 6195 /* Found it. */ 6196 *jump_target = NULL_TREE; 6197 else 6198 continue; 6199 } 6200 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) 6201 continue; 6202 r = cxx_eval_constant_expression (ctx, stmt, false, 6203 non_constant_p, overflow_p, 6204 jump_target); 6205 if (*non_constant_p) 6206 break; 6207 if (returns (jump_target) || breaks (jump_target)) 6208 break; 6209 } 6210 if (*jump_target && jump_target == &local_target) 6211 { 6212 /* We aren't communicating the jump to our caller, so give up. We don't 6213 need to support evaluation of jumps out of statement-exprs. */ 6214 if (!ctx->quiet) 6215 error_at (cp_expr_loc_or_input_loc (r), 6216 "statement is not a constant expression"); 6217 *non_constant_p = true; 6218 } 6219 return r; 6220} 6221 6222/* Evaluate a LOOP_EXPR for side-effects. Handles break and return 6223 semantics; continue semantics are covered by cxx_eval_statement_list. */ 6224 6225static tree 6226cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, 6227 bool *non_constant_p, bool *overflow_p, 6228 tree *jump_target) 6229{ 6230 constexpr_ctx new_ctx = *ctx; 6231 tree local_target; 6232 if (!jump_target) 6233 { 6234 local_target = NULL_TREE; 6235 jump_target = &local_target; 6236 } 6237 6238 tree body, cond = NULL_TREE, expr = NULL_TREE; 6239 int count = 0; 6240 switch (TREE_CODE (t)) 6241 { 6242 case LOOP_EXPR: 6243 body = LOOP_EXPR_BODY (t); 6244 break; 6245 case DO_STMT: 6246 body = DO_BODY (t); 6247 cond = DO_COND (t); 6248 break; 6249 case WHILE_STMT: 6250 body = WHILE_BODY (t); 6251 cond = WHILE_COND (t); 6252 count = -1; 6253 break; 6254 case FOR_STMT: 6255 if (FOR_INIT_STMT (t)) 6256 cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false, 6257 non_constant_p, overflow_p, jump_target); 6258 if (*non_constant_p) 6259 return NULL_TREE; 6260 body = FOR_BODY (t); 6261 cond = FOR_COND (t); 6262 expr = FOR_EXPR (t); 6263 count = -1; 6264 break; 6265 default: 6266 gcc_unreachable (); 6267 } 6268 auto_vec<tree, 10> save_exprs; 6269 new_ctx.save_exprs = &save_exprs; 6270 do 6271 { 6272 if (count != -1) 6273 { 6274 if (body) 6275 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false, 6276 non_constant_p, overflow_p, 6277 jump_target); 6278 if (breaks (jump_target)) 6279 { 6280 *jump_target = NULL_TREE; 6281 break; 6282 } 6283 6284 if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) 6285 *jump_target = NULL_TREE; 6286 6287 if (expr) 6288 cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false, 6289 non_constant_p, overflow_p, 6290 jump_target); 6291 } 6292 6293 if (cond) 6294 { 6295 tree res 6296 = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false, 6297 non_constant_p, overflow_p, 6298 jump_target); 6299 if (res) 6300 { 6301 if (verify_constant (res, ctx->quiet, non_constant_p, 6302 overflow_p)) 6303 break; 6304 if (integer_zerop (res)) 6305 break; 6306 } 6307 else 6308 gcc_assert (*jump_target); 6309 } 6310 6311 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ 6312 for (tree save_expr : save_exprs) 6313 ctx->global->values.remove (save_expr); 6314 save_exprs.truncate (0); 6315 6316 if (++count >= constexpr_loop_limit) 6317 { 6318 if (!ctx->quiet) 6319 error_at (cp_expr_loc_or_input_loc (t), 6320 "%<constexpr%> loop iteration count exceeds limit of %d " 6321 "(use %<-fconstexpr-loop-limit=%> to increase the limit)", 6322 constexpr_loop_limit); 6323 *non_constant_p = true; 6324 break; 6325 } 6326 } 6327 while (!returns (jump_target) 6328 && !breaks (jump_target) 6329 && !continues (jump_target) 6330 && (!switches (jump_target) || count == 0) 6331 && !*non_constant_p); 6332 6333 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ 6334 for (tree save_expr : save_exprs) 6335 ctx->global->values.remove (save_expr); 6336 6337 return NULL_TREE; 6338} 6339 6340/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump 6341 semantics. */ 6342 6343static tree 6344cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, 6345 bool *non_constant_p, bool *overflow_p, 6346 tree *jump_target) 6347{ 6348 tree cond 6349 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); 6350 cond = cxx_eval_constant_expression (ctx, cond, false, 6351 non_constant_p, overflow_p); 6352 VERIFY_CONSTANT (cond); 6353 *jump_target = cond; 6354 6355 tree body 6356 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); 6357 constexpr_ctx new_ctx = *ctx; 6358 constexpr_switch_state css = css_default_not_seen; 6359 new_ctx.css_state = &css; 6360 cxx_eval_constant_expression (&new_ctx, body, false, 6361 non_constant_p, overflow_p, jump_target); 6362 if (switches (jump_target) && css == css_default_seen) 6363 { 6364 /* If the SWITCH_EXPR body has default: label, process it once again, 6365 this time instructing label_matches to return true for default: 6366 label on switches (jump_target). */ 6367 css = css_default_processing; 6368 cxx_eval_constant_expression (&new_ctx, body, false, 6369 non_constant_p, overflow_p, jump_target); 6370 } 6371 if (breaks (jump_target) || switches (jump_target)) 6372 *jump_target = NULL_TREE; 6373 return NULL_TREE; 6374} 6375 6376/* Find the object of TYPE under initialization in CTX. */ 6377 6378static tree 6379lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) 6380{ 6381 if (!ctx) 6382 return NULL_TREE; 6383 6384 /* Prefer the outermost matching object, but don't cross 6385 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ 6386 if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) 6387 if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) 6388 return outer_ob; 6389 6390 /* We could use ctx->object unconditionally, but using ctx->ctor when we 6391 can is a minor optimization. */ 6392 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) 6393 return ctx->ctor; 6394 6395 if (!ctx->object) 6396 return NULL_TREE; 6397 6398 /* Since an object cannot have a field of its own type, we can search outward 6399 from ctx->object to find the unique containing object of TYPE. */ 6400 tree ob = ctx->object; 6401 while (ob) 6402 { 6403 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) 6404 break; 6405 if (handled_component_p (ob)) 6406 ob = TREE_OPERAND (ob, 0); 6407 else 6408 ob = NULL_TREE; 6409 } 6410 6411 return ob; 6412} 6413 6414/* Complain about an attempt to evaluate inline assembly. */ 6415 6416static void 6417inline_asm_in_constexpr_error (location_t loc) 6418{ 6419 auto_diagnostic_group d; 6420 error_at (loc, "inline assembly is not a constant expression"); 6421 inform (loc, "only unevaluated inline assembly is allowed in a " 6422 "%<constexpr%> function in C++20"); 6423} 6424 6425/* We're getting the constant value of DECL in a manifestly constant-evaluated 6426 context; maybe complain about that. */ 6427 6428static void 6429maybe_warn_about_constant_value (location_t loc, tree decl) 6430{ 6431 static bool explained = false; 6432 if (cxx_dialect >= cxx17 6433 && warn_interference_size 6434 && !OPTION_SET_P (param_destruct_interfere_size) 6435 && DECL_CONTEXT (decl) == std_node 6436 && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") 6437 && (LOCATION_FILE (input_location) != main_input_filename 6438 || module_exporting_p ()) 6439 && warning_at (loc, OPT_Winterference_size, "use of %qD", decl) 6440 && !explained) 6441 { 6442 explained = true; 6443 inform (loc, "its value can vary between compiler versions or " 6444 "with different %<-mtune%> or %<-mcpu%> flags"); 6445 inform (loc, "if this use is part of a public ABI, change it to " 6446 "instead use a constant variable you define"); 6447 inform (loc, "the default value for the current CPU tuning " 6448 "is %d bytes", param_destruct_interfere_size); 6449 inform (loc, "you can stabilize this value with %<--param " 6450 "hardware_destructive_interference_size=%d%>, or disable " 6451 "this warning with %<-Wno-interference-size%>", 6452 param_destruct_interfere_size); 6453 } 6454} 6455 6456/* For element type ELT_TYPE, return the appropriate type of the heap object 6457 containing such element(s). COOKIE_SIZE is NULL or the size of cookie 6458 in bytes. If COOKIE_SIZE is NULL, return array type 6459 ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return 6460 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; } 6461 where N is is computed such that the size of the struct fits into FULL_SIZE. 6462 If ARG_SIZE is non-NULL, it is the first argument to the new operator. 6463 It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE 6464 will be also 0 and so it is not possible to determine the actual array 6465 size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant 6466 expression evaluation of subexpressions of ARG_SIZE. */ 6467 6468static tree 6469build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type, 6470 tree cookie_size, tree full_size, tree arg_size, 6471 bool *non_constant_p, bool *overflow_p) 6472{ 6473 gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size)); 6474 gcc_assert (tree_fits_uhwi_p (full_size)); 6475 unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0; 6476 if (arg_size) 6477 { 6478 STRIP_NOPS (arg_size); 6479 if (cookie_size) 6480 { 6481 if (TREE_CODE (arg_size) != PLUS_EXPR) 6482 arg_size = NULL_TREE; 6483 else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST 6484 && tree_int_cst_equal (cookie_size, 6485 TREE_OPERAND (arg_size, 0))) 6486 { 6487 arg_size = TREE_OPERAND (arg_size, 1); 6488 STRIP_NOPS (arg_size); 6489 } 6490 else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST 6491 && tree_int_cst_equal (cookie_size, 6492 TREE_OPERAND (arg_size, 1))) 6493 { 6494 arg_size = TREE_OPERAND (arg_size, 0); 6495 STRIP_NOPS (arg_size); 6496 } 6497 else 6498 arg_size = NULL_TREE; 6499 } 6500 if (arg_size && TREE_CODE (arg_size) == MULT_EXPR) 6501 { 6502 tree op0 = TREE_OPERAND (arg_size, 0); 6503 tree op1 = TREE_OPERAND (arg_size, 1); 6504 if (integer_zerop (op0)) 6505 arg_size 6506 = cxx_eval_constant_expression (ctx, op1, false, non_constant_p, 6507 overflow_p); 6508 else if (integer_zerop (op1)) 6509 arg_size 6510 = cxx_eval_constant_expression (ctx, op0, false, non_constant_p, 6511 overflow_p); 6512 else 6513 arg_size = NULL_TREE; 6514 } 6515 else 6516 arg_size = NULL_TREE; 6517 } 6518 6519 unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size); 6520 if (!arg_size) 6521 { 6522 unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type); 6523 gcc_assert (fsz >= csz); 6524 fsz -= csz; 6525 if (esz) 6526 fsz /= esz; 6527 } 6528 tree itype2 = build_index_type (size_int (fsz - 1)); 6529 if (!cookie_size) 6530 return build_cplus_array_type (elt_type, itype2); 6531 return build_new_constexpr_heap_type (elt_type, cookie_size, itype2); 6532} 6533 6534/* Attempt to reduce the expression T to a constant value. 6535 On failure, issue diagnostic and return error_mark_node. */ 6536/* FIXME unify with c_fully_fold */ 6537/* FIXME overflow_p is too global */ 6538 6539static tree 6540cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, 6541 bool lval, 6542 bool *non_constant_p, bool *overflow_p, 6543 tree *jump_target /* = NULL */) 6544{ 6545 if (jump_target && *jump_target) 6546 { 6547 /* If we are jumping, ignore all statements/expressions except those 6548 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ 6549 switch (TREE_CODE (t)) 6550 { 6551 case BIND_EXPR: 6552 case STATEMENT_LIST: 6553 case LOOP_EXPR: 6554 case COND_EXPR: 6555 case IF_STMT: 6556 case DO_STMT: 6557 case WHILE_STMT: 6558 case FOR_STMT: 6559 break; 6560 case LABEL_EXPR: 6561 case CASE_LABEL_EXPR: 6562 if (label_matches (ctx, jump_target, t)) 6563 /* Found it. */ 6564 *jump_target = NULL_TREE; 6565 return NULL_TREE; 6566 default: 6567 return NULL_TREE; 6568 } 6569 } 6570 if (error_operand_p (t)) 6571 { 6572 *non_constant_p = true; 6573 return t; 6574 } 6575 6576 location_t loc = cp_expr_loc_or_input_loc (t); 6577 6578 STRIP_ANY_LOCATION_WRAPPER (t); 6579 6580 if (CONSTANT_CLASS_P (t)) 6581 { 6582 if (TREE_OVERFLOW (t)) 6583 { 6584 if (!ctx->quiet) 6585 permerror (input_location, "overflow in constant expression"); 6586 if (!flag_permissive || ctx->quiet) 6587 *overflow_p = true; 6588 } 6589 6590 if (TREE_CODE (t) == INTEGER_CST 6591 && TYPE_PTR_P (TREE_TYPE (t)) 6592 /* INTEGER_CST with pointer-to-method type is only used 6593 for a virtual method in a pointer to member function. 6594 Don't reject those. */ 6595 && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE 6596 && !integer_zerop (t)) 6597 { 6598 if (!ctx->quiet) 6599 error ("value %qE of type %qT is not a constant expression", 6600 t, TREE_TYPE (t)); 6601 *non_constant_p = true; 6602 } 6603 6604 return t; 6605 } 6606 6607 /* Avoid excessively long constexpr evaluations. */ 6608 if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) 6609 { 6610 if (!ctx->quiet) 6611 error_at (loc, 6612 "%<constexpr%> evaluation operation count exceeds limit of " 6613 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", 6614 constexpr_ops_limit); 6615 ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); 6616 *non_constant_p = true; 6617 return t; 6618 } 6619 6620 constexpr_ctx new_ctx; 6621 tree r = t; 6622 6623 tree_code tcode = TREE_CODE (t); 6624 switch (tcode) 6625 { 6626 case RESULT_DECL: 6627 if (lval) 6628 return t; 6629 /* We ask for an rvalue for the RESULT_DECL when indirecting 6630 through an invisible reference, or in named return value 6631 optimization. */ 6632 if (tree *p = ctx->global->values.get (t)) 6633 return *p; 6634 else 6635 { 6636 if (!ctx->quiet) 6637 error ("%qE is not a constant expression", t); 6638 *non_constant_p = true; 6639 } 6640 break; 6641 6642 case VAR_DECL: 6643 if (DECL_HAS_VALUE_EXPR_P (t)) 6644 { 6645 if (is_normal_capture_proxy (t) 6646 && current_function_decl == DECL_CONTEXT (t)) 6647 { 6648 /* Function parms aren't constexpr within the function 6649 definition, so don't try to look at the closure. But if the 6650 captured variable is constant, try to evaluate it directly. */ 6651 r = DECL_CAPTURED_VARIABLE (t); 6652 tree type = TREE_TYPE (t); 6653 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) 6654 { 6655 /* Adjust r to match the reference-ness of t. */ 6656 if (TYPE_REF_P (type)) 6657 r = build_address (r); 6658 else 6659 r = convert_from_reference (r); 6660 } 6661 } 6662 else 6663 r = DECL_VALUE_EXPR (t); 6664 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, 6665 overflow_p); 6666 } 6667 /* fall through */ 6668 case CONST_DECL: 6669 /* We used to not check lval for CONST_DECL, but darwin.cc uses 6670 CONST_DECL for aggregate constants. */ 6671 if (lval) 6672 return t; 6673 else if (t == ctx->object) 6674 return ctx->ctor; 6675 if (VAR_P (t)) 6676 if (tree *p = ctx->global->values.get (t)) 6677 if (*p != NULL_TREE) 6678 { 6679 r = *p; 6680 break; 6681 } 6682 if (ctx->manifestly_const_eval) 6683 maybe_warn_about_constant_value (loc, t); 6684 if (COMPLETE_TYPE_P (TREE_TYPE (t)) 6685 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 6686 { 6687 /* If the class is empty, we aren't actually loading anything. */ 6688 r = build_constructor (TREE_TYPE (t), NULL); 6689 TREE_CONSTANT (r) = true; 6690 } 6691 else if (ctx->strict) 6692 r = decl_really_constant_value (t, /*unshare_p=*/false); 6693 else 6694 r = decl_constant_value (t, /*unshare_p=*/false); 6695 if (TREE_CODE (r) == TARGET_EXPR 6696 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) 6697 r = TARGET_EXPR_INITIAL (r); 6698 if (DECL_P (r)) 6699 { 6700 if (!ctx->quiet) 6701 non_const_var_error (loc, r); 6702 *non_constant_p = true; 6703 } 6704 break; 6705 6706 case DEBUG_BEGIN_STMT: 6707 /* ??? It might be nice to retain this information somehow, so 6708 as to be able to step into a constexpr function call. */ 6709 /* Fall through. */ 6710 6711 case FUNCTION_DECL: 6712 case TEMPLATE_DECL: 6713 case LABEL_DECL: 6714 case LABEL_EXPR: 6715 case CASE_LABEL_EXPR: 6716 case PREDICT_EXPR: 6717 return t; 6718 6719 case PARM_DECL: 6720 if (lval && !TYPE_REF_P (TREE_TYPE (t))) 6721 /* glvalue use. */; 6722 else if (tree *p = ctx->global->values.get (r)) 6723 r = *p; 6724 else if (lval) 6725 /* Defer in case this is only used for its type. */; 6726 else if (COMPLETE_TYPE_P (TREE_TYPE (t)) 6727 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 6728 { 6729 /* If the class is empty, we aren't actually loading anything. */ 6730 r = build_constructor (TREE_TYPE (t), NULL); 6731 TREE_CONSTANT (r) = true; 6732 } 6733 else 6734 { 6735 if (!ctx->quiet) 6736 error ("%qE is not a constant expression", t); 6737 *non_constant_p = true; 6738 } 6739 break; 6740 6741 case CALL_EXPR: 6742 case AGGR_INIT_EXPR: 6743 r = cxx_eval_call_expression (ctx, t, lval, 6744 non_constant_p, overflow_p); 6745 break; 6746 6747 case DECL_EXPR: 6748 { 6749 r = DECL_EXPR_DECL (t); 6750 if (TREE_CODE (r) == USING_DECL) 6751 { 6752 r = void_node; 6753 break; 6754 } 6755 6756 if (VAR_P (r) 6757 && (TREE_STATIC (r) 6758 || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r))) 6759 /* Allow __FUNCTION__ etc. */ 6760 && !DECL_ARTIFICIAL (r)) 6761 { 6762 if (!ctx->quiet) 6763 { 6764 if (CP_DECL_THREAD_LOCAL_P (r)) 6765 error_at (loc, "control passes through definition of %qD " 6766 "with thread storage duration", r); 6767 else 6768 error_at (loc, "control passes through definition of %qD " 6769 "with static storage duration", r); 6770 } 6771 *non_constant_p = true; 6772 break; 6773 } 6774 6775 if (AGGREGATE_TYPE_P (TREE_TYPE (r)) 6776 || VECTOR_TYPE_P (TREE_TYPE (r))) 6777 { 6778 new_ctx = *ctx; 6779 new_ctx.object = r; 6780 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); 6781 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; 6782 ctx->global->values.put (r, new_ctx.ctor); 6783 ctx = &new_ctx; 6784 } 6785 6786 if (tree init = DECL_INITIAL (r)) 6787 { 6788 init = cxx_eval_constant_expression (ctx, init, 6789 false, 6790 non_constant_p, overflow_p); 6791 /* Don't share a CONSTRUCTOR that might be changed. */ 6792 init = unshare_constructor (init); 6793 /* Remember that a constant object's constructor has already 6794 run. */ 6795 if (CLASS_TYPE_P (TREE_TYPE (r)) 6796 && CP_TYPE_CONST_P (TREE_TYPE (r))) 6797 TREE_READONLY (init) = true; 6798 ctx->global->values.put (r, init); 6799 } 6800 else if (ctx == &new_ctx) 6801 /* We gave it a CONSTRUCTOR above. */; 6802 else 6803 ctx->global->values.put (r, NULL_TREE); 6804 } 6805 break; 6806 6807 case TARGET_EXPR: 6808 { 6809 tree type = TREE_TYPE (t); 6810 6811 if (!literal_type_p (type)) 6812 { 6813 if (!ctx->quiet) 6814 { 6815 auto_diagnostic_group d; 6816 error ("temporary of non-literal type %qT in a " 6817 "constant expression", type); 6818 explain_non_literal_class (type); 6819 } 6820 *non_constant_p = true; 6821 break; 6822 } 6823 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); 6824 /* Avoid evaluating a TARGET_EXPR more than once. */ 6825 tree slot = TARGET_EXPR_SLOT (t); 6826 if (tree *p = ctx->global->values.get (slot)) 6827 { 6828 if (lval) 6829 return slot; 6830 r = *p; 6831 break; 6832 } 6833 if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) 6834 { 6835 /* We're being expanded without an explicit target, so start 6836 initializing a new object; expansion with an explicit target 6837 strips the TARGET_EXPR before we get here. */ 6838 new_ctx = *ctx; 6839 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve 6840 any PLACEHOLDER_EXPR within the initializer that refers to the 6841 former object under construction. */ 6842 new_ctx.parent = ctx; 6843 new_ctx.ctor = build_constructor (type, NULL); 6844 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; 6845 new_ctx.object = slot; 6846 ctx->global->values.put (new_ctx.object, new_ctx.ctor); 6847 ctx = &new_ctx; 6848 } 6849 /* Pass false for 'lval' because this indicates 6850 initialization of a temporary. */ 6851 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 6852 false, 6853 non_constant_p, overflow_p); 6854 if (*non_constant_p) 6855 break; 6856 /* If the initializer is complex, evaluate it to initialize slot. */ 6857 bool is_complex = target_expr_needs_replace (t); 6858 if (!is_complex) 6859 { 6860 r = unshare_constructor (r); 6861 /* Adjust the type of the result to the type of the temporary. */ 6862 r = adjust_temp_type (type, r); 6863 ctx->global->values.put (slot, r); 6864 } 6865 if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) 6866 ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); 6867 if (ctx->save_exprs) 6868 ctx->save_exprs->safe_push (slot); 6869 if (lval) 6870 return slot; 6871 if (is_complex) 6872 r = *ctx->global->values.get (slot); 6873 } 6874 break; 6875 6876 case INIT_EXPR: 6877 case MODIFY_EXPR: 6878 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); 6879 r = cxx_eval_store_expression (ctx, t, lval, 6880 non_constant_p, overflow_p); 6881 break; 6882 6883 case SCOPE_REF: 6884 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 6885 lval, 6886 non_constant_p, overflow_p); 6887 break; 6888 6889 case RETURN_EXPR: 6890 if (TREE_OPERAND (t, 0) != NULL_TREE) 6891 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 6892 lval, 6893 non_constant_p, overflow_p); 6894 /* FALLTHRU */ 6895 case BREAK_STMT: 6896 case CONTINUE_STMT: 6897 if (jump_target) 6898 *jump_target = t; 6899 else 6900 { 6901 /* Can happen with ({ return true; }) && false; passed to 6902 maybe_constant_value. There is nothing to jump over in this 6903 case, and the bug will be diagnosed later. */ 6904 gcc_assert (ctx->quiet); 6905 *non_constant_p = true; 6906 } 6907 break; 6908 6909 case SAVE_EXPR: 6910 /* Avoid evaluating a SAVE_EXPR more than once. */ 6911 if (tree *p = ctx->global->values.get (t)) 6912 r = *p; 6913 else 6914 { 6915 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, 6916 non_constant_p, overflow_p); 6917 if (*non_constant_p) 6918 break; 6919 ctx->global->values.put (t, r); 6920 if (ctx->save_exprs) 6921 ctx->save_exprs->safe_push (t); 6922 } 6923 break; 6924 6925 case TRY_CATCH_EXPR: 6926 if (TREE_OPERAND (t, 0) == NULL_TREE) 6927 { 6928 r = void_node; 6929 break; 6930 } 6931 /* FALLTHRU */ 6932 case NON_LVALUE_EXPR: 6933 case TRY_BLOCK: 6934 case MUST_NOT_THROW_EXPR: 6935 case EXPR_STMT: 6936 case EH_SPEC_BLOCK: 6937 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 6938 lval, 6939 non_constant_p, overflow_p, 6940 jump_target); 6941 break; 6942 6943 case CLEANUP_POINT_EXPR: 6944 { 6945 auto_vec<tree, 2> cleanups; 6946 vec<tree> *prev_cleanups = ctx->global->cleanups; 6947 ctx->global->cleanups = &cleanups; 6948 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 6949 lval, 6950 non_constant_p, overflow_p, 6951 jump_target); 6952 ctx->global->cleanups = prev_cleanups; 6953 unsigned int i; 6954 tree cleanup; 6955 /* Evaluate the cleanups. */ 6956 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) 6957 cxx_eval_constant_expression (ctx, cleanup, false, 6958 non_constant_p, overflow_p); 6959 } 6960 break; 6961 6962 case TRY_FINALLY_EXPR: 6963 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, 6964 non_constant_p, overflow_p, 6965 jump_target); 6966 if (!*non_constant_p) 6967 /* Also evaluate the cleanup. */ 6968 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true, 6969 non_constant_p, overflow_p); 6970 break; 6971 6972 case CLEANUP_STMT: 6973 r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, 6974 non_constant_p, overflow_p, 6975 jump_target); 6976 if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) 6977 { 6978 iloc_sentinel ils (loc); 6979 /* Also evaluate the cleanup. */ 6980 cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true, 6981 non_constant_p, overflow_p); 6982 } 6983 break; 6984 6985 /* These differ from cxx_eval_unary_expression in that this doesn't 6986 check for a constant operand or result; an address can be 6987 constant without its operand being, and vice versa. */ 6988 case MEM_REF: 6989 case INDIRECT_REF: 6990 r = cxx_eval_indirect_ref (ctx, t, lval, 6991 non_constant_p, overflow_p); 6992 break; 6993 6994 case ADDR_EXPR: 6995 { 6996 tree oldop = TREE_OPERAND (t, 0); 6997 tree op = cxx_eval_constant_expression (ctx, oldop, 6998 /*lval*/true, 6999 non_constant_p, overflow_p); 7000 /* Don't VERIFY_CONSTANT here. */ 7001 if (*non_constant_p) 7002 return t; 7003 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); 7004 /* This function does more aggressive folding than fold itself. */ 7005 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); 7006 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) 7007 { 7008 ggc_free (r); 7009 return t; 7010 } 7011 break; 7012 } 7013 7014 case REALPART_EXPR: 7015 case IMAGPART_EXPR: 7016 if (lval) 7017 { 7018 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, 7019 non_constant_p, overflow_p); 7020 if (r == error_mark_node) 7021 ; 7022 else if (r == TREE_OPERAND (t, 0)) 7023 r = t; 7024 else 7025 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); 7026 break; 7027 } 7028 /* FALLTHRU */ 7029 case CONJ_EXPR: 7030 case FIX_TRUNC_EXPR: 7031 case FLOAT_EXPR: 7032 case NEGATE_EXPR: 7033 case ABS_EXPR: 7034 case ABSU_EXPR: 7035 case BIT_NOT_EXPR: 7036 case TRUTH_NOT_EXPR: 7037 case FIXED_CONVERT_EXPR: 7038 r = cxx_eval_unary_expression (ctx, t, lval, 7039 non_constant_p, overflow_p); 7040 break; 7041 7042 case SIZEOF_EXPR: 7043 r = fold_sizeof_expr (t); 7044 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, 7045 which could lead to an infinite recursion. */ 7046 if (TREE_CODE (r) != SIZEOF_EXPR) 7047 r = cxx_eval_constant_expression (ctx, r, lval, 7048 non_constant_p, overflow_p, 7049 jump_target); 7050 else 7051 { 7052 *non_constant_p = true; 7053 gcc_assert (ctx->quiet); 7054 } 7055 7056 break; 7057 7058 case COMPOUND_EXPR: 7059 { 7060 /* check_return_expr sometimes wraps a TARGET_EXPR in a 7061 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR 7062 introduced by build_call_a. */ 7063 tree op0 = TREE_OPERAND (t, 0); 7064 tree op1 = TREE_OPERAND (t, 1); 7065 STRIP_NOPS (op1); 7066 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) 7067 || TREE_CODE (op1) == EMPTY_CLASS_EXPR) 7068 r = cxx_eval_constant_expression (ctx, op0, 7069 lval, non_constant_p, overflow_p, 7070 jump_target); 7071 else 7072 { 7073 /* Check that the LHS is constant and then discard it. */ 7074 cxx_eval_constant_expression (ctx, op0, 7075 true, non_constant_p, overflow_p, 7076 jump_target); 7077 if (*non_constant_p) 7078 return t; 7079 op1 = TREE_OPERAND (t, 1); 7080 r = cxx_eval_constant_expression (ctx, op1, 7081 lval, non_constant_p, overflow_p, 7082 jump_target); 7083 } 7084 } 7085 break; 7086 7087 case POINTER_PLUS_EXPR: 7088 case POINTER_DIFF_EXPR: 7089 case PLUS_EXPR: 7090 case MINUS_EXPR: 7091 case MULT_EXPR: 7092 case TRUNC_DIV_EXPR: 7093 case CEIL_DIV_EXPR: 7094 case FLOOR_DIV_EXPR: 7095 case ROUND_DIV_EXPR: 7096 case TRUNC_MOD_EXPR: 7097 case CEIL_MOD_EXPR: 7098 case ROUND_MOD_EXPR: 7099 case RDIV_EXPR: 7100 case EXACT_DIV_EXPR: 7101 case MIN_EXPR: 7102 case MAX_EXPR: 7103 case LSHIFT_EXPR: 7104 case RSHIFT_EXPR: 7105 case LROTATE_EXPR: 7106 case RROTATE_EXPR: 7107 case BIT_IOR_EXPR: 7108 case BIT_XOR_EXPR: 7109 case BIT_AND_EXPR: 7110 case TRUTH_XOR_EXPR: 7111 case LT_EXPR: 7112 case LE_EXPR: 7113 case GT_EXPR: 7114 case GE_EXPR: 7115 case EQ_EXPR: 7116 case NE_EXPR: 7117 case SPACESHIP_EXPR: 7118 case UNORDERED_EXPR: 7119 case ORDERED_EXPR: 7120 case UNLT_EXPR: 7121 case UNLE_EXPR: 7122 case UNGT_EXPR: 7123 case UNGE_EXPR: 7124 case UNEQ_EXPR: 7125 case LTGT_EXPR: 7126 case RANGE_EXPR: 7127 case COMPLEX_EXPR: 7128 r = cxx_eval_binary_expression (ctx, t, lval, 7129 non_constant_p, overflow_p); 7130 break; 7131 7132 /* fold can introduce non-IF versions of these; still treat them as 7133 short-circuiting. */ 7134 case TRUTH_AND_EXPR: 7135 case TRUTH_ANDIF_EXPR: 7136 r = cxx_eval_logical_expression (ctx, t, boolean_false_node, 7137 boolean_true_node, 7138 non_constant_p, overflow_p); 7139 break; 7140 7141 case TRUTH_OR_EXPR: 7142 case TRUTH_ORIF_EXPR: 7143 r = cxx_eval_logical_expression (ctx, t, boolean_true_node, 7144 boolean_false_node, 7145 non_constant_p, overflow_p); 7146 break; 7147 7148 case ARRAY_REF: 7149 r = cxx_eval_array_reference (ctx, t, lval, 7150 non_constant_p, overflow_p); 7151 break; 7152 7153 case COMPONENT_REF: 7154 if (is_overloaded_fn (t)) 7155 { 7156 /* We can only get here in checking mode via 7157 build_non_dependent_expr, because any expression that 7158 calls or takes the address of the function will have 7159 pulled a FUNCTION_DECL out of the COMPONENT_REF. */ 7160 gcc_checking_assert (ctx->quiet || errorcount); 7161 *non_constant_p = true; 7162 return t; 7163 } 7164 r = cxx_eval_component_reference (ctx, t, lval, 7165 non_constant_p, overflow_p); 7166 break; 7167 7168 case BIT_FIELD_REF: 7169 r = cxx_eval_bit_field_ref (ctx, t, lval, 7170 non_constant_p, overflow_p); 7171 break; 7172 7173 case COND_EXPR: 7174 case IF_STMT: 7175 if (jump_target && *jump_target) 7176 { 7177 tree orig_jump = *jump_target; 7178 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) 7179 ? TREE_OPERAND (t, 1) : void_node); 7180 /* When jumping to a label, the label might be either in the 7181 then or else blocks, so process then block first in skipping 7182 mode first, and if we are still in the skipping mode at its end, 7183 process the else block too. */ 7184 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, 7185 overflow_p, jump_target); 7186 /* It's possible that we found the label in the then block. But 7187 it could have been followed by another jumping statement, e.g. 7188 say we're looking for case 1: 7189 if (cond) 7190 { 7191 // skipped statements 7192 case 1:; // clears up *jump_target 7193 return 1; // and sets it to a RETURN_EXPR 7194 } 7195 else { ... } 7196 in which case we need not go looking to the else block. 7197 (goto is not allowed in a constexpr function.) */ 7198 if (*jump_target == orig_jump) 7199 { 7200 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) 7201 ? TREE_OPERAND (t, 2) : void_node); 7202 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, 7203 overflow_p, jump_target); 7204 } 7205 break; 7206 } 7207 r = cxx_eval_conditional_expression (ctx, t, lval, 7208 non_constant_p, overflow_p, 7209 jump_target); 7210 break; 7211 case VEC_COND_EXPR: 7212 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, 7213 overflow_p); 7214 break; 7215 7216 case CONSTRUCTOR: 7217 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) 7218 { 7219 /* Don't re-process a constant CONSTRUCTOR, but do fold it to 7220 VECTOR_CST if applicable. */ 7221 verify_constructor_flags (t); 7222 if (TREE_CONSTANT (t)) 7223 return fold (t); 7224 } 7225 r = cxx_eval_bare_aggregate (ctx, t, lval, 7226 non_constant_p, overflow_p); 7227 break; 7228 7229 case VEC_INIT_EXPR: 7230 /* We can get this in a defaulted constructor for a class with a 7231 non-static data member of array type. Either the initializer will 7232 be NULL, meaning default-initialization, or it will be an lvalue 7233 or xvalue of the same type, meaning direct-initialization from the 7234 corresponding member. */ 7235 r = cxx_eval_vec_init (ctx, t, lval, 7236 non_constant_p, overflow_p); 7237 break; 7238 7239 case VEC_PERM_EXPR: 7240 r = cxx_eval_trinary_expression (ctx, t, lval, 7241 non_constant_p, overflow_p); 7242 break; 7243 7244 case PAREN_EXPR: 7245 gcc_assert (!REF_PARENTHESIZED_P (t)); 7246 /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in 7247 constant expressions since it's unaffected by -fassociative-math. */ 7248 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, 7249 non_constant_p, overflow_p); 7250 break; 7251 7252 case NOP_EXPR: 7253 if (REINTERPRET_CAST_P (t)) 7254 { 7255 if (!ctx->quiet) 7256 error_at (loc, 7257 "%<reinterpret_cast%> is not a constant expression"); 7258 *non_constant_p = true; 7259 return t; 7260 } 7261 /* FALLTHROUGH. */ 7262 case CONVERT_EXPR: 7263 case VIEW_CONVERT_EXPR: 7264 case UNARY_PLUS_EXPR: 7265 { 7266 tree oldop = TREE_OPERAND (t, 0); 7267 7268 tree op = cxx_eval_constant_expression (ctx, oldop, 7269 lval, 7270 non_constant_p, overflow_p); 7271 if (*non_constant_p) 7272 return t; 7273 tree type = TREE_TYPE (t); 7274 7275 if (VOID_TYPE_P (type)) 7276 return void_node; 7277 7278 if (TREE_CODE (t) == CONVERT_EXPR 7279 && ARITHMETIC_TYPE_P (type) 7280 && INDIRECT_TYPE_P (TREE_TYPE (op)) 7281 && ctx->manifestly_const_eval) 7282 { 7283 if (!ctx->quiet) 7284 error_at (loc, 7285 "conversion from pointer type %qT to arithmetic type " 7286 "%qT in a constant expression", TREE_TYPE (op), type); 7287 *non_constant_p = true; 7288 return t; 7289 } 7290 7291 /* [expr.const]: a conversion from type cv void* to a pointer-to-object 7292 type cannot be part of a core constant expression as a resolution to 7293 DR 1312. */ 7294 if (TYPE_PTROB_P (type) 7295 && TYPE_PTR_P (TREE_TYPE (op)) 7296 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) 7297 /* Inside a call to std::construct_at or to 7298 std::allocator<T>::{,de}allocate, we permit casting from void* 7299 because that is compiler-generated code. */ 7300 && !is_std_construct_at (ctx->call) 7301 && !is_std_allocator_allocate (ctx->call)) 7302 { 7303 /* Likewise, don't error when casting from void* when OP is 7304 &heap uninit and similar. */ 7305 tree sop = tree_strip_nop_conversions (op); 7306 if (TREE_CODE (sop) == ADDR_EXPR 7307 && VAR_P (TREE_OPERAND (sop, 0)) 7308 && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0))) 7309 /* OK */; 7310 else 7311 { 7312 if (!ctx->quiet) 7313 error_at (loc, "cast from %qT is not allowed", 7314 TREE_TYPE (op)); 7315 *non_constant_p = true; 7316 return t; 7317 } 7318 } 7319 7320 if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) 7321 { 7322 op = cplus_expand_constant (op); 7323 if (TREE_CODE (op) == PTRMEM_CST) 7324 { 7325 if (!ctx->quiet) 7326 error_at (loc, "%qE is not a constant expression when the " 7327 "class %qT is still incomplete", op, 7328 PTRMEM_CST_CLASS (op)); 7329 *non_constant_p = true; 7330 return t; 7331 } 7332 } 7333 7334 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) 7335 { 7336 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) 7337 && !can_convert_qual (type, op)) 7338 op = cplus_expand_constant (op); 7339 return cp_fold_convert (type, op); 7340 } 7341 7342 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) 7343 { 7344 if (integer_zerop (op)) 7345 { 7346 if (TYPE_REF_P (type)) 7347 { 7348 if (!ctx->quiet) 7349 error_at (loc, "dereferencing a null pointer"); 7350 *non_constant_p = true; 7351 return t; 7352 } 7353 } 7354 else 7355 { 7356 /* This detects for example: 7357 reinterpret_cast<void*>(sizeof 0) 7358 */ 7359 if (!ctx->quiet) 7360 error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " 7361 "a constant expression", 7362 type, op); 7363 *non_constant_p = true; 7364 return t; 7365 } 7366 } 7367 7368 if (INDIRECT_TYPE_P (type) 7369 && TREE_CODE (op) == NOP_EXPR 7370 && TREE_TYPE (op) == ptr_type_node 7371 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR 7372 && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) 7373 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), 7374 0)) == heap_uninit_identifier 7375 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), 7376 0)) == heap_vec_uninit_identifier)) 7377 { 7378 tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); 7379 tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); 7380 tree elt_type = TREE_TYPE (type); 7381 tree cookie_size = NULL_TREE; 7382 tree arg_size = NULL_TREE; 7383 if (TREE_CODE (elt_type) == RECORD_TYPE 7384 && TYPE_NAME (elt_type) == heap_identifier) 7385 { 7386 tree fld1 = TYPE_FIELDS (elt_type); 7387 tree fld2 = DECL_CHAIN (fld1); 7388 elt_type = TREE_TYPE (TREE_TYPE (fld2)); 7389 cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); 7390 } 7391 DECL_NAME (var) 7392 = (DECL_NAME (var) == heap_uninit_identifier 7393 ? heap_identifier : heap_vec_identifier); 7394 /* For zero sized elt_type, try to recover how many outer_nelts 7395 it should have. */ 7396 if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size) 7397 : integer_zerop (var_size)) 7398 && !int_size_in_bytes (elt_type) 7399 && TREE_CODE (oldop) == CALL_EXPR 7400 && call_expr_nargs (oldop) >= 1) 7401 if (tree fun = get_function_named_in_call (oldop)) 7402 if (cxx_replaceable_global_alloc_fn (fun) 7403 && IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) 7404 arg_size = CALL_EXPR_ARG (oldop, 0); 7405 TREE_TYPE (var) 7406 = build_new_constexpr_heap_type (ctx, elt_type, cookie_size, 7407 var_size, arg_size, 7408 non_constant_p, overflow_p); 7409 TREE_TYPE (TREE_OPERAND (op, 0)) 7410 = build_pointer_type (TREE_TYPE (var)); 7411 } 7412 7413 if (op == oldop && tcode != UNARY_PLUS_EXPR) 7414 /* We didn't fold at the top so we could check for ptr-int 7415 conversion. */ 7416 return fold (t); 7417 7418 tree sop; 7419 7420 /* Handle an array's bounds having been deduced after we built 7421 the wrapping expression. */ 7422 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) 7423 r = op; 7424 else if (sop = tree_strip_nop_conversions (op), 7425 sop != op && (same_type_ignoring_tlq_and_bounds_p 7426 (type, TREE_TYPE (sop)))) 7427 r = sop; 7428 else if (tcode == UNARY_PLUS_EXPR) 7429 r = fold_convert (TREE_TYPE (t), op); 7430 else 7431 r = fold_build1 (tcode, type, op); 7432 7433 /* Conversion of an out-of-range value has implementation-defined 7434 behavior; the language considers it different from arithmetic 7435 overflow, which is undefined. */ 7436 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) 7437 TREE_OVERFLOW (r) = false; 7438 } 7439 break; 7440 7441 case EMPTY_CLASS_EXPR: 7442 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering 7443 it to an appropriate CONSTRUCTOR. */ 7444 return build_constructor (TREE_TYPE (t), NULL); 7445 7446 case STATEMENT_LIST: 7447 new_ctx = *ctx; 7448 new_ctx.ctor = new_ctx.object = NULL_TREE; 7449 return cxx_eval_statement_list (&new_ctx, t, 7450 non_constant_p, overflow_p, jump_target); 7451 7452 case BIND_EXPR: 7453 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), 7454 lval, 7455 non_constant_p, overflow_p, 7456 jump_target); 7457 7458 case PREINCREMENT_EXPR: 7459 case POSTINCREMENT_EXPR: 7460 case PREDECREMENT_EXPR: 7461 case POSTDECREMENT_EXPR: 7462 return cxx_eval_increment_expression (ctx, t, 7463 lval, non_constant_p, overflow_p); 7464 7465 case LAMBDA_EXPR: 7466 case NEW_EXPR: 7467 case VEC_NEW_EXPR: 7468 case DELETE_EXPR: 7469 case VEC_DELETE_EXPR: 7470 case THROW_EXPR: 7471 case MODOP_EXPR: 7472 /* GCC internal stuff. */ 7473 case VA_ARG_EXPR: 7474 case NON_DEPENDENT_EXPR: 7475 case BASELINK: 7476 case OFFSET_REF: 7477 if (!ctx->quiet) 7478 error_at (loc, "expression %qE is not a constant expression", t); 7479 *non_constant_p = true; 7480 break; 7481 7482 case OBJ_TYPE_REF: 7483 /* Virtual function lookup. We don't need to do anything fancy. */ 7484 return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), 7485 lval, non_constant_p, overflow_p); 7486 7487 case PLACEHOLDER_EXPR: 7488 /* Use of the value or address of the current object. */ 7489 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) 7490 { 7491 if (TREE_CODE (ctor) == CONSTRUCTOR) 7492 return ctor; 7493 else 7494 return cxx_eval_constant_expression (ctx, ctor, lval, 7495 non_constant_p, overflow_p); 7496 } 7497 /* A placeholder without a referent. We can get here when 7498 checking whether NSDMIs are noexcept, or in massage_init_elt; 7499 just say it's non-constant for now. */ 7500 gcc_assert (ctx->quiet); 7501 *non_constant_p = true; 7502 break; 7503 7504 case EXIT_EXPR: 7505 { 7506 tree cond = TREE_OPERAND (t, 0); 7507 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false, 7508 non_constant_p, overflow_p); 7509 VERIFY_CONSTANT (cond); 7510 if (integer_nonzerop (cond)) 7511 *jump_target = t; 7512 } 7513 break; 7514 7515 case GOTO_EXPR: 7516 if (breaks (&TREE_OPERAND (t, 0)) 7517 || continues (&TREE_OPERAND (t, 0)) 7518 /* Allow for jumping to a cdtor_label. */ 7519 || returns (&TREE_OPERAND (t, 0))) 7520 *jump_target = TREE_OPERAND (t, 0); 7521 else 7522 { 7523 gcc_assert (cxx_dialect >= cxx23); 7524 if (!ctx->quiet) 7525 error_at (loc, "%<goto%> is not a constant expression"); 7526 *non_constant_p = true; 7527 } 7528 break; 7529 7530 case LOOP_EXPR: 7531 case DO_STMT: 7532 case WHILE_STMT: 7533 case FOR_STMT: 7534 cxx_eval_loop_expr (ctx, t, 7535 non_constant_p, overflow_p, jump_target); 7536 break; 7537 7538 case SWITCH_EXPR: 7539 case SWITCH_STMT: 7540 cxx_eval_switch_expr (ctx, t, 7541 non_constant_p, overflow_p, jump_target); 7542 break; 7543 7544 case REQUIRES_EXPR: 7545 /* It's possible to get a requires-expression in a constant 7546 expression. For example: 7547 7548 template<typename T> concept bool C() { 7549 return requires (T t) { t; }; 7550 } 7551 7552 template<typename T> requires !C<T>() void f(T); 7553 7554 Normalization leaves f with the associated constraint 7555 '!requires (T t) { ... }' which is not transformed into 7556 a constraint. */ 7557 if (!processing_template_decl) 7558 return evaluate_requires_expr (t); 7559 else 7560 *non_constant_p = true; 7561 return t; 7562 7563 case ANNOTATE_EXPR: 7564 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 7565 lval, 7566 non_constant_p, overflow_p, 7567 jump_target); 7568 break; 7569 7570 case USING_STMT: 7571 r = void_node; 7572 break; 7573 7574 case TEMPLATE_ID_EXPR: 7575 { 7576 /* We can evaluate template-id that refers to a concept only if 7577 the template arguments are non-dependent. */ 7578 tree id = unpack_concept_check (t); 7579 tree tmpl = TREE_OPERAND (id, 0); 7580 if (!concept_definition_p (tmpl)) 7581 internal_error ("unexpected template-id %qE", t); 7582 7583 if (function_concept_p (tmpl)) 7584 { 7585 if (!ctx->quiet) 7586 error_at (cp_expr_loc_or_input_loc (t), 7587 "function concept must be called"); 7588 r = error_mark_node; 7589 break; 7590 } 7591 7592 if (!value_dependent_expression_p (t) 7593 && !uid_sensitive_constexpr_evaluation_p ()) 7594 r = evaluate_concept_check (t); 7595 else 7596 *non_constant_p = true; 7597 7598 break; 7599 } 7600 7601 case ASM_EXPR: 7602 if (!ctx->quiet) 7603 inline_asm_in_constexpr_error (loc); 7604 *non_constant_p = true; 7605 return t; 7606 7607 case BIT_CAST_EXPR: 7608 if (lval) 7609 { 7610 if (!ctx->quiet) 7611 error_at (EXPR_LOCATION (t), 7612 "address of a call to %qs is not a constant expression", 7613 "__builtin_bit_cast"); 7614 *non_constant_p = true; 7615 return t; 7616 } 7617 r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); 7618 break; 7619 7620 case OMP_PARALLEL: 7621 case OMP_TASK: 7622 case OMP_FOR: 7623 case OMP_SIMD: 7624 case OMP_DISTRIBUTE: 7625 case OMP_TASKLOOP: 7626 case OMP_LOOP: 7627 case OMP_TEAMS: 7628 case OMP_TARGET_DATA: 7629 case OMP_TARGET: 7630 case OMP_SECTIONS: 7631 case OMP_ORDERED: 7632 case OMP_CRITICAL: 7633 case OMP_SINGLE: 7634 case OMP_SCAN: 7635 case OMP_SCOPE: 7636 case OMP_SECTION: 7637 case OMP_MASTER: 7638 case OMP_MASKED: 7639 case OMP_TASKGROUP: 7640 case OMP_TARGET_UPDATE: 7641 case OMP_TARGET_ENTER_DATA: 7642 case OMP_TARGET_EXIT_DATA: 7643 case OMP_ATOMIC: 7644 case OMP_ATOMIC_READ: 7645 case OMP_ATOMIC_CAPTURE_OLD: 7646 case OMP_ATOMIC_CAPTURE_NEW: 7647 case OMP_DEPOBJ: 7648 case OACC_PARALLEL: 7649 case OACC_KERNELS: 7650 case OACC_SERIAL: 7651 case OACC_DATA: 7652 case OACC_HOST_DATA: 7653 case OACC_LOOP: 7654 case OACC_CACHE: 7655 case OACC_DECLARE: 7656 case OACC_ENTER_DATA: 7657 case OACC_EXIT_DATA: 7658 case OACC_UPDATE: 7659 if (!ctx->quiet) 7660 error_at (EXPR_LOCATION (t), 7661 "statement is not a constant expression"); 7662 *non_constant_p = true; 7663 break; 7664 7665 default: 7666 if (STATEMENT_CODE_P (TREE_CODE (t))) 7667 { 7668 /* This function doesn't know how to deal with pre-genericize 7669 statements; this can only happen with statement-expressions, 7670 so for now just fail. */ 7671 if (!ctx->quiet) 7672 error_at (EXPR_LOCATION (t), 7673 "statement is not a constant expression"); 7674 } 7675 else 7676 internal_error ("unexpected expression %qE of kind %s", t, 7677 get_tree_code_name (TREE_CODE (t))); 7678 *non_constant_p = true; 7679 break; 7680 } 7681 7682 if (r == error_mark_node) 7683 *non_constant_p = true; 7684 7685 if (*non_constant_p) 7686 return t; 7687 else 7688 return r; 7689} 7690 7691/* P0859: A function is needed for constant evaluation if it is a constexpr 7692 function that is named by an expression ([basic.def.odr]) that is 7693 potentially constant evaluated. 7694 7695 So we need to instantiate any constexpr functions mentioned by the 7696 expression even if the definition isn't needed for evaluating the 7697 expression. */ 7698 7699static tree 7700instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) 7701{ 7702 if (TREE_CODE (*tp) == FUNCTION_DECL 7703 && DECL_DECLARED_CONSTEXPR_P (*tp) 7704 && !DECL_INITIAL (*tp) 7705 && !trivial_fn_p (*tp) 7706 && DECL_TEMPLOID_INSTANTIATION (*tp) 7707 && !uid_sensitive_constexpr_evaluation_p ()) 7708 { 7709 ++function_depth; 7710 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); 7711 --function_depth; 7712 } 7713 else if (TREE_CODE (*tp) == CALL_EXPR 7714 || TREE_CODE (*tp) == AGGR_INIT_EXPR) 7715 { 7716 if (EXPR_HAS_LOCATION (*tp)) 7717 input_location = EXPR_LOCATION (*tp); 7718 } 7719 7720 if (!EXPR_P (*tp)) 7721 *walk_subtrees = 0; 7722 7723 return NULL_TREE; 7724} 7725 7726static void 7727instantiate_constexpr_fns (tree t) 7728{ 7729 location_t loc = input_location; 7730 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); 7731 input_location = loc; 7732} 7733 7734/* Look for heap variables in the expression *TP. */ 7735 7736static tree 7737find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) 7738{ 7739 if (VAR_P (*tp) 7740 && (DECL_NAME (*tp) == heap_uninit_identifier 7741 || DECL_NAME (*tp) == heap_identifier 7742 || DECL_NAME (*tp) == heap_vec_uninit_identifier 7743 || DECL_NAME (*tp) == heap_vec_identifier 7744 || DECL_NAME (*tp) == heap_deleted_identifier)) 7745 return *tp; 7746 7747 if (TYPE_P (*tp)) 7748 *walk_subtrees = 0; 7749 return NULL_TREE; 7750} 7751 7752/* Find immediate function decls in *TP if any. */ 7753 7754static tree 7755find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) 7756{ 7757 if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) 7758 return *tp; 7759 if (TREE_CODE (*tp) == PTRMEM_CST 7760 && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL 7761 && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) 7762 return PTRMEM_CST_MEMBER (*tp); 7763 return NULL_TREE; 7764} 7765 7766/* T has TREE_CONSTANT set but has been deemed not a valid C++ constant 7767 expression. Return a version of T that has TREE_CONSTANT cleared. */ 7768 7769static tree 7770mark_non_constant (tree t) 7771{ 7772 gcc_checking_assert (TREE_CONSTANT (t)); 7773 7774 /* This isn't actually constant, so unset TREE_CONSTANT. 7775 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires 7776 it to be set if it is invariant address, even when it is not 7777 a valid C++ constant expression. Wrap it with a NOP_EXPR 7778 instead. */ 7779 if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR) 7780 t = copy_node (t); 7781 else if (TREE_CODE (t) == CONSTRUCTOR) 7782 t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t); 7783 else 7784 t = build_nop (TREE_TYPE (t), t); 7785 TREE_CONSTANT (t) = false; 7786 return t; 7787} 7788 7789/* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. 7790 STRICT has the same sense as for constant_value_1: true if we only allow 7791 conforming C++ constant expressions, or false if we want a constant value 7792 even if it doesn't conform. 7793 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as 7794 per P0595 even when ALLOW_NON_CONSTANT is true. 7795 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. 7796 OBJECT must be non-NULL in that case. */ 7797 7798static tree 7799cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, 7800 bool strict = true, 7801 bool manifestly_const_eval = false, 7802 bool constexpr_dtor = false, 7803 tree object = NULL_TREE) 7804{ 7805 auto_timevar time (TV_CONSTEXPR); 7806 7807 bool non_constant_p = false; 7808 bool overflow_p = false; 7809 7810 if (BRACE_ENCLOSED_INITIALIZER_P (t)) 7811 { 7812 gcc_checking_assert (allow_non_constant); 7813 return t; 7814 } 7815 7816 constexpr_global_ctx global_ctx; 7817 constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, 7818 allow_non_constant, strict, 7819 manifestly_const_eval || !allow_non_constant }; 7820 7821 /* Turn off -frounding-math for manifestly constant evaluation. */ 7822 warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval); 7823 tree type = initialized_type (t); 7824 tree r = t; 7825 bool is_consteval = false; 7826 if (VOID_TYPE_P (type)) 7827 { 7828 if (constexpr_dtor) 7829 /* Used for destructors of array elements. */ 7830 type = TREE_TYPE (object); 7831 else 7832 { 7833 if (cxx_dialect < cxx20) 7834 return t; 7835 if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) 7836 return t; 7837 /* Calls to immediate functions returning void need to be 7838 evaluated. */ 7839 tree fndecl = cp_get_callee_fndecl_nofold (t); 7840 if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) 7841 return t; 7842 else 7843 is_consteval = true; 7844 } 7845 } 7846 else if (cxx_dialect >= cxx20 7847 && (TREE_CODE (t) == CALL_EXPR 7848 || TREE_CODE (t) == AGGR_INIT_EXPR 7849 || TREE_CODE (t) == TARGET_EXPR)) 7850 { 7851 /* For non-concept checks, determine if it is consteval. */ 7852 if (!concept_check_p (t)) 7853 { 7854 tree x = t; 7855 if (TREE_CODE (x) == TARGET_EXPR) 7856 x = TARGET_EXPR_INITIAL (x); 7857 tree fndecl = cp_get_callee_fndecl_nofold (x); 7858 if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) 7859 is_consteval = true; 7860 } 7861 } 7862 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) 7863 { 7864 /* In C++14 an NSDMI can participate in aggregate initialization, 7865 and can refer to the address of the object being initialized, so 7866 we need to pass in the relevant VAR_DECL if we want to do the 7867 evaluation in a single pass. The evaluation will dynamically 7868 update ctx.values for the VAR_DECL. We use the same strategy 7869 for C++11 constexpr constructors that refer to the object being 7870 initialized. */ 7871 if (constexpr_dtor) 7872 { 7873 gcc_assert (object && VAR_P (object)); 7874 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); 7875 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); 7876 if (error_operand_p (DECL_INITIAL (object))) 7877 return t; 7878 ctx.ctor = unshare_expr (DECL_INITIAL (object)); 7879 TREE_READONLY (ctx.ctor) = false; 7880 /* Temporarily force decl_really_constant_value to return false 7881 for it, we want to use ctx.ctor for the current value instead. */ 7882 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; 7883 } 7884 else 7885 { 7886 ctx.ctor = build_constructor (type, NULL); 7887 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; 7888 } 7889 if (!object) 7890 { 7891 if (TREE_CODE (t) == TARGET_EXPR) 7892 object = TARGET_EXPR_SLOT (t); 7893 else if (TREE_CODE (t) == AGGR_INIT_EXPR) 7894 object = AGGR_INIT_EXPR_SLOT (t); 7895 } 7896 ctx.object = object; 7897 if (object) 7898 gcc_assert (same_type_ignoring_top_level_qualifiers_p 7899 (type, TREE_TYPE (object))); 7900 if (object && DECL_P (object)) 7901 global_ctx.values.put (object, ctx.ctor); 7902 if (TREE_CODE (r) == TARGET_EXPR) 7903 /* Avoid creating another CONSTRUCTOR when we expand the 7904 TARGET_EXPR. */ 7905 r = TARGET_EXPR_INITIAL (r); 7906 } 7907 7908 auto_vec<tree, 16> cleanups; 7909 global_ctx.cleanups = &cleanups; 7910 7911 if (manifestly_const_eval) 7912 instantiate_constexpr_fns (r); 7913 r = cxx_eval_constant_expression (&ctx, r, 7914 false, &non_constant_p, &overflow_p); 7915 7916 if (!constexpr_dtor) 7917 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); 7918 else 7919 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; 7920 7921 unsigned int i; 7922 tree cleanup; 7923 /* Evaluate the cleanups. */ 7924 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) 7925 cxx_eval_constant_expression (&ctx, cleanup, false, 7926 &non_constant_p, &overflow_p); 7927 7928 /* Mutable logic is a bit tricky: we want to allow initialization of 7929 constexpr variables with mutable members, but we can't copy those 7930 members to another constexpr variable. */ 7931 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) 7932 { 7933 if (!allow_non_constant) 7934 error ("%qE is not a constant expression because it refers to " 7935 "mutable subobjects of %qT", t, type); 7936 non_constant_p = true; 7937 } 7938 7939 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) 7940 { 7941 if (!allow_non_constant) 7942 error ("%qE is not a constant expression because it refers to " 7943 "an incompletely initialized variable", t); 7944 TREE_CONSTANT (r) = false; 7945 non_constant_p = true; 7946 } 7947 7948 if (!global_ctx.heap_vars.is_empty ()) 7949 { 7950 tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, 7951 NULL); 7952 unsigned int i; 7953 if (heap_var) 7954 { 7955 if (!allow_non_constant && !non_constant_p) 7956 error_at (DECL_SOURCE_LOCATION (heap_var), 7957 "%qE is not a constant expression because it refers to " 7958 "a result of %<operator new%>", t); 7959 r = t; 7960 non_constant_p = true; 7961 } 7962 FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) 7963 { 7964 if (DECL_NAME (heap_var) != heap_deleted_identifier) 7965 { 7966 if (!allow_non_constant && !non_constant_p) 7967 error_at (DECL_SOURCE_LOCATION (heap_var), 7968 "%qE is not a constant expression because allocated " 7969 "storage has not been deallocated", t); 7970 r = t; 7971 non_constant_p = true; 7972 } 7973 varpool_node::get (heap_var)->remove (); 7974 } 7975 } 7976 7977 /* Check that immediate invocation does not return an expression referencing 7978 any immediate function decls. */ 7979 if (is_consteval || in_immediate_context ()) 7980 if (tree immediate_fndecl 7981 = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, 7982 NULL)) 7983 { 7984 if (!allow_non_constant && !non_constant_p) 7985 error_at (cp_expr_loc_or_input_loc (t), 7986 "immediate evaluation returns address of immediate " 7987 "function %qD", immediate_fndecl); 7988 r = t; 7989 non_constant_p = true; 7990 } 7991 7992 if (non_constant_p) 7993 /* If we saw something bad, go back to our argument. The wrapping below is 7994 only for the cases of TREE_CONSTANT argument or overflow. */ 7995 r = t; 7996 7997 if (!non_constant_p && overflow_p) 7998 non_constant_p = true; 7999 8000 /* Unshare the result. */ 8001 bool should_unshare = true; 8002 if (r == t || (TREE_CODE (t) == TARGET_EXPR 8003 && TARGET_EXPR_INITIAL (t) == r)) 8004 should_unshare = false; 8005 8006 if (non_constant_p && !allow_non_constant) 8007 return error_mark_node; 8008 else if (constexpr_dtor) 8009 return r; 8010 else if (non_constant_p && TREE_CONSTANT (r)) 8011 r = mark_non_constant (r); 8012 else if (non_constant_p) 8013 return t; 8014 8015 if (should_unshare) 8016 r = unshare_expr (r); 8017 8018 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) 8019 { 8020 r = adjust_temp_type (type, r); 8021 if (TREE_CODE (t) == TARGET_EXPR 8022 && TARGET_EXPR_INITIAL (t) == r) 8023 return t; 8024 else if (TREE_CODE (t) == CONSTRUCTOR) 8025 ; 8026 else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t)) 8027 r = get_target_expr (r); 8028 else 8029 { 8030 r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); 8031 TREE_CONSTANT (r) = true; 8032 } 8033 } 8034 8035 /* Remember the original location if that wouldn't need a wrapper. */ 8036 if (location_t loc = EXPR_LOCATION (t)) 8037 protected_set_expr_location (r, loc); 8038 8039 return r; 8040} 8041 8042/* If T represents a constant expression returns its reduced value. 8043 Otherwise return error_mark_node. If T is dependent, then 8044 return NULL. */ 8045 8046tree 8047cxx_constant_value (tree t, tree decl) 8048{ 8049 return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl); 8050} 8051 8052/* As above, but respect SFINAE. */ 8053 8054tree 8055cxx_constant_value_sfinae (tree t, tree decl, tsubst_flags_t complain) 8056{ 8057 bool sfinae = !(complain & tf_error); 8058 tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true, false, decl); 8059 if (sfinae && !TREE_CONSTANT (r)) 8060 r = error_mark_node; 8061 return r; 8062} 8063 8064/* Like cxx_constant_value, but used for evaluation of constexpr destructors 8065 of constexpr variables. The actual initializer of DECL is not modified. */ 8066 8067void 8068cxx_constant_dtor (tree t, tree decl) 8069{ 8070 cxx_eval_outermost_constant_expr (t, false, true, true, true, decl); 8071} 8072 8073/* Helper routine for fold_simple function. Either return simplified 8074 expression T, otherwise NULL_TREE. 8075 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold 8076 even if we are within template-declaration. So be careful on call, as in 8077 such case types can be undefined. */ 8078 8079static tree 8080fold_simple_1 (tree t) 8081{ 8082 tree op1; 8083 enum tree_code code = TREE_CODE (t); 8084 8085 switch (code) 8086 { 8087 case INTEGER_CST: 8088 case REAL_CST: 8089 case VECTOR_CST: 8090 case FIXED_CST: 8091 case COMPLEX_CST: 8092 return t; 8093 8094 case SIZEOF_EXPR: 8095 return fold_sizeof_expr (t); 8096 8097 case ABS_EXPR: 8098 case ABSU_EXPR: 8099 case CONJ_EXPR: 8100 case REALPART_EXPR: 8101 case IMAGPART_EXPR: 8102 case NEGATE_EXPR: 8103 case BIT_NOT_EXPR: 8104 case TRUTH_NOT_EXPR: 8105 case NOP_EXPR: 8106 case VIEW_CONVERT_EXPR: 8107 case CONVERT_EXPR: 8108 case FLOAT_EXPR: 8109 case FIX_TRUNC_EXPR: 8110 case FIXED_CONVERT_EXPR: 8111 case ADDR_SPACE_CONVERT_EXPR: 8112 8113 op1 = TREE_OPERAND (t, 0); 8114 8115 t = const_unop (code, TREE_TYPE (t), op1); 8116 if (!t) 8117 return NULL_TREE; 8118 8119 if (CONVERT_EXPR_CODE_P (code) 8120 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) 8121 TREE_OVERFLOW (t) = false; 8122 return t; 8123 8124 default: 8125 return NULL_TREE; 8126 } 8127} 8128 8129/* If T is a simple constant expression, returns its simplified value. 8130 Otherwise returns T. In contrast to maybe_constant_value we 8131 simplify only few operations on constant-expressions, and we don't 8132 try to simplify constexpressions. */ 8133 8134tree 8135fold_simple (tree t) 8136{ 8137 if (processing_template_decl) 8138 return t; 8139 8140 tree r = fold_simple_1 (t); 8141 if (r) 8142 return r; 8143 8144 return t; 8145} 8146 8147/* Try folding the expression T to a simple constant. 8148 Returns that constant, otherwise returns T. */ 8149 8150tree 8151fold_to_constant (tree t) 8152{ 8153 tree r = fold (t); 8154 if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r)) 8155 return r; 8156 else 8157 return t; 8158} 8159 8160/* If T is a constant expression, returns its reduced value. 8161 Otherwise, if T does not have TREE_CONSTANT set, returns T. 8162 Otherwise, returns a version of T without TREE_CONSTANT. 8163 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated 8164 as per P0595. */ 8165 8166static GTY((deletable)) hash_map<tree, tree> *cv_cache; 8167 8168tree 8169maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) 8170{ 8171 tree r; 8172 8173 if (!is_nondependent_constant_expression (t)) 8174 { 8175 if (TREE_OVERFLOW_P (t) 8176 || (!processing_template_decl && TREE_CONSTANT (t))) 8177 t = mark_non_constant (t); 8178 return t; 8179 } 8180 else if (CONSTANT_CLASS_P (t)) 8181 /* No caching or evaluation needed. */ 8182 return t; 8183 8184 if (manifestly_const_eval) 8185 return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl); 8186 8187 if (cv_cache == NULL) 8188 cv_cache = hash_map<tree, tree>::create_ggc (101); 8189 if (tree *cached = cv_cache->get (t)) 8190 { 8191 r = *cached; 8192 if (r != t) 8193 { 8194 /* Clear processing_template_decl for sake of break_out_target_exprs; 8195 entries in the cv_cache are non-templated. */ 8196 processing_template_decl_sentinel ptds; 8197 8198 r = break_out_target_exprs (r, /*clear_loc*/true); 8199 protected_set_expr_location (r, EXPR_LOCATION (t)); 8200 } 8201 return r; 8202 } 8203 8204 /* Don't constant evaluate an unevaluated non-manifestly-constant operand, 8205 but at least try folding it to a simple constant. */ 8206 if (cp_unevaluated_operand) 8207 return fold_to_constant (t); 8208 8209 uid_sensitive_constexpr_evaluation_checker c; 8210 r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl); 8211 gcc_checking_assert (r == t 8212 || CONVERT_EXPR_P (t) 8213 || TREE_CODE (t) == VIEW_CONVERT_EXPR 8214 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) 8215 || !cp_tree_equal (r, t)); 8216 if (!c.evaluation_restricted_p ()) 8217 cv_cache->put (t, r); 8218 return r; 8219} 8220 8221/* Dispose of the whole CV_CACHE. */ 8222 8223static void 8224clear_cv_cache (void) 8225{ 8226 if (cv_cache != NULL) 8227 cv_cache->empty (); 8228} 8229 8230/* Dispose of the whole CV_CACHE and FOLD_CACHE. */ 8231 8232void 8233clear_cv_and_fold_caches () 8234{ 8235 clear_cv_cache (); 8236 clear_fold_cache (); 8237} 8238 8239/* Internal function handling expressions in templates for 8240 fold_non_dependent_expr and fold_non_dependent_init. 8241 8242 If we're in a template, but T isn't value dependent, simplify 8243 it. We're supposed to treat: 8244 8245 template <typename T> void f(T[1 + 1]); 8246 template <typename T> void f(T[2]); 8247 8248 as two declarations of the same function, for example. */ 8249 8250static tree 8251fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, 8252 bool manifestly_const_eval, 8253 tree object) 8254{ 8255 gcc_assert (processing_template_decl); 8256 8257 if (is_nondependent_constant_expression (t)) 8258 { 8259 processing_template_decl_sentinel s; 8260 t = instantiate_non_dependent_expr_internal (t, complain); 8261 8262 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) 8263 { 8264 if (TREE_OVERFLOW_P (t)) 8265 { 8266 t = build_nop (TREE_TYPE (t), t); 8267 TREE_CONSTANT (t) = false; 8268 } 8269 return t; 8270 } 8271 else if (CONSTANT_CLASS_P (t)) 8272 /* No evaluation needed. */ 8273 return t; 8274 8275 /* Don't constant evaluate an unevaluated non-manifestly-constant operand, 8276 but at least try folding it to a simple constant. */ 8277 if (cp_unevaluated_operand && !manifestly_const_eval) 8278 return fold_to_constant (t); 8279 8280 tree r = cxx_eval_outermost_constant_expr (t, true, true, 8281 manifestly_const_eval, 8282 false, object); 8283 /* cp_tree_equal looks through NOPs, so allow them. */ 8284 gcc_checking_assert (r == t 8285 || CONVERT_EXPR_P (t) 8286 || TREE_CODE (t) == VIEW_CONVERT_EXPR 8287 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) 8288 || !cp_tree_equal (r, t)); 8289 return r; 8290 } 8291 else if (TREE_OVERFLOW_P (t)) 8292 { 8293 t = build_nop (TREE_TYPE (t), t); 8294 TREE_CONSTANT (t) = false; 8295 } 8296 8297 return t; 8298} 8299 8300/* Like maybe_constant_value but first fully instantiate the argument. 8301 8302 Note: this is equivalent to instantiate_non_dependent_expr_sfinae 8303 (t, complain) followed by maybe_constant_value but is more efficient, 8304 because it calls instantiation_dependent_expression_p and 8305 potential_constant_expression at most once. 8306 The manifestly_const_eval argument is passed to maybe_constant_value. 8307 8308 Callers should generally pass their active complain, or if they are in a 8309 non-template, diagnosing context, they can use the default of 8310 tf_warning_or_error. Callers that might be within a template context, don't 8311 have a complain parameter, and aren't going to remember the result for long 8312 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node 8313 appropriately. */ 8314 8315tree 8316fold_non_dependent_expr (tree t, 8317 tsubst_flags_t complain /* = tf_warning_or_error */, 8318 bool manifestly_const_eval /* = false */, 8319 tree object /* = NULL_TREE */) 8320{ 8321 if (t == NULL_TREE) 8322 return NULL_TREE; 8323 8324 if (processing_template_decl) 8325 return fold_non_dependent_expr_template (t, complain, 8326 manifestly_const_eval, object); 8327 8328 return maybe_constant_value (t, object, manifestly_const_eval); 8329} 8330 8331/* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, 8332 return the original expression. */ 8333 8334tree 8335maybe_fold_non_dependent_expr (tree expr, 8336 tsubst_flags_t complain/*=tf_warning_or_error*/) 8337{ 8338 tree t = fold_non_dependent_expr (expr, complain); 8339 if (t && TREE_CONSTANT (t)) 8340 return t; 8341 8342 return expr; 8343} 8344 8345/* Like maybe_constant_init but first fully instantiate the argument. */ 8346 8347tree 8348fold_non_dependent_init (tree t, 8349 tsubst_flags_t complain /*=tf_warning_or_error*/, 8350 bool manifestly_const_eval /*=false*/, 8351 tree object /* = NULL_TREE */) 8352{ 8353 if (t == NULL_TREE) 8354 return NULL_TREE; 8355 8356 if (processing_template_decl) 8357 { 8358 t = fold_non_dependent_expr_template (t, complain, 8359 manifestly_const_eval, object); 8360 /* maybe_constant_init does this stripping, so do it here too. */ 8361 if (TREE_CODE (t) == TARGET_EXPR) 8362 { 8363 tree init = TARGET_EXPR_INITIAL (t); 8364 if (TREE_CODE (init) == CONSTRUCTOR) 8365 t = init; 8366 } 8367 return t; 8368 } 8369 8370 return maybe_constant_init (t, object, manifestly_const_eval); 8371} 8372 8373/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather 8374 than wrapped in a TARGET_EXPR. 8375 ALLOW_NON_CONSTANT is false if T is required to be a constant expression. 8376 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as 8377 per P0595 even when ALLOW_NON_CONSTANT is true. */ 8378 8379static tree 8380maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, 8381 bool manifestly_const_eval) 8382{ 8383 if (!t) 8384 return t; 8385 if (TREE_CODE (t) == EXPR_STMT) 8386 t = TREE_OPERAND (t, 0); 8387 if (TREE_CODE (t) == CONVERT_EXPR 8388 && VOID_TYPE_P (TREE_TYPE (t))) 8389 t = TREE_OPERAND (t, 0); 8390 if (TREE_CODE (t) == INIT_EXPR) 8391 t = TREE_OPERAND (t, 1); 8392 if (TREE_CODE (t) == TARGET_EXPR) 8393 t = TARGET_EXPR_INITIAL (t); 8394 if (!is_nondependent_static_init_expression (t)) 8395 /* Don't try to evaluate it. */; 8396 else if (CONSTANT_CLASS_P (t) && allow_non_constant) 8397 /* No evaluation needed. */; 8398 else 8399 t = cxx_eval_outermost_constant_expr (t, allow_non_constant, 8400 /*strict*/false, 8401 manifestly_const_eval, false, decl); 8402 if (TREE_CODE (t) == TARGET_EXPR) 8403 { 8404 tree init = TARGET_EXPR_INITIAL (t); 8405 if (TREE_CODE (init) == CONSTRUCTOR) 8406 t = init; 8407 } 8408 return t; 8409} 8410 8411/* Wrapper for maybe_constant_init_1 which permits non constants. */ 8412 8413tree 8414maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) 8415{ 8416 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); 8417} 8418 8419/* Wrapper for maybe_constant_init_1 which does not permit non constants. */ 8420 8421tree 8422cxx_constant_init (tree t, tree decl) 8423{ 8424 return maybe_constant_init_1 (t, decl, false, true); 8425} 8426 8427#if 0 8428/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ 8429/* Return true if the object referred to by REF has automatic or thread 8430 local storage. */ 8431 8432enum { ck_ok, ck_bad, ck_unknown }; 8433static int 8434check_automatic_or_tls (tree ref) 8435{ 8436 machine_mode mode; 8437 poly_int64 bitsize, bitpos; 8438 tree offset; 8439 int volatilep = 0, unsignedp = 0; 8440 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, 8441 &mode, &unsignedp, &volatilep, false); 8442 duration_kind dk; 8443 8444 /* If there isn't a decl in the middle, we don't know the linkage here, 8445 and this isn't a constant expression anyway. */ 8446 if (!DECL_P (decl)) 8447 return ck_unknown; 8448 dk = decl_storage_duration (decl); 8449 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; 8450} 8451#endif 8452 8453/* Data structure for passing data from potential_constant_expression_1 8454 to check_for_return_continue via cp_walk_tree. */ 8455struct check_for_return_continue_data { 8456 hash_set<tree> *pset; 8457 tree continue_stmt; 8458 tree break_stmt; 8459}; 8460 8461/* Helper function for potential_constant_expression_1 SWITCH_STMT handling, 8462 called through cp_walk_tree. Return the first RETURN_EXPR found, or note 8463 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ 8464static tree 8465check_for_return_continue (tree *tp, int *walk_subtrees, void *data) 8466{ 8467 tree t = *tp, s, b; 8468 check_for_return_continue_data *d = (check_for_return_continue_data *) data; 8469 switch (TREE_CODE (t)) 8470 { 8471 case RETURN_EXPR: 8472 return t; 8473 8474 case CONTINUE_STMT: 8475 if (d->continue_stmt == NULL_TREE) 8476 d->continue_stmt = t; 8477 break; 8478 8479 case BREAK_STMT: 8480 if (d->break_stmt == NULL_TREE) 8481 d->break_stmt = t; 8482 break; 8483 8484#define RECUR(x) \ 8485 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ 8486 d->pset)) \ 8487 return r 8488 8489 /* For loops, walk subtrees manually, so that continue stmts found 8490 inside of the bodies of the loops are ignored. */ 8491 case DO_STMT: 8492 *walk_subtrees = 0; 8493 RECUR (DO_COND (t)); 8494 s = d->continue_stmt; 8495 b = d->break_stmt; 8496 RECUR (DO_BODY (t)); 8497 d->continue_stmt = s; 8498 d->break_stmt = b; 8499 break; 8500 8501 case WHILE_STMT: 8502 *walk_subtrees = 0; 8503 RECUR (WHILE_COND (t)); 8504 s = d->continue_stmt; 8505 b = d->break_stmt; 8506 RECUR (WHILE_BODY (t)); 8507 d->continue_stmt = s; 8508 d->break_stmt = b; 8509 break; 8510 8511 case FOR_STMT: 8512 *walk_subtrees = 0; 8513 RECUR (FOR_INIT_STMT (t)); 8514 RECUR (FOR_COND (t)); 8515 RECUR (FOR_EXPR (t)); 8516 s = d->continue_stmt; 8517 b = d->break_stmt; 8518 RECUR (FOR_BODY (t)); 8519 d->continue_stmt = s; 8520 d->break_stmt = b; 8521 break; 8522 8523 case RANGE_FOR_STMT: 8524 *walk_subtrees = 0; 8525 RECUR (RANGE_FOR_EXPR (t)); 8526 s = d->continue_stmt; 8527 b = d->break_stmt; 8528 RECUR (RANGE_FOR_BODY (t)); 8529 d->continue_stmt = s; 8530 d->break_stmt = b; 8531 break; 8532 8533 case SWITCH_STMT: 8534 *walk_subtrees = 0; 8535 RECUR (SWITCH_STMT_COND (t)); 8536 b = d->break_stmt; 8537 RECUR (SWITCH_STMT_BODY (t)); 8538 d->break_stmt = b; 8539 break; 8540#undef RECUR 8541 8542 case STATEMENT_LIST: 8543 case CONSTRUCTOR: 8544 break; 8545 8546 default: 8547 if (!EXPR_P (t)) 8548 *walk_subtrees = 0; 8549 break; 8550 } 8551 8552 return NULL_TREE; 8553} 8554 8555/* Return true if T denotes a potentially constant expression. Issue 8556 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, 8557 an lvalue-rvalue conversion is implied. If NOW is true, we want to 8558 consider the expression in the current context, independent of constexpr 8559 substitution. 8560 8561 C++0x [expr.const] used to say 8562 8563 6 An expression is a potential constant expression if it is 8564 a constant expression where all occurrences of function 8565 parameters are replaced by arbitrary constant expressions 8566 of the appropriate type. 8567 8568 2 A conditional expression is a constant expression unless it 8569 involves one of the following as a potentially evaluated 8570 subexpression (3.2), but subexpressions of logical AND (5.14), 8571 logical OR (5.15), and conditional (5.16) operations that are 8572 not evaluated are not considered. */ 8573 8574static bool 8575potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, 8576 tsubst_flags_t flags, tree *jump_target) 8577{ 8578#define RECUR(T,RV) \ 8579 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target) 8580 8581 enum { any = false, rval = true }; 8582 int i; 8583 tree tmp; 8584 8585 if (t == error_mark_node) 8586 return false; 8587 if (t == NULL_TREE) 8588 return true; 8589 location_t loc = cp_expr_loc_or_input_loc (t); 8590 8591 if (*jump_target) 8592 /* If we are jumping, ignore everything. This is simpler than the 8593 cxx_eval_constant_expression handling because we only need to be 8594 conservatively correct, and we don't necessarily have a constant value 8595 available, so we don't bother with switch tracking. */ 8596 return true; 8597 8598 if (TREE_THIS_VOLATILE (t) && want_rval) 8599 { 8600 if (flags & tf_error) 8601 error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " 8602 "%qE with type %qT", t, TREE_TYPE (t)); 8603 return false; 8604 } 8605 if (CONSTANT_CLASS_P (t)) 8606 return true; 8607 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) 8608 && TREE_TYPE (t) == error_mark_node) 8609 return false; 8610 8611 switch (TREE_CODE (t)) 8612 { 8613 case FUNCTION_DECL: 8614 case BASELINK: 8615 case TEMPLATE_DECL: 8616 case OVERLOAD: 8617 case TEMPLATE_ID_EXPR: 8618 case LABEL_DECL: 8619 case CASE_LABEL_EXPR: 8620 case PREDICT_EXPR: 8621 case CONST_DECL: 8622 case SIZEOF_EXPR: 8623 case ALIGNOF_EXPR: 8624 case OFFSETOF_EXPR: 8625 case NOEXCEPT_EXPR: 8626 case TEMPLATE_PARM_INDEX: 8627 case TRAIT_EXPR: 8628 case IDENTIFIER_NODE: 8629 case USERDEF_LITERAL: 8630 /* We can see a FIELD_DECL in a pointer-to-member expression. */ 8631 case FIELD_DECL: 8632 case RESULT_DECL: 8633 case USING_DECL: 8634 case USING_STMT: 8635 case PLACEHOLDER_EXPR: 8636 case REQUIRES_EXPR: 8637 case STATIC_ASSERT: 8638 case DEBUG_BEGIN_STMT: 8639 return true; 8640 8641 case RETURN_EXPR: 8642 if (!RECUR (TREE_OPERAND (t, 0), any)) 8643 return false; 8644 /* FALLTHROUGH */ 8645 8646 case BREAK_STMT: 8647 case CONTINUE_STMT: 8648 *jump_target = t; 8649 return true; 8650 8651 case PARM_DECL: 8652 if (now && want_rval) 8653 { 8654 tree type = TREE_TYPE (t); 8655 if ((processing_template_decl && !COMPLETE_TYPE_P (type)) 8656 || dependent_type_p (type) 8657 || is_really_empty_class (type, /*ignore_vptr*/false)) 8658 /* An empty class has no data to read. */ 8659 return true; 8660 if (flags & tf_error) 8661 error ("%qE is not a constant expression", t); 8662 return false; 8663 } 8664 return true; 8665 8666 case AGGR_INIT_EXPR: 8667 case CALL_EXPR: 8668 /* -- an invocation of a function other than a constexpr function 8669 or a constexpr constructor. */ 8670 { 8671 tree fun = get_function_named_in_call (t); 8672 const int nargs = call_expr_nargs (t); 8673 i = 0; 8674 8675 if (fun == NULL_TREE) 8676 { 8677 /* Reset to allow the function to continue past the end 8678 of the block below. Otherwise return early. */ 8679 bool bail = true; 8680 8681 if (TREE_CODE (t) == CALL_EXPR 8682 && CALL_EXPR_FN (t) == NULL_TREE) 8683 switch (CALL_EXPR_IFN (t)) 8684 { 8685 /* These should be ignored, they are optimized away from 8686 constexpr functions. */ 8687 case IFN_UBSAN_NULL: 8688 case IFN_UBSAN_BOUNDS: 8689 case IFN_UBSAN_VPTR: 8690 case IFN_FALLTHROUGH: 8691 return true; 8692 8693 case IFN_ADD_OVERFLOW: 8694 case IFN_SUB_OVERFLOW: 8695 case IFN_MUL_OVERFLOW: 8696 case IFN_LAUNDER: 8697 case IFN_VEC_CONVERT: 8698 bail = false; 8699 break; 8700 8701 default: 8702 break; 8703 } 8704 8705 if (bail) 8706 { 8707 /* fold_call_expr can't do anything with IFN calls. */ 8708 if (flags & tf_error) 8709 error_at (loc, "call to internal function %qE", t); 8710 return false; 8711 } 8712 } 8713 8714 if (fun && is_overloaded_fn (fun)) 8715 { 8716 if (TREE_CODE (fun) == FUNCTION_DECL) 8717 { 8718 if (builtin_valid_in_constant_expr_p (fun)) 8719 return true; 8720 if (!maybe_constexpr_fn (fun) 8721 /* Allow any built-in function; if the expansion 8722 isn't constant, we'll deal with that then. */ 8723 && !fndecl_built_in_p (fun) 8724 /* In C++20, replaceable global allocation functions 8725 are constant expressions. */ 8726 && (!cxx_replaceable_global_alloc_fn (fun) 8727 || TREE_CODE (t) != CALL_EXPR 8728 || (!CALL_FROM_NEW_OR_DELETE_P (t) 8729 && (current_function_decl == NULL_TREE 8730 || !is_std_allocator_allocate 8731 (current_function_decl)))) 8732 /* Allow placement new in std::construct_at. */ 8733 && (!cxx_placement_new_fn (fun) 8734 || TREE_CODE (t) != CALL_EXPR 8735 || current_function_decl == NULL_TREE 8736 || !is_std_construct_at (current_function_decl)) 8737 && !cxx_dynamic_cast_fn_p (fun)) 8738 { 8739 if (flags & tf_error) 8740 { 8741 error_at (loc, "call to non-%<constexpr%> function %qD", 8742 fun); 8743 explain_invalid_constexpr_fn (fun); 8744 } 8745 return false; 8746 } 8747 /* A call to a non-static member function takes the address 8748 of the object as the first argument. But in a constant 8749 expression the address will be folded away, so look 8750 through it now. */ 8751 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) 8752 && !DECL_CONSTRUCTOR_P (fun)) 8753 { 8754 tree x = get_nth_callarg (t, 0); 8755 if (is_this_parameter (x)) 8756 return true; 8757 /* Don't require an immediately constant value, as 8758 constexpr substitution might not use the value. */ 8759 bool sub_now = false; 8760 if (!potential_constant_expression_1 (x, rval, strict, 8761 sub_now, flags, 8762 jump_target)) 8763 return false; 8764 i = 1; 8765 } 8766 } 8767 else 8768 { 8769 if (!RECUR (fun, true)) 8770 return false; 8771 fun = get_first_fn (fun); 8772 } 8773 /* Skip initial arguments to base constructors. */ 8774 if (DECL_BASE_CONSTRUCTOR_P (fun)) 8775 i = num_artificial_parms_for (fun); 8776 fun = DECL_ORIGIN (fun); 8777 } 8778 else if (fun) 8779 { 8780 if (RECUR (fun, FUNCTION_POINTER_TYPE_P (fun) ? rval : any)) 8781 /* Might end up being a constant function pointer. But it 8782 could also be a function object with constexpr op(), so 8783 we pass 'any' so that the underlying VAR_DECL is deemed 8784 as potentially-constant even though it wasn't declared 8785 constexpr. */; 8786 else 8787 return false; 8788 } 8789 for (; i < nargs; ++i) 8790 { 8791 tree x = get_nth_callarg (t, i); 8792 /* In a template, reference arguments haven't been converted to 8793 REFERENCE_TYPE and we might not even know if the parameter 8794 is a reference, so accept lvalue constants too. */ 8795 bool rv = processing_template_decl ? any : rval; 8796 /* Don't require an immediately constant value, as constexpr 8797 substitution might not use the value of the argument. */ 8798 bool sub_now = false; 8799 if (!potential_constant_expression_1 (x, rv, strict, 8800 sub_now, flags, jump_target)) 8801 return false; 8802 } 8803 return true; 8804 } 8805 8806 case NON_LVALUE_EXPR: 8807 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to 8808 -- an lvalue of integral type that refers to a non-volatile 8809 const variable or static data member initialized with 8810 constant expressions, or 8811 8812 -- an lvalue of literal type that refers to non-volatile 8813 object defined with constexpr, or that refers to a 8814 sub-object of such an object; */ 8815 return RECUR (TREE_OPERAND (t, 0), rval); 8816 8817 case VAR_DECL: 8818 if (DECL_HAS_VALUE_EXPR_P (t)) 8819 { 8820 if (now && is_normal_capture_proxy (t)) 8821 { 8822 /* -- in a lambda-expression, a reference to this or to a 8823 variable with automatic storage duration defined outside that 8824 lambda-expression, where the reference would be an 8825 odr-use. */ 8826 8827 if (want_rval) 8828 /* Since we're doing an lvalue-rvalue conversion, this might 8829 not be an odr-use, so evaluate the variable directly. */ 8830 return RECUR (DECL_CAPTURED_VARIABLE (t), rval); 8831 8832 if (flags & tf_error) 8833 { 8834 tree cap = DECL_CAPTURED_VARIABLE (t); 8835 error ("lambda capture of %qE is not a constant expression", 8836 cap); 8837 if (decl_constant_var_p (cap)) 8838 inform (input_location, "because it is used as a glvalue"); 8839 } 8840 return false; 8841 } 8842 /* Treat __PRETTY_FUNCTION__ inside a template function as 8843 potentially-constant. */ 8844 else if (DECL_PRETTY_FUNCTION_P (t) 8845 && DECL_VALUE_EXPR (t) == error_mark_node) 8846 return true; 8847 return RECUR (DECL_VALUE_EXPR (t), rval); 8848 } 8849 if (want_rval 8850 && !var_in_maybe_constexpr_fn (t) 8851 && !type_dependent_expression_p (t) 8852 && !decl_maybe_constant_var_p (t) 8853 && (strict 8854 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) 8855 || (DECL_INITIAL (t) 8856 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) 8857 && COMPLETE_TYPE_P (TREE_TYPE (t)) 8858 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 8859 { 8860 if (flags & tf_error) 8861 non_const_var_error (loc, t); 8862 return false; 8863 } 8864 return true; 8865 8866 case NOP_EXPR: 8867 if (REINTERPRET_CAST_P (t)) 8868 { 8869 if (flags & tf_error) 8870 error_at (loc, "%<reinterpret_cast%> is not a constant expression"); 8871 return false; 8872 } 8873 /* FALLTHRU */ 8874 case CONVERT_EXPR: 8875 case VIEW_CONVERT_EXPR: 8876 /* -- a reinterpret_cast. FIXME not implemented, and this rule 8877 may change to something more specific to type-punning (DR 1312). */ 8878 { 8879 tree from = TREE_OPERAND (t, 0); 8880 if (location_wrapper_p (t)) 8881 return (RECUR (from, want_rval)); 8882 if (INDIRECT_TYPE_P (TREE_TYPE (t))) 8883 { 8884 STRIP_ANY_LOCATION_WRAPPER (from); 8885 if (TREE_CODE (from) == INTEGER_CST 8886 && !integer_zerop (from)) 8887 { 8888 if (flags & tf_error) 8889 error_at (loc, 8890 "%<reinterpret_cast%> from integer to pointer"); 8891 return false; 8892 } 8893 } 8894 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); 8895 } 8896 8897 case ADDRESSOF_EXPR: 8898 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ 8899 t = TREE_OPERAND (t, 0); 8900 goto handle_addr_expr; 8901 8902 case ADDR_EXPR: 8903 /* -- a unary operator & that is applied to an lvalue that 8904 designates an object with thread or automatic storage 8905 duration; */ 8906 t = TREE_OPERAND (t, 0); 8907 8908 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) 8909 /* A pointer-to-member constant. */ 8910 return true; 8911 8912 handle_addr_expr: 8913#if 0 8914 /* FIXME adjust when issue 1197 is fully resolved. For now don't do 8915 any checking here, as we might dereference the pointer later. If 8916 we remove this code, also remove check_automatic_or_tls. */ 8917 i = check_automatic_or_tls (t); 8918 if (i == ck_ok) 8919 return true; 8920 if (i == ck_bad) 8921 { 8922 if (flags & tf_error) 8923 error ("address-of an object %qE with thread local or " 8924 "automatic storage is not a constant expression", t); 8925 return false; 8926 } 8927#endif 8928 return RECUR (t, any); 8929 8930 case COMPONENT_REF: 8931 case ARROW_EXPR: 8932 case OFFSET_REF: 8933 /* -- a class member access unless its postfix-expression is 8934 of literal type or of pointer to literal type. */ 8935 /* This test would be redundant, as it follows from the 8936 postfix-expression being a potential constant expression. */ 8937 if (type_unknown_p (t)) 8938 return true; 8939 if (is_overloaded_fn (t)) 8940 /* In a template, a COMPONENT_REF of a function expresses ob.fn(), 8941 which uses ob as an lvalue. */ 8942 want_rval = false; 8943 gcc_fallthrough (); 8944 8945 case REALPART_EXPR: 8946 case IMAGPART_EXPR: 8947 case BIT_FIELD_REF: 8948 return RECUR (TREE_OPERAND (t, 0), want_rval); 8949 8950 case EXPR_PACK_EXPANSION: 8951 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); 8952 8953 case INDIRECT_REF: 8954 { 8955 tree x = TREE_OPERAND (t, 0); 8956 STRIP_NOPS (x); 8957 if (is_this_parameter (x) && !is_capture_proxy (x)) 8958 { 8959 if (!var_in_maybe_constexpr_fn (x)) 8960 { 8961 if (flags & tf_error) 8962 error_at (loc, "use of %<this%> in a constant expression"); 8963 return false; 8964 } 8965 return true; 8966 } 8967 return RECUR (x, rval); 8968 } 8969 8970 case STATEMENT_LIST: 8971 for (tree stmt : tsi_range (t)) 8972 if (!RECUR (stmt, any)) 8973 return false; 8974 return true; 8975 8976 case MODIFY_EXPR: 8977 if (cxx_dialect < cxx14) 8978 goto fail; 8979 if (!RECUR (TREE_OPERAND (t, 0), any)) 8980 return false; 8981 /* Just ignore clobbers. */ 8982 if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) 8983 return true; 8984 if (!RECUR (TREE_OPERAND (t, 1), rval)) 8985 return false; 8986 return true; 8987 8988 case MODOP_EXPR: 8989 if (cxx_dialect < cxx14) 8990 goto fail; 8991 if (!RECUR (TREE_OPERAND (t, 0), rval)) 8992 return false; 8993 if (!RECUR (TREE_OPERAND (t, 2), rval)) 8994 return false; 8995 return true; 8996 8997 case DO_STMT: 8998 if (!RECUR (DO_COND (t), rval)) 8999 return false; 9000 if (!RECUR (DO_BODY (t), any)) 9001 return false; 9002 if (breaks (jump_target) || continues (jump_target)) 9003 *jump_target = NULL_TREE; 9004 return true; 9005 9006 case FOR_STMT: 9007 if (!RECUR (FOR_INIT_STMT (t), any)) 9008 return false; 9009 tmp = FOR_COND (t); 9010 if (!RECUR (tmp, rval)) 9011 return false; 9012 if (tmp) 9013 { 9014 if (!processing_template_decl) 9015 tmp = cxx_eval_outermost_constant_expr (tmp, true); 9016 /* If we couldn't evaluate the condition, it might not ever be 9017 true. */ 9018 if (!integer_onep (tmp)) 9019 { 9020 /* Before returning true, check if the for body can contain 9021 a return. */ 9022 hash_set<tree> pset; 9023 check_for_return_continue_data data = { &pset, NULL_TREE, 9024 NULL_TREE }; 9025 if (tree ret_expr 9026 = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, 9027 &data, &pset)) 9028 *jump_target = ret_expr; 9029 return true; 9030 } 9031 } 9032 if (!RECUR (FOR_EXPR (t), any)) 9033 return false; 9034 if (!RECUR (FOR_BODY (t), any)) 9035 return false; 9036 if (breaks (jump_target) || continues (jump_target)) 9037 *jump_target = NULL_TREE; 9038 return true; 9039 9040 case RANGE_FOR_STMT: 9041 if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) 9042 return false; 9043 if (!RECUR (RANGE_FOR_EXPR (t), any)) 9044 return false; 9045 if (!RECUR (RANGE_FOR_BODY (t), any)) 9046 return false; 9047 if (breaks (jump_target) || continues (jump_target)) 9048 *jump_target = NULL_TREE; 9049 return true; 9050 9051 case WHILE_STMT: 9052 tmp = WHILE_COND (t); 9053 if (!RECUR (tmp, rval)) 9054 return false; 9055 if (!processing_template_decl) 9056 tmp = cxx_eval_outermost_constant_expr (tmp, true); 9057 /* If we couldn't evaluate the condition, it might not ever be true. */ 9058 if (!integer_onep (tmp)) 9059 { 9060 /* Before returning true, check if the while body can contain 9061 a return. */ 9062 hash_set<tree> pset; 9063 check_for_return_continue_data data = { &pset, NULL_TREE, 9064 NULL_TREE }; 9065 if (tree ret_expr 9066 = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, 9067 &data, &pset)) 9068 *jump_target = ret_expr; 9069 return true; 9070 } 9071 if (!RECUR (WHILE_BODY (t), any)) 9072 return false; 9073 if (breaks (jump_target) || continues (jump_target)) 9074 *jump_target = NULL_TREE; 9075 return true; 9076 9077 case SWITCH_STMT: 9078 if (!RECUR (SWITCH_STMT_COND (t), rval)) 9079 return false; 9080 /* FIXME we don't check SWITCH_STMT_BODY currently, because even 9081 unreachable labels would be checked and it is enough if there is 9082 a single switch cond value for which it is a valid constant 9083 expression. We need to check if there are any RETURN_EXPRs 9084 or CONTINUE_STMTs inside of the body though, as in that case 9085 we need to set *jump_target. */ 9086 else 9087 { 9088 hash_set<tree> pset; 9089 check_for_return_continue_data data = { &pset, NULL_TREE, 9090 NULL_TREE }; 9091 if (tree ret_expr 9092 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, 9093 &data, &pset)) 9094 /* The switch might return. */ 9095 *jump_target = ret_expr; 9096 else if (data.continue_stmt) 9097 /* The switch can't return, but might continue. */ 9098 *jump_target = data.continue_stmt; 9099 } 9100 return true; 9101 9102 case STMT_EXPR: 9103 return RECUR (STMT_EXPR_STMT (t), rval); 9104 9105 case LAMBDA_EXPR: 9106 if (cxx_dialect >= cxx17) 9107 /* In C++17 lambdas can be constexpr, don't give up yet. */ 9108 return true; 9109 else if (flags & tf_error) 9110 error_at (loc, "lambda-expression is not a constant expression " 9111 "before C++17"); 9112 return false; 9113 9114 case DYNAMIC_CAST_EXPR: 9115 case PSEUDO_DTOR_EXPR: 9116 case NEW_EXPR: 9117 case VEC_NEW_EXPR: 9118 case DELETE_EXPR: 9119 case VEC_DELETE_EXPR: 9120 case THROW_EXPR: 9121 case OMP_PARALLEL: 9122 case OMP_TASK: 9123 case OMP_FOR: 9124 case OMP_SIMD: 9125 case OMP_DISTRIBUTE: 9126 case OMP_TASKLOOP: 9127 case OMP_LOOP: 9128 case OMP_TEAMS: 9129 case OMP_TARGET_DATA: 9130 case OMP_TARGET: 9131 case OMP_SECTIONS: 9132 case OMP_ORDERED: 9133 case OMP_CRITICAL: 9134 case OMP_SINGLE: 9135 case OMP_SCAN: 9136 case OMP_SCOPE: 9137 case OMP_SECTION: 9138 case OMP_MASTER: 9139 case OMP_MASKED: 9140 case OMP_TASKGROUP: 9141 case OMP_TARGET_UPDATE: 9142 case OMP_TARGET_ENTER_DATA: 9143 case OMP_TARGET_EXIT_DATA: 9144 case OMP_ATOMIC: 9145 case OMP_ATOMIC_READ: 9146 case OMP_ATOMIC_CAPTURE_OLD: 9147 case OMP_ATOMIC_CAPTURE_NEW: 9148 case OMP_DEPOBJ: 9149 case OACC_PARALLEL: 9150 case OACC_KERNELS: 9151 case OACC_SERIAL: 9152 case OACC_DATA: 9153 case OACC_HOST_DATA: 9154 case OACC_LOOP: 9155 case OACC_CACHE: 9156 case OACC_DECLARE: 9157 case OACC_ENTER_DATA: 9158 case OACC_EXIT_DATA: 9159 case OACC_UPDATE: 9160 /* GCC internal stuff. */ 9161 case VA_ARG_EXPR: 9162 case TRANSACTION_EXPR: 9163 case AT_ENCODE_EXPR: 9164 fail: 9165 if (flags & tf_error) 9166 error_at (loc, "expression %qE is not a constant expression", t); 9167 return false; 9168 9169 case ASM_EXPR: 9170 if (flags & tf_error) 9171 inline_asm_in_constexpr_error (loc); 9172 return false; 9173 9174 case OBJ_TYPE_REF: 9175 if (cxx_dialect >= cxx20) 9176 /* In C++20 virtual calls can be constexpr, don't give up yet. */ 9177 return true; 9178 else if (flags & tf_error) 9179 error_at (loc, 9180 "virtual functions cannot be %<constexpr%> before C++20"); 9181 return false; 9182 9183 case TYPEID_EXPR: 9184 /* In C++20, a typeid expression whose operand is of polymorphic 9185 class type can be constexpr. */ 9186 { 9187 tree e = TREE_OPERAND (t, 0); 9188 if (cxx_dialect < cxx20 9189 && strict 9190 && !TYPE_P (e) 9191 && !type_dependent_expression_p (e) 9192 && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) 9193 { 9194 if (flags & tf_error) 9195 error_at (loc, "%<typeid%> is not a constant expression " 9196 "because %qE is of polymorphic type", e); 9197 return false; 9198 } 9199 return true; 9200 } 9201 9202 case POINTER_DIFF_EXPR: 9203 case MINUS_EXPR: 9204 want_rval = true; 9205 goto binary; 9206 9207 case LT_EXPR: 9208 case LE_EXPR: 9209 case GT_EXPR: 9210 case GE_EXPR: 9211 case EQ_EXPR: 9212 case NE_EXPR: 9213 case SPACESHIP_EXPR: 9214 want_rval = true; 9215 goto binary; 9216 9217 case PREINCREMENT_EXPR: 9218 case POSTINCREMENT_EXPR: 9219 case PREDECREMENT_EXPR: 9220 case POSTDECREMENT_EXPR: 9221 if (cxx_dialect < cxx14) 9222 goto fail; 9223 goto unary; 9224 9225 case BIT_NOT_EXPR: 9226 /* A destructor. */ 9227 if (TYPE_P (TREE_OPERAND (t, 0))) 9228 return true; 9229 /* fall through. */ 9230 9231 case CONJ_EXPR: 9232 case SAVE_EXPR: 9233 case FIX_TRUNC_EXPR: 9234 case FLOAT_EXPR: 9235 case NEGATE_EXPR: 9236 case ABS_EXPR: 9237 case ABSU_EXPR: 9238 case TRUTH_NOT_EXPR: 9239 case FIXED_CONVERT_EXPR: 9240 case UNARY_PLUS_EXPR: 9241 case UNARY_LEFT_FOLD_EXPR: 9242 case UNARY_RIGHT_FOLD_EXPR: 9243 unary: 9244 return RECUR (TREE_OPERAND (t, 0), rval); 9245 9246 case CAST_EXPR: 9247 case CONST_CAST_EXPR: 9248 case STATIC_CAST_EXPR: 9249 case REINTERPRET_CAST_EXPR: 9250 case IMPLICIT_CONV_EXPR: 9251 if (cxx_dialect < cxx11 9252 && !dependent_type_p (TREE_TYPE (t)) 9253 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) 9254 /* In C++98, a conversion to non-integral type can't be part of a 9255 constant expression. */ 9256 { 9257 if (flags & tf_error) 9258 error_at (loc, 9259 "cast to non-integral type %qT in a constant expression", 9260 TREE_TYPE (t)); 9261 return false; 9262 } 9263 /* This might be a conversion from a class to a (potentially) literal 9264 type. Let's consider it potentially constant since the conversion 9265 might be a constexpr user-defined conversion. */ 9266 else if (cxx_dialect >= cxx11 9267 && (dependent_type_p (TREE_TYPE (t)) 9268 || !COMPLETE_TYPE_P (TREE_TYPE (t)) 9269 || literal_type_p (TREE_TYPE (t))) 9270 && TREE_OPERAND (t, 0)) 9271 { 9272 tree type = TREE_TYPE (TREE_OPERAND (t, 0)); 9273 /* If this is a dependent type, it could end up being a class 9274 with conversions. */ 9275 if (type == NULL_TREE || WILDCARD_TYPE_P (type)) 9276 return true; 9277 /* Or a non-dependent class which has conversions. */ 9278 else if (CLASS_TYPE_P (type) 9279 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) 9280 return true; 9281 } 9282 9283 return (RECUR (TREE_OPERAND (t, 0), 9284 !TYPE_REF_P (TREE_TYPE (t)))); 9285 9286 case BIND_EXPR: 9287 return RECUR (BIND_EXPR_BODY (t), want_rval); 9288 9289 case NON_DEPENDENT_EXPR: 9290 /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by 9291 constexpr evaluation or tsubst, so fold_non_dependent_expr can't 9292 do anything useful with it. And we shouldn't see it in a context 9293 where a constant expression is strictly required, hence the assert. */ 9294 gcc_checking_assert (!(flags & tf_error)); 9295 return false; 9296 9297 case CLEANUP_POINT_EXPR: 9298 case MUST_NOT_THROW_EXPR: 9299 case TRY_CATCH_EXPR: 9300 case TRY_BLOCK: 9301 case EH_SPEC_BLOCK: 9302 case EXPR_STMT: 9303 case PAREN_EXPR: 9304 /* For convenience. */ 9305 case LOOP_EXPR: 9306 case EXIT_EXPR: 9307 return RECUR (TREE_OPERAND (t, 0), want_rval); 9308 9309 case DECL_EXPR: 9310 tmp = DECL_EXPR_DECL (t); 9311 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) 9312 { 9313 if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)) 9314 { 9315 if (flags & tf_error) 9316 error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined " 9317 "%<thread_local%> in %<constexpr%> context", tmp); 9318 return false; 9319 } 9320 else if (TREE_STATIC (tmp)) 9321 { 9322 if (flags & tf_error) 9323 error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined " 9324 "%<static%> in %<constexpr%> context", tmp); 9325 return false; 9326 } 9327 else if (!check_for_uninitialized_const_var 9328 (tmp, /*constexpr_context_p=*/true, flags)) 9329 return false; 9330 } 9331 return RECUR (tmp, want_rval); 9332 9333 case TRY_FINALLY_EXPR: 9334 return (RECUR (TREE_OPERAND (t, 0), want_rval) 9335 && RECUR (TREE_OPERAND (t, 1), any)); 9336 9337 case SCOPE_REF: 9338 return RECUR (TREE_OPERAND (t, 1), want_rval); 9339 9340 case TARGET_EXPR: 9341 if (!TARGET_EXPR_DIRECT_INIT_P (t) 9342 && !literal_type_p (TREE_TYPE (t))) 9343 { 9344 if (flags & tf_error) 9345 { 9346 auto_diagnostic_group d; 9347 error_at (loc, "temporary of non-literal type %qT in a " 9348 "constant expression", TREE_TYPE (t)); 9349 explain_non_literal_class (TREE_TYPE (t)); 9350 } 9351 return false; 9352 } 9353 /* FALLTHRU */ 9354 case INIT_EXPR: 9355 return RECUR (TREE_OPERAND (t, 1), rval); 9356 9357 case CONSTRUCTOR: 9358 { 9359 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); 9360 constructor_elt *ce; 9361 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 9362 if (!RECUR (ce->value, want_rval)) 9363 return false; 9364 return true; 9365 } 9366 9367 case TREE_LIST: 9368 { 9369 gcc_assert (TREE_PURPOSE (t) == NULL_TREE 9370 || DECL_P (TREE_PURPOSE (t))); 9371 if (!RECUR (TREE_VALUE (t), want_rval)) 9372 return false; 9373 if (TREE_CHAIN (t) == NULL_TREE) 9374 return true; 9375 return RECUR (TREE_CHAIN (t), want_rval); 9376 } 9377 9378 case TRUNC_DIV_EXPR: 9379 case CEIL_DIV_EXPR: 9380 case FLOOR_DIV_EXPR: 9381 case ROUND_DIV_EXPR: 9382 case TRUNC_MOD_EXPR: 9383 case CEIL_MOD_EXPR: 9384 case ROUND_MOD_EXPR: 9385 { 9386 tree denom = TREE_OPERAND (t, 1); 9387 if (!RECUR (denom, rval)) 9388 return false; 9389 /* We can't call cxx_eval_outermost_constant_expr on an expression 9390 that hasn't been through instantiate_non_dependent_expr yet. */ 9391 if (!processing_template_decl) 9392 denom = cxx_eval_outermost_constant_expr (denom, true); 9393 if (integer_zerop (denom)) 9394 { 9395 if (flags & tf_error) 9396 error ("division by zero is not a constant expression"); 9397 return false; 9398 } 9399 else 9400 { 9401 want_rval = true; 9402 return RECUR (TREE_OPERAND (t, 0), want_rval); 9403 } 9404 } 9405 9406 case COMPOUND_EXPR: 9407 { 9408 /* check_return_expr sometimes wraps a TARGET_EXPR in a 9409 COMPOUND_EXPR; don't get confused. */ 9410 tree op0 = TREE_OPERAND (t, 0); 9411 tree op1 = TREE_OPERAND (t, 1); 9412 STRIP_NOPS (op1); 9413 if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) 9414 return RECUR (op0, want_rval); 9415 else 9416 goto binary; 9417 } 9418 9419 /* If the first operand is the non-short-circuit constant, look at 9420 the second operand; otherwise we only care about the first one for 9421 potentiality. */ 9422 case TRUTH_AND_EXPR: 9423 case TRUTH_ANDIF_EXPR: 9424 tmp = boolean_true_node; 9425 goto truth; 9426 case TRUTH_OR_EXPR: 9427 case TRUTH_ORIF_EXPR: 9428 tmp = boolean_false_node; 9429 truth: 9430 { 9431 tree op0 = TREE_OPERAND (t, 0); 9432 tree op1 = TREE_OPERAND (t, 1); 9433 if (!RECUR (op0, rval)) 9434 return false; 9435 if (!(flags & tf_error) && RECUR (op1, rval)) 9436 /* When quiet, try to avoid expensive trial evaluation by first 9437 checking potentiality of the second operand. */ 9438 return true; 9439 if (!processing_template_decl) 9440 op0 = cxx_eval_outermost_constant_expr (op0, true); 9441 if (tree_int_cst_equal (op0, tmp)) 9442 return (flags & tf_error) ? RECUR (op1, rval) : false; 9443 else 9444 return true; 9445 } 9446 9447 case PLUS_EXPR: 9448 case MULT_EXPR: 9449 case POINTER_PLUS_EXPR: 9450 case RDIV_EXPR: 9451 case EXACT_DIV_EXPR: 9452 case MIN_EXPR: 9453 case MAX_EXPR: 9454 case LSHIFT_EXPR: 9455 case RSHIFT_EXPR: 9456 case LROTATE_EXPR: 9457 case RROTATE_EXPR: 9458 case BIT_IOR_EXPR: 9459 case BIT_XOR_EXPR: 9460 case BIT_AND_EXPR: 9461 case TRUTH_XOR_EXPR: 9462 case UNORDERED_EXPR: 9463 case ORDERED_EXPR: 9464 case UNLT_EXPR: 9465 case UNLE_EXPR: 9466 case UNGT_EXPR: 9467 case UNGE_EXPR: 9468 case UNEQ_EXPR: 9469 case LTGT_EXPR: 9470 case RANGE_EXPR: 9471 case COMPLEX_EXPR: 9472 want_rval = true; 9473 /* Fall through. */ 9474 case ARRAY_REF: 9475 case ARRAY_RANGE_REF: 9476 case MEMBER_REF: 9477 case DOTSTAR_EXPR: 9478 case MEM_REF: 9479 case BINARY_LEFT_FOLD_EXPR: 9480 case BINARY_RIGHT_FOLD_EXPR: 9481 binary: 9482 for (i = 0; i < 2; ++i) 9483 if (!RECUR (TREE_OPERAND (t, i), want_rval)) 9484 return false; 9485 return true; 9486 9487 case VEC_PERM_EXPR: 9488 for (i = 0; i < 3; ++i) 9489 if (!RECUR (TREE_OPERAND (t, i), true)) 9490 return false; 9491 return true; 9492 9493 case COND_EXPR: 9494 if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) 9495 { 9496 if (flags & tf_error) 9497 error_at (loc, "%<delete[]%> is not a constant expression"); 9498 return false; 9499 } 9500 /* Fall through. */ 9501 case IF_STMT: 9502 case VEC_COND_EXPR: 9503 /* If the condition is a known constant, we know which of the legs we 9504 care about; otherwise we only require that the condition and 9505 either of the legs be potentially constant. */ 9506 tmp = TREE_OPERAND (t, 0); 9507 if (!RECUR (tmp, rval)) 9508 return false; 9509 if (!processing_template_decl) 9510 tmp = cxx_eval_outermost_constant_expr (tmp, true); 9511 /* potential_constant_expression* isn't told if it is called for 9512 manifestly_const_eval or not, so for consteval if always 9513 process both branches as if the condition is not a known 9514 constant. */ 9515 if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) 9516 { 9517 if (integer_zerop (tmp)) 9518 return RECUR (TREE_OPERAND (t, 2), want_rval); 9519 else if (TREE_CODE (tmp) == INTEGER_CST) 9520 return RECUR (TREE_OPERAND (t, 1), want_rval); 9521 } 9522 tmp = *jump_target; 9523 for (i = 1; i < 3; ++i) 9524 { 9525 tree this_jump_target = tmp; 9526 if (potential_constant_expression_1 (TREE_OPERAND (t, i), 9527 want_rval, strict, now, 9528 tf_none, &this_jump_target)) 9529 { 9530 if (returns (&this_jump_target)) 9531 *jump_target = this_jump_target; 9532 else if (!returns (jump_target)) 9533 { 9534 if (breaks (&this_jump_target) 9535 || continues (&this_jump_target)) 9536 *jump_target = this_jump_target; 9537 if (i == 1) 9538 { 9539 /* If the then branch is potentially constant, but 9540 does not return, check if the else branch 9541 couldn't return, break or continue. */ 9542 hash_set<tree> pset; 9543 check_for_return_continue_data data = { &pset, NULL_TREE, 9544 NULL_TREE }; 9545 if (tree ret_expr 9546 = cp_walk_tree (&TREE_OPERAND (t, 2), 9547 check_for_return_continue, &data, 9548 &pset)) 9549 *jump_target = ret_expr; 9550 else if (*jump_target == NULL_TREE) 9551 { 9552 if (data.continue_stmt) 9553 *jump_target = data.continue_stmt; 9554 else if (data.break_stmt) 9555 *jump_target = data.break_stmt; 9556 } 9557 } 9558 } 9559 return true; 9560 } 9561 } 9562 if (flags & tf_error) 9563 { 9564 if (TREE_CODE (t) == IF_STMT) 9565 error_at (loc, "neither branch of %<if%> is a constant expression"); 9566 else 9567 error_at (loc, "expression %qE is not a constant expression", t); 9568 } 9569 return false; 9570 9571 case VEC_INIT_EXPR: 9572 if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) 9573 return true; 9574 if (flags & tf_error) 9575 { 9576 error_at (loc, "non-constant array initialization"); 9577 diagnose_non_constexpr_vec_init (t); 9578 } 9579 return false; 9580 9581 case TYPE_DECL: 9582 case TAG_DEFN: 9583 /* We can see these in statement-expressions. */ 9584 return true; 9585 9586 case CLEANUP_STMT: 9587 if (!RECUR (CLEANUP_BODY (t), any)) 9588 return false; 9589 if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) 9590 return false; 9591 return true; 9592 9593 case EMPTY_CLASS_EXPR: 9594 return true; 9595 9596 case GOTO_EXPR: 9597 { 9598 tree *target = &TREE_OPERAND (t, 0); 9599 /* Gotos representing break, continue and cdtor return are OK. */ 9600 if (breaks (target) || continues (target) || returns (target)) 9601 { 9602 *jump_target = *target; 9603 return true; 9604 } 9605 if (flags & tf_error) 9606 error_at (loc, "%<goto%> is not a constant expression"); 9607 return false; 9608 } 9609 9610 case LABEL_EXPR: 9611 t = LABEL_EXPR_LABEL (t); 9612 if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) 9613 return true; 9614 else if (flags & tf_error) 9615 error_at (loc, "label definition in %<constexpr%> function only " 9616 "available with %<-std=c++2b%> or %<-std=gnu++2b%>"); 9617 return false; 9618 9619 case ANNOTATE_EXPR: 9620 return RECUR (TREE_OPERAND (t, 0), rval); 9621 9622 case BIT_CAST_EXPR: 9623 return RECUR (TREE_OPERAND (t, 0), rval); 9624 9625 /* Coroutine await, yield and return expressions are not. */ 9626 case CO_AWAIT_EXPR: 9627 case CO_YIELD_EXPR: 9628 case CO_RETURN_EXPR: 9629 return false; 9630 9631 case NONTYPE_ARGUMENT_PACK: 9632 { 9633 tree args = ARGUMENT_PACK_ARGS (t); 9634 int len = TREE_VEC_LENGTH (args); 9635 for (int i = 0; i < len; ++i) 9636 if (!RECUR (TREE_VEC_ELT (args, i), any)) 9637 return false; 9638 return true; 9639 } 9640 9641 default: 9642 if (objc_non_constant_expr_p (t)) 9643 return false; 9644 9645 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); 9646 gcc_unreachable (); 9647 return false; 9648 } 9649#undef RECUR 9650} 9651 9652bool 9653potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, 9654 tsubst_flags_t flags) 9655{ 9656 if (flags & tf_error) 9657 { 9658 /* Check potentiality quietly first, as that could be performed more 9659 efficiently in some cases (currently only for TRUTH_*_EXPR). If 9660 that fails, replay the check noisily to give errors. */ 9661 flags &= ~tf_error; 9662 if (potential_constant_expression_1 (t, want_rval, strict, now, flags)) 9663 return true; 9664 flags |= tf_error; 9665 } 9666 9667 tree target = NULL_TREE; 9668 return potential_constant_expression_1 (t, want_rval, strict, now, 9669 flags, &target); 9670} 9671 9672/* The main entry point to the above. */ 9673 9674bool 9675potential_constant_expression (tree t) 9676{ 9677 return potential_constant_expression_1 (t, false, true, false, tf_none); 9678} 9679 9680/* As above, but require a constant rvalue. */ 9681 9682bool 9683potential_rvalue_constant_expression (tree t) 9684{ 9685 return potential_constant_expression_1 (t, true, true, false, tf_none); 9686} 9687 9688/* Like above, but complain about non-constant expressions. */ 9689 9690bool 9691require_potential_constant_expression (tree t) 9692{ 9693 return potential_constant_expression_1 (t, false, true, false, 9694 tf_warning_or_error); 9695} 9696 9697/* Cross product of the above. */ 9698 9699bool 9700require_potential_rvalue_constant_expression (tree t) 9701{ 9702 return potential_constant_expression_1 (t, true, true, false, 9703 tf_warning_or_error); 9704} 9705 9706/* Like above, but don't consider PARM_DECL a potential_constant_expression. */ 9707 9708bool 9709require_rvalue_constant_expression (tree t) 9710{ 9711 return potential_constant_expression_1 (t, true, true, true, 9712 tf_warning_or_error); 9713} 9714 9715/* Like potential_constant_expression, but don't consider possible constexpr 9716 substitution of the current function. That is, PARM_DECL qualifies under 9717 potential_constant_expression, but not here. 9718 9719 This is basically what you can check when any actual constant values might 9720 be value-dependent. */ 9721 9722bool 9723is_constant_expression (tree t) 9724{ 9725 return potential_constant_expression_1 (t, false, true, true, tf_none); 9726} 9727 9728/* As above, but expect an rvalue. */ 9729 9730bool 9731is_rvalue_constant_expression (tree t) 9732{ 9733 return potential_constant_expression_1 (t, true, true, true, tf_none); 9734} 9735 9736/* Like above, but complain about non-constant expressions. */ 9737 9738bool 9739require_constant_expression (tree t) 9740{ 9741 return potential_constant_expression_1 (t, false, true, true, 9742 tf_warning_or_error); 9743} 9744 9745/* Like is_constant_expression, but allow const variables that are not allowed 9746 under constexpr rules. */ 9747 9748bool 9749is_static_init_expression (tree t) 9750{ 9751 return potential_constant_expression_1 (t, false, false, true, tf_none); 9752} 9753 9754/* Returns true if T is a potential constant expression that is not 9755 instantiation-dependent, and therefore a candidate for constant folding even 9756 in a template. */ 9757 9758bool 9759is_nondependent_constant_expression (tree t) 9760{ 9761 return (!type_unknown_p (t) 9762 && is_constant_expression (t) 9763 && !instantiation_dependent_expression_p (t)); 9764} 9765 9766/* Returns true if T is a potential static initializer expression that is not 9767 instantiation-dependent. */ 9768 9769bool 9770is_nondependent_static_init_expression (tree t) 9771{ 9772 return (!type_unknown_p (t) 9773 && is_static_init_expression (t) 9774 && !instantiation_dependent_expression_p (t)); 9775} 9776 9777/* True iff FN is an implicitly constexpr function. */ 9778 9779bool 9780decl_implicit_constexpr_p (tree fn) 9781{ 9782 if (!(flag_implicit_constexpr 9783 && TREE_CODE (fn) == FUNCTION_DECL 9784 && DECL_DECLARED_CONSTEXPR_P (fn))) 9785 return false; 9786 9787 if (DECL_CLONED_FUNCTION_P (fn)) 9788 fn = DECL_CLONED_FUNCTION (fn); 9789 9790 return (DECL_LANG_SPECIFIC (fn) 9791 && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr); 9792} 9793 9794/* Finalize constexpr processing after parsing. */ 9795 9796void 9797fini_constexpr (void) 9798{ 9799 /* The contexpr call and fundef copies tables are no longer needed. */ 9800 constexpr_call_table = NULL; 9801 fundef_copies_table = NULL; 9802} 9803 9804#include "gt-cp-constexpr.h" 9805