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