1 /* Handle initialization things in C++. 2 Copyright (C) 1987-2019 Free Software Foundation, Inc. 3 Contributed by Michael Tiemann (tiemann@cygnus.com) 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 GCC is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 /* High-level class interface. */ 22 23 #include "config.h" 24 #include "system.h" 25 #include "coretypes.h" 26 #include "target.h" 27 #include "cp-tree.h" 28 #include "stringpool.h" 29 #include "varasm.h" 30 #include "gimplify.h" 31 #include "c-family/c-ubsan.h" 32 #include "intl.h" 33 #include "stringpool.h" 34 #include "attribs.h" 35 #include "asan.h" 36 37 static bool begin_init_stmts (tree *, tree *); 38 static tree finish_init_stmts (bool, tree, tree); 39 static void construct_virtual_base (tree, tree); 40 static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t); 41 static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t); 42 static void perform_member_init (tree, tree); 43 static int member_init_ok_or_else (tree, tree, tree); 44 static void expand_virtual_init (tree, tree); 45 static tree sort_mem_initializers (tree, tree); 46 static tree initializing_context (tree); 47 static void expand_cleanup_for_base (tree, tree); 48 static tree dfs_initialize_vtbl_ptrs (tree, void *); 49 static tree build_field_list (tree, tree, int *); 50 static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool); 51 52 static GTY(()) tree fn; 53 54 /* We are about to generate some complex initialization code. 55 Conceptually, it is all a single expression. However, we may want 56 to include conditionals, loops, and other such statement-level 57 constructs. Therefore, we build the initialization code inside a 58 statement-expression. This function starts such an expression. 59 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function; 60 pass them back to finish_init_stmts when the expression is 61 complete. */ 62 63 static bool 64 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p) 65 { 66 bool is_global = !building_stmt_list_p (); 67 68 *stmt_expr_p = begin_stmt_expr (); 69 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE); 70 71 return is_global; 72 } 73 74 /* Finish out the statement-expression begun by the previous call to 75 begin_init_stmts. Returns the statement-expression itself. */ 76 77 static tree 78 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt) 79 { 80 finish_compound_stmt (compound_stmt); 81 82 stmt_expr = finish_stmt_expr (stmt_expr, true); 83 84 gcc_assert (!building_stmt_list_p () == is_global); 85 86 return stmt_expr; 87 } 88 89 /* Constructors */ 90 91 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base 92 which we want to initialize the vtable pointer for, DATA is 93 TREE_LIST whose TREE_VALUE is the this ptr expression. */ 94 95 static tree 96 dfs_initialize_vtbl_ptrs (tree binfo, void *data) 97 { 98 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 99 return dfs_skip_bases; 100 101 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo)) 102 { 103 tree base_ptr = TREE_VALUE ((tree) data); 104 105 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1, 106 tf_warning_or_error); 107 108 expand_virtual_init (binfo, base_ptr); 109 } 110 111 return NULL_TREE; 112 } 113 114 /* Initialize all the vtable pointers in the object pointed to by 115 ADDR. */ 116 117 void 118 initialize_vtbl_ptrs (tree addr) 119 { 120 tree list; 121 tree type; 122 123 type = TREE_TYPE (TREE_TYPE (addr)); 124 list = build_tree_list (type, addr); 125 126 /* Walk through the hierarchy, initializing the vptr in each base 127 class. We do these in pre-order because we can't find the virtual 128 bases for a class until we've initialized the vtbl for that 129 class. */ 130 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list); 131 } 132 133 /* Return an expression for the zero-initialization of an object with 134 type T. This expression will either be a constant (in the case 135 that T is a scalar), or a CONSTRUCTOR (in the case that T is an 136 aggregate), or NULL (in the case that T does not require 137 initialization). In either case, the value can be used as 138 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static 139 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS 140 is the number of elements in the array. If STATIC_STORAGE_P is 141 TRUE, initializers are only generated for entities for which 142 zero-initialization does not simply mean filling the storage with 143 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field, 144 subfields with bit positions at or above that bit size shouldn't 145 be added. Note that this only works when the result is assigned 146 to a base COMPONENT_REF; if we only have a pointer to the base subobject, 147 expand_assignment will end up clearing the full size of TYPE. */ 148 149 static tree 150 build_zero_init_1 (tree type, tree nelts, bool static_storage_p, 151 tree field_size) 152 { 153 tree init = NULL_TREE; 154 155 /* [dcl.init] 156 157 To zero-initialize an object of type T means: 158 159 -- if T is a scalar type, the storage is set to the value of zero 160 converted to T. 161 162 -- if T is a non-union class type, the storage for each nonstatic 163 data member and each base-class subobject is zero-initialized. 164 165 -- if T is a union type, the storage for its first data member is 166 zero-initialized. 167 168 -- if T is an array type, the storage for each element is 169 zero-initialized. 170 171 -- if T is a reference type, no initialization is performed. */ 172 173 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST); 174 175 if (type == error_mark_node) 176 ; 177 else if (static_storage_p && zero_init_p (type)) 178 /* In order to save space, we do not explicitly build initializers 179 for items that do not need them. GCC's semantics are that 180 items with static storage duration that are not otherwise 181 initialized are initialized to zero. */ 182 ; 183 else if (TYPE_PTR_OR_PTRMEM_P (type)) 184 init = fold (convert (type, nullptr_node)); 185 else if (NULLPTR_TYPE_P (type)) 186 init = build_int_cst (type, 0); 187 else if (SCALAR_TYPE_P (type)) 188 init = fold (convert (type, integer_zero_node)); 189 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type))) 190 { 191 tree field; 192 vec<constructor_elt, va_gc> *v = NULL; 193 194 /* Iterate over the fields, building initializations. */ 195 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 196 { 197 if (TREE_CODE (field) != FIELD_DECL) 198 continue; 199 200 if (TREE_TYPE (field) == error_mark_node) 201 continue; 202 203 /* Don't add virtual bases for base classes if they are beyond 204 the size of the current field, that means it is present 205 somewhere else in the object. */ 206 if (field_size) 207 { 208 tree bitpos = bit_position (field); 209 if (TREE_CODE (bitpos) == INTEGER_CST 210 && !tree_int_cst_lt (bitpos, field_size)) 211 continue; 212 } 213 214 /* Note that for class types there will be FIELD_DECLs 215 corresponding to base classes as well. Thus, iterating 216 over TYPE_FIELDs will result in correct initialization of 217 all of the subobjects. */ 218 if (!static_storage_p || !zero_init_p (TREE_TYPE (field))) 219 { 220 tree new_field_size 221 = (DECL_FIELD_IS_BASE (field) 222 && DECL_SIZE (field) 223 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST) 224 ? DECL_SIZE (field) : NULL_TREE; 225 tree value = build_zero_init_1 (TREE_TYPE (field), 226 /*nelts=*/NULL_TREE, 227 static_storage_p, 228 new_field_size); 229 if (value) 230 CONSTRUCTOR_APPEND_ELT(v, field, value); 231 } 232 233 /* For unions, only the first field is initialized. */ 234 if (TREE_CODE (type) == UNION_TYPE) 235 break; 236 } 237 238 /* Build a constructor to contain the initializations. */ 239 init = build_constructor (type, v); 240 } 241 else if (TREE_CODE (type) == ARRAY_TYPE) 242 { 243 tree max_index; 244 vec<constructor_elt, va_gc> *v = NULL; 245 246 /* Iterate over the array elements, building initializations. */ 247 if (nelts) 248 max_index = fold_build2_loc (input_location, 249 MINUS_EXPR, TREE_TYPE (nelts), 250 nelts, integer_one_node); 251 else 252 max_index = array_type_nelts (type); 253 254 /* If we have an error_mark here, we should just return error mark 255 as we don't know the size of the array yet. */ 256 if (max_index == error_mark_node) 257 return error_mark_node; 258 gcc_assert (TREE_CODE (max_index) == INTEGER_CST); 259 260 /* A zero-sized array, which is accepted as an extension, will 261 have an upper bound of -1. */ 262 if (!tree_int_cst_equal (max_index, integer_minus_one_node)) 263 { 264 constructor_elt ce; 265 266 /* If this is a one element array, we just use a regular init. */ 267 if (tree_int_cst_equal (size_zero_node, max_index)) 268 ce.index = size_zero_node; 269 else 270 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, 271 max_index); 272 273 ce.value = build_zero_init_1 (TREE_TYPE (type), 274 /*nelts=*/NULL_TREE, 275 static_storage_p, NULL_TREE); 276 if (ce.value) 277 { 278 vec_alloc (v, 1); 279 v->quick_push (ce); 280 } 281 } 282 283 /* Build a constructor to contain the initializations. */ 284 init = build_constructor (type, v); 285 } 286 else if (VECTOR_TYPE_P (type)) 287 init = build_zero_cst (type); 288 else 289 { 290 gcc_assert (TYPE_REF_P (type)); 291 init = build_zero_cst (type); 292 } 293 294 /* In all cases, the initializer is a constant. */ 295 if (init) 296 TREE_CONSTANT (init) = 1; 297 298 return init; 299 } 300 301 /* Return an expression for the zero-initialization of an object with 302 type T. This expression will either be a constant (in the case 303 that T is a scalar), or a CONSTRUCTOR (in the case that T is an 304 aggregate), or NULL (in the case that T does not require 305 initialization). In either case, the value can be used as 306 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static 307 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS 308 is the number of elements in the array. If STATIC_STORAGE_P is 309 TRUE, initializers are only generated for entities for which 310 zero-initialization does not simply mean filling the storage with 311 zero bytes. */ 312 313 tree 314 build_zero_init (tree type, tree nelts, bool static_storage_p) 315 { 316 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE); 317 } 318 319 /* Return a suitable initializer for value-initializing an object of type 320 TYPE, as described in [dcl.init]. */ 321 322 tree 323 build_value_init (tree type, tsubst_flags_t complain) 324 { 325 /* [dcl.init] 326 327 To value-initialize an object of type T means: 328 329 - if T is a class type (clause 9) with either no default constructor 330 (12.1) or a default constructor that is user-provided or deleted, 331 then the object is default-initialized; 332 333 - if T is a (possibly cv-qualified) class type without a user-provided 334 or deleted default constructor, then the object is zero-initialized 335 and the semantic constraints for default-initialization are checked, 336 and if T has a non-trivial default constructor, the object is 337 default-initialized; 338 339 - if T is an array type, then each element is value-initialized; 340 341 - otherwise, the object is zero-initialized. 342 343 A program that calls for default-initialization or 344 value-initialization of an entity of reference type is ill-formed. */ 345 346 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */ 347 gcc_assert (!processing_template_decl 348 || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)); 349 350 if (CLASS_TYPE_P (type) 351 && type_build_ctor_call (type)) 352 { 353 tree ctor = 354 build_special_member_call (NULL_TREE, complete_ctor_identifier, 355 NULL, type, LOOKUP_NORMAL, 356 complain); 357 if (ctor == error_mark_node) 358 return ctor; 359 tree fn = NULL_TREE; 360 if (TREE_CODE (ctor) == CALL_EXPR) 361 fn = get_callee_fndecl (ctor); 362 ctor = build_aggr_init_expr (type, ctor); 363 if (fn && user_provided_p (fn)) 364 return ctor; 365 else if (TYPE_HAS_COMPLEX_DFLT (type)) 366 { 367 /* This is a class that needs constructing, but doesn't have 368 a user-provided constructor. So we need to zero-initialize 369 the object and then call the implicitly defined ctor. 370 This will be handled in simplify_aggr_init_expr. */ 371 AGGR_INIT_ZERO_FIRST (ctor) = 1; 372 return ctor; 373 } 374 } 375 376 /* Discard any access checking during subobject initialization; 377 the checks are implied by the call to the ctor which we have 378 verified is OK (cpp0x/defaulted46.C). */ 379 push_deferring_access_checks (dk_deferred); 380 tree r = build_value_init_noctor (type, complain); 381 pop_deferring_access_checks (); 382 return r; 383 } 384 385 /* Like build_value_init, but don't call the constructor for TYPE. Used 386 for base initializers. */ 387 388 tree 389 build_value_init_noctor (tree type, tsubst_flags_t complain) 390 { 391 if (!COMPLETE_TYPE_P (type)) 392 { 393 if (complain & tf_error) 394 error ("value-initialization of incomplete type %qT", type); 395 return error_mark_node; 396 } 397 /* FIXME the class and array cases should just use digest_init once it is 398 SFINAE-enabled. */ 399 if (CLASS_TYPE_P (type)) 400 { 401 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type) 402 || errorcount != 0); 403 404 if (TREE_CODE (type) != UNION_TYPE) 405 { 406 tree field; 407 vec<constructor_elt, va_gc> *v = NULL; 408 409 /* Iterate over the fields, building initializations. */ 410 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 411 { 412 tree ftype, value; 413 414 if (TREE_CODE (field) != FIELD_DECL) 415 continue; 416 417 ftype = TREE_TYPE (field); 418 419 if (ftype == error_mark_node) 420 continue; 421 422 /* Ignore flexible array members for value initialization. */ 423 if (TREE_CODE (ftype) == ARRAY_TYPE 424 && !COMPLETE_TYPE_P (ftype) 425 && !TYPE_DOMAIN (ftype) 426 && COMPLETE_TYPE_P (TREE_TYPE (ftype)) 427 && (next_initializable_field (DECL_CHAIN (field)) 428 == NULL_TREE)) 429 continue; 430 431 /* We could skip vfields and fields of types with 432 user-defined constructors, but I think that won't improve 433 performance at all; it should be simpler in general just 434 to zero out the entire object than try to only zero the 435 bits that actually need it. */ 436 437 /* Note that for class types there will be FIELD_DECLs 438 corresponding to base classes as well. Thus, iterating 439 over TYPE_FIELDs will result in correct initialization of 440 all of the subobjects. */ 441 value = build_value_init (ftype, complain); 442 value = maybe_constant_init (value); 443 444 if (value == error_mark_node) 445 return error_mark_node; 446 447 CONSTRUCTOR_APPEND_ELT(v, field, value); 448 449 /* We shouldn't have gotten here for anything that would need 450 non-trivial initialization, and gimplify_init_ctor_preeval 451 would need to be fixed to allow it. */ 452 gcc_assert (TREE_CODE (value) != TARGET_EXPR 453 && TREE_CODE (value) != AGGR_INIT_EXPR); 454 } 455 456 /* Build a constructor to contain the zero- initializations. */ 457 return build_constructor (type, v); 458 } 459 } 460 else if (TREE_CODE (type) == ARRAY_TYPE) 461 { 462 vec<constructor_elt, va_gc> *v = NULL; 463 464 /* Iterate over the array elements, building initializations. */ 465 tree max_index = array_type_nelts (type); 466 467 /* If we have an error_mark here, we should just return error mark 468 as we don't know the size of the array yet. */ 469 if (max_index == error_mark_node) 470 { 471 if (complain & tf_error) 472 error ("cannot value-initialize array of unknown bound %qT", 473 type); 474 return error_mark_node; 475 } 476 gcc_assert (TREE_CODE (max_index) == INTEGER_CST); 477 478 /* A zero-sized array, which is accepted as an extension, will 479 have an upper bound of -1. */ 480 if (!tree_int_cst_equal (max_index, integer_minus_one_node)) 481 { 482 constructor_elt ce; 483 484 /* If this is a one element array, we just use a regular init. */ 485 if (tree_int_cst_equal (size_zero_node, max_index)) 486 ce.index = size_zero_node; 487 else 488 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index); 489 490 ce.value = build_value_init (TREE_TYPE (type), complain); 491 ce.value = maybe_constant_init (ce.value); 492 if (ce.value == error_mark_node) 493 return error_mark_node; 494 495 vec_alloc (v, 1); 496 v->quick_push (ce); 497 498 /* We shouldn't have gotten here for anything that would need 499 non-trivial initialization, and gimplify_init_ctor_preeval 500 would need to be fixed to allow it. */ 501 gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR 502 && TREE_CODE (ce.value) != AGGR_INIT_EXPR); 503 } 504 505 /* Build a constructor to contain the initializations. */ 506 return build_constructor (type, v); 507 } 508 else if (TREE_CODE (type) == FUNCTION_TYPE) 509 { 510 if (complain & tf_error) 511 error ("value-initialization of function type %qT", type); 512 return error_mark_node; 513 } 514 else if (TYPE_REF_P (type)) 515 { 516 if (complain & tf_error) 517 error ("value-initialization of reference type %qT", type); 518 return error_mark_node; 519 } 520 521 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false); 522 } 523 524 /* Initialize current class with INIT, a TREE_LIST of 525 arguments for a target constructor. If TREE_LIST is void_type_node, 526 an empty initializer list was given. */ 527 528 static void 529 perform_target_ctor (tree init) 530 { 531 tree decl = current_class_ref; 532 tree type = current_class_type; 533 534 finish_expr_stmt (build_aggr_init (decl, init, 535 LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS, 536 tf_warning_or_error)); 537 if (type_build_dtor_call (type)) 538 { 539 tree expr = build_delete (type, decl, sfk_complete_destructor, 540 LOOKUP_NORMAL 541 |LOOKUP_NONVIRTUAL 542 |LOOKUP_DESTRUCTOR, 543 0, tf_warning_or_error); 544 if (expr != error_mark_node 545 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 546 finish_eh_cleanup (expr); 547 } 548 } 549 550 /* Return the non-static data initializer for FIELD_DECL MEMBER. */ 551 552 static GTY((cache)) tree_cache_map *nsdmi_inst; 553 554 tree 555 get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain) 556 { 557 tree init; 558 tree save_ccp = current_class_ptr; 559 tree save_ccr = current_class_ref; 560 561 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member)) 562 { 563 init = DECL_INITIAL (DECL_TI_TEMPLATE (member)); 564 location_t expr_loc 565 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member)); 566 tree *slot; 567 if (TREE_CODE (init) == DEFAULT_ARG) 568 /* Unparsed. */; 569 else if (nsdmi_inst && (slot = nsdmi_inst->get (member))) 570 init = *slot; 571 /* Check recursive instantiation. */ 572 else if (DECL_INSTANTIATING_NSDMI_P (member)) 573 { 574 if (complain & tf_error) 575 error_at (expr_loc, "recursive instantiation of default member " 576 "initializer for %qD", member); 577 init = error_mark_node; 578 } 579 else 580 { 581 cp_evaluated ev; 582 583 location_t sloc = input_location; 584 input_location = expr_loc; 585 586 DECL_INSTANTIATING_NSDMI_P (member) = 1; 587 588 bool pushed = false; 589 if (!currently_open_class (DECL_CONTEXT (member))) 590 { 591 push_to_top_level (); 592 push_nested_class (DECL_CONTEXT (member)); 593 pushed = true; 594 } 595 596 gcc_checking_assert (!processing_template_decl); 597 598 inject_this_parameter (DECL_CONTEXT (member), TYPE_UNQUALIFIED); 599 600 start_lambda_scope (member); 601 602 /* Do deferred instantiation of the NSDMI. */ 603 init = (tsubst_copy_and_build 604 (init, DECL_TI_ARGS (member), 605 complain, member, /*function_p=*/false, 606 /*integral_constant_expression_p=*/false)); 607 init = digest_nsdmi_init (member, init, complain); 608 609 finish_lambda_scope (); 610 611 DECL_INSTANTIATING_NSDMI_P (member) = 0; 612 613 if (init != error_mark_node) 614 { 615 if (!nsdmi_inst) 616 nsdmi_inst = tree_cache_map::create_ggc (37); 617 nsdmi_inst->put (member, init); 618 } 619 620 if (pushed) 621 { 622 pop_nested_class (); 623 pop_from_top_level (); 624 } 625 626 input_location = sloc; 627 } 628 } 629 else 630 init = DECL_INITIAL (member); 631 632 if (init && TREE_CODE (init) == DEFAULT_ARG) 633 { 634 if (complain & tf_error) 635 { 636 error ("default member initializer for %qD required before the end " 637 "of its enclosing class", member); 638 inform (location_of (init), "defined here"); 639 DECL_INITIAL (member) = error_mark_node; 640 } 641 init = error_mark_node; 642 } 643 644 if (in_ctor) 645 { 646 current_class_ptr = save_ccp; 647 current_class_ref = save_ccr; 648 } 649 else 650 { 651 /* Use a PLACEHOLDER_EXPR when we don't have a 'this' parameter to 652 refer to; constexpr evaluation knows what to do with it. */ 653 current_class_ref = build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (member)); 654 current_class_ptr = build_address (current_class_ref); 655 } 656 657 /* Strip redundant TARGET_EXPR so we don't need to remap it, and 658 so the aggregate init code below will see a CONSTRUCTOR. */ 659 bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init)); 660 if (simple_target) 661 init = TARGET_EXPR_INITIAL (init); 662 init = break_out_target_exprs (init, /*loc*/true); 663 if (simple_target && TREE_CODE (init) != CONSTRUCTOR) 664 /* Now put it back so C++17 copy elision works. */ 665 init = get_target_expr (init); 666 667 current_class_ptr = save_ccp; 668 current_class_ref = save_ccr; 669 return init; 670 } 671 672 /* Diagnose the flexible array MEMBER if its INITializer is non-null 673 and return true if so. Otherwise return false. */ 674 675 bool 676 maybe_reject_flexarray_init (tree member, tree init) 677 { 678 tree type = TREE_TYPE (member); 679 680 if (!init 681 || TREE_CODE (type) != ARRAY_TYPE 682 || TYPE_DOMAIN (type)) 683 return false; 684 685 /* Point at the flexible array member declaration if it's initialized 686 in-class, and at the ctor if it's initialized in a ctor member 687 initializer list. */ 688 location_t loc; 689 if (DECL_INITIAL (member) == init 690 || !current_function_decl 691 || DECL_DEFAULTED_FN (current_function_decl)) 692 loc = DECL_SOURCE_LOCATION (member); 693 else 694 loc = DECL_SOURCE_LOCATION (current_function_decl); 695 696 error_at (loc, "initializer for flexible array member %q#D", member); 697 return true; 698 } 699 700 /* If INIT's value can come from a call to std::initializer_list<T>::begin, 701 return that function. Otherwise, NULL_TREE. */ 702 703 static tree 704 find_list_begin (tree init) 705 { 706 STRIP_NOPS (init); 707 while (TREE_CODE (init) == COMPOUND_EXPR) 708 init = TREE_OPERAND (init, 1); 709 STRIP_NOPS (init); 710 if (TREE_CODE (init) == COND_EXPR) 711 { 712 tree left = TREE_OPERAND (init, 1); 713 if (!left) 714 left = TREE_OPERAND (init, 0); 715 left = find_list_begin (left); 716 if (left) 717 return left; 718 return find_list_begin (TREE_OPERAND (init, 2)); 719 } 720 if (TREE_CODE (init) == CALL_EXPR) 721 if (tree fn = get_callee_fndecl (init)) 722 if (id_equal (DECL_NAME (fn), "begin") 723 && is_std_init_list (DECL_CONTEXT (fn))) 724 return fn; 725 return NULL_TREE; 726 } 727 728 /* If INIT initializing MEMBER is copying the address of the underlying array 729 of an initializer_list, warn. */ 730 731 static void 732 maybe_warn_list_ctor (tree member, tree init) 733 { 734 tree memtype = TREE_TYPE (member); 735 if (!init || !TYPE_PTR_P (memtype) 736 || !is_list_ctor (current_function_decl)) 737 return; 738 739 tree parms = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl); 740 tree initlist = non_reference (TREE_VALUE (parms)); 741 tree targs = CLASSTYPE_TI_ARGS (initlist); 742 tree elttype = TREE_VEC_ELT (targs, 0); 743 744 if (!same_type_ignoring_top_level_qualifiers_p 745 (TREE_TYPE (memtype), elttype)) 746 return; 747 748 tree begin = find_list_begin (init); 749 if (!begin) 750 return; 751 752 location_t loc = cp_expr_loc_or_loc (init, input_location); 753 warning_at (loc, OPT_Winit_list_lifetime, 754 "initializing %qD from %qE does not extend the lifetime " 755 "of the underlying array", member, begin); 756 } 757 758 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of 759 arguments. If TREE_LIST is void_type_node, an empty initializer 760 list was given; if NULL_TREE no initializer was given. */ 761 762 static void 763 perform_member_init (tree member, tree init) 764 { 765 tree decl; 766 tree type = TREE_TYPE (member); 767 768 /* Use the non-static data member initializer if there was no 769 mem-initializer for this field. */ 770 if (init == NULL_TREE) 771 init = get_nsdmi (member, /*ctor*/true, tf_warning_or_error); 772 773 if (init == error_mark_node) 774 return; 775 776 /* Effective C++ rule 12 requires that all data members be 777 initialized. */ 778 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE) 779 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__, 780 "%qD should be initialized in the member initialization list", 781 member); 782 783 /* Get an lvalue for the data member. */ 784 decl = build_class_member_access_expr (current_class_ref, member, 785 /*access_path=*/NULL_TREE, 786 /*preserve_reference=*/true, 787 tf_warning_or_error); 788 if (decl == error_mark_node) 789 return; 790 791 if (warn_init_self && init && TREE_CODE (init) == TREE_LIST 792 && TREE_CHAIN (init) == NULL_TREE) 793 { 794 tree val = TREE_VALUE (init); 795 /* Handle references. */ 796 if (REFERENCE_REF_P (val)) 797 val = TREE_OPERAND (val, 0); 798 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member 799 && TREE_OPERAND (val, 0) == current_class_ref) 800 warning_at (DECL_SOURCE_LOCATION (current_function_decl), 801 OPT_Winit_self, "%qD is initialized with itself", 802 member); 803 } 804 805 if (init == void_type_node) 806 { 807 /* mem() means value-initialization. */ 808 if (TREE_CODE (type) == ARRAY_TYPE) 809 { 810 init = build_vec_init_expr (type, init, tf_warning_or_error); 811 init = build2 (INIT_EXPR, type, decl, init); 812 finish_expr_stmt (init); 813 } 814 else 815 { 816 tree value = build_value_init (type, tf_warning_or_error); 817 if (value == error_mark_node) 818 return; 819 init = build2 (INIT_EXPR, type, decl, value); 820 finish_expr_stmt (init); 821 } 822 } 823 /* Deal with this here, as we will get confused if we try to call the 824 assignment op for an anonymous union. This can happen in a 825 synthesized copy constructor. */ 826 else if (ANON_AGGR_TYPE_P (type)) 827 { 828 if (init) 829 { 830 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init)); 831 finish_expr_stmt (init); 832 } 833 } 834 else if (init 835 && (TYPE_REF_P (type) 836 /* Pre-digested NSDMI. */ 837 || (((TREE_CODE (init) == CONSTRUCTOR 838 && TREE_TYPE (init) == type) 839 /* { } mem-initializer. */ 840 || (TREE_CODE (init) == TREE_LIST 841 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))) 842 && (CP_AGGREGATE_TYPE_P (type) 843 || is_std_init_list (type))))) 844 { 845 /* With references and list-initialization, we need to deal with 846 extending temporary lifetimes. 12.2p5: "A temporary bound to a 847 reference member in a constructor’s ctor-initializer (12.6.2) 848 persists until the constructor exits." */ 849 unsigned i; tree t; 850 vec<tree, va_gc> *cleanups = make_tree_vector (); 851 if (TREE_CODE (init) == TREE_LIST) 852 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT, 853 tf_warning_or_error); 854 if (TREE_TYPE (init) != type) 855 { 856 if (BRACE_ENCLOSED_INITIALIZER_P (init) 857 && CP_AGGREGATE_TYPE_P (type)) 858 init = reshape_init (type, init, tf_warning_or_error); 859 init = digest_init (type, init, tf_warning_or_error); 860 } 861 if (init == error_mark_node) 862 return; 863 if (DECL_SIZE (member) && integer_zerop (DECL_SIZE (member)) 864 && !TREE_SIDE_EFFECTS (init)) 865 /* Don't add trivial initialization of an empty base/field, as they 866 might not be ordered the way the back-end expects. */ 867 return; 868 /* A FIELD_DECL doesn't really have a suitable lifetime, but 869 make_temporary_var_for_ref_to_temp will treat it as automatic and 870 set_up_extended_ref_temp wants to use the decl in a warning. */ 871 init = extend_ref_init_temps (member, init, &cleanups); 872 if (TREE_CODE (type) == ARRAY_TYPE 873 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))) 874 init = build_vec_init_expr (type, init, tf_warning_or_error); 875 init = build2 (INIT_EXPR, type, decl, init); 876 finish_expr_stmt (init); 877 FOR_EACH_VEC_ELT (*cleanups, i, t) 878 push_cleanup (decl, t, false); 879 release_tree_vector (cleanups); 880 } 881 else if (type_build_ctor_call (type) 882 || (init && CLASS_TYPE_P (strip_array_types (type)))) 883 { 884 if (TREE_CODE (type) == ARRAY_TYPE) 885 { 886 if (init) 887 { 888 /* Check to make sure the member initializer is valid and 889 something like a CONSTRUCTOR in: T a[] = { 1, 2 } and 890 if it isn't, return early to avoid triggering another 891 error below. */ 892 if (maybe_reject_flexarray_init (member, init)) 893 return; 894 895 if (TREE_CODE (init) != TREE_LIST || TREE_CHAIN (init)) 896 init = error_mark_node; 897 else 898 init = TREE_VALUE (init); 899 900 if (BRACE_ENCLOSED_INITIALIZER_P (init)) 901 init = digest_init (type, init, tf_warning_or_error); 902 } 903 if (init == NULL_TREE 904 || same_type_ignoring_top_level_qualifiers_p (type, 905 TREE_TYPE (init))) 906 { 907 if (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))) 908 { 909 /* Initialize the array only if it's not a flexible 910 array member (i.e., if it has an upper bound). */ 911 init = build_vec_init_expr (type, init, tf_warning_or_error); 912 init = build2 (INIT_EXPR, type, decl, init); 913 finish_expr_stmt (init); 914 } 915 } 916 else 917 error ("invalid initializer for array member %q#D", member); 918 } 919 else 920 { 921 int flags = LOOKUP_NORMAL; 922 if (DECL_DEFAULTED_FN (current_function_decl)) 923 flags |= LOOKUP_DEFAULTED; 924 if (CP_TYPE_CONST_P (type) 925 && init == NULL_TREE 926 && default_init_uninitialized_part (type)) 927 { 928 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a 929 vtable; still give this diagnostic. */ 930 auto_diagnostic_group d; 931 if (permerror (DECL_SOURCE_LOCATION (current_function_decl), 932 "uninitialized const member in %q#T", type)) 933 inform (DECL_SOURCE_LOCATION (member), 934 "%q#D should be initialized", member ); 935 } 936 finish_expr_stmt (build_aggr_init (decl, init, flags, 937 tf_warning_or_error)); 938 } 939 } 940 else 941 { 942 if (init == NULL_TREE) 943 { 944 tree core_type; 945 /* member traversal: note it leaves init NULL */ 946 if (TYPE_REF_P (type)) 947 { 948 auto_diagnostic_group d; 949 if (permerror (DECL_SOURCE_LOCATION (current_function_decl), 950 "uninitialized reference member in %q#T", type)) 951 inform (DECL_SOURCE_LOCATION (member), 952 "%q#D should be initialized", member); 953 } 954 else if (CP_TYPE_CONST_P (type)) 955 { 956 auto_diagnostic_group d; 957 if (permerror (DECL_SOURCE_LOCATION (current_function_decl), 958 "uninitialized const member in %q#T", type)) 959 inform (DECL_SOURCE_LOCATION (member), 960 "%q#D should be initialized", member ); 961 } 962 963 core_type = strip_array_types (type); 964 965 if (CLASS_TYPE_P (core_type) 966 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type) 967 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))) 968 diagnose_uninitialized_cst_or_ref_member (core_type, 969 /*using_new=*/false, 970 /*complain=*/true); 971 } 972 else if (TREE_CODE (init) == TREE_LIST) 973 /* There was an explicit member initialization. Do some work 974 in that case. */ 975 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT, 976 tf_warning_or_error); 977 978 maybe_warn_list_ctor (member, init); 979 980 /* Reject a member initializer for a flexible array member. */ 981 if (init && !maybe_reject_flexarray_init (member, init)) 982 finish_expr_stmt (cp_build_modify_expr (input_location, decl, 983 INIT_EXPR, init, 984 tf_warning_or_error)); 985 } 986 987 if (type_build_dtor_call (type)) 988 { 989 tree expr; 990 991 expr = build_class_member_access_expr (current_class_ref, member, 992 /*access_path=*/NULL_TREE, 993 /*preserve_reference=*/false, 994 tf_warning_or_error); 995 expr = build_delete (type, expr, sfk_complete_destructor, 996 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0, 997 tf_warning_or_error); 998 999 if (expr != error_mark_node 1000 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 1001 finish_eh_cleanup (expr); 1002 } 1003 } 1004 1005 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all 1006 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */ 1007 1008 static tree 1009 build_field_list (tree t, tree list, int *uses_unions_or_anon_p) 1010 { 1011 tree fields; 1012 1013 /* Note whether or not T is a union. */ 1014 if (TREE_CODE (t) == UNION_TYPE) 1015 *uses_unions_or_anon_p = 1; 1016 1017 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields)) 1018 { 1019 tree fieldtype; 1020 1021 /* Skip CONST_DECLs for enumeration constants and so forth. */ 1022 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields)) 1023 continue; 1024 1025 fieldtype = TREE_TYPE (fields); 1026 1027 /* For an anonymous struct or union, we must recursively 1028 consider the fields of the anonymous type. They can be 1029 directly initialized from the constructor. */ 1030 if (ANON_AGGR_TYPE_P (fieldtype)) 1031 { 1032 /* Add this field itself. Synthesized copy constructors 1033 initialize the entire aggregate. */ 1034 list = tree_cons (fields, NULL_TREE, list); 1035 /* And now add the fields in the anonymous aggregate. */ 1036 list = build_field_list (fieldtype, list, uses_unions_or_anon_p); 1037 *uses_unions_or_anon_p = 1; 1038 } 1039 /* Add this field. */ 1040 else if (DECL_NAME (fields)) 1041 list = tree_cons (fields, NULL_TREE, list); 1042 } 1043 1044 return list; 1045 } 1046 1047 /* Return the innermost aggregate scope for FIELD, whether that is 1048 the enclosing class or an anonymous aggregate within it. */ 1049 1050 static tree 1051 innermost_aggr_scope (tree field) 1052 { 1053 if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 1054 return TREE_TYPE (field); 1055 else 1056 return DECL_CONTEXT (field); 1057 } 1058 1059 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives 1060 a FIELD_DECL or BINFO in T that needs initialization. The 1061 TREE_VALUE gives the initializer, or list of initializer arguments. 1062 1063 Return a TREE_LIST containing all of the initializations required 1064 for T, in the order in which they should be performed. The output 1065 list has the same format as the input. */ 1066 1067 static tree 1068 sort_mem_initializers (tree t, tree mem_inits) 1069 { 1070 tree init; 1071 tree base, binfo, base_binfo; 1072 tree sorted_inits; 1073 tree next_subobject; 1074 vec<tree, va_gc> *vbases; 1075 int i; 1076 int uses_unions_or_anon_p = 0; 1077 1078 /* Build up a list of initializations. The TREE_PURPOSE of entry 1079 will be the subobject (a FIELD_DECL or BINFO) to initialize. The 1080 TREE_VALUE will be the constructor arguments, or NULL if no 1081 explicit initialization was provided. */ 1082 sorted_inits = NULL_TREE; 1083 1084 /* Process the virtual bases. */ 1085 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 1086 vec_safe_iterate (vbases, i, &base); i++) 1087 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits); 1088 1089 /* Process the direct bases. */ 1090 for (binfo = TYPE_BINFO (t), i = 0; 1091 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 1092 if (!BINFO_VIRTUAL_P (base_binfo)) 1093 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits); 1094 1095 /* Process the non-static data members. */ 1096 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_or_anon_p); 1097 /* Reverse the entire list of initializations, so that they are in 1098 the order that they will actually be performed. */ 1099 sorted_inits = nreverse (sorted_inits); 1100 1101 /* If the user presented the initializers in an order different from 1102 that in which they will actually occur, we issue a warning. Keep 1103 track of the next subobject which can be explicitly initialized 1104 without issuing a warning. */ 1105 next_subobject = sorted_inits; 1106 1107 /* Go through the explicit initializers, filling in TREE_PURPOSE in 1108 the SORTED_INITS. */ 1109 for (init = mem_inits; init; init = TREE_CHAIN (init)) 1110 { 1111 tree subobject; 1112 tree subobject_init; 1113 1114 subobject = TREE_PURPOSE (init); 1115 1116 /* If the explicit initializers are in sorted order, then 1117 SUBOBJECT will be NEXT_SUBOBJECT, or something following 1118 it. */ 1119 for (subobject_init = next_subobject; 1120 subobject_init; 1121 subobject_init = TREE_CHAIN (subobject_init)) 1122 if (TREE_PURPOSE (subobject_init) == subobject) 1123 break; 1124 1125 /* Issue a warning if the explicit initializer order does not 1126 match that which will actually occur. 1127 ??? Are all these on the correct lines? */ 1128 if (warn_reorder && !subobject_init) 1129 { 1130 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL) 1131 warning_at (DECL_SOURCE_LOCATION (TREE_PURPOSE (next_subobject)), 1132 OPT_Wreorder, "%qD will be initialized after", 1133 TREE_PURPOSE (next_subobject)); 1134 else 1135 warning (OPT_Wreorder, "base %qT will be initialized after", 1136 TREE_PURPOSE (next_subobject)); 1137 if (TREE_CODE (subobject) == FIELD_DECL) 1138 warning_at (DECL_SOURCE_LOCATION (subobject), 1139 OPT_Wreorder, " %q#D", subobject); 1140 else 1141 warning (OPT_Wreorder, " base %qT", subobject); 1142 warning_at (DECL_SOURCE_LOCATION (current_function_decl), 1143 OPT_Wreorder, " when initialized here"); 1144 } 1145 1146 /* Look again, from the beginning of the list. */ 1147 if (!subobject_init) 1148 { 1149 subobject_init = sorted_inits; 1150 while (TREE_PURPOSE (subobject_init) != subobject) 1151 subobject_init = TREE_CHAIN (subobject_init); 1152 } 1153 1154 /* It is invalid to initialize the same subobject more than 1155 once. */ 1156 if (TREE_VALUE (subobject_init)) 1157 { 1158 if (TREE_CODE (subobject) == FIELD_DECL) 1159 error_at (DECL_SOURCE_LOCATION (current_function_decl), 1160 "multiple initializations given for %qD", 1161 subobject); 1162 else 1163 error_at (DECL_SOURCE_LOCATION (current_function_decl), 1164 "multiple initializations given for base %qT", 1165 subobject); 1166 } 1167 1168 /* Record the initialization. */ 1169 TREE_VALUE (subobject_init) = TREE_VALUE (init); 1170 next_subobject = subobject_init; 1171 } 1172 1173 /* [class.base.init] 1174 1175 If a ctor-initializer specifies more than one mem-initializer for 1176 multiple members of the same union (including members of 1177 anonymous unions), the ctor-initializer is ill-formed. 1178 1179 Here we also splice out uninitialized union members. */ 1180 if (uses_unions_or_anon_p) 1181 { 1182 tree *last_p = NULL; 1183 tree *p; 1184 for (p = &sorted_inits; *p; ) 1185 { 1186 tree field; 1187 tree ctx; 1188 1189 init = *p; 1190 1191 field = TREE_PURPOSE (init); 1192 1193 /* Skip base classes. */ 1194 if (TREE_CODE (field) != FIELD_DECL) 1195 goto next; 1196 1197 /* If this is an anonymous aggregate with no explicit initializer, 1198 splice it out. */ 1199 if (!TREE_VALUE (init) && ANON_AGGR_TYPE_P (TREE_TYPE (field))) 1200 goto splice; 1201 1202 /* See if this field is a member of a union, or a member of a 1203 structure contained in a union, etc. */ 1204 ctx = innermost_aggr_scope (field); 1205 1206 /* If this field is not a member of a union, skip it. */ 1207 if (TREE_CODE (ctx) != UNION_TYPE 1208 && !ANON_AGGR_TYPE_P (ctx)) 1209 goto next; 1210 1211 /* If this union member has no explicit initializer and no NSDMI, 1212 splice it out. */ 1213 if (TREE_VALUE (init) || DECL_INITIAL (field)) 1214 /* OK. */; 1215 else 1216 goto splice; 1217 1218 /* It's only an error if we have two initializers for the same 1219 union type. */ 1220 if (!last_p) 1221 { 1222 last_p = p; 1223 goto next; 1224 } 1225 1226 /* See if LAST_FIELD and the field initialized by INIT are 1227 members of the same union (or the union itself). If so, there's 1228 a problem, unless they're actually members of the same structure 1229 which is itself a member of a union. For example, given: 1230 1231 union { struct { int i; int j; }; }; 1232 1233 initializing both `i' and `j' makes sense. */ 1234 ctx = common_enclosing_class 1235 (innermost_aggr_scope (field), 1236 innermost_aggr_scope (TREE_PURPOSE (*last_p))); 1237 1238 if (ctx && (TREE_CODE (ctx) == UNION_TYPE 1239 || ctx == TREE_TYPE (TREE_PURPOSE (*last_p)))) 1240 { 1241 /* A mem-initializer hides an NSDMI. */ 1242 if (TREE_VALUE (init) && !TREE_VALUE (*last_p)) 1243 *last_p = TREE_CHAIN (*last_p); 1244 else if (TREE_VALUE (*last_p) && !TREE_VALUE (init)) 1245 goto splice; 1246 else 1247 { 1248 error_at (DECL_SOURCE_LOCATION (current_function_decl), 1249 "initializations for multiple members of %qT", 1250 ctx); 1251 goto splice; 1252 } 1253 } 1254 1255 last_p = p; 1256 1257 next: 1258 p = &TREE_CHAIN (*p); 1259 continue; 1260 splice: 1261 *p = TREE_CHAIN (*p); 1262 continue; 1263 } 1264 } 1265 1266 return sorted_inits; 1267 } 1268 1269 /* Callback for cp_walk_tree to mark all PARM_DECLs in a tree as read. */ 1270 1271 static tree 1272 mark_exp_read_r (tree *tp, int *, void *) 1273 { 1274 tree t = *tp; 1275 if (TREE_CODE (t) == PARM_DECL) 1276 mark_exp_read (t); 1277 return NULL_TREE; 1278 } 1279 1280 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS 1281 is a TREE_LIST giving the explicit mem-initializer-list for the 1282 constructor. The TREE_PURPOSE of each entry is a subobject (a 1283 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE 1284 is a TREE_LIST giving the arguments to the constructor or 1285 void_type_node for an empty list of arguments. */ 1286 1287 void 1288 emit_mem_initializers (tree mem_inits) 1289 { 1290 int flags = LOOKUP_NORMAL; 1291 1292 /* We will already have issued an error message about the fact that 1293 the type is incomplete. */ 1294 if (!COMPLETE_TYPE_P (current_class_type)) 1295 return; 1296 1297 if (mem_inits 1298 && TYPE_P (TREE_PURPOSE (mem_inits)) 1299 && same_type_p (TREE_PURPOSE (mem_inits), current_class_type)) 1300 { 1301 /* Delegating constructor. */ 1302 gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE); 1303 perform_target_ctor (TREE_VALUE (mem_inits)); 1304 return; 1305 } 1306 1307 if (DECL_DEFAULTED_FN (current_function_decl) 1308 && ! DECL_INHERITED_CTOR (current_function_decl)) 1309 flags |= LOOKUP_DEFAULTED; 1310 1311 /* Sort the mem-initializers into the order in which the 1312 initializations should be performed. */ 1313 mem_inits = sort_mem_initializers (current_class_type, mem_inits); 1314 1315 in_base_initializer = 1; 1316 1317 /* Initialize base classes. */ 1318 for (; (mem_inits 1319 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL); 1320 mem_inits = TREE_CHAIN (mem_inits)) 1321 { 1322 tree subobject = TREE_PURPOSE (mem_inits); 1323 tree arguments = TREE_VALUE (mem_inits); 1324 1325 /* We already have issued an error message. */ 1326 if (arguments == error_mark_node) 1327 continue; 1328 1329 /* Suppress access control when calling the inherited ctor. */ 1330 bool inherited_base = (DECL_INHERITED_CTOR (current_function_decl) 1331 && flag_new_inheriting_ctors 1332 && arguments); 1333 if (inherited_base) 1334 push_deferring_access_checks (dk_deferred); 1335 1336 if (arguments == NULL_TREE) 1337 { 1338 /* If these initializations are taking place in a copy constructor, 1339 the base class should probably be explicitly initialized if there 1340 is a user-defined constructor in the base class (other than the 1341 default constructor, which will be called anyway). */ 1342 if (extra_warnings 1343 && DECL_COPY_CONSTRUCTOR_P (current_function_decl) 1344 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject))) 1345 warning_at (DECL_SOURCE_LOCATION (current_function_decl), 1346 OPT_Wextra, "base class %q#T should be explicitly " 1347 "initialized in the copy constructor", 1348 BINFO_TYPE (subobject)); 1349 } 1350 1351 /* Initialize the base. */ 1352 if (!BINFO_VIRTUAL_P (subobject)) 1353 { 1354 tree base_addr; 1355 1356 base_addr = build_base_path (PLUS_EXPR, current_class_ptr, 1357 subobject, 1, tf_warning_or_error); 1358 expand_aggr_init_1 (subobject, NULL_TREE, 1359 cp_build_fold_indirect_ref (base_addr), 1360 arguments, 1361 flags, 1362 tf_warning_or_error); 1363 expand_cleanup_for_base (subobject, NULL_TREE); 1364 } 1365 else if (!ABSTRACT_CLASS_TYPE_P (current_class_type)) 1366 /* C++14 DR1658 Means we do not have to construct vbases of 1367 abstract classes. */ 1368 construct_virtual_base (subobject, arguments); 1369 else 1370 /* When not constructing vbases of abstract classes, at least mark 1371 the arguments expressions as read to avoid 1372 -Wunused-but-set-parameter false positives. */ 1373 cp_walk_tree (&arguments, mark_exp_read_r, NULL, NULL); 1374 1375 if (inherited_base) 1376 pop_deferring_access_checks (); 1377 } 1378 in_base_initializer = 0; 1379 1380 /* Initialize the vptrs. */ 1381 initialize_vtbl_ptrs (current_class_ptr); 1382 1383 /* Initialize the data members. */ 1384 while (mem_inits) 1385 { 1386 perform_member_init (TREE_PURPOSE (mem_inits), 1387 TREE_VALUE (mem_inits)); 1388 mem_inits = TREE_CHAIN (mem_inits); 1389 } 1390 } 1391 1392 /* Returns the address of the vtable (i.e., the value that should be 1393 assigned to the vptr) for BINFO. */ 1394 1395 tree 1396 build_vtbl_address (tree binfo) 1397 { 1398 tree binfo_for = binfo; 1399 tree vtbl; 1400 1401 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo)) 1402 /* If this is a virtual primary base, then the vtable we want to store 1403 is that for the base this is being used as the primary base of. We 1404 can't simply skip the initialization, because we may be expanding the 1405 inits of a subobject constructor where the virtual base layout 1406 can be different. */ 1407 while (BINFO_PRIMARY_P (binfo_for)) 1408 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for); 1409 1410 /* Figure out what vtable BINFO's vtable is based on, and mark it as 1411 used. */ 1412 vtbl = get_vtbl_decl_for_binfo (binfo_for); 1413 TREE_USED (vtbl) = true; 1414 1415 /* Now compute the address to use when initializing the vptr. */ 1416 vtbl = unshare_expr (BINFO_VTABLE (binfo_for)); 1417 if (VAR_P (vtbl)) 1418 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl); 1419 1420 return vtbl; 1421 } 1422 1423 /* This code sets up the virtual function tables appropriate for 1424 the pointer DECL. It is a one-ply initialization. 1425 1426 BINFO is the exact type that DECL is supposed to be. In 1427 multiple inheritance, this might mean "C's A" if C : A, B. */ 1428 1429 static void 1430 expand_virtual_init (tree binfo, tree decl) 1431 { 1432 tree vtbl, vtbl_ptr; 1433 tree vtt_index; 1434 1435 /* Compute the initializer for vptr. */ 1436 vtbl = build_vtbl_address (binfo); 1437 1438 /* We may get this vptr from a VTT, if this is a subobject 1439 constructor or subobject destructor. */ 1440 vtt_index = BINFO_VPTR_INDEX (binfo); 1441 if (vtt_index) 1442 { 1443 tree vtbl2; 1444 tree vtt_parm; 1445 1446 /* Compute the value to use, when there's a VTT. */ 1447 vtt_parm = current_vtt_parm; 1448 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index); 1449 vtbl2 = cp_build_fold_indirect_ref (vtbl2); 1450 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2); 1451 1452 /* The actual initializer is the VTT value only in the subobject 1453 constructor. In maybe_clone_body we'll substitute NULL for 1454 the vtt_parm in the case of the non-subobject constructor. */ 1455 vtbl = build_if_in_charge (vtbl, vtbl2); 1456 } 1457 1458 /* Compute the location of the vtpr. */ 1459 vtbl_ptr = build_vfield_ref (cp_build_fold_indirect_ref (decl), 1460 TREE_TYPE (binfo)); 1461 gcc_assert (vtbl_ptr != error_mark_node); 1462 1463 /* Assign the vtable to the vptr. */ 1464 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error); 1465 finish_expr_stmt (cp_build_modify_expr (input_location, vtbl_ptr, NOP_EXPR, 1466 vtbl, tf_warning_or_error)); 1467 } 1468 1469 /* If an exception is thrown in a constructor, those base classes already 1470 constructed must be destroyed. This function creates the cleanup 1471 for BINFO, which has just been constructed. If FLAG is non-NULL, 1472 it is a DECL which is nonzero when this base needs to be 1473 destroyed. */ 1474 1475 static void 1476 expand_cleanup_for_base (tree binfo, tree flag) 1477 { 1478 tree expr; 1479 1480 if (!type_build_dtor_call (BINFO_TYPE (binfo))) 1481 return; 1482 1483 /* Call the destructor. */ 1484 expr = build_special_member_call (current_class_ref, 1485 base_dtor_identifier, 1486 NULL, 1487 binfo, 1488 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL, 1489 tf_warning_or_error); 1490 1491 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo))) 1492 return; 1493 1494 if (flag) 1495 expr = fold_build3_loc (input_location, 1496 COND_EXPR, void_type_node, 1497 c_common_truthvalue_conversion (input_location, flag), 1498 expr, integer_zero_node); 1499 1500 finish_eh_cleanup (expr); 1501 } 1502 1503 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its 1504 constructor. */ 1505 1506 static void 1507 construct_virtual_base (tree vbase, tree arguments) 1508 { 1509 tree inner_if_stmt; 1510 tree exp; 1511 tree flag; 1512 1513 /* If there are virtual base classes with destructors, we need to 1514 emit cleanups to destroy them if an exception is thrown during 1515 the construction process. These exception regions (i.e., the 1516 period during which the cleanups must occur) begin from the time 1517 the construction is complete to the end of the function. If we 1518 create a conditional block in which to initialize the 1519 base-classes, then the cleanup region for the virtual base begins 1520 inside a block, and ends outside of that block. This situation 1521 confuses the sjlj exception-handling code. Therefore, we do not 1522 create a single conditional block, but one for each 1523 initialization. (That way the cleanup regions always begin 1524 in the outer block.) We trust the back end to figure out 1525 that the FLAG will not change across initializations, and 1526 avoid doing multiple tests. */ 1527 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl)); 1528 inner_if_stmt = begin_if_stmt (); 1529 finish_if_stmt_cond (flag, inner_if_stmt); 1530 1531 /* Compute the location of the virtual base. If we're 1532 constructing virtual bases, then we must be the most derived 1533 class. Therefore, we don't have to look up the virtual base; 1534 we already know where it is. */ 1535 exp = convert_to_base_statically (current_class_ref, vbase); 1536 1537 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments, 1538 0, tf_warning_or_error); 1539 finish_then_clause (inner_if_stmt); 1540 finish_if_stmt (inner_if_stmt); 1541 1542 expand_cleanup_for_base (vbase, flag); 1543 } 1544 1545 /* Find the context in which this FIELD can be initialized. */ 1546 1547 static tree 1548 initializing_context (tree field) 1549 { 1550 tree t = DECL_CONTEXT (field); 1551 1552 /* Anonymous union members can be initialized in the first enclosing 1553 non-anonymous union context. */ 1554 while (t && ANON_AGGR_TYPE_P (t)) 1555 t = TYPE_CONTEXT (t); 1556 return t; 1557 } 1558 1559 /* Function to give error message if member initialization specification 1560 is erroneous. FIELD is the member we decided to initialize. 1561 TYPE is the type for which the initialization is being performed. 1562 FIELD must be a member of TYPE. 1563 1564 MEMBER_NAME is the name of the member. */ 1565 1566 static int 1567 member_init_ok_or_else (tree field, tree type, tree member_name) 1568 { 1569 if (field == error_mark_node) 1570 return 0; 1571 if (!field) 1572 { 1573 error ("class %qT does not have any field named %qD", type, 1574 member_name); 1575 return 0; 1576 } 1577 if (VAR_P (field)) 1578 { 1579 error ("%q#D is a static data member; it can only be " 1580 "initialized at its definition", 1581 field); 1582 return 0; 1583 } 1584 if (TREE_CODE (field) != FIELD_DECL) 1585 { 1586 error ("%q#D is not a non-static data member of %qT", 1587 field, type); 1588 return 0; 1589 } 1590 if (initializing_context (field) != type) 1591 { 1592 error ("class %qT does not have any field named %qD", type, 1593 member_name); 1594 return 0; 1595 } 1596 1597 return 1; 1598 } 1599 1600 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it 1601 is a _TYPE node or TYPE_DECL which names a base for that type. 1602 Check the validity of NAME, and return either the base _TYPE, base 1603 binfo, or the FIELD_DECL of the member. If NAME is invalid, return 1604 NULL_TREE and issue a diagnostic. 1605 1606 An old style unnamed direct single base construction is permitted, 1607 where NAME is NULL. */ 1608 1609 tree 1610 expand_member_init (tree name) 1611 { 1612 tree basetype; 1613 tree field; 1614 1615 if (!current_class_ref) 1616 return NULL_TREE; 1617 1618 if (!name) 1619 { 1620 /* This is an obsolete unnamed base class initializer. The 1621 parser will already have warned about its use. */ 1622 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type))) 1623 { 1624 case 0: 1625 error ("unnamed initializer for %qT, which has no base classes", 1626 current_class_type); 1627 return NULL_TREE; 1628 case 1: 1629 basetype = BINFO_TYPE 1630 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0)); 1631 break; 1632 default: 1633 error ("unnamed initializer for %qT, which uses multiple inheritance", 1634 current_class_type); 1635 return NULL_TREE; 1636 } 1637 } 1638 else if (TYPE_P (name)) 1639 { 1640 basetype = TYPE_MAIN_VARIANT (name); 1641 name = TYPE_NAME (name); 1642 } 1643 else if (TREE_CODE (name) == TYPE_DECL) 1644 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name)); 1645 else 1646 basetype = NULL_TREE; 1647 1648 if (basetype) 1649 { 1650 tree class_binfo; 1651 tree direct_binfo; 1652 tree virtual_binfo; 1653 int i; 1654 1655 if (current_template_parms 1656 || same_type_p (basetype, current_class_type)) 1657 return basetype; 1658 1659 class_binfo = TYPE_BINFO (current_class_type); 1660 direct_binfo = NULL_TREE; 1661 virtual_binfo = NULL_TREE; 1662 1663 /* Look for a direct base. */ 1664 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i) 1665 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype)) 1666 break; 1667 1668 /* Look for a virtual base -- unless the direct base is itself 1669 virtual. */ 1670 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo)) 1671 virtual_binfo = binfo_for_vbase (basetype, current_class_type); 1672 1673 /* [class.base.init] 1674 1675 If a mem-initializer-id is ambiguous because it designates 1676 both a direct non-virtual base class and an inherited virtual 1677 base class, the mem-initializer is ill-formed. */ 1678 if (direct_binfo && virtual_binfo) 1679 { 1680 error ("%qD is both a direct base and an indirect virtual base", 1681 basetype); 1682 return NULL_TREE; 1683 } 1684 1685 if (!direct_binfo && !virtual_binfo) 1686 { 1687 if (CLASSTYPE_VBASECLASSES (current_class_type)) 1688 error ("type %qT is not a direct or virtual base of %qT", 1689 basetype, current_class_type); 1690 else 1691 error ("type %qT is not a direct base of %qT", 1692 basetype, current_class_type); 1693 return NULL_TREE; 1694 } 1695 1696 return direct_binfo ? direct_binfo : virtual_binfo; 1697 } 1698 else 1699 { 1700 if (identifier_p (name)) 1701 field = lookup_field (current_class_type, name, 1, false); 1702 else 1703 field = name; 1704 1705 if (member_init_ok_or_else (field, current_class_type, name)) 1706 return field; 1707 } 1708 1709 return NULL_TREE; 1710 } 1711 1712 /* This is like `expand_member_init', only it stores one aggregate 1713 value into another. 1714 1715 INIT comes in two flavors: it is either a value which 1716 is to be stored in EXP, or it is a parameter list 1717 to go to a constructor, which will operate on EXP. 1718 If INIT is not a parameter list for a constructor, then set 1719 LOOKUP_ONLYCONVERTING. 1720 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of 1721 the initializer, if FLAGS is 0, then it is the (init) form. 1722 If `init' is a CONSTRUCTOR, then we emit a warning message, 1723 explaining that such initializations are invalid. 1724 1725 If INIT resolves to a CALL_EXPR which happens to return 1726 something of the type we are looking for, then we know 1727 that we can safely use that call to perform the 1728 initialization. 1729 1730 The virtual function table pointer cannot be set up here, because 1731 we do not really know its type. 1732 1733 This never calls operator=(). 1734 1735 When initializing, nothing is CONST. 1736 1737 A default copy constructor may have to be used to perform the 1738 initialization. 1739 1740 A constructor or a conversion operator may have to be used to 1741 perform the initialization, but not both, as it would be ambiguous. */ 1742 1743 tree 1744 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain) 1745 { 1746 tree stmt_expr; 1747 tree compound_stmt; 1748 int destroy_temps; 1749 tree type = TREE_TYPE (exp); 1750 int was_const = TREE_READONLY (exp); 1751 int was_volatile = TREE_THIS_VOLATILE (exp); 1752 int is_global; 1753 1754 if (init == error_mark_node) 1755 return error_mark_node; 1756 1757 location_t init_loc = (init 1758 ? cp_expr_loc_or_loc (init, input_location) 1759 : location_of (exp)); 1760 1761 TREE_READONLY (exp) = 0; 1762 TREE_THIS_VOLATILE (exp) = 0; 1763 1764 if (TREE_CODE (type) == ARRAY_TYPE) 1765 { 1766 tree itype = init ? TREE_TYPE (init) : NULL_TREE; 1767 int from_array = 0; 1768 1769 if (VAR_P (exp) && DECL_DECOMPOSITION_P (exp)) 1770 { 1771 from_array = 1; 1772 init = mark_rvalue_use (init); 1773 if (init 1774 && DECL_P (tree_strip_any_location_wrapper (init)) 1775 && !(flags & LOOKUP_ONLYCONVERTING)) 1776 { 1777 /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init 1778 recognizes it as direct-initialization. */ 1779 init = build_constructor_single (init_list_type_node, 1780 NULL_TREE, init); 1781 CONSTRUCTOR_IS_DIRECT_INIT (init) = true; 1782 } 1783 } 1784 else 1785 { 1786 /* Must arrange to initialize each element of EXP 1787 from elements of INIT. */ 1788 if (cv_qualified_p (type)) 1789 TREE_TYPE (exp) = cv_unqualified (type); 1790 if (itype && cv_qualified_p (itype)) 1791 TREE_TYPE (init) = cv_unqualified (itype); 1792 from_array = (itype && same_type_p (TREE_TYPE (init), 1793 TREE_TYPE (exp))); 1794 1795 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init) 1796 && (!from_array 1797 || (TREE_CODE (init) != CONSTRUCTOR 1798 /* Can happen, eg, handling the compound-literals 1799 extension (ext/complit12.C). */ 1800 && TREE_CODE (init) != TARGET_EXPR))) 1801 { 1802 if (complain & tf_error) 1803 error_at (init_loc, "array must be initialized " 1804 "with a brace-enclosed initializer"); 1805 return error_mark_node; 1806 } 1807 } 1808 1809 stmt_expr = build_vec_init (exp, NULL_TREE, init, 1810 /*explicit_value_init_p=*/false, 1811 from_array, 1812 complain); 1813 TREE_READONLY (exp) = was_const; 1814 TREE_THIS_VOLATILE (exp) = was_volatile; 1815 TREE_TYPE (exp) = type; 1816 /* Restore the type of init unless it was used directly. */ 1817 if (init && TREE_CODE (stmt_expr) != INIT_EXPR) 1818 TREE_TYPE (init) = itype; 1819 return stmt_expr; 1820 } 1821 1822 if (init && init != void_type_node 1823 && TREE_CODE (init) != TREE_LIST 1824 && !(TREE_CODE (init) == TARGET_EXPR 1825 && TARGET_EXPR_DIRECT_INIT_P (init)) 1826 && !DIRECT_LIST_INIT_P (init)) 1827 flags |= LOOKUP_ONLYCONVERTING; 1828 1829 is_global = begin_init_stmts (&stmt_expr, &compound_stmt); 1830 destroy_temps = stmts_are_full_exprs_p (); 1831 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 1832 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp, 1833 init, LOOKUP_NORMAL|flags, complain); 1834 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); 1835 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; 1836 TREE_READONLY (exp) = was_const; 1837 TREE_THIS_VOLATILE (exp) = was_volatile; 1838 1839 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL) 1840 && TREE_SIDE_EFFECTS (stmt_expr) 1841 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type))) 1842 /* Just know that we've seen something for this node. */ 1843 TREE_USED (exp) = 1; 1844 1845 return stmt_expr; 1846 } 1847 1848 static void 1849 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags, 1850 tsubst_flags_t complain) 1851 { 1852 tree type = TREE_TYPE (exp); 1853 1854 /* It fails because there may not be a constructor which takes 1855 its own type as the first (or only parameter), but which does 1856 take other types via a conversion. So, if the thing initializing 1857 the expression is a unit element of type X, first try X(X&), 1858 followed by initialization by X. If neither of these work 1859 out, then look hard. */ 1860 tree rval; 1861 vec<tree, va_gc> *parms; 1862 1863 /* If we have direct-initialization from an initializer list, pull 1864 it out of the TREE_LIST so the code below can see it. */ 1865 if (init && TREE_CODE (init) == TREE_LIST 1866 && DIRECT_LIST_INIT_P (TREE_VALUE (init))) 1867 { 1868 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0 1869 && TREE_CHAIN (init) == NULL_TREE); 1870 init = TREE_VALUE (init); 1871 /* Only call reshape_init if it has not been called earlier 1872 by the callers. */ 1873 if (BRACE_ENCLOSED_INITIALIZER_P (init) && CP_AGGREGATE_TYPE_P (type)) 1874 init = reshape_init (type, init, complain); 1875 } 1876 1877 if (init && BRACE_ENCLOSED_INITIALIZER_P (init) 1878 && CP_AGGREGATE_TYPE_P (type)) 1879 /* A brace-enclosed initializer for an aggregate. In C++0x this can 1880 happen for direct-initialization, too. */ 1881 init = digest_init (type, init, complain); 1882 1883 /* A CONSTRUCTOR of the target's type is a previously digested 1884 initializer, whether that happened just above or in 1885 cp_parser_late_parsing_nsdmi. 1886 1887 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P 1888 set represents the whole initialization, so we shouldn't build up 1889 another ctor call. */ 1890 if (init 1891 && (TREE_CODE (init) == CONSTRUCTOR 1892 || (TREE_CODE (init) == TARGET_EXPR 1893 && (TARGET_EXPR_DIRECT_INIT_P (init) 1894 || TARGET_EXPR_LIST_INIT_P (init)))) 1895 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type)) 1896 { 1897 /* Early initialization via a TARGET_EXPR only works for 1898 complete objects. */ 1899 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp); 1900 1901 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init); 1902 TREE_SIDE_EFFECTS (init) = 1; 1903 finish_expr_stmt (init); 1904 return; 1905 } 1906 1907 if (init && TREE_CODE (init) != TREE_LIST 1908 && (flags & LOOKUP_ONLYCONVERTING)) 1909 { 1910 /* Base subobjects should only get direct-initialization. */ 1911 gcc_assert (true_exp == exp); 1912 1913 if (flags & DIRECT_BIND) 1914 /* Do nothing. We hit this in two cases: Reference initialization, 1915 where we aren't initializing a real variable, so we don't want 1916 to run a new constructor; and catching an exception, where we 1917 have already built up the constructor call so we could wrap it 1918 in an exception region. */; 1919 else 1920 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, 1921 flags, complain); 1922 1923 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR) 1924 /* We need to protect the initialization of a catch parm with a 1925 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR 1926 around the TARGET_EXPR for the copy constructor. See 1927 initialize_handler_parm. */ 1928 { 1929 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp, 1930 TREE_OPERAND (init, 0)); 1931 TREE_TYPE (init) = void_type_node; 1932 } 1933 else 1934 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init); 1935 TREE_SIDE_EFFECTS (init) = 1; 1936 finish_expr_stmt (init); 1937 return; 1938 } 1939 1940 if (init == NULL_TREE) 1941 parms = NULL; 1942 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init)) 1943 { 1944 parms = make_tree_vector (); 1945 for (; init != NULL_TREE; init = TREE_CHAIN (init)) 1946 vec_safe_push (parms, TREE_VALUE (init)); 1947 } 1948 else 1949 parms = make_tree_vector_single (init); 1950 1951 if (exp == current_class_ref && current_function_decl 1952 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl)) 1953 { 1954 /* Delegating constructor. */ 1955 tree complete; 1956 tree base; 1957 tree elt; unsigned i; 1958 1959 /* Unshare the arguments for the second call. */ 1960 vec<tree, va_gc> *parms2 = make_tree_vector (); 1961 FOR_EACH_VEC_SAFE_ELT (parms, i, elt) 1962 { 1963 elt = break_out_target_exprs (elt); 1964 vec_safe_push (parms2, elt); 1965 } 1966 complete = build_special_member_call (exp, complete_ctor_identifier, 1967 &parms2, binfo, flags, 1968 complain); 1969 complete = fold_build_cleanup_point_expr (void_type_node, complete); 1970 release_tree_vector (parms2); 1971 1972 base = build_special_member_call (exp, base_ctor_identifier, 1973 &parms, binfo, flags, 1974 complain); 1975 base = fold_build_cleanup_point_expr (void_type_node, base); 1976 rval = build_if_in_charge (complete, base); 1977 } 1978 else 1979 { 1980 tree ctor_name = (true_exp == exp 1981 ? complete_ctor_identifier : base_ctor_identifier); 1982 1983 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags, 1984 complain); 1985 } 1986 1987 if (parms != NULL) 1988 release_tree_vector (parms); 1989 1990 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR) 1991 { 1992 tree fn = get_callee_fndecl (rval); 1993 if (fn && DECL_DECLARED_CONSTEXPR_P (fn)) 1994 { 1995 tree e = maybe_constant_init (rval, exp); 1996 if (TREE_CONSTANT (e)) 1997 rval = build2 (INIT_EXPR, type, exp, e); 1998 } 1999 } 2000 2001 /* FIXME put back convert_to_void? */ 2002 if (TREE_SIDE_EFFECTS (rval)) 2003 finish_expr_stmt (rval); 2004 } 2005 2006 /* This function is responsible for initializing EXP with INIT 2007 (if any). 2008 2009 BINFO is the binfo of the type for who we are performing the 2010 initialization. For example, if W is a virtual base class of A and B, 2011 and C : A, B. 2012 If we are initializing B, then W must contain B's W vtable, whereas 2013 were we initializing C, W must contain C's W vtable. 2014 2015 TRUE_EXP is nonzero if it is the true expression being initialized. 2016 In this case, it may be EXP, or may just contain EXP. The reason we 2017 need this is because if EXP is a base element of TRUE_EXP, we 2018 don't necessarily know by looking at EXP where its virtual 2019 baseclass fields should really be pointing. But we do know 2020 from TRUE_EXP. In constructors, we don't know anything about 2021 the value being initialized. 2022 2023 FLAGS is just passed to `build_new_method_call'. See that function 2024 for its description. */ 2025 2026 static void 2027 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags, 2028 tsubst_flags_t complain) 2029 { 2030 tree type = TREE_TYPE (exp); 2031 2032 gcc_assert (init != error_mark_node && type != error_mark_node); 2033 gcc_assert (building_stmt_list_p ()); 2034 2035 /* Use a function returning the desired type to initialize EXP for us. 2036 If the function is a constructor, and its first argument is 2037 NULL_TREE, know that it was meant for us--just slide exp on 2038 in and expand the constructor. Constructors now come 2039 as TARGET_EXPRs. */ 2040 2041 if (init && VAR_P (exp) 2042 && COMPOUND_LITERAL_P (init)) 2043 { 2044 vec<tree, va_gc> *cleanups = NULL; 2045 /* If store_init_value returns NULL_TREE, the INIT has been 2046 recorded as the DECL_INITIAL for EXP. That means there's 2047 nothing more we have to do. */ 2048 init = store_init_value (exp, init, &cleanups, flags); 2049 if (init) 2050 finish_expr_stmt (init); 2051 gcc_assert (!cleanups); 2052 return; 2053 } 2054 2055 /* List-initialization from {} becomes value-initialization for non-aggregate 2056 classes with default constructors. Handle this here when we're 2057 initializing a base, so protected access works. */ 2058 if (exp != true_exp && init && TREE_CODE (init) == TREE_LIST) 2059 { 2060 tree elt = TREE_VALUE (init); 2061 if (DIRECT_LIST_INIT_P (elt) 2062 && CONSTRUCTOR_ELTS (elt) == 0 2063 && CLASSTYPE_NON_AGGREGATE (type) 2064 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) 2065 init = void_type_node; 2066 } 2067 2068 /* If an explicit -- but empty -- initializer list was present, 2069 that's value-initialization. */ 2070 if (init == void_type_node) 2071 { 2072 /* If the type has data but no user-provided ctor, we need to zero 2073 out the object. */ 2074 if (!type_has_user_provided_constructor (type) 2075 && !is_really_empty_class (type, /*ignore_vptr*/true)) 2076 { 2077 tree field_size = NULL_TREE; 2078 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type) 2079 /* Don't clobber already initialized virtual bases. */ 2080 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type)); 2081 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false, 2082 field_size); 2083 init = build2 (INIT_EXPR, type, exp, init); 2084 finish_expr_stmt (init); 2085 } 2086 2087 /* If we don't need to mess with the constructor at all, 2088 then we're done. */ 2089 if (! type_build_ctor_call (type)) 2090 return; 2091 2092 /* Otherwise fall through and call the constructor. */ 2093 init = NULL_TREE; 2094 } 2095 2096 /* We know that expand_default_init can handle everything we want 2097 at this point. */ 2098 expand_default_init (binfo, true_exp, exp, init, flags, complain); 2099 } 2100 2101 /* Report an error if TYPE is not a user-defined, class type. If 2102 OR_ELSE is nonzero, give an error message. */ 2103 2104 int 2105 is_class_type (tree type, int or_else) 2106 { 2107 if (type == error_mark_node) 2108 return 0; 2109 2110 if (! CLASS_TYPE_P (type)) 2111 { 2112 if (or_else) 2113 error ("%qT is not a class type", type); 2114 return 0; 2115 } 2116 return 1; 2117 } 2118 2119 tree 2120 get_type_value (tree name) 2121 { 2122 if (name == error_mark_node) 2123 return NULL_TREE; 2124 2125 if (IDENTIFIER_HAS_TYPE_VALUE (name)) 2126 return IDENTIFIER_TYPE_VALUE (name); 2127 else 2128 return NULL_TREE; 2129 } 2130 2131 /* Build a reference to a member of an aggregate. This is not a C++ 2132 `&', but really something which can have its address taken, and 2133 then act as a pointer to member, for example TYPE :: FIELD can have 2134 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if 2135 this expression is the operand of "&". 2136 2137 @@ Prints out lousy diagnostics for operator <typename> 2138 @@ fields. 2139 2140 @@ This function should be rewritten and placed in search.c. */ 2141 2142 tree 2143 build_offset_ref (tree type, tree member, bool address_p, 2144 tsubst_flags_t complain) 2145 { 2146 tree decl; 2147 tree basebinfo = NULL_TREE; 2148 2149 /* class templates can come in as TEMPLATE_DECLs here. */ 2150 if (TREE_CODE (member) == TEMPLATE_DECL) 2151 return member; 2152 2153 if (dependent_scope_p (type) || type_dependent_expression_p (member)) 2154 return build_qualified_name (NULL_TREE, type, member, 2155 /*template_p=*/false); 2156 2157 gcc_assert (TYPE_P (type)); 2158 if (! is_class_type (type, 1)) 2159 return error_mark_node; 2160 2161 gcc_assert (DECL_P (member) || BASELINK_P (member)); 2162 /* Callers should call mark_used before this point. */ 2163 gcc_assert (!DECL_P (member) || TREE_USED (member)); 2164 2165 type = TYPE_MAIN_VARIANT (type); 2166 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type))) 2167 { 2168 if (complain & tf_error) 2169 error ("incomplete type %qT does not have member %qD", type, member); 2170 return error_mark_node; 2171 } 2172 2173 /* Entities other than non-static members need no further 2174 processing. */ 2175 if (TREE_CODE (member) == TYPE_DECL) 2176 return member; 2177 if (VAR_P (member) || TREE_CODE (member) == CONST_DECL) 2178 return convert_from_reference (member); 2179 2180 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member)) 2181 { 2182 if (complain & tf_error) 2183 error ("invalid pointer to bit-field %qD", member); 2184 return error_mark_node; 2185 } 2186 2187 /* Set up BASEBINFO for member lookup. */ 2188 decl = maybe_dummy_object (type, &basebinfo); 2189 2190 /* A lot of this logic is now handled in lookup_member. */ 2191 if (BASELINK_P (member)) 2192 { 2193 /* Go from the TREE_BASELINK to the member function info. */ 2194 tree t = BASELINK_FUNCTIONS (member); 2195 2196 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t)) 2197 { 2198 /* Get rid of a potential OVERLOAD around it. */ 2199 t = OVL_FIRST (t); 2200 2201 /* Unique functions are handled easily. */ 2202 2203 /* For non-static member of base class, we need a special rule 2204 for access checking [class.protected]: 2205 2206 If the access is to form a pointer to member, the 2207 nested-name-specifier shall name the derived class 2208 (or any class derived from that class). */ 2209 bool ok; 2210 if (address_p && DECL_P (t) 2211 && DECL_NONSTATIC_MEMBER_P (t)) 2212 ok = perform_or_defer_access_check (TYPE_BINFO (type), t, t, 2213 complain); 2214 else 2215 ok = perform_or_defer_access_check (basebinfo, t, t, 2216 complain); 2217 if (!ok) 2218 return error_mark_node; 2219 if (DECL_STATIC_FUNCTION_P (t)) 2220 return t; 2221 member = t; 2222 } 2223 else 2224 TREE_TYPE (member) = unknown_type_node; 2225 } 2226 else if (address_p && TREE_CODE (member) == FIELD_DECL) 2227 { 2228 /* We need additional test besides the one in 2229 check_accessibility_of_qualified_id in case it is 2230 a pointer to non-static member. */ 2231 if (!perform_or_defer_access_check (TYPE_BINFO (type), member, member, 2232 complain)) 2233 return error_mark_node; 2234 } 2235 2236 if (!address_p) 2237 { 2238 /* If MEMBER is non-static, then the program has fallen afoul of 2239 [expr.prim]: 2240 2241 An id-expression that denotes a nonstatic data member or 2242 nonstatic member function of a class can only be used: 2243 2244 -- as part of a class member access (_expr.ref_) in which the 2245 object-expression refers to the member's class or a class 2246 derived from that class, or 2247 2248 -- to form a pointer to member (_expr.unary.op_), or 2249 2250 -- in the body of a nonstatic member function of that class or 2251 of a class derived from that class (_class.mfct.nonstatic_), or 2252 2253 -- in a mem-initializer for a constructor for that class or for 2254 a class derived from that class (_class.base.init_). */ 2255 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member)) 2256 { 2257 /* Build a representation of the qualified name suitable 2258 for use as the operand to "&" -- even though the "&" is 2259 not actually present. */ 2260 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member); 2261 /* In Microsoft mode, treat a non-static member function as if 2262 it were a pointer-to-member. */ 2263 if (flag_ms_extensions) 2264 { 2265 PTRMEM_OK_P (member) = 1; 2266 return cp_build_addr_expr (member, complain); 2267 } 2268 if (complain & tf_error) 2269 error ("invalid use of non-static member function %qD", 2270 TREE_OPERAND (member, 1)); 2271 return error_mark_node; 2272 } 2273 else if (TREE_CODE (member) == FIELD_DECL) 2274 { 2275 if (complain & tf_error) 2276 error ("invalid use of non-static data member %qD", member); 2277 return error_mark_node; 2278 } 2279 return member; 2280 } 2281 2282 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member); 2283 PTRMEM_OK_P (member) = 1; 2284 return member; 2285 } 2286 2287 /* If DECL is a scalar enumeration constant or variable with a 2288 constant initializer, return the initializer (or, its initializers, 2289 recursively); otherwise, return DECL. If STRICT_P, the 2290 initializer is only returned if DECL is a 2291 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to 2292 return an aggregate constant. */ 2293 2294 static tree 2295 constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p) 2296 { 2297 while (TREE_CODE (decl) == CONST_DECL 2298 || decl_constant_var_p (decl) 2299 || (!strict_p && VAR_P (decl) 2300 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))) 2301 { 2302 tree init; 2303 /* If DECL is a static data member in a template 2304 specialization, we must instantiate it here. The 2305 initializer for the static data member is not processed 2306 until needed; we need it now. */ 2307 mark_used (decl, tf_none); 2308 init = DECL_INITIAL (decl); 2309 if (init == error_mark_node) 2310 { 2311 if (TREE_CODE (decl) == CONST_DECL 2312 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) 2313 /* Treat the error as a constant to avoid cascading errors on 2314 excessively recursive template instantiation (c++/9335). */ 2315 return init; 2316 else 2317 return decl; 2318 } 2319 /* Initializers in templates are generally expanded during 2320 instantiation, so before that for const int i(2) 2321 INIT is a TREE_LIST with the actual initializer as 2322 TREE_VALUE. */ 2323 if (processing_template_decl 2324 && init 2325 && TREE_CODE (init) == TREE_LIST 2326 && TREE_CHAIN (init) == NULL_TREE) 2327 init = TREE_VALUE (init); 2328 /* Instantiate a non-dependent initializer for user variables. We 2329 mustn't do this for the temporary for an array compound literal; 2330 trying to instatiate the initializer will keep creating new 2331 temporaries until we crash. Probably it's not useful to do it for 2332 other artificial variables, either. */ 2333 if (!DECL_ARTIFICIAL (decl)) 2334 init = instantiate_non_dependent_or_null (init); 2335 if (!init 2336 || !TREE_TYPE (init) 2337 || !TREE_CONSTANT (init) 2338 || (!return_aggregate_cst_ok_p 2339 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not 2340 return an aggregate constant (of which string 2341 literals are a special case), as we do not want 2342 to make inadvertent copies of such entities, and 2343 we must be sure that their addresses are the 2344 same everywhere. */ 2345 && (TREE_CODE (init) == CONSTRUCTOR 2346 || TREE_CODE (init) == STRING_CST))) 2347 break; 2348 /* Don't return a CONSTRUCTOR for a variable with partial run-time 2349 initialization, since it doesn't represent the entire value. 2350 Similarly for VECTOR_CSTs created by cp_folding those 2351 CONSTRUCTORs. */ 2352 if ((TREE_CODE (init) == CONSTRUCTOR 2353 || TREE_CODE (init) == VECTOR_CST) 2354 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) 2355 break; 2356 /* If the variable has a dynamic initializer, don't use its 2357 DECL_INITIAL which doesn't reflect the real value. */ 2358 if (VAR_P (decl) 2359 && TREE_STATIC (decl) 2360 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) 2361 && DECL_NONTRIVIALLY_INITIALIZED_P (decl)) 2362 break; 2363 decl = unshare_expr (init); 2364 } 2365 return decl; 2366 } 2367 2368 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant 2369 of integral or enumeration type, or a constexpr variable of scalar type, 2370 then return that value. These are those variables permitted in constant 2371 expressions by [5.19/1]. */ 2372 2373 tree 2374 scalar_constant_value (tree decl) 2375 { 2376 return constant_value_1 (decl, /*strict_p=*/true, 2377 /*return_aggregate_cst_ok_p=*/false); 2378 } 2379 2380 /* Like scalar_constant_value, but can also return aggregate initializers. */ 2381 2382 tree 2383 decl_really_constant_value (tree decl) 2384 { 2385 return constant_value_1 (decl, /*strict_p=*/true, 2386 /*return_aggregate_cst_ok_p=*/true); 2387 } 2388 2389 /* A more relaxed version of scalar_constant_value, used by the 2390 common C/C++ code. */ 2391 2392 tree 2393 decl_constant_value (tree decl) 2394 { 2395 return constant_value_1 (decl, /*strict_p=*/processing_template_decl, 2396 /*return_aggregate_cst_ok_p=*/true); 2397 } 2398 2399 /* Common subroutines of build_new and build_vec_delete. */ 2400 2401 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is 2402 the type of the object being allocated; otherwise, it's just TYPE. 2403 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the 2404 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is 2405 a vector of arguments to be provided as arguments to a placement 2406 new operator. This routine performs no semantic checks; it just 2407 creates and returns a NEW_EXPR. */ 2408 2409 static tree 2410 build_raw_new_expr (vec<tree, va_gc> *placement, tree type, tree nelts, 2411 vec<tree, va_gc> *init, int use_global_new) 2412 { 2413 tree init_list; 2414 tree new_expr; 2415 2416 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR. 2417 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This 2418 permits us to distinguish the case of a missing initializer "new 2419 int" from an empty initializer "new int()". */ 2420 if (init == NULL) 2421 init_list = NULL_TREE; 2422 else if (init->is_empty ()) 2423 init_list = void_node; 2424 else 2425 init_list = build_tree_list_vec (init); 2426 2427 new_expr = build4 (NEW_EXPR, build_pointer_type (type), 2428 build_tree_list_vec (placement), type, nelts, 2429 init_list); 2430 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new; 2431 TREE_SIDE_EFFECTS (new_expr) = 1; 2432 2433 return new_expr; 2434 } 2435 2436 /* Diagnose uninitialized const members or reference members of type 2437 TYPE. USING_NEW is used to disambiguate the diagnostic between a 2438 new expression without a new-initializer and a declaration. Returns 2439 the error count. */ 2440 2441 static int 2442 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin, 2443 bool using_new, bool complain) 2444 { 2445 tree field; 2446 int error_count = 0; 2447 2448 if (type_has_user_provided_constructor (type)) 2449 return 0; 2450 2451 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 2452 { 2453 tree field_type; 2454 2455 if (TREE_CODE (field) != FIELD_DECL) 2456 continue; 2457 2458 field_type = strip_array_types (TREE_TYPE (field)); 2459 2460 if (type_has_user_provided_constructor (field_type)) 2461 continue; 2462 2463 if (TYPE_REF_P (field_type)) 2464 { 2465 ++ error_count; 2466 if (complain) 2467 { 2468 if (DECL_CONTEXT (field) == origin) 2469 { 2470 if (using_new) 2471 error ("uninitialized reference member in %q#T " 2472 "using %<new%> without new-initializer", origin); 2473 else 2474 error ("uninitialized reference member in %q#T", origin); 2475 } 2476 else 2477 { 2478 if (using_new) 2479 error ("uninitialized reference member in base %q#T " 2480 "of %q#T using %<new%> without new-initializer", 2481 DECL_CONTEXT (field), origin); 2482 else 2483 error ("uninitialized reference member in base %q#T " 2484 "of %q#T", DECL_CONTEXT (field), origin); 2485 } 2486 inform (DECL_SOURCE_LOCATION (field), 2487 "%q#D should be initialized", field); 2488 } 2489 } 2490 2491 if (CP_TYPE_CONST_P (field_type)) 2492 { 2493 ++ error_count; 2494 if (complain) 2495 { 2496 if (DECL_CONTEXT (field) == origin) 2497 { 2498 if (using_new) 2499 error ("uninitialized const member in %q#T " 2500 "using %<new%> without new-initializer", origin); 2501 else 2502 error ("uninitialized const member in %q#T", origin); 2503 } 2504 else 2505 { 2506 if (using_new) 2507 error ("uninitialized const member in base %q#T " 2508 "of %q#T using %<new%> without new-initializer", 2509 DECL_CONTEXT (field), origin); 2510 else 2511 error ("uninitialized const member in base %q#T " 2512 "of %q#T", DECL_CONTEXT (field), origin); 2513 } 2514 inform (DECL_SOURCE_LOCATION (field), 2515 "%q#D should be initialized", field); 2516 } 2517 } 2518 2519 if (CLASS_TYPE_P (field_type)) 2520 error_count 2521 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin, 2522 using_new, complain); 2523 } 2524 return error_count; 2525 } 2526 2527 int 2528 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain) 2529 { 2530 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain); 2531 } 2532 2533 /* Call __cxa_bad_array_new_length to indicate that the size calculation 2534 overflowed. Pretend it returns sizetype so that it plays nicely in the 2535 COND_EXPR. */ 2536 2537 tree 2538 throw_bad_array_new_length (void) 2539 { 2540 if (!fn) 2541 { 2542 tree name = get_identifier ("__cxa_throw_bad_array_new_length"); 2543 2544 fn = get_global_binding (name); 2545 if (!fn) 2546 fn = push_throw_library_fn 2547 (name, build_function_type_list (sizetype, NULL_TREE)); 2548 } 2549 2550 return build_cxx_call (fn, 0, NULL, tf_warning_or_error); 2551 } 2552 2553 /* Attempt to find the initializer for flexible array field T in the 2554 initializer INIT, when non-null. Returns the initializer when 2555 successful and NULL otherwise. */ 2556 static tree 2557 find_flexarray_init (tree t, tree init) 2558 { 2559 if (!init || init == error_mark_node) 2560 return NULL_TREE; 2561 2562 unsigned HOST_WIDE_INT idx; 2563 tree field, elt; 2564 2565 /* Iterate over all top-level initializer elements. */ 2566 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt) 2567 /* If the member T is found, return it. */ 2568 if (field == t) 2569 return elt; 2570 2571 return NULL_TREE; 2572 } 2573 2574 /* Attempt to verify that the argument, OPER, of a placement new expression 2575 refers to an object sufficiently large for an object of TYPE or an array 2576 of NELTS of such objects when NELTS is non-null, and issue a warning when 2577 it does not. SIZE specifies the size needed to construct the object or 2578 array and captures the result of NELTS * sizeof (TYPE). (SIZE could be 2579 greater when the array under construction requires a cookie to store 2580 NELTS. GCC's placement new expression stores the cookie when invoking 2581 a user-defined placement new operator function but not the default one. 2582 Placement new expressions with user-defined placement new operator are 2583 not diagnosed since we don't know how they use the buffer (this could 2584 be a future extension). */ 2585 static void 2586 warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper) 2587 { 2588 location_t loc = cp_expr_loc_or_loc (oper, input_location); 2589 2590 /* The number of bytes to add to or subtract from the size of the provided 2591 buffer based on an offset into an array or an array element reference. 2592 Although intermediate results may be negative (as in a[3] - 2) a valid 2593 final result cannot be. */ 2594 offset_int adjust = 0; 2595 /* True when the size of the entire destination object should be used 2596 to compute the possibly optimistic estimate of the available space. */ 2597 bool use_obj_size = false; 2598 /* True when the reference to the destination buffer is an ADDR_EXPR. */ 2599 bool addr_expr = false; 2600 2601 STRIP_NOPS (oper); 2602 2603 /* Using a function argument or a (non-array) variable as an argument 2604 to placement new is not checked since it's unknown what it might 2605 point to. */ 2606 if (TREE_CODE (oper) == PARM_DECL 2607 || VAR_P (oper) 2608 || TREE_CODE (oper) == COMPONENT_REF) 2609 return; 2610 2611 /* Evaluate any constant expressions. */ 2612 size = fold_non_dependent_expr (size); 2613 2614 /* Handle the common case of array + offset expression when the offset 2615 is a constant. */ 2616 if (TREE_CODE (oper) == POINTER_PLUS_EXPR) 2617 { 2618 /* If the offset is compile-time constant, use it to compute a more 2619 accurate estimate of the size of the buffer. Since the operand 2620 of POINTER_PLUS_EXPR is represented as an unsigned type, convert 2621 it to signed first. 2622 Otherwise, use the size of the entire array as an optimistic 2623 estimate (this may lead to false negatives). */ 2624 tree adj = TREE_OPERAND (oper, 1); 2625 adj = fold_for_warn (adj); 2626 if (CONSTANT_CLASS_P (adj)) 2627 adjust += wi::to_offset (convert (ssizetype, adj)); 2628 else 2629 use_obj_size = true; 2630 2631 oper = TREE_OPERAND (oper, 0); 2632 2633 STRIP_NOPS (oper); 2634 } 2635 2636 if (TREE_CODE (oper) == TARGET_EXPR) 2637 oper = TREE_OPERAND (oper, 1); 2638 else if (TREE_CODE (oper) == ADDR_EXPR) 2639 { 2640 addr_expr = true; 2641 oper = TREE_OPERAND (oper, 0); 2642 } 2643 2644 STRIP_NOPS (oper); 2645 2646 if (TREE_CODE (oper) == ARRAY_REF 2647 && (addr_expr || TREE_CODE (TREE_TYPE (oper)) == ARRAY_TYPE)) 2648 { 2649 /* Similar to the offset computed above, see if the array index 2650 is a compile-time constant. If so, and unless the offset was 2651 not a compile-time constant, use the index to determine the 2652 size of the buffer. Otherwise, use the entire array as 2653 an optimistic estimate of the size. */ 2654 const_tree adj = fold_non_dependent_expr (TREE_OPERAND (oper, 1)); 2655 if (!use_obj_size && CONSTANT_CLASS_P (adj)) 2656 adjust += wi::to_offset (adj); 2657 else 2658 { 2659 use_obj_size = true; 2660 adjust = 0; 2661 } 2662 2663 oper = TREE_OPERAND (oper, 0); 2664 } 2665 2666 /* Refers to the declared object that constains the subobject referenced 2667 by OPER. When the object is initialized, makes it possible to determine 2668 the actual size of a flexible array member used as the buffer passed 2669 as OPER to placement new. */ 2670 tree var_decl = NULL_TREE; 2671 /* True when operand is a COMPONENT_REF, to distinguish flexible array 2672 members from arrays of unspecified size. */ 2673 bool compref = TREE_CODE (oper) == COMPONENT_REF; 2674 2675 /* For COMPONENT_REF (i.e., a struct member) the size of the entire 2676 enclosing struct. Used to validate the adjustment (offset) into 2677 an array at the end of a struct. */ 2678 offset_int compsize = 0; 2679 2680 /* Descend into a struct or union to find the member whose address 2681 is being used as the argument. */ 2682 if (TREE_CODE (oper) == COMPONENT_REF) 2683 { 2684 tree comptype = TREE_TYPE (TREE_OPERAND (oper, 0)); 2685 compsize = wi::to_offset (TYPE_SIZE_UNIT (comptype)); 2686 2687 tree op0 = oper; 2688 while (TREE_CODE (op0 = TREE_OPERAND (op0, 0)) == COMPONENT_REF); 2689 STRIP_ANY_LOCATION_WRAPPER (op0); 2690 if (VAR_P (op0)) 2691 var_decl = op0; 2692 oper = TREE_OPERAND (oper, 1); 2693 } 2694 2695 STRIP_ANY_LOCATION_WRAPPER (oper); 2696 tree opertype = TREE_TYPE (oper); 2697 if ((addr_expr || !INDIRECT_TYPE_P (opertype)) 2698 && (VAR_P (oper) 2699 || TREE_CODE (oper) == FIELD_DECL 2700 || TREE_CODE (oper) == PARM_DECL)) 2701 { 2702 /* A possibly optimistic estimate of the number of bytes available 2703 in the destination buffer. */ 2704 offset_int bytes_avail = 0; 2705 /* True when the estimate above is in fact the exact size 2706 of the destination buffer rather than an estimate. */ 2707 bool exact_size = true; 2708 2709 /* Treat members of unions and members of structs uniformly, even 2710 though the size of a member of a union may be viewed as extending 2711 to the end of the union itself (it is by __builtin_object_size). */ 2712 if ((VAR_P (oper) || use_obj_size) 2713 && DECL_SIZE_UNIT (oper) 2714 && tree_fits_uhwi_p (DECL_SIZE_UNIT (oper))) 2715 { 2716 /* Use the size of the entire array object when the expression 2717 refers to a variable or its size depends on an expression 2718 that's not a compile-time constant. */ 2719 bytes_avail = wi::to_offset (DECL_SIZE_UNIT (oper)); 2720 exact_size = !use_obj_size; 2721 } 2722 else if (tree opersize = TYPE_SIZE_UNIT (opertype)) 2723 { 2724 /* Use the size of the type of the destination buffer object 2725 as the optimistic estimate of the available space in it. 2726 Use the maximum possible size for zero-size arrays and 2727 flexible array members (except of initialized objects 2728 thereof). */ 2729 if (TREE_CODE (opersize) == INTEGER_CST) 2730 bytes_avail = wi::to_offset (opersize); 2731 } 2732 2733 if (bytes_avail == 0) 2734 { 2735 if (var_decl) 2736 { 2737 /* Constructing into a buffer provided by the flexible array 2738 member of a declared object (which is permitted as a G++ 2739 extension). If the array member has been initialized, 2740 determine its size from the initializer. Otherwise, 2741 the array size is zero. */ 2742 if (tree init = find_flexarray_init (oper, 2743 DECL_INITIAL (var_decl))) 2744 bytes_avail = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (init))); 2745 } 2746 else 2747 bytes_avail = (wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node)) 2748 - compsize); 2749 } 2750 2751 tree_code oper_code = TREE_CODE (opertype); 2752 2753 if (compref && oper_code == ARRAY_TYPE) 2754 { 2755 tree nelts = array_type_nelts_top (opertype); 2756 tree nelts_cst = maybe_constant_value (nelts); 2757 if (TREE_CODE (nelts_cst) == INTEGER_CST 2758 && integer_onep (nelts_cst) 2759 && !var_decl 2760 && warn_placement_new < 2) 2761 return; 2762 } 2763 2764 /* Reduce the size of the buffer by the adjustment computed above 2765 from the offset and/or the index into the array. */ 2766 if (bytes_avail < adjust || adjust < 0) 2767 bytes_avail = 0; 2768 else 2769 { 2770 tree elttype = (TREE_CODE (opertype) == ARRAY_TYPE 2771 ? TREE_TYPE (opertype) : opertype); 2772 if (tree eltsize = TYPE_SIZE_UNIT (elttype)) 2773 { 2774 bytes_avail -= adjust * wi::to_offset (eltsize); 2775 if (bytes_avail < 0) 2776 bytes_avail = 0; 2777 } 2778 } 2779 2780 /* The minimum amount of space needed for the allocation. This 2781 is an optimistic estimate that makes it possible to detect 2782 placement new invocation for some undersize buffers but not 2783 others. */ 2784 offset_int bytes_need; 2785 2786 if (nelts) 2787 nelts = fold_for_warn (nelts); 2788 2789 if (CONSTANT_CLASS_P (size)) 2790 bytes_need = wi::to_offset (size); 2791 else if (nelts && CONSTANT_CLASS_P (nelts)) 2792 bytes_need = (wi::to_offset (nelts) 2793 * wi::to_offset (TYPE_SIZE_UNIT (type))); 2794 else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))) 2795 bytes_need = wi::to_offset (TYPE_SIZE_UNIT (type)); 2796 else 2797 { 2798 /* The type is a VLA. */ 2799 return; 2800 } 2801 2802 if (bytes_avail < bytes_need) 2803 { 2804 if (nelts) 2805 if (CONSTANT_CLASS_P (nelts)) 2806 warning_at (loc, OPT_Wplacement_new_, 2807 exact_size ? 2808 "placement new constructing an object of type " 2809 "%<%T [%wu]%> and size %qwu in a region of type %qT " 2810 "and size %qwi" 2811 : "placement new constructing an object of type " 2812 "%<%T [%wu]%> and size %qwu in a region of type %qT " 2813 "and size at most %qwu", 2814 type, tree_to_uhwi (nelts), bytes_need.to_uhwi (), 2815 opertype, bytes_avail.to_uhwi ()); 2816 else 2817 warning_at (loc, OPT_Wplacement_new_, 2818 exact_size ? 2819 "placement new constructing an array of objects " 2820 "of type %qT and size %qwu in a region of type %qT " 2821 "and size %qwi" 2822 : "placement new constructing an array of objects " 2823 "of type %qT and size %qwu in a region of type %qT " 2824 "and size at most %qwu", 2825 type, bytes_need.to_uhwi (), opertype, 2826 bytes_avail.to_uhwi ()); 2827 else 2828 warning_at (loc, OPT_Wplacement_new_, 2829 exact_size ? 2830 "placement new constructing an object of type %qT " 2831 "and size %qwu in a region of type %qT and size %qwi" 2832 : "placement new constructing an object of type %qT " 2833 "and size %qwu in a region of type %qT and size " 2834 "at most %qwu", 2835 type, bytes_need.to_uhwi (), opertype, 2836 bytes_avail.to_uhwi ()); 2837 } 2838 } 2839 } 2840 2841 /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */ 2842 2843 bool 2844 type_has_new_extended_alignment (tree t) 2845 { 2846 return (aligned_new_threshold 2847 && TYPE_ALIGN_UNIT (t) > (unsigned)aligned_new_threshold); 2848 } 2849 2850 /* Return the alignment we expect malloc to guarantee. This should just be 2851 MALLOC_ABI_ALIGNMENT, but that macro defaults to only BITS_PER_WORD for some 2852 reason, so don't let the threshold be smaller than max_align_t_align. */ 2853 2854 unsigned 2855 malloc_alignment () 2856 { 2857 return MAX (max_align_t_align(), MALLOC_ABI_ALIGNMENT); 2858 } 2859 2860 /* Determine whether an allocation function is a namespace-scope 2861 non-replaceable placement new function. See DR 1748. 2862 TODO: Enable in all standard modes. */ 2863 static bool 2864 std_placement_new_fn_p (tree alloc_fn) 2865 { 2866 if (DECL_NAMESPACE_SCOPE_P (alloc_fn)) 2867 { 2868 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn))); 2869 if ((TREE_VALUE (first_arg) == ptr_type_node) 2870 && TREE_CHAIN (first_arg) == void_list_node) 2871 return true; 2872 } 2873 return false; 2874 } 2875 2876 /* Generate code for a new-expression, including calling the "operator 2877 new" function, initializing the object, and, if an exception occurs 2878 during construction, cleaning up. The arguments are as for 2879 build_raw_new_expr. This may change PLACEMENT and INIT. 2880 TYPE is the type of the object being constructed, possibly an array 2881 of NELTS elements when NELTS is non-null (in "new T[NELTS]", T may 2882 be an array of the form U[inner], with the whole expression being 2883 "new U[NELTS][inner]"). */ 2884 2885 static tree 2886 build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, 2887 vec<tree, va_gc> **init, bool globally_qualified_p, 2888 tsubst_flags_t complain) 2889 { 2890 tree size, rval; 2891 /* True iff this is a call to "operator new[]" instead of just 2892 "operator new". */ 2893 bool array_p = false; 2894 /* If ARRAY_P is true, the element type of the array. This is never 2895 an ARRAY_TYPE; for something like "new int[3][4]", the 2896 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as 2897 TYPE. */ 2898 tree elt_type; 2899 /* The type of the new-expression. (This type is always a pointer 2900 type.) */ 2901 tree pointer_type; 2902 tree non_const_pointer_type; 2903 /* The most significant array bound in int[OUTER_NELTS][inner]. */ 2904 tree outer_nelts = NULL_TREE; 2905 /* For arrays with a non-constant number of elements, a bounds checks 2906 on the NELTS parameter to avoid integer overflow at runtime. */ 2907 tree outer_nelts_check = NULL_TREE; 2908 bool outer_nelts_from_type = false; 2909 /* Number of the "inner" elements in "new T[OUTER_NELTS][inner]". */ 2910 offset_int inner_nelts_count = 1; 2911 tree alloc_call, alloc_expr; 2912 /* Size of the inner array elements (those with constant dimensions). */ 2913 offset_int inner_size; 2914 /* The address returned by the call to "operator new". This node is 2915 a VAR_DECL and is therefore reusable. */ 2916 tree alloc_node; 2917 tree alloc_fn; 2918 tree cookie_expr, init_expr; 2919 int nothrow, check_new; 2920 /* If non-NULL, the number of extra bytes to allocate at the 2921 beginning of the storage allocated for an array-new expression in 2922 order to store the number of elements. */ 2923 tree cookie_size = NULL_TREE; 2924 tree placement_first; 2925 tree placement_expr = NULL_TREE; 2926 /* True if the function we are calling is a placement allocation 2927 function. */ 2928 bool placement_allocation_fn_p; 2929 /* True if the storage must be initialized, either by a constructor 2930 or due to an explicit new-initializer. */ 2931 bool is_initialized; 2932 /* The address of the thing allocated, not including any cookie. In 2933 particular, if an array cookie is in use, DATA_ADDR is the 2934 address of the first array element. This node is a VAR_DECL, and 2935 is therefore reusable. */ 2936 tree data_addr; 2937 tree init_preeval_expr = NULL_TREE; 2938 tree orig_type = type; 2939 2940 if (nelts) 2941 { 2942 outer_nelts = nelts; 2943 array_p = true; 2944 } 2945 else if (TREE_CODE (type) == ARRAY_TYPE) 2946 { 2947 /* Transforms new (T[N]) to new T[N]. The former is a GNU 2948 extension for variable N. (This also covers new T where T is 2949 a VLA typedef.) */ 2950 array_p = true; 2951 nelts = array_type_nelts_top (type); 2952 outer_nelts = nelts; 2953 type = TREE_TYPE (type); 2954 outer_nelts_from_type = true; 2955 } 2956 2957 /* Lots of logic below depends on whether we have a constant number of 2958 elements, so go ahead and fold it now. */ 2959 const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain); 2960 2961 /* If our base type is an array, then make sure we know how many elements 2962 it has. */ 2963 for (elt_type = type; 2964 TREE_CODE (elt_type) == ARRAY_TYPE; 2965 elt_type = TREE_TYPE (elt_type)) 2966 { 2967 tree inner_nelts = array_type_nelts_top (elt_type); 2968 tree inner_nelts_cst = maybe_constant_value (inner_nelts); 2969 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST) 2970 { 2971 wi::overflow_type overflow; 2972 offset_int result = wi::mul (wi::to_offset (inner_nelts_cst), 2973 inner_nelts_count, SIGNED, &overflow); 2974 if (overflow) 2975 { 2976 if (complain & tf_error) 2977 error ("integer overflow in array size"); 2978 nelts = error_mark_node; 2979 } 2980 inner_nelts_count = result; 2981 } 2982 else 2983 { 2984 if (complain & tf_error) 2985 { 2986 error_at (cp_expr_loc_or_loc (inner_nelts, input_location), 2987 "array size in new-expression must be constant"); 2988 cxx_constant_value(inner_nelts); 2989 } 2990 nelts = error_mark_node; 2991 } 2992 if (nelts != error_mark_node) 2993 nelts = cp_build_binary_op (input_location, 2994 MULT_EXPR, nelts, 2995 inner_nelts_cst, 2996 complain); 2997 } 2998 2999 if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error)) 3000 { 3001 error ("variably modified type not allowed in new-expression"); 3002 return error_mark_node; 3003 } 3004 3005 if (nelts == error_mark_node) 3006 return error_mark_node; 3007 3008 /* Warn if we performed the (T[N]) to T[N] transformation and N is 3009 variable. */ 3010 if (outer_nelts_from_type 3011 && !TREE_CONSTANT (cst_outer_nelts)) 3012 { 3013 if (complain & tf_warning_or_error) 3014 { 3015 pedwarn (cp_expr_loc_or_loc (outer_nelts, input_location), OPT_Wvla, 3016 typedef_variant_p (orig_type) 3017 ? G_("non-constant array new length must be specified " 3018 "directly, not by typedef") 3019 : G_("non-constant array new length must be specified " 3020 "without parentheses around the type-id")); 3021 } 3022 else 3023 return error_mark_node; 3024 } 3025 3026 if (VOID_TYPE_P (elt_type)) 3027 { 3028 if (complain & tf_error) 3029 error ("invalid type %<void%> for new"); 3030 return error_mark_node; 3031 } 3032 3033 if (is_std_init_list (elt_type)) 3034 warning (OPT_Winit_list_lifetime, 3035 "%<new%> of initializer_list does not " 3036 "extend the lifetime of the underlying array"); 3037 3038 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain)) 3039 return error_mark_node; 3040 3041 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL); 3042 3043 if (*init == NULL && cxx_dialect < cxx11) 3044 { 3045 bool maybe_uninitialized_error = false; 3046 /* A program that calls for default-initialization [...] of an 3047 entity of reference type is ill-formed. */ 3048 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type)) 3049 maybe_uninitialized_error = true; 3050 3051 /* A new-expression that creates an object of type T initializes 3052 that object as follows: 3053 - If the new-initializer is omitted: 3054 -- If T is a (possibly cv-qualified) non-POD class type 3055 (or array thereof), the object is default-initialized (8.5). 3056 [...] 3057 -- Otherwise, the object created has indeterminate 3058 value. If T is a const-qualified type, or a (possibly 3059 cv-qualified) POD class type (or array thereof) 3060 containing (directly or indirectly) a member of 3061 const-qualified type, the program is ill-formed; */ 3062 3063 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type)) 3064 maybe_uninitialized_error = true; 3065 3066 if (maybe_uninitialized_error 3067 && diagnose_uninitialized_cst_or_ref_member (elt_type, 3068 /*using_new=*/true, 3069 complain & tf_error)) 3070 return error_mark_node; 3071 } 3072 3073 if (CP_TYPE_CONST_P (elt_type) && *init == NULL 3074 && default_init_uninitialized_part (elt_type)) 3075 { 3076 if (complain & tf_error) 3077 error ("uninitialized const in %<new%> of %q#T", elt_type); 3078 return error_mark_node; 3079 } 3080 3081 size = size_in_bytes (elt_type); 3082 if (array_p) 3083 { 3084 /* Maximum available size in bytes. Half of the address space 3085 minus the cookie size. */ 3086 offset_int max_size 3087 = wi::set_bit_in_zero <offset_int> (TYPE_PRECISION (sizetype) - 1); 3088 /* Maximum number of outer elements which can be allocated. */ 3089 offset_int max_outer_nelts; 3090 tree max_outer_nelts_tree; 3091 3092 gcc_assert (TREE_CODE (size) == INTEGER_CST); 3093 cookie_size = targetm.cxx.get_cookie_size (elt_type); 3094 gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST); 3095 gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size)); 3096 /* Unconditionally subtract the cookie size. This decreases the 3097 maximum object size and is safe even if we choose not to use 3098 a cookie after all. */ 3099 max_size -= wi::to_offset (cookie_size); 3100 wi::overflow_type overflow; 3101 inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED, 3102 &overflow); 3103 if (overflow || wi::gtu_p (inner_size, max_size)) 3104 { 3105 if (complain & tf_error) 3106 { 3107 cst_size_error error; 3108 if (overflow) 3109 error = cst_size_overflow; 3110 else 3111 { 3112 error = cst_size_too_big; 3113 size = size_binop (MULT_EXPR, size, 3114 wide_int_to_tree (sizetype, 3115 inner_nelts_count)); 3116 size = cp_fully_fold (size); 3117 } 3118 invalid_array_size_error (input_location, error, size, 3119 /*name=*/NULL_TREE); 3120 } 3121 return error_mark_node; 3122 } 3123 3124 max_outer_nelts = wi::udiv_trunc (max_size, inner_size); 3125 max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts); 3126 3127 size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts)); 3128 3129 if (TREE_CODE (cst_outer_nelts) == INTEGER_CST) 3130 { 3131 if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts)) 3132 { 3133 /* When the array size is constant, check it at compile time 3134 to make sure it doesn't exceed the implementation-defined 3135 maximum, as required by C++ 14 (in C++ 11 this requirement 3136 isn't explicitly stated but it's enforced anyway -- see 3137 grokdeclarator in cp/decl.c). */ 3138 if (complain & tf_error) 3139 { 3140 size = cp_fully_fold (size); 3141 invalid_array_size_error (input_location, cst_size_too_big, 3142 size, NULL_TREE); 3143 } 3144 return error_mark_node; 3145 } 3146 } 3147 else 3148 { 3149 /* When a runtime check is necessary because the array size 3150 isn't constant, keep only the top-most seven bits (starting 3151 with the most significant non-zero bit) of the maximum size 3152 to compare the array size against, to simplify encoding the 3153 constant maximum size in the instruction stream. */ 3154 3155 unsigned shift = (max_outer_nelts.get_precision ()) - 7 3156 - wi::clz (max_outer_nelts); 3157 max_outer_nelts = (max_outer_nelts >> shift) << shift; 3158 3159 outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node, 3160 outer_nelts, 3161 max_outer_nelts_tree); 3162 } 3163 } 3164 3165 tree align_arg = NULL_TREE; 3166 if (type_has_new_extended_alignment (elt_type)) 3167 align_arg = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (elt_type)); 3168 3169 alloc_fn = NULL_TREE; 3170 3171 /* If PLACEMENT is a single simple pointer type not passed by 3172 reference, prepare to capture it in a temporary variable. Do 3173 this now, since PLACEMENT will change in the calls below. */ 3174 placement_first = NULL_TREE; 3175 if (vec_safe_length (*placement) == 1 3176 && (TYPE_PTR_P (TREE_TYPE ((**placement)[0])))) 3177 placement_first = (**placement)[0]; 3178 3179 bool member_new_p = false; 3180 3181 /* Allocate the object. */ 3182 tree fnname; 3183 tree fns; 3184 3185 fnname = ovl_op_identifier (false, array_p ? VEC_NEW_EXPR : NEW_EXPR); 3186 3187 member_new_p = !globally_qualified_p 3188 && CLASS_TYPE_P (elt_type) 3189 && (array_p 3190 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type) 3191 : TYPE_HAS_NEW_OPERATOR (elt_type)); 3192 3193 if (member_new_p) 3194 { 3195 /* Use a class-specific operator new. */ 3196 /* If a cookie is required, add some extra space. */ 3197 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)) 3198 size = size_binop (PLUS_EXPR, size, cookie_size); 3199 else 3200 { 3201 cookie_size = NULL_TREE; 3202 /* No size arithmetic necessary, so the size check is 3203 not needed. */ 3204 if (outer_nelts_check != NULL && inner_size == 1) 3205 outer_nelts_check = NULL_TREE; 3206 } 3207 /* Perform the overflow check. */ 3208 tree errval = TYPE_MAX_VALUE (sizetype); 3209 if (cxx_dialect >= cxx11 && flag_exceptions) 3210 errval = throw_bad_array_new_length (); 3211 if (outer_nelts_check != NULL_TREE) 3212 size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check, 3213 size, errval); 3214 /* Create the argument list. */ 3215 vec_safe_insert (*placement, 0, size); 3216 /* Do name-lookup to find the appropriate operator. */ 3217 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2); 3218 if (fns == NULL_TREE) 3219 { 3220 if (complain & tf_error) 3221 error ("no suitable %qD found in class %qT", fnname, elt_type); 3222 return error_mark_node; 3223 } 3224 if (TREE_CODE (fns) == TREE_LIST) 3225 { 3226 if (complain & tf_error) 3227 { 3228 error ("request for member %qD is ambiguous", fnname); 3229 print_candidates (fns); 3230 } 3231 return error_mark_node; 3232 } 3233 tree dummy = build_dummy_object (elt_type); 3234 alloc_call = NULL_TREE; 3235 if (align_arg) 3236 { 3237 vec<tree, va_gc> *align_args 3238 = vec_copy_and_insert (*placement, align_arg, 1); 3239 alloc_call 3240 = build_new_method_call (dummy, fns, &align_args, 3241 /*conversion_path=*/NULL_TREE, 3242 LOOKUP_NORMAL, &alloc_fn, tf_none); 3243 /* If no matching function is found and the allocated object type 3244 has new-extended alignment, the alignment argument is removed 3245 from the argument list, and overload resolution is performed 3246 again. */ 3247 if (alloc_call == error_mark_node) 3248 alloc_call = NULL_TREE; 3249 } 3250 if (!alloc_call) 3251 alloc_call = build_new_method_call (dummy, fns, placement, 3252 /*conversion_path=*/NULL_TREE, 3253 LOOKUP_NORMAL, 3254 &alloc_fn, complain); 3255 } 3256 else 3257 { 3258 /* Use a global operator new. */ 3259 /* See if a cookie might be required. */ 3260 if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))) 3261 { 3262 cookie_size = NULL_TREE; 3263 /* No size arithmetic necessary, so the size check is 3264 not needed. */ 3265 if (outer_nelts_check != NULL && inner_size == 1) 3266 outer_nelts_check = NULL_TREE; 3267 } 3268 3269 alloc_call = build_operator_new_call (fnname, placement, 3270 &size, &cookie_size, 3271 align_arg, outer_nelts_check, 3272 &alloc_fn, complain); 3273 } 3274 3275 if (alloc_call == error_mark_node) 3276 return error_mark_node; 3277 3278 gcc_assert (alloc_fn != NULL_TREE); 3279 3280 /* Now, check to see if this function is actually a placement 3281 allocation function. This can happen even when PLACEMENT is NULL 3282 because we might have something like: 3283 3284 struct S { void* operator new (size_t, int i = 0); }; 3285 3286 A call to `new S' will get this allocation function, even though 3287 there is no explicit placement argument. If there is more than 3288 one argument, or there are variable arguments, then this is a 3289 placement allocation function. */ 3290 placement_allocation_fn_p 3291 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1 3292 || varargs_function_p (alloc_fn)); 3293 3294 if (warn_aligned_new 3295 && !placement_allocation_fn_p 3296 && TYPE_ALIGN (elt_type) > malloc_alignment () 3297 && (warn_aligned_new > 1 3298 || CP_DECL_CONTEXT (alloc_fn) == global_namespace) 3299 && !aligned_allocation_fn_p (alloc_fn)) 3300 { 3301 auto_diagnostic_group d; 3302 if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended " 3303 "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type))) 3304 { 3305 inform (input_location, "uses %qD, which does not have an alignment " 3306 "parameter", alloc_fn); 3307 if (!aligned_new_threshold) 3308 inform (input_location, "use %<-faligned-new%> to enable C++17 " 3309 "over-aligned new support"); 3310 } 3311 } 3312 3313 /* If we found a simple case of PLACEMENT_EXPR above, then copy it 3314 into a temporary variable. */ 3315 if (!processing_template_decl 3316 && TREE_CODE (alloc_call) == CALL_EXPR 3317 && call_expr_nargs (alloc_call) == 2 3318 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE 3319 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1)))) 3320 { 3321 tree placement = CALL_EXPR_ARG (alloc_call, 1); 3322 3323 if (placement_first != NULL_TREE 3324 && (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement))) 3325 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement))))) 3326 { 3327 placement_expr = get_target_expr (placement_first); 3328 CALL_EXPR_ARG (alloc_call, 1) 3329 = fold_convert (TREE_TYPE (placement), placement_expr); 3330 } 3331 3332 if (!member_new_p 3333 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))) 3334 { 3335 /* Attempt to make the warning point at the operator new argument. */ 3336 if (placement_first) 3337 placement = placement_first; 3338 3339 warn_placement_new_too_small (orig_type, nelts, size, placement); 3340 } 3341 } 3342 3343 /* In the simple case, we can stop now. */ 3344 pointer_type = build_pointer_type (type); 3345 if (!cookie_size && !is_initialized) 3346 return build_nop (pointer_type, alloc_call); 3347 3348 /* Store the result of the allocation call in a variable so that we can 3349 use it more than once. */ 3350 alloc_expr = get_target_expr (alloc_call); 3351 alloc_node = TARGET_EXPR_SLOT (alloc_expr); 3352 3353 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */ 3354 while (TREE_CODE (alloc_call) == COMPOUND_EXPR) 3355 alloc_call = TREE_OPERAND (alloc_call, 1); 3356 3357 /* Preevaluate the placement args so that we don't reevaluate them for a 3358 placement delete. */ 3359 if (placement_allocation_fn_p) 3360 { 3361 tree inits; 3362 stabilize_call (alloc_call, &inits); 3363 if (inits) 3364 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits, 3365 alloc_expr); 3366 } 3367 3368 /* unless an allocation function is declared with an empty excep- 3369 tion-specification (_except.spec_), throw(), it indicates failure to 3370 allocate storage by throwing a bad_alloc exception (clause _except_, 3371 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo- 3372 cation function is declared with an empty exception-specification, 3373 throw(), it returns null to indicate failure to allocate storage and a 3374 non-null pointer otherwise. 3375 3376 So check for a null exception spec on the op new we just called. */ 3377 3378 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn)); 3379 check_new 3380 = flag_check_new || (nothrow && !std_placement_new_fn_p (alloc_fn)); 3381 3382 if (cookie_size) 3383 { 3384 tree cookie; 3385 tree cookie_ptr; 3386 tree size_ptr_type; 3387 3388 /* Adjust so we're pointing to the start of the object. */ 3389 data_addr = fold_build_pointer_plus (alloc_node, cookie_size); 3390 3391 /* Store the number of bytes allocated so that we can know how 3392 many elements to destroy later. We use the last sizeof 3393 (size_t) bytes to store the number of elements. */ 3394 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype)); 3395 cookie_ptr = fold_build_pointer_plus_loc (input_location, 3396 alloc_node, cookie_ptr); 3397 size_ptr_type = build_pointer_type (sizetype); 3398 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr); 3399 cookie = cp_build_fold_indirect_ref (cookie_ptr); 3400 3401 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts); 3402 3403 if (targetm.cxx.cookie_has_size ()) 3404 { 3405 /* Also store the element size. */ 3406 cookie_ptr = fold_build_pointer_plus (cookie_ptr, 3407 fold_build1_loc (input_location, 3408 NEGATE_EXPR, sizetype, 3409 size_in_bytes (sizetype))); 3410 3411 cookie = cp_build_fold_indirect_ref (cookie_ptr); 3412 cookie = build2 (MODIFY_EXPR, sizetype, cookie, 3413 size_in_bytes (elt_type)); 3414 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr), 3415 cookie, cookie_expr); 3416 } 3417 } 3418 else 3419 { 3420 cookie_expr = NULL_TREE; 3421 data_addr = alloc_node; 3422 } 3423 3424 /* Now use a pointer to the type we've actually allocated. */ 3425 3426 /* But we want to operate on a non-const version to start with, 3427 since we'll be modifying the elements. */ 3428 non_const_pointer_type = build_pointer_type 3429 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST)); 3430 3431 data_addr = fold_convert (non_const_pointer_type, data_addr); 3432 /* Any further uses of alloc_node will want this type, too. */ 3433 alloc_node = fold_convert (non_const_pointer_type, alloc_node); 3434 3435 /* Now initialize the allocated object. Note that we preevaluate the 3436 initialization expression, apart from the actual constructor call or 3437 assignment--we do this because we want to delay the allocation as long 3438 as possible in order to minimize the size of the exception region for 3439 placement delete. */ 3440 if (is_initialized) 3441 { 3442 bool stable; 3443 bool explicit_value_init_p = false; 3444 3445 if (*init != NULL && (*init)->is_empty ()) 3446 { 3447 *init = NULL; 3448 explicit_value_init_p = true; 3449 } 3450 3451 if (processing_template_decl) 3452 { 3453 /* Avoid an ICE when converting to a base in build_simple_base_path. 3454 We'll throw this all away anyway, and build_new will create 3455 a NEW_EXPR. */ 3456 tree t = fold_convert (build_pointer_type (elt_type), data_addr); 3457 /* build_value_init doesn't work in templates, and we don't need 3458 the initializer anyway since we're going to throw it away and 3459 rebuild it at instantiation time, so just build up a single 3460 constructor call to get any appropriate diagnostics. */ 3461 init_expr = cp_build_fold_indirect_ref (t); 3462 if (type_build_ctor_call (elt_type)) 3463 init_expr = build_special_member_call (init_expr, 3464 complete_ctor_identifier, 3465 init, elt_type, 3466 LOOKUP_NORMAL, 3467 complain); 3468 stable = stabilize_init (init_expr, &init_preeval_expr); 3469 } 3470 else if (array_p) 3471 { 3472 tree vecinit = NULL_TREE; 3473 if (vec_safe_length (*init) == 1 3474 && DIRECT_LIST_INIT_P ((**init)[0])) 3475 { 3476 vecinit = (**init)[0]; 3477 if (CONSTRUCTOR_NELTS (vecinit) == 0) 3478 /* List-value-initialization, leave it alone. */; 3479 else 3480 { 3481 tree arraytype, domain; 3482 if (TREE_CONSTANT (nelts)) 3483 domain = compute_array_index_type (NULL_TREE, nelts, 3484 complain); 3485 else 3486 /* We'll check the length at runtime. */ 3487 domain = NULL_TREE; 3488 arraytype = build_cplus_array_type (type, domain); 3489 vecinit = digest_init (arraytype, vecinit, complain); 3490 } 3491 } 3492 else if (*init) 3493 { 3494 if (complain & tf_error) 3495 error ("parenthesized initializer in array new"); 3496 return error_mark_node; 3497 } 3498 init_expr 3499 = build_vec_init (data_addr, 3500 cp_build_binary_op (input_location, 3501 MINUS_EXPR, outer_nelts, 3502 integer_one_node, 3503 complain), 3504 vecinit, 3505 explicit_value_init_p, 3506 /*from_array=*/0, 3507 complain); 3508 3509 /* An array initialization is stable because the initialization 3510 of each element is a full-expression, so the temporaries don't 3511 leak out. */ 3512 stable = true; 3513 } 3514 else 3515 { 3516 init_expr = cp_build_fold_indirect_ref (data_addr); 3517 3518 if (type_build_ctor_call (type) && !explicit_value_init_p) 3519 { 3520 init_expr = build_special_member_call (init_expr, 3521 complete_ctor_identifier, 3522 init, elt_type, 3523 LOOKUP_NORMAL, 3524 complain|tf_no_cleanup); 3525 } 3526 else if (explicit_value_init_p) 3527 { 3528 /* Something like `new int()'. NO_CLEANUP is needed so 3529 we don't try and build a (possibly ill-formed) 3530 destructor. */ 3531 tree val = build_value_init (type, complain | tf_no_cleanup); 3532 if (val == error_mark_node) 3533 return error_mark_node; 3534 init_expr = build2 (INIT_EXPR, type, init_expr, val); 3535 } 3536 else 3537 { 3538 tree ie; 3539 3540 /* We are processing something like `new int (10)', which 3541 means allocate an int, and initialize it with 10. */ 3542 3543 ie = build_x_compound_expr_from_vec (*init, "new initializer", 3544 complain); 3545 init_expr = cp_build_modify_expr (input_location, init_expr, 3546 INIT_EXPR, ie, complain); 3547 } 3548 /* If the initializer uses C++14 aggregate NSDMI that refer to the 3549 object being initialized, replace them now and don't try to 3550 preevaluate. */ 3551 bool had_placeholder = false; 3552 if (!processing_template_decl 3553 && TREE_CODE (init_expr) == INIT_EXPR) 3554 TREE_OPERAND (init_expr, 1) 3555 = replace_placeholders (TREE_OPERAND (init_expr, 1), 3556 TREE_OPERAND (init_expr, 0), 3557 &had_placeholder); 3558 stable = (!had_placeholder 3559 && stabilize_init (init_expr, &init_preeval_expr)); 3560 } 3561 3562 if (init_expr == error_mark_node) 3563 return error_mark_node; 3564 3565 /* If any part of the object initialization terminates by throwing an 3566 exception and a suitable deallocation function can be found, the 3567 deallocation function is called to free the memory in which the 3568 object was being constructed, after which the exception continues 3569 to propagate in the context of the new-expression. If no 3570 unambiguous matching deallocation function can be found, 3571 propagating the exception does not cause the object's memory to be 3572 freed. */ 3573 if (flag_exceptions) 3574 { 3575 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR; 3576 tree cleanup; 3577 3578 /* The Standard is unclear here, but the right thing to do 3579 is to use the same method for finding deallocation 3580 functions that we use for finding allocation functions. */ 3581 cleanup = (build_op_delete_call 3582 (dcode, 3583 alloc_node, 3584 size, 3585 globally_qualified_p, 3586 placement_allocation_fn_p ? alloc_call : NULL_TREE, 3587 alloc_fn, 3588 complain)); 3589 3590 if (!cleanup) 3591 /* We're done. */; 3592 else if (stable) 3593 /* This is much simpler if we were able to preevaluate all of 3594 the arguments to the constructor call. */ 3595 { 3596 /* CLEANUP is compiler-generated, so no diagnostics. */ 3597 TREE_NO_WARNING (cleanup) = true; 3598 init_expr = build2 (TRY_CATCH_EXPR, void_type_node, 3599 init_expr, cleanup); 3600 /* Likewise, this try-catch is compiler-generated. */ 3601 TREE_NO_WARNING (init_expr) = true; 3602 } 3603 else 3604 /* Ack! First we allocate the memory. Then we set our sentry 3605 variable to true, and expand a cleanup that deletes the 3606 memory if sentry is true. Then we run the constructor, and 3607 finally clear the sentry. 3608 3609 We need to do this because we allocate the space first, so 3610 if there are any temporaries with cleanups in the 3611 constructor args and we weren't able to preevaluate them, we 3612 need this EH region to extend until end of full-expression 3613 to preserve nesting. */ 3614 { 3615 tree end, sentry, begin; 3616 3617 begin = get_target_expr (boolean_true_node); 3618 CLEANUP_EH_ONLY (begin) = 1; 3619 3620 sentry = TARGET_EXPR_SLOT (begin); 3621 3622 /* CLEANUP is compiler-generated, so no diagnostics. */ 3623 TREE_NO_WARNING (cleanup) = true; 3624 3625 TARGET_EXPR_CLEANUP (begin) 3626 = build3 (COND_EXPR, void_type_node, sentry, 3627 cleanup, void_node); 3628 3629 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry), 3630 sentry, boolean_false_node); 3631 3632 init_expr 3633 = build2 (COMPOUND_EXPR, void_type_node, begin, 3634 build2 (COMPOUND_EXPR, void_type_node, init_expr, 3635 end)); 3636 /* Likewise, this is compiler-generated. */ 3637 TREE_NO_WARNING (init_expr) = true; 3638 } 3639 } 3640 } 3641 else 3642 init_expr = NULL_TREE; 3643 3644 /* Now build up the return value in reverse order. */ 3645 3646 rval = data_addr; 3647 3648 if (init_expr) 3649 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval); 3650 if (cookie_expr) 3651 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval); 3652 3653 if (rval == data_addr) 3654 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR 3655 and return the call (which doesn't need to be adjusted). */ 3656 rval = TARGET_EXPR_INITIAL (alloc_expr); 3657 else 3658 { 3659 if (check_new) 3660 { 3661 tree ifexp = cp_build_binary_op (input_location, 3662 NE_EXPR, alloc_node, 3663 nullptr_node, 3664 complain); 3665 rval = build_conditional_expr (input_location, ifexp, rval, 3666 alloc_node, complain); 3667 } 3668 3669 /* Perform the allocation before anything else, so that ALLOC_NODE 3670 has been initialized before we start using it. */ 3671 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval); 3672 } 3673 3674 if (init_preeval_expr) 3675 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval); 3676 3677 /* A new-expression is never an lvalue. */ 3678 gcc_assert (!obvalue_p (rval)); 3679 3680 return convert (pointer_type, rval); 3681 } 3682 3683 /* Generate a representation for a C++ "new" expression. *PLACEMENT 3684 is a vector of placement-new arguments (or NULL if none). If NELTS 3685 is NULL, TYPE is the type of the storage to be allocated. If NELTS 3686 is not NULL, then this is an array-new allocation; TYPE is the type 3687 of the elements in the array and NELTS is the number of elements in 3688 the array. *INIT, if non-NULL, is the initializer for the new 3689 object, or an empty vector to indicate an initializer of "()". If 3690 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new" 3691 rather than just "new". This may change PLACEMENT and INIT. */ 3692 3693 tree 3694 build_new (vec<tree, va_gc> **placement, tree type, tree nelts, 3695 vec<tree, va_gc> **init, int use_global_new, tsubst_flags_t complain) 3696 { 3697 tree rval; 3698 vec<tree, va_gc> *orig_placement = NULL; 3699 tree orig_nelts = NULL_TREE; 3700 vec<tree, va_gc> *orig_init = NULL; 3701 3702 if (type == error_mark_node) 3703 return error_mark_node; 3704 3705 if (nelts == NULL_TREE 3706 /* Don't do auto deduction where it might affect mangling. */ 3707 && (!processing_template_decl || at_function_scope_p ())) 3708 { 3709 tree auto_node = type_uses_auto (type); 3710 if (auto_node) 3711 { 3712 tree d_init = NULL_TREE; 3713 const size_t len = vec_safe_length (*init); 3714 /* E.g. new auto(x) must have exactly one element, or 3715 a {} initializer will have one element. */ 3716 if (len == 1) 3717 { 3718 d_init = (**init)[0]; 3719 d_init = resolve_nondeduced_context (d_init, complain); 3720 } 3721 /* For the rest, e.g. new A(1, 2, 3), create a list. */ 3722 else if (len > 1) 3723 { 3724 unsigned int n; 3725 tree t; 3726 tree *pp = &d_init; 3727 FOR_EACH_VEC_ELT (**init, n, t) 3728 { 3729 t = resolve_nondeduced_context (t, complain); 3730 *pp = build_tree_list (NULL_TREE, t); 3731 pp = &TREE_CHAIN (*pp); 3732 } 3733 } 3734 type = do_auto_deduction (type, d_init, auto_node, complain); 3735 } 3736 } 3737 3738 if (processing_template_decl) 3739 { 3740 if (dependent_type_p (type) 3741 || any_type_dependent_arguments_p (*placement) 3742 || (nelts && type_dependent_expression_p (nelts)) 3743 || (nelts && *init) 3744 || any_type_dependent_arguments_p (*init)) 3745 return build_raw_new_expr (*placement, type, nelts, *init, 3746 use_global_new); 3747 3748 orig_placement = make_tree_vector_copy (*placement); 3749 orig_nelts = nelts; 3750 if (*init) 3751 { 3752 orig_init = make_tree_vector_copy (*init); 3753 /* Also copy any CONSTRUCTORs in *init, since reshape_init and 3754 digest_init clobber them in place. */ 3755 for (unsigned i = 0; i < orig_init->length(); ++i) 3756 { 3757 tree e = (**init)[i]; 3758 if (TREE_CODE (e) == CONSTRUCTOR) 3759 (**init)[i] = copy_node (e); 3760 } 3761 } 3762 3763 make_args_non_dependent (*placement); 3764 if (nelts) 3765 nelts = build_non_dependent_expr (nelts); 3766 make_args_non_dependent (*init); 3767 } 3768 3769 if (nelts) 3770 { 3771 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false)) 3772 { 3773 if (complain & tf_error) 3774 permerror (input_location, "size in array new must have integral type"); 3775 else 3776 return error_mark_node; 3777 } 3778 3779 /* Try to determine the constant value only for the purposes 3780 of the diagnostic below but continue to use the original 3781 value and handle const folding later. */ 3782 const_tree cst_nelts = fold_non_dependent_expr (nelts, complain); 3783 3784 /* The expression in a noptr-new-declarator is erroneous if it's of 3785 non-class type and its value before converting to std::size_t is 3786 less than zero. ... If the expression is a constant expression, 3787 the program is ill-fomed. */ 3788 if (TREE_CODE (cst_nelts) == INTEGER_CST 3789 && !valid_array_size_p (input_location, cst_nelts, NULL_TREE, 3790 complain & tf_error)) 3791 return error_mark_node; 3792 3793 nelts = mark_rvalue_use (nelts); 3794 nelts = cp_save_expr (cp_convert (sizetype, nelts, complain)); 3795 } 3796 3797 /* ``A reference cannot be created by the new operator. A reference 3798 is not an object (8.2.2, 8.4.3), so a pointer to it could not be 3799 returned by new.'' ARM 5.3.3 */ 3800 if (TYPE_REF_P (type)) 3801 { 3802 if (complain & tf_error) 3803 error ("new cannot be applied to a reference type"); 3804 else 3805 return error_mark_node; 3806 type = TREE_TYPE (type); 3807 } 3808 3809 if (TREE_CODE (type) == FUNCTION_TYPE) 3810 { 3811 if (complain & tf_error) 3812 error ("new cannot be applied to a function type"); 3813 return error_mark_node; 3814 } 3815 3816 /* The type allocated must be complete. If the new-type-id was 3817 "T[N]" then we are just checking that "T" is complete here, but 3818 that is equivalent, since the value of "N" doesn't matter. */ 3819 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) 3820 return error_mark_node; 3821 3822 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain); 3823 if (rval == error_mark_node) 3824 return error_mark_node; 3825 3826 if (processing_template_decl) 3827 { 3828 tree ret = build_raw_new_expr (orig_placement, type, orig_nelts, 3829 orig_init, use_global_new); 3830 release_tree_vector (orig_placement); 3831 release_tree_vector (orig_init); 3832 return ret; 3833 } 3834 3835 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */ 3836 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval); 3837 TREE_NO_WARNING (rval) = 1; 3838 3839 return rval; 3840 } 3841 3842 static tree 3843 build_vec_delete_1 (tree base, tree maxindex, tree type, 3844 special_function_kind auto_delete_vec, 3845 int use_global_delete, tsubst_flags_t complain) 3846 { 3847 tree virtual_size; 3848 tree ptype = build_pointer_type (type = complete_type (type)); 3849 tree size_exp; 3850 3851 /* Temporary variables used by the loop. */ 3852 tree tbase, tbase_init; 3853 3854 /* This is the body of the loop that implements the deletion of a 3855 single element, and moves temp variables to next elements. */ 3856 tree body; 3857 3858 /* This is the LOOP_EXPR that governs the deletion of the elements. */ 3859 tree loop = 0; 3860 3861 /* This is the thing that governs what to do after the loop has run. */ 3862 tree deallocate_expr = 0; 3863 3864 /* This is the BIND_EXPR which holds the outermost iterator of the 3865 loop. It is convenient to set this variable up and test it before 3866 executing any other code in the loop. 3867 This is also the containing expression returned by this function. */ 3868 tree controller = NULL_TREE; 3869 tree tmp; 3870 3871 /* We should only have 1-D arrays here. */ 3872 gcc_assert (TREE_CODE (type) != ARRAY_TYPE); 3873 3874 if (base == error_mark_node || maxindex == error_mark_node) 3875 return error_mark_node; 3876 3877 if (!COMPLETE_TYPE_P (type)) 3878 { 3879 if (complain & tf_warning) 3880 { 3881 auto_diagnostic_group d; 3882 if (warning (OPT_Wdelete_incomplete, 3883 "possible problem detected in invocation of " 3884 "delete [] operator:")) 3885 { 3886 cxx_incomplete_type_diagnostic (base, type, DK_WARNING); 3887 inform (input_location, "neither the destructor nor the " 3888 "class-specific operator delete [] will be called, " 3889 "even if they are declared when the class is defined"); 3890 } 3891 } 3892 /* This size won't actually be used. */ 3893 size_exp = size_one_node; 3894 goto no_destructor; 3895 } 3896 3897 size_exp = size_in_bytes (type); 3898 3899 if (! MAYBE_CLASS_TYPE_P (type)) 3900 goto no_destructor; 3901 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) 3902 { 3903 /* Make sure the destructor is callable. */ 3904 if (type_build_dtor_call (type)) 3905 { 3906 tmp = build_delete (ptype, base, sfk_complete_destructor, 3907 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1, 3908 complain); 3909 if (tmp == error_mark_node) 3910 return error_mark_node; 3911 } 3912 goto no_destructor; 3913 } 3914 3915 /* The below is short by the cookie size. */ 3916 virtual_size = size_binop (MULT_EXPR, size_exp, 3917 fold_convert (sizetype, maxindex)); 3918 3919 tbase = create_temporary_var (ptype); 3920 tbase_init 3921 = cp_build_modify_expr (input_location, tbase, NOP_EXPR, 3922 fold_build_pointer_plus_loc (input_location, 3923 fold_convert (ptype, 3924 base), 3925 virtual_size), 3926 complain); 3927 if (tbase_init == error_mark_node) 3928 return error_mark_node; 3929 controller = build3 (BIND_EXPR, void_type_node, tbase, 3930 NULL_TREE, NULL_TREE); 3931 TREE_SIDE_EFFECTS (controller) = 1; 3932 3933 body = build1 (EXIT_EXPR, void_type_node, 3934 build2 (EQ_EXPR, boolean_type_node, tbase, 3935 fold_convert (ptype, base))); 3936 tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp); 3937 tmp = fold_build_pointer_plus (tbase, tmp); 3938 tmp = cp_build_modify_expr (input_location, tbase, NOP_EXPR, tmp, complain); 3939 if (tmp == error_mark_node) 3940 return error_mark_node; 3941 body = build_compound_expr (input_location, body, tmp); 3942 tmp = build_delete (ptype, tbase, sfk_complete_destructor, 3943 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1, 3944 complain); 3945 if (tmp == error_mark_node) 3946 return error_mark_node; 3947 body = build_compound_expr (input_location, body, tmp); 3948 3949 loop = build1 (LOOP_EXPR, void_type_node, body); 3950 loop = build_compound_expr (input_location, tbase_init, loop); 3951 3952 no_destructor: 3953 /* Delete the storage if appropriate. */ 3954 if (auto_delete_vec == sfk_deleting_destructor) 3955 { 3956 tree base_tbd; 3957 3958 /* The below is short by the cookie size. */ 3959 virtual_size = size_binop (MULT_EXPR, size_exp, 3960 fold_convert (sizetype, maxindex)); 3961 3962 if (! TYPE_VEC_NEW_USES_COOKIE (type)) 3963 /* no header */ 3964 base_tbd = base; 3965 else 3966 { 3967 tree cookie_size; 3968 3969 cookie_size = targetm.cxx.get_cookie_size (type); 3970 base_tbd = cp_build_binary_op (input_location, 3971 MINUS_EXPR, 3972 cp_convert (string_type_node, 3973 base, complain), 3974 cookie_size, 3975 complain); 3976 if (base_tbd == error_mark_node) 3977 return error_mark_node; 3978 base_tbd = cp_convert (ptype, base_tbd, complain); 3979 /* True size with header. */ 3980 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size); 3981 } 3982 3983 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR, 3984 base_tbd, virtual_size, 3985 use_global_delete & 1, 3986 /*placement=*/NULL_TREE, 3987 /*alloc_fn=*/NULL_TREE, 3988 complain); 3989 } 3990 3991 body = loop; 3992 if (!deallocate_expr) 3993 ; 3994 else if (!body) 3995 body = deallocate_expr; 3996 else 3997 /* The delete operator mist be called, even if a destructor 3998 throws. */ 3999 body = build2 (TRY_FINALLY_EXPR, void_type_node, body, deallocate_expr); 4000 4001 if (!body) 4002 body = integer_zero_node; 4003 4004 /* Outermost wrapper: If pointer is null, punt. */ 4005 tree cond = build2_loc (input_location, NE_EXPR, boolean_type_node, base, 4006 fold_convert (TREE_TYPE (base), nullptr_node)); 4007 /* This is a compiler generated comparison, don't emit 4008 e.g. -Wnonnull-compare warning for it. */ 4009 TREE_NO_WARNING (cond) = 1; 4010 body = build3_loc (input_location, COND_EXPR, void_type_node, 4011 cond, body, integer_zero_node); 4012 COND_EXPR_IS_VEC_DELETE (body) = true; 4013 body = build1 (NOP_EXPR, void_type_node, body); 4014 4015 if (controller) 4016 { 4017 TREE_OPERAND (controller, 1) = body; 4018 body = controller; 4019 } 4020 4021 if (TREE_CODE (base) == SAVE_EXPR) 4022 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */ 4023 body = build2 (COMPOUND_EXPR, void_type_node, base, body); 4024 4025 return convert_to_void (body, ICV_CAST, complain); 4026 } 4027 4028 /* Create an unnamed variable of the indicated TYPE. */ 4029 4030 tree 4031 create_temporary_var (tree type) 4032 { 4033 tree decl; 4034 4035 decl = build_decl (input_location, 4036 VAR_DECL, NULL_TREE, type); 4037 TREE_USED (decl) = 1; 4038 DECL_ARTIFICIAL (decl) = 1; 4039 DECL_IGNORED_P (decl) = 1; 4040 DECL_CONTEXT (decl) = current_function_decl; 4041 4042 return decl; 4043 } 4044 4045 /* Create a new temporary variable of the indicated TYPE, initialized 4046 to INIT. 4047 4048 It is not entered into current_binding_level, because that breaks 4049 things when it comes time to do final cleanups (which take place 4050 "outside" the binding contour of the function). */ 4051 4052 tree 4053 get_temp_regvar (tree type, tree init) 4054 { 4055 tree decl; 4056 4057 decl = create_temporary_var (type); 4058 add_decl_expr (decl); 4059 4060 finish_expr_stmt (cp_build_modify_expr (input_location, decl, INIT_EXPR, 4061 init, tf_warning_or_error)); 4062 4063 return decl; 4064 } 4065 4066 /* Subroutine of build_vec_init. Returns true if assigning to an array of 4067 INNER_ELT_TYPE from INIT is trivial. */ 4068 4069 static bool 4070 vec_copy_assign_is_trivial (tree inner_elt_type, tree init) 4071 { 4072 tree fromtype = inner_elt_type; 4073 if (lvalue_p (init)) 4074 fromtype = cp_build_reference_type (fromtype, /*rval*/false); 4075 return is_trivially_xible (MODIFY_EXPR, inner_elt_type, fromtype); 4076 } 4077 4078 /* Subroutine of build_vec_init: Check that the array has at least N 4079 elements. Other parameters are local variables in build_vec_init. */ 4080 4081 void 4082 finish_length_check (tree atype, tree iterator, tree obase, unsigned n) 4083 { 4084 tree nelts = build_int_cst (ptrdiff_type_node, n - 1); 4085 if (TREE_CODE (atype) != ARRAY_TYPE) 4086 { 4087 if (flag_exceptions) 4088 { 4089 tree c = fold_build2 (LT_EXPR, boolean_type_node, iterator, 4090 nelts); 4091 c = build3 (COND_EXPR, void_type_node, c, 4092 throw_bad_array_new_length (), void_node); 4093 finish_expr_stmt (c); 4094 } 4095 /* Don't check an array new when -fno-exceptions. */ 4096 } 4097 else if (sanitize_flags_p (SANITIZE_BOUNDS) 4098 && current_function_decl != NULL_TREE) 4099 { 4100 /* Make sure the last element of the initializer is in bounds. */ 4101 finish_expr_stmt 4102 (ubsan_instrument_bounds 4103 (input_location, obase, &nelts, /*ignore_off_by_one*/false)); 4104 } 4105 } 4106 4107 /* `build_vec_init' returns tree structure that performs 4108 initialization of a vector of aggregate types. 4109 4110 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer 4111 to the first element, of POINTER_TYPE. 4112 MAXINDEX is the maximum index of the array (one less than the 4113 number of elements). It is only used if BASE is a pointer or 4114 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE. 4115 4116 INIT is the (possibly NULL) initializer. 4117 4118 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All 4119 elements in the array are value-initialized. 4120 4121 FROM_ARRAY is 0 if we should init everything with INIT 4122 (i.e., every element initialized from INIT). 4123 FROM_ARRAY is 1 if we should index into INIT in parallel 4124 with initialization of DECL. 4125 FROM_ARRAY is 2 if we should index into INIT in parallel, 4126 but use assignment instead of initialization. */ 4127 4128 tree 4129 build_vec_init (tree base, tree maxindex, tree init, 4130 bool explicit_value_init_p, 4131 int from_array, tsubst_flags_t complain) 4132 { 4133 tree rval; 4134 tree base2 = NULL_TREE; 4135 tree itype = NULL_TREE; 4136 tree iterator; 4137 /* The type of BASE. */ 4138 tree atype = TREE_TYPE (base); 4139 /* The type of an element in the array. */ 4140 tree type = TREE_TYPE (atype); 4141 /* The element type reached after removing all outer array 4142 types. */ 4143 tree inner_elt_type; 4144 /* The type of a pointer to an element in the array. */ 4145 tree ptype; 4146 tree stmt_expr; 4147 tree compound_stmt; 4148 int destroy_temps; 4149 tree try_block = NULL_TREE; 4150 HOST_WIDE_INT num_initialized_elts = 0; 4151 bool is_global; 4152 tree obase = base; 4153 bool xvalue = false; 4154 bool errors = false; 4155 location_t loc = (init ? cp_expr_loc_or_loc (init, input_location) 4156 : location_of (base)); 4157 4158 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype)) 4159 maxindex = array_type_nelts (atype); 4160 4161 if (maxindex == NULL_TREE || maxindex == error_mark_node) 4162 return error_mark_node; 4163 4164 maxindex = maybe_constant_value (maxindex); 4165 if (explicit_value_init_p) 4166 gcc_assert (!init); 4167 4168 inner_elt_type = strip_array_types (type); 4169 4170 /* Look through the TARGET_EXPR around a compound literal. */ 4171 if (init && TREE_CODE (init) == TARGET_EXPR 4172 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR 4173 && from_array != 2) 4174 init = TARGET_EXPR_INITIAL (init); 4175 4176 bool direct_init = false; 4177 if (from_array && init && BRACE_ENCLOSED_INITIALIZER_P (init) 4178 && CONSTRUCTOR_NELTS (init) == 1) 4179 { 4180 tree elt = CONSTRUCTOR_ELT (init, 0)->value; 4181 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE) 4182 { 4183 direct_init = DIRECT_LIST_INIT_P (init); 4184 init = elt; 4185 } 4186 } 4187 4188 /* If we have a braced-init-list or string constant, make sure that the array 4189 is big enough for all the initializers. */ 4190 bool length_check = (init 4191 && (TREE_CODE (init) == STRING_CST 4192 || (TREE_CODE (init) == CONSTRUCTOR 4193 && CONSTRUCTOR_NELTS (init) > 0)) 4194 && !TREE_CONSTANT (maxindex)); 4195 4196 if (init 4197 && TREE_CODE (atype) == ARRAY_TYPE 4198 && TREE_CONSTANT (maxindex) 4199 && (from_array == 2 4200 ? vec_copy_assign_is_trivial (inner_elt_type, init) 4201 : !TYPE_NEEDS_CONSTRUCTING (type)) 4202 && ((TREE_CODE (init) == CONSTRUCTOR 4203 && (BRACE_ENCLOSED_INITIALIZER_P (init) 4204 || (same_type_ignoring_top_level_qualifiers_p 4205 (atype, TREE_TYPE (init)))) 4206 /* Don't do this if the CONSTRUCTOR might contain something 4207 that might throw and require us to clean up. */ 4208 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)) 4209 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type))) 4210 || from_array)) 4211 { 4212 /* Do non-default initialization of trivial arrays resulting from 4213 brace-enclosed initializers. In this case, digest_init and 4214 store_constructor will handle the semantics for us. */ 4215 4216 if (BRACE_ENCLOSED_INITIALIZER_P (init)) 4217 init = digest_init (atype, init, complain); 4218 stmt_expr = build2 (INIT_EXPR, atype, base, init); 4219 return stmt_expr; 4220 } 4221 4222 maxindex = cp_convert (ptrdiff_type_node, maxindex, complain); 4223 maxindex = fold_simple (maxindex); 4224 4225 if (TREE_CODE (atype) == ARRAY_TYPE) 4226 { 4227 ptype = build_pointer_type (type); 4228 base = decay_conversion (base, complain); 4229 if (base == error_mark_node) 4230 return error_mark_node; 4231 base = cp_convert (ptype, base, complain); 4232 } 4233 else 4234 ptype = atype; 4235 4236 /* The code we are generating looks like: 4237 ({ 4238 T* t1 = (T*) base; 4239 T* rval = t1; 4240 ptrdiff_t iterator = maxindex; 4241 try { 4242 for (; iterator != -1; --iterator) { 4243 ... initialize *t1 ... 4244 ++t1; 4245 } 4246 } catch (...) { 4247 ... destroy elements that were constructed ... 4248 } 4249 rval; 4250 }) 4251 4252 We can omit the try and catch blocks if we know that the 4253 initialization will never throw an exception, or if the array 4254 elements do not have destructors. We can omit the loop completely if 4255 the elements of the array do not have constructors. 4256 4257 We actually wrap the entire body of the above in a STMT_EXPR, for 4258 tidiness. 4259 4260 When copying from array to another, when the array elements have 4261 only trivial copy constructors, we should use __builtin_memcpy 4262 rather than generating a loop. That way, we could take advantage 4263 of whatever cleverness the back end has for dealing with copies 4264 of blocks of memory. */ 4265 4266 is_global = begin_init_stmts (&stmt_expr, &compound_stmt); 4267 destroy_temps = stmts_are_full_exprs_p (); 4268 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 4269 rval = get_temp_regvar (ptype, base); 4270 base = get_temp_regvar (ptype, rval); 4271 iterator = get_temp_regvar (ptrdiff_type_node, maxindex); 4272 4273 /* If initializing one array from another, initialize element by 4274 element. We rely upon the below calls to do the argument 4275 checking. Evaluate the initializer before entering the try block. */ 4276 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR) 4277 { 4278 if (lvalue_kind (init) & clk_rvalueref) 4279 xvalue = true; 4280 base2 = decay_conversion (init, complain); 4281 if (base2 == error_mark_node) 4282 return error_mark_node; 4283 itype = TREE_TYPE (base2); 4284 base2 = get_temp_regvar (itype, base2); 4285 itype = TREE_TYPE (itype); 4286 } 4287 4288 /* Protect the entire array initialization so that we can destroy 4289 the partially constructed array if an exception is thrown. 4290 But don't do this if we're assigning. */ 4291 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 4292 && from_array != 2) 4293 { 4294 try_block = begin_try_block (); 4295 } 4296 4297 /* Should we try to create a constant initializer? */ 4298 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE 4299 && TREE_CONSTANT (maxindex) 4300 && (init ? TREE_CODE (init) == CONSTRUCTOR 4301 : (type_has_constexpr_default_constructor 4302 (inner_elt_type))) 4303 && (literal_type_p (inner_elt_type) 4304 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type))); 4305 vec<constructor_elt, va_gc> *const_vec = NULL; 4306 bool saw_non_const = false; 4307 /* If we're initializing a static array, we want to do static 4308 initialization of any elements with constant initializers even if 4309 some are non-constant. */ 4310 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase)); 4311 4312 bool empty_list = false; 4313 if (init && BRACE_ENCLOSED_INITIALIZER_P (init) 4314 && CONSTRUCTOR_NELTS (init) == 0) 4315 /* Skip over the handling of non-empty init lists. */ 4316 empty_list = true; 4317 4318 /* Maybe pull out constant value when from_array? */ 4319 4320 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR) 4321 { 4322 /* Do non-default initialization of non-trivial arrays resulting from 4323 brace-enclosed initializers. */ 4324 unsigned HOST_WIDE_INT idx; 4325 tree field, elt; 4326 /* If the constructor already has the array type, it's been through 4327 digest_init, so we shouldn't try to do anything more. */ 4328 bool digested = same_type_p (atype, TREE_TYPE (init)); 4329 from_array = 0; 4330 4331 if (length_check) 4332 finish_length_check (atype, iterator, obase, CONSTRUCTOR_NELTS (init)); 4333 4334 if (try_const) 4335 vec_alloc (const_vec, CONSTRUCTOR_NELTS (init)); 4336 4337 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt) 4338 { 4339 tree baseref = build1 (INDIRECT_REF, type, base); 4340 tree one_init; 4341 4342 num_initialized_elts++; 4343 4344 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 4345 if (digested) 4346 one_init = build2 (INIT_EXPR, type, baseref, elt); 4347 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE) 4348 one_init = build_aggr_init (baseref, elt, 0, complain); 4349 else 4350 one_init = cp_build_modify_expr (input_location, baseref, 4351 NOP_EXPR, elt, complain); 4352 if (one_init == error_mark_node) 4353 errors = true; 4354 if (try_const) 4355 { 4356 tree e = maybe_constant_init (one_init); 4357 if (reduced_constant_expression_p (e)) 4358 { 4359 CONSTRUCTOR_APPEND_ELT (const_vec, field, e); 4360 if (do_static_init) 4361 one_init = NULL_TREE; 4362 else 4363 one_init = build2 (INIT_EXPR, type, baseref, e); 4364 } 4365 else 4366 { 4367 if (do_static_init) 4368 { 4369 tree value = build_zero_init (TREE_TYPE (e), NULL_TREE, 4370 true); 4371 if (value) 4372 CONSTRUCTOR_APPEND_ELT (const_vec, field, value); 4373 } 4374 saw_non_const = true; 4375 } 4376 } 4377 4378 if (one_init) 4379 finish_expr_stmt (one_init); 4380 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 4381 4382 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, false, 4383 complain); 4384 if (one_init == error_mark_node) 4385 errors = true; 4386 else 4387 finish_expr_stmt (one_init); 4388 4389 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false, 4390 complain); 4391 if (one_init == error_mark_node) 4392 errors = true; 4393 else 4394 finish_expr_stmt (one_init); 4395 } 4396 4397 /* Any elements without explicit initializers get T{}. */ 4398 empty_list = true; 4399 } 4400 else if (init && TREE_CODE (init) == STRING_CST) 4401 { 4402 /* Check that the array is at least as long as the string. */ 4403 if (length_check) 4404 finish_length_check (atype, iterator, obase, 4405 TREE_STRING_LENGTH (init)); 4406 tree length = build_int_cst (ptrdiff_type_node, 4407 TREE_STRING_LENGTH (init)); 4408 4409 /* Copy the string to the first part of the array. */ 4410 tree alias_set = build_int_cst (build_pointer_type (type), 0); 4411 tree lhs = build2 (MEM_REF, TREE_TYPE (init), base, alias_set); 4412 tree stmt = build2 (MODIFY_EXPR, void_type_node, lhs, init); 4413 finish_expr_stmt (stmt); 4414 4415 /* Adjust the counter and pointer. */ 4416 stmt = cp_build_binary_op (loc, MINUS_EXPR, iterator, length, complain); 4417 stmt = build2 (MODIFY_EXPR, void_type_node, iterator, stmt); 4418 finish_expr_stmt (stmt); 4419 4420 stmt = cp_build_binary_op (loc, PLUS_EXPR, base, length, complain); 4421 stmt = build2 (MODIFY_EXPR, void_type_node, base, stmt); 4422 finish_expr_stmt (stmt); 4423 4424 /* And set the rest of the array to NUL. */ 4425 from_array = 0; 4426 explicit_value_init_p = true; 4427 } 4428 else if (from_array) 4429 { 4430 if (init) 4431 /* OK, we set base2 above. */; 4432 else if (CLASS_TYPE_P (type) 4433 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) 4434 { 4435 if (complain & tf_error) 4436 error ("initializer ends prematurely"); 4437 errors = true; 4438 } 4439 } 4440 4441 /* Now, default-initialize any remaining elements. We don't need to 4442 do that if a) the type does not need constructing, or b) we've 4443 already initialized all the elements. 4444 4445 We do need to keep going if we're copying an array. */ 4446 4447 if (try_const && !init) 4448 /* With a constexpr default constructor, which we checked for when 4449 setting try_const above, default-initialization is equivalent to 4450 value-initialization, and build_value_init gives us something more 4451 friendly to maybe_constant_init. */ 4452 explicit_value_init_p = true; 4453 if (from_array 4454 || ((type_build_ctor_call (type) || init || explicit_value_init_p) 4455 && ! (tree_fits_shwi_p (maxindex) 4456 && (num_initialized_elts 4457 == tree_to_shwi (maxindex) + 1)))) 4458 { 4459 /* If the ITERATOR is lesser or equal to -1, then we don't have to loop; 4460 we've already initialized all the elements. */ 4461 tree for_stmt; 4462 tree elt_init; 4463 tree to; 4464 4465 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE); 4466 finish_init_stmt (for_stmt); 4467 finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator, 4468 build_int_cst (TREE_TYPE (iterator), -1)), 4469 for_stmt, false, 0); 4470 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false, 4471 complain); 4472 if (elt_init == error_mark_node) 4473 errors = true; 4474 finish_for_expr (elt_init, for_stmt); 4475 4476 to = build1 (INDIRECT_REF, type, base); 4477 4478 /* If the initializer is {}, then all elements are initialized from T{}. 4479 But for non-classes, that's the same as value-initialization. */ 4480 if (empty_list) 4481 { 4482 if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type)) 4483 { 4484 init = build_constructor (init_list_type_node, NULL); 4485 } 4486 else 4487 { 4488 init = NULL_TREE; 4489 explicit_value_init_p = true; 4490 } 4491 } 4492 4493 if (from_array) 4494 { 4495 tree from; 4496 4497 if (base2) 4498 { 4499 from = build1 (INDIRECT_REF, itype, base2); 4500 if (xvalue) 4501 from = move (from); 4502 if (direct_init) 4503 from = build_tree_list (NULL_TREE, from); 4504 } 4505 else 4506 from = NULL_TREE; 4507 4508 if (TREE_CODE (type) == ARRAY_TYPE) 4509 elt_init = build_vec_init (to, NULL_TREE, from, /*val_init*/false, 4510 from_array, complain); 4511 else if (from_array == 2) 4512 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, 4513 from, complain); 4514 else if (type_build_ctor_call (type)) 4515 elt_init = build_aggr_init (to, from, 0, complain); 4516 else if (from) 4517 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, from, 4518 complain); 4519 else 4520 gcc_unreachable (); 4521 } 4522 else if (TREE_CODE (type) == ARRAY_TYPE) 4523 { 4524 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)) 4525 { 4526 if ((complain & tf_error)) 4527 error_at (loc, "array must be initialized " 4528 "with a brace-enclosed initializer"); 4529 elt_init = error_mark_node; 4530 } 4531 else 4532 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base), 4533 0, init, 4534 explicit_value_init_p, 4535 0, complain); 4536 } 4537 else if (explicit_value_init_p) 4538 { 4539 elt_init = build_value_init (type, complain); 4540 if (elt_init != error_mark_node) 4541 elt_init = build2 (INIT_EXPR, type, to, elt_init); 4542 } 4543 else 4544 { 4545 gcc_assert (type_build_ctor_call (type) || init); 4546 if (CLASS_TYPE_P (type)) 4547 elt_init = build_aggr_init (to, init, 0, complain); 4548 else 4549 { 4550 if (TREE_CODE (init) == TREE_LIST) 4551 init = build_x_compound_expr_from_list (init, ELK_INIT, 4552 complain); 4553 elt_init = (init == error_mark_node 4554 ? error_mark_node 4555 : build2 (INIT_EXPR, type, to, init)); 4556 } 4557 } 4558 4559 if (elt_init == error_mark_node) 4560 errors = true; 4561 4562 if (try_const) 4563 { 4564 /* FIXME refs to earlier elts */ 4565 tree e = maybe_constant_init (elt_init); 4566 if (reduced_constant_expression_p (e)) 4567 { 4568 if (initializer_zerop (e)) 4569 /* Don't fill the CONSTRUCTOR with zeros. */ 4570 e = NULL_TREE; 4571 if (do_static_init) 4572 elt_init = NULL_TREE; 4573 } 4574 else 4575 { 4576 saw_non_const = true; 4577 if (do_static_init) 4578 e = build_zero_init (TREE_TYPE (e), NULL_TREE, true); 4579 else 4580 e = NULL_TREE; 4581 } 4582 4583 if (e) 4584 { 4585 HOST_WIDE_INT last = tree_to_shwi (maxindex); 4586 if (num_initialized_elts <= last) 4587 { 4588 tree field = size_int (num_initialized_elts); 4589 if (num_initialized_elts != last) 4590 field = build2 (RANGE_EXPR, sizetype, field, 4591 size_int (last)); 4592 CONSTRUCTOR_APPEND_ELT (const_vec, field, e); 4593 } 4594 } 4595 } 4596 4597 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 4598 if (elt_init && !errors) 4599 finish_expr_stmt (elt_init); 4600 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 4601 4602 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false, 4603 complain)); 4604 if (base2) 4605 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, false, 4606 complain)); 4607 4608 finish_for_stmt (for_stmt); 4609 } 4610 4611 /* Make sure to cleanup any partially constructed elements. */ 4612 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 4613 && from_array != 2) 4614 { 4615 tree e; 4616 tree m = cp_build_binary_op (input_location, 4617 MINUS_EXPR, maxindex, iterator, 4618 complain); 4619 4620 /* Flatten multi-dimensional array since build_vec_delete only 4621 expects one-dimensional array. */ 4622 if (TREE_CODE (type) == ARRAY_TYPE) 4623 m = cp_build_binary_op (input_location, 4624 MULT_EXPR, m, 4625 /* Avoid mixing signed and unsigned. */ 4626 convert (TREE_TYPE (m), 4627 array_type_nelts_total (type)), 4628 complain); 4629 4630 finish_cleanup_try_block (try_block); 4631 e = build_vec_delete_1 (rval, m, 4632 inner_elt_type, sfk_complete_destructor, 4633 /*use_global_delete=*/0, complain); 4634 if (e == error_mark_node) 4635 errors = true; 4636 finish_cleanup (e, try_block); 4637 } 4638 4639 /* The value of the array initialization is the array itself, RVAL 4640 is a pointer to the first element. */ 4641 finish_stmt_expr_expr (rval, stmt_expr); 4642 4643 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); 4644 4645 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; 4646 4647 if (errors) 4648 return error_mark_node; 4649 4650 if (try_const) 4651 { 4652 if (!saw_non_const) 4653 { 4654 tree const_init = build_constructor (atype, const_vec); 4655 return build2 (INIT_EXPR, atype, obase, const_init); 4656 } 4657 else if (do_static_init && !vec_safe_is_empty (const_vec)) 4658 DECL_INITIAL (obase) = build_constructor (atype, const_vec); 4659 else 4660 vec_free (const_vec); 4661 } 4662 4663 /* Now make the result have the correct type. */ 4664 if (TREE_CODE (atype) == ARRAY_TYPE) 4665 { 4666 atype = build_pointer_type (atype); 4667 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr); 4668 stmt_expr = cp_build_fold_indirect_ref (stmt_expr); 4669 TREE_NO_WARNING (stmt_expr) = 1; 4670 } 4671 4672 return stmt_expr; 4673 } 4674 4675 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for 4676 build_delete. */ 4677 4678 static tree 4679 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags, 4680 tsubst_flags_t complain) 4681 { 4682 tree name; 4683 switch (dtor_kind) 4684 { 4685 case sfk_complete_destructor: 4686 name = complete_dtor_identifier; 4687 break; 4688 4689 case sfk_base_destructor: 4690 name = base_dtor_identifier; 4691 break; 4692 4693 case sfk_deleting_destructor: 4694 name = deleting_dtor_identifier; 4695 break; 4696 4697 default: 4698 gcc_unreachable (); 4699 } 4700 4701 return build_special_member_call (exp, name, 4702 /*args=*/NULL, 4703 /*binfo=*/TREE_TYPE (exp), 4704 flags, 4705 complain); 4706 } 4707 4708 /* Generate a call to a destructor. TYPE is the type to cast ADDR to. 4709 ADDR is an expression which yields the store to be destroyed. 4710 AUTO_DELETE is the name of the destructor to call, i.e., either 4711 sfk_complete_destructor, sfk_base_destructor, or 4712 sfk_deleting_destructor. 4713 4714 FLAGS is the logical disjunction of zero or more LOOKUP_ 4715 flags. See cp-tree.h for more info. */ 4716 4717 tree 4718 build_delete (tree otype, tree addr, special_function_kind auto_delete, 4719 int flags, int use_global_delete, tsubst_flags_t complain) 4720 { 4721 tree expr; 4722 4723 if (addr == error_mark_node) 4724 return error_mark_node; 4725 4726 tree type = TYPE_MAIN_VARIANT (otype); 4727 4728 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type 4729 set to `error_mark_node' before it gets properly cleaned up. */ 4730 if (type == error_mark_node) 4731 return error_mark_node; 4732 4733 if (TYPE_PTR_P (type)) 4734 type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 4735 4736 if (TREE_CODE (type) == ARRAY_TYPE) 4737 { 4738 if (TYPE_DOMAIN (type) == NULL_TREE) 4739 { 4740 if (complain & tf_error) 4741 error ("unknown array size in delete"); 4742 return error_mark_node; 4743 } 4744 return build_vec_delete (addr, array_type_nelts (type), 4745 auto_delete, use_global_delete, complain); 4746 } 4747 4748 bool deleting = (auto_delete == sfk_deleting_destructor); 4749 gcc_assert (deleting == !(flags & LOOKUP_DESTRUCTOR)); 4750 4751 if (TYPE_PTR_P (otype)) 4752 { 4753 addr = mark_rvalue_use (addr); 4754 4755 /* We don't want to warn about delete of void*, only other 4756 incomplete types. Deleting other incomplete types 4757 invokes undefined behavior, but it is not ill-formed, so 4758 compile to something that would even do The Right Thing 4759 (TM) should the type have a trivial dtor and no delete 4760 operator. */ 4761 if (!VOID_TYPE_P (type)) 4762 { 4763 complete_type (type); 4764 if (!COMPLETE_TYPE_P (type)) 4765 { 4766 if (complain & tf_warning) 4767 { 4768 auto_diagnostic_group d; 4769 if (warning (OPT_Wdelete_incomplete, 4770 "possible problem detected in invocation of " 4771 "delete operator:")) 4772 { 4773 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING); 4774 inform (input_location, 4775 "neither the destructor nor the class-specific " 4776 "operator delete will be called, even if they " 4777 "are declared when the class is defined"); 4778 } 4779 } 4780 } 4781 else if (deleting && warn_delnonvdtor 4782 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type) 4783 && TYPE_POLYMORPHIC_P (type)) 4784 { 4785 tree dtor = CLASSTYPE_DESTRUCTOR (type); 4786 if (!dtor || !DECL_VINDEX (dtor)) 4787 { 4788 if (CLASSTYPE_PURE_VIRTUALS (type)) 4789 warning (OPT_Wdelete_non_virtual_dtor, 4790 "deleting object of abstract class type %qT" 4791 " which has non-virtual destructor" 4792 " will cause undefined behavior", type); 4793 else 4794 warning (OPT_Wdelete_non_virtual_dtor, 4795 "deleting object of polymorphic class type %qT" 4796 " which has non-virtual destructor" 4797 " might cause undefined behavior", type); 4798 } 4799 } 4800 } 4801 4802 /* Throw away const and volatile on target type of addr. */ 4803 addr = convert_force (build_pointer_type (type), addr, 0, complain); 4804 } 4805 else 4806 { 4807 /* Don't check PROTECT here; leave that decision to the 4808 destructor. If the destructor is accessible, call it, 4809 else report error. */ 4810 addr = cp_build_addr_expr (addr, complain); 4811 if (addr == error_mark_node) 4812 return error_mark_node; 4813 4814 addr = convert_force (build_pointer_type (type), addr, 0, complain); 4815 } 4816 4817 if (deleting) 4818 /* We will use ADDR multiple times so we must save it. */ 4819 addr = save_expr (addr); 4820 4821 bool virtual_p = false; 4822 if (type_build_dtor_call (type)) 4823 { 4824 if (CLASSTYPE_LAZY_DESTRUCTOR (type)) 4825 lazily_declare_fn (sfk_destructor, type); 4826 virtual_p = DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type)); 4827 } 4828 4829 tree head = NULL_TREE; 4830 tree do_delete = NULL_TREE; 4831 bool destroying_delete = false; 4832 4833 if (!deleting) 4834 { 4835 /* Leave do_delete null. */ 4836 } 4837 /* For `::delete x', we must not use the deleting destructor 4838 since then we would not be sure to get the global `operator 4839 delete'. */ 4840 else if (use_global_delete) 4841 { 4842 head = get_target_expr (build_headof (addr)); 4843 /* Delete the object. */ 4844 do_delete = build_op_delete_call (DELETE_EXPR, 4845 head, 4846 cxx_sizeof_nowarn (type), 4847 /*global_p=*/true, 4848 /*placement=*/NULL_TREE, 4849 /*alloc_fn=*/NULL_TREE, 4850 complain); 4851 /* Otherwise, treat this like a complete object destructor 4852 call. */ 4853 auto_delete = sfk_complete_destructor; 4854 } 4855 /* If the destructor is non-virtual, there is no deleting 4856 variant. Instead, we must explicitly call the appropriate 4857 `operator delete' here. */ 4858 else if (!virtual_p) 4859 { 4860 /* Build the call. */ 4861 do_delete = build_op_delete_call (DELETE_EXPR, 4862 addr, 4863 cxx_sizeof_nowarn (type), 4864 /*global_p=*/false, 4865 /*placement=*/NULL_TREE, 4866 /*alloc_fn=*/NULL_TREE, 4867 complain); 4868 /* Call the complete object destructor. */ 4869 auto_delete = sfk_complete_destructor; 4870 if (do_delete != error_mark_node) 4871 { 4872 tree fn = get_callee_fndecl (do_delete); 4873 destroying_delete = destroying_delete_p (fn); 4874 } 4875 } 4876 else if (TYPE_GETS_REG_DELETE (type)) 4877 { 4878 /* Make sure we have access to the member op delete, even though 4879 we'll actually be calling it from the destructor. */ 4880 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type), 4881 /*global_p=*/false, 4882 /*placement=*/NULL_TREE, 4883 /*alloc_fn=*/NULL_TREE, 4884 complain); 4885 } 4886 4887 if (!destroying_delete && type_build_dtor_call (type)) 4888 expr = build_dtor_call (cp_build_fold_indirect_ref (addr), 4889 auto_delete, flags, complain); 4890 else 4891 expr = build_trivial_dtor_call (addr); 4892 if (expr == error_mark_node) 4893 return error_mark_node; 4894 4895 if (!deleting) 4896 return expr; 4897 4898 if (do_delete && !TREE_SIDE_EFFECTS (expr)) 4899 expr = do_delete; 4900 else if (do_delete) 4901 /* The delete operator must be called, regardless of whether 4902 the destructor throws. 4903 4904 [expr.delete]/7 The deallocation function is called 4905 regardless of whether the destructor for the object or some 4906 element of the array throws an exception. */ 4907 expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete); 4908 4909 /* We need to calculate this before the dtor changes the vptr. */ 4910 if (head) 4911 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr); 4912 4913 /* Handle deleting a null pointer. */ 4914 warning_sentinel s (warn_address); 4915 tree ifexp = cp_build_binary_op (input_location, NE_EXPR, addr, 4916 nullptr_node, complain); 4917 ifexp = cp_fully_fold (ifexp); 4918 4919 if (ifexp == error_mark_node) 4920 return error_mark_node; 4921 /* This is a compiler generated comparison, don't emit 4922 e.g. -Wnonnull-compare warning for it. */ 4923 else if (TREE_CODE (ifexp) == NE_EXPR) 4924 TREE_NO_WARNING (ifexp) = 1; 4925 4926 if (!integer_nonzerop (ifexp)) 4927 expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node); 4928 4929 return expr; 4930 } 4931 4932 /* At the beginning of a destructor, push cleanups that will call the 4933 destructors for our base classes and members. 4934 4935 Called from begin_destructor_body. */ 4936 4937 void 4938 push_base_cleanups (void) 4939 { 4940 tree binfo, base_binfo; 4941 int i; 4942 tree member; 4943 tree expr; 4944 vec<tree, va_gc> *vbases; 4945 4946 /* Run destructors for all virtual baseclasses. */ 4947 if (!ABSTRACT_CLASS_TYPE_P (current_class_type) 4948 && CLASSTYPE_VBASECLASSES (current_class_type)) 4949 { 4950 tree cond = (condition_conversion 4951 (build2 (BIT_AND_EXPR, integer_type_node, 4952 current_in_charge_parm, 4953 integer_two_node))); 4954 4955 /* The CLASSTYPE_VBASECLASSES vector is in initialization 4956 order, which is also the right order for pushing cleanups. */ 4957 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0; 4958 vec_safe_iterate (vbases, i, &base_binfo); i++) 4959 { 4960 if (type_build_dtor_call (BINFO_TYPE (base_binfo))) 4961 { 4962 expr = build_special_member_call (current_class_ref, 4963 base_dtor_identifier, 4964 NULL, 4965 base_binfo, 4966 (LOOKUP_NORMAL 4967 | LOOKUP_NONVIRTUAL), 4968 tf_warning_or_error); 4969 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))) 4970 { 4971 expr = build3 (COND_EXPR, void_type_node, cond, 4972 expr, void_node); 4973 finish_decl_cleanup (NULL_TREE, expr); 4974 } 4975 } 4976 } 4977 } 4978 4979 /* Take care of the remaining baseclasses. */ 4980 for (binfo = TYPE_BINFO (current_class_type), i = 0; 4981 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 4982 { 4983 if (BINFO_VIRTUAL_P (base_binfo) 4984 || !type_build_dtor_call (BINFO_TYPE (base_binfo))) 4985 continue; 4986 4987 expr = build_special_member_call (current_class_ref, 4988 base_dtor_identifier, 4989 NULL, base_binfo, 4990 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL, 4991 tf_warning_or_error); 4992 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))) 4993 finish_decl_cleanup (NULL_TREE, expr); 4994 } 4995 4996 /* Don't automatically destroy union members. */ 4997 if (TREE_CODE (current_class_type) == UNION_TYPE) 4998 return; 4999 5000 for (member = TYPE_FIELDS (current_class_type); member; 5001 member = DECL_CHAIN (member)) 5002 { 5003 tree this_type = TREE_TYPE (member); 5004 if (this_type == error_mark_node 5005 || TREE_CODE (member) != FIELD_DECL 5006 || DECL_ARTIFICIAL (member)) 5007 continue; 5008 if (ANON_AGGR_TYPE_P (this_type)) 5009 continue; 5010 if (type_build_dtor_call (this_type)) 5011 { 5012 tree this_member = (build_class_member_access_expr 5013 (current_class_ref, member, 5014 /*access_path=*/NULL_TREE, 5015 /*preserve_reference=*/false, 5016 tf_warning_or_error)); 5017 expr = build_delete (this_type, this_member, 5018 sfk_complete_destructor, 5019 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 5020 0, tf_warning_or_error); 5021 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type)) 5022 finish_decl_cleanup (NULL_TREE, expr); 5023 } 5024 } 5025 } 5026 5027 /* Build a C++ vector delete expression. 5028 MAXINDEX is the number of elements to be deleted. 5029 ELT_SIZE is the nominal size of each element in the vector. 5030 BASE is the expression that should yield the store to be deleted. 5031 This function expands (or synthesizes) these calls itself. 5032 AUTO_DELETE_VEC says whether the container (vector) should be deallocated. 5033 5034 This also calls delete for virtual baseclasses of elements of the vector. 5035 5036 Update: MAXINDEX is no longer needed. The size can be extracted from the 5037 start of the vector for pointers, and from the type for arrays. We still 5038 use MAXINDEX for arrays because it happens to already have one of the 5039 values we'd have to extract. (We could use MAXINDEX with pointers to 5040 confirm the size, and trap if the numbers differ; not clear that it'd 5041 be worth bothering.) */ 5042 5043 tree 5044 build_vec_delete (tree base, tree maxindex, 5045 special_function_kind auto_delete_vec, 5046 int use_global_delete, tsubst_flags_t complain) 5047 { 5048 tree type; 5049 tree rval; 5050 tree base_init = NULL_TREE; 5051 5052 type = TREE_TYPE (base); 5053 5054 if (TYPE_PTR_P (type)) 5055 { 5056 /* Step back one from start of vector, and read dimension. */ 5057 tree cookie_addr; 5058 tree size_ptr_type = build_pointer_type (sizetype); 5059 5060 base = mark_rvalue_use (base); 5061 if (TREE_SIDE_EFFECTS (base)) 5062 { 5063 base_init = get_target_expr (base); 5064 base = TARGET_EXPR_SLOT (base_init); 5065 } 5066 type = strip_array_types (TREE_TYPE (type)); 5067 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR, 5068 sizetype, TYPE_SIZE_UNIT (sizetype)); 5069 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base), 5070 cookie_addr); 5071 maxindex = cp_build_fold_indirect_ref (cookie_addr); 5072 } 5073 else if (TREE_CODE (type) == ARRAY_TYPE) 5074 { 5075 /* Get the total number of things in the array, maxindex is a 5076 bad name. */ 5077 maxindex = array_type_nelts_total (type); 5078 type = strip_array_types (type); 5079 base = decay_conversion (base, complain); 5080 if (base == error_mark_node) 5081 return error_mark_node; 5082 if (TREE_SIDE_EFFECTS (base)) 5083 { 5084 base_init = get_target_expr (base); 5085 base = TARGET_EXPR_SLOT (base_init); 5086 } 5087 } 5088 else 5089 { 5090 if (base != error_mark_node && !(complain & tf_error)) 5091 error ("type to vector delete is neither pointer or array type"); 5092 return error_mark_node; 5093 } 5094 5095 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec, 5096 use_global_delete, complain); 5097 if (base_init && rval != error_mark_node) 5098 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval); 5099 5100 return rval; 5101 } 5102 5103 #include "gt-cp-init.h" 5104