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-2019 Free Software Foundation, Inc. 6 7 This file is part of GCC. 8 9 GCC is free software; you can redistribute it and/or modify it 10 under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3, or (at your option) 12 any later version. 13 14 GCC is distributed in the hope that it will be useful, but 15 WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with GCC; see the file COPYING3. If not see 21 <http://www.gnu.org/licenses/>. */ 22 23 #include "config.h" 24 #include "system.h" 25 #include "coretypes.h" 26 #include "cp-tree.h" 27 #include "varasm.h" 28 #include "c-family/c-objc.h" 29 #include "tree-iterator.h" 30 #include "gimplify.h" 31 #include "builtins.h" 32 #include "tree-inline.h" 33 #include "ubsan.h" 34 #include "gimple-fold.h" 35 #include "timevar.h" 36 #include "fold-const-call.h" 37 38 static bool verify_constant (tree, bool, bool *, bool *); 39 #define VERIFY_CONSTANT(X) \ 40 do { \ 41 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ 42 return t; \ 43 } while (0) 44 45 static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, 46 bool insert = false); 47 48 /* Returns true iff FUN is an instantiation of a constexpr function 49 template or a defaulted constexpr function. */ 50 51 bool 52 is_instantiation_of_constexpr (tree fun) 53 { 54 return ((DECL_TEMPLOID_INSTANTIATION (fun) 55 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) 56 || (DECL_DEFAULTED_FN (fun) 57 && DECL_DECLARED_CONSTEXPR_P (fun))); 58 } 59 60 /* Return true if T is a literal type. */ 61 62 bool 63 literal_type_p (tree t) 64 { 65 if (SCALAR_TYPE_P (t) 66 || VECTOR_TYPE_P (t) 67 || TYPE_REF_P (t) 68 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) 69 return true; 70 if (CLASS_TYPE_P (t)) 71 { 72 t = complete_type (t); 73 gcc_assert (COMPLETE_TYPE_P (t) || errorcount); 74 return CLASSTYPE_LITERAL_P (t); 75 } 76 if (TREE_CODE (t) == ARRAY_TYPE) 77 return literal_type_p (strip_array_types (t)); 78 return false; 79 } 80 81 /* If DECL is a variable declared `constexpr', require its type 82 be literal. Return error_mark_node if we give an error, the 83 DECL otherwise. */ 84 85 tree 86 ensure_literal_type_for_constexpr_object (tree decl) 87 { 88 tree type = TREE_TYPE (decl); 89 if (VAR_P (decl) 90 && (DECL_DECLARED_CONSTEXPR_P (decl) 91 || var_in_constexpr_fn (decl)) 92 && !processing_template_decl) 93 { 94 tree stype = strip_array_types (type); 95 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) 96 /* Don't complain here, we'll complain about incompleteness 97 when we try to initialize the variable. */; 98 else if (type_uses_auto (type)) 99 /* We don't know the actual type yet. */; 100 else if (!literal_type_p (type)) 101 { 102 if (DECL_DECLARED_CONSTEXPR_P (decl)) 103 { 104 auto_diagnostic_group d; 105 error_at (DECL_SOURCE_LOCATION (decl), 106 "the type %qT of %<constexpr%> variable %qD " 107 "is not literal", type, decl); 108 explain_non_literal_class (type); 109 decl = error_mark_node; 110 } 111 else 112 { 113 if (!is_instantiation_of_constexpr (current_function_decl)) 114 { 115 auto_diagnostic_group d; 116 error_at (DECL_SOURCE_LOCATION (decl), 117 "variable %qD of non-literal type %qT in " 118 "%<constexpr%> function", decl, type); 119 explain_non_literal_class (type); 120 decl = error_mark_node; 121 } 122 cp_function_chain->invalid_constexpr = true; 123 } 124 } 125 else if (DECL_DECLARED_CONSTEXPR_P (decl) 126 && variably_modified_type_p (type, NULL_TREE)) 127 { 128 error_at (DECL_SOURCE_LOCATION (decl), 129 "%<constexpr%> variable %qD has variably-modified " 130 "type %qT", decl, type); 131 decl = error_mark_node; 132 } 133 } 134 return decl; 135 } 136 137 /* Representation of entries in the constexpr function definition table. */ 138 139 struct GTY((for_user)) constexpr_fundef { 140 tree decl; 141 tree body; 142 tree parms; 143 tree result; 144 }; 145 146 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> 147 { 148 static hashval_t hash (constexpr_fundef *); 149 static bool equal (constexpr_fundef *, constexpr_fundef *); 150 }; 151 152 /* This table holds all constexpr function definitions seen in 153 the current translation unit. */ 154 155 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; 156 157 /* Utility function used for managing the constexpr function table. 158 Return true if the entries pointed to by P and Q are for the 159 same constexpr function. */ 160 161 inline bool 162 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs) 163 { 164 return lhs->decl == rhs->decl; 165 } 166 167 /* Utility function used for managing the constexpr function table. 168 Return a hash value for the entry pointed to by Q. */ 169 170 inline hashval_t 171 constexpr_fundef_hasher::hash (constexpr_fundef *fundef) 172 { 173 return DECL_UID (fundef->decl); 174 } 175 176 /* Return a previously saved definition of function FUN. */ 177 178 static constexpr_fundef * 179 retrieve_constexpr_fundef (tree fun) 180 { 181 if (constexpr_fundef_table == NULL) 182 return NULL; 183 184 constexpr_fundef fundef = { fun, NULL, NULL, NULL }; 185 return constexpr_fundef_table->find (&fundef); 186 } 187 188 /* Check whether the parameter and return types of FUN are valid for a 189 constexpr function, and complain if COMPLAIN. */ 190 191 bool 192 is_valid_constexpr_fn (tree fun, bool complain) 193 { 194 bool ret = true; 195 196 if (DECL_INHERITED_CTOR (fun) 197 && TREE_CODE (fun) == TEMPLATE_DECL) 198 { 199 ret = false; 200 if (complain) 201 error ("inherited constructor %qD is not %<constexpr%>", 202 DECL_INHERITED_CTOR (fun)); 203 } 204 else 205 { 206 for (tree parm = FUNCTION_FIRST_USER_PARM (fun); 207 parm != NULL_TREE; parm = TREE_CHAIN (parm)) 208 if (!literal_type_p (TREE_TYPE (parm))) 209 { 210 ret = false; 211 if (complain) 212 { 213 auto_diagnostic_group d; 214 error ("invalid type for parameter %d of %<constexpr%> " 215 "function %q+#D", DECL_PARM_INDEX (parm), fun); 216 explain_non_literal_class (TREE_TYPE (parm)); 217 } 218 } 219 } 220 221 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) 222 { 223 ret = false; 224 if (complain) 225 inform (DECL_SOURCE_LOCATION (fun), 226 "lambdas are implicitly %<constexpr%> only in C++17 and later"); 227 } 228 else if (!DECL_CONSTRUCTOR_P (fun)) 229 { 230 tree rettype = TREE_TYPE (TREE_TYPE (fun)); 231 if (!literal_type_p (rettype)) 232 { 233 ret = false; 234 if (complain) 235 { 236 auto_diagnostic_group d; 237 error ("invalid return type %qT of %<constexpr%> function %q+D", 238 rettype, fun); 239 explain_non_literal_class (rettype); 240 } 241 } 242 243 /* C++14 DR 1684 removed this restriction. */ 244 if (cxx_dialect < cxx14 245 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) 246 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) 247 { 248 ret = false; 249 if (complain) 250 { 251 auto_diagnostic_group d; 252 if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, 253 "enclosing class of %<constexpr%> non-static" 254 " member function %q+#D is not a literal type", 255 fun)) 256 explain_non_literal_class (DECL_CONTEXT (fun)); 257 } 258 } 259 } 260 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) 261 { 262 ret = false; 263 if (complain) 264 error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); 265 } 266 267 return ret; 268 } 269 270 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF 271 for a member of an anonymous aggregate, INIT is the initializer for that 272 member, and VEC_OUTER is the vector of constructor elements for the class 273 whose constructor we are processing. Add the initializer to the vector 274 and return true to indicate success. */ 275 276 static bool 277 build_anon_member_initialization (tree member, tree init, 278 vec<constructor_elt, va_gc> **vec_outer) 279 { 280 /* MEMBER presents the relevant fields from the inside out, but we need 281 to build up the initializer from the outside in so that we can reuse 282 previously built CONSTRUCTORs if this is, say, the second field in an 283 anonymous struct. So we use a vec as a stack. */ 284 auto_vec<tree, 2> fields; 285 do 286 { 287 fields.safe_push (TREE_OPERAND (member, 1)); 288 member = TREE_OPERAND (member, 0); 289 } 290 while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) 291 && TREE_CODE (member) == COMPONENT_REF); 292 293 /* VEC has the constructor elements vector for the context of FIELD. 294 If FIELD is an anonymous aggregate, we will push inside it. */ 295 vec<constructor_elt, va_gc> **vec = vec_outer; 296 tree field; 297 while (field = fields.pop(), 298 ANON_AGGR_TYPE_P (TREE_TYPE (field))) 299 { 300 tree ctor; 301 /* If there is already an outer constructor entry for the anonymous 302 aggregate FIELD, use it; otherwise, insert one. */ 303 if (vec_safe_is_empty (*vec) 304 || (*vec)->last().index != field) 305 { 306 ctor = build_constructor (TREE_TYPE (field), NULL); 307 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); 308 } 309 else 310 ctor = (*vec)->last().value; 311 vec = &CONSTRUCTOR_ELTS (ctor); 312 } 313 314 /* Now we're at the innermost field, the one that isn't an anonymous 315 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ 316 gcc_assert (fields.is_empty()); 317 CONSTRUCTOR_APPEND_ELT (*vec, field, init); 318 319 return true; 320 } 321 322 /* Subroutine of build_constexpr_constructor_member_initializers. 323 The expression tree T represents a data member initialization 324 in a (constexpr) constructor definition. Build a pairing of 325 the data member with its initializer, and prepend that pair 326 to the existing initialization pair INITS. */ 327 328 static bool 329 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) 330 { 331 tree member, init; 332 if (TREE_CODE (t) == CLEANUP_POINT_EXPR) 333 t = TREE_OPERAND (t, 0); 334 if (TREE_CODE (t) == EXPR_STMT) 335 t = TREE_OPERAND (t, 0); 336 if (t == error_mark_node) 337 return false; 338 if (TREE_CODE (t) == STATEMENT_LIST) 339 { 340 tree_stmt_iterator i; 341 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) 342 { 343 if (! build_data_member_initialization (tsi_stmt (i), vec)) 344 return false; 345 } 346 return true; 347 } 348 if (TREE_CODE (t) == CLEANUP_STMT) 349 { 350 /* We can't see a CLEANUP_STMT in a constructor for a literal class, 351 but we can in a constexpr constructor for a non-literal class. Just 352 ignore it; either all the initialization will be constant, in which 353 case the cleanup can't run, or it can't be constexpr. 354 Still recurse into CLEANUP_BODY. */ 355 return build_data_member_initialization (CLEANUP_BODY (t), vec); 356 } 357 if (TREE_CODE (t) == CONVERT_EXPR) 358 t = TREE_OPERAND (t, 0); 359 if (TREE_CODE (t) == INIT_EXPR 360 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only 361 use what this function builds for cx_check_missing_mem_inits, and 362 assignment in the ctor body doesn't count. */ 363 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) 364 { 365 member = TREE_OPERAND (t, 0); 366 init = break_out_target_exprs (TREE_OPERAND (t, 1)); 367 } 368 else if (TREE_CODE (t) == CALL_EXPR) 369 { 370 tree fn = get_callee_fndecl (t); 371 if (!fn || !DECL_CONSTRUCTOR_P (fn)) 372 /* We're only interested in calls to subobject constructors. */ 373 return true; 374 member = CALL_EXPR_ARG (t, 0); 375 /* We don't use build_cplus_new here because it complains about 376 abstract bases. Leaving the call unwrapped means that it has the 377 wrong type, but cxx_eval_constant_expression doesn't care. */ 378 init = break_out_target_exprs (t); 379 } 380 else if (TREE_CODE (t) == BIND_EXPR) 381 return build_data_member_initialization (BIND_EXPR_BODY (t), vec); 382 else 383 /* Don't add anything else to the CONSTRUCTOR. */ 384 return true; 385 if (INDIRECT_REF_P (member)) 386 member = TREE_OPERAND (member, 0); 387 if (TREE_CODE (member) == NOP_EXPR) 388 { 389 tree op = member; 390 STRIP_NOPS (op); 391 if (TREE_CODE (op) == ADDR_EXPR) 392 { 393 gcc_assert (same_type_ignoring_top_level_qualifiers_p 394 (TREE_TYPE (TREE_TYPE (op)), 395 TREE_TYPE (TREE_TYPE (member)))); 396 /* Initializing a cv-qualified member; we need to look through 397 the const_cast. */ 398 member = op; 399 } 400 else if (op == current_class_ptr 401 && (same_type_ignoring_top_level_qualifiers_p 402 (TREE_TYPE (TREE_TYPE (member)), 403 current_class_type))) 404 /* Delegating constructor. */ 405 member = op; 406 else 407 { 408 /* This is an initializer for an empty base; keep it for now so 409 we can check it in cxx_eval_bare_aggregate. */ 410 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); 411 } 412 } 413 if (TREE_CODE (member) == ADDR_EXPR) 414 member = TREE_OPERAND (member, 0); 415 if (TREE_CODE (member) == COMPONENT_REF) 416 { 417 tree aggr = TREE_OPERAND (member, 0); 418 if (TREE_CODE (aggr) == VAR_DECL) 419 /* Initializing a local variable, don't add anything. */ 420 return true; 421 if (TREE_CODE (aggr) != COMPONENT_REF) 422 /* Normal member initialization. */ 423 member = TREE_OPERAND (member, 1); 424 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) 425 /* Initializing a member of an anonymous union. */ 426 return build_anon_member_initialization (member, init, vec); 427 else 428 /* We're initializing a vtable pointer in a base. Leave it as 429 COMPONENT_REF so we remember the path to get to the vfield. */ 430 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); 431 } 432 433 /* Value-initialization can produce multiple initializers for the 434 same field; use the last one. */ 435 if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) 436 (*vec)->last().value = init; 437 else 438 CONSTRUCTOR_APPEND_ELT (*vec, member, init); 439 return true; 440 } 441 442 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. 443 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a 444 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ 445 446 static bool 447 check_constexpr_bind_expr_vars (tree t) 448 { 449 gcc_assert (TREE_CODE (t) == BIND_EXPR); 450 451 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) 452 if (TREE_CODE (var) == TYPE_DECL 453 && DECL_IMPLICIT_TYPEDEF_P (var) 454 && !LAMBDA_TYPE_P (TREE_TYPE (var))) 455 return false; 456 return true; 457 } 458 459 /* Subroutine of check_constexpr_ctor_body. */ 460 461 static bool 462 check_constexpr_ctor_body_1 (tree last, tree list) 463 { 464 switch (TREE_CODE (list)) 465 { 466 case DECL_EXPR: 467 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL 468 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) 469 return true; 470 return false; 471 472 case CLEANUP_POINT_EXPR: 473 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), 474 /*complain=*/false); 475 476 case BIND_EXPR: 477 if (!check_constexpr_bind_expr_vars (list) 478 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), 479 /*complain=*/false)) 480 return false; 481 return true; 482 483 case USING_STMT: 484 case STATIC_ASSERT: 485 case DEBUG_BEGIN_STMT: 486 return true; 487 488 default: 489 return false; 490 } 491 } 492 493 /* Make sure that there are no statements after LAST in the constructor 494 body represented by LIST. */ 495 496 bool 497 check_constexpr_ctor_body (tree last, tree list, bool complain) 498 { 499 /* C++14 doesn't require a constexpr ctor to have an empty body. */ 500 if (cxx_dialect >= cxx14) 501 return true; 502 503 bool ok = true; 504 if (TREE_CODE (list) == STATEMENT_LIST) 505 { 506 tree_stmt_iterator i = tsi_last (list); 507 for (; !tsi_end_p (i); tsi_prev (&i)) 508 { 509 tree t = tsi_stmt (i); 510 if (t == last) 511 break; 512 if (!check_constexpr_ctor_body_1 (last, t)) 513 { 514 ok = false; 515 break; 516 } 517 } 518 } 519 else if (list != last 520 && !check_constexpr_ctor_body_1 (last, list)) 521 ok = false; 522 if (!ok) 523 { 524 if (complain) 525 error ("%<constexpr%> constructor does not have empty body"); 526 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; 527 } 528 return ok; 529 } 530 531 /* V is a vector of constructor elements built up for the base and member 532 initializers of a constructor for TYPE. They need to be in increasing 533 offset order, which they might not be yet if TYPE has a primary base 534 which is not first in the base-clause or a vptr and at least one base 535 all of which are non-primary. */ 536 537 static vec<constructor_elt, va_gc> * 538 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) 539 { 540 tree pri = CLASSTYPE_PRIMARY_BINFO (type); 541 tree field_type; 542 unsigned i; 543 constructor_elt *ce; 544 545 if (pri) 546 field_type = BINFO_TYPE (pri); 547 else if (TYPE_CONTAINS_VPTR_P (type)) 548 field_type = vtbl_ptr_type_node; 549 else 550 return v; 551 552 /* Find the element for the primary base or vptr and move it to the 553 beginning of the vec. */ 554 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 555 if (TREE_TYPE (ce->index) == field_type) 556 break; 557 558 if (i > 0 && i < vec_safe_length (v)) 559 { 560 vec<constructor_elt, va_gc> &vref = *v; 561 constructor_elt elt = vref[i]; 562 for (; i > 0; --i) 563 vref[i] = vref[i-1]; 564 vref[0] = elt; 565 } 566 567 return v; 568 } 569 570 /* Build compile-time evalable representations of member-initializer list 571 for a constexpr constructor. */ 572 573 static tree 574 build_constexpr_constructor_member_initializers (tree type, tree body) 575 { 576 vec<constructor_elt, va_gc> *vec = NULL; 577 bool ok = true; 578 while (true) 579 switch (TREE_CODE (body)) 580 { 581 case MUST_NOT_THROW_EXPR: 582 case EH_SPEC_BLOCK: 583 body = TREE_OPERAND (body, 0); 584 break; 585 586 case STATEMENT_LIST: 587 for (tree_stmt_iterator i = tsi_start (body); 588 !tsi_end_p (i); tsi_next (&i)) 589 { 590 body = tsi_stmt (i); 591 if (TREE_CODE (body) == BIND_EXPR) 592 break; 593 } 594 break; 595 596 case BIND_EXPR: 597 body = BIND_EXPR_BODY (body); 598 goto found; 599 600 default: 601 gcc_unreachable (); 602 } 603 found: 604 if (TREE_CODE (body) == TRY_BLOCK) 605 { 606 body = TREE_OPERAND (body, 0); 607 if (TREE_CODE (body) == BIND_EXPR) 608 body = BIND_EXPR_BODY (body); 609 } 610 if (TREE_CODE (body) == CLEANUP_POINT_EXPR) 611 { 612 body = TREE_OPERAND (body, 0); 613 if (TREE_CODE (body) == EXPR_STMT) 614 body = TREE_OPERAND (body, 0); 615 if (TREE_CODE (body) == INIT_EXPR 616 && (same_type_ignoring_top_level_qualifiers_p 617 (TREE_TYPE (TREE_OPERAND (body, 0)), 618 current_class_type))) 619 { 620 /* Trivial copy. */ 621 return TREE_OPERAND (body, 1); 622 } 623 ok = build_data_member_initialization (body, &vec); 624 } 625 else if (TREE_CODE (body) == STATEMENT_LIST) 626 { 627 tree_stmt_iterator i; 628 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) 629 { 630 ok = build_data_member_initialization (tsi_stmt (i), &vec); 631 if (!ok) 632 break; 633 } 634 } 635 else if (EXPR_P (body)) 636 ok = build_data_member_initialization (body, &vec); 637 else 638 gcc_assert (errorcount > 0); 639 if (ok) 640 { 641 if (vec_safe_length (vec) > 0) 642 { 643 /* In a delegating constructor, return the target. */ 644 constructor_elt *ce = &(*vec)[0]; 645 if (ce->index == current_class_ptr) 646 { 647 body = ce->value; 648 vec_free (vec); 649 return body; 650 } 651 } 652 vec = sort_constexpr_mem_initializers (type, vec); 653 return build_constructor (type, vec); 654 } 655 else 656 return error_mark_node; 657 } 658 659 /* We have an expression tree T that represents a call, either CALL_EXPR 660 or AGGR_INIT_EXPR. If the call is lexically to a named function, 661 retrun the _DECL for that function. */ 662 663 static tree 664 get_function_named_in_call (tree t) 665 { 666 tree fun = cp_get_callee (t); 667 if (fun && TREE_CODE (fun) == ADDR_EXPR 668 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) 669 fun = TREE_OPERAND (fun, 0); 670 return fun; 671 } 672 673 /* Subroutine of register_constexpr_fundef. BODY is the body of a function 674 declared to be constexpr, or a sub-statement thereof. Returns the 675 return value if suitable, error_mark_node for a statement not allowed in 676 a constexpr function, or NULL_TREE if no return value was found. */ 677 678 tree 679 constexpr_fn_retval (tree body) 680 { 681 switch (TREE_CODE (body)) 682 { 683 case STATEMENT_LIST: 684 { 685 tree_stmt_iterator i; 686 tree expr = NULL_TREE; 687 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) 688 { 689 tree s = constexpr_fn_retval (tsi_stmt (i)); 690 if (s == error_mark_node) 691 return error_mark_node; 692 else if (s == NULL_TREE) 693 /* Keep iterating. */; 694 else if (expr) 695 /* Multiple return statements. */ 696 return error_mark_node; 697 else 698 expr = s; 699 } 700 return expr; 701 } 702 703 case RETURN_EXPR: 704 return break_out_target_exprs (TREE_OPERAND (body, 0)); 705 706 case DECL_EXPR: 707 { 708 tree decl = DECL_EXPR_DECL (body); 709 if (TREE_CODE (decl) == USING_DECL 710 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ 711 || DECL_ARTIFICIAL (decl)) 712 return NULL_TREE; 713 return error_mark_node; 714 } 715 716 case CLEANUP_POINT_EXPR: 717 return constexpr_fn_retval (TREE_OPERAND (body, 0)); 718 719 case BIND_EXPR: 720 if (!check_constexpr_bind_expr_vars (body)) 721 return error_mark_node; 722 return constexpr_fn_retval (BIND_EXPR_BODY (body)); 723 724 case USING_STMT: 725 case DEBUG_BEGIN_STMT: 726 return NULL_TREE; 727 728 case CALL_EXPR: 729 { 730 tree fun = get_function_named_in_call (body); 731 if (fun != NULL_TREE 732 && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) 733 return NULL_TREE; 734 } 735 /* Fallthru. */ 736 737 default: 738 return error_mark_node; 739 } 740 } 741 742 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of 743 FUN; do the necessary transformations to turn it into a single expression 744 that we can store in the hash table. */ 745 746 static tree 747 massage_constexpr_body (tree fun, tree body) 748 { 749 if (DECL_CONSTRUCTOR_P (fun)) 750 body = build_constexpr_constructor_member_initializers 751 (DECL_CONTEXT (fun), body); 752 else if (cxx_dialect < cxx14) 753 { 754 if (TREE_CODE (body) == EH_SPEC_BLOCK) 755 body = EH_SPEC_STMTS (body); 756 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) 757 body = TREE_OPERAND (body, 0); 758 body = constexpr_fn_retval (body); 759 } 760 return body; 761 } 762 763 /* CTYPE is a type constructed from BODY. Return true if some 764 bases/fields are uninitialized, and complain if COMPLAIN. */ 765 766 static bool 767 cx_check_missing_mem_inits (tree ctype, tree body, bool complain) 768 { 769 unsigned nelts = 0; 770 771 if (body) 772 { 773 if (TREE_CODE (body) != CONSTRUCTOR) 774 return false; 775 nelts = CONSTRUCTOR_NELTS (body); 776 } 777 tree field = TYPE_FIELDS (ctype); 778 779 if (TREE_CODE (ctype) == UNION_TYPE) 780 { 781 if (nelts == 0 && next_initializable_field (field)) 782 { 783 if (complain) 784 error ("%<constexpr%> constructor for union %qT must " 785 "initialize exactly one non-static data member", ctype); 786 return true; 787 } 788 return false; 789 } 790 791 /* Iterate over the CONSTRUCTOR, checking any missing fields don't 792 need an explicit initialization. */ 793 bool bad = false; 794 for (unsigned i = 0; i <= nelts; ++i) 795 { 796 tree index = NULL_TREE; 797 if (i < nelts) 798 { 799 index = CONSTRUCTOR_ELT (body, i)->index; 800 /* Skip base and vtable inits. */ 801 if (TREE_CODE (index) != FIELD_DECL 802 || DECL_ARTIFICIAL (index)) 803 continue; 804 } 805 806 for (; field != index; field = DECL_CHAIN (field)) 807 { 808 tree ftype; 809 if (TREE_CODE (field) != FIELD_DECL) 810 continue; 811 if (DECL_UNNAMED_BIT_FIELD (field)) 812 continue; 813 if (DECL_ARTIFICIAL (field)) 814 continue; 815 if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 816 { 817 /* Recurse to check the anonummous aggregate member. */ 818 bad |= cx_check_missing_mem_inits 819 (TREE_TYPE (field), NULL_TREE, complain); 820 if (bad && !complain) 821 return true; 822 continue; 823 } 824 if (DECL_SIZE (field) && integer_zerop (DECL_SIZE (field))) 825 /* An empty field doesn't need an initializer. */ 826 continue; 827 ftype = strip_array_types (TREE_TYPE (field)); 828 if (type_has_constexpr_default_constructor (ftype)) 829 { 830 /* It's OK to skip a member with a trivial constexpr ctor. 831 A constexpr ctor that isn't trivial should have been 832 added in by now. */ 833 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) 834 || errorcount != 0); 835 continue; 836 } 837 if (!complain) 838 return true; 839 auto_diagnostic_group d; 840 error ("member %qD must be initialized by mem-initializer " 841 "in %<constexpr%> constructor", field); 842 inform (DECL_SOURCE_LOCATION (field), "declared here"); 843 bad = true; 844 } 845 if (field == NULL_TREE) 846 break; 847 848 if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) 849 { 850 /* Check the anonymous aggregate initializer is valid. */ 851 bad |= cx_check_missing_mem_inits 852 (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); 853 if (bad && !complain) 854 return true; 855 } 856 field = DECL_CHAIN (field); 857 } 858 859 return bad; 860 } 861 862 /* We are processing the definition of the constexpr function FUN. 863 Check that its BODY fulfills the propriate requirements and 864 enter it in the constexpr function definition table. 865 For constructor BODY is actually the TREE_LIST of the 866 member-initializer list. */ 867 868 tree 869 register_constexpr_fundef (tree fun, tree body) 870 { 871 constexpr_fundef entry; 872 constexpr_fundef **slot; 873 874 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun))) 875 return NULL; 876 877 tree massaged = massage_constexpr_body (fun, body); 878 if (massaged == NULL_TREE || massaged == error_mark_node) 879 { 880 if (!DECL_CONSTRUCTOR_P (fun)) 881 error ("body of %<constexpr%> function %qD not a return-statement", 882 fun); 883 return NULL; 884 } 885 886 if (!potential_rvalue_constant_expression (massaged)) 887 { 888 if (!DECL_GENERATED_P (fun)) 889 require_potential_rvalue_constant_expression (massaged); 890 return NULL; 891 } 892 893 if (DECL_CONSTRUCTOR_P (fun) 894 && cx_check_missing_mem_inits (DECL_CONTEXT (fun), 895 massaged, !DECL_GENERATED_P (fun))) 896 return NULL; 897 898 /* Create the constexpr function table if necessary. */ 899 if (constexpr_fundef_table == NULL) 900 constexpr_fundef_table 901 = hash_table<constexpr_fundef_hasher>::create_ggc (101); 902 903 entry.decl = fun; 904 tree saved_fn = current_function_decl; 905 bool clear_ctx = false; 906 current_function_decl = fun; 907 if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) 908 { 909 clear_ctx = true; 910 DECL_CONTEXT (DECL_RESULT (fun)) = fun; 911 } 912 entry.body = copy_fn (fun, entry.parms, entry.result); 913 current_function_decl = saved_fn; 914 slot = constexpr_fundef_table->find_slot (&entry, INSERT); 915 if (clear_ctx) 916 DECL_CONTEXT (DECL_RESULT (fun)) = NULL_TREE; 917 918 gcc_assert (*slot == NULL); 919 *slot = ggc_alloc<constexpr_fundef> (); 920 **slot = entry; 921 922 return fun; 923 } 924 925 /* FUN is a non-constexpr function called in a context that requires a 926 constant expression. If it comes from a constexpr template, explain why 927 the instantiation isn't constexpr. */ 928 929 void 930 explain_invalid_constexpr_fn (tree fun) 931 { 932 static hash_set<tree> *diagnosed; 933 tree body; 934 location_t save_loc; 935 /* Only diagnose defaulted functions, lambdas, or instantiations. */ 936 if (!DECL_DEFAULTED_FN (fun) 937 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) 938 && !is_instantiation_of_constexpr (fun)) 939 return; 940 if (diagnosed == NULL) 941 diagnosed = new hash_set<tree>; 942 if (diagnosed->add (fun)) 943 /* Already explained. */ 944 return; 945 946 save_loc = input_location; 947 if (!lambda_static_thunk_p (fun)) 948 { 949 /* Diagnostics should completely ignore the static thunk, so leave 950 input_location set to our caller's location. */ 951 input_location = DECL_SOURCE_LOCATION (fun); 952 inform (input_location, 953 "%qD is not usable as a %<constexpr%> function because:", fun); 954 } 955 /* First check the declaration. */ 956 if (is_valid_constexpr_fn (fun, true)) 957 { 958 /* Then if it's OK, the body. */ 959 if (!DECL_DECLARED_CONSTEXPR_P (fun) 960 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))) 961 explain_implicit_non_constexpr (fun); 962 else 963 { 964 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); 965 require_potential_rvalue_constant_expression (body); 966 if (DECL_CONSTRUCTOR_P (fun)) 967 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); 968 } 969 } 970 input_location = save_loc; 971 } 972 973 /* Objects of this type represent calls to constexpr functions 974 along with the bindings of parameters to their arguments, for 975 the purpose of compile time evaluation. */ 976 977 struct GTY((for_user)) constexpr_call { 978 /* Description of the constexpr function definition. */ 979 constexpr_fundef *fundef; 980 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE 981 is a parameter _DECL and the TREE_VALUE is the value of the parameter. 982 Note: This arrangement is made to accommodate the use of 983 iterative_hash_template_arg (see pt.c). If you change this 984 representation, also change the hash calculation in 985 cxx_eval_call_expression. */ 986 tree bindings; 987 /* Result of the call. 988 NULL means the call is being evaluated. 989 error_mark_node means that the evaluation was erroneous; 990 otherwise, the actuall value of the call. */ 991 tree result; 992 /* The hash of this call; we remember it here to avoid having to 993 recalculate it when expanding the hash table. */ 994 hashval_t hash; 995 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */ 996 bool manifestly_const_eval; 997 }; 998 999 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> 1000 { 1001 static hashval_t hash (constexpr_call *); 1002 static bool equal (constexpr_call *, constexpr_call *); 1003 }; 1004 1005 enum constexpr_switch_state { 1006 /* Used when processing a switch for the first time by cxx_eval_switch_expr 1007 and default: label for that switch has not been seen yet. */ 1008 css_default_not_seen, 1009 /* Used when processing a switch for the first time by cxx_eval_switch_expr 1010 and default: label for that switch has been seen already. */ 1011 css_default_seen, 1012 /* Used when processing a switch for the second time by 1013 cxx_eval_switch_expr, where default: label should match. */ 1014 css_default_processing 1015 }; 1016 1017 /* The constexpr expansion context. CALL is the current function 1018 expansion, CTOR is the current aggregate initializer, OBJECT is the 1019 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES 1020 is a map of values of variables initialized within the expression. */ 1021 1022 struct constexpr_ctx { 1023 /* The innermost call we're evaluating. */ 1024 constexpr_call *call; 1025 /* Values for any temporaries or local variables within the 1026 constant-expression. */ 1027 hash_map<tree,tree> *values; 1028 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we 1029 aren't inside a loop. */ 1030 vec<tree> *save_exprs; 1031 /* The CONSTRUCTOR we're currently building up for an aggregate 1032 initializer. */ 1033 tree ctor; 1034 /* The object we're building the CONSTRUCTOR for. */ 1035 tree object; 1036 /* If inside SWITCH_EXPR. */ 1037 constexpr_switch_state *css_state; 1038 /* Number of cxx_eval_constant_expression calls (except skipped ones, 1039 on simple constants or location wrappers) encountered during current 1040 cxx_eval_outermost_constant_expr call. */ 1041 HOST_WIDE_INT *constexpr_ops_count; 1042 1043 /* Whether we should error on a non-constant expression or fail quietly. */ 1044 bool quiet; 1045 /* Whether we are strictly conforming to constant expression rules or 1046 trying harder to get a constant value. */ 1047 bool strict; 1048 /* Whether __builtin_is_constant_evaluated () should be true. */ 1049 bool manifestly_const_eval; 1050 }; 1051 1052 /* A table of all constexpr calls that have been evaluated by the 1053 compiler in this translation unit. */ 1054 1055 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; 1056 1057 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, 1058 bool, bool *, bool *, tree * = NULL); 1059 1060 /* Compute a hash value for a constexpr call representation. */ 1061 1062 inline hashval_t 1063 constexpr_call_hasher::hash (constexpr_call *info) 1064 { 1065 return info->hash; 1066 } 1067 1068 /* Return true if the objects pointed to by P and Q represent calls 1069 to the same constexpr function with the same arguments. 1070 Otherwise, return false. */ 1071 1072 bool 1073 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) 1074 { 1075 tree lhs_bindings; 1076 tree rhs_bindings; 1077 if (lhs == rhs) 1078 return true; 1079 if (lhs->hash != rhs->hash) 1080 return false; 1081 if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) 1082 return false; 1083 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) 1084 return false; 1085 lhs_bindings = lhs->bindings; 1086 rhs_bindings = rhs->bindings; 1087 while (lhs_bindings != NULL && rhs_bindings != NULL) 1088 { 1089 tree lhs_arg = TREE_VALUE (lhs_bindings); 1090 tree rhs_arg = TREE_VALUE (rhs_bindings); 1091 gcc_assert (same_type_ignoring_top_level_qualifiers_p 1092 (TREE_TYPE (lhs_arg), TREE_TYPE (rhs_arg))); 1093 if (!cp_tree_equal (lhs_arg, rhs_arg)) 1094 return false; 1095 lhs_bindings = TREE_CHAIN (lhs_bindings); 1096 rhs_bindings = TREE_CHAIN (rhs_bindings); 1097 } 1098 return lhs_bindings == rhs_bindings; 1099 } 1100 1101 /* Initialize the constexpr call table, if needed. */ 1102 1103 static void 1104 maybe_initialize_constexpr_call_table (void) 1105 { 1106 if (constexpr_call_table == NULL) 1107 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); 1108 } 1109 1110 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when 1111 a function happens to get called recursively, we unshare the callee 1112 function's body and evaluate this unshared copy instead of evaluating the 1113 original body. 1114 1115 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function 1116 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map 1117 that's keyed off of the original FUNCTION_DECL and whose value is a 1118 TREE_LIST of this function's unused copies awaiting reuse. 1119 1120 This is not GC-deletable to avoid GC affecting UID generation. */ 1121 1122 static GTY(()) hash_map<tree, tree> *fundef_copies_table; 1123 1124 /* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */ 1125 1126 static void 1127 maybe_initialize_fundef_copies_table () 1128 { 1129 if (fundef_copies_table == NULL) 1130 fundef_copies_table = hash_map<tree,tree>::create_ggc (101); 1131 } 1132 1133 /* Reuse a copy or create a new unshared copy of the function FUN. 1134 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE 1135 is parms, TYPE is result. */ 1136 1137 static tree 1138 get_fundef_copy (constexpr_fundef *fundef) 1139 { 1140 maybe_initialize_fundef_copies_table (); 1141 1142 tree copy; 1143 bool existed; 1144 tree *slot = &fundef_copies_table->get_or_insert (fundef->decl, &existed); 1145 1146 if (!existed) 1147 { 1148 /* There is no cached function available, or in use. We can use 1149 the function directly. That the slot is now created records 1150 that this function is now in use. */ 1151 copy = build_tree_list (fundef->body, fundef->parms); 1152 TREE_TYPE (copy) = fundef->result; 1153 } 1154 else if (*slot == NULL_TREE) 1155 { 1156 /* We've already used the function itself, so make a copy. */ 1157 copy = build_tree_list (NULL, NULL); 1158 tree saved_body = DECL_SAVED_TREE (fundef->decl); 1159 tree saved_parms = DECL_ARGUMENTS (fundef->decl); 1160 tree saved_result = DECL_RESULT (fundef->decl); 1161 tree saved_fn = current_function_decl; 1162 DECL_SAVED_TREE (fundef->decl) = fundef->body; 1163 DECL_ARGUMENTS (fundef->decl) = fundef->parms; 1164 DECL_RESULT (fundef->decl) = fundef->result; 1165 current_function_decl = fundef->decl; 1166 TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), 1167 TREE_TYPE (copy)); 1168 current_function_decl = saved_fn; 1169 DECL_RESULT (fundef->decl) = saved_result; 1170 DECL_ARGUMENTS (fundef->decl) = saved_parms; 1171 DECL_SAVED_TREE (fundef->decl) = saved_body; 1172 } 1173 else 1174 { 1175 /* We have a cached function available. */ 1176 copy = *slot; 1177 *slot = TREE_CHAIN (copy); 1178 } 1179 1180 return copy; 1181 } 1182 1183 /* Save the copy COPY of function FUN for later reuse by 1184 get_fundef_copy(). By construction, there will always be an entry 1185 to find. */ 1186 1187 static void 1188 save_fundef_copy (tree fun, tree copy) 1189 { 1190 tree *slot = fundef_copies_table->get (fun); 1191 TREE_CHAIN (copy) = *slot; 1192 *slot = copy; 1193 } 1194 1195 /* We have an expression tree T that represents a call, either CALL_EXPR 1196 or AGGR_INIT_EXPR. Return the Nth argument. */ 1197 1198 static inline tree 1199 get_nth_callarg (tree t, int n) 1200 { 1201 switch (TREE_CODE (t)) 1202 { 1203 case CALL_EXPR: 1204 return CALL_EXPR_ARG (t, n); 1205 1206 case AGGR_INIT_EXPR: 1207 return AGGR_INIT_EXPR_ARG (t, n); 1208 1209 default: 1210 gcc_unreachable (); 1211 return NULL; 1212 } 1213 } 1214 1215 /* Attempt to evaluate T which represents a call to a builtin function. 1216 We assume here that all builtin functions evaluate to scalar types 1217 represented by _CST nodes. */ 1218 1219 static tree 1220 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, 1221 bool lval, 1222 bool *non_constant_p, bool *overflow_p) 1223 { 1224 const int nargs = call_expr_nargs (t); 1225 tree *args = (tree *) alloca (nargs * sizeof (tree)); 1226 tree new_call; 1227 int i; 1228 1229 /* Don't fold __builtin_constant_p within a constexpr function. */ 1230 bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); 1231 1232 /* If we aren't requiring a constant expression, defer __builtin_constant_p 1233 in a constexpr function until we have values for the parameters. */ 1234 if (bi_const_p 1235 && !ctx->manifestly_const_eval 1236 && current_function_decl 1237 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) 1238 { 1239 *non_constant_p = true; 1240 return t; 1241 } 1242 1243 /* For __builtin_is_constant_evaluated, defer it if not 1244 ctx->manifestly_const_eval, otherwise fold it to true. */ 1245 if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, 1246 BUILT_IN_FRONTEND)) 1247 { 1248 if (!ctx->manifestly_const_eval) 1249 { 1250 *non_constant_p = true; 1251 return t; 1252 } 1253 return boolean_true_node; 1254 } 1255 1256 /* Be permissive for arguments to built-ins; __builtin_constant_p should 1257 return constant false for a non-constant argument. */ 1258 constexpr_ctx new_ctx = *ctx; 1259 new_ctx.quiet = true; 1260 for (i = 0; i < nargs; ++i) 1261 { 1262 args[i] = CALL_EXPR_ARG (t, i); 1263 /* If builtin_valid_in_constant_expr_p is true, 1264 potential_constant_expression_1 has not recursed into the arguments 1265 of the builtin, verify it here. */ 1266 if (!builtin_valid_in_constant_expr_p (fun) 1267 || potential_constant_expression (args[i])) 1268 { 1269 bool dummy1 = false, dummy2 = false; 1270 args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false, 1271 &dummy1, &dummy2); 1272 } 1273 1274 if (bi_const_p) 1275 /* For __builtin_constant_p, fold all expressions with constant values 1276 even if they aren't C++ constant-expressions. */ 1277 args[i] = cp_fold_rvalue (args[i]); 1278 } 1279 1280 bool save_ffbcp = force_folding_builtin_constant_p; 1281 force_folding_builtin_constant_p = true; 1282 tree save_cur_fn = current_function_decl; 1283 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ 1284 if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) 1285 && ctx->call 1286 && ctx->call->fundef) 1287 current_function_decl = ctx->call->fundef->decl; 1288 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), 1289 CALL_EXPR_FN (t), nargs, args); 1290 current_function_decl = save_cur_fn; 1291 force_folding_builtin_constant_p = save_ffbcp; 1292 if (new_call == NULL) 1293 { 1294 if (!*non_constant_p && !ctx->quiet) 1295 { 1296 /* Do not allow__builtin_unreachable in constexpr function. 1297 The __builtin_unreachable call with BUILTINS_LOCATION 1298 comes from cp_maybe_instrument_return. */ 1299 if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE) 1300 && EXPR_LOCATION (t) == BUILTINS_LOCATION) 1301 error ("%<constexpr%> call flows off the end of the function"); 1302 else 1303 { 1304 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), 1305 CALL_EXPR_FN (t), nargs, args); 1306 error ("%q+E is not a constant expression", new_call); 1307 } 1308 } 1309 *non_constant_p = true; 1310 return t; 1311 } 1312 1313 if (!potential_constant_expression (new_call)) 1314 { 1315 if (!*non_constant_p && !ctx->quiet) 1316 error ("%q+E is not a constant expression", new_call); 1317 *non_constant_p = true; 1318 return t; 1319 } 1320 1321 return cxx_eval_constant_expression (&new_ctx, new_call, lval, 1322 non_constant_p, overflow_p); 1323 } 1324 1325 /* TEMP is the constant value of a temporary object of type TYPE. Adjust 1326 the type of the value to match. */ 1327 1328 static tree 1329 adjust_temp_type (tree type, tree temp) 1330 { 1331 if (same_type_p (TREE_TYPE (temp), type)) 1332 return temp; 1333 /* Avoid wrapping an aggregate value in a NOP_EXPR. */ 1334 if (TREE_CODE (temp) == CONSTRUCTOR) 1335 { 1336 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ 1337 tree t = copy_node (temp); 1338 TREE_TYPE (t) = type; 1339 return t; 1340 } 1341 if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) 1342 return build0 (EMPTY_CLASS_EXPR, type); 1343 gcc_assert (scalarish_type_p (type)); 1344 /* Now we know we're dealing with a scalar, and a prvalue of non-class 1345 type is cv-unqualified. */ 1346 return cp_fold_convert (cv_unqualified (type), temp); 1347 } 1348 1349 /* Callback for walk_tree used by unshare_constructor. */ 1350 1351 static tree 1352 find_constructor (tree *tp, int *walk_subtrees, void *) 1353 { 1354 if (TYPE_P (*tp)) 1355 *walk_subtrees = 0; 1356 if (TREE_CODE (*tp) == CONSTRUCTOR) 1357 return *tp; 1358 return NULL_TREE; 1359 } 1360 1361 /* If T is a CONSTRUCTOR or an expression that has a CONSTRUCTOR node as a 1362 subexpression, return an unshared copy of T. Otherwise return T. */ 1363 1364 tree 1365 unshare_constructor (tree t) 1366 { 1367 tree ctor = walk_tree (&t, find_constructor, NULL, NULL); 1368 if (ctor != NULL_TREE) 1369 return unshare_expr (t); 1370 return t; 1371 } 1372 1373 /* Subroutine of cxx_eval_call_expression. 1374 We are processing a call expression (either CALL_EXPR or 1375 AGGR_INIT_EXPR) in the context of CTX. Evaluate 1376 all arguments and bind their values to correspondings 1377 parameters, making up the NEW_CALL context. */ 1378 1379 static void 1380 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, 1381 constexpr_call *new_call, 1382 bool *non_constant_p, bool *overflow_p, 1383 bool *non_constant_args) 1384 { 1385 const int nargs = call_expr_nargs (t); 1386 tree fun = new_call->fundef->decl; 1387 tree parms = new_call->fundef->parms; 1388 int i; 1389 tree *p = &new_call->bindings; 1390 for (i = 0; i < nargs; ++i) 1391 { 1392 tree x, arg; 1393 tree type = parms ? TREE_TYPE (parms) : void_type_node; 1394 x = get_nth_callarg (t, i); 1395 /* For member function, the first argument is a pointer to the implied 1396 object. For a constructor, it might still be a dummy object, in 1397 which case we get the real argument from ctx. */ 1398 if (i == 0 && DECL_CONSTRUCTOR_P (fun) 1399 && is_dummy_object (x)) 1400 { 1401 x = ctx->object; 1402 x = build_address (x); 1403 } 1404 if (TREE_ADDRESSABLE (type)) 1405 /* Undo convert_for_arg_passing work here. */ 1406 x = convert_from_reference (x); 1407 arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false, 1408 non_constant_p, overflow_p); 1409 /* Don't VERIFY_CONSTANT here. */ 1410 if (*non_constant_p && ctx->quiet) 1411 return; 1412 /* Just discard ellipsis args after checking their constantitude. */ 1413 if (!parms) 1414 continue; 1415 1416 if (!*non_constant_p) 1417 { 1418 /* Don't share a CONSTRUCTOR that might be changed. */ 1419 arg = unshare_constructor (arg); 1420 /* Make sure the binding has the same type as the parm. But 1421 only for constant args. */ 1422 if (!TYPE_REF_P (type)) 1423 arg = adjust_temp_type (type, arg); 1424 if (!TREE_CONSTANT (arg)) 1425 *non_constant_args = true; 1426 1427 /* For virtual calls, adjust the this argument, so that it is 1428 the object on which the method is called, rather than 1429 one of its bases. */ 1430 if (i == 0 && DECL_VIRTUAL_P (fun)) 1431 { 1432 tree addr = arg; 1433 STRIP_NOPS (addr); 1434 if (TREE_CODE (addr) == ADDR_EXPR) 1435 { 1436 tree obj = TREE_OPERAND (addr, 0); 1437 while (TREE_CODE (obj) == COMPONENT_REF 1438 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) 1439 && !same_type_ignoring_top_level_qualifiers_p 1440 (TREE_TYPE (obj), DECL_CONTEXT (fun))) 1441 obj = TREE_OPERAND (obj, 0); 1442 if (obj != TREE_OPERAND (addr, 0)) 1443 arg = build_fold_addr_expr_with_type (obj, 1444 TREE_TYPE (arg)); 1445 } 1446 } 1447 1448 *p = build_tree_list (parms, arg); 1449 p = &TREE_CHAIN (*p); 1450 } 1451 parms = TREE_CHAIN (parms); 1452 } 1453 } 1454 1455 /* Variables and functions to manage constexpr call expansion context. 1456 These do not need to be marked for PCH or GC. */ 1457 1458 /* FIXME remember and print actual constant arguments. */ 1459 static vec<tree> call_stack; 1460 static int call_stack_tick; 1461 static int last_cx_error_tick; 1462 1463 static bool 1464 push_cx_call_context (tree call) 1465 { 1466 ++call_stack_tick; 1467 if (!EXPR_HAS_LOCATION (call)) 1468 SET_EXPR_LOCATION (call, input_location); 1469 call_stack.safe_push (call); 1470 if (call_stack.length () > (unsigned) max_constexpr_depth) 1471 return false; 1472 return true; 1473 } 1474 1475 static void 1476 pop_cx_call_context (void) 1477 { 1478 ++call_stack_tick; 1479 call_stack.pop (); 1480 } 1481 1482 vec<tree> 1483 cx_error_context (void) 1484 { 1485 vec<tree> r = vNULL; 1486 if (call_stack_tick != last_cx_error_tick 1487 && !call_stack.is_empty ()) 1488 r = call_stack; 1489 last_cx_error_tick = call_stack_tick; 1490 return r; 1491 } 1492 1493 /* Evaluate a call T to a GCC internal function when possible and return 1494 the evaluated result or, under the control of CTX, give an error, set 1495 NON_CONSTANT_P, and return the unevaluated call T otherwise. */ 1496 1497 static tree 1498 cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, 1499 bool lval, 1500 bool *non_constant_p, bool *overflow_p) 1501 { 1502 enum tree_code opcode = ERROR_MARK; 1503 1504 switch (CALL_EXPR_IFN (t)) 1505 { 1506 case IFN_UBSAN_NULL: 1507 case IFN_UBSAN_BOUNDS: 1508 case IFN_UBSAN_VPTR: 1509 case IFN_FALLTHROUGH: 1510 return void_node; 1511 1512 case IFN_ADD_OVERFLOW: 1513 opcode = PLUS_EXPR; 1514 break; 1515 case IFN_SUB_OVERFLOW: 1516 opcode = MINUS_EXPR; 1517 break; 1518 case IFN_MUL_OVERFLOW: 1519 opcode = MULT_EXPR; 1520 break; 1521 1522 case IFN_LAUNDER: 1523 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), 1524 false, non_constant_p, overflow_p); 1525 1526 case IFN_VEC_CONVERT: 1527 { 1528 tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), 1529 false, non_constant_p, 1530 overflow_p); 1531 if (TREE_CODE (arg) == VECTOR_CST) 1532 return fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg); 1533 else 1534 { 1535 *non_constant_p = true; 1536 return t; 1537 } 1538 } 1539 1540 default: 1541 if (!ctx->quiet) 1542 error_at (cp_expr_loc_or_loc (t, input_location), 1543 "call to internal function %qE", t); 1544 *non_constant_p = true; 1545 return t; 1546 } 1547 1548 /* Evaluate constant arguments using OPCODE and return a complex 1549 number containing the result and the overflow bit. */ 1550 tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, 1551 non_constant_p, overflow_p); 1552 tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, 1553 non_constant_p, overflow_p); 1554 1555 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) 1556 { 1557 location_t loc = cp_expr_loc_or_loc (t, input_location); 1558 tree type = TREE_TYPE (TREE_TYPE (t)); 1559 tree result = fold_binary_loc (loc, opcode, type, 1560 fold_convert_loc (loc, type, arg0), 1561 fold_convert_loc (loc, type, arg1)); 1562 tree ovf 1563 = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); 1564 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ 1565 if (TREE_OVERFLOW (result)) 1566 TREE_OVERFLOW (result) = 0; 1567 1568 return build_complex (TREE_TYPE (t), result, ovf); 1569 } 1570 1571 *non_constant_p = true; 1572 return t; 1573 } 1574 1575 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ 1576 1577 static void 1578 clear_no_implicit_zero (tree ctor) 1579 { 1580 if (CONSTRUCTOR_NO_CLEARING (ctor)) 1581 { 1582 CONSTRUCTOR_NO_CLEARING (ctor) = false; 1583 tree elt; unsigned HOST_WIDE_INT idx; 1584 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, elt) 1585 if (TREE_CODE (elt) == CONSTRUCTOR) 1586 clear_no_implicit_zero (elt); 1587 } 1588 } 1589 1590 /* Subroutine of cxx_eval_constant_expression. 1591 Evaluate the call expression tree T in the context of OLD_CALL expression 1592 evaluation. */ 1593 1594 static tree 1595 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, 1596 bool lval, 1597 bool *non_constant_p, bool *overflow_p) 1598 { 1599 location_t loc = cp_expr_loc_or_loc (t, input_location); 1600 tree fun = get_function_named_in_call (t); 1601 constexpr_call new_call 1602 = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; 1603 bool depth_ok; 1604 1605 if (fun == NULL_TREE) 1606 return cxx_eval_internal_function (ctx, t, lval, 1607 non_constant_p, overflow_p); 1608 1609 if (TREE_CODE (fun) != FUNCTION_DECL) 1610 { 1611 /* Might be a constexpr function pointer. */ 1612 fun = cxx_eval_constant_expression (ctx, fun, 1613 /*lval*/false, non_constant_p, 1614 overflow_p); 1615 STRIP_NOPS (fun); 1616 if (TREE_CODE (fun) == ADDR_EXPR) 1617 fun = TREE_OPERAND (fun, 0); 1618 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no 1619 indirection, the called expression is a pointer into the 1620 virtual table which should contain FDESC_EXPR. Extract the 1621 FUNCTION_DECL from there. */ 1622 else if (TARGET_VTABLE_USES_DESCRIPTORS 1623 && TREE_CODE (fun) == POINTER_PLUS_EXPR 1624 && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR 1625 && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) 1626 { 1627 tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); 1628 if (VAR_P (d) 1629 && DECL_VTABLE_OR_VTT_P (d) 1630 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE 1631 && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type 1632 && DECL_INITIAL (d) 1633 && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) 1634 { 1635 tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), 1636 TYPE_SIZE_UNIT (vtable_entry_type)); 1637 HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); 1638 if (idx >= 0) 1639 { 1640 tree fdesc 1641 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; 1642 if (TREE_CODE (fdesc) == FDESC_EXPR 1643 && integer_zerop (TREE_OPERAND (fdesc, 1))) 1644 fun = TREE_OPERAND (fdesc, 0); 1645 } 1646 } 1647 } 1648 } 1649 if (TREE_CODE (fun) != FUNCTION_DECL) 1650 { 1651 if (!ctx->quiet && !*non_constant_p) 1652 error_at (loc, "expression %qE does not designate a %<constexpr%> " 1653 "function", fun); 1654 *non_constant_p = true; 1655 return t; 1656 } 1657 if (DECL_CLONED_FUNCTION_P (fun)) 1658 fun = DECL_CLONED_FUNCTION (fun); 1659 1660 if (is_ubsan_builtin_p (fun)) 1661 return void_node; 1662 1663 if (fndecl_built_in_p (fun)) 1664 return cxx_eval_builtin_function_call (ctx, t, fun, 1665 lval, non_constant_p, overflow_p); 1666 if (!DECL_DECLARED_CONSTEXPR_P (fun)) 1667 { 1668 if (!ctx->quiet) 1669 { 1670 if (!lambda_static_thunk_p (fun)) 1671 error_at (loc, "call to non-%<constexpr%> function %qD", fun); 1672 explain_invalid_constexpr_fn (fun); 1673 } 1674 *non_constant_p = true; 1675 return t; 1676 } 1677 1678 constexpr_ctx new_ctx = *ctx; 1679 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object 1680 && TREE_CODE (t) == AGGR_INIT_EXPR) 1681 { 1682 /* We want to have an initialization target for an AGGR_INIT_EXPR. 1683 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ 1684 new_ctx.object = AGGR_INIT_EXPR_SLOT (t); 1685 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); 1686 CONSTRUCTOR_NO_CLEARING (ctor) = true; 1687 ctx->values->put (new_ctx.object, ctor); 1688 ctx = &new_ctx; 1689 } 1690 1691 /* Shortcut trivial constructor/op=. */ 1692 if (trivial_fn_p (fun)) 1693 { 1694 tree init = NULL_TREE; 1695 if (call_expr_nargs (t) == 2) 1696 init = convert_from_reference (get_nth_callarg (t, 1)); 1697 else if (TREE_CODE (t) == AGGR_INIT_EXPR 1698 && AGGR_INIT_ZERO_FIRST (t)) 1699 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); 1700 if (init) 1701 { 1702 tree op = get_nth_callarg (t, 0); 1703 if (is_dummy_object (op)) 1704 op = ctx->object; 1705 else 1706 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); 1707 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); 1708 new_ctx.call = &new_call; 1709 return cxx_eval_constant_expression (&new_ctx, set, lval, 1710 non_constant_p, overflow_p); 1711 } 1712 } 1713 1714 /* We can't defer instantiating the function any longer. */ 1715 if (!DECL_INITIAL (fun) 1716 && DECL_TEMPLOID_INSTANTIATION (fun)) 1717 { 1718 location_t save_loc = input_location; 1719 input_location = loc; 1720 ++function_depth; 1721 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); 1722 --function_depth; 1723 input_location = save_loc; 1724 } 1725 1726 /* If in direct recursive call, optimize definition search. */ 1727 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) 1728 new_call.fundef = ctx->call->fundef; 1729 else 1730 { 1731 new_call.fundef = retrieve_constexpr_fundef (fun); 1732 if (new_call.fundef == NULL || new_call.fundef->body == NULL 1733 || fun == current_function_decl) 1734 { 1735 if (!ctx->quiet) 1736 { 1737 /* We need to check for current_function_decl here in case we're 1738 being called during cp_fold_function, because at that point 1739 DECL_INITIAL is set properly and we have a fundef but we 1740 haven't lowered invisirefs yet (c++/70344). */ 1741 if (DECL_INITIAL (fun) == error_mark_node 1742 || fun == current_function_decl) 1743 error_at (loc, "%qD called in a constant expression before its " 1744 "definition is complete", fun); 1745 else if (DECL_INITIAL (fun)) 1746 { 1747 /* The definition of fun was somehow unsuitable. But pretend 1748 that lambda static thunks don't exist. */ 1749 if (!lambda_static_thunk_p (fun)) 1750 error_at (loc, "%qD called in a constant expression", fun); 1751 explain_invalid_constexpr_fn (fun); 1752 } 1753 else 1754 error_at (loc, "%qD used before its definition", fun); 1755 } 1756 *non_constant_p = true; 1757 return t; 1758 } 1759 } 1760 1761 bool non_constant_args = false; 1762 cxx_bind_parameters_in_call (ctx, t, &new_call, 1763 non_constant_p, overflow_p, &non_constant_args); 1764 if (*non_constant_p) 1765 return t; 1766 1767 depth_ok = push_cx_call_context (t); 1768 1769 tree result = NULL_TREE; 1770 1771 constexpr_call *entry = NULL; 1772 if (depth_ok && !non_constant_args && ctx->strict) 1773 { 1774 new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); 1775 new_call.hash 1776 = iterative_hash_template_arg (new_call.bindings, new_call.hash); 1777 new_call.hash 1778 = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); 1779 1780 /* If we have seen this call before, we are done. */ 1781 maybe_initialize_constexpr_call_table (); 1782 constexpr_call **slot 1783 = constexpr_call_table->find_slot (&new_call, INSERT); 1784 entry = *slot; 1785 if (entry == NULL) 1786 { 1787 /* We need to keep a pointer to the entry, not just the slot, as the 1788 slot can move in the call to cxx_eval_builtin_function_call. */ 1789 *slot = entry = ggc_alloc<constexpr_call> (); 1790 *entry = new_call; 1791 } 1792 /* Calls that are in progress have their result set to NULL, 1793 so that we can detect circular dependencies. */ 1794 else if (entry->result == NULL) 1795 { 1796 if (!ctx->quiet) 1797 error ("call has circular dependency"); 1798 *non_constant_p = true; 1799 entry->result = result = error_mark_node; 1800 } 1801 else 1802 result = entry->result; 1803 } 1804 1805 if (!depth_ok) 1806 { 1807 if (!ctx->quiet) 1808 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " 1809 "%<-fconstexpr-depth=%> to increase the maximum)", 1810 max_constexpr_depth); 1811 *non_constant_p = true; 1812 result = error_mark_node; 1813 } 1814 else 1815 { 1816 if (result && result != error_mark_node) 1817 /* OK */; 1818 else if (!DECL_SAVED_TREE (fun)) 1819 { 1820 /* When at_eof >= 2, cgraph has started throwing away 1821 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of 1822 late code generation for VEC_INIT_EXPR, which needs to be 1823 completely reconsidered. */ 1824 gcc_assert (at_eof >= 2 && ctx->quiet); 1825 *non_constant_p = true; 1826 } 1827 else 1828 { 1829 tree body, parms, res; 1830 1831 /* Reuse or create a new unshared copy of this function's body. */ 1832 tree copy = get_fundef_copy (new_call.fundef); 1833 body = TREE_PURPOSE (copy); 1834 parms = TREE_VALUE (copy); 1835 res = TREE_TYPE (copy); 1836 1837 /* Associate the bindings with the remapped parms. */ 1838 tree bound = new_call.bindings; 1839 tree remapped = parms; 1840 while (bound) 1841 { 1842 tree oparm = TREE_PURPOSE (bound); 1843 tree arg = TREE_VALUE (bound); 1844 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm)); 1845 /* Don't share a CONSTRUCTOR that might be changed. */ 1846 arg = unshare_constructor (arg); 1847 ctx->values->put (remapped, arg); 1848 bound = TREE_CHAIN (bound); 1849 remapped = DECL_CHAIN (remapped); 1850 } 1851 /* Add the RESULT_DECL to the values map, too. */ 1852 tree slot = NULL_TREE; 1853 if (DECL_BY_REFERENCE (res)) 1854 { 1855 slot = AGGR_INIT_EXPR_SLOT (t); 1856 tree addr = build_address (slot); 1857 addr = build_nop (TREE_TYPE (res), addr); 1858 ctx->values->put (res, addr); 1859 ctx->values->put (slot, NULL_TREE); 1860 } 1861 else 1862 ctx->values->put (res, NULL_TREE); 1863 1864 /* Track the callee's evaluated SAVE_EXPRs so that we can forget 1865 their values after the call. */ 1866 constexpr_ctx ctx_with_save_exprs = *ctx; 1867 auto_vec<tree, 10> save_exprs; 1868 ctx_with_save_exprs.save_exprs = &save_exprs; 1869 ctx_with_save_exprs.call = &new_call; 1870 1871 tree jump_target = NULL_TREE; 1872 cxx_eval_constant_expression (&ctx_with_save_exprs, body, 1873 lval, non_constant_p, overflow_p, 1874 &jump_target); 1875 1876 if (DECL_CONSTRUCTOR_P (fun)) 1877 /* This can be null for a subobject constructor call, in 1878 which case what we care about is the initialization 1879 side-effects rather than the value. We could get at the 1880 value by evaluating *this, but we don't bother; there's 1881 no need to put such a call in the hash table. */ 1882 result = lval ? ctx->object : ctx->ctor; 1883 else if (VOID_TYPE_P (TREE_TYPE (res))) 1884 result = void_node; 1885 else 1886 { 1887 result = *ctx->values->get (slot ? slot : res); 1888 if (result == NULL_TREE && !*non_constant_p) 1889 { 1890 if (!ctx->quiet) 1891 error ("%<constexpr%> call flows off the end " 1892 "of the function"); 1893 *non_constant_p = true; 1894 } 1895 } 1896 1897 /* Forget the saved values of the callee's SAVE_EXPRs. */ 1898 unsigned int i; 1899 tree save_expr; 1900 FOR_EACH_VEC_ELT (save_exprs, i, save_expr) 1901 ctx_with_save_exprs.values->remove (save_expr); 1902 1903 /* Remove the parms/result from the values map. Is it worth 1904 bothering to do this when the map itself is only live for 1905 one constexpr evaluation? If so, maybe also clear out 1906 other vars from call, maybe in BIND_EXPR handling? */ 1907 ctx->values->remove (res); 1908 if (slot) 1909 ctx->values->remove (slot); 1910 for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) 1911 ctx->values->remove (parm); 1912 1913 /* Make the unshared function copy we used available for re-use. */ 1914 save_fundef_copy (fun, copy); 1915 } 1916 1917 if (result == error_mark_node) 1918 *non_constant_p = true; 1919 if (*non_constant_p || *overflow_p) 1920 result = error_mark_node; 1921 else if (!result) 1922 result = void_node; 1923 if (entry) 1924 entry->result = result; 1925 } 1926 1927 /* The result of a constexpr function must be completely initialized. */ 1928 if (TREE_CODE (result) == CONSTRUCTOR) 1929 clear_no_implicit_zero (result); 1930 1931 pop_cx_call_context (); 1932 return unshare_constructor (result); 1933 } 1934 1935 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ 1936 1937 bool 1938 reduced_constant_expression_p (tree t) 1939 { 1940 if (t == NULL_TREE) 1941 return false; 1942 1943 switch (TREE_CODE (t)) 1944 { 1945 case PTRMEM_CST: 1946 /* Even if we can't lower this yet, it's constant. */ 1947 return true; 1948 1949 case CONSTRUCTOR: 1950 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ 1951 tree idx, val, field; unsigned HOST_WIDE_INT i; 1952 if (CONSTRUCTOR_NO_CLEARING (t)) 1953 { 1954 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) 1955 /* An initialized vector would have a VECTOR_CST. */ 1956 return false; 1957 else 1958 field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); 1959 } 1960 else 1961 field = NULL_TREE; 1962 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val) 1963 { 1964 /* If VAL is null, we're in the middle of initializing this 1965 element. */ 1966 if (!reduced_constant_expression_p (val)) 1967 return false; 1968 if (field) 1969 { 1970 if (idx != field) 1971 return false; 1972 field = next_initializable_field (DECL_CHAIN (field)); 1973 } 1974 } 1975 if (field) 1976 return false; 1977 else if (CONSTRUCTOR_NO_CLEARING (t)) 1978 /* All the fields are initialized. */ 1979 CONSTRUCTOR_NO_CLEARING (t) = false; 1980 return true; 1981 1982 default: 1983 /* FIXME are we calling this too much? */ 1984 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; 1985 } 1986 } 1987 1988 /* Some expressions may have constant operands but are not constant 1989 themselves, such as 1/0. Call this function to check for that 1990 condition. 1991 1992 We only call this in places that require an arithmetic constant, not in 1993 places where we might have a non-constant expression that can be a 1994 component of a constant expression, such as the address of a constexpr 1995 variable that might be dereferenced later. */ 1996 1997 static bool 1998 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, 1999 bool *overflow_p) 2000 { 2001 if (!*non_constant_p && !reduced_constant_expression_p (t)) 2002 { 2003 if (!allow_non_constant) 2004 error ("%q+E is not a constant expression", t); 2005 *non_constant_p = true; 2006 } 2007 if (TREE_OVERFLOW_P (t)) 2008 { 2009 if (!allow_non_constant) 2010 { 2011 permerror (input_location, "overflow in constant expression"); 2012 /* If we're being permissive (and are in an enforcing 2013 context), ignore the overflow. */ 2014 if (flag_permissive) 2015 return *non_constant_p; 2016 } 2017 *overflow_p = true; 2018 } 2019 return *non_constant_p; 2020 } 2021 2022 /* Check whether the shift operation with code CODE and type TYPE on LHS 2023 and RHS is undefined. If it is, give an error with an explanation, 2024 and return true; return false otherwise. */ 2025 2026 static bool 2027 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, 2028 enum tree_code code, tree type, tree lhs, tree rhs) 2029 { 2030 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) 2031 || TREE_CODE (lhs) != INTEGER_CST 2032 || TREE_CODE (rhs) != INTEGER_CST) 2033 return false; 2034 2035 tree lhstype = TREE_TYPE (lhs); 2036 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); 2037 2038 /* [expr.shift] The behavior is undefined if the right operand 2039 is negative, or greater than or equal to the length in bits 2040 of the promoted left operand. */ 2041 if (tree_int_cst_sgn (rhs) == -1) 2042 { 2043 if (!ctx->quiet) 2044 permerror (loc, "right operand of shift expression %q+E is negative", 2045 build2_loc (loc, code, type, lhs, rhs)); 2046 return (!flag_permissive || ctx->quiet); 2047 } 2048 if (compare_tree_int (rhs, uprec) >= 0) 2049 { 2050 if (!ctx->quiet) 2051 permerror (loc, "right operand of shift expression %q+E is >= than " 2052 "the precision of the left operand", 2053 build2_loc (loc, code, type, lhs, rhs)); 2054 return (!flag_permissive || ctx->quiet); 2055 } 2056 2057 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] 2058 if E1 has a signed type and non-negative value, and E1x2^E2 is 2059 representable in the corresponding unsigned type of the result type, 2060 then that value, converted to the result type, is the resulting value; 2061 otherwise, the behavior is undefined. 2062 For C++2a: 2063 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo 2064 2^N, where N is the range exponent of the type of the result. */ 2065 if (code == LSHIFT_EXPR 2066 && !TYPE_UNSIGNED (lhstype) 2067 && cxx_dialect >= cxx11 2068 && cxx_dialect < cxx2a) 2069 { 2070 if (tree_int_cst_sgn (lhs) == -1) 2071 { 2072 if (!ctx->quiet) 2073 permerror (loc, 2074 "left operand of shift expression %q+E is negative", 2075 build2_loc (loc, code, type, lhs, rhs)); 2076 return (!flag_permissive || ctx->quiet); 2077 } 2078 /* For signed x << y the following: 2079 (unsigned) x >> ((prec (lhs) - 1) - y) 2080 if > 1, is undefined. The right-hand side of this formula 2081 is the highest bit of the LHS that can be set (starting from 0), 2082 so that the shift doesn't overflow. We then right-shift the LHS 2083 to see whether any other bit is set making the original shift 2084 undefined -- the result is not representable in the corresponding 2085 unsigned type. */ 2086 tree t = build_int_cst (unsigned_type_node, uprec - 1); 2087 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); 2088 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); 2089 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); 2090 if (tree_int_cst_lt (integer_one_node, t)) 2091 { 2092 if (!ctx->quiet) 2093 permerror (loc, "shift expression %q+E overflows", 2094 build2_loc (loc, code, type, lhs, rhs)); 2095 return (!flag_permissive || ctx->quiet); 2096 } 2097 } 2098 return false; 2099 } 2100 2101 /* Subroutine of cxx_eval_constant_expression. 2102 Attempt to reduce the unary expression tree T to a compile time value. 2103 If successful, return the value. Otherwise issue a diagnostic 2104 and return error_mark_node. */ 2105 2106 static tree 2107 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, 2108 bool /*lval*/, 2109 bool *non_constant_p, bool *overflow_p) 2110 { 2111 tree r; 2112 tree orig_arg = TREE_OPERAND (t, 0); 2113 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false, 2114 non_constant_p, overflow_p); 2115 VERIFY_CONSTANT (arg); 2116 location_t loc = EXPR_LOCATION (t); 2117 enum tree_code code = TREE_CODE (t); 2118 tree type = TREE_TYPE (t); 2119 r = fold_unary_loc (loc, code, type, arg); 2120 if (r == NULL_TREE) 2121 { 2122 if (arg == orig_arg) 2123 r = t; 2124 else 2125 r = build1_loc (loc, code, type, arg); 2126 } 2127 VERIFY_CONSTANT (r); 2128 return r; 2129 } 2130 2131 /* Helper function for cxx_eval_binary_expression. Try to optimize 2132 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the 2133 generic folding should be used. */ 2134 2135 static tree 2136 cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, 2137 tree lhs, tree rhs, bool *non_constant_p, 2138 bool *overflow_p) 2139 { 2140 STRIP_NOPS (lhs); 2141 if (TREE_CODE (lhs) != ADDR_EXPR) 2142 return NULL_TREE; 2143 2144 lhs = TREE_OPERAND (lhs, 0); 2145 2146 /* &A[i] p+ j => &A[i + j] */ 2147 if (TREE_CODE (lhs) == ARRAY_REF 2148 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST 2149 && TREE_CODE (rhs) == INTEGER_CST 2150 && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) 2151 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) 2152 { 2153 tree orig_type = TREE_TYPE (t); 2154 location_t loc = EXPR_LOCATION (t); 2155 tree type = TREE_TYPE (lhs); 2156 2157 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); 2158 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); 2159 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, 2160 overflow_p); 2161 if (*non_constant_p) 2162 return NULL_TREE; 2163 /* Don't fold an out-of-bound access. */ 2164 if (!tree_int_cst_le (t, nelts)) 2165 return NULL_TREE; 2166 rhs = cp_fold_convert (ssizetype, rhs); 2167 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. 2168 constexpr int A[1]; ... (char *)&A[0] + 1 */ 2169 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, 2170 rhs, TYPE_SIZE_UNIT (type)))) 2171 return NULL_TREE; 2172 /* Make sure to treat the second operand of POINTER_PLUS_EXPR 2173 as signed. */ 2174 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, 2175 TYPE_SIZE_UNIT (type)); 2176 t = size_binop_loc (loc, PLUS_EXPR, rhs, t); 2177 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), 2178 t, NULL_TREE, NULL_TREE); 2179 t = cp_build_addr_expr (t, tf_warning_or_error); 2180 t = cp_fold_convert (orig_type, t); 2181 return cxx_eval_constant_expression (ctx, t, /*lval*/false, 2182 non_constant_p, overflow_p); 2183 } 2184 2185 return NULL_TREE; 2186 } 2187 2188 /* Subroutine of cxx_eval_constant_expression. 2189 Like cxx_eval_unary_expression, except for binary expressions. */ 2190 2191 static tree 2192 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, 2193 bool /*lval*/, 2194 bool *non_constant_p, bool *overflow_p) 2195 { 2196 tree r = NULL_TREE; 2197 tree orig_lhs = TREE_OPERAND (t, 0); 2198 tree orig_rhs = TREE_OPERAND (t, 1); 2199 tree lhs, rhs; 2200 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false, 2201 non_constant_p, overflow_p); 2202 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer 2203 subtraction. */ 2204 if (*non_constant_p) 2205 return t; 2206 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false, 2207 non_constant_p, overflow_p); 2208 if (*non_constant_p) 2209 return t; 2210 2211 location_t loc = EXPR_LOCATION (t); 2212 enum tree_code code = TREE_CODE (t); 2213 tree type = TREE_TYPE (t); 2214 2215 if (code == EQ_EXPR || code == NE_EXPR) 2216 { 2217 bool is_code_eq = (code == EQ_EXPR); 2218 2219 if (TREE_CODE (lhs) == PTRMEM_CST 2220 && TREE_CODE (rhs) == PTRMEM_CST) 2221 { 2222 tree lmem = PTRMEM_CST_MEMBER (lhs); 2223 tree rmem = PTRMEM_CST_MEMBER (rhs); 2224 bool eq; 2225 if (TREE_CODE (lmem) == TREE_CODE (rmem) 2226 && TREE_CODE (lmem) == FIELD_DECL 2227 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE 2228 && same_type_p (DECL_CONTEXT (lmem), 2229 DECL_CONTEXT (rmem))) 2230 /* If both refer to (possibly different) members of the same union 2231 (12.3), they compare equal. */ 2232 eq = true; 2233 else 2234 eq = cp_tree_equal (lhs, rhs); 2235 r = constant_boolean_node (eq == is_code_eq, type); 2236 } 2237 else if ((TREE_CODE (lhs) == PTRMEM_CST 2238 || TREE_CODE (rhs) == PTRMEM_CST) 2239 && (null_member_pointer_value_p (lhs) 2240 || null_member_pointer_value_p (rhs))) 2241 r = constant_boolean_node (!is_code_eq, type); 2242 else if (TREE_CODE (lhs) == PTRMEM_CST) 2243 lhs = cplus_expand_constant (lhs); 2244 else if (TREE_CODE (rhs) == PTRMEM_CST) 2245 rhs = cplus_expand_constant (rhs); 2246 } 2247 if (code == POINTER_PLUS_EXPR && !*non_constant_p 2248 && integer_zerop (lhs) && !integer_zerop (rhs)) 2249 { 2250 if (!ctx->quiet) 2251 error ("arithmetic involving a null pointer in %qE", lhs); 2252 *non_constant_p = true; 2253 return t; 2254 } 2255 else if (code == POINTER_PLUS_EXPR) 2256 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, 2257 overflow_p); 2258 2259 if (r == NULL_TREE) 2260 r = fold_binary_loc (loc, code, type, lhs, rhs); 2261 2262 if (r == NULL_TREE) 2263 { 2264 if (lhs == orig_lhs && rhs == orig_rhs) 2265 r = t; 2266 else 2267 r = build2_loc (loc, code, type, lhs, rhs); 2268 } 2269 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) 2270 *non_constant_p = true; 2271 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to 2272 a local array in a constexpr function. */ 2273 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); 2274 if (!ptr) 2275 VERIFY_CONSTANT (r); 2276 return r; 2277 } 2278 2279 /* Subroutine of cxx_eval_constant_expression. 2280 Attempt to evaluate condition expressions. Dead branches are not 2281 looked into. */ 2282 2283 static tree 2284 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, 2285 bool lval, 2286 bool *non_constant_p, bool *overflow_p, 2287 tree *jump_target) 2288 { 2289 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 2290 /*lval*/false, 2291 non_constant_p, overflow_p); 2292 VERIFY_CONSTANT (val); 2293 /* Don't VERIFY_CONSTANT the other operands. */ 2294 if (integer_zerop (val)) 2295 val = TREE_OPERAND (t, 2); 2296 else 2297 val = TREE_OPERAND (t, 1); 2298 if (TREE_CODE (t) == IF_STMT && !val) 2299 val = void_node; 2300 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, 2301 overflow_p, jump_target); 2302 } 2303 2304 /* Subroutine of cxx_eval_constant_expression. 2305 Attempt to evaluate vector condition expressions. Unlike 2306 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal 2307 ternary arithmetics operation, where all 3 arguments have to be 2308 evaluated as constants and then folding computes the result from 2309 them. */ 2310 2311 static tree 2312 cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, 2313 bool *non_constant_p, bool *overflow_p) 2314 { 2315 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 2316 /*lval*/false, 2317 non_constant_p, overflow_p); 2318 VERIFY_CONSTANT (arg1); 2319 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 2320 /*lval*/false, 2321 non_constant_p, overflow_p); 2322 VERIFY_CONSTANT (arg2); 2323 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), 2324 /*lval*/false, 2325 non_constant_p, overflow_p); 2326 VERIFY_CONSTANT (arg3); 2327 location_t loc = EXPR_LOCATION (t); 2328 tree type = TREE_TYPE (t); 2329 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); 2330 if (r == NULL_TREE) 2331 { 2332 if (arg1 == TREE_OPERAND (t, 0) 2333 && arg2 == TREE_OPERAND (t, 1) 2334 && arg3 == TREE_OPERAND (t, 2)) 2335 r = t; 2336 else 2337 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); 2338 } 2339 VERIFY_CONSTANT (r); 2340 return r; 2341 } 2342 2343 /* Returns less than, equal to, or greater than zero if KEY is found to be 2344 less than, to match, or to be greater than the constructor_elt's INDEX. */ 2345 2346 static int 2347 array_index_cmp (tree key, tree index) 2348 { 2349 gcc_assert (TREE_CODE (key) == INTEGER_CST); 2350 2351 switch (TREE_CODE (index)) 2352 { 2353 case INTEGER_CST: 2354 return tree_int_cst_compare (key, index); 2355 case RANGE_EXPR: 2356 { 2357 tree lo = TREE_OPERAND (index, 0); 2358 tree hi = TREE_OPERAND (index, 1); 2359 if (tree_int_cst_lt (key, lo)) 2360 return -1; 2361 else if (tree_int_cst_lt (hi, key)) 2362 return 1; 2363 else 2364 return 0; 2365 } 2366 default: 2367 gcc_unreachable (); 2368 } 2369 } 2370 2371 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 2372 if none. If INSERT is true, insert a matching element rather than fail. */ 2373 2374 static HOST_WIDE_INT 2375 find_array_ctor_elt (tree ary, tree dindex, bool insert) 2376 { 2377 if (tree_int_cst_sgn (dindex) < 0) 2378 return -1; 2379 2380 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); 2381 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); 2382 unsigned HOST_WIDE_INT len = vec_safe_length (elts); 2383 2384 unsigned HOST_WIDE_INT end = len; 2385 unsigned HOST_WIDE_INT begin = 0; 2386 2387 /* If the last element of the CONSTRUCTOR has its own index, we can assume 2388 that the same is true of the other elements and index directly. */ 2389 if (end > 0) 2390 { 2391 tree cindex = (*elts)[end - 1].index; 2392 if (TREE_CODE (cindex) == INTEGER_CST 2393 && compare_tree_int (cindex, end - 1) == 0) 2394 { 2395 if (i < end) 2396 return i; 2397 else 2398 begin = end; 2399 } 2400 } 2401 2402 /* Otherwise, find a matching index by means of a binary search. */ 2403 while (begin != end) 2404 { 2405 unsigned HOST_WIDE_INT middle = (begin + end) / 2; 2406 constructor_elt &elt = (*elts)[middle]; 2407 tree idx = elt.index; 2408 2409 int cmp = array_index_cmp (dindex, idx); 2410 if (cmp < 0) 2411 end = middle; 2412 else if (cmp > 0) 2413 begin = middle + 1; 2414 else 2415 { 2416 if (insert && TREE_CODE (idx) == RANGE_EXPR) 2417 { 2418 /* We need to split the range. */ 2419 constructor_elt e; 2420 tree lo = TREE_OPERAND (idx, 0); 2421 tree hi = TREE_OPERAND (idx, 1); 2422 tree value = elt.value; 2423 dindex = fold_convert (sizetype, dindex); 2424 if (tree_int_cst_lt (lo, dindex)) 2425 { 2426 /* There are still some lower elts; shorten the range. */ 2427 tree new_hi = int_const_binop (MINUS_EXPR, dindex, 2428 size_one_node); 2429 if (tree_int_cst_equal (lo, new_hi)) 2430 /* Only one element left, no longer a range. */ 2431 elt.index = lo; 2432 else 2433 TREE_OPERAND (idx, 1) = new_hi; 2434 /* Append the element we want to insert. */ 2435 ++middle; 2436 e.index = dindex; 2437 e.value = unshare_constructor (value); 2438 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); 2439 } 2440 else 2441 /* No lower elts, the range elt is now ours. */ 2442 elt.index = dindex; 2443 2444 if (tree_int_cst_lt (dindex, hi)) 2445 { 2446 /* There are still some higher elts; append a range. */ 2447 tree new_lo = int_const_binop (PLUS_EXPR, dindex, 2448 size_one_node); 2449 if (tree_int_cst_equal (new_lo, hi)) 2450 e.index = hi; 2451 else 2452 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); 2453 e.value = unshare_constructor (value); 2454 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); 2455 } 2456 } 2457 return middle; 2458 } 2459 } 2460 2461 if (insert) 2462 { 2463 constructor_elt e = { dindex, NULL_TREE }; 2464 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); 2465 return end; 2466 } 2467 2468 return -1; 2469 } 2470 2471 /* Under the control of CTX, issue a detailed diagnostic for 2472 an out-of-bounds subscript INDEX into the expression ARRAY. */ 2473 2474 static void 2475 diag_array_subscript (const constexpr_ctx *ctx, tree array, tree index) 2476 { 2477 if (!ctx->quiet) 2478 { 2479 tree arraytype = TREE_TYPE (array); 2480 2481 /* Convert the unsigned array subscript to a signed integer to avoid 2482 printing huge numbers for small negative values. */ 2483 tree sidx = fold_convert (ssizetype, index); 2484 if (DECL_P (array)) 2485 { 2486 if (TYPE_DOMAIN (arraytype)) 2487 error ("array subscript value %qE is outside the bounds " 2488 "of array %qD of type %qT", sidx, array, arraytype); 2489 else 2490 error ("non-zero array subscript %qE is used with array %qD of " 2491 "type %qT with unknown bounds", sidx, array, arraytype); 2492 inform (DECL_SOURCE_LOCATION (array), "declared here"); 2493 } 2494 else if (TYPE_DOMAIN (arraytype)) 2495 error ("array subscript value %qE is outside the bounds " 2496 "of array type %qT", sidx, arraytype); 2497 else 2498 error ("non-zero array subscript %qE is used with array of type %qT " 2499 "with unknown bounds", sidx, arraytype); 2500 } 2501 } 2502 2503 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or 2504 a VECTOR_TYPE). */ 2505 2506 static tree 2507 get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, 2508 bool *non_constant_p, bool *overflow_p) 2509 { 2510 tree nelts; 2511 if (TREE_CODE (type) == ARRAY_TYPE) 2512 { 2513 if (TYPE_DOMAIN (type)) 2514 nelts = array_type_nelts_top (type); 2515 else 2516 nelts = size_zero_node; 2517 } 2518 else if (VECTOR_TYPE_P (type)) 2519 nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); 2520 else 2521 gcc_unreachable (); 2522 2523 /* For VLAs, the number of elements won't be an integer constant. */ 2524 nelts = cxx_eval_constant_expression (ctx, nelts, false, 2525 non_constant_p, overflow_p); 2526 return nelts; 2527 } 2528 2529 /* Extract element INDEX consisting of CHARS_PER_ELT chars from 2530 STRING_CST STRING. */ 2531 2532 static tree 2533 extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) 2534 { 2535 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); 2536 tree r; 2537 2538 if (chars_per_elt == 1) 2539 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); 2540 else 2541 { 2542 const unsigned char *ptr 2543 = ((const unsigned char *)TREE_STRING_POINTER (string) 2544 + index * chars_per_elt); 2545 r = native_interpret_expr (type, ptr, chars_per_elt); 2546 } 2547 return r; 2548 } 2549 2550 /* Subroutine of cxx_eval_constant_expression. 2551 Attempt to reduce a reference to an array slot. */ 2552 2553 static tree 2554 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, 2555 bool lval, 2556 bool *non_constant_p, bool *overflow_p) 2557 { 2558 tree oldary = TREE_OPERAND (t, 0); 2559 tree ary = cxx_eval_constant_expression (ctx, oldary, 2560 lval, 2561 non_constant_p, overflow_p); 2562 tree index, oldidx; 2563 HOST_WIDE_INT i = 0; 2564 tree elem_type = NULL_TREE; 2565 unsigned len = 0, elem_nchars = 1; 2566 if (*non_constant_p) 2567 return t; 2568 oldidx = TREE_OPERAND (t, 1); 2569 index = cxx_eval_constant_expression (ctx, oldidx, 2570 false, 2571 non_constant_p, overflow_p); 2572 VERIFY_CONSTANT (index); 2573 if (!lval) 2574 { 2575 elem_type = TREE_TYPE (TREE_TYPE (ary)); 2576 if (TREE_CODE (ary) == VIEW_CONVERT_EXPR 2577 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) 2578 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) 2579 ary = TREE_OPERAND (ary, 0); 2580 if (TREE_CODE (ary) == CONSTRUCTOR) 2581 len = CONSTRUCTOR_NELTS (ary); 2582 else if (TREE_CODE (ary) == STRING_CST) 2583 { 2584 elem_nchars = (TYPE_PRECISION (elem_type) 2585 / TYPE_PRECISION (char_type_node)); 2586 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; 2587 } 2588 else if (TREE_CODE (ary) == VECTOR_CST) 2589 /* We don't create variable-length VECTOR_CSTs. */ 2590 len = VECTOR_CST_NELTS (ary).to_constant (); 2591 else 2592 { 2593 /* We can't do anything with other tree codes, so use 2594 VERIFY_CONSTANT to complain and fail. */ 2595 VERIFY_CONSTANT (ary); 2596 gcc_unreachable (); 2597 } 2598 2599 if (!tree_fits_shwi_p (index) 2600 || (i = tree_to_shwi (index)) < 0) 2601 { 2602 diag_array_subscript (ctx, ary, index); 2603 *non_constant_p = true; 2604 return t; 2605 } 2606 } 2607 2608 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, 2609 overflow_p); 2610 VERIFY_CONSTANT (nelts); 2611 if ((lval 2612 ? !tree_int_cst_le (index, nelts) 2613 : !tree_int_cst_lt (index, nelts)) 2614 || tree_int_cst_sgn (index) < 0) 2615 { 2616 diag_array_subscript (ctx, ary, index); 2617 *non_constant_p = true; 2618 return t; 2619 } 2620 2621 if (lval && ary == oldary && index == oldidx) 2622 return t; 2623 else if (lval) 2624 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); 2625 2626 bool found; 2627 if (TREE_CODE (ary) == CONSTRUCTOR) 2628 { 2629 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); 2630 found = (ix >= 0); 2631 if (found) 2632 i = ix; 2633 } 2634 else 2635 found = (i < len); 2636 2637 if (found) 2638 { 2639 tree r; 2640 if (TREE_CODE (ary) == CONSTRUCTOR) 2641 r = (*CONSTRUCTOR_ELTS (ary))[i].value; 2642 else if (TREE_CODE (ary) == VECTOR_CST) 2643 r = VECTOR_CST_ELT (ary, i); 2644 else 2645 r = extract_string_elt (ary, elem_nchars, i); 2646 2647 if (r) 2648 /* Don't VERIFY_CONSTANT here. */ 2649 return r; 2650 2651 /* Otherwise the element doesn't have a value yet. */ 2652 } 2653 2654 /* Not found. */ 2655 2656 if (TREE_CODE (ary) == CONSTRUCTOR 2657 && CONSTRUCTOR_NO_CLEARING (ary)) 2658 { 2659 /* 'ary' is part of the aggregate initializer we're currently 2660 building; if there's no initializer for this element yet, 2661 that's an error. */ 2662 if (!ctx->quiet) 2663 error ("accessing uninitialized array element"); 2664 *non_constant_p = true; 2665 return t; 2666 } 2667 2668 /* If it's within the array bounds but doesn't have an explicit 2669 initializer, it's initialized from {}. But use build_value_init 2670 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ 2671 tree val; 2672 if (CP_AGGREGATE_TYPE_P (elem_type)) 2673 { 2674 tree empty_ctor = build_constructor (init_list_type_node, NULL); 2675 val = digest_init (elem_type, empty_ctor, tf_warning_or_error); 2676 } 2677 else 2678 val = build_value_init (elem_type, tf_warning_or_error); 2679 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, 2680 overflow_p); 2681 } 2682 2683 /* Subroutine of cxx_eval_constant_expression. 2684 Attempt to reduce a field access of a value of class type. */ 2685 2686 static tree 2687 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, 2688 bool lval, 2689 bool *non_constant_p, bool *overflow_p) 2690 { 2691 unsigned HOST_WIDE_INT i; 2692 tree field; 2693 tree value; 2694 tree part = TREE_OPERAND (t, 1); 2695 tree orig_whole = TREE_OPERAND (t, 0); 2696 tree whole = cxx_eval_constant_expression (ctx, orig_whole, 2697 lval, 2698 non_constant_p, overflow_p); 2699 if (INDIRECT_REF_P (whole) 2700 && integer_zerop (TREE_OPERAND (whole, 0))) 2701 { 2702 if (!ctx->quiet) 2703 error ("dereferencing a null pointer in %qE", orig_whole); 2704 *non_constant_p = true; 2705 return t; 2706 } 2707 2708 if (TREE_CODE (whole) == PTRMEM_CST) 2709 whole = cplus_expand_constant (whole); 2710 if (whole == orig_whole) 2711 return t; 2712 if (lval) 2713 return fold_build3 (COMPONENT_REF, TREE_TYPE (t), 2714 whole, part, NULL_TREE); 2715 /* Don't VERIFY_CONSTANT here; we only want to check that we got a 2716 CONSTRUCTOR. */ 2717 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) 2718 { 2719 if (!ctx->quiet) 2720 error ("%qE is not a constant expression", orig_whole); 2721 *non_constant_p = true; 2722 } 2723 if (DECL_MUTABLE_P (part)) 2724 { 2725 if (!ctx->quiet) 2726 error ("mutable %qD is not usable in a constant expression", part); 2727 *non_constant_p = true; 2728 } 2729 if (*non_constant_p) 2730 return t; 2731 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); 2732 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) 2733 { 2734 /* Use name match for PMF fields, as a variant will have a 2735 different FIELD_DECL with a different type. */ 2736 if (pmf ? DECL_NAME (field) == DECL_NAME (part) 2737 : field == part) 2738 { 2739 if (value) 2740 { 2741 STRIP_ANY_LOCATION_WRAPPER (value); 2742 return value; 2743 } 2744 else 2745 /* We're in the middle of initializing it. */ 2746 break; 2747 } 2748 } 2749 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE 2750 && CONSTRUCTOR_NELTS (whole) > 0) 2751 { 2752 /* DR 1188 says we don't have to deal with this. */ 2753 if (!ctx->quiet) 2754 error ("accessing %qD member instead of initialized %qD member in " 2755 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index); 2756 *non_constant_p = true; 2757 return t; 2758 } 2759 2760 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty 2761 classes never get represented; throw together a value now. */ 2762 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 2763 return build_constructor (TREE_TYPE (t), NULL); 2764 2765 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); 2766 2767 if (CONSTRUCTOR_NO_CLEARING (whole)) 2768 { 2769 /* 'whole' is part of the aggregate initializer we're currently 2770 building; if there's no initializer for this member yet, that's an 2771 error. */ 2772 if (!ctx->quiet) 2773 error ("accessing uninitialized member %qD", part); 2774 *non_constant_p = true; 2775 return t; 2776 } 2777 2778 /* If there's no explicit init for this field, it's value-initialized. */ 2779 value = build_value_init (TREE_TYPE (t), tf_warning_or_error); 2780 return cxx_eval_constant_expression (ctx, value, 2781 lval, 2782 non_constant_p, overflow_p); 2783 } 2784 2785 /* Subroutine of cxx_eval_constant_expression. 2786 Attempt to reduce a field access of a value of class type that is 2787 expressed as a BIT_FIELD_REF. */ 2788 2789 static tree 2790 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, 2791 bool lval, 2792 bool *non_constant_p, bool *overflow_p) 2793 { 2794 tree orig_whole = TREE_OPERAND (t, 0); 2795 tree retval, fldval, utype, mask; 2796 bool fld_seen = false; 2797 HOST_WIDE_INT istart, isize; 2798 tree whole = cxx_eval_constant_expression (ctx, orig_whole, 2799 lval, 2800 non_constant_p, overflow_p); 2801 tree start, field, value; 2802 unsigned HOST_WIDE_INT i; 2803 2804 if (whole == orig_whole) 2805 return t; 2806 /* Don't VERIFY_CONSTANT here; we only want to check that we got a 2807 CONSTRUCTOR. */ 2808 if (!*non_constant_p 2809 && TREE_CODE (whole) != VECTOR_CST 2810 && TREE_CODE (whole) != CONSTRUCTOR) 2811 { 2812 if (!ctx->quiet) 2813 error ("%qE is not a constant expression", orig_whole); 2814 *non_constant_p = true; 2815 } 2816 if (*non_constant_p) 2817 return t; 2818 2819 if (TREE_CODE (whole) == VECTOR_CST) 2820 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, 2821 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); 2822 2823 start = TREE_OPERAND (t, 2); 2824 istart = tree_to_shwi (start); 2825 isize = tree_to_shwi (TREE_OPERAND (t, 1)); 2826 utype = TREE_TYPE (t); 2827 if (!TYPE_UNSIGNED (utype)) 2828 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); 2829 retval = build_int_cst (utype, 0); 2830 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) 2831 { 2832 tree bitpos = bit_position (field); 2833 STRIP_ANY_LOCATION_WRAPPER (value); 2834 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) 2835 return value; 2836 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE 2837 && TREE_CODE (value) == INTEGER_CST 2838 && tree_fits_shwi_p (bitpos) 2839 && tree_fits_shwi_p (DECL_SIZE (field))) 2840 { 2841 HOST_WIDE_INT bit = tree_to_shwi (bitpos); 2842 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); 2843 HOST_WIDE_INT shift; 2844 if (bit >= istart && bit + sz <= istart + isize) 2845 { 2846 fldval = fold_convert (utype, value); 2847 mask = build_int_cst_type (utype, -1); 2848 mask = fold_build2 (LSHIFT_EXPR, utype, mask, 2849 size_int (TYPE_PRECISION (utype) - sz)); 2850 mask = fold_build2 (RSHIFT_EXPR, utype, mask, 2851 size_int (TYPE_PRECISION (utype) - sz)); 2852 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); 2853 shift = bit - istart; 2854 if (BYTES_BIG_ENDIAN) 2855 shift = TYPE_PRECISION (utype) - shift - sz; 2856 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, 2857 size_int (shift)); 2858 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); 2859 fld_seen = true; 2860 } 2861 } 2862 } 2863 if (fld_seen) 2864 return fold_convert (TREE_TYPE (t), retval); 2865 gcc_unreachable (); 2866 return error_mark_node; 2867 } 2868 2869 /* Subroutine of cxx_eval_constant_expression. 2870 Evaluate a short-circuited logical expression T in the context 2871 of a given constexpr CALL. BAILOUT_VALUE is the value for 2872 early return. CONTINUE_VALUE is used here purely for 2873 sanity check purposes. */ 2874 2875 static tree 2876 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, 2877 tree bailout_value, tree continue_value, 2878 bool lval, 2879 bool *non_constant_p, bool *overflow_p) 2880 { 2881 tree r; 2882 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 2883 lval, 2884 non_constant_p, overflow_p); 2885 VERIFY_CONSTANT (lhs); 2886 if (tree_int_cst_equal (lhs, bailout_value)) 2887 return lhs; 2888 gcc_assert (tree_int_cst_equal (lhs, continue_value)); 2889 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 2890 lval, non_constant_p, 2891 overflow_p); 2892 VERIFY_CONSTANT (r); 2893 return r; 2894 } 2895 2896 /* REF is a COMPONENT_REF designating a particular field. V is a vector of 2897 CONSTRUCTOR elements to initialize (part of) an object containing that 2898 field. Return a pointer to the constructor_elt corresponding to the 2899 initialization of the field. */ 2900 2901 static constructor_elt * 2902 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) 2903 { 2904 tree aggr = TREE_OPERAND (ref, 0); 2905 tree field = TREE_OPERAND (ref, 1); 2906 HOST_WIDE_INT i; 2907 constructor_elt *ce; 2908 2909 gcc_assert (TREE_CODE (ref) == COMPONENT_REF); 2910 2911 if (TREE_CODE (aggr) == COMPONENT_REF) 2912 { 2913 constructor_elt *base_ce 2914 = base_field_constructor_elt (v, aggr); 2915 v = CONSTRUCTOR_ELTS (base_ce->value); 2916 } 2917 2918 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 2919 if (ce->index == field) 2920 return ce; 2921 2922 gcc_unreachable (); 2923 return NULL; 2924 } 2925 2926 /* Some of the expressions fed to the constexpr mechanism are calls to 2927 constructors, which have type void. In that case, return the type being 2928 initialized by the constructor. */ 2929 2930 static tree 2931 initialized_type (tree t) 2932 { 2933 if (TYPE_P (t)) 2934 return t; 2935 tree type = TREE_TYPE (t); 2936 if (TREE_CODE (t) == CALL_EXPR) 2937 { 2938 /* A constructor call has void type, so we need to look deeper. */ 2939 tree fn = get_function_named_in_call (t); 2940 if (fn && TREE_CODE (fn) == FUNCTION_DECL 2941 && DECL_CXX_CONSTRUCTOR_P (fn)) 2942 type = DECL_CONTEXT (fn); 2943 } 2944 else if (TREE_CODE (t) == COMPOUND_EXPR) 2945 return initialized_type (TREE_OPERAND (t, 1)); 2946 else if (TREE_CODE (t) == AGGR_INIT_EXPR) 2947 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); 2948 return cv_unqualified (type); 2949 } 2950 2951 /* We're about to initialize element INDEX of an array or class from VALUE. 2952 Set up NEW_CTX appropriately by adjusting .object to refer to the 2953 subobject and creating a new CONSTRUCTOR if the element is itself 2954 a class or array. */ 2955 2956 static void 2957 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, 2958 tree index, tree &value) 2959 { 2960 new_ctx = *ctx; 2961 2962 if (index && TREE_CODE (index) != INTEGER_CST 2963 && TREE_CODE (index) != FIELD_DECL) 2964 /* This won't have an element in the new CONSTRUCTOR. */ 2965 return; 2966 2967 tree type = initialized_type (value); 2968 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) 2969 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ 2970 return; 2971 2972 /* The sub-aggregate initializer might contain a placeholder; 2973 update object to refer to the subobject and ctor to refer to 2974 the (newly created) sub-initializer. */ 2975 if (ctx->object) 2976 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); 2977 tree elt = build_constructor (type, NULL); 2978 CONSTRUCTOR_NO_CLEARING (elt) = true; 2979 new_ctx.ctor = elt; 2980 2981 if (TREE_CODE (value) == TARGET_EXPR) 2982 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ 2983 value = TARGET_EXPR_INITIAL (value); 2984 } 2985 2986 /* We're about to process an initializer for a class or array TYPE. Make 2987 sure that CTX is set up appropriately. */ 2988 2989 static void 2990 verify_ctor_sanity (const constexpr_ctx *ctx, tree type) 2991 { 2992 /* We don't bother building a ctor for an empty base subobject. */ 2993 if (is_empty_class (type)) 2994 return; 2995 2996 /* We're in the middle of an initializer that might involve placeholders; 2997 our caller should have created a CONSTRUCTOR for us to put the 2998 initializer into. We will either return that constructor or T. */ 2999 gcc_assert (ctx->ctor); 3000 gcc_assert (same_type_ignoring_top_level_qualifiers_p 3001 (type, TREE_TYPE (ctx->ctor))); 3002 /* We used to check that ctx->ctor was empty, but that isn't the case when 3003 the object is zero-initialized before calling the constructor. */ 3004 if (ctx->object) 3005 { 3006 tree otype = TREE_TYPE (ctx->object); 3007 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) 3008 /* Handle flexible array members. */ 3009 || (TREE_CODE (otype) == ARRAY_TYPE 3010 && TYPE_DOMAIN (otype) == NULL_TREE 3011 && TREE_CODE (type) == ARRAY_TYPE 3012 && (same_type_ignoring_top_level_qualifiers_p 3013 (TREE_TYPE (type), TREE_TYPE (otype))))); 3014 } 3015 gcc_assert (!ctx->object || !DECL_P (ctx->object) 3016 || *(ctx->values->get (ctx->object)) == ctx->ctor); 3017 } 3018 3019 /* Subroutine of cxx_eval_constant_expression. 3020 The expression tree T denotes a C-style array or a C-style 3021 aggregate. Reduce it to a constant expression. */ 3022 3023 static tree 3024 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, 3025 bool lval, 3026 bool *non_constant_p, bool *overflow_p) 3027 { 3028 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); 3029 bool changed = false; 3030 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); 3031 tree type = TREE_TYPE (t); 3032 3033 constexpr_ctx new_ctx; 3034 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) 3035 { 3036 /* We don't really need the ctx->ctor business for a PMF or 3037 vector, but it's simpler to use the same code. */ 3038 new_ctx = *ctx; 3039 new_ctx.ctor = build_constructor (type, NULL); 3040 new_ctx.object = NULL_TREE; 3041 ctx = &new_ctx; 3042 }; 3043 verify_ctor_sanity (ctx, type); 3044 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); 3045 vec_alloc (*p, vec_safe_length (v)); 3046 3047 unsigned i; 3048 tree index, value; 3049 bool constant_p = true; 3050 bool side_effects_p = false; 3051 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) 3052 { 3053 tree orig_value = value; 3054 init_subob_ctx (ctx, new_ctx, index, value); 3055 if (new_ctx.ctor != ctx->ctor) 3056 /* If we built a new CONSTRUCTOR, attach it now so that other 3057 initializers can refer to it. */ 3058 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor); 3059 tree elt = cxx_eval_constant_expression (&new_ctx, value, 3060 lval, 3061 non_constant_p, overflow_p); 3062 /* Don't VERIFY_CONSTANT here. */ 3063 if (ctx->quiet && *non_constant_p) 3064 break; 3065 if (elt != orig_value) 3066 changed = true; 3067 3068 if (!TREE_CONSTANT (elt)) 3069 constant_p = false; 3070 if (TREE_SIDE_EFFECTS (elt)) 3071 side_effects_p = true; 3072 if (index && TREE_CODE (index) == COMPONENT_REF) 3073 { 3074 /* This is an initialization of a vfield inside a base 3075 subaggregate that we already initialized; push this 3076 initialization into the previous initialization. */ 3077 constructor_elt *inner = base_field_constructor_elt (*p, index); 3078 inner->value = elt; 3079 changed = true; 3080 } 3081 else if (index 3082 && (TREE_CODE (index) == NOP_EXPR 3083 || TREE_CODE (index) == POINTER_PLUS_EXPR)) 3084 { 3085 /* This is an initializer for an empty base; now that we've 3086 checked that it's constant, we can ignore it. */ 3087 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); 3088 changed = true; 3089 } 3090 else 3091 { 3092 if (new_ctx.ctor != ctx->ctor) 3093 { 3094 /* We appended this element above; update the value. */ 3095 gcc_assert ((*p)->last().index == index); 3096 (*p)->last().value = elt; 3097 } 3098 else 3099 CONSTRUCTOR_APPEND_ELT (*p, index, elt); 3100 /* Adding or replacing an element might change the ctor's flags. */ 3101 TREE_CONSTANT (ctx->ctor) = constant_p; 3102 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; 3103 } 3104 } 3105 if (*non_constant_p || !changed) 3106 return t; 3107 t = ctx->ctor; 3108 /* We're done building this CONSTRUCTOR, so now we can interpret an 3109 element without an explicit initializer as value-initialized. */ 3110 CONSTRUCTOR_NO_CLEARING (t) = false; 3111 TREE_CONSTANT (t) = constant_p; 3112 TREE_SIDE_EFFECTS (t) = side_effects_p; 3113 if (VECTOR_TYPE_P (type)) 3114 t = fold (t); 3115 return t; 3116 } 3117 3118 /* Subroutine of cxx_eval_constant_expression. 3119 The expression tree T is a VEC_INIT_EXPR which denotes the desired 3120 initialization of a non-static data member of array type. Reduce it to a 3121 CONSTRUCTOR. 3122 3123 Note that apart from value-initialization (when VALUE_INIT is true), 3124 this is only intended to support value-initialization and the 3125 initializations done by defaulted constructors for classes with 3126 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT 3127 will either be NULL_TREE for the default constructor, or a COMPONENT_REF 3128 for the copy/move constructor. */ 3129 3130 static tree 3131 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, 3132 bool value_init, bool lval, 3133 bool *non_constant_p, bool *overflow_p) 3134 { 3135 tree elttype = TREE_TYPE (atype); 3136 verify_ctor_sanity (ctx, atype); 3137 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); 3138 bool pre_init = false; 3139 unsigned HOST_WIDE_INT i; 3140 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; 3141 3142 if (init && TREE_CODE (init) == CONSTRUCTOR) 3143 return cxx_eval_bare_aggregate (ctx, init, lval, 3144 non_constant_p, overflow_p); 3145 3146 /* For the default constructor, build up a call to the default 3147 constructor of the element type. We only need to handle class types 3148 here, as for a constructor to be constexpr, all members must be 3149 initialized, which for a defaulted default constructor means they must 3150 be of a class type with a constexpr default constructor. */ 3151 if (TREE_CODE (elttype) == ARRAY_TYPE) 3152 /* We only do this at the lowest level. */; 3153 else if (value_init) 3154 { 3155 init = build_value_init (elttype, complain); 3156 pre_init = true; 3157 } 3158 else if (!init) 3159 { 3160 vec<tree, va_gc> *argvec = make_tree_vector (); 3161 init = build_special_member_call (NULL_TREE, complete_ctor_identifier, 3162 &argvec, elttype, LOOKUP_NORMAL, 3163 complain); 3164 release_tree_vector (argvec); 3165 init = build_aggr_init_expr (elttype, init); 3166 pre_init = true; 3167 } 3168 3169 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, 3170 overflow_p); 3171 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); 3172 for (i = 0; i < max; ++i) 3173 { 3174 tree idx = build_int_cst (size_type_node, i); 3175 tree eltinit; 3176 bool reuse = false; 3177 constexpr_ctx new_ctx; 3178 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); 3179 if (new_ctx.ctor != ctx->ctor) 3180 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); 3181 if (TREE_CODE (elttype) == ARRAY_TYPE) 3182 { 3183 /* A multidimensional array; recurse. */ 3184 if (value_init || init == NULL_TREE) 3185 { 3186 eltinit = NULL_TREE; 3187 reuse = i == 0; 3188 } 3189 else 3190 eltinit = cp_build_array_ref (input_location, init, idx, complain); 3191 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, 3192 lval, 3193 non_constant_p, overflow_p); 3194 } 3195 else if (pre_init) 3196 { 3197 /* Initializing an element using value or default initialization 3198 we just pre-built above. */ 3199 if (init == void_node) 3200 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ 3201 return ctx->ctor; 3202 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, 3203 non_constant_p, overflow_p); 3204 reuse = i == 0; 3205 } 3206 else 3207 { 3208 /* Copying an element. */ 3209 gcc_assert (same_type_ignoring_top_level_qualifiers_p 3210 (atype, TREE_TYPE (init))); 3211 eltinit = cp_build_array_ref (input_location, init, idx, complain); 3212 if (!lvalue_p (init)) 3213 eltinit = move (eltinit); 3214 eltinit = force_rvalue (eltinit, complain); 3215 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, 3216 non_constant_p, overflow_p); 3217 } 3218 if (*non_constant_p && !ctx->quiet) 3219 break; 3220 if (new_ctx.ctor != ctx->ctor) 3221 { 3222 /* We appended this element above; update the value. */ 3223 gcc_assert ((*p)->last().index == idx); 3224 (*p)->last().value = eltinit; 3225 } 3226 else 3227 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); 3228 /* Reuse the result of cxx_eval_constant_expression call 3229 from the first iteration to all others if it is a constant 3230 initializer that doesn't require relocations. */ 3231 if (reuse 3232 && max > 1 3233 && (eltinit == NULL_TREE 3234 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) 3235 == null_pointer_node))) 3236 { 3237 if (new_ctx.ctor != ctx->ctor) 3238 eltinit = new_ctx.ctor; 3239 tree range = build2 (RANGE_EXPR, size_type_node, 3240 build_int_cst (size_type_node, 1), 3241 build_int_cst (size_type_node, max - 1)); 3242 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); 3243 break; 3244 } 3245 else if (i == 0) 3246 vec_safe_reserve (*p, max); 3247 } 3248 3249 if (!*non_constant_p) 3250 { 3251 init = ctx->ctor; 3252 CONSTRUCTOR_NO_CLEARING (init) = false; 3253 } 3254 return init; 3255 } 3256 3257 static tree 3258 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, 3259 bool lval, 3260 bool *non_constant_p, bool *overflow_p) 3261 { 3262 tree atype = TREE_TYPE (t); 3263 tree init = VEC_INIT_EXPR_INIT (t); 3264 tree r = cxx_eval_vec_init_1 (ctx, atype, init, 3265 VEC_INIT_EXPR_VALUE_INIT (t), 3266 lval, non_constant_p, overflow_p); 3267 if (*non_constant_p) 3268 return t; 3269 else 3270 return r; 3271 } 3272 3273 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case 3274 where the desired type is an array of unknown bounds because the variable 3275 has had its bounds deduced since the wrapping expression was created. */ 3276 3277 static bool 3278 same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) 3279 { 3280 while (TREE_CODE (type1) == ARRAY_TYPE 3281 && TREE_CODE (type2) == ARRAY_TYPE 3282 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) 3283 { 3284 type1 = TREE_TYPE (type1); 3285 type2 = TREE_TYPE (type2); 3286 } 3287 return same_type_ignoring_top_level_qualifiers_p (type1, type2); 3288 } 3289 3290 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to 3291 match. We want to be less strict for simple *& folding; if we have a 3292 non-const temporary that we access through a const pointer, that should 3293 work. We handle this here rather than change fold_indirect_ref_1 3294 because we're dealing with things like ADDR_EXPR of INTEGER_CST which 3295 don't really make sense outside of constant expression evaluation. Also 3296 we want to allow folding to COMPONENT_REF, which could cause trouble 3297 with TBAA in fold_indirect_ref_1. 3298 3299 Try to keep this function synced with fold_indirect_ref_1. */ 3300 3301 static tree 3302 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base) 3303 { 3304 tree sub = op0; 3305 tree subtype; 3306 poly_uint64 const_op01; 3307 3308 STRIP_NOPS (sub); 3309 subtype = TREE_TYPE (sub); 3310 if (!INDIRECT_TYPE_P (subtype)) 3311 return NULL_TREE; 3312 3313 if (TREE_CODE (sub) == ADDR_EXPR) 3314 { 3315 tree op = TREE_OPERAND (sub, 0); 3316 tree optype = TREE_TYPE (op); 3317 3318 /* *&CONST_DECL -> to the value of the const decl. */ 3319 if (TREE_CODE (op) == CONST_DECL) 3320 return DECL_INITIAL (op); 3321 /* *&p => p; make sure to handle *&"str"[cst] here. */ 3322 if (same_type_ignoring_tlq_and_bounds_p (optype, type)) 3323 { 3324 tree fop = fold_read_from_constant_string (op); 3325 if (fop) 3326 return fop; 3327 else 3328 return op; 3329 } 3330 /* *(foo *)&fooarray => fooarray[0] */ 3331 else if (TREE_CODE (optype) == ARRAY_TYPE 3332 && (same_type_ignoring_top_level_qualifiers_p 3333 (type, TREE_TYPE (optype)))) 3334 { 3335 tree type_domain = TYPE_DOMAIN (optype); 3336 tree min_val = size_zero_node; 3337 if (type_domain && TYPE_MIN_VALUE (type_domain)) 3338 min_val = TYPE_MIN_VALUE (type_domain); 3339 return build4_loc (loc, ARRAY_REF, type, op, min_val, 3340 NULL_TREE, NULL_TREE); 3341 } 3342 /* *(foo *)&complexfoo => __real__ complexfoo */ 3343 else if (TREE_CODE (optype) == COMPLEX_TYPE 3344 && (same_type_ignoring_top_level_qualifiers_p 3345 (type, TREE_TYPE (optype)))) 3346 return fold_build1_loc (loc, REALPART_EXPR, type, op); 3347 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ 3348 else if (VECTOR_TYPE_P (optype) 3349 && (same_type_ignoring_top_level_qualifiers_p 3350 (type, TREE_TYPE (optype)))) 3351 { 3352 tree part_width = TYPE_SIZE (type); 3353 tree index = bitsize_int (0); 3354 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, 3355 index); 3356 } 3357 /* Also handle conversion to an empty base class, which 3358 is represented with a NOP_EXPR. */ 3359 else if (is_empty_class (type) 3360 && CLASS_TYPE_P (optype) 3361 && DERIVED_FROM_P (type, optype)) 3362 { 3363 *empty_base = true; 3364 return op; 3365 } 3366 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */ 3367 else if (RECORD_OR_UNION_TYPE_P (optype)) 3368 { 3369 tree field = TYPE_FIELDS (optype); 3370 for (; field; field = DECL_CHAIN (field)) 3371 if (TREE_CODE (field) == FIELD_DECL 3372 && TREE_TYPE (field) != error_mark_node 3373 && integer_zerop (byte_position (field)) 3374 && (same_type_ignoring_top_level_qualifiers_p 3375 (TREE_TYPE (field), type))) 3376 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE); 3377 } 3378 } 3379 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR 3380 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01)) 3381 { 3382 tree op00 = TREE_OPERAND (sub, 0); 3383 tree op01 = TREE_OPERAND (sub, 1); 3384 3385 STRIP_NOPS (op00); 3386 if (TREE_CODE (op00) == ADDR_EXPR) 3387 { 3388 tree op00type; 3389 op00 = TREE_OPERAND (op00, 0); 3390 op00type = TREE_TYPE (op00); 3391 3392 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */ 3393 if (VECTOR_TYPE_P (op00type) 3394 && same_type_ignoring_top_level_qualifiers_p 3395 (type, TREE_TYPE (op00type)) 3396 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned, 3397 but we want to treat offsets with MSB set as negative. 3398 For the code below negative offsets are invalid and 3399 TYPE_SIZE of the element is something unsigned, so 3400 check whether op01 fits into poly_int64, which implies 3401 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and 3402 then just use poly_uint64 because we want to treat the 3403 value as unsigned. */ 3404 && tree_fits_poly_int64_p (op01)) 3405 { 3406 tree part_width = TYPE_SIZE (type); 3407 poly_uint64 max_offset 3408 = (tree_to_uhwi (part_width) / BITS_PER_UNIT 3409 * TYPE_VECTOR_SUBPARTS (op00type)); 3410 if (known_lt (const_op01, max_offset)) 3411 { 3412 tree index = bitsize_int (const_op01 * BITS_PER_UNIT); 3413 return fold_build3_loc (loc, 3414 BIT_FIELD_REF, type, op00, 3415 part_width, index); 3416 } 3417 } 3418 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ 3419 else if (TREE_CODE (op00type) == COMPLEX_TYPE 3420 && (same_type_ignoring_top_level_qualifiers_p 3421 (type, TREE_TYPE (op00type)))) 3422 { 3423 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)), 3424 const_op01)) 3425 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00); 3426 } 3427 /* ((foo *)&fooarray)[1] => fooarray[1] */ 3428 else if (TREE_CODE (op00type) == ARRAY_TYPE 3429 && (same_type_ignoring_top_level_qualifiers_p 3430 (type, TREE_TYPE (op00type)))) 3431 { 3432 tree type_domain = TYPE_DOMAIN (op00type); 3433 tree min_val = size_zero_node; 3434 if (type_domain && TYPE_MIN_VALUE (type_domain)) 3435 min_val = TYPE_MIN_VALUE (type_domain); 3436 offset_int off = wi::to_offset (op01); 3437 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type)); 3438 offset_int remainder; 3439 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder); 3440 if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST) 3441 { 3442 off = off + wi::to_offset (min_val); 3443 op01 = wide_int_to_tree (sizetype, off); 3444 return build4_loc (loc, ARRAY_REF, type, op00, op01, 3445 NULL_TREE, NULL_TREE); 3446 } 3447 } 3448 /* Also handle conversion to an empty base class, which 3449 is represented with a NOP_EXPR. */ 3450 else if (is_empty_class (type) 3451 && CLASS_TYPE_P (op00type) 3452 && DERIVED_FROM_P (type, op00type)) 3453 { 3454 *empty_base = true; 3455 return op00; 3456 } 3457 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */ 3458 else if (RECORD_OR_UNION_TYPE_P (op00type)) 3459 { 3460 tree field = TYPE_FIELDS (op00type); 3461 for (; field; field = DECL_CHAIN (field)) 3462 if (TREE_CODE (field) == FIELD_DECL 3463 && TREE_TYPE (field) != error_mark_node 3464 && tree_int_cst_equal (byte_position (field), op01) 3465 && (same_type_ignoring_top_level_qualifiers_p 3466 (TREE_TYPE (field), type))) 3467 return fold_build3 (COMPONENT_REF, type, op00, 3468 field, NULL_TREE); 3469 } 3470 } 3471 } 3472 /* *(foo *)fooarrptr => (*fooarrptr)[0] */ 3473 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE 3474 && (same_type_ignoring_top_level_qualifiers_p 3475 (type, TREE_TYPE (TREE_TYPE (subtype))))) 3476 { 3477 tree type_domain; 3478 tree min_val = size_zero_node; 3479 tree newsub 3480 = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL); 3481 if (newsub) 3482 sub = newsub; 3483 else 3484 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); 3485 type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); 3486 if (type_domain && TYPE_MIN_VALUE (type_domain)) 3487 min_val = TYPE_MIN_VALUE (type_domain); 3488 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, 3489 NULL_TREE); 3490 } 3491 3492 return NULL_TREE; 3493 } 3494 3495 static tree 3496 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, 3497 bool lval, 3498 bool *non_constant_p, bool *overflow_p) 3499 { 3500 tree orig_op0 = TREE_OPERAND (t, 0); 3501 bool empty_base = false; 3502 3503 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second 3504 operand is an integer-zero. Otherwise reject the MEM_REF for now. */ 3505 3506 if (TREE_CODE (t) == MEM_REF 3507 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) 3508 { 3509 gcc_assert (ctx->quiet); 3510 *non_constant_p = true; 3511 return t; 3512 } 3513 3514 /* First try to simplify it directly. */ 3515 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0, 3516 &empty_base); 3517 if (!r) 3518 { 3519 /* If that didn't work, evaluate the operand first. */ 3520 tree op0 = cxx_eval_constant_expression (ctx, orig_op0, 3521 /*lval*/false, non_constant_p, 3522 overflow_p); 3523 /* Don't VERIFY_CONSTANT here. */ 3524 if (*non_constant_p) 3525 return t; 3526 3527 if (!lval && integer_zerop (op0)) 3528 { 3529 if (!ctx->quiet) 3530 error ("dereferencing a null pointer"); 3531 *non_constant_p = true; 3532 return t; 3533 } 3534 3535 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0, 3536 &empty_base); 3537 if (r == NULL_TREE) 3538 { 3539 /* We couldn't fold to a constant value. Make sure it's not 3540 something we should have been able to fold. */ 3541 tree sub = op0; 3542 STRIP_NOPS (sub); 3543 if (TREE_CODE (sub) == ADDR_EXPR) 3544 { 3545 gcc_assert (!same_type_ignoring_top_level_qualifiers_p 3546 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); 3547 /* DR 1188 says we don't have to deal with this. */ 3548 if (!ctx->quiet) 3549 error ("accessing value of %qE through a %qT glvalue in a " 3550 "constant expression", build_fold_indirect_ref (sub), 3551 TREE_TYPE (t)); 3552 *non_constant_p = true; 3553 return t; 3554 } 3555 3556 if (lval && op0 != orig_op0) 3557 return build1 (INDIRECT_REF, TREE_TYPE (t), op0); 3558 if (!lval) 3559 VERIFY_CONSTANT (t); 3560 return t; 3561 } 3562 } 3563 3564 r = cxx_eval_constant_expression (ctx, r, 3565 lval, non_constant_p, overflow_p); 3566 if (*non_constant_p) 3567 return t; 3568 3569 /* If we're pulling out the value of an empty base, just return an empty 3570 CONSTRUCTOR. */ 3571 if (empty_base && !lval) 3572 { 3573 r = build_constructor (TREE_TYPE (t), NULL); 3574 TREE_CONSTANT (r) = true; 3575 } 3576 3577 return r; 3578 } 3579 3580 /* Complain about R, a VAR_DECL, not being usable in a constant expression. 3581 Shared between potential_constant_expression and 3582 cxx_eval_constant_expression. */ 3583 3584 static void 3585 non_const_var_error (tree r) 3586 { 3587 tree type = TREE_TYPE (r); 3588 error ("the value of %qD is not usable in a constant " 3589 "expression", r); 3590 /* Avoid error cascade. */ 3591 if (DECL_INITIAL (r) == error_mark_node) 3592 return; 3593 if (DECL_DECLARED_CONSTEXPR_P (r)) 3594 inform (DECL_SOURCE_LOCATION (r), 3595 "%qD used in its own initializer", r); 3596 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 3597 { 3598 if (!CP_TYPE_CONST_P (type)) 3599 inform (DECL_SOURCE_LOCATION (r), 3600 "%q#D is not const", r); 3601 else if (CP_TYPE_VOLATILE_P (type)) 3602 inform (DECL_SOURCE_LOCATION (r), 3603 "%q#D is volatile", r); 3604 else if (!DECL_INITIAL (r) 3605 || !TREE_CONSTANT (DECL_INITIAL (r)) 3606 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) 3607 inform (DECL_SOURCE_LOCATION (r), 3608 "%qD was not initialized with a constant " 3609 "expression", r); 3610 else 3611 gcc_unreachable (); 3612 } 3613 else if (TYPE_REF_P (type)) 3614 inform (DECL_SOURCE_LOCATION (r), 3615 "%qD was not initialized with a constant " 3616 "expression", r); 3617 else 3618 { 3619 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) 3620 inform (DECL_SOURCE_LOCATION (r), 3621 "%qD was not declared %<constexpr%>", r); 3622 else 3623 inform (DECL_SOURCE_LOCATION (r), 3624 "%qD does not have integral or enumeration type", 3625 r); 3626 } 3627 } 3628 3629 /* Subroutine of cxx_eval_constant_expression. 3630 Like cxx_eval_unary_expression, except for trinary expressions. */ 3631 3632 static tree 3633 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, 3634 bool lval, 3635 bool *non_constant_p, bool *overflow_p) 3636 { 3637 int i; 3638 tree args[3]; 3639 tree val; 3640 3641 for (i = 0; i < 3; i++) 3642 { 3643 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), 3644 lval, 3645 non_constant_p, overflow_p); 3646 VERIFY_CONSTANT (args[i]); 3647 } 3648 3649 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), 3650 args[0], args[1], args[2]); 3651 if (val == NULL_TREE) 3652 return t; 3653 VERIFY_CONSTANT (val); 3654 return val; 3655 } 3656 3657 /* True if T was declared in a function declared to be constexpr, and 3658 therefore potentially constant in C++14. */ 3659 3660 bool 3661 var_in_constexpr_fn (tree t) 3662 { 3663 tree ctx = DECL_CONTEXT (t); 3664 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL 3665 && DECL_DECLARED_CONSTEXPR_P (ctx)); 3666 } 3667 3668 /* True if T was declared in a function that might be constexpr: either a 3669 function that was declared constexpr, or a C++17 lambda op(). */ 3670 3671 bool 3672 var_in_maybe_constexpr_fn (tree t) 3673 { 3674 if (cxx_dialect >= cxx17 3675 && DECL_FUNCTION_SCOPE_P (t) 3676 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t))) 3677 return true; 3678 return var_in_constexpr_fn (t); 3679 } 3680 3681 /* We're assigning INIT to TARGET. In do_build_copy_constructor and 3682 build_over_call we implement trivial copy of a class with tail padding using 3683 assignment of character arrays, which is valid in normal code, but not in 3684 constexpr evaluation. We don't need to worry about clobbering tail padding 3685 in constexpr evaluation, so strip the type punning. */ 3686 3687 static void 3688 maybe_simplify_trivial_copy (tree &target, tree &init) 3689 { 3690 if (TREE_CODE (target) == MEM_REF 3691 && TREE_CODE (init) == MEM_REF 3692 && TREE_TYPE (target) == TREE_TYPE (init) 3693 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE 3694 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) 3695 { 3696 target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); 3697 init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); 3698 } 3699 } 3700 3701 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ 3702 3703 static tree 3704 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, 3705 bool lval, 3706 bool *non_constant_p, bool *overflow_p) 3707 { 3708 constexpr_ctx new_ctx = *ctx; 3709 3710 tree init = TREE_OPERAND (t, 1); 3711 if (TREE_CLOBBER_P (init)) 3712 /* Just ignore clobbers. */ 3713 return void_node; 3714 3715 /* First we figure out where we're storing to. */ 3716 tree target = TREE_OPERAND (t, 0); 3717 3718 maybe_simplify_trivial_copy (target, init); 3719 3720 tree type = TREE_TYPE (target); 3721 bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; 3722 if (preeval) 3723 { 3724 /* Evaluate the value to be stored without knowing what object it will be 3725 stored in, so that any side-effects happen first. */ 3726 if (!SCALAR_TYPE_P (type)) 3727 new_ctx.ctor = new_ctx.object = NULL_TREE; 3728 init = cxx_eval_constant_expression (&new_ctx, init, false, 3729 non_constant_p, overflow_p); 3730 if (*non_constant_p) 3731 return t; 3732 } 3733 target = cxx_eval_constant_expression (ctx, target, 3734 true, 3735 non_constant_p, overflow_p); 3736 if (*non_constant_p) 3737 return t; 3738 3739 /* cxx_eval_array_reference for lval = true allows references one past 3740 end of array, because it does not know if it is just taking address 3741 (which is valid), or actual dereference. Here we know it is 3742 a dereference, so diagnose it here. */ 3743 for (tree probe = target; probe; ) 3744 { 3745 switch (TREE_CODE (probe)) 3746 { 3747 case ARRAY_REF: 3748 tree nelts, ary; 3749 ary = TREE_OPERAND (probe, 0); 3750 nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), 3751 non_constant_p, overflow_p); 3752 VERIFY_CONSTANT (nelts); 3753 gcc_assert (TREE_CODE (nelts) == INTEGER_CST 3754 && TREE_CODE (TREE_OPERAND (probe, 1)) == INTEGER_CST); 3755 if (wi::to_widest (TREE_OPERAND (probe, 1)) == wi::to_widest (nelts)) 3756 { 3757 diag_array_subscript (ctx, ary, TREE_OPERAND (probe, 1)); 3758 *non_constant_p = true; 3759 return t; 3760 } 3761 /* FALLTHRU */ 3762 3763 case BIT_FIELD_REF: 3764 case COMPONENT_REF: 3765 probe = TREE_OPERAND (probe, 0); 3766 continue; 3767 3768 default: 3769 probe = NULL_TREE; 3770 continue; 3771 } 3772 } 3773 3774 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type)) 3775 { 3776 /* For initialization of an empty base, the original target will be 3777 *(base*)this, which the above evaluation resolves to the object 3778 argument, which has the derived type rather than the base type. In 3779 this situation, just evaluate the initializer and return, since 3780 there's no actual data to store. */ 3781 gcc_assert (is_empty_class (type)); 3782 return cxx_eval_constant_expression (ctx, init, false, 3783 non_constant_p, overflow_p); 3784 } 3785 3786 /* And then find the underlying variable. */ 3787 vec<tree,va_gc> *refs = make_tree_vector(); 3788 tree object = NULL_TREE; 3789 for (tree probe = target; object == NULL_TREE; ) 3790 { 3791 switch (TREE_CODE (probe)) 3792 { 3793 case BIT_FIELD_REF: 3794 case COMPONENT_REF: 3795 case ARRAY_REF: 3796 vec_safe_push (refs, TREE_OPERAND (probe, 1)); 3797 vec_safe_push (refs, TREE_TYPE (probe)); 3798 probe = TREE_OPERAND (probe, 0); 3799 break; 3800 3801 default: 3802 object = probe; 3803 } 3804 } 3805 3806 /* And then find/build up our initializer for the path to the subobject 3807 we're initializing. */ 3808 tree *valp; 3809 if (object == ctx->object && VAR_P (object) 3810 && DECL_NAME (object) && ctx->call == NULL) 3811 /* The variable we're building up an aggregate initializer for is outside 3812 the constant-expression, so don't evaluate the store. We check 3813 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */ 3814 valp = NULL; 3815 else if (DECL_P (object)) 3816 valp = ctx->values->get (object); 3817 else 3818 valp = NULL; 3819 if (!valp) 3820 { 3821 /* A constant-expression cannot modify objects from outside the 3822 constant-expression. */ 3823 if (!ctx->quiet) 3824 error ("modification of %qE is not a constant expression", object); 3825 *non_constant_p = true; 3826 return t; 3827 } 3828 type = TREE_TYPE (object); 3829 bool no_zero_init = true; 3830 3831 vec<tree,va_gc> *ctors = make_tree_vector (); 3832 while (!refs->is_empty()) 3833 { 3834 if (*valp == NULL_TREE) 3835 { 3836 *valp = build_constructor (type, NULL); 3837 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; 3838 } 3839 else if (TREE_CODE (*valp) == STRING_CST) 3840 { 3841 /* An array was initialized with a string constant, and now 3842 we're writing into one of its elements. Explode the 3843 single initialization into a set of element 3844 initializations. */ 3845 gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 3846 3847 tree string = *valp; 3848 tree elt_type = TREE_TYPE (type); 3849 unsigned chars_per_elt = (TYPE_PRECISION (elt_type) 3850 / TYPE_PRECISION (char_type_node)); 3851 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; 3852 tree ary_ctor = build_constructor (type, NULL); 3853 3854 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); 3855 for (unsigned ix = 0; ix != num_elts; ix++) 3856 { 3857 constructor_elt elt = 3858 { 3859 build_int_cst (size_type_node, ix), 3860 extract_string_elt (string, chars_per_elt, ix) 3861 }; 3862 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); 3863 } 3864 3865 *valp = ary_ctor; 3866 } 3867 3868 /* If the value of object is already zero-initialized, any new ctors for 3869 subobjects will also be zero-initialized. */ 3870 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); 3871 3872 vec_safe_push (ctors, *valp); 3873 3874 enum tree_code code = TREE_CODE (type); 3875 type = refs->pop(); 3876 tree index = refs->pop(); 3877 3878 constructor_elt *cep = NULL; 3879 if (code == ARRAY_TYPE) 3880 { 3881 HOST_WIDE_INT i 3882 = find_array_ctor_elt (*valp, index, /*insert*/true); 3883 gcc_assert (i >= 0); 3884 cep = CONSTRUCTOR_ELT (*valp, i); 3885 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR); 3886 } 3887 else 3888 { 3889 gcc_assert (TREE_CODE (index) == FIELD_DECL); 3890 3891 /* We must keep the CONSTRUCTOR's ELTS in FIELD order. 3892 Usually we meet initializers in that order, but it is 3893 possible for base types to be placed not in program 3894 order. */ 3895 tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); 3896 unsigned HOST_WIDE_INT idx; 3897 3898 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) 3899 && CONSTRUCTOR_ELT (*valp, 0)->index != index) 3900 { 3901 if (cxx_dialect < cxx2a) 3902 { 3903 if (!ctx->quiet) 3904 error_at (cp_expr_loc_or_loc (t, input_location), 3905 "change of the active member of a union " 3906 "from %qD to %qD", 3907 CONSTRUCTOR_ELT (*valp, 0)->index, 3908 index); 3909 *non_constant_p = true; 3910 } 3911 /* Changing active member. */ 3912 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0); 3913 no_zero_init = true; 3914 } 3915 3916 for (idx = 0; 3917 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep); 3918 idx++, fields = DECL_CHAIN (fields)) 3919 { 3920 if (index == cep->index) 3921 goto found; 3922 3923 /* The field we're initializing must be on the field 3924 list. Look to see if it is present before the 3925 field the current ELT initializes. */ 3926 for (; fields != cep->index; fields = DECL_CHAIN (fields)) 3927 if (index == fields) 3928 goto insert; 3929 } 3930 3931 /* We fell off the end of the CONSTRUCTOR, so insert a new 3932 entry at the end. */ 3933 insert: 3934 { 3935 constructor_elt ce = { index, NULL_TREE }; 3936 3937 vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce); 3938 cep = CONSTRUCTOR_ELT (*valp, idx); 3939 } 3940 found:; 3941 } 3942 valp = &cep->value; 3943 } 3944 release_tree_vector (refs); 3945 3946 if (!preeval) 3947 { 3948 /* Create a new CONSTRUCTOR in case evaluation of the initializer 3949 wants to modify it. */ 3950 if (*valp == NULL_TREE) 3951 { 3952 *valp = build_constructor (type, NULL); 3953 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; 3954 } 3955 new_ctx.ctor = *valp; 3956 new_ctx.object = target; 3957 init = cxx_eval_constant_expression (&new_ctx, init, false, 3958 non_constant_p, overflow_p); 3959 if (target == object) 3960 /* The hash table might have moved since the get earlier. */ 3961 valp = ctx->values->get (object); 3962 } 3963 3964 /* Don't share a CONSTRUCTOR that might be changed later. */ 3965 init = unshare_constructor (init); 3966 3967 if (*valp && TREE_CODE (*valp) == CONSTRUCTOR 3968 && TREE_CODE (init) == CONSTRUCTOR) 3969 { 3970 /* An outer ctx->ctor might be pointing to *valp, so replace 3971 its contents. */ 3972 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); 3973 TREE_CONSTANT (*valp) = TREE_CONSTANT (init); 3974 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); 3975 CONSTRUCTOR_NO_CLEARING (*valp) 3976 = CONSTRUCTOR_NO_CLEARING (init); 3977 } 3978 else 3979 *valp = init; 3980 3981 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing 3982 CONSTRUCTORs, if any. */ 3983 tree elt; 3984 unsigned i; 3985 bool c = TREE_CONSTANT (init); 3986 bool s = TREE_SIDE_EFFECTS (init); 3987 if (!c || s) 3988 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt) 3989 { 3990 if (!c) 3991 TREE_CONSTANT (elt) = false; 3992 if (s) 3993 TREE_SIDE_EFFECTS (elt) = true; 3994 } 3995 release_tree_vector (ctors); 3996 3997 if (*non_constant_p) 3998 return t; 3999 else if (lval) 4000 return target; 4001 else 4002 return init; 4003 } 4004 4005 /* Evaluate a ++ or -- expression. */ 4006 4007 static tree 4008 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, 4009 bool lval, 4010 bool *non_constant_p, bool *overflow_p) 4011 { 4012 enum tree_code code = TREE_CODE (t); 4013 tree type = TREE_TYPE (t); 4014 tree op = TREE_OPERAND (t, 0); 4015 tree offset = TREE_OPERAND (t, 1); 4016 gcc_assert (TREE_CONSTANT (offset)); 4017 4018 /* OFFSET is constant, but perhaps not constant enough. We need to 4019 e.g. bash FLOAT_EXPRs to REAL_CSTs. */ 4020 offset = fold_simple (offset); 4021 4022 /* The operand as an lvalue. */ 4023 op = cxx_eval_constant_expression (ctx, op, true, 4024 non_constant_p, overflow_p); 4025 4026 /* The operand as an rvalue. */ 4027 tree val 4028 = cxx_eval_constant_expression (ctx, op, false, 4029 non_constant_p, overflow_p); 4030 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to 4031 a local array in a constexpr function. */ 4032 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); 4033 if (!ptr) 4034 VERIFY_CONSTANT (val); 4035 4036 /* The modified value. */ 4037 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); 4038 tree mod; 4039 if (INDIRECT_TYPE_P (type)) 4040 { 4041 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ 4042 offset = convert_to_ptrofftype (offset); 4043 if (!inc) 4044 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); 4045 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); 4046 } 4047 else 4048 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); 4049 if (!ptr) 4050 VERIFY_CONSTANT (mod); 4051 4052 /* Storing the modified value. */ 4053 tree store = build2 (MODIFY_EXPR, type, op, mod); 4054 cxx_eval_constant_expression (ctx, store, 4055 true, non_constant_p, overflow_p); 4056 4057 /* And the value of the expression. */ 4058 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) 4059 { 4060 /* Prefix ops are lvalues. */ 4061 if (lval) 4062 return op; 4063 else 4064 /* But we optimize when the caller wants an rvalue. */ 4065 return mod; 4066 } 4067 else 4068 /* Postfix ops are rvalues. */ 4069 return val; 4070 } 4071 4072 /* Predicates for the meaning of *jump_target. */ 4073 4074 static bool 4075 returns (tree *jump_target) 4076 { 4077 return *jump_target 4078 && (TREE_CODE (*jump_target) == RETURN_EXPR 4079 || (TREE_CODE (*jump_target) == LABEL_DECL 4080 && LABEL_DECL_CDTOR (*jump_target))); 4081 } 4082 4083 static bool 4084 breaks (tree *jump_target) 4085 { 4086 return *jump_target 4087 && ((TREE_CODE (*jump_target) == LABEL_DECL 4088 && LABEL_DECL_BREAK (*jump_target)) 4089 || TREE_CODE (*jump_target) == BREAK_STMT 4090 || TREE_CODE (*jump_target) == EXIT_EXPR); 4091 } 4092 4093 static bool 4094 continues (tree *jump_target) 4095 { 4096 return *jump_target 4097 && ((TREE_CODE (*jump_target) == LABEL_DECL 4098 && LABEL_DECL_CONTINUE (*jump_target)) 4099 || TREE_CODE (*jump_target) == CONTINUE_STMT); 4100 4101 } 4102 4103 static bool 4104 switches (tree *jump_target) 4105 { 4106 return *jump_target 4107 && TREE_CODE (*jump_target) == INTEGER_CST; 4108 } 4109 4110 /* Subroutine of cxx_eval_statement_list. Determine whether the statement 4111 STMT matches *jump_target. If we're looking for a case label and we see 4112 the default label, note it in ctx->css_state. */ 4113 4114 static bool 4115 label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) 4116 { 4117 switch (TREE_CODE (*jump_target)) 4118 { 4119 case LABEL_DECL: 4120 if (TREE_CODE (stmt) == LABEL_EXPR 4121 && LABEL_EXPR_LABEL (stmt) == *jump_target) 4122 return true; 4123 break; 4124 4125 case INTEGER_CST: 4126 if (TREE_CODE (stmt) == CASE_LABEL_EXPR) 4127 { 4128 gcc_assert (ctx->css_state != NULL); 4129 if (!CASE_LOW (stmt)) 4130 { 4131 /* default: should appear just once in a SWITCH_EXPR 4132 body (excluding nested SWITCH_EXPR). */ 4133 gcc_assert (*ctx->css_state != css_default_seen); 4134 /* When evaluating SWITCH_EXPR body for the second time, 4135 return true for the default: label. */ 4136 if (*ctx->css_state == css_default_processing) 4137 return true; 4138 *ctx->css_state = css_default_seen; 4139 } 4140 else if (CASE_HIGH (stmt)) 4141 { 4142 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) 4143 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) 4144 return true; 4145 } 4146 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) 4147 return true; 4148 } 4149 break; 4150 4151 case BREAK_STMT: 4152 case CONTINUE_STMT: 4153 /* These two are handled directly in cxx_eval_loop_expr by testing 4154 breaks (jump_target) or continues (jump_target). */ 4155 break; 4156 4157 default: 4158 gcc_unreachable (); 4159 } 4160 return false; 4161 } 4162 4163 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump 4164 semantics, for switch, break, continue, and return. */ 4165 4166 static tree 4167 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, 4168 bool *non_constant_p, bool *overflow_p, 4169 tree *jump_target) 4170 { 4171 tree_stmt_iterator i; 4172 tree local_target; 4173 /* In a statement-expression we want to return the last value. 4174 For empty statement expression return void_node. */ 4175 tree r = void_node; 4176 if (!jump_target) 4177 { 4178 local_target = NULL_TREE; 4179 jump_target = &local_target; 4180 } 4181 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) 4182 { 4183 tree stmt = tsi_stmt (i); 4184 /* We've found a continue, so skip everything until we reach 4185 the label its jumping to. */ 4186 if (continues (jump_target)) 4187 { 4188 if (label_matches (ctx, jump_target, stmt)) 4189 /* Found it. */ 4190 *jump_target = NULL_TREE; 4191 else 4192 continue; 4193 } 4194 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) 4195 continue; 4196 r = cxx_eval_constant_expression (ctx, stmt, false, 4197 non_constant_p, overflow_p, 4198 jump_target); 4199 if (*non_constant_p) 4200 break; 4201 if (returns (jump_target) || breaks (jump_target)) 4202 break; 4203 } 4204 if (*jump_target && jump_target == &local_target) 4205 { 4206 /* We aren't communicating the jump to our caller, so give up. We don't 4207 need to support evaluation of jumps out of statement-exprs. */ 4208 if (!ctx->quiet) 4209 error_at (cp_expr_loc_or_loc (r, input_location), 4210 "statement is not a constant expression"); 4211 *non_constant_p = true; 4212 } 4213 return r; 4214 } 4215 4216 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return 4217 semantics; continue semantics are covered by cxx_eval_statement_list. */ 4218 4219 static tree 4220 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, 4221 bool *non_constant_p, bool *overflow_p, 4222 tree *jump_target) 4223 { 4224 constexpr_ctx new_ctx = *ctx; 4225 4226 tree body, cond = NULL_TREE, expr = NULL_TREE; 4227 int count = 0; 4228 switch (TREE_CODE (t)) 4229 { 4230 case LOOP_EXPR: 4231 body = LOOP_EXPR_BODY (t); 4232 break; 4233 case DO_STMT: 4234 body = DO_BODY (t); 4235 cond = DO_COND (t); 4236 break; 4237 case WHILE_STMT: 4238 body = WHILE_BODY (t); 4239 cond = WHILE_COND (t); 4240 count = -1; 4241 break; 4242 case FOR_STMT: 4243 if (FOR_INIT_STMT (t)) 4244 cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false, 4245 non_constant_p, overflow_p, jump_target); 4246 if (*non_constant_p) 4247 return NULL_TREE; 4248 body = FOR_BODY (t); 4249 cond = FOR_COND (t); 4250 expr = FOR_EXPR (t); 4251 count = -1; 4252 break; 4253 default: 4254 gcc_unreachable (); 4255 } 4256 auto_vec<tree, 10> save_exprs; 4257 new_ctx.save_exprs = &save_exprs; 4258 do 4259 { 4260 if (count != -1) 4261 { 4262 if (body) 4263 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false, 4264 non_constant_p, overflow_p, 4265 jump_target); 4266 if (breaks (jump_target)) 4267 { 4268 *jump_target = NULL_TREE; 4269 break; 4270 } 4271 4272 if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) 4273 *jump_target = NULL_TREE; 4274 4275 if (expr) 4276 cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false, 4277 non_constant_p, overflow_p, 4278 jump_target); 4279 } 4280 4281 if (cond) 4282 { 4283 tree res 4284 = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false, 4285 non_constant_p, overflow_p, 4286 jump_target); 4287 if (res) 4288 { 4289 if (verify_constant (res, ctx->quiet, non_constant_p, 4290 overflow_p)) 4291 break; 4292 if (integer_zerop (res)) 4293 break; 4294 } 4295 else 4296 gcc_assert (*jump_target); 4297 } 4298 4299 /* Forget saved values of SAVE_EXPRs. */ 4300 unsigned int i; 4301 tree save_expr; 4302 FOR_EACH_VEC_ELT (save_exprs, i, save_expr) 4303 new_ctx.values->remove (save_expr); 4304 save_exprs.truncate (0); 4305 4306 if (++count >= constexpr_loop_limit) 4307 { 4308 if (!ctx->quiet) 4309 error_at (cp_expr_loc_or_loc (t, input_location), 4310 "%<constexpr%> loop iteration count exceeds limit of %d " 4311 "(use %<-fconstexpr-loop-limit=%> to increase the limit)", 4312 constexpr_loop_limit); 4313 *non_constant_p = true; 4314 break; 4315 } 4316 } 4317 while (!returns (jump_target) 4318 && !breaks (jump_target) 4319 && !continues (jump_target) 4320 && (!switches (jump_target) || count == 0) 4321 && !*non_constant_p); 4322 4323 /* Forget saved values of SAVE_EXPRs. */ 4324 unsigned int i; 4325 tree save_expr; 4326 FOR_EACH_VEC_ELT (save_exprs, i, save_expr) 4327 new_ctx.values->remove (save_expr); 4328 4329 return NULL_TREE; 4330 } 4331 4332 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump 4333 semantics. */ 4334 4335 static tree 4336 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, 4337 bool *non_constant_p, bool *overflow_p, 4338 tree *jump_target) 4339 { 4340 tree cond 4341 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); 4342 cond = cxx_eval_constant_expression (ctx, cond, false, 4343 non_constant_p, overflow_p); 4344 VERIFY_CONSTANT (cond); 4345 *jump_target = cond; 4346 4347 tree body 4348 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); 4349 constexpr_ctx new_ctx = *ctx; 4350 constexpr_switch_state css = css_default_not_seen; 4351 new_ctx.css_state = &css; 4352 cxx_eval_constant_expression (&new_ctx, body, false, 4353 non_constant_p, overflow_p, jump_target); 4354 if (switches (jump_target) && css == css_default_seen) 4355 { 4356 /* If the SWITCH_EXPR body has default: label, process it once again, 4357 this time instructing label_matches to return true for default: 4358 label on switches (jump_target). */ 4359 css = css_default_processing; 4360 cxx_eval_constant_expression (&new_ctx, body, false, 4361 non_constant_p, overflow_p, jump_target); 4362 } 4363 if (breaks (jump_target) || switches (jump_target)) 4364 *jump_target = NULL_TREE; 4365 return NULL_TREE; 4366 } 4367 4368 /* Find the object of TYPE under initialization in CTX. */ 4369 4370 static tree 4371 lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) 4372 { 4373 if (!ctx) 4374 return NULL_TREE; 4375 4376 /* We could use ctx->object unconditionally, but using ctx->ctor when we 4377 can is a minor optimization. */ 4378 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) 4379 return ctx->ctor; 4380 4381 if (!ctx->object) 4382 return NULL_TREE; 4383 4384 /* Since an object cannot have a field of its own type, we can search outward 4385 from ctx->object to find the unique containing object of TYPE. */ 4386 tree ob = ctx->object; 4387 while (ob) 4388 { 4389 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) 4390 break; 4391 if (handled_component_p (ob)) 4392 ob = TREE_OPERAND (ob, 0); 4393 else 4394 ob = NULL_TREE; 4395 } 4396 4397 return ob; 4398 } 4399 4400 /* Attempt to reduce the expression T to a constant value. 4401 On failure, issue diagnostic and return error_mark_node. */ 4402 /* FIXME unify with c_fully_fold */ 4403 /* FIXME overflow_p is too global */ 4404 4405 static tree 4406 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, 4407 bool lval, 4408 bool *non_constant_p, bool *overflow_p, 4409 tree *jump_target /* = NULL */) 4410 { 4411 constexpr_ctx new_ctx; 4412 tree r = t; 4413 4414 if (jump_target && *jump_target) 4415 { 4416 /* If we are jumping, ignore all statements/expressions except those 4417 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ 4418 switch (TREE_CODE (t)) 4419 { 4420 case BIND_EXPR: 4421 case STATEMENT_LIST: 4422 case LOOP_EXPR: 4423 case COND_EXPR: 4424 case IF_STMT: 4425 case DO_STMT: 4426 case WHILE_STMT: 4427 case FOR_STMT: 4428 break; 4429 case LABEL_EXPR: 4430 case CASE_LABEL_EXPR: 4431 if (label_matches (ctx, jump_target, t)) 4432 /* Found it. */ 4433 *jump_target = NULL_TREE; 4434 return NULL_TREE; 4435 default: 4436 return NULL_TREE; 4437 } 4438 } 4439 if (error_operand_p (t)) 4440 { 4441 *non_constant_p = true; 4442 return t; 4443 } 4444 if (CONSTANT_CLASS_P (t)) 4445 { 4446 if (TREE_OVERFLOW (t)) 4447 { 4448 if (!ctx->quiet) 4449 permerror (input_location, "overflow in constant expression"); 4450 if (!flag_permissive || ctx->quiet) 4451 *overflow_p = true; 4452 } 4453 4454 if (TREE_CODE (t) == INTEGER_CST 4455 && TYPE_PTR_P (TREE_TYPE (t)) 4456 && !integer_zerop (t)) 4457 { 4458 if (!ctx->quiet) 4459 error ("value %qE of type %qT is not a constant expression", 4460 t, TREE_TYPE (t)); 4461 *non_constant_p = true; 4462 } 4463 4464 return t; 4465 } 4466 4467 /* Avoid excessively long constexpr evaluations. */ 4468 if (!location_wrapper_p (t) 4469 && ++*ctx->constexpr_ops_count >= constexpr_ops_limit) 4470 { 4471 if (!ctx->quiet) 4472 error_at (cp_expr_loc_or_loc (t, input_location), 4473 "%<constexpr%> evaluation operation count exceeds limit of " 4474 "%wd (use -fconstexpr-ops-limit= to increase the limit)", 4475 constexpr_ops_limit); 4476 *ctx->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); 4477 *non_constant_p = true; 4478 return t; 4479 } 4480 4481 tree_code tcode = TREE_CODE (t); 4482 switch (tcode) 4483 { 4484 case RESULT_DECL: 4485 if (lval) 4486 return t; 4487 /* We ask for an rvalue for the RESULT_DECL when indirecting 4488 through an invisible reference, or in named return value 4489 optimization. */ 4490 if (tree *p = ctx->values->get (t)) 4491 return *p; 4492 else 4493 { 4494 if (!ctx->quiet) 4495 error ("%qE is not a constant expression", t); 4496 *non_constant_p = true; 4497 } 4498 break; 4499 4500 case VAR_DECL: 4501 if (DECL_HAS_VALUE_EXPR_P (t)) 4502 { 4503 if (is_normal_capture_proxy (t) 4504 && current_function_decl == DECL_CONTEXT (t)) 4505 { 4506 /* Function parms aren't constexpr within the function 4507 definition, so don't try to look at the closure. But if the 4508 captured variable is constant, try to evaluate it directly. */ 4509 r = DECL_CAPTURED_VARIABLE (t); 4510 tree type = TREE_TYPE (t); 4511 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) 4512 { 4513 /* Adjust r to match the reference-ness of t. */ 4514 if (TYPE_REF_P (type)) 4515 r = build_address (r); 4516 else 4517 r = convert_from_reference (r); 4518 } 4519 } 4520 else 4521 r = DECL_VALUE_EXPR (t); 4522 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, 4523 overflow_p); 4524 } 4525 /* fall through */ 4526 case CONST_DECL: 4527 /* We used to not check lval for CONST_DECL, but darwin.c uses 4528 CONST_DECL for aggregate constants. */ 4529 if (lval) 4530 return t; 4531 if (COMPLETE_TYPE_P (TREE_TYPE (t)) 4532 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 4533 { 4534 /* If the class is empty, we aren't actually loading anything. */ 4535 r = build_constructor (TREE_TYPE (t), NULL); 4536 TREE_CONSTANT (r) = true; 4537 } 4538 else if (ctx->strict) 4539 r = decl_really_constant_value (t); 4540 else 4541 r = decl_constant_value (t); 4542 if (TREE_CODE (r) == TARGET_EXPR 4543 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) 4544 r = TARGET_EXPR_INITIAL (r); 4545 if (VAR_P (r)) 4546 if (tree *p = ctx->values->get (r)) 4547 if (*p != NULL_TREE) 4548 r = *p; 4549 if (DECL_P (r)) 4550 { 4551 if (!ctx->quiet) 4552 non_const_var_error (r); 4553 *non_constant_p = true; 4554 } 4555 break; 4556 4557 case DEBUG_BEGIN_STMT: 4558 /* ??? It might be nice to retain this information somehow, so 4559 as to be able to step into a constexpr function call. */ 4560 /* Fall through. */ 4561 4562 case FUNCTION_DECL: 4563 case TEMPLATE_DECL: 4564 case LABEL_DECL: 4565 case LABEL_EXPR: 4566 case CASE_LABEL_EXPR: 4567 case PREDICT_EXPR: 4568 return t; 4569 4570 case PARM_DECL: 4571 if (lval && !TYPE_REF_P (TREE_TYPE (t))) 4572 /* glvalue use. */; 4573 else if (tree *p = ctx->values->get (r)) 4574 r = *p; 4575 else if (lval) 4576 /* Defer in case this is only used for its type. */; 4577 else if (TYPE_REF_P (TREE_TYPE (t))) 4578 /* Defer, there's no lvalue->rvalue conversion. */; 4579 else if (COMPLETE_TYPE_P (TREE_TYPE (t)) 4580 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 4581 { 4582 /* If the class is empty, we aren't actually loading anything. */ 4583 r = build_constructor (TREE_TYPE (t), NULL); 4584 TREE_CONSTANT (r) = true; 4585 } 4586 else 4587 { 4588 if (!ctx->quiet) 4589 error ("%qE is not a constant expression", t); 4590 *non_constant_p = true; 4591 } 4592 break; 4593 4594 case CALL_EXPR: 4595 case AGGR_INIT_EXPR: 4596 r = cxx_eval_call_expression (ctx, t, lval, 4597 non_constant_p, overflow_p); 4598 break; 4599 4600 case DECL_EXPR: 4601 { 4602 r = DECL_EXPR_DECL (t); 4603 if (TREE_CODE (r) == USING_DECL) 4604 { 4605 r = void_node; 4606 break; 4607 } 4608 if (AGGREGATE_TYPE_P (TREE_TYPE (r)) 4609 || VECTOR_TYPE_P (TREE_TYPE (r))) 4610 { 4611 new_ctx = *ctx; 4612 new_ctx.object = r; 4613 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); 4614 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; 4615 new_ctx.values->put (r, new_ctx.ctor); 4616 ctx = &new_ctx; 4617 } 4618 4619 if (tree init = DECL_INITIAL (r)) 4620 { 4621 init = cxx_eval_constant_expression (ctx, init, 4622 false, 4623 non_constant_p, overflow_p); 4624 /* Don't share a CONSTRUCTOR that might be changed. */ 4625 init = unshare_constructor (init); 4626 ctx->values->put (r, init); 4627 } 4628 else if (ctx == &new_ctx) 4629 /* We gave it a CONSTRUCTOR above. */; 4630 else 4631 ctx->values->put (r, NULL_TREE); 4632 } 4633 break; 4634 4635 case TARGET_EXPR: 4636 if (!literal_type_p (TREE_TYPE (t))) 4637 { 4638 if (!ctx->quiet) 4639 { 4640 auto_diagnostic_group d; 4641 error ("temporary of non-literal type %qT in a " 4642 "constant expression", TREE_TYPE (t)); 4643 explain_non_literal_class (TREE_TYPE (t)); 4644 } 4645 *non_constant_p = true; 4646 break; 4647 } 4648 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t)))) 4649 { 4650 /* We're being expanded without an explicit target, so start 4651 initializing a new object; expansion with an explicit target 4652 strips the TARGET_EXPR before we get here. */ 4653 new_ctx = *ctx; 4654 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL); 4655 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; 4656 new_ctx.object = TARGET_EXPR_SLOT (t); 4657 ctx->values->put (new_ctx.object, new_ctx.ctor); 4658 ctx = &new_ctx; 4659 } 4660 /* Pass false for 'lval' because this indicates 4661 initialization of a temporary. */ 4662 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 4663 false, 4664 non_constant_p, overflow_p); 4665 if (!*non_constant_p) 4666 /* Adjust the type of the result to the type of the temporary. */ 4667 r = adjust_temp_type (TREE_TYPE (t), r); 4668 if (lval) 4669 { 4670 tree slot = TARGET_EXPR_SLOT (t); 4671 r = unshare_constructor (r); 4672 ctx->values->put (slot, r); 4673 return slot; 4674 } 4675 break; 4676 4677 case INIT_EXPR: 4678 case MODIFY_EXPR: 4679 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); 4680 r = cxx_eval_store_expression (ctx, t, lval, 4681 non_constant_p, overflow_p); 4682 break; 4683 4684 case SCOPE_REF: 4685 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), 4686 lval, 4687 non_constant_p, overflow_p); 4688 break; 4689 4690 case RETURN_EXPR: 4691 if (TREE_OPERAND (t, 0) != NULL_TREE) 4692 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 4693 lval, 4694 non_constant_p, overflow_p); 4695 /* FALLTHRU */ 4696 case BREAK_STMT: 4697 case CONTINUE_STMT: 4698 if (jump_target) 4699 *jump_target = t; 4700 else 4701 { 4702 /* Can happen with ({ return true; }) && false; passed to 4703 maybe_constant_value. There is nothing to jump over in this 4704 case, and the bug will be diagnosed later. */ 4705 gcc_assert (ctx->quiet); 4706 *non_constant_p = true; 4707 } 4708 break; 4709 4710 case SAVE_EXPR: 4711 /* Avoid evaluating a SAVE_EXPR more than once. */ 4712 if (tree *p = ctx->values->get (t)) 4713 r = *p; 4714 else 4715 { 4716 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, 4717 non_constant_p, overflow_p); 4718 ctx->values->put (t, r); 4719 if (ctx->save_exprs) 4720 ctx->save_exprs->safe_push (t); 4721 } 4722 break; 4723 4724 case NON_LVALUE_EXPR: 4725 case TRY_CATCH_EXPR: 4726 case TRY_BLOCK: 4727 case CLEANUP_POINT_EXPR: 4728 case MUST_NOT_THROW_EXPR: 4729 case EXPR_STMT: 4730 case EH_SPEC_BLOCK: 4731 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 4732 lval, 4733 non_constant_p, overflow_p, 4734 jump_target); 4735 break; 4736 4737 case TRY_FINALLY_EXPR: 4738 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, 4739 non_constant_p, overflow_p, 4740 jump_target); 4741 if (!*non_constant_p) 4742 /* Also evaluate the cleanup. */ 4743 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true, 4744 non_constant_p, overflow_p, 4745 jump_target); 4746 break; 4747 4748 case CLEANUP_STMT: 4749 r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, 4750 non_constant_p, overflow_p, 4751 jump_target); 4752 if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) 4753 /* Also evaluate the cleanup. */ 4754 cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true, 4755 non_constant_p, overflow_p, 4756 jump_target); 4757 break; 4758 4759 /* These differ from cxx_eval_unary_expression in that this doesn't 4760 check for a constant operand or result; an address can be 4761 constant without its operand being, and vice versa. */ 4762 case MEM_REF: 4763 case INDIRECT_REF: 4764 r = cxx_eval_indirect_ref (ctx, t, lval, 4765 non_constant_p, overflow_p); 4766 break; 4767 4768 case ADDR_EXPR: 4769 { 4770 tree oldop = TREE_OPERAND (t, 0); 4771 tree op = cxx_eval_constant_expression (ctx, oldop, 4772 /*lval*/true, 4773 non_constant_p, overflow_p); 4774 /* Don't VERIFY_CONSTANT here. */ 4775 if (*non_constant_p) 4776 return t; 4777 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); 4778 /* This function does more aggressive folding than fold itself. */ 4779 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); 4780 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) 4781 return t; 4782 break; 4783 } 4784 4785 case REALPART_EXPR: 4786 case IMAGPART_EXPR: 4787 if (lval) 4788 { 4789 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, 4790 non_constant_p, overflow_p); 4791 if (r == error_mark_node) 4792 ; 4793 else if (r == TREE_OPERAND (t, 0)) 4794 r = t; 4795 else 4796 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); 4797 break; 4798 } 4799 /* FALLTHRU */ 4800 case CONJ_EXPR: 4801 case FIX_TRUNC_EXPR: 4802 case FLOAT_EXPR: 4803 case NEGATE_EXPR: 4804 case ABS_EXPR: 4805 case ABSU_EXPR: 4806 case BIT_NOT_EXPR: 4807 case TRUTH_NOT_EXPR: 4808 case FIXED_CONVERT_EXPR: 4809 r = cxx_eval_unary_expression (ctx, t, lval, 4810 non_constant_p, overflow_p); 4811 break; 4812 4813 case SIZEOF_EXPR: 4814 r = fold_sizeof_expr (t); 4815 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, 4816 which could lead to an infinite recursion. */ 4817 if (TREE_CODE (r) != SIZEOF_EXPR) 4818 r = cxx_eval_constant_expression (ctx, r, lval, 4819 non_constant_p, overflow_p, 4820 jump_target); 4821 else 4822 { 4823 *non_constant_p = true; 4824 gcc_assert (ctx->quiet); 4825 } 4826 4827 break; 4828 4829 case COMPOUND_EXPR: 4830 { 4831 /* check_return_expr sometimes wraps a TARGET_EXPR in a 4832 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR 4833 introduced by build_call_a. */ 4834 tree op0 = TREE_OPERAND (t, 0); 4835 tree op1 = TREE_OPERAND (t, 1); 4836 STRIP_NOPS (op1); 4837 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) 4838 || TREE_CODE (op1) == EMPTY_CLASS_EXPR) 4839 r = cxx_eval_constant_expression (ctx, op0, 4840 lval, non_constant_p, overflow_p, 4841 jump_target); 4842 else 4843 { 4844 /* Check that the LHS is constant and then discard it. */ 4845 cxx_eval_constant_expression (ctx, op0, 4846 true, non_constant_p, overflow_p, 4847 jump_target); 4848 if (*non_constant_p) 4849 return t; 4850 op1 = TREE_OPERAND (t, 1); 4851 r = cxx_eval_constant_expression (ctx, op1, 4852 lval, non_constant_p, overflow_p, 4853 jump_target); 4854 } 4855 } 4856 break; 4857 4858 case POINTER_PLUS_EXPR: 4859 case POINTER_DIFF_EXPR: 4860 case PLUS_EXPR: 4861 case MINUS_EXPR: 4862 case MULT_EXPR: 4863 case TRUNC_DIV_EXPR: 4864 case CEIL_DIV_EXPR: 4865 case FLOOR_DIV_EXPR: 4866 case ROUND_DIV_EXPR: 4867 case TRUNC_MOD_EXPR: 4868 case CEIL_MOD_EXPR: 4869 case ROUND_MOD_EXPR: 4870 case RDIV_EXPR: 4871 case EXACT_DIV_EXPR: 4872 case MIN_EXPR: 4873 case MAX_EXPR: 4874 case LSHIFT_EXPR: 4875 case RSHIFT_EXPR: 4876 case LROTATE_EXPR: 4877 case RROTATE_EXPR: 4878 case BIT_IOR_EXPR: 4879 case BIT_XOR_EXPR: 4880 case BIT_AND_EXPR: 4881 case TRUTH_XOR_EXPR: 4882 case LT_EXPR: 4883 case LE_EXPR: 4884 case GT_EXPR: 4885 case GE_EXPR: 4886 case EQ_EXPR: 4887 case NE_EXPR: 4888 case UNORDERED_EXPR: 4889 case ORDERED_EXPR: 4890 case UNLT_EXPR: 4891 case UNLE_EXPR: 4892 case UNGT_EXPR: 4893 case UNGE_EXPR: 4894 case UNEQ_EXPR: 4895 case LTGT_EXPR: 4896 case RANGE_EXPR: 4897 case COMPLEX_EXPR: 4898 r = cxx_eval_binary_expression (ctx, t, lval, 4899 non_constant_p, overflow_p); 4900 break; 4901 4902 /* fold can introduce non-IF versions of these; still treat them as 4903 short-circuiting. */ 4904 case TRUTH_AND_EXPR: 4905 case TRUTH_ANDIF_EXPR: 4906 r = cxx_eval_logical_expression (ctx, t, boolean_false_node, 4907 boolean_true_node, 4908 lval, 4909 non_constant_p, overflow_p); 4910 break; 4911 4912 case TRUTH_OR_EXPR: 4913 case TRUTH_ORIF_EXPR: 4914 r = cxx_eval_logical_expression (ctx, t, boolean_true_node, 4915 boolean_false_node, 4916 lval, 4917 non_constant_p, overflow_p); 4918 break; 4919 4920 case ARRAY_REF: 4921 r = cxx_eval_array_reference (ctx, t, lval, 4922 non_constant_p, overflow_p); 4923 break; 4924 4925 case COMPONENT_REF: 4926 if (is_overloaded_fn (t)) 4927 { 4928 /* We can only get here in checking mode via 4929 build_non_dependent_expr, because any expression that 4930 calls or takes the address of the function will have 4931 pulled a FUNCTION_DECL out of the COMPONENT_REF. */ 4932 gcc_checking_assert (ctx->quiet || errorcount); 4933 *non_constant_p = true; 4934 return t; 4935 } 4936 r = cxx_eval_component_reference (ctx, t, lval, 4937 non_constant_p, overflow_p); 4938 break; 4939 4940 case BIT_FIELD_REF: 4941 r = cxx_eval_bit_field_ref (ctx, t, lval, 4942 non_constant_p, overflow_p); 4943 break; 4944 4945 case COND_EXPR: 4946 case IF_STMT: 4947 if (jump_target && *jump_target) 4948 { 4949 tree orig_jump = *jump_target; 4950 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) 4951 ? TREE_OPERAND (t, 1) : void_node); 4952 /* When jumping to a label, the label might be either in the 4953 then or else blocks, so process then block first in skipping 4954 mode first, and if we are still in the skipping mode at its end, 4955 process the else block too. */ 4956 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, 4957 overflow_p, jump_target); 4958 /* It's possible that we found the label in the then block. But 4959 it could have been followed by another jumping statement, e.g. 4960 say we're looking for case 1: 4961 if (cond) 4962 { 4963 // skipped statements 4964 case 1:; // clears up *jump_target 4965 return 1; // and sets it to a RETURN_EXPR 4966 } 4967 else { ... } 4968 in which case we need not go looking to the else block. 4969 (goto is not allowed in a constexpr function.) */ 4970 if (*jump_target == orig_jump) 4971 { 4972 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) 4973 ? TREE_OPERAND (t, 2) : void_node); 4974 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, 4975 overflow_p, jump_target); 4976 } 4977 break; 4978 } 4979 r = cxx_eval_conditional_expression (ctx, t, lval, 4980 non_constant_p, overflow_p, 4981 jump_target); 4982 break; 4983 case VEC_COND_EXPR: 4984 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, 4985 overflow_p); 4986 break; 4987 4988 case CONSTRUCTOR: 4989 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) 4990 { 4991 /* Don't re-process a constant CONSTRUCTOR, but do fold it to 4992 VECTOR_CST if applicable. */ 4993 verify_constructor_flags (t); 4994 if (TREE_CONSTANT (t)) 4995 return fold (t); 4996 } 4997 r = cxx_eval_bare_aggregate (ctx, t, lval, 4998 non_constant_p, overflow_p); 4999 break; 5000 5001 case VEC_INIT_EXPR: 5002 /* We can get this in a defaulted constructor for a class with a 5003 non-static data member of array type. Either the initializer will 5004 be NULL, meaning default-initialization, or it will be an lvalue 5005 or xvalue of the same type, meaning direct-initialization from the 5006 corresponding member. */ 5007 r = cxx_eval_vec_init (ctx, t, lval, 5008 non_constant_p, overflow_p); 5009 break; 5010 5011 case VEC_PERM_EXPR: 5012 r = cxx_eval_trinary_expression (ctx, t, lval, 5013 non_constant_p, overflow_p); 5014 break; 5015 5016 case NOP_EXPR: 5017 if (REINTERPRET_CAST_P (t)) 5018 { 5019 if (!ctx->quiet) 5020 error_at (cp_expr_loc_or_loc (t, input_location), 5021 "a reinterpret_cast is not a constant expression"); 5022 *non_constant_p = true; 5023 return t; 5024 } 5025 /* FALLTHROUGH. */ 5026 case CONVERT_EXPR: 5027 case VIEW_CONVERT_EXPR: 5028 case UNARY_PLUS_EXPR: 5029 { 5030 tree oldop = TREE_OPERAND (t, 0); 5031 5032 tree op = cxx_eval_constant_expression (ctx, oldop, 5033 lval, 5034 non_constant_p, overflow_p); 5035 if (*non_constant_p) 5036 return t; 5037 tree type = TREE_TYPE (t); 5038 if (TREE_CODE (op) == PTRMEM_CST 5039 && !TYPE_PTRMEM_P (type)) 5040 op = cplus_expand_constant (op); 5041 5042 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) 5043 { 5044 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) 5045 && !can_convert_qual (type, op)) 5046 op = cplus_expand_constant (op); 5047 return cp_fold_convert (type, op); 5048 } 5049 5050 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) 5051 { 5052 if (integer_zerop (op)) 5053 { 5054 if (TYPE_REF_P (type)) 5055 { 5056 if (!ctx->quiet) 5057 error_at (cp_expr_loc_or_loc (t, input_location), 5058 "dereferencing a null pointer"); 5059 *non_constant_p = true; 5060 return t; 5061 } 5062 else if (TYPE_PTR_P (TREE_TYPE (op))) 5063 { 5064 tree from = TREE_TYPE (op); 5065 5066 if (!can_convert (type, from, tf_none)) 5067 { 5068 if (!ctx->quiet) 5069 error_at (cp_expr_loc_or_loc (t, input_location), 5070 "conversion of %qT null pointer to %qT " 5071 "is not a constant expression", 5072 from, type); 5073 *non_constant_p = true; 5074 return t; 5075 } 5076 } 5077 } 5078 else 5079 { 5080 /* This detects for example: 5081 reinterpret_cast<void*>(sizeof 0) 5082 */ 5083 if (!ctx->quiet) 5084 error_at (cp_expr_loc_or_loc (t, input_location), 5085 "%<reinterpret_cast<%T>(%E)%> is not " 5086 "a constant expression", 5087 type, op); 5088 *non_constant_p = true; 5089 return t; 5090 } 5091 } 5092 5093 if (op == oldop && tcode != UNARY_PLUS_EXPR) 5094 /* We didn't fold at the top so we could check for ptr-int 5095 conversion. */ 5096 return fold (t); 5097 5098 /* Handle an array's bounds having been deduced after we built 5099 the wrapping expression. */ 5100 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) 5101 r = op; 5102 else if (tcode == UNARY_PLUS_EXPR) 5103 r = fold_convert (TREE_TYPE (t), op); 5104 else 5105 r = fold_build1 (tcode, type, op); 5106 5107 /* Conversion of an out-of-range value has implementation-defined 5108 behavior; the language considers it different from arithmetic 5109 overflow, which is undefined. */ 5110 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) 5111 TREE_OVERFLOW (r) = false; 5112 } 5113 break; 5114 5115 case EMPTY_CLASS_EXPR: 5116 /* This is good enough for a function argument that might not get 5117 used, and they can't do anything with it, so just return it. */ 5118 return t; 5119 5120 case STATEMENT_LIST: 5121 new_ctx = *ctx; 5122 new_ctx.ctor = new_ctx.object = NULL_TREE; 5123 return cxx_eval_statement_list (&new_ctx, t, 5124 non_constant_p, overflow_p, jump_target); 5125 5126 case BIND_EXPR: 5127 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), 5128 lval, 5129 non_constant_p, overflow_p, 5130 jump_target); 5131 5132 case PREINCREMENT_EXPR: 5133 case POSTINCREMENT_EXPR: 5134 case PREDECREMENT_EXPR: 5135 case POSTDECREMENT_EXPR: 5136 return cxx_eval_increment_expression (ctx, t, 5137 lval, non_constant_p, overflow_p); 5138 5139 case LAMBDA_EXPR: 5140 case NEW_EXPR: 5141 case VEC_NEW_EXPR: 5142 case DELETE_EXPR: 5143 case VEC_DELETE_EXPR: 5144 case THROW_EXPR: 5145 case MODOP_EXPR: 5146 /* GCC internal stuff. */ 5147 case VA_ARG_EXPR: 5148 case NON_DEPENDENT_EXPR: 5149 case BASELINK: 5150 case OFFSET_REF: 5151 if (!ctx->quiet) 5152 error_at (cp_expr_loc_or_loc (t, input_location), 5153 "expression %qE is not a constant expression", t); 5154 *non_constant_p = true; 5155 break; 5156 5157 case OBJ_TYPE_REF: 5158 { 5159 /* Virtual function call. Let the constexpr machinery figure out 5160 the dynamic type. */ 5161 int token = tree_to_shwi (OBJ_TYPE_REF_TOKEN (t)); 5162 tree obj = OBJ_TYPE_REF_OBJECT (t); 5163 obj = cxx_eval_constant_expression (ctx, obj, lval, non_constant_p, 5164 overflow_p); 5165 STRIP_NOPS (obj); 5166 /* We expect something in the form of &x.D.2103.D.2094; get x. */ 5167 if (TREE_CODE (obj) != ADDR_EXPR 5168 || !DECL_P (get_base_address (TREE_OPERAND (obj, 0)))) 5169 { 5170 if (!ctx->quiet) 5171 error_at (cp_expr_loc_or_loc (t, input_location), 5172 "expression %qE is not a constant expression", t); 5173 *non_constant_p = true; 5174 return t; 5175 } 5176 obj = TREE_OPERAND (obj, 0); 5177 while (TREE_CODE (obj) == COMPONENT_REF 5178 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))) 5179 obj = TREE_OPERAND (obj, 0); 5180 tree objtype = TREE_TYPE (obj); 5181 /* Find the function decl in the virtual functions list. TOKEN is 5182 the DECL_VINDEX that says which function we're looking for. */ 5183 tree virtuals = BINFO_VIRTUALS (TYPE_BINFO (objtype)); 5184 if (TARGET_VTABLE_USES_DESCRIPTORS) 5185 token /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1); 5186 r = TREE_VALUE (chain_index (token, virtuals)); 5187 break; 5188 } 5189 5190 case PLACEHOLDER_EXPR: 5191 /* Use of the value or address of the current object. */ 5192 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) 5193 return cxx_eval_constant_expression (ctx, ctor, lval, 5194 non_constant_p, overflow_p); 5195 /* A placeholder without a referent. We can get here when 5196 checking whether NSDMIs are noexcept, or in massage_init_elt; 5197 just say it's non-constant for now. */ 5198 gcc_assert (ctx->quiet); 5199 *non_constant_p = true; 5200 break; 5201 5202 case EXIT_EXPR: 5203 { 5204 tree cond = TREE_OPERAND (t, 0); 5205 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false, 5206 non_constant_p, overflow_p); 5207 VERIFY_CONSTANT (cond); 5208 if (integer_nonzerop (cond)) 5209 *jump_target = t; 5210 } 5211 break; 5212 5213 case GOTO_EXPR: 5214 *jump_target = TREE_OPERAND (t, 0); 5215 gcc_assert (breaks (jump_target) || continues (jump_target) 5216 /* Allow for jumping to a cdtor_label. */ 5217 || returns (jump_target)); 5218 break; 5219 5220 case LOOP_EXPR: 5221 case DO_STMT: 5222 case WHILE_STMT: 5223 case FOR_STMT: 5224 cxx_eval_loop_expr (ctx, t, 5225 non_constant_p, overflow_p, jump_target); 5226 break; 5227 5228 case SWITCH_EXPR: 5229 case SWITCH_STMT: 5230 cxx_eval_switch_expr (ctx, t, 5231 non_constant_p, overflow_p, jump_target); 5232 break; 5233 5234 case REQUIRES_EXPR: 5235 /* It's possible to get a requires-expression in a constant 5236 expression. For example: 5237 5238 template<typename T> concept bool C() { 5239 return requires (T t) { t; }; 5240 } 5241 5242 template<typename T> requires !C<T>() void f(T); 5243 5244 Normalization leaves f with the associated constraint 5245 '!requires (T t) { ... }' which is not transformed into 5246 a constraint. */ 5247 if (!processing_template_decl) 5248 return evaluate_constraint_expression (t, NULL_TREE); 5249 else 5250 *non_constant_p = true; 5251 return t; 5252 5253 case ANNOTATE_EXPR: 5254 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), 5255 lval, 5256 non_constant_p, overflow_p, 5257 jump_target); 5258 break; 5259 5260 case USING_STMT: 5261 r = void_node; 5262 break; 5263 5264 default: 5265 if (STATEMENT_CODE_P (TREE_CODE (t))) 5266 { 5267 /* This function doesn't know how to deal with pre-genericize 5268 statements; this can only happen with statement-expressions, 5269 so for now just fail. */ 5270 if (!ctx->quiet) 5271 error_at (EXPR_LOCATION (t), 5272 "statement is not a constant expression"); 5273 } 5274 else 5275 internal_error ("unexpected expression %qE of kind %s", t, 5276 get_tree_code_name (TREE_CODE (t))); 5277 *non_constant_p = true; 5278 break; 5279 } 5280 5281 if (r == error_mark_node) 5282 *non_constant_p = true; 5283 5284 if (*non_constant_p) 5285 return t; 5286 else 5287 return r; 5288 } 5289 5290 /* P0859: A function is needed for constant evaluation if it is a constexpr 5291 function that is named by an expression ([basic.def.odr]) that is 5292 potentially constant evaluated. 5293 5294 So we need to instantiate any constexpr functions mentioned by the 5295 expression even if the definition isn't needed for evaluating the 5296 expression. */ 5297 5298 static tree 5299 instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) 5300 { 5301 if (TREE_CODE (*tp) == FUNCTION_DECL 5302 && DECL_DECLARED_CONSTEXPR_P (*tp) 5303 && !DECL_INITIAL (*tp) 5304 && !trivial_fn_p (*tp) 5305 && DECL_TEMPLOID_INSTANTIATION (*tp)) 5306 { 5307 ++function_depth; 5308 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); 5309 --function_depth; 5310 } 5311 else if (TREE_CODE (*tp) == CALL_EXPR 5312 || TREE_CODE (*tp) == AGGR_INIT_EXPR) 5313 { 5314 if (EXPR_HAS_LOCATION (*tp)) 5315 input_location = EXPR_LOCATION (*tp); 5316 } 5317 5318 if (!EXPR_P (*tp)) 5319 *walk_subtrees = 0; 5320 5321 return NULL_TREE; 5322 } 5323 static void 5324 instantiate_constexpr_fns (tree t) 5325 { 5326 location_t loc = input_location; 5327 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); 5328 input_location = loc; 5329 } 5330 5331 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. 5332 STRICT has the same sense as for constant_value_1: true if we only allow 5333 conforming C++ constant expressions, or false if we want a constant value 5334 even if it doesn't conform. 5335 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as 5336 per P0595 even when ALLOW_NON_CONSTANT is true. */ 5337 5338 static tree 5339 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, 5340 bool strict = true, 5341 bool manifestly_const_eval = false, 5342 tree object = NULL_TREE) 5343 { 5344 auto_timevar time (TV_CONSTEXPR); 5345 5346 bool non_constant_p = false; 5347 bool overflow_p = false; 5348 hash_map<tree,tree> map; 5349 HOST_WIDE_INT constexpr_ctx_count = 0; 5350 5351 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL, 5352 &constexpr_ctx_count, allow_non_constant, strict, 5353 manifestly_const_eval || !allow_non_constant }; 5354 5355 tree type = initialized_type (t); 5356 tree r = t; 5357 if (VOID_TYPE_P (type)) 5358 return t; 5359 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) 5360 { 5361 /* In C++14 an NSDMI can participate in aggregate initialization, 5362 and can refer to the address of the object being initialized, so 5363 we need to pass in the relevant VAR_DECL if we want to do the 5364 evaluation in a single pass. The evaluation will dynamically 5365 update ctx.values for the VAR_DECL. We use the same strategy 5366 for C++11 constexpr constructors that refer to the object being 5367 initialized. */ 5368 ctx.ctor = build_constructor (type, NULL); 5369 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; 5370 if (!object) 5371 { 5372 if (TREE_CODE (t) == TARGET_EXPR) 5373 object = TARGET_EXPR_SLOT (t); 5374 else if (TREE_CODE (t) == AGGR_INIT_EXPR) 5375 object = AGGR_INIT_EXPR_SLOT (t); 5376 } 5377 ctx.object = object; 5378 if (object) 5379 gcc_assert (same_type_ignoring_top_level_qualifiers_p 5380 (type, TREE_TYPE (object))); 5381 if (object && DECL_P (object)) 5382 map.put (object, ctx.ctor); 5383 if (TREE_CODE (r) == TARGET_EXPR) 5384 /* Avoid creating another CONSTRUCTOR when we expand the 5385 TARGET_EXPR. */ 5386 r = TARGET_EXPR_INITIAL (r); 5387 } 5388 5389 instantiate_constexpr_fns (r); 5390 r = cxx_eval_constant_expression (&ctx, r, 5391 false, &non_constant_p, &overflow_p); 5392 5393 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); 5394 5395 /* Mutable logic is a bit tricky: we want to allow initialization of 5396 constexpr variables with mutable members, but we can't copy those 5397 members to another constexpr variable. */ 5398 if (TREE_CODE (r) == CONSTRUCTOR 5399 && CONSTRUCTOR_MUTABLE_POISON (r)) 5400 { 5401 if (!allow_non_constant) 5402 error ("%qE is not a constant expression because it refers to " 5403 "mutable subobjects of %qT", t, type); 5404 non_constant_p = true; 5405 } 5406 5407 if (TREE_CODE (r) == CONSTRUCTOR 5408 && CONSTRUCTOR_NO_CLEARING (r)) 5409 { 5410 if (!allow_non_constant) 5411 error ("%qE is not a constant expression because it refers to " 5412 "an incompletely initialized variable", t); 5413 TREE_CONSTANT (r) = false; 5414 non_constant_p = true; 5415 } 5416 5417 /* Technically we should check this for all subexpressions, but that 5418 runs into problems with our internal representation of pointer 5419 subtraction and the 5.19 rules are still in flux. */ 5420 if (CONVERT_EXPR_CODE_P (TREE_CODE (r)) 5421 && ARITHMETIC_TYPE_P (TREE_TYPE (r)) 5422 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR) 5423 { 5424 if (!allow_non_constant) 5425 error ("conversion from pointer type %qT " 5426 "to arithmetic type %qT in a constant expression", 5427 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r)); 5428 non_constant_p = true; 5429 } 5430 5431 if (!non_constant_p && overflow_p) 5432 non_constant_p = true; 5433 5434 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already 5435 unshared. */ 5436 bool should_unshare = true; 5437 if (r == t || TREE_CODE (r) == CONSTRUCTOR) 5438 should_unshare = false; 5439 5440 if (non_constant_p && !allow_non_constant) 5441 return error_mark_node; 5442 else if (non_constant_p && TREE_CONSTANT (r)) 5443 { 5444 /* If __builtin_is_constant_evaluated () was evaluated to true 5445 and the result is not a valid constant expression, we need to 5446 punt. */ 5447 if (manifestly_const_eval) 5448 return cxx_eval_outermost_constant_expr (t, true, strict, 5449 false, object); 5450 /* This isn't actually constant, so unset TREE_CONSTANT. 5451 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires 5452 it to be set if it is invariant address, even when it is not 5453 a valid C++ constant expression. Wrap it with a NOP_EXPR 5454 instead. */ 5455 if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR) 5456 r = copy_node (r); 5457 else if (TREE_CODE (r) == CONSTRUCTOR) 5458 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r); 5459 else 5460 r = build_nop (TREE_TYPE (r), r); 5461 TREE_CONSTANT (r) = false; 5462 } 5463 else if (non_constant_p) 5464 return t; 5465 5466 if (should_unshare) 5467 r = unshare_expr (r); 5468 5469 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) 5470 { 5471 r = adjust_temp_type (type, r); 5472 if (TREE_CODE (t) == TARGET_EXPR 5473 && TARGET_EXPR_INITIAL (t) == r) 5474 return t; 5475 else if (TREE_CODE (t) != CONSTRUCTOR) 5476 { 5477 r = get_target_expr (r); 5478 TREE_CONSTANT (r) = true; 5479 } 5480 } 5481 5482 return r; 5483 } 5484 5485 /* If T represents a constant expression returns its reduced value. 5486 Otherwise return error_mark_node. If T is dependent, then 5487 return NULL. */ 5488 5489 tree 5490 cxx_constant_value (tree t, tree decl) 5491 { 5492 return cxx_eval_outermost_constant_expr (t, false, true, true, decl); 5493 } 5494 5495 /* Helper routine for fold_simple function. Either return simplified 5496 expression T, otherwise NULL_TREE. 5497 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold 5498 even if we are within template-declaration. So be careful on call, as in 5499 such case types can be undefined. */ 5500 5501 static tree 5502 fold_simple_1 (tree t) 5503 { 5504 tree op1; 5505 enum tree_code code = TREE_CODE (t); 5506 5507 switch (code) 5508 { 5509 case INTEGER_CST: 5510 case REAL_CST: 5511 case VECTOR_CST: 5512 case FIXED_CST: 5513 case COMPLEX_CST: 5514 return t; 5515 5516 case SIZEOF_EXPR: 5517 return fold_sizeof_expr (t); 5518 5519 case ABS_EXPR: 5520 case ABSU_EXPR: 5521 case CONJ_EXPR: 5522 case REALPART_EXPR: 5523 case IMAGPART_EXPR: 5524 case NEGATE_EXPR: 5525 case BIT_NOT_EXPR: 5526 case TRUTH_NOT_EXPR: 5527 case NOP_EXPR: 5528 case VIEW_CONVERT_EXPR: 5529 case CONVERT_EXPR: 5530 case FLOAT_EXPR: 5531 case FIX_TRUNC_EXPR: 5532 case FIXED_CONVERT_EXPR: 5533 case ADDR_SPACE_CONVERT_EXPR: 5534 5535 op1 = TREE_OPERAND (t, 0); 5536 5537 t = const_unop (code, TREE_TYPE (t), op1); 5538 if (!t) 5539 return NULL_TREE; 5540 5541 if (CONVERT_EXPR_CODE_P (code) 5542 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) 5543 TREE_OVERFLOW (t) = false; 5544 return t; 5545 5546 default: 5547 return NULL_TREE; 5548 } 5549 } 5550 5551 /* If T is a simple constant expression, returns its simplified value. 5552 Otherwise returns T. In contrast to maybe_constant_value we 5553 simplify only few operations on constant-expressions, and we don't 5554 try to simplify constexpressions. */ 5555 5556 tree 5557 fold_simple (tree t) 5558 { 5559 if (processing_template_decl) 5560 return t; 5561 5562 tree r = fold_simple_1 (t); 5563 if (r) 5564 return r; 5565 5566 return t; 5567 } 5568 5569 /* If T is a constant expression, returns its reduced value. 5570 Otherwise, if T does not have TREE_CONSTANT set, returns T. 5571 Otherwise, returns a version of T without TREE_CONSTANT. 5572 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated 5573 as per P0595. */ 5574 5575 static GTY((deletable)) hash_map<tree, tree> *cv_cache; 5576 5577 tree 5578 maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) 5579 { 5580 tree r; 5581 5582 if (!is_nondependent_constant_expression (t)) 5583 { 5584 if (TREE_OVERFLOW_P (t)) 5585 { 5586 t = build_nop (TREE_TYPE (t), t); 5587 TREE_CONSTANT (t) = false; 5588 } 5589 return t; 5590 } 5591 else if (CONSTANT_CLASS_P (t)) 5592 /* No caching or evaluation needed. */ 5593 return t; 5594 5595 if (manifestly_const_eval) 5596 return cxx_eval_outermost_constant_expr (t, true, true, true, decl); 5597 5598 if (cv_cache == NULL) 5599 cv_cache = hash_map<tree, tree>::create_ggc (101); 5600 if (tree *cached = cv_cache->get (t)) 5601 { 5602 r = *cached; 5603 if (r != t) 5604 { 5605 r = unshare_expr_without_location (r); 5606 protected_set_expr_location (r, EXPR_LOCATION (t)); 5607 } 5608 return r; 5609 } 5610 5611 r = cxx_eval_outermost_constant_expr (t, true, true, false, decl); 5612 gcc_checking_assert (r == t 5613 || CONVERT_EXPR_P (t) 5614 || TREE_CODE (t) == VIEW_CONVERT_EXPR 5615 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) 5616 || !cp_tree_equal (r, t)); 5617 cv_cache->put (t, r); 5618 return r; 5619 } 5620 5621 /* Dispose of the whole CV_CACHE. */ 5622 5623 static void 5624 clear_cv_cache (void) 5625 { 5626 if (cv_cache != NULL) 5627 cv_cache->empty (); 5628 } 5629 5630 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ 5631 5632 void 5633 clear_cv_and_fold_caches (void) 5634 { 5635 clear_cv_cache (); 5636 clear_fold_cache (); 5637 } 5638 5639 /* Internal function handling expressions in templates for 5640 fold_non_dependent_expr and fold_non_dependent_init. 5641 5642 If we're in a template, but T isn't value dependent, simplify 5643 it. We're supposed to treat: 5644 5645 template <typename T> void f(T[1 + 1]); 5646 template <typename T> void f(T[2]); 5647 5648 as two declarations of the same function, for example. */ 5649 5650 static tree 5651 fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, 5652 bool manifestly_const_eval) 5653 { 5654 gcc_assert (processing_template_decl); 5655 5656 if (is_nondependent_constant_expression (t)) 5657 { 5658 processing_template_decl_sentinel s; 5659 t = instantiate_non_dependent_expr_internal (t, complain); 5660 5661 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) 5662 { 5663 if (TREE_OVERFLOW_P (t)) 5664 { 5665 t = build_nop (TREE_TYPE (t), t); 5666 TREE_CONSTANT (t) = false; 5667 } 5668 return t; 5669 } 5670 5671 tree r = cxx_eval_outermost_constant_expr (t, true, true, 5672 manifestly_const_eval, 5673 NULL_TREE); 5674 /* cp_tree_equal looks through NOPs, so allow them. */ 5675 gcc_checking_assert (r == t 5676 || CONVERT_EXPR_P (t) 5677 || TREE_CODE (t) == VIEW_CONVERT_EXPR 5678 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) 5679 || !cp_tree_equal (r, t)); 5680 return r; 5681 } 5682 else if (TREE_OVERFLOW_P (t)) 5683 { 5684 t = build_nop (TREE_TYPE (t), t); 5685 TREE_CONSTANT (t) = false; 5686 } 5687 5688 return t; 5689 } 5690 5691 /* Like maybe_constant_value but first fully instantiate the argument. 5692 5693 Note: this is equivalent to instantiate_non_dependent_expr_sfinae 5694 (t, complain) followed by maybe_constant_value but is more efficient, 5695 because it calls instantiation_dependent_expression_p and 5696 potential_constant_expression at most once. 5697 The manifestly_const_eval argument is passed to maybe_constant_value. 5698 5699 Callers should generally pass their active complain, or if they are in a 5700 non-template, diagnosing context, they can use the default of 5701 tf_warning_or_error. Callers that might be within a template context, don't 5702 have a complain parameter, and aren't going to remember the result for long 5703 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node 5704 appropriately. */ 5705 5706 tree 5707 fold_non_dependent_expr (tree t, 5708 tsubst_flags_t complain /* = tf_warning_or_error */, 5709 bool manifestly_const_eval /* = false */) 5710 { 5711 if (t == NULL_TREE) 5712 return NULL_TREE; 5713 5714 if (processing_template_decl) 5715 return fold_non_dependent_expr_template (t, complain, 5716 manifestly_const_eval); 5717 5718 return maybe_constant_value (t, NULL_TREE, manifestly_const_eval); 5719 } 5720 5721 5722 /* Like maybe_constant_init but first fully instantiate the argument. */ 5723 5724 tree 5725 fold_non_dependent_init (tree t, 5726 tsubst_flags_t complain /*=tf_warning_or_error*/, 5727 bool manifestly_const_eval /*=false*/) 5728 { 5729 if (t == NULL_TREE) 5730 return NULL_TREE; 5731 5732 if (processing_template_decl) 5733 { 5734 t = fold_non_dependent_expr_template (t, complain, 5735 manifestly_const_eval); 5736 /* maybe_constant_init does this stripping, so do it here too. */ 5737 if (TREE_CODE (t) == TARGET_EXPR) 5738 { 5739 tree init = TARGET_EXPR_INITIAL (t); 5740 if (TREE_CODE (init) == CONSTRUCTOR) 5741 t = init; 5742 } 5743 return t; 5744 } 5745 5746 return maybe_constant_init (t, NULL_TREE, manifestly_const_eval); 5747 } 5748 5749 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather 5750 than wrapped in a TARGET_EXPR. 5751 ALLOW_NON_CONSTANT is false if T is required to be a constant expression. 5752 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as 5753 per P0595 even when ALLOW_NON_CONSTANT is true. */ 5754 5755 static tree 5756 maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, 5757 bool manifestly_const_eval) 5758 { 5759 if (!t) 5760 return t; 5761 if (TREE_CODE (t) == EXPR_STMT) 5762 t = TREE_OPERAND (t, 0); 5763 if (TREE_CODE (t) == CONVERT_EXPR 5764 && VOID_TYPE_P (TREE_TYPE (t))) 5765 t = TREE_OPERAND (t, 0); 5766 if (TREE_CODE (t) == INIT_EXPR) 5767 t = TREE_OPERAND (t, 1); 5768 if (TREE_CODE (t) == TARGET_EXPR) 5769 t = TARGET_EXPR_INITIAL (t); 5770 if (!is_nondependent_static_init_expression (t)) 5771 /* Don't try to evaluate it. */; 5772 else if (CONSTANT_CLASS_P (t) && allow_non_constant) 5773 /* No evaluation needed. */; 5774 else 5775 t = cxx_eval_outermost_constant_expr (t, allow_non_constant, 5776 /*strict*/false, 5777 manifestly_const_eval, decl); 5778 if (TREE_CODE (t) == TARGET_EXPR) 5779 { 5780 tree init = TARGET_EXPR_INITIAL (t); 5781 if (TREE_CODE (init) == CONSTRUCTOR) 5782 t = init; 5783 } 5784 return t; 5785 } 5786 5787 /* Wrapper for maybe_constant_init_1 which permits non constants. */ 5788 5789 tree 5790 maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) 5791 { 5792 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); 5793 } 5794 5795 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ 5796 5797 tree 5798 cxx_constant_init (tree t, tree decl) 5799 { 5800 return maybe_constant_init_1 (t, decl, false, true); 5801 } 5802 5803 #if 0 5804 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ 5805 /* Return true if the object referred to by REF has automatic or thread 5806 local storage. */ 5807 5808 enum { ck_ok, ck_bad, ck_unknown }; 5809 static int 5810 check_automatic_or_tls (tree ref) 5811 { 5812 machine_mode mode; 5813 poly_int64 bitsize, bitpos; 5814 tree offset; 5815 int volatilep = 0, unsignedp = 0; 5816 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, 5817 &mode, &unsignedp, &volatilep, false); 5818 duration_kind dk; 5819 5820 /* If there isn't a decl in the middle, we don't know the linkage here, 5821 and this isn't a constant expression anyway. */ 5822 if (!DECL_P (decl)) 5823 return ck_unknown; 5824 dk = decl_storage_duration (decl); 5825 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; 5826 } 5827 #endif 5828 5829 /* Data structure for passing data from potential_constant_expression_1 5830 to check_for_return_continue via cp_walk_tree. */ 5831 struct check_for_return_continue_data { 5832 hash_set<tree> *pset; 5833 tree continue_stmt; 5834 }; 5835 5836 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, 5837 called through cp_walk_tree. Return the first RETURN_EXPR found, or note 5838 the first CONTINUE_STMT if RETURN_EXPR is not found. */ 5839 static tree 5840 check_for_return_continue (tree *tp, int *walk_subtrees, void *data) 5841 { 5842 tree t = *tp, s; 5843 check_for_return_continue_data *d = (check_for_return_continue_data *) data; 5844 switch (TREE_CODE (t)) 5845 { 5846 case RETURN_EXPR: 5847 return t; 5848 5849 case CONTINUE_STMT: 5850 if (d->continue_stmt == NULL_TREE) 5851 d->continue_stmt = t; 5852 break; 5853 5854 #define RECUR(x) \ 5855 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ 5856 d->pset)) \ 5857 return r 5858 5859 /* For loops, walk subtrees manually, so that continue stmts found 5860 inside of the bodies of the loops are ignored. */ 5861 case DO_STMT: 5862 *walk_subtrees = 0; 5863 RECUR (DO_COND (t)); 5864 s = d->continue_stmt; 5865 RECUR (DO_BODY (t)); 5866 d->continue_stmt = s; 5867 break; 5868 5869 case WHILE_STMT: 5870 *walk_subtrees = 0; 5871 RECUR (WHILE_COND (t)); 5872 s = d->continue_stmt; 5873 RECUR (WHILE_BODY (t)); 5874 d->continue_stmt = s; 5875 break; 5876 5877 case FOR_STMT: 5878 *walk_subtrees = 0; 5879 RECUR (FOR_INIT_STMT (t)); 5880 RECUR (FOR_COND (t)); 5881 RECUR (FOR_EXPR (t)); 5882 s = d->continue_stmt; 5883 RECUR (FOR_BODY (t)); 5884 d->continue_stmt = s; 5885 break; 5886 5887 case RANGE_FOR_STMT: 5888 *walk_subtrees = 0; 5889 RECUR (RANGE_FOR_EXPR (t)); 5890 s = d->continue_stmt; 5891 RECUR (RANGE_FOR_BODY (t)); 5892 d->continue_stmt = s; 5893 break; 5894 #undef RECUR 5895 5896 case STATEMENT_LIST: 5897 case CONSTRUCTOR: 5898 break; 5899 5900 default: 5901 if (!EXPR_P (t)) 5902 *walk_subtrees = 0; 5903 break; 5904 } 5905 5906 return NULL_TREE; 5907 } 5908 5909 /* Return true if T denotes a potentially constant expression. Issue 5910 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, 5911 an lvalue-rvalue conversion is implied. If NOW is true, we want to 5912 consider the expression in the current context, independent of constexpr 5913 substitution. 5914 5915 C++0x [expr.const] used to say 5916 5917 6 An expression is a potential constant expression if it is 5918 a constant expression where all occurrences of function 5919 parameters are replaced by arbitrary constant expressions 5920 of the appropriate type. 5921 5922 2 A conditional expression is a constant expression unless it 5923 involves one of the following as a potentially evaluated 5924 subexpression (3.2), but subexpressions of logical AND (5.14), 5925 logical OR (5.15), and conditional (5.16) operations that are 5926 not evaluated are not considered. */ 5927 5928 static bool 5929 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, 5930 tsubst_flags_t flags, tree *jump_target) 5931 { 5932 #define RECUR(T,RV) \ 5933 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target) 5934 5935 enum { any = false, rval = true }; 5936 int i; 5937 tree tmp; 5938 5939 if (t == error_mark_node) 5940 return false; 5941 if (t == NULL_TREE) 5942 return true; 5943 location_t loc = cp_expr_loc_or_loc (t, input_location); 5944 5945 if (*jump_target) 5946 /* If we are jumping, ignore everything. This is simpler than the 5947 cxx_eval_constant_expression handling because we only need to be 5948 conservatively correct, and we don't necessarily have a constant value 5949 available, so we don't bother with switch tracking. */ 5950 return true; 5951 5952 if (TREE_THIS_VOLATILE (t) && want_rval) 5953 { 5954 if (flags & tf_error) 5955 error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " 5956 "%qE with type %qT", t, TREE_TYPE (t)); 5957 return false; 5958 } 5959 if (CONSTANT_CLASS_P (t)) 5960 return true; 5961 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) 5962 && TREE_TYPE (t) == error_mark_node) 5963 return false; 5964 5965 switch (TREE_CODE (t)) 5966 { 5967 case FUNCTION_DECL: 5968 case BASELINK: 5969 case TEMPLATE_DECL: 5970 case OVERLOAD: 5971 case TEMPLATE_ID_EXPR: 5972 case LABEL_DECL: 5973 case LABEL_EXPR: 5974 case CASE_LABEL_EXPR: 5975 case PREDICT_EXPR: 5976 case CONST_DECL: 5977 case SIZEOF_EXPR: 5978 case ALIGNOF_EXPR: 5979 case OFFSETOF_EXPR: 5980 case NOEXCEPT_EXPR: 5981 case TEMPLATE_PARM_INDEX: 5982 case TRAIT_EXPR: 5983 case IDENTIFIER_NODE: 5984 case USERDEF_LITERAL: 5985 /* We can see a FIELD_DECL in a pointer-to-member expression. */ 5986 case FIELD_DECL: 5987 case RESULT_DECL: 5988 case USING_DECL: 5989 case USING_STMT: 5990 case PLACEHOLDER_EXPR: 5991 case REQUIRES_EXPR: 5992 case STATIC_ASSERT: 5993 case DEBUG_BEGIN_STMT: 5994 return true; 5995 5996 case RETURN_EXPR: 5997 if (!RECUR (TREE_OPERAND (t, 0), any)) 5998 return false; 5999 /* FALLTHROUGH */ 6000 6001 case BREAK_STMT: 6002 case CONTINUE_STMT: 6003 *jump_target = t; 6004 return true; 6005 6006 case PARM_DECL: 6007 if (now && want_rval) 6008 { 6009 tree type = TREE_TYPE (t); 6010 if (dependent_type_p (type) 6011 || is_really_empty_class (type, /*ignore_vptr*/false)) 6012 /* An empty class has no data to read. */ 6013 return true; 6014 if (flags & tf_error) 6015 error ("%qE is not a constant expression", t); 6016 return false; 6017 } 6018 return true; 6019 6020 case AGGR_INIT_EXPR: 6021 case CALL_EXPR: 6022 /* -- an invocation of a function other than a constexpr function 6023 or a constexpr constructor. */ 6024 { 6025 tree fun = get_function_named_in_call (t); 6026 const int nargs = call_expr_nargs (t); 6027 i = 0; 6028 6029 if (fun == NULL_TREE) 6030 { 6031 /* Reset to allow the function to continue past the end 6032 of the block below. Otherwise return early. */ 6033 bool bail = true; 6034 6035 if (TREE_CODE (t) == CALL_EXPR 6036 && CALL_EXPR_FN (t) == NULL_TREE) 6037 switch (CALL_EXPR_IFN (t)) 6038 { 6039 /* These should be ignored, they are optimized away from 6040 constexpr functions. */ 6041 case IFN_UBSAN_NULL: 6042 case IFN_UBSAN_BOUNDS: 6043 case IFN_UBSAN_VPTR: 6044 case IFN_FALLTHROUGH: 6045 return true; 6046 6047 case IFN_ADD_OVERFLOW: 6048 case IFN_SUB_OVERFLOW: 6049 case IFN_MUL_OVERFLOW: 6050 case IFN_LAUNDER: 6051 case IFN_VEC_CONVERT: 6052 bail = false; 6053 break; 6054 6055 default: 6056 break; 6057 } 6058 6059 if (bail) 6060 { 6061 /* fold_call_expr can't do anything with IFN calls. */ 6062 if (flags & tf_error) 6063 error_at (loc, "call to internal function %qE", t); 6064 return false; 6065 } 6066 } 6067 6068 if (fun && is_overloaded_fn (fun)) 6069 { 6070 if (TREE_CODE (fun) == FUNCTION_DECL) 6071 { 6072 if (builtin_valid_in_constant_expr_p (fun)) 6073 return true; 6074 if (!DECL_DECLARED_CONSTEXPR_P (fun) 6075 /* Allow any built-in function; if the expansion 6076 isn't constant, we'll deal with that then. */ 6077 && !fndecl_built_in_p (fun)) 6078 { 6079 if (flags & tf_error) 6080 { 6081 error_at (loc, "call to non-%<constexpr%> function %qD", 6082 fun); 6083 explain_invalid_constexpr_fn (fun); 6084 } 6085 return false; 6086 } 6087 /* A call to a non-static member function takes the address 6088 of the object as the first argument. But in a constant 6089 expression the address will be folded away, so look 6090 through it now. */ 6091 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) 6092 && !DECL_CONSTRUCTOR_P (fun)) 6093 { 6094 tree x = get_nth_callarg (t, 0); 6095 if (is_this_parameter (x)) 6096 return true; 6097 /* Don't require an immediately constant value, as 6098 constexpr substitution might not use the value. */ 6099 bool sub_now = false; 6100 if (!potential_constant_expression_1 (x, rval, strict, 6101 sub_now, flags, 6102 jump_target)) 6103 return false; 6104 i = 1; 6105 } 6106 } 6107 else 6108 { 6109 if (!RECUR (fun, true)) 6110 return false; 6111 fun = get_first_fn (fun); 6112 } 6113 /* Skip initial arguments to base constructors. */ 6114 if (DECL_BASE_CONSTRUCTOR_P (fun)) 6115 i = num_artificial_parms_for (fun); 6116 fun = DECL_ORIGIN (fun); 6117 } 6118 else if (fun) 6119 { 6120 if (RECUR (fun, rval)) 6121 /* Might end up being a constant function pointer. */; 6122 else 6123 return false; 6124 } 6125 for (; i < nargs; ++i) 6126 { 6127 tree x = get_nth_callarg (t, i); 6128 /* In a template, reference arguments haven't been converted to 6129 REFERENCE_TYPE and we might not even know if the parameter 6130 is a reference, so accept lvalue constants too. */ 6131 bool rv = processing_template_decl ? any : rval; 6132 /* Don't require an immediately constant value, as constexpr 6133 substitution might not use the value of the argument. */ 6134 bool sub_now = false; 6135 if (!potential_constant_expression_1 (x, rv, strict, 6136 sub_now, flags, jump_target)) 6137 return false; 6138 } 6139 return true; 6140 } 6141 6142 case NON_LVALUE_EXPR: 6143 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to 6144 -- an lvalue of integral type that refers to a non-volatile 6145 const variable or static data member initialized with 6146 constant expressions, or 6147 6148 -- an lvalue of literal type that refers to non-volatile 6149 object defined with constexpr, or that refers to a 6150 sub-object of such an object; */ 6151 return RECUR (TREE_OPERAND (t, 0), rval); 6152 6153 case VAR_DECL: 6154 if (DECL_HAS_VALUE_EXPR_P (t)) 6155 { 6156 if (now && is_normal_capture_proxy (t)) 6157 { 6158 /* -- in a lambda-expression, a reference to this or to a 6159 variable with automatic storage duration defined outside that 6160 lambda-expression, where the reference would be an 6161 odr-use. */ 6162 if (flags & tf_error) 6163 { 6164 tree cap = DECL_CAPTURED_VARIABLE (t); 6165 error ("lambda capture of %qE is not a constant expression", 6166 cap); 6167 if (!want_rval && decl_constant_var_p (cap)) 6168 inform (input_location, "because it is used as a glvalue"); 6169 } 6170 return false; 6171 } 6172 return RECUR (DECL_VALUE_EXPR (t), rval); 6173 } 6174 if (want_rval 6175 && !var_in_maybe_constexpr_fn (t) 6176 && !type_dependent_expression_p (t) 6177 && !decl_maybe_constant_var_p (t) 6178 && (strict 6179 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) 6180 || (DECL_INITIAL (t) 6181 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) 6182 && COMPLETE_TYPE_P (TREE_TYPE (t)) 6183 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) 6184 { 6185 if (flags & tf_error) 6186 non_const_var_error (t); 6187 return false; 6188 } 6189 return true; 6190 6191 case NOP_EXPR: 6192 if (REINTERPRET_CAST_P (t)) 6193 { 6194 if (flags & tf_error) 6195 error_at (loc, "a reinterpret_cast is not a constant expression"); 6196 return false; 6197 } 6198 /* FALLTHRU */ 6199 case CONVERT_EXPR: 6200 case VIEW_CONVERT_EXPR: 6201 /* -- a reinterpret_cast. FIXME not implemented, and this rule 6202 may change to something more specific to type-punning (DR 1312). */ 6203 { 6204 tree from = TREE_OPERAND (t, 0); 6205 if (location_wrapper_p (t)) 6206 return (RECUR (from, want_rval)); 6207 if (INDIRECT_TYPE_P (TREE_TYPE (t))) 6208 { 6209 STRIP_ANY_LOCATION_WRAPPER (from); 6210 if (TREE_CODE (from) == INTEGER_CST 6211 && !integer_zerop (from)) 6212 { 6213 if (flags & tf_error) 6214 error_at (loc, "reinterpret_cast from integer to pointer"); 6215 return false; 6216 } 6217 } 6218 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); 6219 } 6220 6221 case ADDRESSOF_EXPR: 6222 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ 6223 t = TREE_OPERAND (t, 0); 6224 goto handle_addr_expr; 6225 6226 case ADDR_EXPR: 6227 /* -- a unary operator & that is applied to an lvalue that 6228 designates an object with thread or automatic storage 6229 duration; */ 6230 t = TREE_OPERAND (t, 0); 6231 6232 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) 6233 /* A pointer-to-member constant. */ 6234 return true; 6235 6236 handle_addr_expr: 6237 #if 0 6238 /* FIXME adjust when issue 1197 is fully resolved. For now don't do 6239 any checking here, as we might dereference the pointer later. If 6240 we remove this code, also remove check_automatic_or_tls. */ 6241 i = check_automatic_or_tls (t); 6242 if (i == ck_ok) 6243 return true; 6244 if (i == ck_bad) 6245 { 6246 if (flags & tf_error) 6247 error ("address-of an object %qE with thread local or " 6248 "automatic storage is not a constant expression", t); 6249 return false; 6250 } 6251 #endif 6252 return RECUR (t, any); 6253 6254 case COMPONENT_REF: 6255 case ARROW_EXPR: 6256 case OFFSET_REF: 6257 /* -- a class member access unless its postfix-expression is 6258 of literal type or of pointer to literal type. */ 6259 /* This test would be redundant, as it follows from the 6260 postfix-expression being a potential constant expression. */ 6261 if (type_unknown_p (t)) 6262 return true; 6263 if (is_overloaded_fn (t)) 6264 /* In a template, a COMPONENT_REF of a function expresses ob.fn(), 6265 which uses ob as an lvalue. */ 6266 want_rval = false; 6267 gcc_fallthrough (); 6268 6269 case REALPART_EXPR: 6270 case IMAGPART_EXPR: 6271 case BIT_FIELD_REF: 6272 return RECUR (TREE_OPERAND (t, 0), want_rval); 6273 6274 case EXPR_PACK_EXPANSION: 6275 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); 6276 6277 case INDIRECT_REF: 6278 { 6279 tree x = TREE_OPERAND (t, 0); 6280 STRIP_NOPS (x); 6281 if (is_this_parameter (x) && !is_capture_proxy (x)) 6282 { 6283 if (!var_in_maybe_constexpr_fn (x)) 6284 { 6285 if (flags & tf_error) 6286 error_at (loc, "use of %<this%> in a constant expression"); 6287 return false; 6288 } 6289 return true; 6290 } 6291 return RECUR (x, rval); 6292 } 6293 6294 case STATEMENT_LIST: 6295 { 6296 tree_stmt_iterator i; 6297 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) 6298 { 6299 if (!RECUR (tsi_stmt (i), any)) 6300 return false; 6301 } 6302 return true; 6303 } 6304 break; 6305 6306 case MODIFY_EXPR: 6307 if (cxx_dialect < cxx14) 6308 goto fail; 6309 if (!RECUR (TREE_OPERAND (t, 0), any)) 6310 return false; 6311 if (!RECUR (TREE_OPERAND (t, 1), rval)) 6312 return false; 6313 return true; 6314 6315 case MODOP_EXPR: 6316 if (cxx_dialect < cxx14) 6317 goto fail; 6318 if (!RECUR (TREE_OPERAND (t, 0), rval)) 6319 return false; 6320 if (!RECUR (TREE_OPERAND (t, 2), rval)) 6321 return false; 6322 return true; 6323 6324 case DO_STMT: 6325 if (!RECUR (DO_COND (t), rval)) 6326 return false; 6327 if (!RECUR (DO_BODY (t), any)) 6328 return false; 6329 if (breaks (jump_target) || continues (jump_target)) 6330 *jump_target = NULL_TREE; 6331 return true; 6332 6333 case FOR_STMT: 6334 if (!RECUR (FOR_INIT_STMT (t), any)) 6335 return false; 6336 tmp = FOR_COND (t); 6337 if (!RECUR (tmp, rval)) 6338 return false; 6339 if (tmp) 6340 { 6341 if (!processing_template_decl) 6342 tmp = cxx_eval_outermost_constant_expr (tmp, true); 6343 /* If we couldn't evaluate the condition, it might not ever be 6344 true. */ 6345 if (!integer_onep (tmp)) 6346 return true; 6347 } 6348 if (!RECUR (FOR_EXPR (t), any)) 6349 return false; 6350 if (!RECUR (FOR_BODY (t), any)) 6351 return false; 6352 if (breaks (jump_target) || continues (jump_target)) 6353 *jump_target = NULL_TREE; 6354 return true; 6355 6356 case RANGE_FOR_STMT: 6357 if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) 6358 return false; 6359 if (!RECUR (RANGE_FOR_EXPR (t), any)) 6360 return false; 6361 if (!RECUR (RANGE_FOR_BODY (t), any)) 6362 return false; 6363 if (breaks (jump_target) || continues (jump_target)) 6364 *jump_target = NULL_TREE; 6365 return true; 6366 6367 case WHILE_STMT: 6368 tmp = WHILE_COND (t); 6369 if (!RECUR (tmp, rval)) 6370 return false; 6371 if (!processing_template_decl) 6372 tmp = cxx_eval_outermost_constant_expr (tmp, true); 6373 /* If we couldn't evaluate the condition, it might not ever be true. */ 6374 if (!integer_onep (tmp)) 6375 return true; 6376 if (!RECUR (WHILE_BODY (t), any)) 6377 return false; 6378 if (breaks (jump_target) || continues (jump_target)) 6379 *jump_target = NULL_TREE; 6380 return true; 6381 6382 case SWITCH_STMT: 6383 if (!RECUR (SWITCH_STMT_COND (t), rval)) 6384 return false; 6385 /* FIXME we don't check SWITCH_STMT_BODY currently, because even 6386 unreachable labels would be checked and it is enough if there is 6387 a single switch cond value for which it is a valid constant 6388 expression. We need to check if there are any RETURN_EXPRs 6389 or CONTINUE_STMTs inside of the body though, as in that case 6390 we need to set *jump_target. */ 6391 else 6392 { 6393 hash_set<tree> pset; 6394 check_for_return_continue_data data = { &pset, NULL_TREE }; 6395 if (tree ret_expr 6396 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, 6397 &data, &pset)) 6398 /* The switch might return. */ 6399 *jump_target = ret_expr; 6400 else if (data.continue_stmt) 6401 /* The switch can't return, but might continue. */ 6402 *jump_target = data.continue_stmt; 6403 } 6404 return true; 6405 6406 case STMT_EXPR: 6407 return RECUR (STMT_EXPR_STMT (t), rval); 6408 6409 case LAMBDA_EXPR: 6410 if (cxx_dialect >= cxx17) 6411 /* In C++17 lambdas can be constexpr, don't give up yet. */ 6412 return true; 6413 else if (flags & tf_error) 6414 error_at (loc, "lambda-expression is not a constant expression " 6415 "before C++17"); 6416 return false; 6417 6418 case DYNAMIC_CAST_EXPR: 6419 case PSEUDO_DTOR_EXPR: 6420 case NEW_EXPR: 6421 case VEC_NEW_EXPR: 6422 case DELETE_EXPR: 6423 case VEC_DELETE_EXPR: 6424 case THROW_EXPR: 6425 case OMP_PARALLEL: 6426 case OMP_TASK: 6427 case OMP_FOR: 6428 case OMP_SIMD: 6429 case OMP_DISTRIBUTE: 6430 case OMP_TASKLOOP: 6431 case OMP_TEAMS: 6432 case OMP_TARGET_DATA: 6433 case OMP_TARGET: 6434 case OMP_SECTIONS: 6435 case OMP_ORDERED: 6436 case OMP_CRITICAL: 6437 case OMP_SINGLE: 6438 case OMP_SECTION: 6439 case OMP_MASTER: 6440 case OMP_TASKGROUP: 6441 case OMP_TARGET_UPDATE: 6442 case OMP_TARGET_ENTER_DATA: 6443 case OMP_TARGET_EXIT_DATA: 6444 case OMP_ATOMIC: 6445 case OMP_ATOMIC_READ: 6446 case OMP_ATOMIC_CAPTURE_OLD: 6447 case OMP_ATOMIC_CAPTURE_NEW: 6448 case OMP_DEPOBJ: 6449 case OACC_PARALLEL: 6450 case OACC_KERNELS: 6451 case OACC_DATA: 6452 case OACC_HOST_DATA: 6453 case OACC_LOOP: 6454 case OACC_CACHE: 6455 case OACC_DECLARE: 6456 case OACC_ENTER_DATA: 6457 case OACC_EXIT_DATA: 6458 case OACC_UPDATE: 6459 /* GCC internal stuff. */ 6460 case VA_ARG_EXPR: 6461 case TRANSACTION_EXPR: 6462 case ASM_EXPR: 6463 case AT_ENCODE_EXPR: 6464 fail: 6465 if (flags & tf_error) 6466 error_at (loc, "expression %qE is not a constant expression", t); 6467 return false; 6468 6469 case OBJ_TYPE_REF: 6470 if (cxx_dialect >= cxx2a) 6471 /* In C++2a virtual calls can be constexpr, don't give up yet. */ 6472 return true; 6473 else if (flags & tf_error) 6474 error_at (loc, "virtual functions cannot be constexpr before C++2a"); 6475 return false; 6476 6477 case TYPEID_EXPR: 6478 /* -- a typeid expression whose operand is of polymorphic 6479 class type; */ 6480 { 6481 tree e = TREE_OPERAND (t, 0); 6482 if (!TYPE_P (e) && !type_dependent_expression_p (e) 6483 && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) 6484 { 6485 if (flags & tf_error) 6486 error_at (loc, "typeid-expression is not a constant expression " 6487 "because %qE is of polymorphic type", e); 6488 return false; 6489 } 6490 return true; 6491 } 6492 6493 case POINTER_DIFF_EXPR: 6494 case MINUS_EXPR: 6495 want_rval = true; 6496 goto binary; 6497 6498 case LT_EXPR: 6499 case LE_EXPR: 6500 case GT_EXPR: 6501 case GE_EXPR: 6502 case EQ_EXPR: 6503 case NE_EXPR: 6504 want_rval = true; 6505 goto binary; 6506 6507 case PREINCREMENT_EXPR: 6508 case POSTINCREMENT_EXPR: 6509 case PREDECREMENT_EXPR: 6510 case POSTDECREMENT_EXPR: 6511 if (cxx_dialect < cxx14) 6512 goto fail; 6513 goto unary; 6514 6515 case BIT_NOT_EXPR: 6516 /* A destructor. */ 6517 if (TYPE_P (TREE_OPERAND (t, 0))) 6518 return true; 6519 /* fall through. */ 6520 6521 case CONJ_EXPR: 6522 case SAVE_EXPR: 6523 case FIX_TRUNC_EXPR: 6524 case FLOAT_EXPR: 6525 case NEGATE_EXPR: 6526 case ABS_EXPR: 6527 case ABSU_EXPR: 6528 case TRUTH_NOT_EXPR: 6529 case FIXED_CONVERT_EXPR: 6530 case UNARY_PLUS_EXPR: 6531 case UNARY_LEFT_FOLD_EXPR: 6532 case UNARY_RIGHT_FOLD_EXPR: 6533 unary: 6534 return RECUR (TREE_OPERAND (t, 0), rval); 6535 6536 case CAST_EXPR: 6537 case CONST_CAST_EXPR: 6538 case STATIC_CAST_EXPR: 6539 case REINTERPRET_CAST_EXPR: 6540 case IMPLICIT_CONV_EXPR: 6541 if (cxx_dialect < cxx11 6542 && !dependent_type_p (TREE_TYPE (t)) 6543 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) 6544 /* In C++98, a conversion to non-integral type can't be part of a 6545 constant expression. */ 6546 { 6547 if (flags & tf_error) 6548 error_at (loc, 6549 "cast to non-integral type %qT in a constant expression", 6550 TREE_TYPE (t)); 6551 return false; 6552 } 6553 /* This might be a conversion from a class to a (potentially) literal 6554 type. Let's consider it potentially constant since the conversion 6555 might be a constexpr user-defined conversion. */ 6556 else if (cxx_dialect >= cxx11 6557 && (dependent_type_p (TREE_TYPE (t)) 6558 || !COMPLETE_TYPE_P (TREE_TYPE (t)) 6559 || literal_type_p (TREE_TYPE (t))) 6560 && TREE_OPERAND (t, 0)) 6561 { 6562 tree type = TREE_TYPE (TREE_OPERAND (t, 0)); 6563 /* If this is a dependent type, it could end up being a class 6564 with conversions. */ 6565 if (type == NULL_TREE || WILDCARD_TYPE_P (type)) 6566 return true; 6567 /* Or a non-dependent class which has conversions. */ 6568 else if (CLASS_TYPE_P (type) 6569 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) 6570 return true; 6571 } 6572 6573 return (RECUR (TREE_OPERAND (t, 0), 6574 !TYPE_REF_P (TREE_TYPE (t)))); 6575 6576 case BIND_EXPR: 6577 return RECUR (BIND_EXPR_BODY (t), want_rval); 6578 6579 case CLEANUP_POINT_EXPR: 6580 case MUST_NOT_THROW_EXPR: 6581 case TRY_CATCH_EXPR: 6582 case TRY_BLOCK: 6583 case EH_SPEC_BLOCK: 6584 case EXPR_STMT: 6585 case PAREN_EXPR: 6586 case NON_DEPENDENT_EXPR: 6587 /* For convenience. */ 6588 case LOOP_EXPR: 6589 case EXIT_EXPR: 6590 return RECUR (TREE_OPERAND (t, 0), want_rval); 6591 6592 case DECL_EXPR: 6593 tmp = DECL_EXPR_DECL (t); 6594 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) 6595 { 6596 if (TREE_STATIC (tmp)) 6597 { 6598 if (flags & tf_error) 6599 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " 6600 "%<static%> in %<constexpr%> context", tmp); 6601 return false; 6602 } 6603 else if (CP_DECL_THREAD_LOCAL_P (tmp)) 6604 { 6605 if (flags & tf_error) 6606 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " 6607 "%<thread_local%> in %<constexpr%> context", tmp); 6608 return false; 6609 } 6610 else if (!check_for_uninitialized_const_var 6611 (tmp, /*constexpr_context_p=*/true, flags)) 6612 return false; 6613 } 6614 return RECUR (tmp, want_rval); 6615 6616 case TRY_FINALLY_EXPR: 6617 return (RECUR (TREE_OPERAND (t, 0), want_rval) 6618 && RECUR (TREE_OPERAND (t, 1), any)); 6619 6620 case SCOPE_REF: 6621 return RECUR (TREE_OPERAND (t, 1), want_rval); 6622 6623 case TARGET_EXPR: 6624 if (!TARGET_EXPR_DIRECT_INIT_P (t) 6625 && !literal_type_p (TREE_TYPE (t))) 6626 { 6627 if (flags & tf_error) 6628 { 6629 auto_diagnostic_group d; 6630 error_at (loc, "temporary of non-literal type %qT in a " 6631 "constant expression", TREE_TYPE (t)); 6632 explain_non_literal_class (TREE_TYPE (t)); 6633 } 6634 return false; 6635 } 6636 /* FALLTHRU */ 6637 case INIT_EXPR: 6638 return RECUR (TREE_OPERAND (t, 1), rval); 6639 6640 case CONSTRUCTOR: 6641 { 6642 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); 6643 constructor_elt *ce; 6644 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 6645 if (!RECUR (ce->value, want_rval)) 6646 return false; 6647 return true; 6648 } 6649 6650 case TREE_LIST: 6651 { 6652 gcc_assert (TREE_PURPOSE (t) == NULL_TREE 6653 || DECL_P (TREE_PURPOSE (t))); 6654 if (!RECUR (TREE_VALUE (t), want_rval)) 6655 return false; 6656 if (TREE_CHAIN (t) == NULL_TREE) 6657 return true; 6658 return RECUR (TREE_CHAIN (t), want_rval); 6659 } 6660 6661 case TRUNC_DIV_EXPR: 6662 case CEIL_DIV_EXPR: 6663 case FLOOR_DIV_EXPR: 6664 case ROUND_DIV_EXPR: 6665 case TRUNC_MOD_EXPR: 6666 case CEIL_MOD_EXPR: 6667 case ROUND_MOD_EXPR: 6668 { 6669 tree denom = TREE_OPERAND (t, 1); 6670 if (!RECUR (denom, rval)) 6671 return false; 6672 /* We can't call cxx_eval_outermost_constant_expr on an expression 6673 that hasn't been through instantiate_non_dependent_expr yet. */ 6674 if (!processing_template_decl) 6675 denom = cxx_eval_outermost_constant_expr (denom, true); 6676 if (integer_zerop (denom)) 6677 { 6678 if (flags & tf_error) 6679 error ("division by zero is not a constant expression"); 6680 return false; 6681 } 6682 else 6683 { 6684 want_rval = true; 6685 return RECUR (TREE_OPERAND (t, 0), want_rval); 6686 } 6687 } 6688 6689 case COMPOUND_EXPR: 6690 { 6691 /* check_return_expr sometimes wraps a TARGET_EXPR in a 6692 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR 6693 introduced by build_call_a. */ 6694 tree op0 = TREE_OPERAND (t, 0); 6695 tree op1 = TREE_OPERAND (t, 1); 6696 STRIP_NOPS (op1); 6697 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) 6698 || TREE_CODE (op1) == EMPTY_CLASS_EXPR) 6699 return RECUR (op0, want_rval); 6700 else 6701 goto binary; 6702 } 6703 6704 /* If the first operand is the non-short-circuit constant, look at 6705 the second operand; otherwise we only care about the first one for 6706 potentiality. */ 6707 case TRUTH_AND_EXPR: 6708 case TRUTH_ANDIF_EXPR: 6709 tmp = boolean_true_node; 6710 goto truth; 6711 case TRUTH_OR_EXPR: 6712 case TRUTH_ORIF_EXPR: 6713 tmp = boolean_false_node; 6714 truth: 6715 { 6716 tree op = TREE_OPERAND (t, 0); 6717 if (!RECUR (op, rval)) 6718 return false; 6719 if (!processing_template_decl) 6720 op = cxx_eval_outermost_constant_expr (op, true); 6721 if (tree_int_cst_equal (op, tmp)) 6722 return RECUR (TREE_OPERAND (t, 1), rval); 6723 else 6724 return true; 6725 } 6726 6727 case PLUS_EXPR: 6728 case MULT_EXPR: 6729 case POINTER_PLUS_EXPR: 6730 case RDIV_EXPR: 6731 case EXACT_DIV_EXPR: 6732 case MIN_EXPR: 6733 case MAX_EXPR: 6734 case LSHIFT_EXPR: 6735 case RSHIFT_EXPR: 6736 case LROTATE_EXPR: 6737 case RROTATE_EXPR: 6738 case BIT_IOR_EXPR: 6739 case BIT_XOR_EXPR: 6740 case BIT_AND_EXPR: 6741 case TRUTH_XOR_EXPR: 6742 case UNORDERED_EXPR: 6743 case ORDERED_EXPR: 6744 case UNLT_EXPR: 6745 case UNLE_EXPR: 6746 case UNGT_EXPR: 6747 case UNGE_EXPR: 6748 case UNEQ_EXPR: 6749 case LTGT_EXPR: 6750 case RANGE_EXPR: 6751 case COMPLEX_EXPR: 6752 want_rval = true; 6753 /* Fall through. */ 6754 case ARRAY_REF: 6755 case ARRAY_RANGE_REF: 6756 case MEMBER_REF: 6757 case DOTSTAR_EXPR: 6758 case MEM_REF: 6759 case BINARY_LEFT_FOLD_EXPR: 6760 case BINARY_RIGHT_FOLD_EXPR: 6761 binary: 6762 for (i = 0; i < 2; ++i) 6763 if (!RECUR (TREE_OPERAND (t, i), want_rval)) 6764 return false; 6765 return true; 6766 6767 case VEC_PERM_EXPR: 6768 for (i = 0; i < 3; ++i) 6769 if (!RECUR (TREE_OPERAND (t, i), true)) 6770 return false; 6771 return true; 6772 6773 case COND_EXPR: 6774 if (COND_EXPR_IS_VEC_DELETE (t)) 6775 { 6776 if (flags & tf_error) 6777 error_at (loc, "%<delete[]%> is not a constant expression"); 6778 return false; 6779 } 6780 /* Fall through. */ 6781 case IF_STMT: 6782 case VEC_COND_EXPR: 6783 /* If the condition is a known constant, we know which of the legs we 6784 care about; otherwise we only require that the condition and 6785 either of the legs be potentially constant. */ 6786 tmp = TREE_OPERAND (t, 0); 6787 if (!RECUR (tmp, rval)) 6788 return false; 6789 if (!processing_template_decl) 6790 tmp = cxx_eval_outermost_constant_expr (tmp, true); 6791 if (integer_zerop (tmp)) 6792 return RECUR (TREE_OPERAND (t, 2), want_rval); 6793 else if (TREE_CODE (tmp) == INTEGER_CST) 6794 return RECUR (TREE_OPERAND (t, 1), want_rval); 6795 for (i = 1; i < 3; ++i) 6796 if (potential_constant_expression_1 (TREE_OPERAND (t, i), 6797 want_rval, strict, now, 6798 tf_none, jump_target)) 6799 return true; 6800 if (flags & tf_error) 6801 error_at (loc, "expression %qE is not a constant expression", t); 6802 return false; 6803 6804 case VEC_INIT_EXPR: 6805 if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) 6806 return true; 6807 if (flags & tf_error) 6808 { 6809 error_at (loc, "non-constant array initialization"); 6810 diagnose_non_constexpr_vec_init (t); 6811 } 6812 return false; 6813 6814 case TYPE_DECL: 6815 case TAG_DEFN: 6816 /* We can see these in statement-expressions. */ 6817 return true; 6818 6819 case CLEANUP_STMT: 6820 case EMPTY_CLASS_EXPR: 6821 return false; 6822 6823 case GOTO_EXPR: 6824 { 6825 tree *target = &TREE_OPERAND (t, 0); 6826 /* Gotos representing break and continue are OK. */ 6827 if (breaks (target) || continues (target)) 6828 { 6829 *jump_target = *target; 6830 return true; 6831 } 6832 if (flags & tf_error) 6833 error_at (loc, "%<goto%> is not a constant expression"); 6834 return false; 6835 } 6836 6837 case ANNOTATE_EXPR: 6838 return RECUR (TREE_OPERAND (t, 0), rval); 6839 6840 default: 6841 if (objc_is_property_ref (t)) 6842 return false; 6843 6844 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); 6845 gcc_unreachable (); 6846 return false; 6847 } 6848 #undef RECUR 6849 } 6850 6851 bool 6852 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, 6853 tsubst_flags_t flags) 6854 { 6855 tree target = NULL_TREE; 6856 return potential_constant_expression_1 (t, want_rval, strict, now, 6857 flags, &target); 6858 } 6859 6860 /* The main entry point to the above. */ 6861 6862 bool 6863 potential_constant_expression (tree t) 6864 { 6865 return potential_constant_expression_1 (t, false, true, false, tf_none); 6866 } 6867 6868 /* As above, but require a constant rvalue. */ 6869 6870 bool 6871 potential_rvalue_constant_expression (tree t) 6872 { 6873 return potential_constant_expression_1 (t, true, true, false, tf_none); 6874 } 6875 6876 /* Like above, but complain about non-constant expressions. */ 6877 6878 bool 6879 require_potential_constant_expression (tree t) 6880 { 6881 return potential_constant_expression_1 (t, false, true, false, 6882 tf_warning_or_error); 6883 } 6884 6885 /* Cross product of the above. */ 6886 6887 bool 6888 require_potential_rvalue_constant_expression (tree t) 6889 { 6890 return potential_constant_expression_1 (t, true, true, false, 6891 tf_warning_or_error); 6892 } 6893 6894 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ 6895 6896 bool 6897 require_rvalue_constant_expression (tree t) 6898 { 6899 return potential_constant_expression_1 (t, true, true, true, 6900 tf_warning_or_error); 6901 } 6902 6903 /* Like potential_constant_expression, but don't consider possible constexpr 6904 substitution of the current function. That is, PARM_DECL qualifies under 6905 potential_constant_expression, but not here. 6906 6907 This is basically what you can check when any actual constant values might 6908 be value-dependent. */ 6909 6910 bool 6911 is_constant_expression (tree t) 6912 { 6913 return potential_constant_expression_1 (t, false, true, true, tf_none); 6914 } 6915 6916 /* Like above, but complain about non-constant expressions. */ 6917 6918 bool 6919 require_constant_expression (tree t) 6920 { 6921 return potential_constant_expression_1 (t, false, true, true, 6922 tf_warning_or_error); 6923 } 6924 6925 /* Like is_constant_expression, but allow const variables that are not allowed 6926 under constexpr rules. */ 6927 6928 bool 6929 is_static_init_expression (tree t) 6930 { 6931 return potential_constant_expression_1 (t, false, false, true, tf_none); 6932 } 6933 6934 /* Returns true if T is a potential constant expression that is not 6935 instantiation-dependent, and therefore a candidate for constant folding even 6936 in a template. */ 6937 6938 bool 6939 is_nondependent_constant_expression (tree t) 6940 { 6941 return (!type_unknown_p (t) 6942 && !BRACE_ENCLOSED_INITIALIZER_P (t) 6943 && is_constant_expression (t) 6944 && !instantiation_dependent_expression_p (t)); 6945 } 6946 6947 /* Returns true if T is a potential static initializer expression that is not 6948 instantiation-dependent. */ 6949 6950 bool 6951 is_nondependent_static_init_expression (tree t) 6952 { 6953 return (!type_unknown_p (t) 6954 && !BRACE_ENCLOSED_INITIALIZER_P (t) 6955 && is_static_init_expression (t) 6956 && !instantiation_dependent_expression_p (t)); 6957 } 6958 6959 /* Finalize constexpr processing after parsing. */ 6960 6961 void 6962 fini_constexpr (void) 6963 { 6964 /* The contexpr call and fundef copies tables are no longer needed. */ 6965 constexpr_call_table = NULL; 6966 fundef_copies_table = NULL; 6967 } 6968 6969 #include "gt-cp-constexpr.h" 6970