1 /* Handle initialization things in C++. 2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 4 Free Software Foundation, Inc. 5 Contributed by Michael Tiemann (tiemann@cygnus.com) 6 7 This file is part of GCC. 8 9 GCC is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3, or (at your option) 12 any later version. 13 14 GCC is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with GCC; see the file COPYING3. If not see 21 <http://www.gnu.org/licenses/>. */ 22 23 /* High-level class interface. */ 24 25 #include "config.h" 26 #include "system.h" 27 #include "coretypes.h" 28 #include "tm.h" 29 #include "tree.h" 30 #include "rtl.h" 31 #include "expr.h" 32 #include "cp-tree.h" 33 #include "flags.h" 34 #include "output.h" 35 #include "except.h" 36 #include "toplev.h" 37 #include "target.h" 38 39 static bool begin_init_stmts (tree *, tree *); 40 static tree finish_init_stmts (bool, tree, tree); 41 static void construct_virtual_base (tree, tree); 42 static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t); 43 static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t); 44 static tree build_vec_delete_1 (tree, tree, tree, special_function_kind, int); 45 static void perform_member_init (tree, tree); 46 static tree build_builtin_delete_call (tree); 47 static int member_init_ok_or_else (tree, tree, tree); 48 static void expand_virtual_init (tree, tree); 49 static tree sort_mem_initializers (tree, tree); 50 static tree initializing_context (tree); 51 static void expand_cleanup_for_base (tree, tree); 52 static tree get_temp_regvar (tree, tree); 53 static tree dfs_initialize_vtbl_ptrs (tree, void *); 54 static tree build_dtor_call (tree, special_function_kind, int); 55 static tree build_field_list (tree, tree, int *); 56 static tree build_vtbl_address (tree); 57 58 /* We are about to generate some complex initialization code. 59 Conceptually, it is all a single expression. However, we may want 60 to include conditionals, loops, and other such statement-level 61 constructs. Therefore, we build the initialization code inside a 62 statement-expression. This function starts such an expression. 63 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function; 64 pass them back to finish_init_stmts when the expression is 65 complete. */ 66 67 static bool 68 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p) 69 { 70 bool is_global = !building_stmt_tree (); 71 72 *stmt_expr_p = begin_stmt_expr (); 73 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE); 74 75 return is_global; 76 } 77 78 /* Finish out the statement-expression begun by the previous call to 79 begin_init_stmts. Returns the statement-expression itself. */ 80 81 static tree 82 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt) 83 { 84 finish_compound_stmt (compound_stmt); 85 86 stmt_expr = finish_stmt_expr (stmt_expr, true); 87 88 gcc_assert (!building_stmt_tree () == is_global); 89 90 return stmt_expr; 91 } 92 93 /* Constructors */ 94 95 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base 96 which we want to initialize the vtable pointer for, DATA is 97 TREE_LIST whose TREE_VALUE is the this ptr expression. */ 98 99 static tree 100 dfs_initialize_vtbl_ptrs (tree binfo, void *data) 101 { 102 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 103 return dfs_skip_bases; 104 105 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo)) 106 { 107 tree base_ptr = TREE_VALUE ((tree) data); 108 109 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1); 110 111 expand_virtual_init (binfo, base_ptr); 112 } 113 114 return NULL_TREE; 115 } 116 117 /* Initialize all the vtable pointers in the object pointed to by 118 ADDR. */ 119 120 void 121 initialize_vtbl_ptrs (tree addr) 122 { 123 tree list; 124 tree type; 125 126 type = TREE_TYPE (TREE_TYPE (addr)); 127 list = build_tree_list (type, addr); 128 129 /* Walk through the hierarchy, initializing the vptr in each base 130 class. We do these in pre-order because we can't find the virtual 131 bases for a class until we've initialized the vtbl for that 132 class. */ 133 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list); 134 } 135 136 /* Return an expression for the zero-initialization of an object with 137 type T. This expression will either be a constant (in the case 138 that T is a scalar), or a CONSTRUCTOR (in the case that T is an 139 aggregate), or NULL (in the case that T does not require 140 initialization). In either case, the value can be used as 141 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static 142 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS 143 is the number of elements in the array. If STATIC_STORAGE_P is 144 TRUE, initializers are only generated for entities for which 145 zero-initialization does not simply mean filling the storage with 146 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field, 147 subfields with bit positions at or above that bit size shouldn't 148 be added. */ 149 150 static tree 151 build_zero_init_1 (tree type, tree nelts, bool static_storage_p, 152 tree field_size) 153 { 154 tree init = NULL_TREE; 155 156 /* [dcl.init] 157 158 To zero-initialize an object of type T means: 159 160 -- if T is a scalar type, the storage is set to the value of zero 161 converted to T. 162 163 -- if T is a non-union class type, the storage for each nonstatic 164 data member and each base-class subobject is zero-initialized. 165 166 -- if T is a union type, the storage for its first data member is 167 zero-initialized. 168 169 -- if T is an array type, the storage for each element is 170 zero-initialized. 171 172 -- if T is a reference type, no initialization is performed. */ 173 174 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST); 175 176 if (type == error_mark_node) 177 ; 178 else if (static_storage_p && zero_init_p (type)) 179 /* In order to save space, we do not explicitly build initializers 180 for items that do not need them. GCC's semantics are that 181 items with static storage duration that are not otherwise 182 initialized are initialized to zero. */ 183 ; 184 else if (SCALAR_TYPE_P (type)) 185 init = convert (type, integer_zero_node); 186 else if (CLASS_TYPE_P (type)) 187 { 188 tree field; 189 VEC(constructor_elt,gc) *v = NULL; 190 191 /* Iterate over the fields, building initializations. */ 192 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 193 { 194 if (TREE_CODE (field) != FIELD_DECL) 195 continue; 196 197 /* Don't add virtual bases for base classes if they are beyond 198 the size of the current field, that means it is present 199 somewhere else in the object. */ 200 if (field_size) 201 { 202 tree bitpos = bit_position (field); 203 if (TREE_CODE (bitpos) == INTEGER_CST 204 && !tree_int_cst_lt (bitpos, field_size)) 205 continue; 206 } 207 208 /* Note that for class types there will be FIELD_DECLs 209 corresponding to base classes as well. Thus, iterating 210 over TYPE_FIELDs will result in correct initialization of 211 all of the subobjects. */ 212 if (!static_storage_p || !zero_init_p (TREE_TYPE (field))) 213 { 214 tree new_field_size 215 = (DECL_FIELD_IS_BASE (field) 216 && DECL_SIZE (field) 217 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST) 218 ? DECL_SIZE (field) : NULL_TREE; 219 tree value = build_zero_init_1 (TREE_TYPE (field), 220 /*nelts=*/NULL_TREE, 221 static_storage_p, 222 new_field_size); 223 if (value) 224 CONSTRUCTOR_APPEND_ELT(v, field, value); 225 } 226 227 /* For unions, only the first field is initialized. */ 228 if (TREE_CODE (type) == UNION_TYPE) 229 break; 230 } 231 232 /* Build a constructor to contain the initializations. */ 233 init = build_constructor (type, v); 234 } 235 else if (TREE_CODE (type) == ARRAY_TYPE) 236 { 237 tree max_index; 238 VEC(constructor_elt,gc) *v = NULL; 239 240 /* Iterate over the array elements, building initializations. */ 241 if (nelts) 242 max_index = fold_build2_loc (input_location, 243 MINUS_EXPR, TREE_TYPE (nelts), 244 nelts, integer_one_node); 245 else 246 max_index = array_type_nelts (type); 247 248 /* If we have an error_mark here, we should just return error mark 249 as we don't know the size of the array yet. */ 250 if (max_index == error_mark_node) 251 return error_mark_node; 252 gcc_assert (TREE_CODE (max_index) == INTEGER_CST); 253 254 /* A zero-sized array, which is accepted as an extension, will 255 have an upper bound of -1. */ 256 if (!tree_int_cst_equal (max_index, integer_minus_one_node)) 257 { 258 constructor_elt *ce; 259 260 v = VEC_alloc (constructor_elt, gc, 1); 261 ce = VEC_quick_push (constructor_elt, v, NULL); 262 263 /* If this is a one element array, we just use a regular init. */ 264 if (tree_int_cst_equal (size_zero_node, max_index)) 265 ce->index = size_zero_node; 266 else 267 ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node, 268 max_index); 269 270 ce->value = build_zero_init_1 (TREE_TYPE (type), 271 /*nelts=*/NULL_TREE, 272 static_storage_p, NULL_TREE); 273 } 274 275 /* Build a constructor to contain the initializations. */ 276 init = build_constructor (type, v); 277 } 278 else if (TREE_CODE (type) == VECTOR_TYPE) 279 init = fold_convert (type, integer_zero_node); 280 else 281 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); 282 283 /* In all cases, the initializer is a constant. */ 284 if (init) 285 TREE_CONSTANT (init) = 1; 286 287 return init; 288 } 289 290 /* Return an expression for the zero-initialization of an object with 291 type T. This expression will either be a constant (in the case 292 that T is a scalar), or a CONSTRUCTOR (in the case that T is an 293 aggregate), or NULL (in the case that T does not require 294 initialization). In either case, the value can be used as 295 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static 296 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS 297 is the number of elements in the array. If STATIC_STORAGE_P is 298 TRUE, initializers are only generated for entities for which 299 zero-initialization does not simply mean filling the storage with 300 zero bytes. */ 301 302 tree 303 build_zero_init (tree type, tree nelts, bool static_storage_p) 304 { 305 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE); 306 } 307 308 /* Return a suitable initializer for value-initializing an object of type 309 TYPE, as described in [dcl.init]. */ 310 311 tree 312 build_value_init (tree type) 313 { 314 /* [dcl.init] 315 316 To value-initialize an object of type T means: 317 318 - if T is a class type (clause 9) with a user-provided constructor 319 (12.1), then the default constructor for T is called (and the 320 initialization is ill-formed if T has no accessible default 321 constructor); 322 323 - if T is a non-union class type without a user-provided constructor, 324 then every non-static data member and base-class component of T is 325 value-initialized;92) 326 327 - if T is an array type, then each element is value-initialized; 328 329 - otherwise, the object is zero-initialized. 330 331 A program that calls for default-initialization or 332 value-initialization of an entity of reference type is ill-formed. 333 334 92) Value-initialization for such a class object may be implemented by 335 zero-initializing the object and then calling the default 336 constructor. */ 337 338 if (CLASS_TYPE_P (type)) 339 { 340 if (type_has_user_provided_constructor (type)) 341 return build_aggr_init_expr 342 (type, 343 build_special_member_call (NULL_TREE, complete_ctor_identifier, 344 NULL, type, LOOKUP_NORMAL, 345 tf_warning_or_error)); 346 else if (TREE_CODE (type) != UNION_TYPE && TYPE_NEEDS_CONSTRUCTING (type)) 347 { 348 /* This is a class that needs constructing, but doesn't have 349 a user-provided constructor. So we need to zero-initialize 350 the object and then call the implicitly defined ctor. 351 This will be handled in simplify_aggr_init_expr. */ 352 tree ctor = build_special_member_call 353 (NULL_TREE, complete_ctor_identifier, 354 NULL, type, LOOKUP_NORMAL, tf_warning_or_error); 355 356 ctor = build_aggr_init_expr (type, ctor); 357 AGGR_INIT_ZERO_FIRST (ctor) = 1; 358 return ctor; 359 } 360 } 361 return build_value_init_noctor (type); 362 } 363 364 /* Like build_value_init, but don't call the constructor for TYPE. Used 365 for base initializers. */ 366 367 tree 368 build_value_init_noctor (tree type) 369 { 370 if (CLASS_TYPE_P (type)) 371 { 372 gcc_assert (!TYPE_NEEDS_CONSTRUCTING (type)); 373 374 if (TREE_CODE (type) != UNION_TYPE) 375 { 376 tree field; 377 VEC(constructor_elt,gc) *v = NULL; 378 379 /* Iterate over the fields, building initializations. */ 380 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 381 { 382 tree ftype, value; 383 384 if (TREE_CODE (field) != FIELD_DECL) 385 continue; 386 387 ftype = TREE_TYPE (field); 388 389 if (TREE_CODE (ftype) == REFERENCE_TYPE) 390 error ("value-initialization of reference"); 391 392 /* We could skip vfields and fields of types with 393 user-defined constructors, but I think that won't improve 394 performance at all; it should be simpler in general just 395 to zero out the entire object than try to only zero the 396 bits that actually need it. */ 397 398 /* Note that for class types there will be FIELD_DECLs 399 corresponding to base classes as well. Thus, iterating 400 over TYPE_FIELDs will result in correct initialization of 401 all of the subobjects. */ 402 value = build_value_init (ftype); 403 404 if (value) 405 CONSTRUCTOR_APPEND_ELT(v, field, value); 406 } 407 408 /* Build a constructor to contain the zero- initializations. */ 409 return build_constructor (type, v); 410 } 411 } 412 else if (TREE_CODE (type) == ARRAY_TYPE) 413 { 414 VEC(constructor_elt,gc) *v = NULL; 415 416 /* Iterate over the array elements, building initializations. */ 417 tree max_index = array_type_nelts (type); 418 419 /* If we have an error_mark here, we should just return error mark 420 as we don't know the size of the array yet. */ 421 if (max_index == error_mark_node) 422 return error_mark_node; 423 gcc_assert (TREE_CODE (max_index) == INTEGER_CST); 424 425 /* A zero-sized array, which is accepted as an extension, will 426 have an upper bound of -1. */ 427 if (!tree_int_cst_equal (max_index, integer_minus_one_node)) 428 { 429 constructor_elt *ce; 430 431 v = VEC_alloc (constructor_elt, gc, 1); 432 ce = VEC_quick_push (constructor_elt, v, NULL); 433 434 /* If this is a one element array, we just use a regular init. */ 435 if (tree_int_cst_equal (size_zero_node, max_index)) 436 ce->index = size_zero_node; 437 else 438 ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node, 439 max_index); 440 441 ce->value = build_value_init (TREE_TYPE (type)); 442 443 /* The gimplifier can't deal with a RANGE_EXPR of TARGET_EXPRs. */ 444 gcc_assert (TREE_CODE (ce->value) != TARGET_EXPR 445 && TREE_CODE (ce->value) != AGGR_INIT_EXPR); 446 } 447 448 /* Build a constructor to contain the initializations. */ 449 return build_constructor (type, v); 450 } 451 452 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false); 453 } 454 455 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of 456 arguments. If TREE_LIST is void_type_node, an empty initializer 457 list was given; if NULL_TREE no initializer was given. */ 458 459 static void 460 perform_member_init (tree member, tree init) 461 { 462 tree decl; 463 tree type = TREE_TYPE (member); 464 465 /* Effective C++ rule 12 requires that all data members be 466 initialized. */ 467 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE) 468 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__, 469 "%qD should be initialized in the member initialization list", 470 member); 471 472 /* Get an lvalue for the data member. */ 473 decl = build_class_member_access_expr (current_class_ref, member, 474 /*access_path=*/NULL_TREE, 475 /*preserve_reference=*/true, 476 tf_warning_or_error); 477 if (decl == error_mark_node) 478 return; 479 480 if (init == void_type_node) 481 { 482 /* mem() means value-initialization. */ 483 if (TREE_CODE (type) == ARRAY_TYPE) 484 { 485 init = build_vec_init (decl, NULL_TREE, NULL_TREE, 486 /*explicit_value_init_p=*/true, 487 /* from_array=*/0, 488 tf_warning_or_error); 489 finish_expr_stmt (init); 490 } 491 else 492 { 493 if (TREE_CODE (type) == REFERENCE_TYPE) 494 permerror (DECL_SOURCE_LOCATION (current_function_decl), 495 "value-initialization of %q#D, which has reference type", 496 member); 497 else 498 { 499 init = build2 (INIT_EXPR, type, decl, build_value_init (type)); 500 finish_expr_stmt (init); 501 } 502 } 503 } 504 /* Deal with this here, as we will get confused if we try to call the 505 assignment op for an anonymous union. This can happen in a 506 synthesized copy constructor. */ 507 else if (ANON_AGGR_TYPE_P (type)) 508 { 509 if (init) 510 { 511 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init)); 512 finish_expr_stmt (init); 513 } 514 } 515 else if (TYPE_NEEDS_CONSTRUCTING (type)) 516 { 517 if (init != NULL_TREE 518 && TREE_CODE (type) == ARRAY_TYPE 519 && TREE_CHAIN (init) == NULL_TREE 520 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE) 521 { 522 /* Initialization of one array from another. */ 523 finish_expr_stmt (build_vec_init (decl, NULL_TREE, TREE_VALUE (init), 524 /*explicit_value_init_p=*/false, 525 /* from_array=*/1, 526 tf_warning_or_error)); 527 } 528 else 529 { 530 if (CP_TYPE_CONST_P (type) 531 && init == NULL_TREE 532 && !type_has_user_provided_default_constructor (type)) 533 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a 534 vtable; still give this diagnostic. */ 535 permerror (DECL_SOURCE_LOCATION (current_function_decl), 536 "uninitialized member %qD with %<const%> type %qT", 537 member, type); 538 finish_expr_stmt (build_aggr_init (decl, init, 0, 539 tf_warning_or_error)); 540 } 541 } 542 else 543 { 544 if (init == NULL_TREE) 545 { 546 /* member traversal: note it leaves init NULL */ 547 if (TREE_CODE (type) == REFERENCE_TYPE) 548 permerror (DECL_SOURCE_LOCATION (current_function_decl), 549 "uninitialized reference member %qD", 550 member); 551 else if (CP_TYPE_CONST_P (type)) 552 permerror (DECL_SOURCE_LOCATION (current_function_decl), 553 "uninitialized member %qD with %<const%> type %qT", 554 member, type); 555 } 556 else if (TREE_CODE (init) == TREE_LIST) 557 /* There was an explicit member initialization. Do some work 558 in that case. */ 559 init = build_x_compound_expr_from_list (init, "member initializer"); 560 561 if (init) 562 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init, 563 tf_warning_or_error)); 564 } 565 566 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 567 { 568 tree expr; 569 570 expr = build_class_member_access_expr (current_class_ref, member, 571 /*access_path=*/NULL_TREE, 572 /*preserve_reference=*/false, 573 tf_warning_or_error); 574 expr = build_delete (type, expr, sfk_complete_destructor, 575 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0); 576 577 if (expr != error_mark_node) 578 finish_eh_cleanup (expr); 579 } 580 } 581 582 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all 583 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */ 584 585 static tree 586 build_field_list (tree t, tree list, int *uses_unions_p) 587 { 588 tree fields; 589 590 *uses_unions_p = 0; 591 592 /* Note whether or not T is a union. */ 593 if (TREE_CODE (t) == UNION_TYPE) 594 *uses_unions_p = 1; 595 596 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields)) 597 { 598 /* Skip CONST_DECLs for enumeration constants and so forth. */ 599 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields)) 600 continue; 601 602 /* Keep track of whether or not any fields are unions. */ 603 if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE) 604 *uses_unions_p = 1; 605 606 /* For an anonymous struct or union, we must recursively 607 consider the fields of the anonymous type. They can be 608 directly initialized from the constructor. */ 609 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields))) 610 { 611 /* Add this field itself. Synthesized copy constructors 612 initialize the entire aggregate. */ 613 list = tree_cons (fields, NULL_TREE, list); 614 /* And now add the fields in the anonymous aggregate. */ 615 list = build_field_list (TREE_TYPE (fields), list, 616 uses_unions_p); 617 } 618 /* Add this field. */ 619 else if (DECL_NAME (fields)) 620 list = tree_cons (fields, NULL_TREE, list); 621 } 622 623 return list; 624 } 625 626 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives 627 a FIELD_DECL or BINFO in T that needs initialization. The 628 TREE_VALUE gives the initializer, or list of initializer arguments. 629 630 Return a TREE_LIST containing all of the initializations required 631 for T, in the order in which they should be performed. The output 632 list has the same format as the input. */ 633 634 static tree 635 sort_mem_initializers (tree t, tree mem_inits) 636 { 637 tree init; 638 tree base, binfo, base_binfo; 639 tree sorted_inits; 640 tree next_subobject; 641 VEC(tree,gc) *vbases; 642 int i; 643 int uses_unions_p; 644 645 /* Build up a list of initializations. The TREE_PURPOSE of entry 646 will be the subobject (a FIELD_DECL or BINFO) to initialize. The 647 TREE_VALUE will be the constructor arguments, or NULL if no 648 explicit initialization was provided. */ 649 sorted_inits = NULL_TREE; 650 651 /* Process the virtual bases. */ 652 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 653 VEC_iterate (tree, vbases, i, base); i++) 654 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits); 655 656 /* Process the direct bases. */ 657 for (binfo = TYPE_BINFO (t), i = 0; 658 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 659 if (!BINFO_VIRTUAL_P (base_binfo)) 660 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits); 661 662 /* Process the non-static data members. */ 663 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p); 664 /* Reverse the entire list of initializations, so that they are in 665 the order that they will actually be performed. */ 666 sorted_inits = nreverse (sorted_inits); 667 668 /* If the user presented the initializers in an order different from 669 that in which they will actually occur, we issue a warning. Keep 670 track of the next subobject which can be explicitly initialized 671 without issuing a warning. */ 672 next_subobject = sorted_inits; 673 674 /* Go through the explicit initializers, filling in TREE_PURPOSE in 675 the SORTED_INITS. */ 676 for (init = mem_inits; init; init = TREE_CHAIN (init)) 677 { 678 tree subobject; 679 tree subobject_init; 680 681 subobject = TREE_PURPOSE (init); 682 683 /* If the explicit initializers are in sorted order, then 684 SUBOBJECT will be NEXT_SUBOBJECT, or something following 685 it. */ 686 for (subobject_init = next_subobject; 687 subobject_init; 688 subobject_init = TREE_CHAIN (subobject_init)) 689 if (TREE_PURPOSE (subobject_init) == subobject) 690 break; 691 692 /* Issue a warning if the explicit initializer order does not 693 match that which will actually occur. 694 ??? Are all these on the correct lines? */ 695 if (warn_reorder && !subobject_init) 696 { 697 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL) 698 warning (OPT_Wreorder, "%q+D will be initialized after", 699 TREE_PURPOSE (next_subobject)); 700 else 701 warning (OPT_Wreorder, "base %qT will be initialized after", 702 TREE_PURPOSE (next_subobject)); 703 if (TREE_CODE (subobject) == FIELD_DECL) 704 warning (OPT_Wreorder, " %q+#D", subobject); 705 else 706 warning (OPT_Wreorder, " base %qT", subobject); 707 warning_at (DECL_SOURCE_LOCATION (current_function_decl), 708 OPT_Wreorder, " when initialized here"); 709 } 710 711 /* Look again, from the beginning of the list. */ 712 if (!subobject_init) 713 { 714 subobject_init = sorted_inits; 715 while (TREE_PURPOSE (subobject_init) != subobject) 716 subobject_init = TREE_CHAIN (subobject_init); 717 } 718 719 /* It is invalid to initialize the same subobject more than 720 once. */ 721 if (TREE_VALUE (subobject_init)) 722 { 723 if (TREE_CODE (subobject) == FIELD_DECL) 724 error_at (DECL_SOURCE_LOCATION (current_function_decl), 725 "multiple initializations given for %qD", 726 subobject); 727 else 728 error_at (DECL_SOURCE_LOCATION (current_function_decl), 729 "multiple initializations given for base %qT", 730 subobject); 731 } 732 733 /* Record the initialization. */ 734 TREE_VALUE (subobject_init) = TREE_VALUE (init); 735 next_subobject = subobject_init; 736 } 737 738 /* [class.base.init] 739 740 If a ctor-initializer specifies more than one mem-initializer for 741 multiple members of the same union (including members of 742 anonymous unions), the ctor-initializer is ill-formed. */ 743 if (uses_unions_p) 744 { 745 tree last_field = NULL_TREE; 746 for (init = sorted_inits; init; init = TREE_CHAIN (init)) 747 { 748 tree field; 749 tree field_type; 750 int done; 751 752 /* Skip uninitialized members and base classes. */ 753 if (!TREE_VALUE (init) 754 || TREE_CODE (TREE_PURPOSE (init)) != FIELD_DECL) 755 continue; 756 /* See if this field is a member of a union, or a member of a 757 structure contained in a union, etc. */ 758 field = TREE_PURPOSE (init); 759 for (field_type = DECL_CONTEXT (field); 760 !same_type_p (field_type, t); 761 field_type = TYPE_CONTEXT (field_type)) 762 if (TREE_CODE (field_type) == UNION_TYPE) 763 break; 764 /* If this field is not a member of a union, skip it. */ 765 if (TREE_CODE (field_type) != UNION_TYPE) 766 continue; 767 768 /* It's only an error if we have two initializers for the same 769 union type. */ 770 if (!last_field) 771 { 772 last_field = field; 773 continue; 774 } 775 776 /* See if LAST_FIELD and the field initialized by INIT are 777 members of the same union. If so, there's a problem, 778 unless they're actually members of the same structure 779 which is itself a member of a union. For example, given: 780 781 union { struct { int i; int j; }; }; 782 783 initializing both `i' and `j' makes sense. */ 784 field_type = DECL_CONTEXT (field); 785 done = 0; 786 do 787 { 788 tree last_field_type; 789 790 last_field_type = DECL_CONTEXT (last_field); 791 while (1) 792 { 793 if (same_type_p (last_field_type, field_type)) 794 { 795 if (TREE_CODE (field_type) == UNION_TYPE) 796 error_at (DECL_SOURCE_LOCATION (current_function_decl), 797 "initializations for multiple members of %qT", 798 last_field_type); 799 done = 1; 800 break; 801 } 802 803 if (same_type_p (last_field_type, t)) 804 break; 805 806 last_field_type = TYPE_CONTEXT (last_field_type); 807 } 808 809 /* If we've reached the outermost class, then we're 810 done. */ 811 if (same_type_p (field_type, t)) 812 break; 813 814 field_type = TYPE_CONTEXT (field_type); 815 } 816 while (!done); 817 818 last_field = field; 819 } 820 } 821 822 return sorted_inits; 823 } 824 825 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS 826 is a TREE_LIST giving the explicit mem-initializer-list for the 827 constructor. The TREE_PURPOSE of each entry is a subobject (a 828 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE 829 is a TREE_LIST giving the arguments to the constructor or 830 void_type_node for an empty list of arguments. */ 831 832 void 833 emit_mem_initializers (tree mem_inits) 834 { 835 /* We will already have issued an error message about the fact that 836 the type is incomplete. */ 837 if (!COMPLETE_TYPE_P (current_class_type)) 838 return; 839 840 /* Sort the mem-initializers into the order in which the 841 initializations should be performed. */ 842 mem_inits = sort_mem_initializers (current_class_type, mem_inits); 843 844 in_base_initializer = 1; 845 846 /* Initialize base classes. */ 847 while (mem_inits 848 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL) 849 { 850 tree subobject = TREE_PURPOSE (mem_inits); 851 tree arguments = TREE_VALUE (mem_inits); 852 853 /* If these initializations are taking place in a copy constructor, 854 the base class should probably be explicitly initialized if there 855 is a user-defined constructor in the base class (other than the 856 default constructor, which will be called anyway). */ 857 if (extra_warnings && !arguments 858 && DECL_COPY_CONSTRUCTOR_P (current_function_decl) 859 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject))) 860 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Wextra, 861 "base class %q#T should be explicitly initialized in the " 862 "copy constructor", 863 BINFO_TYPE (subobject)); 864 865 /* Initialize the base. */ 866 if (BINFO_VIRTUAL_P (subobject)) 867 construct_virtual_base (subobject, arguments); 868 else 869 { 870 tree base_addr; 871 872 base_addr = build_base_path (PLUS_EXPR, current_class_ptr, 873 subobject, 1); 874 expand_aggr_init_1 (subobject, NULL_TREE, 875 cp_build_indirect_ref (base_addr, RO_NULL, 876 tf_warning_or_error), 877 arguments, 878 LOOKUP_NORMAL, 879 tf_warning_or_error); 880 expand_cleanup_for_base (subobject, NULL_TREE); 881 } 882 883 mem_inits = TREE_CHAIN (mem_inits); 884 } 885 in_base_initializer = 0; 886 887 /* Initialize the vptrs. */ 888 initialize_vtbl_ptrs (current_class_ptr); 889 890 /* Initialize the data members. */ 891 while (mem_inits) 892 { 893 perform_member_init (TREE_PURPOSE (mem_inits), 894 TREE_VALUE (mem_inits)); 895 mem_inits = TREE_CHAIN (mem_inits); 896 } 897 } 898 899 /* Returns the address of the vtable (i.e., the value that should be 900 assigned to the vptr) for BINFO. */ 901 902 static tree 903 build_vtbl_address (tree binfo) 904 { 905 tree binfo_for = binfo; 906 tree vtbl; 907 908 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo)) 909 /* If this is a virtual primary base, then the vtable we want to store 910 is that for the base this is being used as the primary base of. We 911 can't simply skip the initialization, because we may be expanding the 912 inits of a subobject constructor where the virtual base layout 913 can be different. */ 914 while (BINFO_PRIMARY_P (binfo_for)) 915 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for); 916 917 /* Figure out what vtable BINFO's vtable is based on, and mark it as 918 used. */ 919 vtbl = get_vtbl_decl_for_binfo (binfo_for); 920 TREE_USED (vtbl) = 1; 921 922 /* Now compute the address to use when initializing the vptr. */ 923 vtbl = unshare_expr (BINFO_VTABLE (binfo_for)); 924 if (TREE_CODE (vtbl) == VAR_DECL) 925 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl); 926 927 return vtbl; 928 } 929 930 /* This code sets up the virtual function tables appropriate for 931 the pointer DECL. It is a one-ply initialization. 932 933 BINFO is the exact type that DECL is supposed to be. In 934 multiple inheritance, this might mean "C's A" if C : A, B. */ 935 936 static void 937 expand_virtual_init (tree binfo, tree decl) 938 { 939 tree vtbl, vtbl_ptr; 940 tree vtt_index; 941 942 /* Compute the initializer for vptr. */ 943 vtbl = build_vtbl_address (binfo); 944 945 /* We may get this vptr from a VTT, if this is a subobject 946 constructor or subobject destructor. */ 947 vtt_index = BINFO_VPTR_INDEX (binfo); 948 if (vtt_index) 949 { 950 tree vtbl2; 951 tree vtt_parm; 952 953 /* Compute the value to use, when there's a VTT. */ 954 vtt_parm = current_vtt_parm; 955 vtbl2 = build2 (POINTER_PLUS_EXPR, 956 TREE_TYPE (vtt_parm), 957 vtt_parm, 958 vtt_index); 959 vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error); 960 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2); 961 962 /* The actual initializer is the VTT value only in the subobject 963 constructor. In maybe_clone_body we'll substitute NULL for 964 the vtt_parm in the case of the non-subobject constructor. */ 965 vtbl = build3 (COND_EXPR, 966 TREE_TYPE (vtbl), 967 build2 (EQ_EXPR, boolean_type_node, 968 current_in_charge_parm, integer_zero_node), 969 vtbl2, 970 vtbl); 971 } 972 973 /* Compute the location of the vtpr. */ 974 vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL, 975 tf_warning_or_error), 976 TREE_TYPE (binfo)); 977 gcc_assert (vtbl_ptr != error_mark_node); 978 979 /* Assign the vtable to the vptr. */ 980 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0); 981 finish_expr_stmt (cp_build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl, 982 tf_warning_or_error)); 983 } 984 985 /* If an exception is thrown in a constructor, those base classes already 986 constructed must be destroyed. This function creates the cleanup 987 for BINFO, which has just been constructed. If FLAG is non-NULL, 988 it is a DECL which is nonzero when this base needs to be 989 destroyed. */ 990 991 static void 992 expand_cleanup_for_base (tree binfo, tree flag) 993 { 994 tree expr; 995 996 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo))) 997 return; 998 999 /* Call the destructor. */ 1000 expr = build_special_member_call (current_class_ref, 1001 base_dtor_identifier, 1002 NULL, 1003 binfo, 1004 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL, 1005 tf_warning_or_error); 1006 if (flag) 1007 expr = fold_build3_loc (input_location, 1008 COND_EXPR, void_type_node, 1009 c_common_truthvalue_conversion (input_location, flag), 1010 expr, integer_zero_node); 1011 1012 finish_eh_cleanup (expr); 1013 } 1014 1015 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its 1016 constructor. */ 1017 1018 static void 1019 construct_virtual_base (tree vbase, tree arguments) 1020 { 1021 tree inner_if_stmt; 1022 tree exp; 1023 tree flag; 1024 1025 /* If there are virtual base classes with destructors, we need to 1026 emit cleanups to destroy them if an exception is thrown during 1027 the construction process. These exception regions (i.e., the 1028 period during which the cleanups must occur) begin from the time 1029 the construction is complete to the end of the function. If we 1030 create a conditional block in which to initialize the 1031 base-classes, then the cleanup region for the virtual base begins 1032 inside a block, and ends outside of that block. This situation 1033 confuses the sjlj exception-handling code. Therefore, we do not 1034 create a single conditional block, but one for each 1035 initialization. (That way the cleanup regions always begin 1036 in the outer block.) We trust the back end to figure out 1037 that the FLAG will not change across initializations, and 1038 avoid doing multiple tests. */ 1039 flag = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); 1040 inner_if_stmt = begin_if_stmt (); 1041 finish_if_stmt_cond (flag, inner_if_stmt); 1042 1043 /* Compute the location of the virtual base. If we're 1044 constructing virtual bases, then we must be the most derived 1045 class. Therefore, we don't have to look up the virtual base; 1046 we already know where it is. */ 1047 exp = convert_to_base_statically (current_class_ref, vbase); 1048 1049 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments, 1050 LOOKUP_COMPLAIN, tf_warning_or_error); 1051 finish_then_clause (inner_if_stmt); 1052 finish_if_stmt (inner_if_stmt); 1053 1054 expand_cleanup_for_base (vbase, flag); 1055 } 1056 1057 /* Find the context in which this FIELD can be initialized. */ 1058 1059 static tree 1060 initializing_context (tree field) 1061 { 1062 tree t = DECL_CONTEXT (field); 1063 1064 /* Anonymous union members can be initialized in the first enclosing 1065 non-anonymous union context. */ 1066 while (t && ANON_AGGR_TYPE_P (t)) 1067 t = TYPE_CONTEXT (t); 1068 return t; 1069 } 1070 1071 /* Function to give error message if member initialization specification 1072 is erroneous. FIELD is the member we decided to initialize. 1073 TYPE is the type for which the initialization is being performed. 1074 FIELD must be a member of TYPE. 1075 1076 MEMBER_NAME is the name of the member. */ 1077 1078 static int 1079 member_init_ok_or_else (tree field, tree type, tree member_name) 1080 { 1081 if (field == error_mark_node) 1082 return 0; 1083 if (!field) 1084 { 1085 error ("class %qT does not have any field named %qD", type, 1086 member_name); 1087 return 0; 1088 } 1089 if (TREE_CODE (field) == VAR_DECL) 1090 { 1091 error ("%q#D is a static data member; it can only be " 1092 "initialized at its definition", 1093 field); 1094 return 0; 1095 } 1096 if (TREE_CODE (field) != FIELD_DECL) 1097 { 1098 error ("%q#D is not a non-static data member of %qT", 1099 field, type); 1100 return 0; 1101 } 1102 if (initializing_context (field) != type) 1103 { 1104 error ("class %qT does not have any field named %qD", type, 1105 member_name); 1106 return 0; 1107 } 1108 1109 return 1; 1110 } 1111 1112 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it 1113 is a _TYPE node or TYPE_DECL which names a base for that type. 1114 Check the validity of NAME, and return either the base _TYPE, base 1115 binfo, or the FIELD_DECL of the member. If NAME is invalid, return 1116 NULL_TREE and issue a diagnostic. 1117 1118 An old style unnamed direct single base construction is permitted, 1119 where NAME is NULL. */ 1120 1121 tree 1122 expand_member_init (tree name) 1123 { 1124 tree basetype; 1125 tree field; 1126 1127 if (!current_class_ref) 1128 return NULL_TREE; 1129 1130 if (!name) 1131 { 1132 /* This is an obsolete unnamed base class initializer. The 1133 parser will already have warned about its use. */ 1134 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type))) 1135 { 1136 case 0: 1137 error ("unnamed initializer for %qT, which has no base classes", 1138 current_class_type); 1139 return NULL_TREE; 1140 case 1: 1141 basetype = BINFO_TYPE 1142 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0)); 1143 break; 1144 default: 1145 error ("unnamed initializer for %qT, which uses multiple inheritance", 1146 current_class_type); 1147 return NULL_TREE; 1148 } 1149 } 1150 else if (TYPE_P (name)) 1151 { 1152 basetype = TYPE_MAIN_VARIANT (name); 1153 name = TYPE_NAME (name); 1154 } 1155 else if (TREE_CODE (name) == TYPE_DECL) 1156 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name)); 1157 else 1158 basetype = NULL_TREE; 1159 1160 if (basetype) 1161 { 1162 tree class_binfo; 1163 tree direct_binfo; 1164 tree virtual_binfo; 1165 int i; 1166 1167 if (current_template_parms) 1168 return basetype; 1169 1170 class_binfo = TYPE_BINFO (current_class_type); 1171 direct_binfo = NULL_TREE; 1172 virtual_binfo = NULL_TREE; 1173 1174 /* Look for a direct base. */ 1175 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i) 1176 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype)) 1177 break; 1178 1179 /* Look for a virtual base -- unless the direct base is itself 1180 virtual. */ 1181 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo)) 1182 virtual_binfo = binfo_for_vbase (basetype, current_class_type); 1183 1184 /* [class.base.init] 1185 1186 If a mem-initializer-id is ambiguous because it designates 1187 both a direct non-virtual base class and an inherited virtual 1188 base class, the mem-initializer is ill-formed. */ 1189 if (direct_binfo && virtual_binfo) 1190 { 1191 error ("%qD is both a direct base and an indirect virtual base", 1192 basetype); 1193 return NULL_TREE; 1194 } 1195 1196 if (!direct_binfo && !virtual_binfo) 1197 { 1198 if (CLASSTYPE_VBASECLASSES (current_class_type)) 1199 error ("type %qT is not a direct or virtual base of %qT", 1200 basetype, current_class_type); 1201 else 1202 error ("type %qT is not a direct base of %qT", 1203 basetype, current_class_type); 1204 return NULL_TREE; 1205 } 1206 1207 return direct_binfo ? direct_binfo : virtual_binfo; 1208 } 1209 else 1210 { 1211 if (TREE_CODE (name) == IDENTIFIER_NODE) 1212 field = lookup_field (current_class_type, name, 1, false); 1213 else 1214 field = name; 1215 1216 if (member_init_ok_or_else (field, current_class_type, name)) 1217 return field; 1218 } 1219 1220 return NULL_TREE; 1221 } 1222 1223 /* This is like `expand_member_init', only it stores one aggregate 1224 value into another. 1225 1226 INIT comes in two flavors: it is either a value which 1227 is to be stored in EXP, or it is a parameter list 1228 to go to a constructor, which will operate on EXP. 1229 If INIT is not a parameter list for a constructor, then set 1230 LOOKUP_ONLYCONVERTING. 1231 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of 1232 the initializer, if FLAGS is 0, then it is the (init) form. 1233 If `init' is a CONSTRUCTOR, then we emit a warning message, 1234 explaining that such initializations are invalid. 1235 1236 If INIT resolves to a CALL_EXPR which happens to return 1237 something of the type we are looking for, then we know 1238 that we can safely use that call to perform the 1239 initialization. 1240 1241 The virtual function table pointer cannot be set up here, because 1242 we do not really know its type. 1243 1244 This never calls operator=(). 1245 1246 When initializing, nothing is CONST. 1247 1248 A default copy constructor may have to be used to perform the 1249 initialization. 1250 1251 A constructor or a conversion operator may have to be used to 1252 perform the initialization, but not both, as it would be ambiguous. */ 1253 1254 tree 1255 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain) 1256 { 1257 tree stmt_expr; 1258 tree compound_stmt; 1259 int destroy_temps; 1260 tree type = TREE_TYPE (exp); 1261 int was_const = TREE_READONLY (exp); 1262 int was_volatile = TREE_THIS_VOLATILE (exp); 1263 int is_global; 1264 1265 if (init == error_mark_node) 1266 return error_mark_node; 1267 1268 TREE_READONLY (exp) = 0; 1269 TREE_THIS_VOLATILE (exp) = 0; 1270 1271 if (init && TREE_CODE (init) != TREE_LIST 1272 && !(BRACE_ENCLOSED_INITIALIZER_P (init) 1273 && CONSTRUCTOR_IS_DIRECT_INIT (init))) 1274 flags |= LOOKUP_ONLYCONVERTING; 1275 1276 if (TREE_CODE (type) == ARRAY_TYPE) 1277 { 1278 tree itype; 1279 1280 /* An array may not be initialized use the parenthesized 1281 initialization form -- unless the initializer is "()". */ 1282 if (init && TREE_CODE (init) == TREE_LIST) 1283 { 1284 if (complain & tf_error) 1285 error ("bad array initializer"); 1286 return error_mark_node; 1287 } 1288 /* Must arrange to initialize each element of EXP 1289 from elements of INIT. */ 1290 itype = init ? TREE_TYPE (init) : NULL_TREE; 1291 if (cv_qualified_p (type)) 1292 TREE_TYPE (exp) = cv_unqualified (type); 1293 if (itype && cv_qualified_p (itype)) 1294 TREE_TYPE (init) = cv_unqualified (itype); 1295 stmt_expr = build_vec_init (exp, NULL_TREE, init, 1296 /*explicit_value_init_p=*/false, 1297 itype && same_type_p (TREE_TYPE (init), 1298 TREE_TYPE (exp)), 1299 complain); 1300 TREE_READONLY (exp) = was_const; 1301 TREE_THIS_VOLATILE (exp) = was_volatile; 1302 TREE_TYPE (exp) = type; 1303 if (init) 1304 TREE_TYPE (init) = itype; 1305 return stmt_expr; 1306 } 1307 1308 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL) 1309 /* Just know that we've seen something for this node. */ 1310 TREE_USED (exp) = 1; 1311 1312 is_global = begin_init_stmts (&stmt_expr, &compound_stmt); 1313 destroy_temps = stmts_are_full_exprs_p (); 1314 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 1315 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp, 1316 init, LOOKUP_NORMAL|flags, complain); 1317 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); 1318 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; 1319 TREE_READONLY (exp) = was_const; 1320 TREE_THIS_VOLATILE (exp) = was_volatile; 1321 1322 return stmt_expr; 1323 } 1324 1325 static void 1326 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags, 1327 tsubst_flags_t complain) 1328 { 1329 tree type = TREE_TYPE (exp); 1330 tree ctor_name; 1331 1332 /* It fails because there may not be a constructor which takes 1333 its own type as the first (or only parameter), but which does 1334 take other types via a conversion. So, if the thing initializing 1335 the expression is a unit element of type X, first try X(X&), 1336 followed by initialization by X. If neither of these work 1337 out, then look hard. */ 1338 tree rval; 1339 VEC(tree,gc) *parms; 1340 1341 if (init && TREE_CODE (init) != TREE_LIST 1342 && (flags & LOOKUP_ONLYCONVERTING)) 1343 { 1344 /* Base subobjects should only get direct-initialization. */ 1345 gcc_assert (true_exp == exp); 1346 1347 if (flags & DIRECT_BIND) 1348 /* Do nothing. We hit this in two cases: Reference initialization, 1349 where we aren't initializing a real variable, so we don't want 1350 to run a new constructor; and catching an exception, where we 1351 have already built up the constructor call so we could wrap it 1352 in an exception region. */; 1353 else if (BRACE_ENCLOSED_INITIALIZER_P (init) 1354 && CP_AGGREGATE_TYPE_P (type)) 1355 { 1356 /* A brace-enclosed initializer for an aggregate. */ 1357 init = digest_init (type, init); 1358 } 1359 else 1360 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags); 1361 1362 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR) 1363 /* We need to protect the initialization of a catch parm with a 1364 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR 1365 around the TARGET_EXPR for the copy constructor. See 1366 initialize_handler_parm. */ 1367 { 1368 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp, 1369 TREE_OPERAND (init, 0)); 1370 TREE_TYPE (init) = void_type_node; 1371 } 1372 else 1373 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init); 1374 TREE_SIDE_EFFECTS (init) = 1; 1375 finish_expr_stmt (init); 1376 return; 1377 } 1378 1379 if (init == NULL_TREE) 1380 parms = NULL; 1381 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init)) 1382 { 1383 parms = make_tree_vector (); 1384 for (; init != NULL_TREE; init = TREE_CHAIN (init)) 1385 VEC_safe_push (tree, gc, parms, TREE_VALUE (init)); 1386 } 1387 else 1388 parms = make_tree_vector_single (init); 1389 1390 if (true_exp == exp) 1391 ctor_name = complete_ctor_identifier; 1392 else 1393 ctor_name = base_ctor_identifier; 1394 1395 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags, 1396 complain); 1397 1398 if (parms != NULL) 1399 release_tree_vector (parms); 1400 1401 if (TREE_SIDE_EFFECTS (rval)) 1402 finish_expr_stmt (convert_to_void (rval, NULL, complain)); 1403 } 1404 1405 /* This function is responsible for initializing EXP with INIT 1406 (if any). 1407 1408 BINFO is the binfo of the type for who we are performing the 1409 initialization. For example, if W is a virtual base class of A and B, 1410 and C : A, B. 1411 If we are initializing B, then W must contain B's W vtable, whereas 1412 were we initializing C, W must contain C's W vtable. 1413 1414 TRUE_EXP is nonzero if it is the true expression being initialized. 1415 In this case, it may be EXP, or may just contain EXP. The reason we 1416 need this is because if EXP is a base element of TRUE_EXP, we 1417 don't necessarily know by looking at EXP where its virtual 1418 baseclass fields should really be pointing. But we do know 1419 from TRUE_EXP. In constructors, we don't know anything about 1420 the value being initialized. 1421 1422 FLAGS is just passed to `build_new_method_call'. See that function 1423 for its description. */ 1424 1425 static void 1426 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags, 1427 tsubst_flags_t complain) 1428 { 1429 tree type = TREE_TYPE (exp); 1430 1431 gcc_assert (init != error_mark_node && type != error_mark_node); 1432 gcc_assert (building_stmt_tree ()); 1433 1434 /* Use a function returning the desired type to initialize EXP for us. 1435 If the function is a constructor, and its first argument is 1436 NULL_TREE, know that it was meant for us--just slide exp on 1437 in and expand the constructor. Constructors now come 1438 as TARGET_EXPRs. */ 1439 1440 if (init && TREE_CODE (exp) == VAR_DECL 1441 && COMPOUND_LITERAL_P (init)) 1442 { 1443 /* If store_init_value returns NULL_TREE, the INIT has been 1444 recorded as the DECL_INITIAL for EXP. That means there's 1445 nothing more we have to do. */ 1446 init = store_init_value (exp, init, flags); 1447 if (init) 1448 finish_expr_stmt (init); 1449 return; 1450 } 1451 1452 /* If an explicit -- but empty -- initializer list was present, 1453 that's value-initialization. */ 1454 if (init == void_type_node) 1455 { 1456 /* If there's a user-provided constructor, we just call that. */ 1457 if (type_has_user_provided_constructor (type)) 1458 /* Fall through. */; 1459 /* If there isn't, but we still need to call the constructor, 1460 zero out the object first. */ 1461 else if (TYPE_NEEDS_CONSTRUCTING (type)) 1462 { 1463 tree field_size = NULL_TREE; 1464 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type) 1465 /* Don't clobber already initialized virtual bases. */ 1466 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type)); 1467 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false, 1468 field_size); 1469 init = build2 (INIT_EXPR, type, exp, init); 1470 finish_expr_stmt (init); 1471 /* And then call the constructor. */ 1472 } 1473 /* If we don't need to mess with the constructor at all, 1474 then just zero out the object and we're done. */ 1475 else 1476 { 1477 init = build2 (INIT_EXPR, type, exp, build_value_init_noctor (type)); 1478 finish_expr_stmt (init); 1479 return; 1480 } 1481 init = NULL_TREE; 1482 } 1483 1484 /* We know that expand_default_init can handle everything we want 1485 at this point. */ 1486 expand_default_init (binfo, true_exp, exp, init, flags, complain); 1487 } 1488 1489 /* Report an error if TYPE is not a user-defined, class type. If 1490 OR_ELSE is nonzero, give an error message. */ 1491 1492 int 1493 is_class_type (tree type, int or_else) 1494 { 1495 if (type == error_mark_node) 1496 return 0; 1497 1498 if (! CLASS_TYPE_P (type)) 1499 { 1500 if (or_else) 1501 error ("%qT is not a class type", type); 1502 return 0; 1503 } 1504 return 1; 1505 } 1506 1507 tree 1508 get_type_value (tree name) 1509 { 1510 if (name == error_mark_node) 1511 return NULL_TREE; 1512 1513 if (IDENTIFIER_HAS_TYPE_VALUE (name)) 1514 return IDENTIFIER_TYPE_VALUE (name); 1515 else 1516 return NULL_TREE; 1517 } 1518 1519 /* Build a reference to a member of an aggregate. This is not a C++ 1520 `&', but really something which can have its address taken, and 1521 then act as a pointer to member, for example TYPE :: FIELD can have 1522 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if 1523 this expression is the operand of "&". 1524 1525 @@ Prints out lousy diagnostics for operator <typename> 1526 @@ fields. 1527 1528 @@ This function should be rewritten and placed in search.c. */ 1529 1530 tree 1531 build_offset_ref (tree type, tree member, bool address_p) 1532 { 1533 tree decl; 1534 tree basebinfo = NULL_TREE; 1535 1536 /* class templates can come in as TEMPLATE_DECLs here. */ 1537 if (TREE_CODE (member) == TEMPLATE_DECL) 1538 return member; 1539 1540 if (dependent_type_p (type) || type_dependent_expression_p (member)) 1541 return build_qualified_name (NULL_TREE, type, member, 1542 /*template_p=*/false); 1543 1544 gcc_assert (TYPE_P (type)); 1545 if (! is_class_type (type, 1)) 1546 return error_mark_node; 1547 1548 gcc_assert (DECL_P (member) || BASELINK_P (member)); 1549 /* Callers should call mark_used before this point. */ 1550 gcc_assert (!DECL_P (member) || TREE_USED (member)); 1551 1552 if (!COMPLETE_TYPE_P (complete_type (type)) 1553 && !TYPE_BEING_DEFINED (type)) 1554 { 1555 error ("incomplete type %qT does not have member %qD", type, member); 1556 return error_mark_node; 1557 } 1558 1559 /* Entities other than non-static members need no further 1560 processing. */ 1561 if (TREE_CODE (member) == TYPE_DECL) 1562 return member; 1563 if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL) 1564 return convert_from_reference (member); 1565 1566 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member)) 1567 { 1568 error ("invalid pointer to bit-field %qD", member); 1569 return error_mark_node; 1570 } 1571 1572 /* Set up BASEBINFO for member lookup. */ 1573 decl = maybe_dummy_object (type, &basebinfo); 1574 1575 /* A lot of this logic is now handled in lookup_member. */ 1576 if (BASELINK_P (member)) 1577 { 1578 /* Go from the TREE_BASELINK to the member function info. */ 1579 tree t = BASELINK_FUNCTIONS (member); 1580 1581 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t)) 1582 { 1583 /* Get rid of a potential OVERLOAD around it. */ 1584 t = OVL_CURRENT (t); 1585 1586 /* Unique functions are handled easily. */ 1587 1588 /* For non-static member of base class, we need a special rule 1589 for access checking [class.protected]: 1590 1591 If the access is to form a pointer to member, the 1592 nested-name-specifier shall name the derived class 1593 (or any class derived from that class). */ 1594 if (address_p && DECL_P (t) 1595 && DECL_NONSTATIC_MEMBER_P (t)) 1596 perform_or_defer_access_check (TYPE_BINFO (type), t, t); 1597 else 1598 perform_or_defer_access_check (basebinfo, t, t); 1599 1600 if (DECL_STATIC_FUNCTION_P (t)) 1601 return t; 1602 member = t; 1603 } 1604 else 1605 TREE_TYPE (member) = unknown_type_node; 1606 } 1607 else if (address_p && TREE_CODE (member) == FIELD_DECL) 1608 /* We need additional test besides the one in 1609 check_accessibility_of_qualified_id in case it is 1610 a pointer to non-static member. */ 1611 perform_or_defer_access_check (TYPE_BINFO (type), member, member); 1612 1613 if (!address_p) 1614 { 1615 /* If MEMBER is non-static, then the program has fallen afoul of 1616 [expr.prim]: 1617 1618 An id-expression that denotes a nonstatic data member or 1619 nonstatic member function of a class can only be used: 1620 1621 -- as part of a class member access (_expr.ref_) in which the 1622 object-expression refers to the member's class or a class 1623 derived from that class, or 1624 1625 -- to form a pointer to member (_expr.unary.op_), or 1626 1627 -- in the body of a nonstatic member function of that class or 1628 of a class derived from that class (_class.mfct.nonstatic_), or 1629 1630 -- in a mem-initializer for a constructor for that class or for 1631 a class derived from that class (_class.base.init_). */ 1632 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member)) 1633 { 1634 /* Build a representation of the qualified name suitable 1635 for use as the operand to "&" -- even though the "&" is 1636 not actually present. */ 1637 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member); 1638 /* In Microsoft mode, treat a non-static member function as if 1639 it were a pointer-to-member. */ 1640 if (flag_ms_extensions) 1641 { 1642 PTRMEM_OK_P (member) = 1; 1643 return cp_build_unary_op (ADDR_EXPR, member, 0, 1644 tf_warning_or_error); 1645 } 1646 error ("invalid use of non-static member function %qD", 1647 TREE_OPERAND (member, 1)); 1648 return error_mark_node; 1649 } 1650 else if (TREE_CODE (member) == FIELD_DECL) 1651 { 1652 error ("invalid use of non-static data member %qD", member); 1653 return error_mark_node; 1654 } 1655 return member; 1656 } 1657 1658 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member); 1659 PTRMEM_OK_P (member) = 1; 1660 return member; 1661 } 1662 1663 /* If DECL is a scalar enumeration constant or variable with a 1664 constant initializer, return the initializer (or, its initializers, 1665 recursively); otherwise, return DECL. If INTEGRAL_P, the 1666 initializer is only returned if DECL is an integral 1667 constant-expression. */ 1668 1669 static tree 1670 constant_value_1 (tree decl, bool integral_p) 1671 { 1672 while (TREE_CODE (decl) == CONST_DECL 1673 || (integral_p 1674 ? DECL_INTEGRAL_CONSTANT_VAR_P (decl) 1675 : (TREE_CODE (decl) == VAR_DECL 1676 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl))))) 1677 { 1678 tree init; 1679 /* Static data members in template classes may have 1680 non-dependent initializers. References to such non-static 1681 data members are not value-dependent, so we must retrieve the 1682 initializer here. The DECL_INITIAL will have the right type, 1683 but will not have been folded because that would prevent us 1684 from performing all appropriate semantic checks at 1685 instantiation time. */ 1686 if (DECL_CLASS_SCOPE_P (decl) 1687 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) 1688 && uses_template_parms (CLASSTYPE_TI_ARGS 1689 (DECL_CONTEXT (decl)))) 1690 { 1691 ++processing_template_decl; 1692 init = fold_non_dependent_expr (DECL_INITIAL (decl)); 1693 --processing_template_decl; 1694 } 1695 else 1696 { 1697 /* If DECL is a static data member in a template 1698 specialization, we must instantiate it here. The 1699 initializer for the static data member is not processed 1700 until needed; we need it now. */ 1701 mark_used (decl); 1702 init = DECL_INITIAL (decl); 1703 } 1704 if (init == error_mark_node) 1705 return decl; 1706 /* Initializers in templates are generally expanded during 1707 instantiation, so before that for const int i(2) 1708 INIT is a TREE_LIST with the actual initializer as 1709 TREE_VALUE. */ 1710 if (processing_template_decl 1711 && init 1712 && TREE_CODE (init) == TREE_LIST 1713 && TREE_CHAIN (init) == NULL_TREE) 1714 init = TREE_VALUE (init); 1715 if (!init 1716 || !TREE_TYPE (init) 1717 || (integral_p 1718 ? !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (init)) 1719 : (!TREE_CONSTANT (init) 1720 /* Do not return an aggregate constant (of which 1721 string literals are a special case), as we do not 1722 want to make inadvertent copies of such entities, 1723 and we must be sure that their addresses are the 1724 same everywhere. */ 1725 || TREE_CODE (init) == CONSTRUCTOR 1726 || TREE_CODE (init) == STRING_CST))) 1727 break; 1728 decl = unshare_expr (init); 1729 } 1730 return decl; 1731 } 1732 1733 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by 1734 constant of integral or enumeration type, then return that value. 1735 These are those variables permitted in constant expressions by 1736 [5.19/1]. */ 1737 1738 tree 1739 integral_constant_value (tree decl) 1740 { 1741 return constant_value_1 (decl, /*integral_p=*/true); 1742 } 1743 1744 /* A more relaxed version of integral_constant_value, used by the 1745 common C/C++ code and by the C++ front end for optimization 1746 purposes. */ 1747 1748 tree 1749 decl_constant_value (tree decl) 1750 { 1751 return constant_value_1 (decl, 1752 /*integral_p=*/processing_template_decl); 1753 } 1754 1755 /* Common subroutines of build_new and build_vec_delete. */ 1756 1757 /* Call the global __builtin_delete to delete ADDR. */ 1758 1759 static tree 1760 build_builtin_delete_call (tree addr) 1761 { 1762 mark_used (global_delete_fndecl); 1763 return build_call_n (global_delete_fndecl, 1, addr); 1764 } 1765 1766 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is 1767 the type of the object being allocated; otherwise, it's just TYPE. 1768 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the 1769 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is 1770 a vector of arguments to be provided as arguments to a placement 1771 new operator. This routine performs no semantic checks; it just 1772 creates and returns a NEW_EXPR. */ 1773 1774 static tree 1775 build_raw_new_expr (VEC(tree,gc) *placement, tree type, tree nelts, 1776 VEC(tree,gc) *init, int use_global_new) 1777 { 1778 tree init_list; 1779 tree new_expr; 1780 1781 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR. 1782 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This 1783 permits us to distinguish the case of a missing initializer "new 1784 int" from an empty initializer "new int()". */ 1785 if (init == NULL) 1786 init_list = NULL_TREE; 1787 else if (VEC_empty (tree, init)) 1788 init_list = void_zero_node; 1789 else 1790 init_list = build_tree_list_vec (init); 1791 1792 new_expr = build4 (NEW_EXPR, build_pointer_type (type), 1793 build_tree_list_vec (placement), type, nelts, 1794 init_list); 1795 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new; 1796 TREE_SIDE_EFFECTS (new_expr) = 1; 1797 1798 return new_expr; 1799 } 1800 1801 /* Generate code for a new-expression, including calling the "operator 1802 new" function, initializing the object, and, if an exception occurs 1803 during construction, cleaning up. The arguments are as for 1804 build_raw_new_expr. This may change PLACEMENT and INIT. */ 1805 1806 static tree 1807 build_new_1 (VEC(tree,gc) **placement, tree type, tree nelts, 1808 VEC(tree,gc) **init, bool globally_qualified_p, 1809 tsubst_flags_t complain) 1810 { 1811 tree size, rval; 1812 /* True iff this is a call to "operator new[]" instead of just 1813 "operator new". */ 1814 bool array_p = false; 1815 /* If ARRAY_P is true, the element type of the array. This is never 1816 an ARRAY_TYPE; for something like "new int[3][4]", the 1817 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as 1818 TYPE. */ 1819 tree elt_type; 1820 /* The type of the new-expression. (This type is always a pointer 1821 type.) */ 1822 tree pointer_type; 1823 tree non_const_pointer_type; 1824 tree outer_nelts = NULL_TREE; 1825 tree alloc_call, alloc_expr; 1826 /* The address returned by the call to "operator new". This node is 1827 a VAR_DECL and is therefore reusable. */ 1828 tree alloc_node; 1829 tree alloc_fn; 1830 tree cookie_expr, init_expr; 1831 int nothrow, check_new; 1832 int use_java_new = 0; 1833 /* If non-NULL, the number of extra bytes to allocate at the 1834 beginning of the storage allocated for an array-new expression in 1835 order to store the number of elements. */ 1836 tree cookie_size = NULL_TREE; 1837 tree placement_first; 1838 tree placement_expr = NULL_TREE; 1839 /* True if the function we are calling is a placement allocation 1840 function. */ 1841 bool placement_allocation_fn_p; 1842 /* True if the storage must be initialized, either by a constructor 1843 or due to an explicit new-initializer. */ 1844 bool is_initialized; 1845 /* The address of the thing allocated, not including any cookie. In 1846 particular, if an array cookie is in use, DATA_ADDR is the 1847 address of the first array element. This node is a VAR_DECL, and 1848 is therefore reusable. */ 1849 tree data_addr; 1850 tree init_preeval_expr = NULL_TREE; 1851 1852 if (nelts) 1853 { 1854 outer_nelts = nelts; 1855 array_p = true; 1856 } 1857 else if (TREE_CODE (type) == ARRAY_TYPE) 1858 { 1859 array_p = true; 1860 nelts = array_type_nelts_top (type); 1861 outer_nelts = nelts; 1862 type = TREE_TYPE (type); 1863 } 1864 1865 /* If our base type is an array, then make sure we know how many elements 1866 it has. */ 1867 for (elt_type = type; 1868 TREE_CODE (elt_type) == ARRAY_TYPE; 1869 elt_type = TREE_TYPE (elt_type)) 1870 nelts = cp_build_binary_op (input_location, 1871 MULT_EXPR, nelts, 1872 array_type_nelts_top (elt_type), 1873 complain); 1874 1875 if (TREE_CODE (elt_type) == VOID_TYPE) 1876 { 1877 if (complain & tf_error) 1878 error ("invalid type %<void%> for new"); 1879 return error_mark_node; 1880 } 1881 1882 if (abstract_virtuals_error (NULL_TREE, elt_type)) 1883 return error_mark_node; 1884 1885 is_initialized = (TYPE_NEEDS_CONSTRUCTING (elt_type) || *init != NULL); 1886 1887 if (CP_TYPE_CONST_P (elt_type) && *init == NULL 1888 && !type_has_user_provided_default_constructor (elt_type)) 1889 { 1890 if (complain & tf_error) 1891 error ("uninitialized const in %<new%> of %q#T", elt_type); 1892 return error_mark_node; 1893 } 1894 1895 size = size_in_bytes (elt_type); 1896 if (array_p) 1897 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts)); 1898 1899 alloc_fn = NULL_TREE; 1900 1901 /* If PLACEMENT is a single simple pointer type not passed by 1902 reference, prepare to capture it in a temporary variable. Do 1903 this now, since PLACEMENT will change in the calls below. */ 1904 placement_first = NULL_TREE; 1905 if (VEC_length (tree, *placement) == 1 1906 && (TREE_CODE (TREE_TYPE (VEC_index (tree, *placement, 0))) 1907 == POINTER_TYPE)) 1908 placement_first = VEC_index (tree, *placement, 0); 1909 1910 /* Allocate the object. */ 1911 if (VEC_empty (tree, *placement) && TYPE_FOR_JAVA (elt_type)) 1912 { 1913 tree class_addr; 1914 tree class_decl = build_java_class_ref (elt_type); 1915 static const char alloc_name[] = "_Jv_AllocObject"; 1916 1917 if (class_decl == error_mark_node) 1918 return error_mark_node; 1919 1920 use_java_new = 1; 1921 if (!get_global_value_if_present (get_identifier (alloc_name), 1922 &alloc_fn)) 1923 { 1924 if (complain & tf_error) 1925 error ("call to Java constructor with %qs undefined", alloc_name); 1926 return error_mark_node; 1927 } 1928 else if (really_overloaded_fn (alloc_fn)) 1929 { 1930 if (complain & tf_error) 1931 error ("%qD should never be overloaded", alloc_fn); 1932 return error_mark_node; 1933 } 1934 alloc_fn = OVL_CURRENT (alloc_fn); 1935 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl); 1936 alloc_call = (cp_build_function_call 1937 (alloc_fn, 1938 build_tree_list (NULL_TREE, class_addr), 1939 complain)); 1940 } 1941 else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type)) 1942 { 1943 error ("Java class %q#T object allocated using placement new", elt_type); 1944 return error_mark_node; 1945 } 1946 else 1947 { 1948 tree fnname; 1949 tree fns; 1950 1951 fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR); 1952 1953 if (!globally_qualified_p 1954 && CLASS_TYPE_P (elt_type) 1955 && (array_p 1956 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type) 1957 : TYPE_HAS_NEW_OPERATOR (elt_type))) 1958 { 1959 /* Use a class-specific operator new. */ 1960 /* If a cookie is required, add some extra space. */ 1961 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)) 1962 { 1963 cookie_size = targetm.cxx.get_cookie_size (elt_type); 1964 size = size_binop (PLUS_EXPR, size, cookie_size); 1965 } 1966 /* Create the argument list. */ 1967 VEC_safe_insert (tree, gc, *placement, 0, size); 1968 /* Do name-lookup to find the appropriate operator. */ 1969 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2); 1970 if (fns == NULL_TREE) 1971 { 1972 if (complain & tf_error) 1973 error ("no suitable %qD found in class %qT", fnname, elt_type); 1974 return error_mark_node; 1975 } 1976 if (TREE_CODE (fns) == TREE_LIST) 1977 { 1978 if (complain & tf_error) 1979 { 1980 error ("request for member %qD is ambiguous", fnname); 1981 print_candidates (fns); 1982 } 1983 return error_mark_node; 1984 } 1985 alloc_call = build_new_method_call (build_dummy_object (elt_type), 1986 fns, placement, 1987 /*conversion_path=*/NULL_TREE, 1988 LOOKUP_NORMAL, 1989 &alloc_fn, 1990 complain); 1991 } 1992 else 1993 { 1994 /* Use a global operator new. */ 1995 /* See if a cookie might be required. */ 1996 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)) 1997 cookie_size = targetm.cxx.get_cookie_size (elt_type); 1998 else 1999 cookie_size = NULL_TREE; 2000 2001 alloc_call = build_operator_new_call (fnname, placement, 2002 &size, &cookie_size, 2003 &alloc_fn); 2004 } 2005 } 2006 2007 if (alloc_call == error_mark_node) 2008 return error_mark_node; 2009 2010 gcc_assert (alloc_fn != NULL_TREE); 2011 2012 /* If we found a simple case of PLACEMENT_EXPR above, then copy it 2013 into a temporary variable. */ 2014 if (!processing_template_decl 2015 && placement_first != NULL_TREE 2016 && TREE_CODE (alloc_call) == CALL_EXPR 2017 && call_expr_nargs (alloc_call) == 2 2018 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE 2019 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))) == POINTER_TYPE) 2020 { 2021 tree placement_arg = CALL_EXPR_ARG (alloc_call, 1); 2022 2023 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))) 2024 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))) 2025 { 2026 placement_expr = get_target_expr (placement_first); 2027 CALL_EXPR_ARG (alloc_call, 1) 2028 = convert (TREE_TYPE (placement_arg), placement_expr); 2029 } 2030 } 2031 2032 /* In the simple case, we can stop now. */ 2033 pointer_type = build_pointer_type (type); 2034 if (!cookie_size && !is_initialized) 2035 return build_nop (pointer_type, alloc_call); 2036 2037 /* Store the result of the allocation call in a variable so that we can 2038 use it more than once. */ 2039 alloc_expr = get_target_expr (alloc_call); 2040 alloc_node = TARGET_EXPR_SLOT (alloc_expr); 2041 2042 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */ 2043 while (TREE_CODE (alloc_call) == COMPOUND_EXPR) 2044 alloc_call = TREE_OPERAND (alloc_call, 1); 2045 2046 /* Now, check to see if this function is actually a placement 2047 allocation function. This can happen even when PLACEMENT is NULL 2048 because we might have something like: 2049 2050 struct S { void* operator new (size_t, int i = 0); }; 2051 2052 A call to `new S' will get this allocation function, even though 2053 there is no explicit placement argument. If there is more than 2054 one argument, or there are variable arguments, then this is a 2055 placement allocation function. */ 2056 placement_allocation_fn_p 2057 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1 2058 || varargs_function_p (alloc_fn)); 2059 2060 /* Preevaluate the placement args so that we don't reevaluate them for a 2061 placement delete. */ 2062 if (placement_allocation_fn_p) 2063 { 2064 tree inits; 2065 stabilize_call (alloc_call, &inits); 2066 if (inits) 2067 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits, 2068 alloc_expr); 2069 } 2070 2071 /* unless an allocation function is declared with an empty excep- 2072 tion-specification (_except.spec_), throw(), it indicates failure to 2073 allocate storage by throwing a bad_alloc exception (clause _except_, 2074 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo- 2075 cation function is declared with an empty exception-specification, 2076 throw(), it returns null to indicate failure to allocate storage and a 2077 non-null pointer otherwise. 2078 2079 So check for a null exception spec on the op new we just called. */ 2080 2081 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn)); 2082 check_new = (flag_check_new || nothrow) && ! use_java_new; 2083 2084 if (cookie_size) 2085 { 2086 tree cookie; 2087 tree cookie_ptr; 2088 tree size_ptr_type; 2089 2090 /* Adjust so we're pointing to the start of the object. */ 2091 data_addr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (alloc_node), 2092 alloc_node, cookie_size); 2093 2094 /* Store the number of bytes allocated so that we can know how 2095 many elements to destroy later. We use the last sizeof 2096 (size_t) bytes to store the number of elements. */ 2097 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype)); 2098 cookie_ptr = fold_build2_loc (input_location, 2099 POINTER_PLUS_EXPR, TREE_TYPE (alloc_node), 2100 alloc_node, cookie_ptr); 2101 size_ptr_type = build_pointer_type (sizetype); 2102 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr); 2103 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain); 2104 2105 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts); 2106 2107 if (targetm.cxx.cookie_has_size ()) 2108 { 2109 /* Also store the element size. */ 2110 cookie_ptr = build2 (POINTER_PLUS_EXPR, size_ptr_type, cookie_ptr, 2111 fold_build1_loc (input_location, 2112 NEGATE_EXPR, sizetype, 2113 size_in_bytes (sizetype))); 2114 2115 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain); 2116 cookie = build2 (MODIFY_EXPR, sizetype, cookie, 2117 size_in_bytes (elt_type)); 2118 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr), 2119 cookie, cookie_expr); 2120 } 2121 } 2122 else 2123 { 2124 cookie_expr = NULL_TREE; 2125 data_addr = alloc_node; 2126 } 2127 2128 /* Now use a pointer to the type we've actually allocated. */ 2129 2130 /* But we want to operate on a non-const version to start with, 2131 since we'll be modifying the elements. */ 2132 non_const_pointer_type = build_pointer_type 2133 (cp_build_qualified_type (type, TYPE_QUALS (type) & ~TYPE_QUAL_CONST)); 2134 2135 data_addr = fold_convert (non_const_pointer_type, data_addr); 2136 /* Any further uses of alloc_node will want this type, too. */ 2137 alloc_node = fold_convert (non_const_pointer_type, alloc_node); 2138 2139 /* Now initialize the allocated object. Note that we preevaluate the 2140 initialization expression, apart from the actual constructor call or 2141 assignment--we do this because we want to delay the allocation as long 2142 as possible in order to minimize the size of the exception region for 2143 placement delete. */ 2144 if (is_initialized) 2145 { 2146 bool stable; 2147 bool explicit_value_init_p = false; 2148 2149 if (*init != NULL && VEC_empty (tree, *init)) 2150 { 2151 *init = NULL; 2152 explicit_value_init_p = true; 2153 } 2154 2155 if (array_p) 2156 { 2157 tree vecinit = NULL_TREE; 2158 if (*init && VEC_length (tree, *init) == 1 2159 && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *init, 0)) 2160 && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *init, 0))) 2161 { 2162 tree arraytype, domain; 2163 vecinit = VEC_index (tree, *init, 0); 2164 if (TREE_CONSTANT (nelts)) 2165 domain = compute_array_index_type (NULL_TREE, nelts); 2166 else 2167 { 2168 domain = NULL_TREE; 2169 if (CONSTRUCTOR_NELTS (vecinit) > 0) 2170 warning (0, "non-constant array size in new, unable to " 2171 "verify length of initializer-list"); 2172 } 2173 arraytype = build_cplus_array_type (type, domain); 2174 vecinit = digest_init (arraytype, vecinit); 2175 } 2176 else if (*init) 2177 { 2178 if (complain & tf_error) 2179 permerror (input_location, "ISO C++ forbids initialization in array new"); 2180 else 2181 return error_mark_node; 2182 vecinit = build_tree_list_vec (*init); 2183 } 2184 init_expr 2185 = build_vec_init (data_addr, 2186 cp_build_binary_op (input_location, 2187 MINUS_EXPR, outer_nelts, 2188 integer_one_node, 2189 complain), 2190 vecinit, 2191 explicit_value_init_p, 2192 /*from_array=*/0, 2193 complain); 2194 2195 /* An array initialization is stable because the initialization 2196 of each element is a full-expression, so the temporaries don't 2197 leak out. */ 2198 stable = true; 2199 } 2200 else 2201 { 2202 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain); 2203 2204 if (TYPE_NEEDS_CONSTRUCTING (type) 2205 && (!explicit_value_init_p || processing_template_decl)) 2206 { 2207 init_expr = build_special_member_call (init_expr, 2208 complete_ctor_identifier, 2209 init, elt_type, 2210 LOOKUP_NORMAL, 2211 complain); 2212 } 2213 else if (explicit_value_init_p) 2214 { 2215 if (processing_template_decl) 2216 /* Don't worry about it, we'll handle this properly at 2217 instantiation time. */; 2218 else 2219 /* Something like `new int()'. */ 2220 init_expr = build2 (INIT_EXPR, type, 2221 init_expr, build_value_init (type)); 2222 } 2223 else 2224 { 2225 tree ie; 2226 2227 /* We are processing something like `new int (10)', which 2228 means allocate an int, and initialize it with 10. */ 2229 2230 ie = build_x_compound_expr_from_vec (*init, "new initializer"); 2231 init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie, 2232 complain); 2233 } 2234 stable = stabilize_init (init_expr, &init_preeval_expr); 2235 } 2236 2237 if (init_expr == error_mark_node) 2238 return error_mark_node; 2239 2240 /* If any part of the object initialization terminates by throwing an 2241 exception and a suitable deallocation function can be found, the 2242 deallocation function is called to free the memory in which the 2243 object was being constructed, after which the exception continues 2244 to propagate in the context of the new-expression. If no 2245 unambiguous matching deallocation function can be found, 2246 propagating the exception does not cause the object's memory to be 2247 freed. */ 2248 if (flag_exceptions && ! use_java_new) 2249 { 2250 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR; 2251 tree cleanup; 2252 2253 /* The Standard is unclear here, but the right thing to do 2254 is to use the same method for finding deallocation 2255 functions that we use for finding allocation functions. */ 2256 cleanup = (build_op_delete_call 2257 (dcode, 2258 alloc_node, 2259 size, 2260 globally_qualified_p, 2261 placement_allocation_fn_p ? alloc_call : NULL_TREE, 2262 alloc_fn)); 2263 2264 if (!cleanup) 2265 /* We're done. */; 2266 else if (stable) 2267 /* This is much simpler if we were able to preevaluate all of 2268 the arguments to the constructor call. */ 2269 { 2270 /* CLEANUP is compiler-generated, so no diagnostics. */ 2271 TREE_NO_WARNING (cleanup) = true; 2272 init_expr = build2 (TRY_CATCH_EXPR, void_type_node, 2273 init_expr, cleanup); 2274 /* Likewise, this try-catch is compiler-generated. */ 2275 TREE_NO_WARNING (init_expr) = true; 2276 } 2277 else 2278 /* Ack! First we allocate the memory. Then we set our sentry 2279 variable to true, and expand a cleanup that deletes the 2280 memory if sentry is true. Then we run the constructor, and 2281 finally clear the sentry. 2282 2283 We need to do this because we allocate the space first, so 2284 if there are any temporaries with cleanups in the 2285 constructor args and we weren't able to preevaluate them, we 2286 need this EH region to extend until end of full-expression 2287 to preserve nesting. */ 2288 { 2289 tree end, sentry, begin; 2290 2291 begin = get_target_expr (boolean_true_node); 2292 CLEANUP_EH_ONLY (begin) = 1; 2293 2294 sentry = TARGET_EXPR_SLOT (begin); 2295 2296 /* CLEANUP is compiler-generated, so no diagnostics. */ 2297 TREE_NO_WARNING (cleanup) = true; 2298 2299 TARGET_EXPR_CLEANUP (begin) 2300 = build3 (COND_EXPR, void_type_node, sentry, 2301 cleanup, void_zero_node); 2302 2303 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry), 2304 sentry, boolean_false_node); 2305 2306 init_expr 2307 = build2 (COMPOUND_EXPR, void_type_node, begin, 2308 build2 (COMPOUND_EXPR, void_type_node, init_expr, 2309 end)); 2310 /* Likewise, this is compiler-generated. */ 2311 TREE_NO_WARNING (init_expr) = true; 2312 } 2313 } 2314 } 2315 else 2316 init_expr = NULL_TREE; 2317 2318 /* Now build up the return value in reverse order. */ 2319 2320 rval = data_addr; 2321 2322 if (init_expr) 2323 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval); 2324 if (cookie_expr) 2325 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval); 2326 2327 if (rval == data_addr) 2328 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR 2329 and return the call (which doesn't need to be adjusted). */ 2330 rval = TARGET_EXPR_INITIAL (alloc_expr); 2331 else 2332 { 2333 if (check_new) 2334 { 2335 tree ifexp = cp_build_binary_op (input_location, 2336 NE_EXPR, alloc_node, 2337 integer_zero_node, 2338 complain); 2339 rval = build_conditional_expr (ifexp, rval, alloc_node, 2340 complain); 2341 } 2342 2343 /* Perform the allocation before anything else, so that ALLOC_NODE 2344 has been initialized before we start using it. */ 2345 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval); 2346 } 2347 2348 if (init_preeval_expr) 2349 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval); 2350 2351 /* A new-expression is never an lvalue. */ 2352 gcc_assert (!lvalue_p (rval)); 2353 2354 return convert (pointer_type, rval); 2355 } 2356 2357 /* Generate a representation for a C++ "new" expression. *PLACEMENT 2358 is a vector of placement-new arguments (or NULL if none). If NELTS 2359 is NULL, TYPE is the type of the storage to be allocated. If NELTS 2360 is not NULL, then this is an array-new allocation; TYPE is the type 2361 of the elements in the array and NELTS is the number of elements in 2362 the array. *INIT, if non-NULL, is the initializer for the new 2363 object, or an empty vector to indicate an initializer of "()". If 2364 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new" 2365 rather than just "new". This may change PLACEMENT and INIT. */ 2366 2367 tree 2368 build_new (VEC(tree,gc) **placement, tree type, tree nelts, 2369 VEC(tree,gc) **init, int use_global_new, tsubst_flags_t complain) 2370 { 2371 tree rval; 2372 VEC(tree,gc) *orig_placement = NULL; 2373 tree orig_nelts = NULL_TREE; 2374 VEC(tree,gc) *orig_init = NULL; 2375 2376 if (type == error_mark_node) 2377 return error_mark_node; 2378 2379 if (nelts == NULL_TREE && VEC_length (tree, *init) == 1) 2380 { 2381 tree auto_node = type_uses_auto (type); 2382 if (auto_node && describable_type (VEC_index (tree, *init, 0))) 2383 type = do_auto_deduction (type, VEC_index (tree, *init, 0), auto_node); 2384 } 2385 2386 if (processing_template_decl) 2387 { 2388 if (dependent_type_p (type) 2389 || any_type_dependent_arguments_p (*placement) 2390 || (nelts && type_dependent_expression_p (nelts)) 2391 || any_type_dependent_arguments_p (*init)) 2392 return build_raw_new_expr (*placement, type, nelts, *init, 2393 use_global_new); 2394 2395 orig_placement = make_tree_vector_copy (*placement); 2396 orig_nelts = nelts; 2397 orig_init = make_tree_vector_copy (*init); 2398 2399 make_args_non_dependent (*placement); 2400 if (nelts) 2401 nelts = build_non_dependent_expr (nelts); 2402 make_args_non_dependent (*init); 2403 } 2404 2405 if (nelts) 2406 { 2407 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false)) 2408 { 2409 if (complain & tf_error) 2410 permerror (input_location, "size in array new must have integral type"); 2411 else 2412 return error_mark_node; 2413 } 2414 nelts = cp_save_expr (cp_convert (sizetype, nelts)); 2415 } 2416 2417 /* ``A reference cannot be created by the new operator. A reference 2418 is not an object (8.2.2, 8.4.3), so a pointer to it could not be 2419 returned by new.'' ARM 5.3.3 */ 2420 if (TREE_CODE (type) == REFERENCE_TYPE) 2421 { 2422 if (complain & tf_error) 2423 error ("new cannot be applied to a reference type"); 2424 else 2425 return error_mark_node; 2426 type = TREE_TYPE (type); 2427 } 2428 2429 if (TREE_CODE (type) == FUNCTION_TYPE) 2430 { 2431 if (complain & tf_error) 2432 error ("new cannot be applied to a function type"); 2433 return error_mark_node; 2434 } 2435 2436 /* The type allocated must be complete. If the new-type-id was 2437 "T[N]" then we are just checking that "T" is complete here, but 2438 that is equivalent, since the value of "N" doesn't matter. */ 2439 if (!complete_type_or_else (type, NULL_TREE)) 2440 return error_mark_node; 2441 2442 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain); 2443 if (rval == error_mark_node) 2444 return error_mark_node; 2445 2446 if (processing_template_decl) 2447 { 2448 tree ret = build_raw_new_expr (orig_placement, type, orig_nelts, 2449 orig_init, use_global_new); 2450 release_tree_vector (orig_placement); 2451 release_tree_vector (orig_init); 2452 return ret; 2453 } 2454 2455 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */ 2456 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval); 2457 TREE_NO_WARNING (rval) = 1; 2458 2459 return rval; 2460 } 2461 2462 /* Given a Java class, return a decl for the corresponding java.lang.Class. */ 2463 2464 tree 2465 build_java_class_ref (tree type) 2466 { 2467 tree name = NULL_TREE, class_decl; 2468 static tree CL_suffix = NULL_TREE; 2469 if (CL_suffix == NULL_TREE) 2470 CL_suffix = get_identifier("class$"); 2471 if (jclass_node == NULL_TREE) 2472 { 2473 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass")); 2474 if (jclass_node == NULL_TREE) 2475 { 2476 error ("call to Java constructor, while %<jclass%> undefined"); 2477 return error_mark_node; 2478 } 2479 jclass_node = TREE_TYPE (jclass_node); 2480 } 2481 2482 /* Mangle the class$ field. */ 2483 { 2484 tree field; 2485 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 2486 if (DECL_NAME (field) == CL_suffix) 2487 { 2488 mangle_decl (field); 2489 name = DECL_ASSEMBLER_NAME (field); 2490 break; 2491 } 2492 if (!field) 2493 { 2494 error ("can't find %<class$%> in %qT", type); 2495 return error_mark_node; 2496 } 2497 } 2498 2499 class_decl = IDENTIFIER_GLOBAL_VALUE (name); 2500 if (class_decl == NULL_TREE) 2501 { 2502 class_decl = build_decl (input_location, 2503 VAR_DECL, name, TREE_TYPE (jclass_node)); 2504 TREE_STATIC (class_decl) = 1; 2505 DECL_EXTERNAL (class_decl) = 1; 2506 TREE_PUBLIC (class_decl) = 1; 2507 DECL_ARTIFICIAL (class_decl) = 1; 2508 DECL_IGNORED_P (class_decl) = 1; 2509 pushdecl_top_level (class_decl); 2510 make_decl_rtl (class_decl); 2511 } 2512 return class_decl; 2513 } 2514 2515 static tree 2516 build_vec_delete_1 (tree base, tree maxindex, tree type, 2517 special_function_kind auto_delete_vec, int use_global_delete) 2518 { 2519 tree virtual_size; 2520 tree ptype = build_pointer_type (type = complete_type (type)); 2521 tree size_exp = size_in_bytes (type); 2522 2523 /* Temporary variables used by the loop. */ 2524 tree tbase, tbase_init; 2525 2526 /* This is the body of the loop that implements the deletion of a 2527 single element, and moves temp variables to next elements. */ 2528 tree body; 2529 2530 /* This is the LOOP_EXPR that governs the deletion of the elements. */ 2531 tree loop = 0; 2532 2533 /* This is the thing that governs what to do after the loop has run. */ 2534 tree deallocate_expr = 0; 2535 2536 /* This is the BIND_EXPR which holds the outermost iterator of the 2537 loop. It is convenient to set this variable up and test it before 2538 executing any other code in the loop. 2539 This is also the containing expression returned by this function. */ 2540 tree controller = NULL_TREE; 2541 tree tmp; 2542 2543 /* We should only have 1-D arrays here. */ 2544 gcc_assert (TREE_CODE (type) != ARRAY_TYPE); 2545 2546 if (! MAYBE_CLASS_TYPE_P (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) 2547 goto no_destructor; 2548 2549 /* The below is short by the cookie size. */ 2550 virtual_size = size_binop (MULT_EXPR, size_exp, 2551 convert (sizetype, maxindex)); 2552 2553 tbase = create_temporary_var (ptype); 2554 tbase_init = cp_build_modify_expr (tbase, NOP_EXPR, 2555 fold_build2_loc (input_location, 2556 POINTER_PLUS_EXPR, ptype, 2557 fold_convert (ptype, base), 2558 virtual_size), 2559 tf_warning_or_error); 2560 controller = build3 (BIND_EXPR, void_type_node, tbase, 2561 NULL_TREE, NULL_TREE); 2562 TREE_SIDE_EFFECTS (controller) = 1; 2563 2564 body = build1 (EXIT_EXPR, void_type_node, 2565 build2 (EQ_EXPR, boolean_type_node, tbase, 2566 fold_convert (ptype, base))); 2567 tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp); 2568 body = build_compound_expr 2569 (input_location, 2570 body, cp_build_modify_expr (tbase, NOP_EXPR, 2571 build2 (POINTER_PLUS_EXPR, ptype, tbase, tmp), 2572 tf_warning_or_error)); 2573 body = build_compound_expr 2574 (input_location, 2575 body, build_delete (ptype, tbase, sfk_complete_destructor, 2576 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1)); 2577 2578 loop = build1 (LOOP_EXPR, void_type_node, body); 2579 loop = build_compound_expr (input_location, tbase_init, loop); 2580 2581 no_destructor: 2582 /* If the delete flag is one, or anything else with the low bit set, 2583 delete the storage. */ 2584 if (auto_delete_vec != sfk_base_destructor) 2585 { 2586 tree base_tbd; 2587 2588 /* The below is short by the cookie size. */ 2589 virtual_size = size_binop (MULT_EXPR, size_exp, 2590 convert (sizetype, maxindex)); 2591 2592 if (! TYPE_VEC_NEW_USES_COOKIE (type)) 2593 /* no header */ 2594 base_tbd = base; 2595 else 2596 { 2597 tree cookie_size; 2598 2599 cookie_size = targetm.cxx.get_cookie_size (type); 2600 base_tbd 2601 = cp_convert (ptype, 2602 cp_build_binary_op (input_location, 2603 MINUS_EXPR, 2604 cp_convert (string_type_node, 2605 base), 2606 cookie_size, 2607 tf_warning_or_error)); 2608 /* True size with header. */ 2609 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size); 2610 } 2611 2612 if (auto_delete_vec == sfk_deleting_destructor) 2613 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR, 2614 base_tbd, virtual_size, 2615 use_global_delete & 1, 2616 /*placement=*/NULL_TREE, 2617 /*alloc_fn=*/NULL_TREE); 2618 } 2619 2620 body = loop; 2621 if (!deallocate_expr) 2622 ; 2623 else if (!body) 2624 body = deallocate_expr; 2625 else 2626 body = build_compound_expr (input_location, body, deallocate_expr); 2627 2628 if (!body) 2629 body = integer_zero_node; 2630 2631 /* Outermost wrapper: If pointer is null, punt. */ 2632 body = fold_build3_loc (input_location, COND_EXPR, void_type_node, 2633 fold_build2_loc (input_location, 2634 NE_EXPR, boolean_type_node, base, 2635 convert (TREE_TYPE (base), 2636 integer_zero_node)), 2637 body, integer_zero_node); 2638 body = build1 (NOP_EXPR, void_type_node, body); 2639 2640 if (controller) 2641 { 2642 TREE_OPERAND (controller, 1) = body; 2643 body = controller; 2644 } 2645 2646 if (TREE_CODE (base) == SAVE_EXPR) 2647 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */ 2648 body = build2 (COMPOUND_EXPR, void_type_node, base, body); 2649 2650 return convert_to_void (body, /*implicit=*/NULL, tf_warning_or_error); 2651 } 2652 2653 /* Create an unnamed variable of the indicated TYPE. */ 2654 2655 tree 2656 create_temporary_var (tree type) 2657 { 2658 tree decl; 2659 2660 decl = build_decl (input_location, 2661 VAR_DECL, NULL_TREE, type); 2662 TREE_USED (decl) = 1; 2663 DECL_ARTIFICIAL (decl) = 1; 2664 DECL_IGNORED_P (decl) = 1; 2665 DECL_CONTEXT (decl) = current_function_decl; 2666 2667 return decl; 2668 } 2669 2670 /* Create a new temporary variable of the indicated TYPE, initialized 2671 to INIT. 2672 2673 It is not entered into current_binding_level, because that breaks 2674 things when it comes time to do final cleanups (which take place 2675 "outside" the binding contour of the function). */ 2676 2677 static tree 2678 get_temp_regvar (tree type, tree init) 2679 { 2680 tree decl; 2681 2682 decl = create_temporary_var (type); 2683 add_decl_expr (decl); 2684 2685 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init, 2686 tf_warning_or_error)); 2687 2688 return decl; 2689 } 2690 2691 /* `build_vec_init' returns tree structure that performs 2692 initialization of a vector of aggregate types. 2693 2694 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer 2695 to the first element, of POINTER_TYPE. 2696 MAXINDEX is the maximum index of the array (one less than the 2697 number of elements). It is only used if BASE is a pointer or 2698 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE. 2699 2700 INIT is the (possibly NULL) initializer. 2701 2702 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All 2703 elements in the array are value-initialized. 2704 2705 FROM_ARRAY is 0 if we should init everything with INIT 2706 (i.e., every element initialized from INIT). 2707 FROM_ARRAY is 1 if we should index into INIT in parallel 2708 with initialization of DECL. 2709 FROM_ARRAY is 2 if we should index into INIT in parallel, 2710 but use assignment instead of initialization. */ 2711 2712 tree 2713 build_vec_init (tree base, tree maxindex, tree init, 2714 bool explicit_value_init_p, 2715 int from_array, tsubst_flags_t complain) 2716 { 2717 tree rval; 2718 tree base2 = NULL_TREE; 2719 tree itype = NULL_TREE; 2720 tree iterator; 2721 /* The type of BASE. */ 2722 tree atype = TREE_TYPE (base); 2723 /* The type of an element in the array. */ 2724 tree type = TREE_TYPE (atype); 2725 /* The element type reached after removing all outer array 2726 types. */ 2727 tree inner_elt_type; 2728 /* The type of a pointer to an element in the array. */ 2729 tree ptype; 2730 tree stmt_expr; 2731 tree compound_stmt; 2732 int destroy_temps; 2733 tree try_block = NULL_TREE; 2734 int num_initialized_elts = 0; 2735 bool is_global; 2736 2737 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype)) 2738 maxindex = array_type_nelts (atype); 2739 2740 if (maxindex == NULL_TREE || maxindex == error_mark_node) 2741 return error_mark_node; 2742 2743 if (explicit_value_init_p) 2744 gcc_assert (!init); 2745 2746 inner_elt_type = strip_array_types (type); 2747 2748 /* Look through the TARGET_EXPR around a compound literal. */ 2749 if (init && TREE_CODE (init) == TARGET_EXPR 2750 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR 2751 && from_array != 2) 2752 init = TARGET_EXPR_INITIAL (init); 2753 2754 if (init 2755 && TREE_CODE (atype) == ARRAY_TYPE 2756 && (from_array == 2 2757 ? (!CLASS_TYPE_P (inner_elt_type) 2758 || !TYPE_HAS_COMPLEX_ASSIGN_REF (inner_elt_type)) 2759 : !TYPE_NEEDS_CONSTRUCTING (type)) 2760 && ((TREE_CODE (init) == CONSTRUCTOR 2761 /* Don't do this if the CONSTRUCTOR might contain something 2762 that might throw and require us to clean up. */ 2763 && (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)) 2764 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type))) 2765 || from_array)) 2766 { 2767 /* Do non-default initialization of trivial arrays resulting from 2768 brace-enclosed initializers. In this case, digest_init and 2769 store_constructor will handle the semantics for us. */ 2770 2771 stmt_expr = build2 (INIT_EXPR, atype, base, init); 2772 return stmt_expr; 2773 } 2774 2775 maxindex = cp_convert (ptrdiff_type_node, maxindex); 2776 if (TREE_CODE (atype) == ARRAY_TYPE) 2777 { 2778 ptype = build_pointer_type (type); 2779 base = cp_convert (ptype, decay_conversion (base)); 2780 } 2781 else 2782 ptype = atype; 2783 2784 /* The code we are generating looks like: 2785 ({ 2786 T* t1 = (T*) base; 2787 T* rval = t1; 2788 ptrdiff_t iterator = maxindex; 2789 try { 2790 for (; iterator != -1; --iterator) { 2791 ... initialize *t1 ... 2792 ++t1; 2793 } 2794 } catch (...) { 2795 ... destroy elements that were constructed ... 2796 } 2797 rval; 2798 }) 2799 2800 We can omit the try and catch blocks if we know that the 2801 initialization will never throw an exception, or if the array 2802 elements do not have destructors. We can omit the loop completely if 2803 the elements of the array do not have constructors. 2804 2805 We actually wrap the entire body of the above in a STMT_EXPR, for 2806 tidiness. 2807 2808 When copying from array to another, when the array elements have 2809 only trivial copy constructors, we should use __builtin_memcpy 2810 rather than generating a loop. That way, we could take advantage 2811 of whatever cleverness the back end has for dealing with copies 2812 of blocks of memory. */ 2813 2814 is_global = begin_init_stmts (&stmt_expr, &compound_stmt); 2815 destroy_temps = stmts_are_full_exprs_p (); 2816 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 2817 rval = get_temp_regvar (ptype, base); 2818 base = get_temp_regvar (ptype, rval); 2819 iterator = get_temp_regvar (ptrdiff_type_node, maxindex); 2820 2821 /* If initializing one array from another, initialize element by 2822 element. We rely upon the below calls to do the argument 2823 checking. Evaluate the initializer before entering the try block. */ 2824 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR) 2825 { 2826 base2 = decay_conversion (init); 2827 itype = TREE_TYPE (base2); 2828 base2 = get_temp_regvar (itype, base2); 2829 itype = TREE_TYPE (itype); 2830 } 2831 2832 /* Protect the entire array initialization so that we can destroy 2833 the partially constructed array if an exception is thrown. 2834 But don't do this if we're assigning. */ 2835 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 2836 && from_array != 2) 2837 { 2838 try_block = begin_try_block (); 2839 } 2840 2841 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR) 2842 { 2843 /* Do non-default initialization of non-trivial arrays resulting from 2844 brace-enclosed initializers. */ 2845 unsigned HOST_WIDE_INT idx; 2846 tree elt; 2847 from_array = 0; 2848 2849 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt) 2850 { 2851 tree baseref = build1 (INDIRECT_REF, type, base); 2852 2853 num_initialized_elts++; 2854 2855 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 2856 if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE) 2857 finish_expr_stmt (build_aggr_init (baseref, elt, 0, complain)); 2858 else 2859 finish_expr_stmt (cp_build_modify_expr (baseref, NOP_EXPR, 2860 elt, complain)); 2861 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 2862 2863 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0, 2864 complain)); 2865 finish_expr_stmt (cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0, 2866 complain)); 2867 } 2868 2869 /* Clear out INIT so that we don't get confused below. */ 2870 init = NULL_TREE; 2871 } 2872 else if (from_array) 2873 { 2874 if (init) 2875 /* OK, we set base2 above. */; 2876 else if (TYPE_LANG_SPECIFIC (type) 2877 && TYPE_NEEDS_CONSTRUCTING (type) 2878 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) 2879 { 2880 if (complain & tf_error) 2881 error ("initializer ends prematurely"); 2882 return error_mark_node; 2883 } 2884 } 2885 2886 /* Now, default-initialize any remaining elements. We don't need to 2887 do that if a) the type does not need constructing, or b) we've 2888 already initialized all the elements. 2889 2890 We do need to keep going if we're copying an array. */ 2891 2892 if (from_array 2893 || ((TYPE_NEEDS_CONSTRUCTING (type) || explicit_value_init_p) 2894 && ! (host_integerp (maxindex, 0) 2895 && (num_initialized_elts 2896 == tree_low_cst (maxindex, 0) + 1)))) 2897 { 2898 /* If the ITERATOR is equal to -1, then we don't have to loop; 2899 we've already initialized all the elements. */ 2900 tree for_stmt; 2901 tree elt_init; 2902 tree to; 2903 2904 for_stmt = begin_for_stmt (); 2905 finish_for_init_stmt (for_stmt); 2906 finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator, 2907 build_int_cst (TREE_TYPE (iterator), -1)), 2908 for_stmt); 2909 finish_for_expr (cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0, 2910 complain), 2911 for_stmt); 2912 2913 to = build1 (INDIRECT_REF, type, base); 2914 2915 if (from_array) 2916 { 2917 tree from; 2918 2919 if (base2) 2920 from = build1 (INDIRECT_REF, itype, base2); 2921 else 2922 from = NULL_TREE; 2923 2924 if (from_array == 2) 2925 elt_init = cp_build_modify_expr (to, NOP_EXPR, from, 2926 complain); 2927 else if (TYPE_NEEDS_CONSTRUCTING (type)) 2928 elt_init = build_aggr_init (to, from, 0, complain); 2929 else if (from) 2930 elt_init = cp_build_modify_expr (to, NOP_EXPR, from, 2931 complain); 2932 else 2933 gcc_unreachable (); 2934 } 2935 else if (TREE_CODE (type) == ARRAY_TYPE) 2936 { 2937 if (init != 0) 2938 sorry 2939 ("cannot initialize multi-dimensional array with initializer"); 2940 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base), 2941 0, 0, 2942 explicit_value_init_p, 2943 0, complain); 2944 } 2945 else if (explicit_value_init_p) 2946 elt_init = build2 (INIT_EXPR, type, to, 2947 build_value_init (type)); 2948 else 2949 { 2950 gcc_assert (TYPE_NEEDS_CONSTRUCTING (type)); 2951 elt_init = build_aggr_init (to, init, 0, complain); 2952 } 2953 2954 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 2955 finish_expr_stmt (elt_init); 2956 current_stmt_tree ()->stmts_are_full_exprs_p = 0; 2957 2958 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0, 2959 complain)); 2960 if (base2) 2961 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0, 2962 complain)); 2963 2964 finish_for_stmt (for_stmt); 2965 } 2966 2967 /* Make sure to cleanup any partially constructed elements. */ 2968 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 2969 && from_array != 2) 2970 { 2971 tree e; 2972 tree m = cp_build_binary_op (input_location, 2973 MINUS_EXPR, maxindex, iterator, 2974 complain); 2975 2976 /* Flatten multi-dimensional array since build_vec_delete only 2977 expects one-dimensional array. */ 2978 if (TREE_CODE (type) == ARRAY_TYPE) 2979 m = cp_build_binary_op (input_location, 2980 MULT_EXPR, m, 2981 array_type_nelts_total (type), 2982 complain); 2983 2984 finish_cleanup_try_block (try_block); 2985 e = build_vec_delete_1 (rval, m, 2986 inner_elt_type, sfk_base_destructor, 2987 /*use_global_delete=*/0); 2988 finish_cleanup (e, try_block); 2989 } 2990 2991 /* The value of the array initialization is the array itself, RVAL 2992 is a pointer to the first element. */ 2993 finish_stmt_expr_expr (rval, stmt_expr); 2994 2995 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); 2996 2997 /* Now make the result have the correct type. */ 2998 if (TREE_CODE (atype) == ARRAY_TYPE) 2999 { 3000 atype = build_pointer_type (atype); 3001 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr); 3002 stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain); 3003 TREE_NO_WARNING (stmt_expr) = 1; 3004 } 3005 3006 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; 3007 return stmt_expr; 3008 } 3009 3010 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for 3011 build_delete. */ 3012 3013 static tree 3014 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags) 3015 { 3016 tree name; 3017 tree fn; 3018 switch (dtor_kind) 3019 { 3020 case sfk_complete_destructor: 3021 name = complete_dtor_identifier; 3022 break; 3023 3024 case sfk_base_destructor: 3025 name = base_dtor_identifier; 3026 break; 3027 3028 case sfk_deleting_destructor: 3029 name = deleting_dtor_identifier; 3030 break; 3031 3032 default: 3033 gcc_unreachable (); 3034 } 3035 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2); 3036 return build_new_method_call (exp, fn, 3037 /*args=*/NULL, 3038 /*conversion_path=*/NULL_TREE, 3039 flags, 3040 /*fn_p=*/NULL, 3041 tf_warning_or_error); 3042 } 3043 3044 /* Generate a call to a destructor. TYPE is the type to cast ADDR to. 3045 ADDR is an expression which yields the store to be destroyed. 3046 AUTO_DELETE is the name of the destructor to call, i.e., either 3047 sfk_complete_destructor, sfk_base_destructor, or 3048 sfk_deleting_destructor. 3049 3050 FLAGS is the logical disjunction of zero or more LOOKUP_ 3051 flags. See cp-tree.h for more info. */ 3052 3053 tree 3054 build_delete (tree type, tree addr, special_function_kind auto_delete, 3055 int flags, int use_global_delete) 3056 { 3057 tree expr; 3058 3059 if (addr == error_mark_node) 3060 return error_mark_node; 3061 3062 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type 3063 set to `error_mark_node' before it gets properly cleaned up. */ 3064 if (type == error_mark_node) 3065 return error_mark_node; 3066 3067 type = TYPE_MAIN_VARIANT (type); 3068 3069 if (TREE_CODE (type) == POINTER_TYPE) 3070 { 3071 bool complete_p = true; 3072 3073 type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 3074 if (TREE_CODE (type) == ARRAY_TYPE) 3075 goto handle_array; 3076 3077 /* We don't want to warn about delete of void*, only other 3078 incomplete types. Deleting other incomplete types 3079 invokes undefined behavior, but it is not ill-formed, so 3080 compile to something that would even do The Right Thing 3081 (TM) should the type have a trivial dtor and no delete 3082 operator. */ 3083 if (!VOID_TYPE_P (type)) 3084 { 3085 complete_type (type); 3086 if (!COMPLETE_TYPE_P (type)) 3087 { 3088 if (warning (0, "possible problem detected in invocation of " 3089 "delete operator:")) 3090 { 3091 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING); 3092 inform (input_location, "neither the destructor nor the class-specific " 3093 "operator delete will be called, even if they are " 3094 "declared when the class is defined."); 3095 } 3096 complete_p = false; 3097 } 3098 } 3099 if (VOID_TYPE_P (type) || !complete_p || !MAYBE_CLASS_TYPE_P (type)) 3100 /* Call the builtin operator delete. */ 3101 return build_builtin_delete_call (addr); 3102 if (TREE_SIDE_EFFECTS (addr)) 3103 addr = save_expr (addr); 3104 3105 /* Throw away const and volatile on target type of addr. */ 3106 addr = convert_force (build_pointer_type (type), addr, 0); 3107 } 3108 else if (TREE_CODE (type) == ARRAY_TYPE) 3109 { 3110 handle_array: 3111 3112 if (TYPE_DOMAIN (type) == NULL_TREE) 3113 { 3114 error ("unknown array size in delete"); 3115 return error_mark_node; 3116 } 3117 return build_vec_delete (addr, array_type_nelts (type), 3118 auto_delete, use_global_delete); 3119 } 3120 else 3121 { 3122 /* Don't check PROTECT here; leave that decision to the 3123 destructor. If the destructor is accessible, call it, 3124 else report error. */ 3125 addr = cp_build_unary_op (ADDR_EXPR, addr, 0, tf_warning_or_error); 3126 if (TREE_SIDE_EFFECTS (addr)) 3127 addr = save_expr (addr); 3128 3129 addr = convert_force (build_pointer_type (type), addr, 0); 3130 } 3131 3132 gcc_assert (MAYBE_CLASS_TYPE_P (type)); 3133 3134 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) 3135 { 3136 if (auto_delete != sfk_deleting_destructor) 3137 return void_zero_node; 3138 3139 return build_op_delete_call (DELETE_EXPR, addr, 3140 cxx_sizeof_nowarn (type), 3141 use_global_delete, 3142 /*placement=*/NULL_TREE, 3143 /*alloc_fn=*/NULL_TREE); 3144 } 3145 else 3146 { 3147 tree head = NULL_TREE; 3148 tree do_delete = NULL_TREE; 3149 tree ifexp; 3150 3151 if (CLASSTYPE_LAZY_DESTRUCTOR (type)) 3152 lazily_declare_fn (sfk_destructor, type); 3153 3154 /* For `::delete x', we must not use the deleting destructor 3155 since then we would not be sure to get the global `operator 3156 delete'. */ 3157 if (use_global_delete && auto_delete == sfk_deleting_destructor) 3158 { 3159 /* We will use ADDR multiple times so we must save it. */ 3160 addr = save_expr (addr); 3161 head = get_target_expr (build_headof (addr)); 3162 /* Delete the object. */ 3163 do_delete = build_builtin_delete_call (head); 3164 /* Otherwise, treat this like a complete object destructor 3165 call. */ 3166 auto_delete = sfk_complete_destructor; 3167 } 3168 /* If the destructor is non-virtual, there is no deleting 3169 variant. Instead, we must explicitly call the appropriate 3170 `operator delete' here. */ 3171 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type)) 3172 && auto_delete == sfk_deleting_destructor) 3173 { 3174 /* We will use ADDR multiple times so we must save it. */ 3175 addr = save_expr (addr); 3176 /* Build the call. */ 3177 do_delete = build_op_delete_call (DELETE_EXPR, 3178 addr, 3179 cxx_sizeof_nowarn (type), 3180 /*global_p=*/false, 3181 /*placement=*/NULL_TREE, 3182 /*alloc_fn=*/NULL_TREE); 3183 /* Call the complete object destructor. */ 3184 auto_delete = sfk_complete_destructor; 3185 } 3186 else if (auto_delete == sfk_deleting_destructor 3187 && TYPE_GETS_REG_DELETE (type)) 3188 { 3189 /* Make sure we have access to the member op delete, even though 3190 we'll actually be calling it from the destructor. */ 3191 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type), 3192 /*global_p=*/false, 3193 /*placement=*/NULL_TREE, 3194 /*alloc_fn=*/NULL_TREE); 3195 } 3196 3197 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, 3198 tf_warning_or_error), 3199 auto_delete, flags); 3200 if (do_delete) 3201 expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete); 3202 3203 /* We need to calculate this before the dtor changes the vptr. */ 3204 if (head) 3205 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr); 3206 3207 if (flags & LOOKUP_DESTRUCTOR) 3208 /* Explicit destructor call; don't check for null pointer. */ 3209 ifexp = integer_one_node; 3210 else 3211 /* Handle deleting a null pointer. */ 3212 ifexp = fold (cp_build_binary_op (input_location, 3213 NE_EXPR, addr, integer_zero_node, 3214 tf_warning_or_error)); 3215 3216 if (ifexp != integer_one_node) 3217 expr = build3 (COND_EXPR, void_type_node, 3218 ifexp, expr, void_zero_node); 3219 3220 return expr; 3221 } 3222 } 3223 3224 /* At the beginning of a destructor, push cleanups that will call the 3225 destructors for our base classes and members. 3226 3227 Called from begin_destructor_body. */ 3228 3229 void 3230 push_base_cleanups (void) 3231 { 3232 tree binfo, base_binfo; 3233 int i; 3234 tree member; 3235 tree expr; 3236 VEC(tree,gc) *vbases; 3237 3238 /* Run destructors for all virtual baseclasses. */ 3239 if (CLASSTYPE_VBASECLASSES (current_class_type)) 3240 { 3241 tree cond = (condition_conversion 3242 (build2 (BIT_AND_EXPR, integer_type_node, 3243 current_in_charge_parm, 3244 integer_two_node))); 3245 3246 /* The CLASSTYPE_VBASECLASSES vector is in initialization 3247 order, which is also the right order for pushing cleanups. */ 3248 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0; 3249 VEC_iterate (tree, vbases, i, base_binfo); i++) 3250 { 3251 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))) 3252 { 3253 expr = build_special_member_call (current_class_ref, 3254 base_dtor_identifier, 3255 NULL, 3256 base_binfo, 3257 (LOOKUP_NORMAL 3258 | LOOKUP_NONVIRTUAL), 3259 tf_warning_or_error); 3260 expr = build3 (COND_EXPR, void_type_node, cond, 3261 expr, void_zero_node); 3262 finish_decl_cleanup (NULL_TREE, expr); 3263 } 3264 } 3265 } 3266 3267 /* Take care of the remaining baseclasses. */ 3268 for (binfo = TYPE_BINFO (current_class_type), i = 0; 3269 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 3270 { 3271 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)) 3272 || BINFO_VIRTUAL_P (base_binfo)) 3273 continue; 3274 3275 expr = build_special_member_call (current_class_ref, 3276 base_dtor_identifier, 3277 NULL, base_binfo, 3278 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL, 3279 tf_warning_or_error); 3280 finish_decl_cleanup (NULL_TREE, expr); 3281 } 3282 3283 for (member = TYPE_FIELDS (current_class_type); member; 3284 member = TREE_CHAIN (member)) 3285 { 3286 if (TREE_TYPE (member) == error_mark_node 3287 || TREE_CODE (member) != FIELD_DECL 3288 || DECL_ARTIFICIAL (member)) 3289 continue; 3290 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member))) 3291 { 3292 tree this_member = (build_class_member_access_expr 3293 (current_class_ref, member, 3294 /*access_path=*/NULL_TREE, 3295 /*preserve_reference=*/false, 3296 tf_warning_or_error)); 3297 tree this_type = TREE_TYPE (member); 3298 expr = build_delete (this_type, this_member, 3299 sfk_complete_destructor, 3300 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 3301 0); 3302 finish_decl_cleanup (NULL_TREE, expr); 3303 } 3304 } 3305 } 3306 3307 /* Build a C++ vector delete expression. 3308 MAXINDEX is the number of elements to be deleted. 3309 ELT_SIZE is the nominal size of each element in the vector. 3310 BASE is the expression that should yield the store to be deleted. 3311 This function expands (or synthesizes) these calls itself. 3312 AUTO_DELETE_VEC says whether the container (vector) should be deallocated. 3313 3314 This also calls delete for virtual baseclasses of elements of the vector. 3315 3316 Update: MAXINDEX is no longer needed. The size can be extracted from the 3317 start of the vector for pointers, and from the type for arrays. We still 3318 use MAXINDEX for arrays because it happens to already have one of the 3319 values we'd have to extract. (We could use MAXINDEX with pointers to 3320 confirm the size, and trap if the numbers differ; not clear that it'd 3321 be worth bothering.) */ 3322 3323 tree 3324 build_vec_delete (tree base, tree maxindex, 3325 special_function_kind auto_delete_vec, int use_global_delete) 3326 { 3327 tree type; 3328 tree rval; 3329 tree base_init = NULL_TREE; 3330 3331 type = TREE_TYPE (base); 3332 3333 if (TREE_CODE (type) == POINTER_TYPE) 3334 { 3335 /* Step back one from start of vector, and read dimension. */ 3336 tree cookie_addr; 3337 tree size_ptr_type = build_pointer_type (sizetype); 3338 3339 if (TREE_SIDE_EFFECTS (base)) 3340 { 3341 base_init = get_target_expr (base); 3342 base = TARGET_EXPR_SLOT (base_init); 3343 } 3344 type = strip_array_types (TREE_TYPE (type)); 3345 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR, 3346 sizetype, TYPE_SIZE_UNIT (sizetype)); 3347 cookie_addr = build2 (POINTER_PLUS_EXPR, 3348 size_ptr_type, 3349 fold_convert (size_ptr_type, base), 3350 cookie_addr); 3351 maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, tf_warning_or_error); 3352 } 3353 else if (TREE_CODE (type) == ARRAY_TYPE) 3354 { 3355 /* Get the total number of things in the array, maxindex is a 3356 bad name. */ 3357 maxindex = array_type_nelts_total (type); 3358 type = strip_array_types (type); 3359 base = cp_build_unary_op (ADDR_EXPR, base, 1, tf_warning_or_error); 3360 if (TREE_SIDE_EFFECTS (base)) 3361 { 3362 base_init = get_target_expr (base); 3363 base = TARGET_EXPR_SLOT (base_init); 3364 } 3365 } 3366 else 3367 { 3368 if (base != error_mark_node) 3369 error ("type to vector delete is neither pointer or array type"); 3370 return error_mark_node; 3371 } 3372 3373 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec, 3374 use_global_delete); 3375 if (base_init) 3376 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval); 3377 3378 return rval; 3379 } 3380