1 /* Report error messages, build initializers, and perform 2 some front-end optimizations for C++ compiler. 3 Copyright (C) 1987-2024 Free Software Foundation, Inc. 4 Hacked by Michael Tiemann (tiemann (at) cygnus.com) 5 6 This file is part of GCC. 7 8 GCC is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3, or (at your option) 11 any later version. 12 13 GCC is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with GCC; see the file COPYING3. If not see 20 <http://www.gnu.org/licenses/>. */ 21 22 23 /* This file is part of the C++ front end. 24 It contains routines to build C++ expressions given their operands, 25 including computing the types of the result, C and C++ specific error 26 checks, and some optimization. */ 27 28 #include "config.h" 29 #include "system.h" 30 #include "coretypes.h" 31 #include "cp-tree.h" 32 #include "stor-layout.h" 33 #include "varasm.h" 34 #include "intl.h" 35 #include "gcc-rich-location.h" 36 #include "target.h" 37 38 static tree 39 process_init_constructor (tree type, tree init, int nested, int flags, 40 tsubst_flags_t complain); 41 42 43 /* Print an error message stemming from an attempt to use 44 BASETYPE as a base class for TYPE. */ 45 46 tree 47 error_not_base_type (tree basetype, tree type) 48 { 49 if (TREE_CODE (basetype) == FUNCTION_DECL) 50 basetype = DECL_CONTEXT (basetype); 51 error ("type %qT is not a base type for type %qT", basetype, type); 52 return error_mark_node; 53 } 54 55 tree 56 binfo_or_else (tree base, tree type) 57 { 58 tree binfo = lookup_base (type, base, ba_unique, 59 NULL, tf_warning_or_error); 60 61 if (binfo == error_mark_node) 62 return NULL_TREE; 63 else if (!binfo) 64 error_not_base_type (base, type); 65 return binfo; 66 } 67 68 /* According to ARM $7.1.6, "A `const' object may be initialized, but its 69 value may not be changed thereafter. */ 70 71 void 72 cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring) 73 { 74 75 /* This macro is used to emit diagnostics to ensure that all format 76 strings are complete sentences, visible to gettext and checked at 77 compile time. */ 78 79 #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \ 80 do { \ 81 switch (errstring) \ 82 { \ 83 case lv_assign: \ 84 error_at (LOC, AS, ARG); \ 85 break; \ 86 case lv_asm: \ 87 error_at (LOC, ASM, ARG); \ 88 break; \ 89 case lv_increment: \ 90 error_at (LOC, IN, ARG); \ 91 break; \ 92 case lv_decrement: \ 93 error_at (LOC, DE, ARG); \ 94 break; \ 95 default: \ 96 gcc_unreachable (); \ 97 } \ 98 } while (0) 99 100 /* Handle C++-specific things first. */ 101 102 if (VAR_P (arg) 103 && DECL_LANG_SPECIFIC (arg) 104 && DECL_IN_AGGR_P (arg) 105 && !TREE_STATIC (arg)) 106 ERROR_FOR_ASSIGNMENT (loc, 107 G_("assignment of constant field %qD"), 108 G_("constant field %qD used as %<asm%> output"), 109 G_("increment of constant field %qD"), 110 G_("decrement of constant field %qD"), 111 arg); 112 else if (INDIRECT_REF_P (arg) 113 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0))) 114 && (VAR_P (TREE_OPERAND (arg, 0)) 115 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) 116 ERROR_FOR_ASSIGNMENT (loc, 117 G_("assignment of read-only reference %qD"), 118 G_("read-only reference %qD used as %<asm%> output"), 119 G_("increment of read-only reference %qD"), 120 G_("decrement of read-only reference %qD"), 121 TREE_OPERAND (arg, 0)); 122 else 123 readonly_error (loc, arg, errstring); 124 } 125 126 /* If TYPE has abstract virtual functions, issue an error about trying 128 to create an object of that type. DECL is the object declared, or 129 NULL_TREE if the declaration is unavailable, in which case USE specifies 130 the kind of invalid use. Returns 1 if an error occurred; zero if 131 all was well. */ 132 133 static int 134 abstract_virtuals_error (tree decl, tree type, abstract_class_use use, 135 tsubst_flags_t complain) 136 { 137 vec<tree, va_gc> *pure; 138 139 if (TREE_CODE (type) == ARRAY_TYPE) 140 { 141 decl = NULL_TREE; 142 use = ACU_ARRAY; 143 type = strip_array_types (type); 144 } 145 146 /* This function applies only to classes. Any other entity can never 147 be abstract. */ 148 if (!CLASS_TYPE_P (type)) 149 return 0; 150 type = TYPE_MAIN_VARIANT (type); 151 152 #if 0 153 /* Instantiation here seems to be required by the standard, 154 but breaks e.g. boost::bind. FIXME! */ 155 /* In SFINAE, non-N3276 context, force instantiation. */ 156 if (!(complain & (tf_error|tf_decltype))) 157 complete_type (type); 158 #endif 159 160 if (!TYPE_SIZE (type)) 161 /* TYPE is being defined, and during that time 162 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */ 163 return 0; 164 165 pure = CLASSTYPE_PURE_VIRTUALS (type); 166 if (!pure) 167 return 0; 168 169 if (!(complain & tf_error)) 170 return 1; 171 172 auto_diagnostic_group d; 173 if (decl) 174 { 175 if (VAR_P (decl)) 176 error ("cannot declare variable %q+D to be of abstract " 177 "type %qT", decl, type); 178 else if (TREE_CODE (decl) == PARM_DECL) 179 { 180 if (DECL_NAME (decl)) 181 error ("cannot declare parameter %q+D to be of abstract type %qT", 182 decl, type); 183 else 184 error ("cannot declare parameter to be of abstract type %qT", 185 type); 186 } 187 else if (TREE_CODE (decl) == FIELD_DECL) 188 error ("cannot declare field %q+D to be of abstract type %qT", 189 decl, type); 190 else if (TREE_CODE (decl) == FUNCTION_DECL 191 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 192 error ("invalid abstract return type for member function %q+#D", decl); 193 else if (TREE_CODE (decl) == FUNCTION_DECL) 194 error ("invalid abstract return type for function %q+#D", decl); 195 else if (identifier_p (decl)) 196 /* Here we do not have location information. */ 197 error ("invalid abstract type %qT for %qE", type, decl); 198 else 199 error ("invalid abstract type for %q+D", decl); 200 } 201 else switch (use) 202 { 203 case ACU_ARRAY: 204 error ("creating array of %qT, which is an abstract class type", type); 205 break; 206 case ACU_CAST: 207 error ("invalid cast to abstract class type %qT", type); 208 break; 209 case ACU_NEW: 210 error ("invalid new-expression of abstract class type %qT", type); 211 break; 212 case ACU_RETURN: 213 error ("invalid abstract return type %qT", type); 214 break; 215 case ACU_PARM: 216 error ("invalid abstract parameter type %qT", type); 217 break; 218 case ACU_THROW: 219 error ("expression of abstract class type %qT cannot " 220 "be used in throw-expression", type); 221 break; 222 case ACU_CATCH: 223 error ("cannot declare %<catch%> parameter to be of abstract " 224 "class type %qT", type); 225 break; 226 default: 227 error ("cannot allocate an object of abstract type %qT", type); 228 } 229 230 /* Only go through this once. */ 231 if (pure->length ()) 232 { 233 unsigned ix; 234 tree fn; 235 236 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), 237 " because the following virtual functions are pure within %qT:", 238 type); 239 240 FOR_EACH_VEC_ELT (*pure, ix, fn) 241 if (! DECL_CLONED_FUNCTION_P (fn) 242 || DECL_COMPLETE_DESTRUCTOR_P (fn)) 243 inform (DECL_SOURCE_LOCATION (fn), " %#qD", fn); 244 245 /* Now truncate the vector. This leaves it non-null, so we know 246 there are pure virtuals, but empty so we don't list them out 247 again. */ 248 pure->truncate (0); 249 } 250 251 return 1; 252 } 253 254 int 255 abstract_virtuals_error (tree decl, tree type, 256 tsubst_flags_t complain /* = tf_warning_or_error */) 257 { 258 return abstract_virtuals_error (decl, type, ACU_UNKNOWN, complain); 259 } 260 261 int 262 abstract_virtuals_error (abstract_class_use use, tree type, 263 tsubst_flags_t complain /* = tf_warning_or_error */) 264 { 265 return abstract_virtuals_error (NULL_TREE, type, use, complain); 266 } 267 268 269 /* Print an inform about the declaration of the incomplete type TYPE. */ 270 271 void 272 cxx_incomplete_type_inform (const_tree type) 273 { 274 if (!TYPE_MAIN_DECL (type)) 275 return; 276 277 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)); 278 tree ptype = strip_top_quals (CONST_CAST_TREE (type)); 279 280 if (current_class_type 281 && TYPE_BEING_DEFINED (current_class_type) 282 && same_type_p (ptype, current_class_type)) 283 inform (loc, "definition of %q#T is not complete until " 284 "the closing brace", ptype); 285 else if (!TYPE_TEMPLATE_INFO (ptype)) 286 inform (loc, "forward declaration of %q#T", ptype); 287 else 288 inform (loc, "declaration of %q#T", ptype); 289 } 290 291 /* Print an error message for invalid use of an incomplete type. 292 VALUE is the expression that was used (or 0 if that isn't known) 293 and TYPE is the type that was invalid. DIAG_KIND indicates the 294 type of diagnostic (see diagnostic.def). */ 295 296 bool 297 cxx_incomplete_type_diagnostic (location_t loc, const_tree value, 298 const_tree type, diagnostic_t diag_kind) 299 { 300 bool is_decl = false, complained = false; 301 302 /* Avoid duplicate error message. */ 303 if (TREE_CODE (type) == ERROR_MARK) 304 return false; 305 306 if (value) 307 { 308 STRIP_ANY_LOCATION_WRAPPER (value); 309 310 if (VAR_P (value) 311 || TREE_CODE (value) == PARM_DECL 312 || TREE_CODE (value) == FIELD_DECL) 313 { 314 complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0, 315 "%qD has incomplete type", value); 316 is_decl = true; 317 } 318 } 319 retry: 320 /* We must print an error message. Be clever about what it says. */ 321 322 switch (TREE_CODE (type)) 323 { 324 case RECORD_TYPE: 325 case UNION_TYPE: 326 case ENUMERAL_TYPE: 327 if (!is_decl) 328 complained = emit_diagnostic (diag_kind, loc, 0, 329 "invalid use of incomplete type %q#T", 330 type); 331 if (complained) 332 cxx_incomplete_type_inform (type); 333 break; 334 335 case VOID_TYPE: 336 complained = emit_diagnostic (diag_kind, loc, 0, 337 "invalid use of %qT", type); 338 break; 339 340 case ARRAY_TYPE: 341 if (TYPE_DOMAIN (type)) 342 { 343 type = TREE_TYPE (type); 344 goto retry; 345 } 346 complained = emit_diagnostic (diag_kind, loc, 0, 347 "invalid use of array with unspecified bounds"); 348 break; 349 350 case OFFSET_TYPE: 351 bad_member: 352 { 353 tree member = TREE_OPERAND (value, 1); 354 if (is_overloaded_fn (member) && !flag_ms_extensions) 355 { 356 gcc_rich_location richloc (loc); 357 /* If "member" has no arguments (other than "this"), then 358 add a fix-it hint. */ 359 member = MAYBE_BASELINK_FUNCTIONS (member); 360 if (TREE_CODE (member) == FUNCTION_DECL 361 && DECL_OBJECT_MEMBER_FUNCTION_P (member) 362 && type_num_arguments (TREE_TYPE (member)) == 1) 363 richloc.add_fixit_insert_after ("()"); 364 complained = emit_diagnostic (diag_kind, &richloc, 0, 365 "invalid use of member function %qD " 366 "(did you forget the %<()%> ?)", member); 367 } 368 else 369 complained = emit_diagnostic (diag_kind, loc, 0, 370 "invalid use of member %qD " 371 "(did you forget the %<&%> ?)", member); 372 } 373 break; 374 375 case TEMPLATE_TYPE_PARM: 376 if (is_auto (type)) 377 { 378 if (CLASS_PLACEHOLDER_TEMPLATE (type)) 379 complained = emit_diagnostic (diag_kind, loc, 0, 380 "invalid use of placeholder %qT", type); 381 else 382 complained = emit_diagnostic (diag_kind, loc, 0, 383 "invalid use of %qT", type); 384 } 385 else 386 complained = emit_diagnostic (diag_kind, loc, 0, 387 "invalid use of template type parameter %qT", type); 388 break; 389 390 case BOUND_TEMPLATE_TEMPLATE_PARM: 391 complained = emit_diagnostic (diag_kind, loc, 0, 392 "invalid use of template template parameter %qT", 393 TYPE_NAME (type)); 394 break; 395 396 case TYPE_PACK_EXPANSION: 397 complained = emit_diagnostic (diag_kind, loc, 0, 398 "invalid use of pack expansion %qT", type); 399 break; 400 401 case TYPENAME_TYPE: 402 case DECLTYPE_TYPE: 403 complained = emit_diagnostic (diag_kind, loc, 0, 404 "invalid use of dependent type %qT", type); 405 break; 406 407 case LANG_TYPE: 408 if (type == init_list_type_node) 409 { 410 complained = emit_diagnostic (diag_kind, loc, 0, 411 "invalid use of brace-enclosed initializer list"); 412 break; 413 } 414 gcc_assert (type == unknown_type_node); 415 if (value && TREE_CODE (value) == COMPONENT_REF) 416 goto bad_member; 417 else if (value && TREE_CODE (value) == ADDR_EXPR) 418 complained = emit_diagnostic (diag_kind, loc, 0, 419 "address of overloaded function with no contextual " 420 "type information"); 421 else if (value && TREE_CODE (value) == OVERLOAD) 422 complained = emit_diagnostic (diag_kind, loc, 0, 423 "overloaded function with no contextual type information"); 424 else 425 complained = emit_diagnostic (diag_kind, loc, 0, 426 "insufficient contextual information to determine type"); 427 break; 428 429 default: 430 gcc_unreachable (); 431 } 432 433 return complained; 434 } 435 436 /* Print an error message for invalid use of an incomplete type. 437 VALUE is the expression that was used (or 0 if that isn't known) 438 and TYPE is the type that was invalid. */ 439 440 void 441 cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type) 442 { 443 cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR); 444 } 445 446 447 /* We've just initialized subobject SUB; also insert a TARGET_EXPR with an 449 EH-only cleanup for SUB. Because of EH region nesting issues, we need to 450 make the cleanup conditional on a flag that we will clear once the object is 451 fully initialized, so push a new flag onto FLAGS. */ 452 453 static void 454 maybe_push_temp_cleanup (tree sub, vec<tree,va_gc> **flags) 455 { 456 if (!flag_exceptions) 457 return; 458 if (tree cleanup 459 = cxx_maybe_build_cleanup (sub, tf_warning_or_error)) 460 { 461 tree tx = get_target_expr (boolean_true_node); 462 tree flag = TARGET_EXPR_SLOT (tx); 463 CLEANUP_EH_ONLY (tx) = true; 464 TARGET_EXPR_CLEANUP (tx) = build3 (COND_EXPR, void_type_node, 465 flag, cleanup, void_node); 466 add_stmt (tx); 467 vec_safe_push (*flags, flag); 468 } 469 } 470 471 /* F is something added to a cleanup flags vec by maybe_push_temp_cleanup or 472 build_vec_init. Return the code to disable the cleanup it controls. */ 473 474 tree 475 build_disable_temp_cleanup (tree f) 476 { 477 tree d = f; 478 tree i = boolean_false_node; 479 if (TREE_CODE (f) == TREE_LIST) 480 { 481 /* To disable a build_vec_init cleanup, set 482 iterator = maxindex. */ 483 d = TREE_PURPOSE (f); 484 i = TREE_VALUE (f); 485 ggc_free (f); 486 } 487 return build2 (MODIFY_EXPR, TREE_TYPE (d), d, i); 488 } 489 490 /* The recursive part of split_nonconstant_init. DEST is an lvalue 491 expression to which INIT should be assigned. INIT is a CONSTRUCTOR. 492 Return true if the whole of the value was initialized by the 493 generated statements. */ 494 495 static bool 496 split_nonconstant_init_1 (tree dest, tree init, bool last, 497 vec<tree,va_gc> **flags) 498 { 499 unsigned HOST_WIDE_INT idx, tidx = HOST_WIDE_INT_M1U; 500 tree field_index, value; 501 tree type = TREE_TYPE (dest); 502 tree inner_type = NULL; 503 bool array_type_p = false; 504 bool complete_p = true; 505 HOST_WIDE_INT num_split_elts = 0; 506 tree last_split_elt = NULL_TREE; 507 508 switch (TREE_CODE (type)) 509 { 510 case ARRAY_TYPE: 511 inner_type = TREE_TYPE (type); 512 array_type_p = true; 513 if ((TREE_SIDE_EFFECTS (init) 514 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 515 || vla_type_p (type)) 516 { 517 if (!TYPE_DOMAIN (type) 518 && TREE_CODE (init) == CONSTRUCTOR 519 && CONSTRUCTOR_NELTS (init)) 520 { 521 /* Flexible array. */ 522 cp_complete_array_type (&type, init, /*default*/true); 523 dest = build1 (VIEW_CONVERT_EXPR, type, dest); 524 } 525 526 /* For an array, we only need/want a single cleanup region rather 527 than one per element. build_vec_init will handle it. */ 528 tree code = build_vec_init (dest, NULL_TREE, init, false, 1, 529 tf_warning_or_error, flags); 530 add_stmt (code); 531 return true; 532 } 533 /* FALLTHRU */ 534 535 case RECORD_TYPE: 536 case UNION_TYPE: 537 case QUAL_UNION_TYPE: 538 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, 539 field_index, value) 540 { 541 /* The current implementation of this algorithm assumes that 542 the field was set for all the elements. This is usually done 543 by process_init_constructor. */ 544 gcc_assert (field_index); 545 546 if (!array_type_p) 547 inner_type = TREE_TYPE (field_index); 548 549 tree sub; 550 if (array_type_p) 551 sub = build4 (ARRAY_REF, inner_type, dest, field_index, 552 NULL_TREE, NULL_TREE); 553 else 554 sub = build3 (COMPONENT_REF, inner_type, dest, field_index, 555 NULL_TREE); 556 557 bool elt_last = last && idx == CONSTRUCTOR_NELTS (init) - 1; 558 559 /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can 560 handle cleanup flags properly. */ 561 gcc_checking_assert (!target_expr_needs_replace (value)); 562 563 if (TREE_CODE (value) == CONSTRUCTOR) 564 { 565 if (!split_nonconstant_init_1 (sub, value, elt_last, flags) 566 /* For flexible array member with initializer we 567 can't remove the initializer, because only the 568 initializer determines how many elements the 569 flexible array member has. */ 570 || (!array_type_p 571 && TREE_CODE (inner_type) == ARRAY_TYPE 572 && TYPE_DOMAIN (inner_type) == NULL 573 && TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE 574 && COMPLETE_TYPE_P (TREE_TYPE (value)) 575 && !integer_zerop (TYPE_SIZE (TREE_TYPE (value))) 576 && elt_last 577 && TYPE_HAS_TRIVIAL_DESTRUCTOR 578 (strip_array_types (inner_type)))) 579 complete_p = false; 580 else 581 { 582 /* Mark element for removal. */ 583 last_split_elt = field_index; 584 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE; 585 if (idx < tidx) 586 tidx = idx; 587 num_split_elts++; 588 } 589 } 590 else if (tree vi = get_vec_init_expr (value)) 591 { 592 add_stmt (expand_vec_init_expr (sub, vi, tf_warning_or_error, 593 flags)); 594 595 /* Mark element for removal. */ 596 last_split_elt = field_index; 597 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE; 598 if (idx < tidx) 599 tidx = idx; 600 num_split_elts++; 601 } 602 else if (!initializer_constant_valid_p (value, inner_type)) 603 { 604 tree code; 605 606 /* Push cleanups for any preceding members with constant 607 initialization. */ 608 if (CLASS_TYPE_P (type)) 609 for (tree prev = (last_split_elt ? 610 DECL_CHAIN (last_split_elt) 611 : TYPE_FIELDS (type)); 612 ; prev = DECL_CHAIN (prev)) 613 { 614 prev = next_aggregate_field (prev); 615 if (prev == field_index) 616 break; 617 tree ptype = TREE_TYPE (prev); 618 if (TYPE_P (ptype) && type_build_dtor_call (ptype)) 619 { 620 tree pcref = build3 (COMPONENT_REF, ptype, dest, prev, 621 NULL_TREE); 622 maybe_push_temp_cleanup (pcref, flags); 623 } 624 } 625 626 /* Mark element for removal. */ 627 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE; 628 if (idx < tidx) 629 tidx = idx; 630 631 if (TREE_CODE (field_index) == RANGE_EXPR) 632 { 633 /* Use build_vec_init to initialize a range. */ 634 tree low = TREE_OPERAND (field_index, 0); 635 tree hi = TREE_OPERAND (field_index, 1); 636 sub = build4 (ARRAY_REF, inner_type, dest, low, 637 NULL_TREE, NULL_TREE); 638 sub = cp_build_addr_expr (sub, tf_warning_or_error); 639 tree max = size_binop (MINUS_EXPR, hi, low); 640 code = build_vec_init (sub, max, value, false, 0, 641 tf_warning_or_error); 642 add_stmt (code); 643 if (tree_fits_shwi_p (max)) 644 num_split_elts += tree_to_shwi (max); 645 } 646 else 647 { 648 /* We may need to add a copy constructor call if 649 the field has [[no_unique_address]]. */ 650 if (unsafe_return_slot_p (sub)) 651 { 652 /* But not if the initializer is an implicit ctor call 653 we just built in digest_init. */ 654 if (TREE_CODE (value) == TARGET_EXPR 655 && TARGET_EXPR_LIST_INIT_P (value) 656 && make_safe_copy_elision (sub, value)) 657 goto build_init; 658 659 tree name = (DECL_FIELD_IS_BASE (field_index) 660 ? base_ctor_identifier 661 : complete_ctor_identifier); 662 releasing_vec args = make_tree_vector_single (value); 663 code = build_special_member_call 664 (sub, name, &args, inner_type, 665 LOOKUP_NORMAL, tf_warning_or_error); 666 } 667 else 668 { 669 build_init: 670 code = cp_build_init_expr (sub, value); 671 } 672 code = build_stmt (input_location, EXPR_STMT, code); 673 add_stmt (code); 674 if (!elt_last) 675 maybe_push_temp_cleanup (sub, flags); 676 } 677 678 last_split_elt = field_index; 679 num_split_elts++; 680 } 681 } 682 if (num_split_elts == 1) 683 CONSTRUCTOR_ELTS (init)->ordered_remove (tidx); 684 else if (num_split_elts > 1) 685 { 686 /* Perform the delayed ordered removal of non-constant elements 687 we split out. */ 688 for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx) 689 if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE) 690 ; 691 else 692 { 693 *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx); 694 ++tidx; 695 } 696 vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx); 697 } 698 break; 699 700 case VECTOR_TYPE: 701 if (!initializer_constant_valid_p (init, type)) 702 { 703 tree code; 704 tree cons = copy_node (init); 705 CONSTRUCTOR_ELTS (init) = NULL; 706 code = build2 (MODIFY_EXPR, type, dest, cons); 707 code = build_stmt (input_location, EXPR_STMT, code); 708 add_stmt (code); 709 num_split_elts += CONSTRUCTOR_NELTS (init); 710 } 711 break; 712 713 default: 714 gcc_unreachable (); 715 } 716 717 /* The rest of the initializer is now a constant. */ 718 TREE_CONSTANT (init) = 1; 719 TREE_SIDE_EFFECTS (init) = 0; 720 721 /* We didn't split out anything. */ 722 if (num_split_elts == 0) 723 return false; 724 725 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init), 726 num_split_elts, inner_type); 727 } 728 729 /* A subroutine of store_init_value. Splits non-constant static 730 initializer INIT into a constant part and generates code to 731 perform the non-constant part of the initialization to DEST. 732 Returns the code for the runtime init. */ 733 734 tree 735 split_nonconstant_init (tree dest, tree init) 736 { 737 tree code; 738 739 if (TREE_CODE (init) == TARGET_EXPR) 740 init = TARGET_EXPR_INITIAL (init); 741 if (TREE_CODE (init) == CONSTRUCTOR) 742 { 743 /* Subobject initializers are not full-expressions. */ 744 auto fe = (make_temp_override 745 (current_stmt_tree ()->stmts_are_full_exprs_p, 0)); 746 747 init = cp_fully_fold_init (init); 748 code = push_stmt_list (); 749 750 /* If the complete object is an array, build_vec_init's cleanup is 751 enough. Otherwise, collect flags for disabling subobject 752 cleanups once the complete object is fully constructed. */ 753 vec<tree, va_gc> *flags = nullptr; 754 if (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE) 755 flags = make_tree_vector (); 756 757 if (split_nonconstant_init_1 (dest, init, true, &flags)) 758 init = NULL_TREE; 759 760 for (tree f : flags) 761 add_stmt (build_disable_temp_cleanup (f)); 762 release_tree_vector (flags); 763 764 code = pop_stmt_list (code); 765 if (VAR_P (dest) && !is_local_temp (dest)) 766 { 767 DECL_INITIAL (dest) = init; 768 TREE_READONLY (dest) = 0; 769 } 770 else if (init) 771 { 772 tree ie = cp_build_init_expr (dest, init); 773 code = add_stmt_to_compound (ie, code); 774 } 775 } 776 else if (TREE_CODE (init) == STRING_CST 777 && array_of_runtime_bound_p (TREE_TYPE (dest))) 778 code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false, 779 /*from array*/1, tf_warning_or_error); 780 else 781 code = cp_build_init_expr (dest, init); 782 783 return code; 784 } 785 786 /* T is the initializer of a constexpr variable. Set CONSTRUCTOR_MUTABLE_POISON 787 for any CONSTRUCTOR within T that contains (directly or indirectly) a mutable 788 member, thereby poisoning it so it can't be copied to another a constexpr 789 variable or read during constexpr evaluation. */ 790 791 static void 792 poison_mutable_constructors (tree t) 793 { 794 if (TREE_CODE (t) != CONSTRUCTOR) 795 return; 796 797 if (cp_has_mutable_p (TREE_TYPE (t))) 798 { 799 CONSTRUCTOR_MUTABLE_POISON (t) = true; 800 801 if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (t)) 802 for (const constructor_elt &ce : *elts) 803 poison_mutable_constructors (ce.value); 804 } 805 } 806 807 /* Perform appropriate conversions on the initial value of a variable, 808 store it in the declaration DECL, 809 and print any error messages that are appropriate. 810 If the init is invalid, store an ERROR_MARK. 811 812 C++: Note that INIT might be a TREE_LIST, which would mean that it is 813 a base class initializer for some aggregate type, hopefully compatible 814 with DECL. If INIT is a single element, and DECL is an aggregate 815 type, we silently convert INIT into a TREE_LIST, allowing a constructor 816 to be called. 817 818 If INIT is a TREE_LIST and there is no constructor, turn INIT 819 into a CONSTRUCTOR and use standard initialization techniques. 820 Perhaps a warning should be generated? 821 822 Returns code to be executed if initialization could not be performed 823 for static variable. In that case, caller must emit the code. */ 824 825 tree 826 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags) 827 { 828 tree value, type; 829 830 /* If variable's type was invalidly declared, just ignore it. */ 831 832 type = TREE_TYPE (decl); 833 if (TREE_CODE (type) == ERROR_MARK) 834 return NULL_TREE; 835 836 if (MAYBE_CLASS_TYPE_P (type)) 837 { 838 if (TREE_CODE (init) == TREE_LIST) 839 { 840 error ("constructor syntax used, but no constructor declared " 841 "for type %qT", type); 842 init = build_constructor_from_list (init_list_type_node, nreverse (init)); 843 } 844 } 845 846 /* End of special C++ code. */ 847 848 if (flags & LOOKUP_ALREADY_DIGESTED) 849 value = init; 850 else 851 { 852 if (TREE_STATIC (decl)) 853 flags |= LOOKUP_ALLOW_FLEXARRAY_INIT; 854 /* Digest the specified initializer into an expression. */ 855 value = digest_init_flags (type, init, flags, tf_warning_or_error); 856 } 857 858 /* Look for braced array initializers for character arrays and 859 recursively convert them into STRING_CSTs. */ 860 value = braced_lists_to_strings (type, value); 861 862 current_ref_temp_count = 0; 863 value = extend_ref_init_temps (decl, value, cleanups); 864 865 /* In C++11 constant expression is a semantic, not syntactic, property. 866 In C++98, make sure that what we thought was a constant expression at 867 template definition time is still constant and otherwise perform this 868 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */ 869 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl)) 870 { 871 bool const_init; 872 tree oldval = value; 873 if (DECL_DECLARED_CONSTEXPR_P (decl) 874 || DECL_DECLARED_CONSTINIT_P (decl) 875 || (DECL_IN_AGGR_P (decl) 876 && DECL_INITIALIZED_IN_CLASS_P (decl))) 877 { 878 value = fold_non_dependent_expr (value, tf_warning_or_error, 879 /*manifestly_const_eval=*/true, 880 decl); 881 if (value == error_mark_node) 882 ; 883 /* Diagnose a non-constant initializer for constexpr variable or 884 non-inline in-class-initialized static data member. */ 885 else if (!is_constant_expression (value)) 886 { 887 /* Maybe we want to give this message for constexpr variables as 888 well, but that will mean a lot of testsuite adjustment. */ 889 if (DECL_DECLARED_CONSTINIT_P (decl)) 890 error_at (location_of (decl), 891 "%<constinit%> variable %qD does not have a " 892 "constant initializer", decl); 893 require_constant_expression (value); 894 value = error_mark_node; 895 } 896 else 897 { 898 value = maybe_constant_init (value, decl, true); 899 900 /* In a template we might not have done the necessary 901 transformations to make value actually constant, 902 e.g. extend_ref_init_temps. */ 903 if (!processing_template_decl 904 && !TREE_CONSTANT (value)) 905 { 906 if (DECL_DECLARED_CONSTINIT_P (decl)) 907 error_at (location_of (decl), 908 "%<constinit%> variable %qD does not have a " 909 "constant initializer", decl); 910 value = cxx_constant_init (value, decl); 911 } 912 } 913 } 914 else 915 value = fold_non_dependent_init (value, tf_warning_or_error, 916 /*manifestly_const_eval=*/true, decl); 917 poison_mutable_constructors (value); 918 const_init = (reduced_constant_expression_p (value) 919 || error_operand_p (value)); 920 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init; 921 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ 922 if (!TYPE_REF_P (type)) 923 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl); 924 if (!const_init) 925 value = oldval; 926 } 927 /* Don't fold initializers of automatic variables in constexpr functions, 928 that might fold away something that needs to be diagnosed at constexpr 929 evaluation time. */ 930 if (!current_function_decl 931 || !DECL_DECLARED_CONSTEXPR_P (current_function_decl) 932 || TREE_STATIC (decl)) 933 value = cp_fully_fold_init (value); 934 935 /* Handle aggregate NSDMI in non-constant initializers, too. */ 936 value = replace_placeholders (value, decl); 937 938 /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get 939 here it should have been digested into an actual value for the type. */ 940 gcc_checking_assert (TREE_CODE (value) != CONSTRUCTOR 941 || processing_template_decl 942 || VECTOR_TYPE_P (type) 943 || !TREE_HAS_CONSTRUCTOR (value)); 944 945 /* If the initializer is not a constant, fill in DECL_INITIAL with 946 the bits that are constant, and then return an expression that 947 will perform the dynamic initialization. */ 948 if (value != error_mark_node 949 && !processing_template_decl 950 && (TREE_SIDE_EFFECTS (value) 951 || vla_type_p (type) 952 || ! reduced_constant_expression_p (value))) 953 return split_nonconstant_init (decl, value); 954 955 /* DECL may change value; purge caches. */ 956 clear_cv_and_fold_caches (); 957 958 /* If the value is a constant, just put it in DECL_INITIAL. If DECL 959 is an automatic variable, the middle end will turn this into a 960 dynamic initialization later. */ 961 DECL_INITIAL (decl) = value; 962 return NULL_TREE; 963 } 964 965 966 /* Give diagnostic about narrowing conversions within { }, or as part of 968 a converted constant expression. If CONST_ONLY, only check 969 constants. */ 970 971 bool 972 check_narrowing (tree type, tree init, tsubst_flags_t complain, 973 bool const_only/*= false*/) 974 { 975 tree ftype = unlowered_expr_type (init); 976 bool ok = true; 977 REAL_VALUE_TYPE d; 978 979 if (((!warn_narrowing || !(complain & tf_warning)) 980 && cxx_dialect == cxx98) 981 || !ARITHMETIC_TYPE_P (type) 982 /* Don't emit bogus warnings with e.g. value-dependent trees. */ 983 || instantiation_dependent_expression_p (init)) 984 return ok; 985 986 if (BRACE_ENCLOSED_INITIALIZER_P (init) 987 && TREE_CODE (type) == COMPLEX_TYPE) 988 { 989 tree elttype = TREE_TYPE (type); 990 if (CONSTRUCTOR_NELTS (init) > 0) 991 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value, 992 complain); 993 if (CONSTRUCTOR_NELTS (init) > 1) 994 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value, 995 complain); 996 return ok; 997 } 998 999 /* Even non-dependent expressions can still have template 1000 codes like CAST_EXPR, so use *_non_dependent_expr to cope. */ 1001 init = fold_non_dependent_expr (init, complain, /*manifest*/true); 1002 if (init == error_mark_node) 1003 return ok; 1004 1005 /* If we were asked to only check constants, return early. */ 1006 if (const_only && !TREE_CONSTANT (init)) 1007 return ok; 1008 1009 if (CP_INTEGRAL_TYPE_P (type) 1010 && SCALAR_FLOAT_TYPE_P (ftype)) 1011 ok = false; 1012 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) 1013 && CP_INTEGRAL_TYPE_P (type)) 1014 { 1015 if (TREE_CODE (ftype) == ENUMERAL_TYPE) 1016 /* Check for narrowing based on the values of the enumeration. */ 1017 ftype = ENUM_UNDERLYING_TYPE (ftype); 1018 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type), 1019 TYPE_MAX_VALUE (ftype)) 1020 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype), 1021 TYPE_MIN_VALUE (type))) 1022 && (TREE_CODE (init) != INTEGER_CST 1023 || !int_fits_type_p (init, type))) 1024 ok = false; 1025 } 1026 /* [dcl.init.list]#7.2: "from long double to double or float, or from 1027 double to float". */ 1028 else if (SCALAR_FLOAT_TYPE_P (ftype) 1029 && SCALAR_FLOAT_TYPE_P (type)) 1030 { 1031 if ((extended_float_type_p (ftype) || extended_float_type_p (type)) 1032 ? /* "from a floating-point type T to another floating-point type 1033 whose floating-point conversion rank is neither greater than 1034 nor equal to that of T". 1035 So, it is ok if 1036 cp_compare_floating_point_conversion_ranks (ftype, type) 1037 returns -2 (type has greater conversion rank than ftype) 1038 or [-1..1] (type has equal conversion rank as ftype, possibly 1039 different subrank. Only do this if at least one of the 1040 types is extended floating-point type, otherwise keep doing 1041 what we did before (for the sake of non-standard 1042 backend types). */ 1043 cp_compare_floating_point_conversion_ranks (ftype, type) >= 2 1044 : ((same_type_p (ftype, long_double_type_node) 1045 && (same_type_p (type, double_type_node) 1046 || same_type_p (type, float_type_node))) 1047 || (same_type_p (ftype, double_type_node) 1048 && same_type_p (type, float_type_node)) 1049 || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)))) 1050 { 1051 if (TREE_CODE (init) == REAL_CST) 1052 { 1053 /* Issue 703: Loss of precision is OK as long as the value is 1054 within the representable range of the new type. */ 1055 REAL_VALUE_TYPE r; 1056 d = TREE_REAL_CST (init); 1057 real_convert (&r, TYPE_MODE (type), &d); 1058 if (real_isinf (&r)) 1059 ok = false; 1060 } 1061 else 1062 ok = false; 1063 } 1064 } 1065 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) 1066 && SCALAR_FLOAT_TYPE_P (type)) 1067 { 1068 ok = false; 1069 if (TREE_CODE (init) == INTEGER_CST) 1070 { 1071 d = real_value_from_int_cst (0, init); 1072 if (exact_real_truncate (TYPE_MODE (type), &d)) 1073 ok = true; 1074 } 1075 } 1076 else if (TREE_CODE (type) == BOOLEAN_TYPE 1077 && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype))) 1078 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member 1079 type to bool should be considered narrowing. This is a DR so is not 1080 limited to C++20 only. */ 1081 ok = false; 1082 1083 bool almost_ok = ok; 1084 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error)) 1085 { 1086 tree folded = cp_fully_fold (init); 1087 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none)) 1088 almost_ok = true; 1089 } 1090 1091 if (!ok) 1092 { 1093 location_t loc = cp_expr_loc_or_input_loc (init); 1094 if (cxx_dialect == cxx98) 1095 { 1096 if (complain & tf_warning) 1097 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE " 1098 "from %qH to %qI is ill-formed in C++11", 1099 init, ftype, type); 1100 ok = true; 1101 } 1102 else if (!CONSTANT_CLASS_P (init)) 1103 { 1104 if (complain & tf_warning_or_error) 1105 { 1106 auto_diagnostic_group d; 1107 if ((!almost_ok || pedantic) 1108 && pedwarn (loc, OPT_Wnarrowing, 1109 "narrowing conversion of %qE from %qH to %qI", 1110 init, ftype, type) 1111 && almost_ok) 1112 inform (loc, " the expression has a constant value but is not " 1113 "a C++ constant-expression"); 1114 ok = true; 1115 } 1116 } 1117 else if (complain & tf_error) 1118 { 1119 int savederrorcount = errorcount; 1120 permerror_opt (loc, OPT_Wnarrowing, 1121 "narrowing conversion of %qE from %qH to %qI", 1122 init, ftype, type); 1123 if (errorcount == savederrorcount) 1124 ok = true; 1125 } 1126 } 1127 1128 return ok; 1129 } 1130 1131 /* True iff TYPE is a C++20 "ordinary" character type. */ 1132 1133 bool 1134 ordinary_char_type_p (tree type) 1135 { 1136 type = TYPE_MAIN_VARIANT (type); 1137 return (type == char_type_node 1138 || type == signed_char_type_node 1139 || type == unsigned_char_type_node); 1140 } 1141 1142 /* True iff the string literal INIT has a type suitable for initializing array 1143 TYPE. */ 1144 1145 bool 1146 array_string_literal_compatible_p (tree type, tree init) 1147 { 1148 tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 1149 tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))); 1150 1151 if (to_char_type == from_char_type) 1152 return true; 1153 /* The array element type does not match the initializing string 1154 literal element type; this is only allowed when both types are 1155 ordinary character type. There are no string literals of 1156 signed or unsigned char type in the language, but we can get 1157 them internally from converting braced-init-lists to 1158 STRING_CST. */ 1159 if (ordinary_char_type_p (to_char_type) 1160 && ordinary_char_type_p (from_char_type)) 1161 return true; 1162 1163 /* P2513 (C++20/C++23): "an array of char or unsigned char may 1164 be initialized by a UTF-8 string literal, or by such a string 1165 literal enclosed in braces." */ 1166 if (from_char_type == char8_type_node 1167 && (to_char_type == char_type_node 1168 || to_char_type == unsigned_char_type_node)) 1169 return true; 1170 1171 return false; 1172 } 1173 1174 /* Process the initializer INIT for a variable of type TYPE, emitting 1175 diagnostics for invalid initializers and converting the initializer as 1176 appropriate. 1177 1178 For aggregate types, it assumes that reshape_init has already run, thus the 1179 initializer will have the right shape (brace elision has been undone). 1180 1181 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR, 1182 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */ 1183 1184 static tree 1185 digest_init_r (tree type, tree init, int nested, int flags, 1186 tsubst_flags_t complain) 1187 { 1188 enum tree_code code = TREE_CODE (type); 1189 1190 if (error_operand_p (init)) 1191 return error_mark_node; 1192 1193 gcc_assert (init); 1194 1195 /* We must strip the outermost array type when completing the type, 1196 because the its bounds might be incomplete at the moment. */ 1197 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE 1198 ? TREE_TYPE (type) : type, NULL_TREE, 1199 complain)) 1200 return error_mark_node; 1201 1202 location_t loc = cp_expr_loc_or_input_loc (init); 1203 1204 tree stripped_init = init; 1205 1206 if (BRACE_ENCLOSED_INITIALIZER_P (init) 1207 && CONSTRUCTOR_IS_PAREN_INIT (init)) 1208 flags |= LOOKUP_AGGREGATE_PAREN_INIT; 1209 1210 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue 1211 (g++.old-deja/g++.law/casts2.C). */ 1212 if (TREE_CODE (init) == NON_LVALUE_EXPR) 1213 stripped_init = TREE_OPERAND (init, 0); 1214 1215 stripped_init = tree_strip_any_location_wrapper (stripped_init); 1216 1217 /* Initialization of an array of chars from a string constant. The initializer 1218 can be optionally enclosed in braces, but reshape_init has already removed 1219 them if they were present. */ 1220 if (code == ARRAY_TYPE) 1221 { 1222 if (nested && !TYPE_DOMAIN (type)) 1223 /* C++ flexible array members have a null domain. */ 1224 { 1225 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT) 1226 pedwarn (loc, OPT_Wpedantic, 1227 "initialization of a flexible array member"); 1228 else 1229 { 1230 if (complain & tf_error) 1231 error_at (loc, "non-static initialization of" 1232 " a flexible array member"); 1233 return error_mark_node; 1234 } 1235 } 1236 1237 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 1238 if (char_type_p (typ1) 1239 && TREE_CODE (stripped_init) == STRING_CST) 1240 { 1241 if (!array_string_literal_compatible_p (type, init)) 1242 { 1243 if (complain & tf_error) 1244 error_at (loc, "cannot initialize array of %qT from " 1245 "a string literal with type array of %qT", 1246 typ1, 1247 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)))); 1248 return error_mark_node; 1249 } 1250 1251 if (nested == 2 && !TYPE_DOMAIN (type)) 1252 { 1253 if (complain & tf_error) 1254 error_at (loc, "initialization of flexible array member " 1255 "in a nested context"); 1256 return error_mark_node; 1257 } 1258 1259 if (type != TREE_TYPE (init) 1260 && !variably_modified_type_p (type, NULL_TREE)) 1261 { 1262 init = copy_node (init); 1263 TREE_TYPE (init) = type; 1264 /* If we have a location wrapper, then also copy the wrapped 1265 node, and update the copy's type. */ 1266 if (location_wrapper_p (init)) 1267 { 1268 stripped_init = copy_node (stripped_init); 1269 TREE_OPERAND (init, 0) = stripped_init; 1270 TREE_TYPE (stripped_init) = type; 1271 } 1272 } 1273 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type))) 1274 { 1275 /* Not a flexible array member. */ 1276 int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); 1277 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; 1278 /* In C it is ok to subtract 1 from the length of the string 1279 because it's ok to ignore the terminating null char that is 1280 counted in the length of the constant, but in C++ this would 1281 be invalid. */ 1282 if (size < TREE_STRING_LENGTH (stripped_init)) 1283 { 1284 permerror (loc, "initializer-string for %qT is too long", 1285 type); 1286 1287 init = build_string (size, 1288 TREE_STRING_POINTER (stripped_init)); 1289 TREE_TYPE (init) = type; 1290 } 1291 } 1292 return init; 1293 } 1294 } 1295 1296 /* Handle scalar types (including conversions) and references. */ 1297 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) 1298 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE)) 1299 { 1300 /* Narrowing is OK when initializing an aggregate from 1301 a parenthesized list. */ 1302 if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT)) 1303 flags |= LOOKUP_NO_NARROWING; 1304 init = convert_for_initialization (0, type, init, flags, 1305 ICR_INIT, NULL_TREE, 0, 1306 complain); 1307 1308 return init; 1309 } 1310 1311 /* Come here only for aggregates: records, arrays, unions, complex numbers 1312 and vectors. */ 1313 gcc_assert (code == ARRAY_TYPE 1314 || VECTOR_TYPE_P (type) 1315 || code == RECORD_TYPE 1316 || code == UNION_TYPE 1317 || code == OPAQUE_TYPE 1318 || code == COMPLEX_TYPE); 1319 1320 /* "If T is a class type and the initializer list has a single 1321 element of type cv U, where U is T or a class derived from T, 1322 the object is initialized from that element." */ 1323 if (cxx_dialect >= cxx11 1324 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init) 1325 && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init) 1326 && CONSTRUCTOR_NELTS (stripped_init) == 1 1327 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type)) 1328 || VECTOR_TYPE_P (type))) 1329 { 1330 tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value; 1331 if (reference_related_p (type, TREE_TYPE (elt))) 1332 { 1333 /* In C++17, aggregates can have bases, thus participate in 1334 aggregate initialization. In the following case: 1335 1336 struct B { int c; }; 1337 struct D : B { }; 1338 D d{{D{{42}}}}; 1339 1340 there's an extra set of braces, so the D temporary initializes 1341 the first element of d, which is the B base subobject. The base 1342 of type B is copy-initialized from the D temporary, causing 1343 object slicing. */ 1344 tree field = next_aggregate_field (TYPE_FIELDS (type)); 1345 if (field && DECL_FIELD_IS_BASE (field)) 1346 { 1347 if (warning_at (loc, 0, "initializing a base class of type %qT " 1348 "results in object slicing", TREE_TYPE (field))) 1349 inform (loc, "remove %<{ }%> around initializer"); 1350 } 1351 else if (flag_checking) 1352 /* We should have fixed this in reshape_init. */ 1353 gcc_unreachable (); 1354 } 1355 } 1356 1357 if (SIMPLE_TARGET_EXPR_P (stripped_init)) 1358 stripped_init = TARGET_EXPR_INITIAL (stripped_init); 1359 1360 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init) 1361 && !TYPE_NON_AGGREGATE_CLASS (type)) 1362 return process_init_constructor (type, stripped_init, nested, flags, 1363 complain); 1364 else 1365 { 1366 if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE) 1367 { 1368 if (complain & tf_error) 1369 error_at (loc, "cannot initialize aggregate of type %qT with " 1370 "a compound literal", type); 1371 1372 return error_mark_node; 1373 } 1374 1375 if (code == ARRAY_TYPE 1376 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) 1377 { 1378 /* Allow the result of build_array_copy and of 1379 build_value_init_noctor. */ 1380 if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR 1381 || TREE_CODE (stripped_init) == CONSTRUCTOR) 1382 && (same_type_ignoring_top_level_qualifiers_p 1383 (type, TREE_TYPE (init)))) 1384 return init; 1385 1386 if (complain & tf_error) 1387 error_at (loc, "array must be initialized with a brace-enclosed" 1388 " initializer"); 1389 return error_mark_node; 1390 } 1391 1392 return convert_for_initialization (NULL_TREE, type, init, 1393 flags, 1394 ICR_INIT, NULL_TREE, 0, 1395 complain); 1396 } 1397 } 1398 1399 tree 1400 digest_init (tree type, tree init, tsubst_flags_t complain) 1401 { 1402 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain); 1403 } 1404 1405 tree 1406 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain) 1407 { 1408 return digest_init_r (type, init, 0, flags, complain); 1409 } 1410 1411 /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used 1412 in the context of guaranteed copy elision). */ 1413 1414 static tree 1415 replace_placeholders_for_class_temp_r (tree *tp, int *, void *data) 1416 { 1417 tree t = *tp; 1418 auto pset = static_cast<hash_set<tree> *>(data); 1419 1420 /* We're looking for a TARGET_EXPR nested in the whole expression. */ 1421 if (TREE_CODE (t) == TARGET_EXPR 1422 /* That serves as temporary materialization, not an initializer. */ 1423 && !TARGET_EXPR_ELIDING_P (t) 1424 && !pset->add (t)) 1425 { 1426 tree init = TARGET_EXPR_INITIAL (t); 1427 while (TREE_CODE (init) == COMPOUND_EXPR) 1428 init = TREE_OPERAND (init, 1); 1429 if (TREE_CODE (init) == CONSTRUCTOR 1430 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)) 1431 { 1432 tree obj = TARGET_EXPR_SLOT (t); 1433 replace_placeholders (init, obj); 1434 /* We should have dealt with all PLACEHOLDER_EXPRs. */ 1435 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = false; 1436 gcc_checking_assert (!find_placeholders (init)); 1437 } 1438 } 1439 /* TARGET_EXPRs initializing function arguments are not marked as eliding, 1440 even though gimplify_arg drops them on the floor. Don't go replacing 1441 placeholders in them. */ 1442 else if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR) 1443 for (int i = 0; i < call_expr_nargs (t); ++i) 1444 { 1445 tree arg = get_nth_callarg (t, i); 1446 if (TREE_CODE (arg) == TARGET_EXPR && !TARGET_EXPR_ELIDING_P (arg)) 1447 pset->add (arg); 1448 } 1449 1450 return NULL_TREE; 1451 } 1452 1453 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */ 1454 tree 1455 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain) 1456 { 1457 gcc_assert (TREE_CODE (decl) == FIELD_DECL); 1458 1459 tree type = TREE_TYPE (decl); 1460 if (DECL_BIT_FIELD_TYPE (decl)) 1461 type = DECL_BIT_FIELD_TYPE (decl); 1462 int flags = LOOKUP_IMPLICIT; 1463 if (DIRECT_LIST_INIT_P (init)) 1464 { 1465 flags = LOOKUP_NORMAL; 1466 complain |= tf_no_cleanup; 1467 } 1468 if (BRACE_ENCLOSED_INITIALIZER_P (init) 1469 && CP_AGGREGATE_TYPE_P (type)) 1470 init = reshape_init (type, init, complain); 1471 init = digest_init_flags (type, init, flags, complain); 1472 set_target_expr_eliding (init); 1473 1474 /* We may have temporary materialization in a NSDMI, if the initializer 1475 has something like A{} in it. Digesting the {} could have introduced 1476 a PLACEHOLDER_EXPR referring to A. Now that we've got a TARGET_EXPR, 1477 we have an object we can refer to. The reason we bother doing this 1478 here is for code like 1479 1480 struct A { 1481 int x; 1482 int y = x; 1483 }; 1484 1485 struct B { 1486 int x = 0; 1487 int y = A{x}.y; // #1 1488 }; 1489 1490 where in #1 we don't want to end up with two PLACEHOLDER_EXPRs for 1491 different types on the same level in a {} when lookup_placeholder 1492 wouldn't find a named object for the PLACEHOLDER_EXPR for A. Note, 1493 temporary materialization does not occur when initializing an object 1494 from a prvalue of the same type, therefore we must not replace the 1495 placeholder with a temporary object so that it can be elided. */ 1496 hash_set<tree> pset; 1497 cp_walk_tree (&init, replace_placeholders_for_class_temp_r, &pset, nullptr); 1498 1499 return init; 1500 } 1501 1502 /* Set of flags used within process_init_constructor to describe the 1504 initializers. */ 1505 #define PICFLAG_ERRONEOUS 1 1506 #define PICFLAG_NOT_ALL_CONSTANT 2 1507 #define PICFLAG_NOT_ALL_SIMPLE 4 1508 #define PICFLAG_SIDE_EFFECTS 8 1509 #define PICFLAG_VEC_INIT 16 1510 1511 /* Given an initializer INIT, return the flag (PICFLAG_*) which better 1512 describe it. */ 1513 1514 static int 1515 picflag_from_initializer (tree init) 1516 { 1517 if (init == error_mark_node) 1518 return PICFLAG_ERRONEOUS; 1519 else if (!TREE_CONSTANT (init)) 1520 { 1521 if (TREE_SIDE_EFFECTS (init)) 1522 return PICFLAG_SIDE_EFFECTS; 1523 else 1524 return PICFLAG_NOT_ALL_CONSTANT; 1525 } 1526 else if (!initializer_constant_valid_p (init, TREE_TYPE (init))) 1527 return PICFLAG_NOT_ALL_SIMPLE; 1528 return 0; 1529 } 1530 1531 /* Adjust INIT for going into a CONSTRUCTOR. */ 1532 1533 static tree 1534 massage_init_elt (tree type, tree init, int nested, int flags, 1535 tsubst_flags_t complain) 1536 { 1537 int new_flags = LOOKUP_IMPLICIT; 1538 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT) 1539 new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT; 1540 if (flags & LOOKUP_AGGREGATE_PAREN_INIT) 1541 new_flags |= LOOKUP_AGGREGATE_PAREN_INIT; 1542 init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain); 1543 /* When we defer constant folding within a statement, we may want to 1544 defer this folding as well. Don't call this on CONSTRUCTORs in 1545 a template because their elements have already been folded, and 1546 we must avoid folding the result of get_nsdmi. */ 1547 if (!(processing_template_decl && TREE_CODE (init) == CONSTRUCTOR)) 1548 { 1549 tree t = fold_non_dependent_init (init, complain); 1550 if (TREE_CONSTANT (t)) 1551 init = t; 1552 set_target_expr_eliding (init); 1553 } 1554 return init; 1555 } 1556 1557 /* Subroutine of process_init_constructor, which will process an initializer 1558 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*) 1559 which describe the initializers. */ 1560 1561 static int 1562 process_init_constructor_array (tree type, tree init, int nested, int flags, 1563 tsubst_flags_t complain) 1564 { 1565 unsigned HOST_WIDE_INT i, len = 0; 1566 int picflags = 0; 1567 bool unbounded = false; 1568 constructor_elt *ce; 1569 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init); 1570 1571 gcc_assert (TREE_CODE (type) == ARRAY_TYPE 1572 || VECTOR_TYPE_P (type)); 1573 1574 if (TREE_CODE (type) == ARRAY_TYPE) 1575 { 1576 /* C++ flexible array members have a null domain. */ 1577 tree domain = TYPE_DOMAIN (type); 1578 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain))) 1579 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain)) 1580 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1, 1581 TYPE_PRECISION (TREE_TYPE (domain)), 1582 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi (); 1583 else 1584 unbounded = true; /* Take as many as there are. */ 1585 1586 if (nested == 2 && !domain && !vec_safe_is_empty (v)) 1587 { 1588 if (complain & tf_error) 1589 error_at (cp_expr_loc_or_input_loc (init), 1590 "initialization of flexible array member " 1591 "in a nested context"); 1592 return PICFLAG_ERRONEOUS; 1593 } 1594 } 1595 else 1596 /* Vectors are like simple fixed-size arrays. */ 1597 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len); 1598 1599 /* There must not be more initializers than needed. */ 1600 if (!unbounded && vec_safe_length (v) > len) 1601 { 1602 if (complain & tf_error) 1603 error ("too many initializers for %qT", type); 1604 else 1605 return PICFLAG_ERRONEOUS; 1606 } 1607 1608 FOR_EACH_VEC_SAFE_ELT (v, i, ce) 1609 { 1610 if (!ce->index) 1611 ce->index = size_int (i); 1612 else if (!check_array_designated_initializer (ce, i)) 1613 ce->index = error_mark_node; 1614 gcc_assert (ce->value); 1615 ce->value 1616 = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags, 1617 complain); 1618 1619 gcc_checking_assert 1620 (ce->value == error_mark_node 1621 || (same_type_ignoring_top_level_qualifiers_p 1622 (strip_array_types (TREE_TYPE (type)), 1623 strip_array_types (TREE_TYPE (ce->value))))); 1624 1625 picflags |= picflag_from_initializer (ce->value); 1626 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer 1627 CONSTRUCTOR. */ 1628 if (TREE_CODE (ce->value) == CONSTRUCTOR 1629 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value)) 1630 { 1631 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; 1632 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0; 1633 } 1634 } 1635 1636 /* No more initializers. If the array is unbounded, we are done. Otherwise, 1637 we must add initializers ourselves. */ 1638 if (!unbounded) 1639 for (; i < len; ++i) 1640 { 1641 tree next; 1642 1643 if (type_build_ctor_call (TREE_TYPE (type))) 1644 { 1645 /* If this type needs constructors run for default-initialization, 1646 we can't rely on the back end to do it for us, so make the 1647 initialization explicit by list-initializing from T{}. */ 1648 next = build_constructor (init_list_type_node, NULL); 1649 next = massage_init_elt (TREE_TYPE (type), next, nested, flags, 1650 complain); 1651 if (initializer_zerop (next)) 1652 /* The default zero-initialization is fine for us; don't 1653 add anything to the CONSTRUCTOR. */ 1654 next = NULL_TREE; 1655 } 1656 else if (!zero_init_p (TREE_TYPE (type))) 1657 next = build_zero_init (TREE_TYPE (type), 1658 /*nelts=*/NULL_TREE, 1659 /*static_storage_p=*/false); 1660 else 1661 /* The default zero-initialization is fine for us; don't 1662 add anything to the CONSTRUCTOR. */ 1663 next = NULL_TREE; 1664 1665 if (next) 1666 { 1667 if (next != error_mark_node 1668 && (initializer_constant_valid_p (next, TREE_TYPE (next)) 1669 != null_pointer_node)) 1670 { 1671 /* Use VEC_INIT_EXPR for non-constant initialization of 1672 trailing elements with no explicit initializers. */ 1673 picflags |= PICFLAG_VEC_INIT; 1674 break; 1675 } 1676 1677 picflags |= picflag_from_initializer (next); 1678 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer 1679 CONSTRUCTOR. */ 1680 if (TREE_CODE (next) == CONSTRUCTOR 1681 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next)) 1682 { 1683 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; 1684 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0; 1685 } 1686 if (len > i+1) 1687 { 1688 tree range = build2 (RANGE_EXPR, size_type_node, 1689 build_int_cst (size_type_node, i), 1690 build_int_cst (size_type_node, len - 1)); 1691 CONSTRUCTOR_APPEND_ELT (v, range, next); 1692 break; 1693 } 1694 else 1695 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next); 1696 } 1697 else 1698 /* Don't bother checking all the other elements. */ 1699 break; 1700 } 1701 1702 CONSTRUCTOR_ELTS (init) = v; 1703 return picflags; 1704 } 1705 1706 /* Subroutine of process_init_constructor, which will process an initializer 1707 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe 1708 the initializers. */ 1709 1710 static int 1711 process_init_constructor_record (tree type, tree init, int nested, int flags, 1712 tsubst_flags_t complain) 1713 { 1714 vec<constructor_elt, va_gc> *v = NULL; 1715 tree field; 1716 int skipped = 0; 1717 1718 gcc_assert (TREE_CODE (type) == RECORD_TYPE); 1719 gcc_assert (!CLASSTYPE_VBASECLASSES (type)); 1720 gcc_assert (!TYPE_BINFO (type) 1721 || cxx_dialect >= cxx17 1722 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type))); 1723 gcc_assert (!TYPE_POLYMORPHIC_P (type)); 1724 1725 restart: 1726 int picflags = 0; 1727 unsigned HOST_WIDE_INT idx = 0; 1728 int designator_skip = -1; 1729 /* Generally, we will always have an index for each initializer (which is 1730 a FIELD_DECL, put by reshape_init), but compound literals don't go trough 1731 reshape_init. So we need to handle both cases. */ 1732 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 1733 { 1734 tree next; 1735 1736 if (TREE_CODE (field) != FIELD_DECL 1737 || (DECL_ARTIFICIAL (field) 1738 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))) 1739 continue; 1740 1741 if (DECL_UNNAMED_BIT_FIELD (field)) 1742 continue; 1743 1744 /* If this is a bitfield, first convert to the declared type. */ 1745 tree fldtype = TREE_TYPE (field); 1746 if (DECL_BIT_FIELD_TYPE (field)) 1747 fldtype = DECL_BIT_FIELD_TYPE (field); 1748 if (fldtype == error_mark_node) 1749 return PICFLAG_ERRONEOUS; 1750 1751 next = NULL_TREE; 1752 if (idx < CONSTRUCTOR_NELTS (init)) 1753 { 1754 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx]; 1755 if (ce->index) 1756 { 1757 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The 1758 latter case can happen in templates where lookup has to be 1759 deferred. */ 1760 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL 1761 || identifier_p (ce->index)); 1762 if (ce->index == field || ce->index == DECL_NAME (field)) 1763 next = ce->value; 1764 else 1765 { 1766 ce = NULL; 1767 if (designator_skip == -1) 1768 designator_skip = 1; 1769 } 1770 } 1771 else 1772 { 1773 designator_skip = 0; 1774 next = ce->value; 1775 } 1776 1777 if (ce) 1778 { 1779 gcc_assert (ce->value); 1780 next = massage_init_elt (fldtype, next, nested, flags, complain); 1781 ++idx; 1782 } 1783 } 1784 if (next == error_mark_node) 1785 /* We skip initializers for empty bases/fields, so skipping an invalid 1786 one could make us accept invalid code. */ 1787 return PICFLAG_ERRONEOUS; 1788 else if (next) 1789 /* Already handled above. */; 1790 else if (DECL_INITIAL (field)) 1791 { 1792 if (skipped > 0) 1793 { 1794 /* We're using an NSDMI past a field with implicit 1795 zero-init. Go back and make it explicit. */ 1796 skipped = -1; 1797 vec_safe_truncate (v, 0); 1798 goto restart; 1799 } 1800 /* C++14 aggregate NSDMI. */ 1801 next = get_nsdmi (field, /*ctor*/false, complain); 1802 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) 1803 && find_placeholders (next)) 1804 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; 1805 } 1806 else if (type_build_ctor_call (fldtype)) 1807 { 1808 /* If this type needs constructors run for 1809 default-initialization, we can't rely on the back end to do it 1810 for us, so build up TARGET_EXPRs. If the type in question is 1811 a class, just build one up; if it's an array, recurse. */ 1812 next = build_constructor (init_list_type_node, NULL); 1813 next = massage_init_elt (fldtype, next, nested, flags, complain); 1814 if (TREE_CODE (next) == TARGET_EXPR 1815 && unsafe_copy_elision_p (field, next)) 1816 TARGET_EXPR_ELIDING_P (next) = false; 1817 1818 /* Warn when some struct elements are implicitly initialized. */ 1819 if ((complain & tf_warning) 1820 && !cp_unevaluated_operand 1821 && !EMPTY_CONSTRUCTOR_P (init)) 1822 warning (OPT_Wmissing_field_initializers, 1823 "missing initializer for member %qD", field); 1824 } 1825 else 1826 { 1827 if (TYPE_REF_P (fldtype)) 1828 { 1829 if (complain & tf_error) 1830 error ("member %qD is uninitialized reference", field); 1831 else 1832 return PICFLAG_ERRONEOUS; 1833 } 1834 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype)) 1835 { 1836 if (complain & tf_error) 1837 error ("member %qD with uninitialized reference fields", field); 1838 else 1839 return PICFLAG_ERRONEOUS; 1840 } 1841 /* Do nothing for flexible array members since they need not have any 1842 elements. Don't worry about 'skipped' because a flexarray has to 1843 be the last field. */ 1844 else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype)) 1845 continue; 1846 1847 /* Warn when some struct elements are implicitly initialized 1848 to zero. */ 1849 if ((complain & tf_warning) 1850 && !cp_unevaluated_operand 1851 && !EMPTY_CONSTRUCTOR_P (init) 1852 && !is_really_empty_class (fldtype, /*ignore_vptr*/false)) 1853 warning (OPT_Wmissing_field_initializers, 1854 "missing initializer for member %qD", field); 1855 1856 if (!zero_init_p (fldtype) || skipped < 0) 1857 { 1858 if (TYPE_REF_P (fldtype)) 1859 next = build_zero_cst (fldtype); 1860 else 1861 next = build_zero_init (fldtype, /*nelts=*/NULL_TREE, 1862 /*static_storage_p=*/false); 1863 } 1864 else 1865 { 1866 /* The default zero-initialization is fine for us; don't 1867 add anything to the CONSTRUCTOR. */ 1868 skipped = 1; 1869 continue; 1870 } 1871 } 1872 1873 /* We can't actually elide the temporary when initializing a 1874 potentially-overlapping field from a function that returns by 1875 value. */ 1876 if (TREE_CODE (next) == TARGET_EXPR 1877 && unsafe_copy_elision_p (field, next)) 1878 TARGET_EXPR_ELIDING_P (next) = false; 1879 1880 if (is_empty_field (field) 1881 && !TREE_SIDE_EFFECTS (next)) 1882 /* Don't add trivial initialization of an empty base/field to the 1883 constructor, as they might not be ordered the way the back-end 1884 expects. */ 1885 continue; 1886 1887 /* If this is a bitfield, now convert to the lowered type. */ 1888 if (fldtype != TREE_TYPE (field)) 1889 next = cp_convert_and_check (TREE_TYPE (field), next, complain); 1890 picflags |= picflag_from_initializer (next); 1891 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */ 1892 if (TREE_CODE (next) == CONSTRUCTOR 1893 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next)) 1894 { 1895 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; 1896 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0; 1897 } 1898 CONSTRUCTOR_APPEND_ELT (v, field, next); 1899 } 1900 1901 if (idx < CONSTRUCTOR_NELTS (init)) 1902 { 1903 if (complain & tf_error) 1904 { 1905 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx]; 1906 /* For better diagnostics, try to find out if it is really 1907 the case of too many initializers or if designators are 1908 in incorrect order. */ 1909 if (designator_skip == 1 && ce->index) 1910 { 1911 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL 1912 || identifier_p (ce->index)); 1913 for (field = TYPE_FIELDS (type); 1914 field; field = DECL_CHAIN (field)) 1915 { 1916 if (TREE_CODE (field) != FIELD_DECL 1917 || (DECL_ARTIFICIAL (field) 1918 && !(cxx_dialect >= cxx17 1919 && DECL_FIELD_IS_BASE (field)))) 1920 continue; 1921 1922 if (DECL_UNNAMED_BIT_FIELD (field)) 1923 continue; 1924 1925 if (ce->index == field || ce->index == DECL_NAME (field)) 1926 break; 1927 } 1928 } 1929 if (field) 1930 error ("designator order for field %qD does not match declaration " 1931 "order in %qT", field, type); 1932 else 1933 error ("too many initializers for %qT", type); 1934 } 1935 else 1936 return PICFLAG_ERRONEOUS; 1937 } 1938 1939 CONSTRUCTOR_ELTS (init) = v; 1940 return picflags; 1941 } 1942 1943 /* Subroutine of process_init_constructor, which will process a single 1944 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*) 1945 which describe the initializer. */ 1946 1947 static int 1948 process_init_constructor_union (tree type, tree init, int nested, int flags, 1949 tsubst_flags_t complain) 1950 { 1951 constructor_elt *ce; 1952 int len; 1953 1954 /* If the initializer was empty, use the union's NSDMI if it has one. 1955 Otherwise use default zero initialization. */ 1956 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) 1957 { 1958 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 1959 { 1960 if (TREE_CODE (field) == FIELD_DECL 1961 && DECL_INITIAL (field) != NULL_TREE) 1962 { 1963 tree val = get_nsdmi (field, /*in_ctor=*/false, complain); 1964 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) 1965 && find_placeholders (val)) 1966 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; 1967 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val); 1968 break; 1969 } 1970 } 1971 1972 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) 1973 return 0; 1974 } 1975 1976 len = CONSTRUCTOR_ELTS (init)->length (); 1977 if (len > 1) 1978 { 1979 if (!(complain & tf_error)) 1980 return PICFLAG_ERRONEOUS; 1981 error ("too many initializers for %qT", type); 1982 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1); 1983 } 1984 1985 ce = &(*CONSTRUCTOR_ELTS (init))[0]; 1986 1987 /* If this element specifies a field, initialize via that field. */ 1988 if (ce->index) 1989 { 1990 if (TREE_CODE (ce->index) == FIELD_DECL) 1991 ; 1992 else if (identifier_p (ce->index)) 1993 { 1994 /* This can happen within a cast, see g++.dg/opt/cse2.C. */ 1995 tree name = ce->index; 1996 tree field; 1997 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 1998 if (DECL_NAME (field) == name) 1999 break; 2000 if (!field) 2001 { 2002 if (complain & tf_error) 2003 error ("no field %qD found in union being initialized", 2004 field); 2005 ce->value = error_mark_node; 2006 } 2007 ce->index = field; 2008 } 2009 else 2010 { 2011 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST 2012 || TREE_CODE (ce->index) == RANGE_EXPR); 2013 if (complain & tf_error) 2014 error ("index value instead of field name in union initializer"); 2015 ce->value = error_mark_node; 2016 } 2017 } 2018 else 2019 { 2020 /* Find the first named field. ANSI decided in September 1990 2021 that only named fields count here. */ 2022 tree field = TYPE_FIELDS (type); 2023 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL)) 2024 field = TREE_CHAIN (field); 2025 if (field == NULL_TREE) 2026 { 2027 if (complain & tf_error) 2028 error ("too many initializers for %qT", type); 2029 ce->value = error_mark_node; 2030 } 2031 ce->index = field; 2032 } 2033 2034 if (ce->value && ce->value != error_mark_node) 2035 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested, 2036 flags, complain); 2037 2038 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */ 2039 if (ce->value 2040 && TREE_CODE (ce->value) == CONSTRUCTOR 2041 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value)) 2042 { 2043 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; 2044 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0; 2045 } 2046 return picflag_from_initializer (ce->value); 2047 } 2048 2049 /* Process INIT, a constructor for a variable of aggregate type TYPE. The 2050 constructor is a brace-enclosed initializer, and will be modified in-place. 2051 2052 Each element is converted to the right type through digest_init, and 2053 missing initializers are added following the language rules (zero-padding, 2054 etc.). 2055 2056 After the execution, the initializer will have TREE_CONSTANT if all elts are 2057 constant, and TREE_STATIC set if, in addition, all elts are simple enough 2058 constants that the assembler and linker can compute them. 2059 2060 The function returns the initializer itself, or error_mark_node in case 2061 of error. */ 2062 2063 static tree 2064 process_init_constructor (tree type, tree init, int nested, int flags, 2065 tsubst_flags_t complain) 2066 { 2067 int picflags; 2068 2069 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); 2070 2071 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type)) 2072 picflags = process_init_constructor_array (type, init, nested, flags, 2073 complain); 2074 else if (TREE_CODE (type) == RECORD_TYPE) 2075 picflags = process_init_constructor_record (type, init, nested, flags, 2076 complain); 2077 else if (TREE_CODE (type) == UNION_TYPE) 2078 picflags = process_init_constructor_union (type, init, nested, flags, 2079 complain); 2080 else 2081 gcc_unreachable (); 2082 2083 if (picflags & PICFLAG_ERRONEOUS) 2084 return error_mark_node; 2085 2086 TREE_TYPE (init) = type; 2087 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE) 2088 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0); 2089 if (picflags & PICFLAG_SIDE_EFFECTS) 2090 { 2091 TREE_CONSTANT (init) = false; 2092 TREE_SIDE_EFFECTS (init) = true; 2093 } 2094 else if (picflags & PICFLAG_NOT_ALL_CONSTANT) 2095 { 2096 /* Make sure TREE_CONSTANT isn't set from build_constructor. */ 2097 TREE_CONSTANT (init) = false; 2098 TREE_SIDE_EFFECTS (init) = false; 2099 } 2100 else 2101 { 2102 TREE_CONSTANT (init) = 1; 2103 TREE_SIDE_EFFECTS (init) = false; 2104 if (!(picflags & PICFLAG_NOT_ALL_SIMPLE)) 2105 TREE_STATIC (init) = 1; 2106 } 2107 if (picflags & PICFLAG_VEC_INIT) 2108 { 2109 /* Defer default-initialization of array elements with no corresponding 2110 initializer-clause until later so we can use a loop. */ 2111 TREE_TYPE (init) = init_list_type_node; 2112 init = build_vec_init_expr (type, init, complain); 2113 init = get_target_expr (init); 2114 } 2115 return init; 2116 } 2117 2118 /* Given a structure or union value DATUM, construct and return 2120 the structure or union component which results from narrowing 2121 that value to the base specified in BASETYPE. For example, given the 2122 hierarchy 2123 2124 class L { int ii; }; 2125 class A : L { ... }; 2126 class B : L { ... }; 2127 class C : A, B { ... }; 2128 2129 and the declaration 2130 2131 C x; 2132 2133 then the expression 2134 2135 x.A::ii refers to the ii member of the L part of 2136 the A part of the C object named by X. In this case, 2137 DATUM would be x, and BASETYPE would be A. 2138 2139 I used to think that this was nonconformant, that the standard specified 2140 that first we look up ii in A, then convert x to an L& and pull out the 2141 ii part. But in fact, it does say that we convert x to an A&; A here 2142 is known as the "naming class". (jason 2000-12-19) 2143 2144 BINFO_P points to a variable initialized either to NULL_TREE or to the 2145 binfo for the specific base subobject we want to convert to. */ 2146 2147 tree 2148 build_scoped_ref (tree datum, tree basetype, tree* binfo_p) 2149 { 2150 tree binfo; 2151 2152 if (datum == error_mark_node) 2153 return error_mark_node; 2154 if (*binfo_p) 2155 binfo = *binfo_p; 2156 else 2157 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, 2158 NULL, tf_warning_or_error); 2159 2160 if (!binfo || binfo == error_mark_node) 2161 { 2162 *binfo_p = NULL_TREE; 2163 if (!binfo) 2164 error_not_base_type (basetype, TREE_TYPE (datum)); 2165 return error_mark_node; 2166 } 2167 2168 *binfo_p = binfo; 2169 return build_base_path (PLUS_EXPR, datum, binfo, 1, 2170 tf_warning_or_error); 2171 } 2172 2173 /* Build a reference to an object specified by the C++ `->' operator. 2174 Usually this just involves dereferencing the object, but if the 2175 `->' operator is overloaded, then such overloads must be 2176 performed until an object which does not have the `->' operator 2177 overloaded is found. An error is reported when circular pointer 2178 delegation is detected. */ 2179 2180 tree 2181 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain) 2182 { 2183 tree orig_expr = expr; 2184 tree type = TREE_TYPE (expr); 2185 tree last_rval = NULL_TREE; 2186 vec<tree, va_gc> *types_memoized = NULL; 2187 2188 if (type == error_mark_node) 2189 return error_mark_node; 2190 2191 if (processing_template_decl) 2192 { 2193 tree ttype = NULL_TREE; 2194 if (type && TYPE_PTR_P (type)) 2195 ttype = TREE_TYPE (type); 2196 if (ttype && !dependent_scope_p (ttype)) 2197 /* Pointer to current instantiation, don't treat as dependent. */; 2198 else if (type_dependent_expression_p (expr)) 2199 { 2200 expr = build_min_nt_loc (loc, ARROW_EXPR, expr); 2201 TREE_TYPE (expr) = ttype; 2202 return expr; 2203 } 2204 } 2205 2206 if (MAYBE_CLASS_TYPE_P (type)) 2207 { 2208 struct tinst_level *actual_inst = current_instantiation (); 2209 tree fn = NULL; 2210 2211 while ((expr = build_new_op (loc, COMPONENT_REF, 2212 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE, 2213 NULL_TREE, &fn, complain))) 2214 { 2215 if (expr == error_mark_node) 2216 return error_mark_node; 2217 2218 /* This provides a better instantiation backtrace in case of 2219 error. */ 2220 if (fn && DECL_USE_TEMPLATE (fn)) 2221 push_tinst_level_loc (fn, 2222 (current_instantiation () != actual_inst) 2223 ? DECL_SOURCE_LOCATION (fn) 2224 : input_location); 2225 fn = NULL; 2226 2227 if (vec_member (TREE_TYPE (expr), types_memoized)) 2228 { 2229 if (complain & tf_error) 2230 error ("circular pointer delegation detected"); 2231 return error_mark_node; 2232 } 2233 2234 vec_safe_push (types_memoized, TREE_TYPE (expr)); 2235 last_rval = expr; 2236 } 2237 2238 while (current_instantiation () != actual_inst) 2239 pop_tinst_level (); 2240 2241 if (last_rval == NULL_TREE) 2242 { 2243 if (complain & tf_error) 2244 error ("base operand of %<->%> has non-pointer type %qT", type); 2245 return error_mark_node; 2246 } 2247 2248 if (TYPE_REF_P (TREE_TYPE (last_rval))) 2249 last_rval = convert_from_reference (last_rval); 2250 } 2251 else 2252 { 2253 last_rval = decay_conversion (expr, complain); 2254 if (last_rval == error_mark_node) 2255 return error_mark_node; 2256 } 2257 2258 if (TYPE_PTR_P (TREE_TYPE (last_rval))) 2259 { 2260 if (processing_template_decl) 2261 { 2262 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)), 2263 orig_expr); 2264 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval); 2265 return expr; 2266 } 2267 2268 return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain); 2269 } 2270 2271 if (complain & tf_error) 2272 { 2273 if (types_memoized) 2274 error ("result of %<operator->()%> yields non-pointer result"); 2275 else 2276 error ("base operand of %<->%> is not a pointer"); 2277 } 2278 return error_mark_node; 2279 } 2280 2281 /* Return an expression for "DATUM .* COMPONENT". DATUM has not 2282 already been checked out to be of aggregate type. */ 2283 2284 tree 2285 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain) 2286 { 2287 tree ptrmem_type; 2288 tree objtype; 2289 tree type; 2290 tree binfo; 2291 tree ctype; 2292 2293 datum = mark_lvalue_use (datum); 2294 component = mark_rvalue_use (component); 2295 2296 if (error_operand_p (datum) || error_operand_p (component)) 2297 return error_mark_node; 2298 2299 ptrmem_type = TREE_TYPE (component); 2300 if (!TYPE_PTRMEM_P (ptrmem_type)) 2301 { 2302 if (complain & tf_error) 2303 error ("%qE cannot be used as a member pointer, since it is of " 2304 "type %qT", component, ptrmem_type); 2305 return error_mark_node; 2306 } 2307 2308 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum)); 2309 if (! MAYBE_CLASS_TYPE_P (objtype)) 2310 { 2311 if (complain & tf_error) 2312 error ("cannot apply member pointer %qE to %qE, which is of " 2313 "non-class type %qT", component, datum, objtype); 2314 return error_mark_node; 2315 } 2316 2317 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type); 2318 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type)); 2319 2320 if (!COMPLETE_TYPE_P (ctype)) 2321 { 2322 if (!same_type_p (ctype, objtype)) 2323 goto mismatch; 2324 binfo = NULL; 2325 } 2326 else 2327 { 2328 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain); 2329 2330 if (!binfo) 2331 { 2332 mismatch: 2333 if (complain & tf_error) 2334 error ("pointer to member type %qT incompatible with object " 2335 "type %qT", type, objtype); 2336 return error_mark_node; 2337 } 2338 else if (binfo == error_mark_node) 2339 return error_mark_node; 2340 } 2341 2342 if (TYPE_PTRDATAMEM_P (ptrmem_type)) 2343 { 2344 bool is_lval = real_lvalue_p (datum); 2345 tree ptype; 2346 2347 /* Compute the type of the field, as described in [expr.ref]. 2348 There's no such thing as a mutable pointer-to-member, so 2349 things are not as complex as they are for references to 2350 non-static data members. */ 2351 type = cp_build_qualified_type (type, 2352 (cp_type_quals (type) 2353 | cp_type_quals (TREE_TYPE (datum)))); 2354 2355 datum = cp_build_addr_expr (datum, complain); 2356 2357 /* Convert object to the correct base. */ 2358 if (binfo) 2359 { 2360 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain); 2361 if (datum == error_mark_node) 2362 return error_mark_node; 2363 } 2364 2365 /* Build an expression for "object + offset" where offset is the 2366 value stored in the pointer-to-data-member. */ 2367 ptype = build_pointer_type (type); 2368 datum = convert (ptype, datum); 2369 if (!processing_template_decl) 2370 datum = build2 (POINTER_PLUS_EXPR, ptype, 2371 datum, convert_to_ptrofftype (component)); 2372 datum = cp_fully_fold (datum); 2373 datum = cp_build_fold_indirect_ref (datum); 2374 if (datum == error_mark_node) 2375 return error_mark_node; 2376 2377 /* If the object expression was an rvalue, return an rvalue. */ 2378 if (!is_lval) 2379 datum = move (datum); 2380 return datum; 2381 } 2382 else 2383 { 2384 /* 5.5/6: In a .* expression whose object expression is an rvalue, the 2385 program is ill-formed if the second operand is a pointer to member 2386 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq 2387 is const). In a .* expression whose object expression is an lvalue, 2388 the program is ill-formed if the second operand is a pointer to member 2389 function with ref-qualifier &&. */ 2390 if (FUNCTION_REF_QUALIFIED (type)) 2391 { 2392 bool lval = lvalue_p (datum); 2393 if (lval && FUNCTION_RVALUE_QUALIFIED (type)) 2394 { 2395 if (complain & tf_error) 2396 error ("pointer-to-member-function type %qT requires an rvalue", 2397 ptrmem_type); 2398 return error_mark_node; 2399 } 2400 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type)) 2401 { 2402 if ((type_memfn_quals (type) 2403 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)) 2404 != TYPE_QUAL_CONST) 2405 { 2406 if (complain & tf_error) 2407 error ("pointer-to-member-function type %qT requires " 2408 "an lvalue", ptrmem_type); 2409 return error_mark_node; 2410 } 2411 else if (cxx_dialect < cxx20) 2412 { 2413 if (complain & tf_warning_or_error) 2414 pedwarn (input_location, OPT_Wpedantic, 2415 "pointer-to-member-function type %qT requires " 2416 "an lvalue before C++20", ptrmem_type); 2417 else 2418 return error_mark_node; 2419 } 2420 } 2421 } 2422 return build2 (OFFSET_REF, type, datum, component); 2423 } 2424 } 2425 2426 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ 2427 2428 static tree 2429 build_functional_cast_1 (location_t loc, tree exp, tree parms, 2430 tsubst_flags_t complain) 2431 { 2432 /* This is either a call to a constructor, 2433 or a C cast in C++'s `functional' notation. */ 2434 2435 /* The type to which we are casting. */ 2436 tree type; 2437 2438 if (error_operand_p (exp) || parms == error_mark_node) 2439 return error_mark_node; 2440 2441 if (TREE_CODE (exp) == TYPE_DECL) 2442 { 2443 type = TREE_TYPE (exp); 2444 2445 if (DECL_ARTIFICIAL (exp)) 2446 cp_handle_deprecated_or_unavailable (type); 2447 } 2448 else 2449 type = exp; 2450 2451 /* We need to check this explicitly, since value-initialization of 2452 arrays is allowed in other situations. */ 2453 if (TREE_CODE (type) == ARRAY_TYPE) 2454 { 2455 if (complain & tf_error) 2456 error_at (loc, "functional cast to array type %qT", type); 2457 return error_mark_node; 2458 } 2459 2460 if (tree anode = type_uses_auto (type)) 2461 { 2462 tree init; 2463 if (CLASS_PLACEHOLDER_TEMPLATE (anode)) 2464 init = parms; 2465 /* C++23 auto(x). */ 2466 else if (!AUTO_IS_DECLTYPE (anode) 2467 && list_length (parms) == 1) 2468 { 2469 init = TREE_VALUE (parms); 2470 if (is_constrained_auto (anode)) 2471 { 2472 if (complain & tf_error) 2473 error_at (loc, "%<auto(x)%> cannot be constrained"); 2474 return error_mark_node; 2475 } 2476 else if (cxx_dialect < cxx23) 2477 pedwarn (loc, OPT_Wc__23_extensions, 2478 "%<auto(x)%> only available with " 2479 "%<-std=c++2b%> or %<-std=gnu++2b%>"); 2480 } 2481 else 2482 { 2483 if (complain & tf_error) 2484 error_at (loc, "invalid use of %qT", anode); 2485 return error_mark_node; 2486 } 2487 type = do_auto_deduction (type, init, anode, complain, 2488 adc_variable_type); 2489 if (type == error_mark_node) 2490 return error_mark_node; 2491 } 2492 2493 if (processing_template_decl) 2494 { 2495 tree t; 2496 2497 /* Diagnose this even in a template. We could also try harder 2498 to give all the usual errors when the type and args are 2499 non-dependent... */ 2500 if (TYPE_REF_P (type) && !parms) 2501 { 2502 if (complain & tf_error) 2503 error_at (loc, "invalid value-initialization of reference type"); 2504 return error_mark_node; 2505 } 2506 2507 t = build_min (CAST_EXPR, type, parms); 2508 /* We don't know if it will or will not have side effects. */ 2509 TREE_SIDE_EFFECTS (t) = 1; 2510 return t; 2511 } 2512 2513 if (! MAYBE_CLASS_TYPE_P (type)) 2514 { 2515 if (parms == NULL_TREE) 2516 { 2517 if (VOID_TYPE_P (type)) 2518 return void_node; 2519 return build_value_init (cv_unqualified (type), complain); 2520 } 2521 2522 /* This must build a C cast. */ 2523 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain); 2524 return cp_build_c_cast (loc, type, parms, complain); 2525 } 2526 2527 /* Prepare to evaluate as a call to a constructor. If this expression 2528 is actually used, for example, 2529 2530 return X (arg1, arg2, ...); 2531 2532 then the slot being initialized will be filled in. */ 2533 2534 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) 2535 return error_mark_node; 2536 if (abstract_virtuals_error (ACU_CAST, type, complain)) 2537 return error_mark_node; 2538 2539 /* [expr.type.conv] 2540 2541 If the expression list is a single-expression, the type 2542 conversion is equivalent (in definedness, and if defined in 2543 meaning) to the corresponding cast expression. */ 2544 if (parms && TREE_CHAIN (parms) == NULL_TREE) 2545 return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain); 2546 2547 /* [expr.type.conv] 2548 2549 The expression T(), where T is a simple-type-specifier for a 2550 non-array complete object type or the (possibly cv-qualified) 2551 void type, creates an rvalue of the specified type, which is 2552 value-initialized. */ 2553 2554 if (parms == NULL_TREE) 2555 { 2556 exp = build_value_init (type, complain); 2557 exp = get_target_expr (exp, complain); 2558 return exp; 2559 } 2560 2561 /* Call the constructor. */ 2562 releasing_vec parmvec; 2563 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms)) 2564 vec_safe_push (parmvec, TREE_VALUE (parms)); 2565 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, 2566 &parmvec, type, LOOKUP_NORMAL, complain); 2567 2568 if (exp == error_mark_node) 2569 return error_mark_node; 2570 2571 return build_cplus_new (type, exp, complain); 2572 } 2573 2574 tree 2575 build_functional_cast (location_t loc, tree exp, tree parms, 2576 tsubst_flags_t complain) 2577 { 2578 tree result = build_functional_cast_1 (loc, exp, parms, complain); 2579 protected_set_expr_location (result, loc); 2580 return result; 2581 } 2582 2583 2585 /* Add new exception specifier SPEC, to the LIST we currently have. 2586 If it's already in LIST then do nothing. 2587 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we 2588 know what we're doing. */ 2589 2590 tree 2591 add_exception_specifier (tree list, tree spec, tsubst_flags_t complain) 2592 { 2593 bool ok; 2594 tree core = spec; 2595 bool is_ptr; 2596 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */ 2597 2598 if (spec == error_mark_node) 2599 return list; 2600 2601 gcc_assert (spec && (!list || TREE_VALUE (list))); 2602 2603 /* [except.spec] 1, type in an exception specifier shall not be 2604 incomplete, or pointer or ref to incomplete other than pointer 2605 to cv void. */ 2606 is_ptr = TYPE_PTR_P (core); 2607 if (is_ptr || TYPE_REF_P (core)) 2608 core = TREE_TYPE (core); 2609 if (complain < 0) 2610 ok = true; 2611 else if (VOID_TYPE_P (core)) 2612 ok = is_ptr; 2613 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM) 2614 ok = true; 2615 else if (processing_template_decl) 2616 ok = true; 2617 else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core, 2618 !(complain & tf_error))) 2619 return error_mark_node; 2620 else 2621 { 2622 ok = true; 2623 /* 15.4/1 says that types in an exception specifier must be complete, 2624 but it seems more reasonable to only require this on definitions 2625 and calls. So just give a pedwarn at this point; we will give an 2626 error later if we hit one of those two cases. */ 2627 if (!COMPLETE_TYPE_P (complete_type (core))) 2628 diag_type = DK_PEDWARN; /* pedwarn */ 2629 } 2630 2631 if (ok) 2632 { 2633 tree probe; 2634 2635 for (probe = list; probe; probe = TREE_CHAIN (probe)) 2636 if (same_type_p (TREE_VALUE (probe), spec)) 2637 break; 2638 if (!probe) 2639 list = tree_cons (NULL_TREE, spec, list); 2640 } 2641 else 2642 diag_type = DK_ERROR; /* error */ 2643 2644 if (diag_type != DK_UNSPECIFIED 2645 && (complain & tf_warning_or_error)) 2646 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type); 2647 2648 return list; 2649 } 2650 2651 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */ 2652 2653 static bool 2654 nothrow_spec_p_uninst (const_tree spec) 2655 { 2656 if (DEFERRED_NOEXCEPT_SPEC_P (spec)) 2657 return false; 2658 return nothrow_spec_p (spec); 2659 } 2660 2661 /* Combine the two exceptions specifier lists LIST and ADD, and return 2662 their union. */ 2663 2664 tree 2665 merge_exception_specifiers (tree list, tree add) 2666 { 2667 tree noex, orig_list; 2668 2669 if (list == error_mark_node || add == error_mark_node) 2670 return error_mark_node; 2671 2672 /* No exception-specifier or noexcept(false) are less strict than 2673 anything else. Prefer the newer variant (LIST). */ 2674 if (!list || list == noexcept_false_spec) 2675 return list; 2676 else if (!add || add == noexcept_false_spec) 2677 return add; 2678 2679 /* noexcept(true) and throw() are stricter than anything else. 2680 As above, prefer the more recent one (LIST). */ 2681 if (nothrow_spec_p_uninst (add)) 2682 return list; 2683 2684 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */ 2685 if (UNEVALUATED_NOEXCEPT_SPEC_P (add) 2686 && UNEVALUATED_NOEXCEPT_SPEC_P (list)) 2687 return list; 2688 /* We should have instantiated other deferred noexcept specs by now. */ 2689 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add)); 2690 2691 if (nothrow_spec_p_uninst (list)) 2692 return add; 2693 noex = TREE_PURPOSE (list); 2694 gcc_checking_assert (!TREE_PURPOSE (add) 2695 || errorcount || !flag_exceptions 2696 || cp_tree_equal (noex, TREE_PURPOSE (add))); 2697 2698 /* Combine the dynamic-exception-specifiers, if any. */ 2699 orig_list = list; 2700 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add)) 2701 { 2702 tree spec = TREE_VALUE (add); 2703 tree probe; 2704 2705 for (probe = orig_list; probe && TREE_VALUE (probe); 2706 probe = TREE_CHAIN (probe)) 2707 if (same_type_p (TREE_VALUE (probe), spec)) 2708 break; 2709 if (!probe) 2710 { 2711 spec = build_tree_list (NULL_TREE, spec); 2712 TREE_CHAIN (spec) = list; 2713 list = spec; 2714 } 2715 } 2716 2717 /* Keep the noexcept-specifier at the beginning of the list. */ 2718 if (noex != TREE_PURPOSE (list)) 2719 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list)); 2720 2721 return list; 2722 } 2723 2724 /* Subroutine of build_call. Ensure that each of the types in the 2725 exception specification is complete. Technically, 15.4/1 says that 2726 they need to be complete when we see a declaration of the function, 2727 but we should be able to get away with only requiring this when the 2728 function is defined or called. See also add_exception_specifier. */ 2729 2730 void 2731 require_complete_eh_spec_types (tree fntype, tree decl) 2732 { 2733 tree raises; 2734 /* Don't complain about calls to op new. */ 2735 if (decl && DECL_ARTIFICIAL (decl)) 2736 return; 2737 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises; 2738 raises = TREE_CHAIN (raises)) 2739 { 2740 tree type = TREE_VALUE (raises); 2741 if (type && !COMPLETE_TYPE_P (type)) 2742 { 2743 if (decl) 2744 error 2745 ("call to function %qD which throws incomplete type %q#T", 2746 decl, type); 2747 else 2748 error ("call to function which throws incomplete type %q#T", 2749 decl); 2750 } 2751 } 2752 } 2753 2754 /* Record that any TARGET_EXPR in T are going to be elided in 2755 cp_gimplify_init_expr (or sooner). */ 2756 2757 void 2758 set_target_expr_eliding (tree t) 2759 { 2760 if (!t) 2761 return; 2762 switch (TREE_CODE (t)) 2763 { 2764 case TARGET_EXPR: 2765 TARGET_EXPR_ELIDING_P (t) = true; 2766 break; 2767 case COMPOUND_EXPR: 2768 set_target_expr_eliding (TREE_OPERAND (t, 1)); 2769 break; 2770 case COND_EXPR: 2771 set_target_expr_eliding (TREE_OPERAND (t, 1)); 2772 set_target_expr_eliding (TREE_OPERAND (t, 2)); 2773 break; 2774 2775 default: 2776 break; 2777 } 2778 } 2779 2780 /* Call the above in the process of building an INIT_EXPR. */ 2781 2782 tree 2783 cp_build_init_expr (location_t loc, tree target, tree init) 2784 { 2785 set_target_expr_eliding (init); 2786 tree ie = build2_loc (loc, INIT_EXPR, TREE_TYPE (target), 2787 target, init); 2788 TREE_SIDE_EFFECTS (ie) = true; 2789 return ie; 2790 } 2791