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