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