1/* Handle initialization things in C++. 2 Copyright (C) 1987, 89, 92-98, 1999 Free Software Foundation, Inc. 3 Contributed by Michael Tiemann (tiemann@cygnus.com) 4 5This file is part of GNU CC. 6 7GNU CC is free software; you can redistribute it and/or modify 8it under the terms of the GNU General Public License as published by 9the Free Software Foundation; either version 2, or (at your option) 10any later version. 11 12GNU CC is distributed in the hope that it will be useful, 13but WITHOUT ANY WARRANTY; without even the implied warranty of 14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15GNU General Public License for more details. 16 17You should have received a copy of the GNU General Public License 18along with GNU CC; see the file COPYING. If not, write to 19the Free Software Foundation, 59 Temple Place - Suite 330, 20Boston, MA 02111-1307, USA. */ 21 22/* High-level class interface. */ 23 24#include "config.h" 25#include "system.h" 26#include "tree.h" 27#include "rtl.h" 28#include "cp-tree.h" 29#include "flags.h" 30#include "output.h" 31#include "except.h" 32#include "expr.h" 33#include "toplev.h" 34 35/* In C++, structures with well-defined constructors are initialized by 36 those constructors, unasked. CURRENT_BASE_INIT_LIST 37 holds a list of stmts for a BASE_INIT term in the grammar. 38 This list has one element for each base class which must be 39 initialized. The list elements are [basename, init], with 40 type basetype. This allows the possibly anachronistic form 41 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)" 42 where each successive term can be handed down the constructor 43 line. Perhaps this was not intended. */ 44tree current_base_init_list, current_member_init_list; 45 46static void expand_aggr_vbase_init_1 PROTO((tree, tree, tree, tree)); 47static void construct_virtual_bases PROTO((tree, tree, tree, tree, tree)); 48static void expand_aggr_init_1 PROTO((tree, tree, tree, tree, int)); 49static void expand_default_init PROTO((tree, tree, tree, tree, int)); 50static tree build_vec_delete_1 PROTO((tree, tree, tree, tree, tree, 51 int)); 52static void perform_member_init PROTO((tree, tree, tree, int)); 53static void sort_base_init PROTO((tree, tree *, tree *)); 54static tree build_builtin_delete_call PROTO((tree)); 55static int member_init_ok_or_else PROTO((tree, tree, const char *)); 56static void expand_virtual_init PROTO((tree, tree)); 57static tree sort_member_init PROTO((tree)); 58static tree initializing_context PROTO((tree)); 59static void expand_vec_init_try_block PROTO((tree)); 60static void expand_vec_init_catch_clause PROTO((tree, tree, tree, tree)); 61static tree build_java_class_ref PROTO((tree)); 62static void expand_cleanup_for_base PROTO((tree, tree, tree)); 63static int pvbasecount PROTO((tree, int)); 64 65/* Cache the identifier nodes for the magic field of a new cookie. */ 66static tree nc_nelts_field_id; 67 68static tree minus_one; 69 70/* Set up local variable for this file. MUST BE CALLED AFTER 71 INIT_DECL_PROCESSING. */ 72 73static tree BI_header_type, BI_header_size; 74 75void init_init_processing () 76{ 77 tree fields[1]; 78 79 minus_one = build_int_2 (-1, -1); 80 81 /* Define the structure that holds header information for 82 arrays allocated via operator new. */ 83 BI_header_type = make_lang_type (RECORD_TYPE); 84 nc_nelts_field_id = get_identifier ("nelts"); 85 fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype); 86 finish_builtin_type (BI_header_type, "__new_cookie", fields, 87 0, double_type_node); 88 BI_header_size = size_in_bytes (BI_header_type); 89} 90 91/* Subroutine of emit_base_init. For BINFO, initialize all the 92 virtual function table pointers, except those that come from 93 virtual base classes. Initialize binfo's vtable pointer, if 94 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual 95 function table pointers in all bases have been initialized already, 96 probably because their constructors have just be run. ADDR is the 97 pointer to the object whos vtables we are going to initialize. 98 99 REAL_BINFO is usually the same as BINFO, except when addr is not of 100 pointer to the type of the real derived type that we want to 101 initialize for. This is the case when addr is a pointer to a sub 102 object of a complete object, and we only want to do part of the 103 complete object's initialization of vtable pointers. This is done 104 for all virtual table pointers in virtual base classes. REAL_BINFO 105 is used to find the BINFO_VTABLE that we initialize with. BINFO is 106 used for conversions of addr to subobjects. 107 108 BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo). 109 110 Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE 111 (addr))). */ 112 113void 114expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr) 115 tree real_binfo, binfo, addr; 116 int init_self, can_elide; 117{ 118 tree real_binfos = BINFO_BASETYPES (real_binfo); 119 tree binfos = BINFO_BASETYPES (binfo); 120 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0; 121 122 for (i = 0; i < n_baselinks; i++) 123 { 124 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i); 125 tree base_binfo = TREE_VEC_ELT (binfos, i); 126 int is_not_base_vtable 127 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo)); 128 if (! TREE_VIA_VIRTUAL (real_base_binfo)) 129 expand_direct_vtbls_init (real_base_binfo, base_binfo, 130 is_not_base_vtable, can_elide, addr); 131 } 132#if 0 133 /* Before turning this on, make sure it is correct. */ 134 if (can_elide && ! BINFO_MODIFIED (binfo)) 135 return; 136#endif 137 /* Should we use something besides CLASSTYPE_VFIELDS? */ 138 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo))) 139 { 140 tree base_ptr = convert_pointer_to_real (binfo, addr); 141 expand_virtual_init (real_binfo, base_ptr); 142 } 143} 144 145/* 348 - 351 */ 146/* Subroutine of emit_base_init. */ 147 148static void 149perform_member_init (member, name, init, explicit) 150 tree member, name, init; 151 int explicit; 152{ 153 tree decl; 154 tree type = TREE_TYPE (member); 155 156 expand_start_target_temps (); 157 158 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit); 159 160 /* Deal with this here, as we will get confused if we try to call the 161 assignment op for an anonymous union. This can happen in a 162 synthesized copy constructor. */ 163 if (ANON_AGGR_TYPE_P (type)) 164 { 165 init = build (INIT_EXPR, type, decl, TREE_VALUE (init)); 166 TREE_SIDE_EFFECTS (init) = 1; 167 expand_expr_stmt (init); 168 } 169 else if (TYPE_NEEDS_CONSTRUCTING (type) 170 || (init && TYPE_HAS_CONSTRUCTOR (type))) 171 { 172 /* Since `init' is already a TREE_LIST on the current_member_init_list, 173 only build it into one if we aren't already a list. */ 174 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST) 175 init = build_expr_list (NULL_TREE, init); 176 177 if (explicit 178 && TREE_CODE (type) == ARRAY_TYPE 179 && init != NULL_TREE 180 && TREE_CHAIN (init) == NULL_TREE 181 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE) 182 { 183 /* Initialization of one array from another. */ 184 expand_vec_init (TREE_OPERAND (decl, 1), decl, 185 array_type_nelts (type), TREE_VALUE (init), 1); 186 } 187 else 188 expand_aggr_init (decl, init, 0); 189 } 190 else 191 { 192 if (init == NULL_TREE) 193 { 194 if (explicit) 195 { 196 /* default-initialization. */ 197 if (AGGREGATE_TYPE_P (type)) 198 init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); 199 else if (TREE_CODE (type) == REFERENCE_TYPE) 200 { 201 cp_error ("default-initialization of `%#D', which has reference type", 202 member); 203 init = error_mark_node; 204 } 205 else 206 init = integer_zero_node; 207 } 208 /* member traversal: note it leaves init NULL */ 209 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE) 210 cp_pedwarn ("uninitialized reference member `%D'", member); 211 } 212 else if (TREE_CODE (init) == TREE_LIST) 213 { 214 /* There was an explicit member initialization. Do some 215 work in that case. */ 216 if (TREE_CHAIN (init)) 217 { 218 warning ("initializer list treated as compound expression"); 219 init = build_compound_expr (init); 220 } 221 else 222 init = TREE_VALUE (init); 223 } 224 225 /* We only build this with a null init if we got it from the 226 current_member_init_list. */ 227 if (init || explicit) 228 { 229 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init)); 230 } 231 } 232 233 expand_end_target_temps (); 234 free_temp_slots (); 235 236 if (TYPE_NEEDS_DESTRUCTOR (type)) 237 { 238 tree expr; 239 240 /* All cleanups must be on the function_obstack. */ 241 push_obstacks_nochange (); 242 resume_temporary_allocation (); 243 244 expr = build_component_ref (current_class_ref, name, NULL_TREE, 245 explicit); 246 expr = build_delete (type, expr, integer_zero_node, 247 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0); 248 249 if (expr != error_mark_node) 250 add_partial_entry (expr); 251 252 pop_obstacks (); 253 } 254} 255 256extern int warn_reorder; 257 258/* Subroutine of emit_member_init. */ 259 260static tree 261sort_member_init (t) 262 tree t; 263{ 264 tree x, member, name, field; 265 tree init_list = NULL_TREE; 266 int last_pos = 0; 267 tree last_field = NULL_TREE; 268 269 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member)) 270 { 271 int pos; 272 273 /* member could be, for example, a CONST_DECL for an enumerated 274 tag; we don't want to try to initialize that, since it already 275 has a value. */ 276 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member)) 277 continue; 278 279 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos) 280 { 281 /* If we cleared this out, then pay no attention to it. */ 282 if (TREE_PURPOSE (x) == NULL_TREE) 283 continue; 284 name = TREE_PURPOSE (x); 285 286 if (TREE_CODE (name) == IDENTIFIER_NODE) 287 field = IDENTIFIER_CLASS_VALUE (name); 288 else 289 { 290 my_friendly_assert (TREE_CODE (name) == FIELD_DECL, 348); 291 field = name; 292 } 293 294 /* If one member shadows another, get the outermost one. */ 295 if (TREE_CODE (field) == TREE_LIST) 296 field = TREE_VALUE (field); 297 298 if (field == member) 299 { 300 if (warn_reorder) 301 { 302 if (pos < last_pos) 303 { 304 cp_warning_at ("member initializers for `%#D'", last_field); 305 cp_warning_at (" and `%#D'", field); 306 warning (" will be re-ordered to match declaration order"); 307 } 308 last_pos = pos; 309 last_field = field; 310 } 311 312 /* Make sure we won't try to work on this init again. */ 313 TREE_PURPOSE (x) = NULL_TREE; 314 x = build_tree_list (name, TREE_VALUE (x)); 315 goto got_it; 316 } 317 } 318 319 /* If we didn't find MEMBER in the list, create a dummy entry 320 so the two lists (INIT_LIST and the list of members) will be 321 symmetrical. */ 322 x = build_tree_list (NULL_TREE, NULL_TREE); 323 got_it: 324 init_list = chainon (init_list, x); 325 } 326 327 /* Initializers for base members go at the end. */ 328 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x)) 329 { 330 name = TREE_PURPOSE (x); 331 if (name) 332 { 333 if (purpose_member (name, init_list)) 334 { 335 cp_error ("multiple initializations given for member `%D'", 336 IDENTIFIER_CLASS_VALUE (name)); 337 continue; 338 } 339 340 init_list = chainon (init_list, 341 build_tree_list (name, TREE_VALUE (x))); 342 TREE_PURPOSE (x) = NULL_TREE; 343 } 344 } 345 346 return init_list; 347} 348 349static void 350sort_base_init (t, rbase_ptr, vbase_ptr) 351 tree t, *rbase_ptr, *vbase_ptr; 352{ 353 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t)); 354 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; 355 356 int i; 357 tree x; 358 tree last; 359 360 /* For warn_reorder. */ 361 int last_pos = 0; 362 tree last_base = NULL_TREE; 363 364 tree rbases = NULL_TREE; 365 tree vbases = NULL_TREE; 366 367 /* First walk through and splice out vbase and invalid initializers. 368 Also replace names with binfos. */ 369 370 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list); 371 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x)) 372 { 373 tree basetype = TREE_PURPOSE (x); 374 tree binfo = NULL_TREE; 375 376 if (basetype == NULL_TREE) 377 { 378 /* Initializer for single base class. Must not 379 use multiple inheritance or this is ambiguous. */ 380 switch (n_baseclasses) 381 { 382 case 0: 383 cp_error ("`%T' does not have a base class to initialize", 384 current_class_type); 385 return; 386 case 1: 387 break; 388 default: 389 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance", 390 current_class_type); 391 return; 392 } 393 binfo = TREE_VEC_ELT (binfos, 0); 394 } 395 else if (is_aggr_type (basetype, 1)) 396 { 397 binfo = binfo_or_else (basetype, t); 398 if (binfo == NULL_TREE) 399 continue; 400 401 /* Virtual base classes are special cases. Their initializers 402 are recorded with this constructor, and they are used when 403 this constructor is the top-level constructor called. */ 404 if (TREE_VIA_VIRTUAL (binfo)) 405 { 406 tree v = CLASSTYPE_VBASECLASSES (t); 407 while (BINFO_TYPE (v) != BINFO_TYPE (binfo)) 408 v = TREE_CHAIN (v); 409 410 vbases = tree_cons (v, TREE_VALUE (x), vbases); 411 continue; 412 } 413 else 414 { 415 /* Otherwise, if it is not an immediate base class, complain. */ 416 for (i = n_baseclasses-1; i >= 0; i--) 417 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i))) 418 break; 419 if (i < 0) 420 { 421 cp_error ("`%T' is not an immediate base class of `%T'", 422 basetype, current_class_type); 423 continue; 424 } 425 } 426 } 427 else 428 my_friendly_abort (365); 429 430 TREE_PURPOSE (x) = binfo; 431 TREE_CHAIN (last) = x; 432 last = x; 433 } 434 TREE_CHAIN (last) = NULL_TREE; 435 436 /* Now walk through our regular bases and make sure they're initialized. */ 437 438 for (i = 0; i < n_baseclasses; ++i) 439 { 440 tree base_binfo = TREE_VEC_ELT (binfos, i); 441 int pos; 442 443 if (TREE_VIA_VIRTUAL (base_binfo)) 444 continue; 445 446 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos) 447 { 448 tree binfo = TREE_PURPOSE (x); 449 450 if (binfo == NULL_TREE) 451 continue; 452 453 if (binfo == base_binfo) 454 { 455 if (warn_reorder) 456 { 457 if (pos < last_pos) 458 { 459 cp_warning_at ("base initializers for `%#T'", last_base); 460 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo)); 461 warning (" will be re-ordered to match inheritance order"); 462 } 463 last_pos = pos; 464 last_base = BINFO_TYPE (binfo); 465 } 466 467 /* Make sure we won't try to work on this init again. */ 468 TREE_PURPOSE (x) = NULL_TREE; 469 x = build_tree_list (binfo, TREE_VALUE (x)); 470 goto got_it; 471 } 472 } 473 474 /* If we didn't find BASE_BINFO in the list, create a dummy entry 475 so the two lists (RBASES and the list of bases) will be 476 symmetrical. */ 477 x = build_tree_list (NULL_TREE, NULL_TREE); 478 got_it: 479 rbases = chainon (rbases, x); 480 } 481 482 *rbase_ptr = rbases; 483 *vbase_ptr = vbases; 484} 485 486/* Invoke a base-class destructor. REF is the object being destroyed, 487 BINFO is the base class, and DTOR_ARG indicates whether the base 488 class should invoke delete. */ 489 490tree 491build_base_dtor_call (ref, binfo, dtor_arg) 492 tree ref, binfo, dtor_arg; 493{ 494 tree args = NULL_TREE; 495 tree vlist = lookup_name (vlist_identifier, 0); 496 tree call, decr; 497 498 if (TYPE_USES_PVBASES (BINFO_TYPE (binfo))) 499 { 500 args = expr_tree_cons (NULL_TREE, vlist, args); 501 dtor_arg = build (BIT_IOR_EXPR, integer_type_node, 502 dtor_arg, build_int_2 (4, 0)); 503 dtor_arg = fold (dtor_arg); 504 } 505 args = expr_tree_cons (NULL_TREE, dtor_arg, args); 506 call = build_scoped_method_call (ref, binfo, dtor_identifier, args); 507 508 if (!TYPE_USES_PVBASES (BINFO_TYPE (binfo))) 509 /* For plain inheritance, do not try to adjust __vlist. */ 510 return call; 511 512 /* Now decrement __vlist by the number of slots consumed by the base 513 dtor. */ 514 decr = build_int_2 (pvbasecount (BINFO_TYPE (binfo), 0), 0); 515 decr = build_binary_op (MINUS_EXPR, vlist, decr); 516 decr = build_modify_expr (vlist, NOP_EXPR, decr); 517 518 return build (COMPOUND_EXPR, void_type_node, call, decr); 519} 520 521/* Return the number of vlist entries needed to initialize TYPE, 522 depending on whether it is IN_CHARGE. */ 523 524static int 525pvbasecount (type, in_charge) 526 tree type; 527 int in_charge; 528{ 529 int i; 530 int result = 0; 531 tree vbase; 532 533 for (vbase = (CLASSTYPE_VBASECLASSES (type)); vbase; 534 vbase = TREE_CHAIN (vbase)) 535 { 536 result += list_length (CLASSTYPE_VFIELDS (BINFO_TYPE (vbase))); 537 if (in_charge) 538 result += pvbasecount (BINFO_TYPE (vbase), 0); 539 } 540 541 for (i=0; i < CLASSTYPE_N_BASECLASSES (type); i++) 542 { 543 tree base = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i); 544 if (TREE_VIA_VIRTUAL (base)) 545 continue; 546 result += pvbasecount (BINFO_TYPE (base), 0); 547 } 548 return result; 549} 550 551void 552init_vlist (t) 553 tree t; 554{ 555 char *name; 556 tree expr; 557 tree vlist = lookup_name (vlist_identifier, 0); 558 559 name = alloca (strlen (VLIST_NAME_FORMAT) 560 + TYPE_ASSEMBLER_NAME_LENGTH (t) + 2); 561 sprintf (name, VLIST_NAME_FORMAT, TYPE_ASSEMBLER_NAME_STRING (t)); 562 563 expr = get_identifier (name); 564 expr = lookup_name (expr, 0); 565 expr = build1 (ADDR_EXPR, TREE_TYPE (vlist), expr); 566 if (DECL_DESTRUCTOR_FOR_PVBASE_P (current_function_decl)) 567 /* Move to the end of the vlist. */ 568 expr = build_binary_op (PLUS_EXPR, expr, 569 build_int_2 (pvbasecount (t, 1), 0)); 570 expand_expr_stmt (build_modify_expr (vlist, NOP_EXPR, expr)); 571} 572 573/* Perform whatever initializations have yet to be done on the base 574 class of the class variable. These actions are in the global 575 variable CURRENT_BASE_INIT_LIST. Such an action could be 576 NULL_TREE, meaning that the user has explicitly called the base 577 class constructor with no arguments. 578 579 If there is a need for a call to a constructor, we must surround 580 that call with a pushlevel/poplevel pair, since we are technically 581 at the PARM level of scope. 582 583 Argument IMMEDIATELY, if zero, forces a new sequence to be 584 generated to contain these new insns, so it can be emitted later. 585 This sequence is saved in the global variable BASE_INIT_EXPR. 586 Otherwise, the insns are emitted into the current sequence. 587 588 Note that emit_base_init does *not* initialize virtual base 589 classes. That is done specially, elsewhere. */ 590 591extern tree base_init_expr, rtl_expr_chain; 592 593void 594emit_base_init (t, immediately) 595 tree t; 596 int immediately; 597{ 598 tree member; 599 tree mem_init_list; 600 tree rbase_init_list, vbase_init_list; 601 tree t_binfo = TYPE_BINFO (t); 602 tree binfos = BINFO_BASETYPES (t_binfo); 603 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; 604 tree expr = NULL_TREE; 605 tree vlist = lookup_name (vlist_identifier, 0); 606 607 if (! immediately) 608 { 609 int momentary; 610 do_pending_stack_adjust (); 611 /* Make the RTL_EXPR node temporary, not momentary, 612 so that rtl_expr_chain doesn't become garbage. */ 613 momentary = suspend_momentary (); 614 expr = make_node (RTL_EXPR); 615 resume_momentary (momentary); 616 start_sequence_for_rtl_expr (expr); 617 } 618 619 if (write_symbols == NO_DEBUG) 620 /* As a matter of principle, `start_sequence' should do this. */ 621 emit_note (0, -1); 622 else 623 /* Always emit a line number note so we can step into constructors. */ 624 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl), 625 DECL_SOURCE_LINE (current_function_decl)); 626 627 mem_init_list = sort_member_init (t); 628 current_member_init_list = NULL_TREE; 629 630 sort_base_init (t, &rbase_init_list, &vbase_init_list); 631 current_base_init_list = NULL_TREE; 632 633 /* First, initialize the virtual base classes, if we are 634 constructing the most-derived object. */ 635 if (TYPE_USES_VIRTUAL_BASECLASSES (t)) 636 { 637 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); 638 construct_virtual_bases (t, current_class_ref, current_class_ptr, 639 vbase_init_list, first_arg); 640 } 641 642 /* Now, perform initialization of non-virtual base classes. */ 643 for (i = 0; i < n_baseclasses; i++) 644 { 645 tree base_binfo = TREE_VEC_ELT (binfos, i); 646 tree init = void_list_node; 647 648 if (TREE_VIA_VIRTUAL (base_binfo)) 649 continue; 650 651 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo, 652 999); 653 654 if (TREE_PURPOSE (rbase_init_list)) 655 init = TREE_VALUE (rbase_init_list); 656 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo))) 657 { 658 init = NULL_TREE; 659 if (extra_warnings && copy_args_p (current_function_decl)) 660 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor", 661 BINFO_TYPE (base_binfo)); 662 } 663 664 if (init != void_list_node) 665 { 666 expand_start_target_temps (); 667 668 member = convert_pointer_to_real (base_binfo, current_class_ptr); 669 expand_aggr_init_1 (base_binfo, NULL_TREE, 670 build_indirect_ref (member, NULL_PTR), init, 671 LOOKUP_NORMAL); 672 673 expand_end_target_temps (); 674 free_temp_slots (); 675 } 676 677 expand_cleanup_for_base (base_binfo, vlist, NULL_TREE); 678 rbase_init_list = TREE_CHAIN (rbase_init_list); 679 } 680 681 /* Initialize all the virtual function table fields that 682 do come from virtual base classes. */ 683 if (TYPE_USES_VIRTUAL_BASECLASSES (t)) 684 expand_indirect_vtbls_init (t_binfo, current_class_ref, current_class_ptr); 685 686 /* Initialize all the virtual function table fields that 687 do not come from virtual base classes. */ 688 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_ptr); 689 690 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member)) 691 { 692 tree init, name; 693 int from_init_list; 694 695 /* member could be, for example, a CONST_DECL for an enumerated 696 tag; we don't want to try to initialize that, since it already 697 has a value. */ 698 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member)) 699 continue; 700 701 /* See if we had a user-specified member initialization. */ 702 if (TREE_PURPOSE (mem_init_list)) 703 { 704 name = TREE_PURPOSE (mem_init_list); 705 init = TREE_VALUE (mem_init_list); 706 from_init_list = 1; 707 708 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE 709 || TREE_CODE (name) == FIELD_DECL, 349); 710 } 711 else 712 { 713 name = DECL_NAME (member); 714 init = DECL_INITIAL (member); 715 716 from_init_list = 0; 717 718 /* Effective C++ rule 12. */ 719 if (warn_ecpp && init == NULL_TREE 720 && !DECL_ARTIFICIAL (member) 721 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE) 722 cp_warning ("`%D' should be initialized in the member initialization list", member); 723 } 724 725 perform_member_init (member, name, init, from_init_list); 726 mem_init_list = TREE_CHAIN (mem_init_list); 727 } 728 729 /* Now initialize any members from our bases. */ 730 while (mem_init_list) 731 { 732 tree name, init, field; 733 734 if (TREE_PURPOSE (mem_init_list)) 735 { 736 name = TREE_PURPOSE (mem_init_list); 737 init = TREE_VALUE (mem_init_list); 738 739 if (TREE_CODE (name) == IDENTIFIER_NODE) 740 field = IDENTIFIER_CLASS_VALUE (name); 741 else 742 field = name; 743 744 /* If one member shadows another, get the outermost one. */ 745 if (TREE_CODE (field) == TREE_LIST) 746 { 747 field = TREE_VALUE (field); 748 if (decl_type_context (field) != current_class_type) 749 cp_error ("field `%D' not in immediate context", field); 750 } 751 752#if 0 753 /* It turns out if you have an anonymous union in the 754 class, a member from it can end up not being on the 755 list of fields (rather, the type is), and therefore 756 won't be seen by the for loop above. */ 757 758 /* The code in this for loop is derived from a general loop 759 which had this check in it. Theoretically, we've hit 760 every initialization for the list of members in T, so 761 we shouldn't have anything but these left in this list. */ 762 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351); 763#endif 764 765 perform_member_init (field, name, init, 1); 766 } 767 mem_init_list = TREE_CHAIN (mem_init_list); 768 } 769 770 if (! immediately) 771 { 772 do_pending_stack_adjust (); 773 my_friendly_assert (base_init_expr == 0, 207); 774 base_init_expr = expr; 775 TREE_TYPE (expr) = void_type_node; 776 RTL_EXPR_RTL (expr) = const0_rtx; 777 RTL_EXPR_SEQUENCE (expr) = get_insns (); 778 rtl_expr_chain = tree_cons (NULL_TREE, expr, rtl_expr_chain); 779 end_sequence (); 780 TREE_SIDE_EFFECTS (expr) = 1; 781 } 782 783 /* All the implicit try blocks we built up will be zapped 784 when we come to a real binding contour boundary. */ 785} 786 787/* Check that all fields are properly initialized after 788 an assignment to `this'. */ 789 790void 791check_base_init (t) 792 tree t; 793{ 794 tree member; 795 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member)) 796 if (DECL_NAME (member) && TREE_USED (member)) 797 cp_error ("field `%D' used before initialized (after assignment to `this')", 798 member); 799} 800 801/* This code sets up the virtual function tables appropriate for 802 the pointer DECL. It is a one-ply initialization. 803 804 BINFO is the exact type that DECL is supposed to be. In 805 multiple inheritance, this might mean "C's A" if C : A, B. */ 806 807static void 808expand_virtual_init (binfo, decl) 809 tree binfo, decl; 810{ 811 tree type = BINFO_TYPE (binfo); 812 tree vtbl, vtbl_ptr; 813 tree vtype, vtype_binfo; 814 815 /* This code is crusty. Should be simple, like: 816 vtbl = BINFO_VTABLE (binfo); 817 */ 818 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type)); 819 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0); 820 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo)); 821 assemble_external (vtbl); 822 TREE_USED (vtbl) = 1; 823 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl); 824 decl = convert_pointer_to_real (vtype_binfo, decl); 825 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype); 826 if (vtbl_ptr == error_mark_node) 827 return; 828 829 /* Have to convert VTBL since array sizes may be different. */ 830 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0); 831 expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl)); 832} 833 834/* If an exception is thrown in a constructor, those base classes already 835 constructed must be destroyed. This function creates the cleanup 836 for BINFO, which has just been constructed. If FLAG is non-NULL, 837 it is a DECL which is non-zero when this base needs to be 838 destroyed. */ 839 840static void 841expand_cleanup_for_base (binfo, vlist, flag) 842 tree binfo; 843 tree vlist; 844 tree flag; 845{ 846 tree expr; 847 848 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo))) 849 { 850 /* All cleanups must be on the function_obstack. */ 851 push_obstacks_nochange (); 852 resume_temporary_allocation (); 853 854 /* Call the destructor. */ 855 expr = build_base_dtor_call (current_class_ref, binfo, 856 integer_zero_node); 857 if (flag) 858 expr = fold (build (COND_EXPR, void_type_node, 859 truthvalue_conversion (flag), 860 expr, integer_zero_node)); 861 862 pop_obstacks (); 863 add_partial_entry (expr); 864 } 865 866 if (TYPE_USES_PVBASES (BINFO_TYPE (binfo))) 867 { 868 /* Increment vlist by number of base's vbase classes. */ 869 expr = build_int_2 (pvbasecount (BINFO_TYPE (binfo), 0), 0); 870 expr = build_binary_op (PLUS_EXPR, vlist, expr); 871 expr = build_modify_expr (vlist, NOP_EXPR, expr); 872 expand_expr_stmt (expr); 873 } 874} 875 876/* Subroutine of `expand_aggr_vbase_init'. 877 BINFO is the binfo of the type that is being initialized. 878 INIT_LIST is the list of initializers for the virtual baseclass. */ 879 880static void 881expand_aggr_vbase_init_1 (binfo, exp, addr, init_list) 882 tree binfo, exp, addr, init_list; 883{ 884 tree init = purpose_member (binfo, init_list); 885 tree ref = build_indirect_ref (addr, NULL_PTR); 886 887 expand_start_target_temps (); 888 889 if (init) 890 init = TREE_VALUE (init); 891 /* Call constructors, but don't set up vtables. */ 892 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN); 893 894 expand_end_target_temps (); 895 free_temp_slots (); 896} 897 898/* Construct the virtual base-classes of THIS_REF (whose address is 899 THIS_PTR). The object has the indicated TYPE. The construction 900 actually takes place only if FLAG is non-zero. INIT_LIST is list 901 of initialization for constructor to perform. */ 902 903static void 904construct_virtual_bases (type, this_ref, this_ptr, init_list, flag) 905 tree type; 906 tree this_ref; 907 tree this_ptr; 908 tree init_list; 909 tree flag; 910{ 911 tree vbases; 912 tree result; 913 tree vlist = NULL_TREE; 914 915 /* If there are no virtual baseclasses, we shouldn't even be here. */ 916 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621); 917 918 /* First set the pointers in our object that tell us where to find 919 our virtual baseclasses. */ 920 expand_start_cond (flag, 0); 921 if (TYPE_USES_PVBASES (type)) 922 { 923 init_vlist (type); 924 vlist = lookup_name (vlist_identifier, 0); 925 } 926 result = init_vbase_pointers (type, this_ptr); 927 if (result) 928 expand_expr_stmt (build_compound_expr (result)); 929 expand_end_cond (); 930 931 /* Now, run through the baseclasses, initializing each. */ 932 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; 933 vbases = TREE_CHAIN (vbases)) 934 { 935 tree tmp = purpose_member (vbases, result); 936 937 /* If there are virtual base classes with destructors, we need to 938 emit cleanups to destroy them if an exception is thrown during 939 the construction process. These exception regions (i.e., the 940 period during which the cleanups must occur) begin from the time 941 the construction is complete to the end of the function. If we 942 create a conditional block in which to initialize the 943 base-classes, then the cleanup region for the virtual base begins 944 inside a block, and ends outside of that block. This situation 945 confuses the sjlj exception-handling code. Therefore, we do not 946 create a single conditional block, but one for each 947 initialization. (That way the cleanup regions always begin 948 in the outer block.) We trust the back-end to figure out 949 that the FLAG will not change across initializations, and 950 avoid doing multiple tests. */ 951 expand_start_cond (flag, 0); 952 expand_aggr_vbase_init_1 (vbases, this_ref, 953 TREE_OPERAND (TREE_VALUE (tmp), 0), 954 init_list); 955 expand_end_cond (); 956 957 expand_cleanup_for_base (vbases, vlist, flag); 958 } 959} 960 961/* Find the context in which this FIELD can be initialized. */ 962 963static tree 964initializing_context (field) 965 tree field; 966{ 967 tree t = DECL_CONTEXT (field); 968 969 /* Anonymous union members can be initialized in the first enclosing 970 non-anonymous union context. */ 971 while (t && ANON_AGGR_TYPE_P (t)) 972 t = TYPE_CONTEXT (t); 973 return t; 974} 975 976/* Function to give error message if member initialization specification 977 is erroneous. FIELD is the member we decided to initialize. 978 TYPE is the type for which the initialization is being performed. 979 FIELD must be a member of TYPE. 980 981 MEMBER_NAME is the name of the member. */ 982 983static int 984member_init_ok_or_else (field, type, member_name) 985 tree field; 986 tree type; 987 const char *member_name; 988{ 989 if (field == error_mark_node) 990 return 0; 991 if (field == NULL_TREE || initializing_context (field) != type) 992 { 993 cp_error ("class `%T' does not have any field named `%s'", type, 994 member_name); 995 return 0; 996 } 997 if (TREE_STATIC (field)) 998 { 999 cp_error ("field `%#D' is static; only point of initialization is its declaration", 1000 field); 1001 return 0; 1002 } 1003 1004 return 1; 1005} 1006 1007/* If NAME is a viable field name for the aggregate DECL, 1008 and PARMS is a viable parameter list, then expand an _EXPR 1009 which describes this initialization. 1010 1011 Note that we do not need to chase through the class's base classes 1012 to look for NAME, because if it's in that list, it will be handled 1013 by the constructor for that base class. 1014 1015 We do not yet have a fixed-point finder to instantiate types 1016 being fed to overloaded constructors. If there is a unique 1017 constructor, then argument types can be got from that one. 1018 1019 If INIT is non-NULL, then it the initialization should 1020 be placed in `current_base_init_list', where it will be processed 1021 by `emit_base_init'. */ 1022 1023void 1024expand_member_init (exp, name, init) 1025 tree exp, name, init; 1026{ 1027 tree basetype = NULL_TREE, field; 1028 tree type; 1029 1030 if (exp == NULL_TREE) 1031 return; /* complain about this later */ 1032 1033 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); 1034 1035 if (name && TREE_CODE (name) == TYPE_DECL) 1036 { 1037 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name)); 1038 name = DECL_NAME (name); 1039 } 1040 1041 if (name == NULL_TREE && IS_AGGR_TYPE (type)) 1042 switch (CLASSTYPE_N_BASECLASSES (type)) 1043 { 1044 case 0: 1045 error ("base class initializer specified, but no base class to initialize"); 1046 return; 1047 case 1: 1048 basetype = TYPE_BINFO_BASETYPE (type, 0); 1049 break; 1050 default: 1051 error ("initializer for unnamed base class ambiguous"); 1052 cp_error ("(type `%T' uses multiple inheritance)", type); 1053 return; 1054 } 1055 1056 my_friendly_assert (init != NULL_TREE, 0); 1057 1058 /* The grammar should not allow fields which have names that are 1059 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we 1060 may assume that this is an attempt to initialize a base class 1061 member of the current type. Otherwise, it is an attempt to 1062 initialize a member field. */ 1063 1064 if (init == void_type_node) 1065 init = NULL_TREE; 1066 1067 if (name == NULL_TREE || basetype) 1068 { 1069 tree base_init; 1070 1071 if (name == NULL_TREE) 1072 { 1073#if 0 1074 if (basetype) 1075 name = TYPE_IDENTIFIER (basetype); 1076 else 1077 { 1078 error ("no base class to initialize"); 1079 return; 1080 } 1081#endif 1082 } 1083 else if (basetype != type 1084 && ! current_template_parms 1085 && ! vec_binfo_member (basetype, 1086 TYPE_BINFO_BASETYPES (type)) 1087 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type))) 1088 { 1089 if (IDENTIFIER_CLASS_VALUE (name)) 1090 goto try_member; 1091 if (TYPE_USES_VIRTUAL_BASECLASSES (type)) 1092 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'", 1093 basetype, type); 1094 else 1095 cp_error ("type `%T' is not an immediate basetype for `%T'", 1096 basetype, type); 1097 return; 1098 } 1099 1100 if (purpose_member (basetype, current_base_init_list)) 1101 { 1102 cp_error ("base class `%T' already initialized", basetype); 1103 return; 1104 } 1105 1106 if (warn_reorder && current_member_init_list) 1107 { 1108 cp_warning ("base initializer for `%T'", basetype); 1109 warning (" will be re-ordered to precede member initializations"); 1110 } 1111 1112 base_init = build_tree_list (basetype, init); 1113 current_base_init_list = chainon (current_base_init_list, base_init); 1114 } 1115 else 1116 { 1117 tree member_init; 1118 1119 try_member: 1120 field = lookup_field (type, name, 1, 0); 1121 1122 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name))) 1123 return; 1124 1125 if (purpose_member (name, current_member_init_list)) 1126 { 1127 cp_error ("field `%D' already initialized", field); 1128 return; 1129 } 1130 1131 member_init = build_tree_list (name, init); 1132 current_member_init_list = chainon (current_member_init_list, member_init); 1133 } 1134} 1135 1136/* This is like `expand_member_init', only it stores one aggregate 1137 value into another. 1138 1139 INIT comes in two flavors: it is either a value which 1140 is to be stored in EXP, or it is a parameter list 1141 to go to a constructor, which will operate on EXP. 1142 If INIT is not a parameter list for a constructor, then set 1143 LOOKUP_ONLYCONVERTING. 1144 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of 1145 the initializer, if FLAGS is 0, then it is the (init) form. 1146 If `init' is a CONSTRUCTOR, then we emit a warning message, 1147 explaining that such initializations are invalid. 1148 1149 ALIAS_THIS is nonzero iff we are initializing something which is 1150 essentially an alias for current_class_ref. In this case, the base 1151 constructor may move it on us, and we must keep track of such 1152 deviations. 1153 1154 If INIT resolves to a CALL_EXPR which happens to return 1155 something of the type we are looking for, then we know 1156 that we can safely use that call to perform the 1157 initialization. 1158 1159 The virtual function table pointer cannot be set up here, because 1160 we do not really know its type. 1161 1162 Virtual baseclass pointers are also set up here. 1163 1164 This never calls operator=(). 1165 1166 When initializing, nothing is CONST. 1167 1168 A default copy constructor may have to be used to perform the 1169 initialization. 1170 1171 A constructor or a conversion operator may have to be used to 1172 perform the initialization, but not both, as it would be ambiguous. */ 1173 1174void 1175expand_aggr_init (exp, init, flags) 1176 tree exp, init; 1177 int flags; 1178{ 1179 tree type = TREE_TYPE (exp); 1180 int was_const = TREE_READONLY (exp); 1181 int was_volatile = TREE_THIS_VOLATILE (exp); 1182 1183 if (init == error_mark_node) 1184 return; 1185 1186 TREE_READONLY (exp) = 0; 1187 TREE_THIS_VOLATILE (exp) = 0; 1188 1189 if (init && TREE_CODE (init) != TREE_LIST) 1190 flags |= LOOKUP_ONLYCONVERTING; 1191 1192 if (TREE_CODE (type) == ARRAY_TYPE) 1193 { 1194 /* Must arrange to initialize each element of EXP 1195 from elements of INIT. */ 1196 tree itype = init ? TREE_TYPE (init) : NULL_TREE; 1197 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED) 1198 { 1199 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type); 1200 if (init) 1201 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype); 1202 } 1203 if (init && TREE_TYPE (init) == NULL_TREE) 1204 { 1205 /* Handle bad initializers like: 1206 class COMPLEX { 1207 public: 1208 double re, im; 1209 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;}; 1210 ~COMPLEX() {}; 1211 }; 1212 1213 int main(int argc, char **argv) { 1214 COMPLEX zees(1.0, 0.0)[10]; 1215 } 1216 */ 1217 error ("bad array initializer"); 1218 return; 1219 } 1220 expand_vec_init (exp, exp, array_type_nelts (type), init, 1221 init && same_type_p (TREE_TYPE (init), 1222 TREE_TYPE (exp))); 1223 TREE_READONLY (exp) = was_const; 1224 TREE_THIS_VOLATILE (exp) = was_volatile; 1225 TREE_TYPE (exp) = type; 1226 if (init) 1227 TREE_TYPE (init) = itype; 1228 return; 1229 } 1230 1231 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL) 1232 /* just know that we've seen something for this node */ 1233 TREE_USED (exp) = 1; 1234 1235#if 0 1236 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the 1237 constructor as parameters to an implicit GNU C++ constructor. */ 1238 if (init && TREE_CODE (init) == CONSTRUCTOR 1239 && TYPE_HAS_CONSTRUCTOR (type) 1240 && TREE_TYPE (init) == type) 1241 init = CONSTRUCTOR_ELTS (init); 1242#endif 1243 1244 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type); 1245 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp, 1246 init, LOOKUP_NORMAL|flags); 1247 TREE_TYPE (exp) = type; 1248 TREE_READONLY (exp) = was_const; 1249 TREE_THIS_VOLATILE (exp) = was_volatile; 1250} 1251 1252static tree 1253no_vlist_base_init (rval, exp, init, binfo, flags) 1254 tree rval, exp, init, binfo; 1255 int flags; 1256{ 1257 tree nrval, func, parms; 1258 1259 /* Obtain the vlist-expecting ctor. */ 1260 func = rval; 1261 my_friendly_assert (TREE_CODE (func) == CALL_EXPR, 20000131); 1262 func = TREE_OPERAND (func, 0); 1263 my_friendly_assert (TREE_CODE (func) == ADDR_EXPR, 20000132); 1264 func = TREE_OPERAND (func, 0); 1265 my_friendly_assert (TREE_CODE (func) == FUNCTION_DECL, 20000133); 1266 1267 /* If we have already seen a definition for the wrapped function, 1268 we don't need to declare it weak. Also, declare_weak will complain 1269 if we do. */ 1270 if (!TREE_ASM_WRITTEN (func)) 1271 declare_weak (func); 1272 1273 if (init == NULL_TREE 1274 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init))) 1275 { 1276 parms = init; 1277 if (parms) 1278 init = TREE_VALUE (parms); 1279 } 1280 else 1281 parms = build_expr_list (NULL_TREE, init); 1282 1283 flags &= ~LOOKUP_HAS_VLIST; 1284 1285 parms = expr_tree_cons (NULL_TREE, integer_zero_node, parms); 1286 flags |= LOOKUP_HAS_IN_CHARGE; 1287 1288 nrval = build_method_call (exp, ctor_identifier, 1289 parms, binfo, flags); 1290 1291 func = build (NE_EXPR, boolean_type_node, 1292 func, null_pointer_node); 1293 nrval = build (COND_EXPR, void_type_node, 1294 func, rval, nrval); 1295 return nrval; 1296} 1297 1298static void 1299expand_default_init (binfo, true_exp, exp, init, flags) 1300 tree binfo; 1301 tree true_exp, exp; 1302 tree init; 1303 int flags; 1304{ 1305 tree type = TREE_TYPE (exp); 1306 1307 /* It fails because there may not be a constructor which takes 1308 its own type as the first (or only parameter), but which does 1309 take other types via a conversion. So, if the thing initializing 1310 the expression is a unit element of type X, first try X(X&), 1311 followed by initialization by X. If neither of these work 1312 out, then look hard. */ 1313 tree rval; 1314 tree parms; 1315 tree vlist = NULL_TREE; 1316 tree orig_init = init; 1317 1318 if (init && TREE_CODE (init) != TREE_LIST 1319 && (flags & LOOKUP_ONLYCONVERTING)) 1320 { 1321 /* Base subobjects should only get direct-initialization. */ 1322 if (true_exp != exp) 1323 abort (); 1324 1325 if (flags & DIRECT_BIND) 1326 /* Do nothing. We hit this in two cases: Reference initialization, 1327 where we aren't initializing a real variable, so we don't want 1328 to run a new constructor; and catching an exception, where we 1329 have already built up the constructor call so we could wrap it 1330 in an exception region. */; 1331 else 1332 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags); 1333 1334 if (TREE_CODE (init) == TRY_CATCH_EXPR) 1335 /* We need to protect the initialization of a catch parm 1336 with a call to terminate(), which shows up as a TRY_CATCH_EXPR 1337 around the TARGET_EXPR for the copy constructor. See 1338 expand_start_catch_block. */ 1339 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp, 1340 TREE_OPERAND (init, 0)); 1341 else 1342 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init); 1343 TREE_SIDE_EFFECTS (init) = 1; 1344 expand_expr_stmt (init); 1345 return; 1346 } 1347 1348 if (init == NULL_TREE 1349 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init))) 1350 { 1351 parms = init; 1352 if (parms) 1353 init = TREE_VALUE (parms); 1354 } 1355 else 1356 parms = build_expr_list (NULL_TREE, init); 1357 1358 if (TYPE_USES_VIRTUAL_BASECLASSES (type)) 1359 { 1360 if (TYPE_USES_PVBASES (type)) 1361 { 1362 /* In compatibility mode, when not calling a base ctor, 1363 we do not pass the vlist argument. */ 1364 if (true_exp == exp) 1365 vlist = flag_vtable_thunks_compat? NULL_TREE : vlist_zero_node; 1366 else 1367 vlist = lookup_name (vlist_identifier, 0); 1368 1369 if (vlist) 1370 { 1371 parms = expr_tree_cons (NULL_TREE, vlist, parms); 1372 flags |= LOOKUP_HAS_VLIST; 1373 } 1374 } 1375 if (true_exp == exp) 1376 parms = expr_tree_cons (NULL_TREE, integer_one_node, parms); 1377 else 1378 parms = expr_tree_cons (NULL_TREE, integer_zero_node, parms); 1379 flags |= LOOKUP_HAS_IN_CHARGE; 1380 } 1381 1382 rval = build_method_call (exp, ctor_identifier, 1383 parms, binfo, flags); 1384 if (vlist && true_exp != exp && flag_vtable_thunks_compat) 1385 { 1386 rval = no_vlist_base_init (rval, exp, orig_init, binfo, flags); 1387 } 1388 if (TREE_SIDE_EFFECTS (rval)) 1389 expand_expr_stmt (rval); 1390} 1391 1392/* This function is responsible for initializing EXP with INIT 1393 (if any). 1394 1395 BINFO is the binfo of the type for who we are performing the 1396 initialization. For example, if W is a virtual base class of A and B, 1397 and C : A, B. 1398 If we are initializing B, then W must contain B's W vtable, whereas 1399 were we initializing C, W must contain C's W vtable. 1400 1401 TRUE_EXP is nonzero if it is the true expression being initialized. 1402 In this case, it may be EXP, or may just contain EXP. The reason we 1403 need this is because if EXP is a base element of TRUE_EXP, we 1404 don't necessarily know by looking at EXP where its virtual 1405 baseclass fields should really be pointing. But we do know 1406 from TRUE_EXP. In constructors, we don't know anything about 1407 the value being initialized. 1408 1409 ALIAS_THIS serves the same purpose it serves for expand_aggr_init. 1410 1411 FLAGS is just passes to `build_method_call'. See that function for 1412 its description. */ 1413 1414static void 1415expand_aggr_init_1 (binfo, true_exp, exp, init, flags) 1416 tree binfo; 1417 tree true_exp, exp; 1418 tree init; 1419 int flags; 1420{ 1421 tree type = TREE_TYPE (exp); 1422 1423 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211); 1424 1425 /* Use a function returning the desired type to initialize EXP for us. 1426 If the function is a constructor, and its first argument is 1427 NULL_TREE, know that it was meant for us--just slide exp on 1428 in and expand the constructor. Constructors now come 1429 as TARGET_EXPRs. */ 1430 1431 if (init && TREE_CODE (exp) == VAR_DECL 1432 && TREE_CODE (init) == CONSTRUCTOR 1433 && TREE_HAS_CONSTRUCTOR (init)) 1434 { 1435 tree t = store_init_value (exp, init); 1436 if (!t) 1437 { 1438 expand_decl_init (exp); 1439 return; 1440 } 1441 t = build (INIT_EXPR, type, exp, init); 1442 TREE_SIDE_EFFECTS (t) = 1; 1443 expand_expr_stmt (t); 1444 return; 1445 } 1446 1447 /* We know that expand_default_init can handle everything we want 1448 at this point. */ 1449 expand_default_init (binfo, true_exp, exp, init, flags); 1450} 1451 1452/* Report an error if NAME is not the name of a user-defined, 1453 aggregate type. If OR_ELSE is nonzero, give an error message. */ 1454 1455int 1456is_aggr_typedef (name, or_else) 1457 tree name; 1458 int or_else; 1459{ 1460 tree type; 1461 1462 if (name == error_mark_node) 1463 return 0; 1464 1465 if (IDENTIFIER_HAS_TYPE_VALUE (name)) 1466 type = IDENTIFIER_TYPE_VALUE (name); 1467 else 1468 { 1469 if (or_else) 1470 cp_error ("`%T' is not an aggregate typedef", name); 1471 return 0; 1472 } 1473 1474 if (! IS_AGGR_TYPE (type) 1475 && TREE_CODE (type) != TEMPLATE_TYPE_PARM 1476 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM) 1477 { 1478 if (or_else) 1479 cp_error ("`%T' is not an aggregate type", type); 1480 return 0; 1481 } 1482 return 1; 1483} 1484 1485/* Report an error if TYPE is not a user-defined, aggregate type. If 1486 OR_ELSE is nonzero, give an error message. */ 1487 1488int 1489is_aggr_type (type, or_else) 1490 tree type; 1491 int or_else; 1492{ 1493 if (type == error_mark_node) 1494 return 0; 1495 1496 if (! IS_AGGR_TYPE (type) 1497 && TREE_CODE (type) != TEMPLATE_TYPE_PARM 1498 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM) 1499 { 1500 if (or_else) 1501 cp_error ("`%T' is not an aggregate type", type); 1502 return 0; 1503 } 1504 return 1; 1505} 1506 1507/* Like is_aggr_typedef, but returns typedef if successful. */ 1508 1509tree 1510get_aggr_from_typedef (name, or_else) 1511 tree name; 1512 int or_else; 1513{ 1514 tree type; 1515 1516 if (name == error_mark_node) 1517 return NULL_TREE; 1518 1519 if (IDENTIFIER_HAS_TYPE_VALUE (name)) 1520 type = IDENTIFIER_TYPE_VALUE (name); 1521 else 1522 { 1523 if (or_else) 1524 cp_error ("`%T' fails to be an aggregate typedef", name); 1525 return NULL_TREE; 1526 } 1527 1528 if (! IS_AGGR_TYPE (type) 1529 && TREE_CODE (type) != TEMPLATE_TYPE_PARM 1530 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM) 1531 { 1532 if (or_else) 1533 cp_error ("type `%T' is of non-aggregate type", type); 1534 return NULL_TREE; 1535 } 1536 return type; 1537} 1538 1539tree 1540get_type_value (name) 1541 tree name; 1542{ 1543 if (name == error_mark_node) 1544 return NULL_TREE; 1545 1546 if (IDENTIFIER_HAS_TYPE_VALUE (name)) 1547 return IDENTIFIER_TYPE_VALUE (name); 1548 else 1549 return NULL_TREE; 1550} 1551 1552 1553/* This code could just as well go in `class.c', but is placed here for 1554 modularity. */ 1555 1556/* For an expression of the form TYPE :: NAME (PARMLIST), build 1557 the appropriate function call. */ 1558 1559tree 1560build_member_call (type, name, parmlist) 1561 tree type, name, parmlist; 1562{ 1563 tree t; 1564 tree method_name; 1565 int dtor = 0; 1566 tree basetype_path, decl; 1567 1568 if (TREE_CODE (name) == TEMPLATE_ID_EXPR 1569 && TREE_CODE (type) == NAMESPACE_DECL) 1570 { 1571 /* 'name' already refers to the decls from the namespace, since we 1572 hit do_identifier for template_ids. */ 1573 method_name = TREE_OPERAND (name, 0); 1574 /* FIXME: Since we don't do independent names right yet, the 1575 name might also be a LOOKUP_EXPR. Once we resolve this to a 1576 real decl earlier, this can go. This may happen during 1577 tsubst'ing. */ 1578 if (TREE_CODE (method_name) == LOOKUP_EXPR) 1579 { 1580 method_name = lookup_namespace_name 1581 (type, TREE_OPERAND (method_name, 0)); 1582 TREE_OPERAND (name, 0) = method_name; 1583 } 1584 my_friendly_assert (is_overloaded_fn (method_name), 980519); 1585 return build_x_function_call (name, parmlist, current_class_ref); 1586 } 1587 1588 if (type == std_node) 1589 return build_x_function_call (do_scoped_id (name, 0), parmlist, 1590 current_class_ref); 1591 if (TREE_CODE (type) == NAMESPACE_DECL) 1592 return build_x_function_call (lookup_namespace_name (type, name), 1593 parmlist, current_class_ref); 1594 1595 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 1596 { 1597 method_name = TREE_OPERAND (name, 0); 1598 if (TREE_CODE (method_name) == COMPONENT_REF) 1599 method_name = TREE_OPERAND (method_name, 1); 1600 if (is_overloaded_fn (method_name)) 1601 method_name = DECL_NAME (OVL_CURRENT (method_name)); 1602 TREE_OPERAND (name, 0) = method_name; 1603 } 1604 else 1605 method_name = name; 1606 1607 if (TREE_CODE (method_name) == BIT_NOT_EXPR) 1608 { 1609 method_name = TREE_OPERAND (method_name, 0); 1610 dtor = 1; 1611 } 1612 1613 /* This shouldn't be here, and build_member_call shouldn't appear in 1614 parse.y! (mrs) */ 1615 if (type && TREE_CODE (type) == IDENTIFIER_NODE 1616 && get_aggr_from_typedef (type, 0) == 0) 1617 { 1618 tree ns = lookup_name (type, 0); 1619 if (ns && TREE_CODE (ns) == NAMESPACE_DECL) 1620 { 1621 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref); 1622 } 1623 } 1624 1625 if (type == NULL_TREE || ! is_aggr_type (type, 1)) 1626 return error_mark_node; 1627 1628 /* An operator we did not like. */ 1629 if (name == NULL_TREE) 1630 return error_mark_node; 1631 1632 if (dtor) 1633 { 1634 cp_error ("cannot call destructor `%T::~%T' without object", type, 1635 method_name); 1636 return error_mark_node; 1637 } 1638 1639 decl = maybe_dummy_object (type, &basetype_path); 1640 1641 /* Convert 'this' to the specified type to disambiguate conversion 1642 to the function's context. Apparently Standard C++ says that we 1643 shouldn't do this. */ 1644 if (decl == current_class_ref 1645 && ! pedantic 1646 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type)) 1647 { 1648 tree olddecl = current_class_ptr; 1649 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl)); 1650 if (oldtype != type) 1651 { 1652 tree newtype = build_qualified_type (type, TYPE_QUALS (oldtype)); 1653 decl = convert_force (build_pointer_type (newtype), olddecl, 0); 1654 decl = build_indirect_ref (decl, NULL_PTR); 1655 } 1656 } 1657 1658 if (method_name == constructor_name (type) 1659 || method_name == constructor_name_full (type)) 1660 return build_functional_cast (type, parmlist); 1661 if (lookup_fnfields (basetype_path, method_name, 0)) 1662 return build_method_call (decl, 1663 TREE_CODE (name) == TEMPLATE_ID_EXPR 1664 ? name : method_name, 1665 parmlist, basetype_path, 1666 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL); 1667 if (TREE_CODE (name) == IDENTIFIER_NODE 1668 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0)))) 1669 { 1670 if (t == error_mark_node) 1671 return error_mark_node; 1672 if (TREE_CODE (t) == FIELD_DECL) 1673 { 1674 if (is_dummy_object (decl)) 1675 { 1676 cp_error ("invalid use of non-static field `%D'", t); 1677 return error_mark_node; 1678 } 1679 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t); 1680 } 1681 else if (TREE_CODE (t) == VAR_DECL) 1682 decl = t; 1683 else 1684 { 1685 cp_error ("invalid use of member `%D'", t); 1686 return error_mark_node; 1687 } 1688 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))) 1689 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, 1690 parmlist, NULL_TREE); 1691 return build_function_call (decl, parmlist); 1692 } 1693 else 1694 { 1695 cp_error ("no method `%T::%D'", type, name); 1696 return error_mark_node; 1697 } 1698} 1699 1700/* Build a reference to a member of an aggregate. This is not a 1701 C++ `&', but really something which can have its address taken, 1702 and then act as a pointer to member, for example TYPE :: FIELD 1703 can have its address taken by saying & TYPE :: FIELD. 1704 1705 @@ Prints out lousy diagnostics for operator <typename> 1706 @@ fields. 1707 1708 @@ This function should be rewritten and placed in search.c. */ 1709 1710tree 1711build_offset_ref (type, name) 1712 tree type, name; 1713{ 1714 tree decl, t = error_mark_node; 1715 tree member; 1716 tree basebinfo = NULL_TREE; 1717 tree orig_name = name; 1718 1719 /* class templates can come in as TEMPLATE_DECLs here. */ 1720 if (TREE_CODE (name) == TEMPLATE_DECL) 1721 return name; 1722 1723 if (type == std_node) 1724 return do_scoped_id (name, 0); 1725 1726 if (processing_template_decl || uses_template_parms (type)) 1727 return build_min_nt (SCOPE_REF, type, name); 1728 1729 /* Handle namespace names fully here. */ 1730 if (TREE_CODE (type) == NAMESPACE_DECL) 1731 { 1732 t = lookup_namespace_name (type, name); 1733 if (t != error_mark_node && ! type_unknown_p (t)) 1734 { 1735 mark_used (t); 1736 t = convert_from_reference (t); 1737 } 1738 return t; 1739 } 1740 1741 if (type == NULL_TREE || ! is_aggr_type (type, 1)) 1742 return error_mark_node; 1743 1744 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 1745 { 1746 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at 1747 something like `a.template f<int>' or the like. For the most 1748 part, we treat this just like a.f. We do remember, however, 1749 the template-id that was used. */ 1750 name = TREE_OPERAND (orig_name, 0); 1751 1752 if (TREE_CODE (name) == LOOKUP_EXPR) 1753 /* This can happen during tsubst'ing. */ 1754 name = TREE_OPERAND (name, 0); 1755 1756 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0); 1757 } 1758 1759 if (TREE_CODE (name) == BIT_NOT_EXPR) 1760 { 1761 if (! check_dtor_name (type, name)) 1762 cp_error ("qualified type `%T' does not match destructor name `~%T'", 1763 type, TREE_OPERAND (name, 0)); 1764 name = dtor_identifier; 1765 } 1766#if 0 1767 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */ 1768 else if (name == constructor_name_full (type) 1769 || name == constructor_name (type)) 1770 name = ctor_identifier; 1771#endif 1772 1773 if (TYPE_SIZE (complete_type (type)) == 0 1774 && !TYPE_BEING_DEFINED (type)) 1775 { 1776 cp_error ("incomplete type `%T' does not have member `%D'", type, 1777 name); 1778 return error_mark_node; 1779 } 1780 1781 decl = maybe_dummy_object (type, &basebinfo); 1782 1783 member = lookup_member (basebinfo, name, 1, 0); 1784 1785 if (member == error_mark_node) 1786 return error_mark_node; 1787 1788 /* A lot of this logic is now handled in lookup_field and 1789 lookup_fnfield. */ 1790 if (member && BASELINK_P (member)) 1791 { 1792 /* Go from the TREE_BASELINK to the member function info. */ 1793 tree fnfields = member; 1794 t = TREE_VALUE (fnfields); 1795 1796 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR) 1797 { 1798 /* The FNFIELDS are going to contain functions that aren't 1799 necessarily templates, and templates that don't 1800 necessarily match the explicit template parameters. We 1801 save all the functions, and the explicit parameters, and 1802 then figure out exactly what to instantiate with what 1803 arguments in instantiate_type. */ 1804 1805 if (TREE_CODE (t) != OVERLOAD) 1806 /* The code in instantiate_type which will process this 1807 expects to encounter OVERLOADs, not raw functions. */ 1808 t = ovl_cons (t, NULL_TREE); 1809 1810 return build (OFFSET_REF, 1811 unknown_type_node, 1812 decl, 1813 build (TEMPLATE_ID_EXPR, 1814 TREE_TYPE (t), 1815 t, 1816 TREE_OPERAND (orig_name, 1))); 1817 } 1818 1819 if (!really_overloaded_fn (t)) 1820 { 1821 /* Get rid of a potential OVERLOAD around it */ 1822 t = OVL_CURRENT (t); 1823 1824 /* unique functions are handled easily. */ 1825 basebinfo = TREE_PURPOSE (fnfields); 1826 if (!enforce_access (basebinfo, t)) 1827 return error_mark_node; 1828 mark_used (t); 1829 if (DECL_STATIC_FUNCTION_P (t)) 1830 return t; 1831 return build (OFFSET_REF, TREE_TYPE (t), decl, t); 1832 } 1833 1834 /* FNFIELDS is most likely allocated on the search_obstack, 1835 which will go away after this class scope. If we need 1836 to save this value for later (i.e. for use as an initializer 1837 for a static variable), then do so here. 1838 1839 ??? The smart thing to do for the case of saving initializers 1840 is to resolve them before we're done with this scope. */ 1841 if (!TREE_PERMANENT (fnfields) 1842 && ! allocation_temporary_p ()) 1843 fnfields = copy_list (fnfields); 1844 1845 TREE_TYPE (fnfields) = unknown_type_node; 1846 return build (OFFSET_REF, unknown_type_node, decl, fnfields); 1847 } 1848 1849 t = member; 1850 1851 if (t == NULL_TREE) 1852 { 1853 cp_error ("`%D' is not a member of type `%T'", name, type); 1854 return error_mark_node; 1855 } 1856 1857 if (TREE_CODE (t) == TYPE_DECL) 1858 { 1859 TREE_USED (t) = 1; 1860 return t; 1861 } 1862 /* static class members and class-specific enum 1863 values can be returned without further ado. */ 1864 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL) 1865 { 1866 mark_used (t); 1867 return convert_from_reference (t); 1868 } 1869 1870 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t)) 1871 { 1872 cp_error ("illegal pointer to bit field `%D'", t); 1873 return error_mark_node; 1874 } 1875 1876 /* static class functions too. */ 1877 if (TREE_CODE (t) == FUNCTION_DECL 1878 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE) 1879 my_friendly_abort (53); 1880 1881 /* In member functions, the form `type::name' is no longer 1882 equivalent to `this->type::name', at least not until 1883 resolve_offset_ref. */ 1884 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t); 1885} 1886 1887/* If a OFFSET_REF made it through to here, then it did 1888 not have its address taken. */ 1889 1890tree 1891resolve_offset_ref (exp) 1892 tree exp; 1893{ 1894 tree type = TREE_TYPE (exp); 1895 tree base = NULL_TREE; 1896 tree member; 1897 tree basetype, addr; 1898 1899 if (TREE_CODE (exp) == OFFSET_REF) 1900 { 1901 member = TREE_OPERAND (exp, 1); 1902 base = TREE_OPERAND (exp, 0); 1903 } 1904 else 1905 { 1906 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214); 1907 if (TYPE_OFFSET_BASETYPE (type) != current_class_type) 1908 { 1909 error ("object missing in use of pointer-to-member construct"); 1910 return error_mark_node; 1911 } 1912 member = exp; 1913 type = TREE_TYPE (type); 1914 base = current_class_ref; 1915 } 1916 1917 if (BASELINK_P (member)) 1918 { 1919 cp_pedwarn ("assuming & on overloaded member function"); 1920 return build_unary_op (ADDR_EXPR, exp, 0); 1921 } 1922 1923 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE) 1924 { 1925 cp_pedwarn ("assuming & on `%E'", member); 1926 return build_unary_op (ADDR_EXPR, exp, 0); 1927 } 1928 1929 if ((TREE_CODE (member) == VAR_DECL 1930 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)) 1931 && ! TYPE_PTRMEM_P (TREE_TYPE (member))) 1932 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE) 1933 { 1934 /* These were static members. */ 1935 if (mark_addressable (member) == 0) 1936 return error_mark_node; 1937 return member; 1938 } 1939 1940 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE 1941 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE) 1942 return member; 1943 1944 /* Syntax error can cause a member which should 1945 have been seen as static to be grok'd as non-static. */ 1946 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE) 1947 { 1948 if (TREE_ADDRESSABLE (member) == 0) 1949 { 1950 cp_error_at ("member `%D' is non-static but referenced as a static member", 1951 member); 1952 error ("at this point in file"); 1953 TREE_ADDRESSABLE (member) = 1; 1954 } 1955 return error_mark_node; 1956 } 1957 1958 /* The first case is really just a reference to a member of `this'. */ 1959 if (TREE_CODE (member) == FIELD_DECL 1960 && (base == current_class_ref || is_dummy_object (base))) 1961 { 1962 tree basetype_path; 1963 tree expr; 1964 1965 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE) 1966 basetype = TYPE_OFFSET_BASETYPE (type); 1967 else 1968 basetype = DECL_CONTEXT (member); 1969 1970 base = current_class_ptr; 1971 1972 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0) 1973 { 1974 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base))); 1975 return error_mark_node; 1976 } 1977 /* Kludge: we need to use basetype_path now, because 1978 convert_pointer_to will bash it. */ 1979 enforce_access (basetype_path, member); 1980 addr = convert_pointer_to (basetype, base); 1981 1982 /* Even in the case of illegal access, we form the 1983 COMPONENT_REF; that will allow better error recovery than 1984 just feeding back error_mark_node. */ 1985 expr = build (COMPONENT_REF, TREE_TYPE (member), 1986 build_indirect_ref (addr, NULL_PTR), member); 1987 return convert_from_reference (expr); 1988 } 1989 1990 /* Ensure that we have an object. */ 1991 if (is_dummy_object (base)) 1992 addr = error_mark_node; 1993 else 1994 /* If this is a reference to a member function, then return the 1995 address of the member function (which may involve going 1996 through the object's vtable), otherwise, return an expression 1997 for the dereferenced pointer-to-member construct. */ 1998 addr = build_unary_op (ADDR_EXPR, base, 0); 1999 2000 if (TYPE_PTRMEM_P (TREE_TYPE (member))) 2001 { 2002 if (addr == error_mark_node) 2003 { 2004 cp_error ("object missing in `%E'", exp); 2005 return error_mark_node; 2006 } 2007 2008 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member))); 2009 addr = convert_pointer_to (basetype, addr); 2010 member = cp_convert (ptrdiff_type_node, member); 2011 2012 /* Pointer to data members are offset by one, so that a null 2013 pointer with a real value of 0 is distinguishable from an 2014 offset of the first member of a structure. */ 2015 member = build_binary_op (MINUS_EXPR, member, 2016 cp_convert (ptrdiff_type_node, integer_one_node)); 2017 2018 return build1 (INDIRECT_REF, type, 2019 build (PLUS_EXPR, build_pointer_type (type), 2020 addr, member)); 2021 } 2022 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member))) 2023 { 2024 return get_member_function_from_ptrfunc (&addr, member); 2025 } 2026 my_friendly_abort (56); 2027 /* NOTREACHED */ 2028 return NULL_TREE; 2029} 2030 2031/* Return either DECL or its known constant value (if it has one). */ 2032 2033tree 2034decl_constant_value (decl) 2035 tree decl; 2036{ 2037 if (! TREE_THIS_VOLATILE (decl) 2038 && DECL_INITIAL (decl) 2039 && DECL_INITIAL (decl) != error_mark_node 2040 /* This is invalid if initial value is not constant. 2041 If it has either a function call, a memory reference, 2042 or a variable, then re-evaluating it could give different results. */ 2043 && TREE_CONSTANT (DECL_INITIAL (decl)) 2044 /* Check for cases where this is sub-optimal, even though valid. */ 2045 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) 2046 return DECL_INITIAL (decl); 2047 return decl; 2048} 2049 2050/* Common subroutines of build_new and build_vec_delete. */ 2051 2052/* Call the global __builtin_delete to delete ADDR. */ 2053 2054static tree 2055build_builtin_delete_call (addr) 2056 tree addr; 2057{ 2058 mark_used (global_delete_fndecl); 2059 return build_call (global_delete_fndecl, 2060 void_type_node, build_expr_list (NULL_TREE, addr)); 2061} 2062 2063/* Generate a C++ "new" expression. DECL is either a TREE_LIST 2064 (which needs to go through some sort of groktypename) or it 2065 is the name of the class we are newing. INIT is an initialization value. 2066 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces. 2067 If INIT is void_type_node, it means do *not* call a constructor 2068 for this instance. 2069 2070 For types with constructors, the data returned is initialized 2071 by the appropriate constructor. 2072 2073 Whether the type has a constructor or not, if it has a pointer 2074 to a virtual function table, then that pointer is set up 2075 here. 2076 2077 Unless I am mistaken, a call to new () will return initialized 2078 data regardless of whether the constructor itself is private or 2079 not. NOPE; new fails if the constructor is private (jcm). 2080 2081 Note that build_new does nothing to assure that any special 2082 alignment requirements of the type are met. Rather, it leaves 2083 it up to malloc to do the right thing. Otherwise, folding to 2084 the right alignment cal cause problems if the user tries to later 2085 free the memory returned by `new'. 2086 2087 PLACEMENT is the `placement' list for user-defined operator new (). */ 2088 2089extern int flag_check_new; 2090 2091tree 2092build_new (placement, decl, init, use_global_new) 2093 tree placement; 2094 tree decl, init; 2095 int use_global_new; 2096{ 2097 tree type, rval; 2098 tree nelts = NULL_TREE, t; 2099 int has_array = 0; 2100 2101 tree pending_sizes = NULL_TREE; 2102 2103 if (decl == error_mark_node) 2104 return error_mark_node; 2105 2106 if (TREE_CODE (decl) == TREE_LIST) 2107 { 2108 tree absdcl = TREE_VALUE (decl); 2109 tree last_absdcl = NULL_TREE; 2110 int old_immediate_size_expand = 0; 2111 2112 if (current_function_decl 2113 && DECL_CONSTRUCTOR_P (current_function_decl)) 2114 { 2115 old_immediate_size_expand = immediate_size_expand; 2116 immediate_size_expand = 0; 2117 } 2118 2119 nelts = integer_one_node; 2120 2121 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR) 2122 my_friendly_abort (215); 2123 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF) 2124 { 2125 last_absdcl = absdcl; 2126 absdcl = TREE_OPERAND (absdcl, 0); 2127 } 2128 2129 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF) 2130 { 2131 /* probably meant to be a vec new */ 2132 tree this_nelts; 2133 2134 while (TREE_OPERAND (absdcl, 0) 2135 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF) 2136 { 2137 last_absdcl = absdcl; 2138 absdcl = TREE_OPERAND (absdcl, 0); 2139 } 2140 2141 has_array = 1; 2142 this_nelts = TREE_OPERAND (absdcl, 1); 2143 if (this_nelts != error_mark_node) 2144 { 2145 if (this_nelts == NULL_TREE) 2146 error ("new of array type fails to specify size"); 2147 else if (processing_template_decl) 2148 { 2149 nelts = this_nelts; 2150 absdcl = TREE_OPERAND (absdcl, 0); 2151 } 2152 else 2153 { 2154 int flags = pedantic ? WANT_INT : (WANT_INT | WANT_ENUM); 2155 if (build_expr_type_conversion (flags, this_nelts, 0) 2156 == NULL_TREE) 2157 pedwarn ("size in array new must have integral type"); 2158 2159 this_nelts = save_expr (cp_convert (sizetype, this_nelts)); 2160 absdcl = TREE_OPERAND (absdcl, 0); 2161 if (this_nelts == integer_zero_node) 2162 { 2163 warning ("zero size array reserves no space"); 2164 nelts = integer_zero_node; 2165 } 2166 else 2167 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts); 2168 } 2169 } 2170 else 2171 nelts = integer_zero_node; 2172 } 2173 2174 if (last_absdcl) 2175 TREE_OPERAND (last_absdcl, 0) = absdcl; 2176 else 2177 TREE_VALUE (decl) = absdcl; 2178 2179 type = groktypename (decl); 2180 if (! type || type == error_mark_node) 2181 { 2182 immediate_size_expand = old_immediate_size_expand; 2183 return error_mark_node; 2184 } 2185 2186 if (current_function_decl 2187 && DECL_CONSTRUCTOR_P (current_function_decl)) 2188 { 2189 pending_sizes = get_pending_sizes (); 2190 immediate_size_expand = old_immediate_size_expand; 2191 } 2192 } 2193 else if (TREE_CODE (decl) == IDENTIFIER_NODE) 2194 { 2195 if (IDENTIFIER_HAS_TYPE_VALUE (decl)) 2196 { 2197 /* An aggregate type. */ 2198 type = IDENTIFIER_TYPE_VALUE (decl); 2199 decl = TYPE_MAIN_DECL (type); 2200 } 2201 else 2202 { 2203 /* A builtin type. */ 2204 decl = lookup_name (decl, 1); 2205 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215); 2206 type = TREE_TYPE (decl); 2207 } 2208 } 2209 else if (TREE_CODE (decl) == TYPE_DECL) 2210 { 2211 type = TREE_TYPE (decl); 2212 } 2213 else 2214 { 2215 type = decl; 2216 decl = TYPE_MAIN_DECL (type); 2217 } 2218 2219 if (processing_template_decl) 2220 { 2221 if (has_array) 2222 t = min_tree_cons (min_tree_cons (NULL_TREE, type, NULL_TREE), 2223 build_min_nt (ARRAY_REF, NULL_TREE, nelts), 2224 NULL_TREE); 2225 else 2226 t = type; 2227 2228 rval = build_min_nt (NEW_EXPR, placement, t, init); 2229 NEW_EXPR_USE_GLOBAL (rval) = use_global_new; 2230 return rval; 2231 } 2232 2233 /* ``A reference cannot be created by the new operator. A reference 2234 is not an object (8.2.2, 8.4.3), so a pointer to it could not be 2235 returned by new.'' ARM 5.3.3 */ 2236 if (TREE_CODE (type) == REFERENCE_TYPE) 2237 { 2238 error ("new cannot be applied to a reference type"); 2239 type = TREE_TYPE (type); 2240 } 2241 2242 if (TREE_CODE (type) == FUNCTION_TYPE) 2243 { 2244 error ("new cannot be applied to a function type"); 2245 return error_mark_node; 2246 } 2247 2248 /* When the object being created is an array, the new-expression yields a 2249 pointer to the initial element (if any) of the array. For example, 2250 both new int and new int[10] return an int*. 5.3.4. */ 2251 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0) 2252 { 2253 nelts = array_type_nelts_top (type); 2254 has_array = 1; 2255 type = TREE_TYPE (type); 2256 } 2257 2258 if (has_array) 2259 t = build_nt (ARRAY_REF, type, nelts); 2260 else 2261 t = type; 2262 2263 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init); 2264 NEW_EXPR_USE_GLOBAL (rval) = use_global_new; 2265 TREE_SIDE_EFFECTS (rval) = 1; 2266 2267 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */ 2268 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval); 2269 TREE_NO_UNUSED_WARNING (rval) = 1; 2270 2271 if (pending_sizes) 2272 rval = build_compound_expr (chainon (pending_sizes, 2273 build_expr_list (NULL_TREE, rval))); 2274 2275 return rval; 2276} 2277 2278/* If non-NULL, a POINTER_TYPE equivalent to (java::lang::Class*). */ 2279 2280static tree jclass_node = NULL_TREE; 2281 2282/* Given a Java class, return a decl for the corresponding java.lang.Class. */ 2283 2284static tree 2285build_java_class_ref (type) 2286 tree type; 2287{ 2288 tree name, class_decl; 2289 static tree CL_prefix = NULL_TREE; 2290 if (CL_prefix == NULL_TREE) 2291 CL_prefix = get_identifier("_CL_"); 2292 if (jclass_node == NULL_TREE) 2293 { 2294 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass")); 2295 if (jclass_node == NULL_TREE) 2296 fatal("call to Java constructor, while `jclass' undefined"); 2297 jclass_node = TREE_TYPE (jclass_node); 2298 } 2299 name = build_overload_with_type (CL_prefix, type); 2300 class_decl = IDENTIFIER_GLOBAL_VALUE (name); 2301 if (class_decl == NULL_TREE) 2302 { 2303 push_obstacks_nochange (); 2304 end_temporary_allocation (); 2305 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node)); 2306 TREE_STATIC (class_decl) = 1; 2307 DECL_EXTERNAL (class_decl) = 1; 2308 TREE_PUBLIC (class_decl) = 1; 2309 DECL_ARTIFICIAL (class_decl) = 1; 2310 DECL_IGNORED_P (class_decl) = 1; 2311 pushdecl_top_level (class_decl); 2312 make_decl_rtl (class_decl, NULL_PTR, 1); 2313 pop_obstacks (); 2314 } 2315 return class_decl; 2316} 2317 2318/* Called from cplus_expand_expr when expanding a NEW_EXPR. The return 2319 value is immediately handed to expand_expr. */ 2320 2321tree 2322build_new_1 (exp) 2323 tree exp; 2324{ 2325 tree placement, init; 2326 tree type, true_type, size, rval; 2327 tree nelts = NULL_TREE; 2328 tree alloc_expr, alloc_node = NULL_TREE; 2329 int has_array = 0; 2330 enum tree_code code = NEW_EXPR; 2331 int use_cookie, nothrow, check_new; 2332 int use_global_new; 2333 int use_java_new = 0; 2334 2335 placement = TREE_OPERAND (exp, 0); 2336 type = TREE_OPERAND (exp, 1); 2337 init = TREE_OPERAND (exp, 2); 2338 use_global_new = NEW_EXPR_USE_GLOBAL (exp); 2339 2340 if (TREE_CODE (type) == ARRAY_REF) 2341 { 2342 has_array = 1; 2343 nelts = TREE_OPERAND (type, 1); 2344 type = TREE_OPERAND (type, 0); 2345 } 2346 true_type = type; 2347 2348 if (CP_TYPE_QUALS (type)) 2349 type = TYPE_MAIN_VARIANT (type); 2350 2351 /* If our base type is an array, then make sure we know how many elements 2352 it has. */ 2353 while (TREE_CODE (true_type) == ARRAY_TYPE) 2354 { 2355 tree this_nelts = array_type_nelts_top (true_type); 2356 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts); 2357 true_type = TREE_TYPE (true_type); 2358 } 2359 2360 if (!complete_type_or_else (true_type, exp)) 2361 return error_mark_node; 2362 2363 if (has_array) 2364 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type), 2365 nelts)); 2366 else 2367 size = size_in_bytes (type); 2368 2369 if (TREE_CODE (true_type) == VOID_TYPE) 2370 { 2371 error ("invalid type `void' for new"); 2372 return error_mark_node; 2373 } 2374 2375 if (TYPE_LANG_SPECIFIC (true_type) 2376 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type)) 2377 { 2378 abstract_virtuals_error (NULL_TREE, true_type); 2379 return error_mark_node; 2380 } 2381 2382 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type)) 2383 { 2384 signature_error (NULL_TREE, true_type); 2385 return error_mark_node; 2386 } 2387 2388 /* When we allocate an array, and the corresponding deallocation 2389 function takes a second argument of type size_t, and that's the 2390 "usual deallocation function", we allocate some extra space at 2391 the beginning of the array to store the size of the array. 2392 2393 Well, that's what we should do. For backwards compatibility, we 2394 have to do this whenever there's a two-argument array-delete 2395 operator. 2396 2397 FIXME: For -fnew-abi, we don't have to maintain backwards 2398 compatibility and we should fix this. */ 2399 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type) 2400 && ! (placement && ! TREE_CHAIN (placement) 2401 && TREE_TYPE (TREE_VALUE (placement)) == ptr_type_node)); 2402 2403 if (use_cookie) 2404 { 2405 tree extra = BI_header_size; 2406 2407 size = size_binop (PLUS_EXPR, size, extra); 2408 } 2409 2410 if (has_array) 2411 { 2412 code = VEC_NEW_EXPR; 2413 2414 if (init && pedantic) 2415 cp_pedwarn ("initialization in array new"); 2416 } 2417 2418 /* Allocate the object. */ 2419 2420 if (! has_array && ! placement && flag_this_is_variable > 0 2421 && TYPE_NEEDS_CONSTRUCTING (true_type) && init != void_type_node) 2422 { 2423 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST) 2424 rval = NULL_TREE; 2425 else 2426 { 2427 error ("constructors take parameter lists"); 2428 return error_mark_node; 2429 } 2430 } 2431 else if (! placement && TYPE_FOR_JAVA (true_type)) 2432 { 2433 tree class_addr, alloc_decl; 2434 tree class_decl = build_java_class_ref (true_type); 2435 tree class_size = size_in_bytes (true_type); 2436 static char alloc_name[] = "_Jv_AllocObject"; 2437 use_java_new = 1; 2438 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name)); 2439 if (alloc_decl == NULL_TREE) 2440 fatal("call to Java constructor, while `%s' undefined", alloc_name); 2441 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl); 2442 rval = build_function_call (alloc_decl, 2443 tree_cons (NULL_TREE, class_addr, 2444 build_tree_list (NULL_TREE, 2445 class_size))); 2446 rval = cp_convert (build_pointer_type (true_type), rval); 2447 } 2448 else 2449 { 2450 int susp = 0; 2451 2452 if (flag_exceptions) 2453 /* We will use RVAL when generating an exception handler for 2454 this new-expression, so we must save it. */ 2455 susp = suspend_momentary (); 2456 2457 rval = build_op_new_call 2458 (code, true_type, expr_tree_cons (NULL_TREE, size, placement), 2459 LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL)); 2460 rval = cp_convert (build_pointer_type (true_type), rval); 2461 2462 if (flag_exceptions) 2463 resume_momentary (susp); 2464 } 2465 2466 /* unless an allocation function is declared with an empty excep- 2467 tion-specification (_except.spec_), throw(), it indicates failure to 2468 allocate storage by throwing a bad_alloc exception (clause _except_, 2469 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo- 2470 cation function is declared with an empty exception-specification, 2471 throw(), it returns null to indicate failure to allocate storage and a 2472 non-null pointer otherwise. 2473 2474 So check for a null exception spec on the op new we just called. */ 2475 2476 nothrow = 0; 2477 if (rval) 2478 { 2479 /* The CALL_EXPR. */ 2480 tree t = TREE_OPERAND (rval, 0); 2481 /* The function. */ 2482 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0); 2483 nothrow = TYPE_NOTHROW_P (TREE_TYPE (t)); 2484 } 2485 check_new = (flag_check_new || nothrow) && ! use_java_new; 2486 2487 if ((check_new || flag_exceptions) && rval) 2488 { 2489 alloc_expr = get_target_expr (rval); 2490 alloc_node = rval = TREE_OPERAND (alloc_expr, 0); 2491 } 2492 else 2493 alloc_expr = NULL_TREE; 2494 2495 /* if rval is NULL_TREE I don't have to allocate it, but are we totally 2496 sure we have some extra bytes in that case for the BI_header_size 2497 cookies? And how does that interact with the code below? (mrs) */ 2498 /* Finish up some magic for new'ed arrays */ 2499 if (use_cookie && rval != NULL_TREE) 2500 { 2501 tree extra = BI_header_size; 2502 tree cookie, exp1; 2503 rval = convert (string_type_node, rval); /* for ptr arithmetic */ 2504 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra)); 2505 /* Store header info. */ 2506 cookie = build_indirect_ref (build (MINUS_EXPR, 2507 build_pointer_type (BI_header_type), 2508 rval, extra), NULL_PTR); 2509 exp1 = build (MODIFY_EXPR, void_type_node, 2510 build_component_ref (cookie, nc_nelts_field_id, 2511 NULL_TREE, 0), 2512 nelts); 2513 TREE_SIDE_EFFECTS (exp1) = 1; 2514 rval = cp_convert (build_pointer_type (true_type), rval); 2515 rval = build_compound_expr 2516 (expr_tree_cons (NULL_TREE, exp1, 2517 build_expr_list (NULL_TREE, rval))); 2518 } 2519 2520 if (rval == error_mark_node) 2521 return error_mark_node; 2522 2523 /* Don't call any constructors or do any initialization. */ 2524 if (init == void_type_node) 2525 goto done; 2526 2527 if (TYPE_NEEDS_CONSTRUCTING (type) || init) 2528 { 2529 if (! TYPE_NEEDS_CONSTRUCTING (type) 2530 && ! IS_AGGR_TYPE (type) && ! has_array) 2531 { 2532 /* We are processing something like `new int (10)', which 2533 means allocate an int, and initialize it with 10. */ 2534 tree deref; 2535 tree deref_type; 2536 2537 /* At present RVAL is a temporary variable, created to hold 2538 the value from the call to `operator new'. We transform 2539 it to (*RVAL = INIT, RVAL). */ 2540 rval = save_expr (rval); 2541 deref = build_indirect_ref (rval, NULL_PTR); 2542 2543 /* Even for something like `new const int (10)' we must 2544 allow the expression to be non-const while we do the 2545 initialization. */ 2546 deref_type = TREE_TYPE (deref); 2547 if (CP_TYPE_CONST_P (deref_type)) 2548 TREE_TYPE (deref) 2549 = cp_build_qualified_type (deref_type, 2550 CP_TYPE_QUALS (deref_type) 2551 & ~TYPE_QUAL_CONST); 2552 TREE_READONLY (deref) = 0; 2553 2554 if (TREE_CHAIN (init) != NULL_TREE) 2555 pedwarn ("initializer list being treated as compound expression"); 2556 else if (TREE_CODE (init) == CONSTRUCTOR) 2557 { 2558 pedwarn ("initializer list appears where operand should be used"); 2559 init = TREE_OPERAND (init, 1); 2560 } 2561 init = build_compound_expr (init); 2562 2563 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL, 2564 "new", NULL_TREE, 0); 2565 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), 2566 build_modify_expr (deref, NOP_EXPR, init), 2567 rval); 2568 TREE_NO_UNUSED_WARNING (rval) = 1; 2569 TREE_SIDE_EFFECTS (rval) = 1; 2570 } 2571 else if (! has_array) 2572 { 2573 tree newrval; 2574 /* Constructors are never virtual. If it has an initialization, we 2575 need to complain if we aren't allowed to use the ctor that took 2576 that argument. */ 2577 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN; 2578 2579 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type)) 2580 { 2581 if (TYPE_USES_PVBASES (true_type) 2582 && !flag_vtable_thunks_compat) 2583 { 2584 init = expr_tree_cons (NULL_TREE, vlist_zero_node, init); 2585 flags |= LOOKUP_HAS_VLIST; 2586 } 2587 init = expr_tree_cons (NULL_TREE, integer_one_node, init); 2588 flags |= LOOKUP_HAS_IN_CHARGE; 2589 } 2590 2591 if (use_java_new) 2592 rval = save_expr (rval); 2593 newrval = rval; 2594 2595 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE) 2596 newrval = build_indirect_ref (newrval, NULL_PTR); 2597 2598 newrval = build_method_call (newrval, ctor_identifier, 2599 init, TYPE_BINFO (true_type), flags); 2600 2601 if (newrval == NULL_TREE || newrval == error_mark_node) 2602 return error_mark_node; 2603 2604 /* Java constructors compiled by jc1 do not return this. */ 2605 if (use_java_new) 2606 newrval = build (COMPOUND_EXPR, TREE_TYPE (newrval), 2607 newrval, rval); 2608 rval = newrval; 2609 TREE_HAS_CONSTRUCTOR (rval) = 1; 2610 } 2611 else 2612 rval = build (VEC_INIT_EXPR, TREE_TYPE (rval), 2613 save_expr (rval), init, nelts); 2614 2615 /* If any part of the object initialization terminates by throwing an 2616 exception and a suitable deallocation function can be found, the 2617 deallocation function is called to free the memory in which the 2618 object was being constructed, after which the exception continues 2619 to propagate in the context of the new-expression. If no 2620 unambiguous matching deallocation function can be found, 2621 propagating the exception does not cause the object's memory to be 2622 freed. */ 2623 if (flag_exceptions && alloc_expr && ! use_java_new) 2624 { 2625 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR; 2626 tree cleanup, fn = NULL_TREE; 2627 int flags = LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL); 2628 2629 /* All cleanups must last longer than normal. */ 2630 int yes = suspend_momentary (); 2631 2632 if (placement) 2633 { 2634 flags |= LOOKUP_SPECULATIVELY; 2635 2636 /* We expect alloc_expr to look like a TARGET_EXPR around 2637 a NOP_EXPR around the CALL_EXPR we want. */ 2638 fn = TREE_OPERAND (alloc_expr, 1); 2639 fn = TREE_OPERAND (fn, 0); 2640 } 2641 2642 /* Copy size to the saveable obstack. */ 2643 size = mapcar (size, permanent_p); 2644 2645 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn); 2646 2647 resume_momentary (yes); 2648 2649 /* Ack! First we allocate the memory. Then we set our sentry 2650 variable to true, and expand a cleanup that deletes the memory 2651 if sentry is true. Then we run the constructor and store the 2652 returned pointer in buf. Then we clear sentry and return buf. */ 2653 2654 if (cleanup) 2655 { 2656 tree end, sentry, begin, buf, t = TREE_TYPE (rval); 2657 2658 begin = get_target_expr (boolean_true_node); 2659 sentry = TREE_OPERAND (begin, 0); 2660 2661 yes = suspend_momentary (); 2662 TREE_OPERAND (begin, 2) 2663 = build (COND_EXPR, void_type_node, sentry, 2664 cleanup, void_zero_node); 2665 resume_momentary (yes); 2666 2667 rval = get_target_expr (rval); 2668 2669 end = build (MODIFY_EXPR, TREE_TYPE (sentry), 2670 sentry, boolean_false_node); 2671 TREE_SIDE_EFFECTS (end) = 1; 2672 2673 buf = TREE_OPERAND (rval, 0); 2674 2675 rval = build (COMPOUND_EXPR, t, begin, 2676 build (COMPOUND_EXPR, t, rval, 2677 build (COMPOUND_EXPR, t, end, buf))); 2678 } 2679 } 2680 } 2681 else if (CP_TYPE_CONST_P (true_type)) 2682 cp_error ("uninitialized const in `new' of `%#T'", true_type); 2683 2684 done: 2685 2686 if (alloc_expr && rval == alloc_node) 2687 { 2688 rval = TREE_OPERAND (alloc_expr, 1); 2689 alloc_expr = NULL_TREE; 2690 } 2691 2692 if (check_new && alloc_expr) 2693 { 2694 /* Did we modify the storage? */ 2695 tree ifexp = build_binary_op (NE_EXPR, alloc_node, 2696 integer_zero_node); 2697 rval = build_conditional_expr (ifexp, rval, alloc_node); 2698 } 2699 2700 if (alloc_expr) 2701 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval); 2702 2703 if (rval && TREE_TYPE (rval) != build_pointer_type (type)) 2704 { 2705 /* The type of new int [3][3] is not int *, but int [3] * */ 2706 rval = build_c_cast (build_pointer_type (type), rval); 2707 } 2708 2709 return rval; 2710} 2711 2712static tree 2713build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete, 2714 use_global_delete) 2715 tree base, maxindex, type; 2716 tree auto_delete_vec, auto_delete; 2717 int use_global_delete; 2718{ 2719 tree virtual_size; 2720 tree ptype = build_pointer_type (type = complete_type (type)); 2721 tree size_exp = size_in_bytes (type); 2722 2723 /* Temporary variables used by the loop. */ 2724 tree tbase, tbase_init; 2725 2726 /* This is the body of the loop that implements the deletion of a 2727 single element, and moves temp variables to next elements. */ 2728 tree body; 2729 2730 /* This is the LOOP_EXPR that governs the deletion of the elements. */ 2731 tree loop; 2732 2733 /* This is the thing that governs what to do after the loop has run. */ 2734 tree deallocate_expr = 0; 2735 2736 /* This is the BIND_EXPR which holds the outermost iterator of the 2737 loop. It is convenient to set this variable up and test it before 2738 executing any other code in the loop. 2739 This is also the containing expression returned by this function. */ 2740 tree controller = NULL_TREE; 2741 2742 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type)) 2743 { 2744 loop = integer_zero_node; 2745 goto no_destructor; 2746 } 2747 2748 /* The below is short by BI_header_size */ 2749 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex)); 2750 2751 tbase = build_decl (VAR_DECL, NULL_TREE, ptype); 2752 tbase_init = build_modify_expr (tbase, NOP_EXPR, 2753 fold (build (PLUS_EXPR, ptype, 2754 base, 2755 virtual_size))); 2756 DECL_REGISTER (tbase) = 1; 2757 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE); 2758 TREE_SIDE_EFFECTS (controller) = 1; 2759 2760 if (auto_delete != integer_zero_node 2761 && auto_delete != integer_two_node) 2762 { 2763 tree base_tbd = cp_convert (ptype, 2764 build_binary_op (MINUS_EXPR, 2765 cp_convert (ptr_type_node, base), 2766 BI_header_size)); 2767 /* This is the real size */ 2768 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size); 2769 body = build_expr_list (NULL_TREE, 2770 build_x_delete (base_tbd, 2771 2 | use_global_delete, 2772 virtual_size)); 2773 body = build (COND_EXPR, void_type_node, 2774 build (BIT_AND_EXPR, integer_type_node, 2775 auto_delete, integer_one_node), 2776 body, integer_zero_node); 2777 } 2778 else 2779 body = NULL_TREE; 2780 2781 body = expr_tree_cons (NULL_TREE, 2782 build_delete (ptype, tbase, auto_delete, 2783 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1), 2784 body); 2785 2786 body = expr_tree_cons (NULL_TREE, 2787 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)), 2788 body); 2789 2790 body = expr_tree_cons (NULL_TREE, 2791 build (EXIT_EXPR, void_type_node, 2792 build (EQ_EXPR, boolean_type_node, base, tbase)), 2793 body); 2794 2795 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body)); 2796 2797 loop = expr_tree_cons (NULL_TREE, tbase_init, 2798 expr_tree_cons (NULL_TREE, loop, NULL_TREE)); 2799 loop = build_compound_expr (loop); 2800 2801 no_destructor: 2802 /* If the delete flag is one, or anything else with the low bit set, 2803 delete the storage. */ 2804 if (auto_delete_vec == integer_zero_node) 2805 deallocate_expr = integer_zero_node; 2806 else 2807 { 2808 tree base_tbd; 2809 2810 /* The below is short by BI_header_size */ 2811 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex)); 2812 2813 if (! TYPE_VEC_NEW_USES_COOKIE (type)) 2814 /* no header */ 2815 base_tbd = base; 2816 else 2817 { 2818 base_tbd = cp_convert (ptype, 2819 build_binary_op (MINUS_EXPR, 2820 cp_convert (string_type_node, base), 2821 BI_header_size)); 2822 /* True size with header. */ 2823 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size); 2824 } 2825 deallocate_expr = build_x_delete (base_tbd, 2826 2 | use_global_delete, 2827 virtual_size); 2828 if (auto_delete_vec != integer_one_node) 2829 deallocate_expr = build (COND_EXPR, void_type_node, 2830 build (BIT_AND_EXPR, integer_type_node, 2831 auto_delete_vec, integer_one_node), 2832 deallocate_expr, integer_zero_node); 2833 } 2834 2835 if (loop && deallocate_expr != integer_zero_node) 2836 { 2837 body = expr_tree_cons (NULL_TREE, loop, 2838 expr_tree_cons (NULL_TREE, deallocate_expr, NULL_TREE)); 2839 body = build_compound_expr (body); 2840 } 2841 else 2842 body = loop; 2843 2844 /* Outermost wrapper: If pointer is null, punt. */ 2845 body = build (COND_EXPR, void_type_node, 2846 build (NE_EXPR, boolean_type_node, base, integer_zero_node), 2847 body, integer_zero_node); 2848 body = build1 (NOP_EXPR, void_type_node, body); 2849 2850 if (controller) 2851 { 2852 TREE_OPERAND (controller, 1) = body; 2853 return controller; 2854 } 2855 else 2856 return cp_convert (void_type_node, body); 2857} 2858 2859/* Protect the vector initialization with a try-block so that we can 2860 destroy the first few elements if constructing a later element 2861 causes an exception to be thrown. TYPE is the type of the array 2862 elements. */ 2863 2864static void 2865expand_vec_init_try_block (type) 2866 tree type; 2867{ 2868 if (!TYPE_NEEDS_DESTRUCTOR (type) || !flag_exceptions) 2869 return; 2870 2871 /* The code we generate looks like: 2872 2873 try { 2874 // Initialize the vector. 2875 } catch (...) { 2876 // Destory the elements that need destroying. 2877 throw; 2878 } 2879 2880 Here we're just beginning the `try'. */ 2881 2882 expand_eh_region_start (); 2883} 2884 2885/* Add code to destroy the array elements constructed so far if the 2886 construction of some element in the array causes an exception to be 2887 thrown. RVAL is the address of the last element in the array. 2888 TYPE is the type of the array elements. MAXINDEX is the maximum 2889 allowable index into the array. ITERATOR is an integer variable 2890 indicating how many elements remain to be constructed. */ 2891 2892static void 2893expand_vec_init_catch_clause (rval, type, maxindex, iterator) 2894 tree rval; 2895 tree type; 2896 tree maxindex; 2897 tree iterator; 2898{ 2899 tree e; 2900 tree cleanup; 2901 2902 if (!TYPE_NEEDS_DESTRUCTOR (type) || !flag_exceptions) 2903 return; 2904 2905 /* We have to ensure that this can live to the cleanup expansion 2906 time, since we know it is only ever needed once, generate code 2907 now. */ 2908 push_obstacks_nochange (); 2909 resume_temporary_allocation (); 2910 2911 cleanup = make_node (RTL_EXPR); 2912 TREE_TYPE (cleanup) = void_type_node; 2913 RTL_EXPR_RTL (cleanup) = const0_rtx; 2914 TREE_SIDE_EFFECTS (cleanup) = 1; 2915 do_pending_stack_adjust (); 2916 start_sequence_for_rtl_expr (cleanup); 2917 2918 e = build_vec_delete_1 (rval, 2919 build_binary_op (MINUS_EXPR, maxindex, 2920 iterator), 2921 type, 2922 /*auto_delete_vec=*/integer_zero_node, 2923 /*auto_delete=*/integer_zero_node, 2924 /*use_global_delete=*/0); 2925 expand_expr (e, const0_rtx, VOIDmode, EXPAND_NORMAL); 2926 2927 do_pending_stack_adjust (); 2928 RTL_EXPR_SEQUENCE (cleanup) = get_insns (); 2929 end_sequence (); 2930 cleanup = protect_with_terminate (cleanup); 2931 expand_eh_region_end (cleanup); 2932 pop_obstacks (); 2933} 2934 2935/* `expand_vec_init' performs initialization of a vector of aggregate 2936 types. 2937 2938 DECL is passed only for error reporting, and provides line number 2939 and source file name information. 2940 BASE is the space where the vector will be. 2941 MAXINDEX is the maximum index of the array (one less than the 2942 number of elements). 2943 INIT is the (possibly NULL) initializer. 2944 2945 FROM_ARRAY is 0 if we should init everything with INIT 2946 (i.e., every element initialized from INIT). 2947 FROM_ARRAY is 1 if we should index into INIT in parallel 2948 with initialization of DECL. 2949 FROM_ARRAY is 2 if we should index into INIT in parallel, 2950 but use assignment instead of initialization. */ 2951 2952tree 2953expand_vec_init (decl, base, maxindex, init, from_array) 2954 tree decl, base, maxindex, init; 2955 int from_array; 2956{ 2957 tree rval; 2958 tree base2 = NULL_TREE; 2959 tree type = TREE_TYPE (TREE_TYPE (base)); 2960 tree size; 2961 tree itype = NULL_TREE; 2962 tree iterator; 2963 int num_initialized_elts = 0; 2964 2965 maxindex = cp_convert (ptrdiff_type_node, maxindex); 2966 if (maxindex == error_mark_node) 2967 return error_mark_node; 2968 2969 if (current_function_decl == NULL_TREE) 2970 { 2971 rval = make_tree_vec (3); 2972 TREE_VEC_ELT (rval, 0) = base; 2973 TREE_VEC_ELT (rval, 1) = maxindex; 2974 TREE_VEC_ELT (rval, 2) = init; 2975 return rval; 2976 } 2977 2978 size = size_in_bytes (type); 2979 2980 base = default_conversion (base); 2981 base = cp_convert (build_pointer_type (type), base); 2982 rval = get_temp_regvar (build_pointer_type (type), base); 2983 base = get_temp_regvar (build_pointer_type (type), base); 2984 iterator = get_temp_regvar (ptrdiff_type_node, maxindex); 2985 2986 /* Protect the entire array initialization so that we can destroy 2987 the partially constructed array if an exception is thrown. */ 2988 expand_vec_init_try_block (type); 2989 2990 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR 2991 && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl)))) 2992 { 2993 /* Do non-default initialization resulting from brace-enclosed 2994 initializers. */ 2995 2996 tree elts; 2997 tree baseref = build1 (INDIRECT_REF, type, base); 2998 2999 from_array = 0; 3000 3001 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts)) 3002 { 3003 tree elt = TREE_VALUE (elts); 3004 3005 num_initialized_elts++; 3006 3007 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE) 3008 expand_aggr_init (baseref, elt, 0); 3009 else 3010 expand_assignment (baseref, elt, 0, 0); 3011 3012 expand_assignment (base, 3013 build (PLUS_EXPR, build_pointer_type (type), 3014 base, size), 3015 0, 0); 3016 expand_assignment (iterator, 3017 build (MINUS_EXPR, ptrdiff_type_node, 3018 iterator, integer_one_node), 3019 0, 0); 3020 } 3021 3022 /* Clear out INIT so that we don't get confused below. */ 3023 init = NULL_TREE; 3024 3025 if (obey_regdecls) 3026 use_variable (DECL_RTL (base)); 3027 } 3028 else if (from_array) 3029 { 3030 /* If initializing one array from another, initialize element by 3031 element. We rely upon the below calls the do argument 3032 checking. */ 3033 if (decl == NULL_TREE) 3034 { 3035 sorry ("initialization of array from dissimilar array type"); 3036 return error_mark_node; 3037 } 3038 if (init) 3039 { 3040 base2 = default_conversion (init); 3041 itype = TREE_TYPE (base2); 3042 base2 = get_temp_regvar (itype, base2); 3043 itype = TREE_TYPE (itype); 3044 } 3045 else if (TYPE_LANG_SPECIFIC (type) 3046 && TYPE_NEEDS_CONSTRUCTING (type) 3047 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) 3048 { 3049 error ("initializer ends prematurely"); 3050 return error_mark_node; 3051 } 3052 } 3053 3054 /* Now, default-initialize any remaining elements. We don't need to 3055 do that if a) the type does not need constructing, or b) we've 3056 already initialized all the elements. 3057 3058 We do need to keep going if we're copying an array. */ 3059 3060 if (from_array 3061 || (TYPE_NEEDS_CONSTRUCTING (type) 3062 && !(TREE_CODE (maxindex) == INTEGER_CST 3063 && num_initialized_elts == TREE_INT_CST_LOW (maxindex) + 1))) 3064 { 3065 /* If the ITERATOR is equal to -1, then we don't have to loop; 3066 we've already initialized all the elements. */ 3067 expand_start_cond (build (NE_EXPR, boolean_type_node, 3068 iterator, minus_one), 3069 0); 3070 3071 /* Otherwise, loop through the elements. */ 3072 expand_start_loop_continue_elsewhere (1); 3073 3074 /* The initialization of each array element is a full-expression. */ 3075 expand_start_target_temps (); 3076 3077 if (from_array) 3078 { 3079 tree to = build1 (INDIRECT_REF, type, base); 3080 tree from; 3081 3082 if (base2) 3083 from = build1 (INDIRECT_REF, itype, base2); 3084 else 3085 from = NULL_TREE; 3086 3087 if (from_array == 2) 3088 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from)); 3089 else if (TYPE_NEEDS_CONSTRUCTING (type)) 3090 expand_aggr_init (to, from, 0); 3091 else if (from) 3092 expand_assignment (to, from, 0, 0); 3093 else 3094 my_friendly_abort (57); 3095 } 3096 else if (TREE_CODE (type) == ARRAY_TYPE) 3097 { 3098 if (init != 0) 3099 sorry ("cannot initialize multi-dimensional array with initializer"); 3100 expand_vec_init (decl, 3101 build1 (NOP_EXPR, 3102 build_pointer_type (TREE_TYPE 3103 (type)), 3104 base), 3105 array_type_nelts (type), 0, 0); 3106 } 3107 else 3108 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0); 3109 3110 expand_assignment (base, 3111 build (PLUS_EXPR, build_pointer_type (type), 3112 base, size), 0, 0); 3113 if (base2) 3114 expand_assignment (base2, 3115 build (PLUS_EXPR, build_pointer_type (type), 3116 base2, size), 0, 0); 3117 3118 /* Cleanup any temporaries needed for the initial value. */ 3119 expand_end_target_temps (); 3120 3121 expand_loop_continue_here (); 3122 expand_exit_loop_if_false (0, build (NE_EXPR, boolean_type_node, 3123 build (PREDECREMENT_EXPR, 3124 ptrdiff_type_node, 3125 iterator, 3126 integer_one_node), 3127 minus_one)); 3128 3129 if (obey_regdecls) 3130 { 3131 use_variable (DECL_RTL (base)); 3132 if (base2) 3133 use_variable (DECL_RTL (base2)); 3134 } 3135 3136 expand_end_loop (); 3137 expand_end_cond (); 3138 } 3139 3140 /* Make sure to cleanup any partially constructed elements. */ 3141 expand_vec_init_catch_clause (rval, type, maxindex, iterator); 3142 3143 if (obey_regdecls) 3144 { 3145 use_variable (DECL_RTL (iterator)); 3146 use_variable (DECL_RTL (rval)); 3147 } 3148 3149 return rval; 3150} 3151 3152/* Free up storage of type TYPE, at address ADDR. 3153 3154 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type 3155 of pointer. 3156 3157 VIRTUAL_SIZE is the amount of storage that was allocated, and is 3158 used as the second argument to operator delete. It can include 3159 things like padding and magic size cookies. It has virtual in it, 3160 because if you have a base pointer and you delete through a virtual 3161 destructor, it should be the size of the dynamic object, not the 3162 static object, see Free Store 12.5 ANSI C++ WP. 3163 3164 This does not call any destructors. */ 3165 3166tree 3167build_x_delete (addr, which_delete, virtual_size) 3168 tree addr; 3169 int which_delete; 3170 tree virtual_size; 3171{ 3172 int use_global_delete = which_delete & 1; 3173 int use_vec_delete = !!(which_delete & 2); 3174 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR; 3175 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL); 3176 3177 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE); 3178} 3179 3180/* Generate a call to a destructor. TYPE is the type to cast ADDR to. 3181 ADDR is an expression which yields the store to be destroyed. 3182 AUTO_DELETE is nonzero if a call to DELETE should be made or not. 3183 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the 3184 virtual baseclasses. 3185 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate. 3186 3187 FLAGS is the logical disjunction of zero or more LOOKUP_ 3188 flags. See cp-tree.h for more info. 3189 3190 This function does not delete an object's virtual base classes. */ 3191 3192tree 3193build_delete (type, addr, auto_delete, flags, use_global_delete) 3194 tree type, addr; 3195 tree auto_delete; 3196 int flags; 3197 int use_global_delete; 3198{ 3199 tree member; 3200 tree expr; 3201 tree ref; 3202 3203 if (addr == error_mark_node) 3204 return error_mark_node; 3205 3206 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type 3207 set to `error_mark_node' before it gets properly cleaned up. */ 3208 if (type == error_mark_node) 3209 return error_mark_node; 3210 3211 type = TYPE_MAIN_VARIANT (type); 3212 3213 if (TREE_CODE (type) == POINTER_TYPE) 3214 { 3215 type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 3216 if (type != void_type_node && !complete_type_or_else (type, addr)) 3217 return error_mark_node; 3218 if (TREE_CODE (type) == ARRAY_TYPE) 3219 goto handle_array; 3220 if (! IS_AGGR_TYPE (type)) 3221 { 3222 /* Call the builtin operator delete. */ 3223 return build_builtin_delete_call (addr); 3224 } 3225 if (TREE_SIDE_EFFECTS (addr)) 3226 addr = save_expr (addr); 3227 3228 /* throw away const and volatile on target type of addr */ 3229 addr = convert_force (build_pointer_type (type), addr, 0); 3230 ref = build_indirect_ref (addr, NULL_PTR); 3231 } 3232 else if (TREE_CODE (type) == ARRAY_TYPE) 3233 { 3234 handle_array: 3235 if (TREE_SIDE_EFFECTS (addr)) 3236 addr = save_expr (addr); 3237 if (TYPE_DOMAIN (type) == NULL_TREE) 3238 { 3239 error ("unknown array size in delete"); 3240 return error_mark_node; 3241 } 3242 return build_vec_delete (addr, array_type_nelts (type), 3243 auto_delete, integer_zero_node, 3244 use_global_delete); 3245 } 3246 else 3247 { 3248 /* Don't check PROTECT here; leave that decision to the 3249 destructor. If the destructor is accessible, call it, 3250 else report error. */ 3251 addr = build_unary_op (ADDR_EXPR, addr, 0); 3252 if (TREE_SIDE_EFFECTS (addr)) 3253 addr = save_expr (addr); 3254 3255 if (TREE_CONSTANT (addr)) 3256 addr = convert_pointer_to (type, addr); 3257 else 3258 addr = convert_force (build_pointer_type (type), addr, 0); 3259 3260 ref = build_indirect_ref (addr, NULL_PTR); 3261 } 3262 3263 my_friendly_assert (IS_AGGR_TYPE (type), 220); 3264 3265 if (! TYPE_NEEDS_DESTRUCTOR (type)) 3266 { 3267 if (auto_delete == integer_zero_node) 3268 return void_zero_node; 3269 3270 return build_op_delete_call 3271 (DELETE_EXPR, addr, c_sizeof_nowarn (type), 3272 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL), 3273 NULL_TREE); 3274 } 3275 3276 /* Below, we will reverse the order in which these calls are made. 3277 If we have a destructor, then that destructor will take care 3278 of the base classes; otherwise, we must do that here. */ 3279 if (TYPE_HAS_DESTRUCTOR (type)) 3280 { 3281 tree passed_auto_delete; 3282 tree do_delete = NULL_TREE; 3283 tree ifexp; 3284 3285 if (use_global_delete) 3286 { 3287 tree cond = fold (build (BIT_AND_EXPR, integer_type_node, 3288 auto_delete, integer_one_node)); 3289 tree call = build_builtin_delete_call (addr); 3290 3291 cond = fold (build (COND_EXPR, void_type_node, cond, 3292 call, void_zero_node)); 3293 if (cond != void_zero_node) 3294 do_delete = cond; 3295 3296 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node, 3297 auto_delete, integer_two_node)); 3298 } 3299 else 3300 passed_auto_delete = auto_delete; 3301 3302 /* Maybe pass vlist pointer to destructor. */ 3303 if (TYPE_USES_PVBASES (type)) 3304 { 3305 /* Pass vlist_zero even if in backwards compatibility mode, 3306 as the extra argument should not hurt if it is not used. */ 3307 expr = build_expr_list (NULL_TREE, vlist_zero_node); 3308 flags |= LOOKUP_HAS_VLIST; 3309 } 3310 else 3311 expr = NULL_TREE; 3312 3313 expr = expr_tree_cons (NULL_TREE, passed_auto_delete, expr); 3314 3315 expr = build_method_call (ref, dtor_identifier, expr, 3316 NULL_TREE, flags); 3317 3318 if (do_delete) 3319 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete); 3320 3321 if (flags & LOOKUP_DESTRUCTOR) 3322 /* Explicit destructor call; don't check for null pointer. */ 3323 ifexp = integer_one_node; 3324 else 3325 /* Handle deleting a null pointer. */ 3326 ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node)); 3327 3328 if (ifexp != integer_one_node) 3329 expr = build (COND_EXPR, void_type_node, 3330 ifexp, expr, void_zero_node); 3331 3332 return expr; 3333 } 3334 else 3335 { 3336 /* We only get here from finish_function for a destructor. */ 3337 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type)); 3338 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; 3339 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE; 3340 tree exprstmt = NULL_TREE; 3341 tree parent_auto_delete = auto_delete; 3342 tree cond; 3343 3344 /* Set this again before we call anything, as we might get called 3345 recursively. */ 3346 TYPE_HAS_DESTRUCTOR (type) = 1; 3347 3348 /* If we have member delete or vbases, we call delete in 3349 finish_function. */ 3350 if (auto_delete == integer_zero_node) 3351 cond = NULL_TREE; 3352 else if (base_binfo == NULL_TREE 3353 || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))) 3354 { 3355 cond = build (COND_EXPR, void_type_node, 3356 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node), 3357 build_builtin_delete_call (addr), 3358 void_zero_node); 3359 } 3360 else 3361 cond = NULL_TREE; 3362 3363 if (cond) 3364 exprstmt = build_expr_list (NULL_TREE, cond); 3365 3366 if (base_binfo 3367 && ! TREE_VIA_VIRTUAL (base_binfo) 3368 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))) 3369 { 3370 tree this_auto_delete; 3371 3372 /* Should the base invoke delete? */ 3373 if (BINFO_OFFSET_ZEROP (base_binfo)) 3374 this_auto_delete = parent_auto_delete; 3375 else 3376 this_auto_delete = integer_zero_node; 3377 3378 expr = build_base_dtor_call (ref, base_binfo, this_auto_delete); 3379 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt); 3380 } 3381 3382 /* Take care of the remaining baseclasses. */ 3383 for (i = 1; i < n_baseclasses; i++) 3384 { 3385 base_binfo = TREE_VEC_ELT (binfos, i); 3386 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)) 3387 || TREE_VIA_VIRTUAL (base_binfo)) 3388 continue; 3389 3390 expr = build_base_dtor_call (ref, base_binfo, integer_zero_node); 3391 3392 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt); 3393 } 3394 3395 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member)) 3396 { 3397 if (TREE_CODE (member) != FIELD_DECL) 3398 continue; 3399 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member))) 3400 { 3401 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0); 3402 tree this_type = TREE_TYPE (member); 3403 expr = build_delete (this_type, this_member, integer_two_node, flags, 0); 3404 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt); 3405 } 3406 } 3407 3408 if (exprstmt) 3409 return build_compound_expr (exprstmt); 3410 /* Virtual base classes make this function do nothing. */ 3411 return void_zero_node; 3412 } 3413} 3414 3415/* For type TYPE, delete the virtual baseclass objects of DECL. */ 3416 3417tree 3418build_vbase_delete (type, decl) 3419 tree type, decl; 3420{ 3421 tree vbases = CLASSTYPE_VBASECLASSES (type); 3422 tree result = NULL_TREE; 3423 tree addr = build_unary_op (ADDR_EXPR, decl, 0); 3424 3425 my_friendly_assert (addr != error_mark_node, 222); 3426 3427 while (vbases) 3428 { 3429 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)), 3430 addr, 0); 3431 result = expr_tree_cons (NULL_TREE, 3432 build_delete (TREE_TYPE (this_addr), this_addr, 3433 integer_zero_node, 3434 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0), 3435 result); 3436 vbases = TREE_CHAIN (vbases); 3437 } 3438 return build_compound_expr (nreverse (result)); 3439} 3440 3441/* Build a C++ vector delete expression. 3442 MAXINDEX is the number of elements to be deleted. 3443 ELT_SIZE is the nominal size of each element in the vector. 3444 BASE is the expression that should yield the store to be deleted. 3445 This function expands (or synthesizes) these calls itself. 3446 AUTO_DELETE_VEC says whether the container (vector) should be deallocated. 3447 AUTO_DELETE say whether each item in the container should be deallocated. 3448 3449 This also calls delete for virtual baseclasses of elements of the vector. 3450 3451 Update: MAXINDEX is no longer needed. The size can be extracted from the 3452 start of the vector for pointers, and from the type for arrays. We still 3453 use MAXINDEX for arrays because it happens to already have one of the 3454 values we'd have to extract. (We could use MAXINDEX with pointers to 3455 confirm the size, and trap if the numbers differ; not clear that it'd 3456 be worth bothering.) */ 3457 3458tree 3459build_vec_delete (base, maxindex, auto_delete_vec, auto_delete, 3460 use_global_delete) 3461 tree base, maxindex; 3462 tree auto_delete_vec, auto_delete; 3463 int use_global_delete; 3464{ 3465 tree type; 3466 3467 if (TREE_CODE (base) == OFFSET_REF) 3468 base = resolve_offset_ref (base); 3469 3470 type = TREE_TYPE (base); 3471 3472 base = stabilize_reference (base); 3473 3474 /* Since we can use base many times, save_expr it. */ 3475 if (TREE_SIDE_EFFECTS (base)) 3476 base = save_expr (base); 3477 3478 if (TREE_CODE (type) == POINTER_TYPE) 3479 { 3480 /* Step back one from start of vector, and read dimension. */ 3481 tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type), 3482 base, BI_header_size); 3483 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR); 3484 maxindex = build_component_ref (cookie, nc_nelts_field_id, NULL_TREE, 0); 3485 do 3486 type = TREE_TYPE (type); 3487 while (TREE_CODE (type) == ARRAY_TYPE); 3488 } 3489 else if (TREE_CODE (type) == ARRAY_TYPE) 3490 { 3491 /* get the total number of things in the array, maxindex is a bad name */ 3492 maxindex = array_type_nelts_total (type); 3493 while (TREE_CODE (type) == ARRAY_TYPE) 3494 type = TREE_TYPE (type); 3495 base = build_unary_op (ADDR_EXPR, base, 1); 3496 } 3497 else 3498 { 3499 if (base != error_mark_node) 3500 error ("type to vector delete is neither pointer or array type"); 3501 return error_mark_node; 3502 } 3503 3504 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete, 3505 use_global_delete); 3506} 3507