1/* Handle initialization things in C++. |
2 Copyright (C) 1987, 89, 92-96, 1997 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 |
35extern void compiler_error (); |
36 37/* In C++, structures with well-defined constructors are initialized by 38 those constructors, unasked. CURRENT_BASE_INIT_LIST 39 holds a list of stmts for a BASE_INIT term in the grammar. 40 This list has one element for each base class which must be 41 initialized. The list elements are [basename, init], with 42 type basetype. This allows the possibly anachronistic form 43 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)" 44 where each successive term can be handed down the constructor 45 line. Perhaps this was not intended. */ 46tree current_base_init_list, current_member_init_list; 47 |
48static void expand_aggr_vbase_init_1 PROTO((tree, tree, tree, tree)); 49static void expand_aggr_vbase_init PROTO((tree, tree, tree, tree)); 50static void expand_aggr_init_1 PROTO((tree, tree, tree, tree, int, 51 int)); 52static void expand_default_init PROTO((tree, tree, tree, tree, int, 53 int)); 54static tree build_vec_delete_1 PROTO((tree, tree, tree, tree, tree, 55 int)); 56static void perform_member_init PROTO((tree, tree, tree, int)); 57static void sort_base_init PROTO((tree, tree *, tree *)); 58static tree build_builtin_call PROTO((tree, tree, tree)); 59static tree build_array_eh_cleanup PROTO((tree, tree, tree)); 60static int member_init_ok_or_else PROTO((tree, tree, char *)); |
61static void expand_virtual_init PROTO((tree, tree)); |
62static tree sort_member_init PROTO((tree)); 63static tree build_partial_cleanup_for PROTO((tree)); 64static tree initializing_context PROTO((tree)); |
65 |
66/* Cache _builtin_new and _builtin_delete exprs. */ 67static tree BIN, BID, BIVN, BIVD; 68 |
69/* Cache the identifier nodes for the magic field of a new cookie. */ |
70static tree nc_nelts_field_id; |
71 72static tree minus_one; 73 74/* Set up local variable for this file. MUST BE CALLED AFTER 75 INIT_DECL_PROCESSING. */ 76 |
77static tree BI_header_type, BI_header_size; |
78 79void init_init_processing () 80{ 81 tree fields[1]; 82 83 /* Define implicit `operator new' and `operator delete' functions. */ 84 BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR]))); 85 TREE_USED (TREE_OPERAND (BIN, 0)) = 0; --- 31 unchanged lines hidden (view full) --- 117 for all virtual table pointers in virtual base classes. REAL_BINFO 118 is used to find the BINFO_VTABLE that we initialize with. BINFO is 119 used for conversions of addr to subobjects. 120 121 BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo). 122 123 Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE 124 (addr))). */ |
125 |
126void 127expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr) 128 tree real_binfo, binfo, addr; 129 int init_self, can_elide; 130{ 131 tree real_binfos = BINFO_BASETYPES (real_binfo); 132 tree binfos = BINFO_BASETYPES (binfo); 133 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0; 134 135 for (i = 0; i < n_baselinks; i++) 136 { 137 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i); 138 tree base_binfo = TREE_VEC_ELT (binfos, i); |
139 int is_not_base_vtable 140 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo)); |
141 if (! TREE_VIA_VIRTUAL (real_base_binfo)) 142 expand_direct_vtbls_init (real_base_binfo, base_binfo, 143 is_not_base_vtable, can_elide, addr); 144 } 145#if 0 146 /* Before turning this on, make sure it is correct. */ 147 if (can_elide && ! BINFO_MODIFIED (binfo)) 148 return; 149#endif 150 /* Should we use something besides CLASSTYPE_VFIELDS? */ 151 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo))) 152 { 153 tree base_ptr = convert_pointer_to_real (binfo, addr); 154 expand_virtual_init (real_binfo, base_ptr); 155 } 156} 157 158/* 348 - 351 */ 159/* Subroutine of emit_base_init. */ |
160 |
161static void |
162perform_member_init (member, name, init, explicit) 163 tree member, name, init; |
164 int explicit; 165{ 166 tree decl; 167 tree type = TREE_TYPE (member); 168 |
169 expand_start_target_temps (); 170 |
171 if (TYPE_NEEDS_CONSTRUCTING (type) 172 || (init && TYPE_HAS_CONSTRUCTOR (type))) 173 { 174 /* Since `init' is already a TREE_LIST on the current_member_init_list, 175 only build it into one if we aren't already a list. */ 176 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST) |
177 init = build_expr_list (NULL_TREE, init); |
178 |
179 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit); |
180 181 if (explicit 182 && TREE_CODE (type) == ARRAY_TYPE 183 && init != NULL_TREE 184 && TREE_CHAIN (init) == NULL_TREE 185 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE) 186 { 187 /* Initialization of one array from another. */ --- 4 unchanged lines hidden (view full) --- 192 expand_aggr_init (decl, init, 0, 0); 193 } 194 else 195 { 196 if (init == NULL_TREE) 197 { 198 if (explicit) 199 { |
200 /* default-initialization. */ 201 if (AGGREGATE_TYPE_P (type)) 202 init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); 203 else if (TREE_CODE (type) == REFERENCE_TYPE) 204 { 205 cp_error ("default-initialization of `%#D', which has reference type", 206 member); 207 init = error_mark_node; 208 } 209 else 210 init = integer_zero_node; |
211 } 212 /* member traversal: note it leaves init NULL */ 213 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE) 214 cp_pedwarn ("uninitialized reference member `%D'", member); 215 } 216 else if (TREE_CODE (init) == TREE_LIST) 217 { 218 /* There was an explicit member initialization. Do some --- 6 unchanged lines hidden (view full) --- 225 else 226 init = TREE_VALUE (init); 227 } 228 229 /* We only build this with a null init if we got it from the 230 current_member_init_list. */ 231 if (init || explicit) 232 { |
233 decl = build_component_ref (current_class_ref, name, NULL_TREE, 234 explicit); |
235 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init)); 236 } 237 } |
238 |
239 expand_end_target_temps (); 240 free_temp_slots (); 241 |
242 if (TYPE_NEEDS_DESTRUCTOR (type)) 243 { |
244 tree expr; 245 246 /* All cleanups must be on the function_obstack. */ 247 push_obstacks_nochange (); 248 resume_temporary_allocation (); 249 250 expr = build_component_ref (current_class_ref, name, NULL_TREE, 251 explicit); |
252 expr = build_delete (type, expr, integer_zero_node, 253 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0); 254 255 if (expr != error_mark_node) |
256 add_partial_entry (expr); 257 258 pop_obstacks (); |
259 } 260} 261 262extern int warn_reorder; 263 264/* Subroutine of emit_member_init. */ |
265 |
266static tree 267sort_member_init (t) 268 tree t; 269{ |
270 tree x, member, name, field; |
271 tree init_list = NULL_TREE; |
272 int last_pos = 0; |
273 tree last_field = NULL_TREE; |
274 275 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member)) 276 { 277 int pos; 278 279 /* member could be, for example, a CONST_DECL for an enumerated 280 tag; we don't want to try to initialize that, since it already 281 has a value. */ 282 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member)) 283 continue; 284 285 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos) 286 { 287 /* If we cleared this out, then pay no attention to it. */ 288 if (TREE_PURPOSE (x) == NULL_TREE) 289 continue; 290 name = TREE_PURPOSE (x); 291 292#if 0 |
293 /* This happens in templates, since the IDENTIFIER is replaced 294 with the COMPONENT_REF in tsubst_expr. */ |
295 field = (TREE_CODE (name) == COMPONENT_REF 296 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name)); 297#else 298 /* Let's find out when this happens. */ 299 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348); 300 field = IDENTIFIER_CLASS_VALUE (name); 301#endif 302 --- 71 unchanged lines hidden (view full) --- 374 tree vbases = NULL_TREE; 375 376 /* First walk through and splice out vbase and invalid initializers. 377 Also replace names with binfos. */ 378 379 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list); 380 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x)) 381 { |
382 tree basetype = TREE_PURPOSE (x); 383 tree binfo = NULL_TREE; |
384 |
385 if (basetype == NULL_TREE) |
386 { 387 /* Initializer for single base class. Must not 388 use multiple inheritance or this is ambiguous. */ 389 switch (n_baseclasses) 390 { 391 case 0: 392 cp_error ("`%T' does not have a base class to initialize", 393 current_class_type); 394 return; 395 case 1: 396 break; 397 default: 398 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance", 399 current_class_type); 400 return; 401 } 402 binfo = TREE_VEC_ELT (binfos, 0); 403 } |
404 else if (is_aggr_type (basetype, 1)) |
405 { |
406 binfo = binfo_or_else (basetype, t); |
407 if (binfo == NULL_TREE) 408 continue; 409 410 /* Virtual base classes are special cases. Their initializers 411 are recorded with this constructor, and they are used when 412 this constructor is the top-level constructor called. */ 413 if (TREE_VIA_VIRTUAL (binfo)) 414 { --- 8 unchanged lines hidden (view full) --- 423 { 424 /* Otherwise, if it is not an immediate base class, complain. */ 425 for (i = n_baseclasses-1; i >= 0; i--) 426 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i))) 427 break; 428 if (i < 0) 429 { 430 cp_error ("`%T' is not an immediate base class of `%T'", |
431 basetype, current_class_type); |
432 continue; 433 } 434 } 435 } 436 else 437 my_friendly_abort (365); 438 439 TREE_PURPOSE (x) = binfo; --- 48 unchanged lines hidden (view full) --- 488 rbases = chainon (rbases, x); 489 } 490 491 *rbase_ptr = rbases; 492 *vbase_ptr = vbases; 493} 494 495/* Perform partial cleanups for a base for exception handling. */ |
496 |
497static tree 498build_partial_cleanup_for (binfo) 499 tree binfo; 500{ |
501 return build_scoped_method_call 502 (current_class_ref, binfo, dtor_identifier, 503 build_expr_list (NULL_TREE, integer_zero_node)); |
504} 505 506/* Perform whatever initializations have yet to be done on the base 507 class of the class variable. These actions are in the global 508 variable CURRENT_BASE_INIT_LIST. Such an action could be 509 NULL_TREE, meaning that the user has explicitly called the base 510 class constructor with no arguments. 511 --- 11 unchanged lines hidden (view full) --- 523 524extern tree base_init_expr, rtl_expr_chain; 525 526void 527emit_base_init (t, immediately) 528 tree t; 529 int immediately; 530{ |
531 tree member; |
532 tree mem_init_list; 533 tree rbase_init_list, vbase_init_list; 534 tree t_binfo = TYPE_BINFO (t); 535 tree binfos = BINFO_BASETYPES (t_binfo); 536 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; 537 tree expr = NULL_TREE; 538 |
539 if (! immediately) 540 { 541 int momentary; 542 do_pending_stack_adjust (); 543 /* Make the RTL_EXPR node temporary, not momentary, 544 so that rtl_expr_chain doesn't become garbage. */ 545 momentary = suspend_momentary (); 546 expr = make_node (RTL_EXPR); --- 15 unchanged lines hidden (view full) --- 562 sort_base_init (t, &rbase_init_list, &vbase_init_list); 563 current_base_init_list = NULL_TREE; 564 565 if (TYPE_USES_VIRTUAL_BASECLASSES (t)) 566 { 567 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); 568 569 expand_start_cond (first_arg, 0); |
570 expand_aggr_vbase_init (t_binfo, current_class_ref, current_class_ptr, |
571 vbase_init_list); 572 expand_end_cond (); 573 } 574 575 /* Now, perform initialization of non-virtual base classes. */ 576 for (i = 0; i < n_baseclasses; i++) 577 { |
578 tree base_binfo = TREE_VEC_ELT (binfos, i); 579 tree init = void_list_node; 580 581 if (TREE_VIA_VIRTUAL (base_binfo)) 582 continue; 583 584#if 0 /* Once unsharing happens soon enough. */ |
585 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo, 999); |
586#else 587 BINFO_INHERITANCE_CHAIN (base_binfo) = t_binfo; 588#endif 589 590 if (TREE_PURPOSE (rbase_init_list)) 591 init = TREE_VALUE (rbase_init_list); 592 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo))) |
593 { 594 init = NULL_TREE; 595 if (extra_warnings && copy_args_p (current_function_decl)) 596 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor", 597 BINFO_TYPE (base_binfo)); 598 } |
599 600 if (init != void_list_node) 601 { |
602 expand_start_target_temps (); 603 604 member = convert_pointer_to_real (base_binfo, current_class_ptr); 605 expand_aggr_init_1 (base_binfo, NULL_TREE, |
606 build_indirect_ref (member, NULL_PTR), init, 607 BINFO_OFFSET_ZEROP (base_binfo), LOOKUP_NORMAL); |
608 609 expand_end_target_temps (); 610 free_temp_slots (); |
611 } 612 613 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))) 614 { |
615 tree expr; 616 617 /* All cleanups must be on the function_obstack. */ 618 push_obstacks_nochange (); 619 resume_temporary_allocation (); 620 expr = build_partial_cleanup_for (base_binfo); 621 pop_obstacks (); 622 add_partial_entry (expr); |
623 } 624 625 rbase_init_list = TREE_CHAIN (rbase_init_list); 626 } 627 628 /* Initialize all the virtual function table fields that |
629 do come from virtual base classes. */ |
630 if (TYPE_USES_VIRTUAL_BASECLASSES (t)) |
631 expand_indirect_vtbls_init (t_binfo, current_class_ref, current_class_ptr); |
632 633 /* Initialize all the virtual function table fields that 634 do not come from virtual base classes. */ |
635 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_ptr); |
636 637 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member)) 638 { 639 tree init, name; 640 int from_init_list; 641 642 /* member could be, for example, a CONST_DECL for an enumerated 643 tag; we don't want to try to initialize that, since it already 644 has a value. */ 645 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member)) 646 continue; 647 648 /* See if we had a user-specified member initialization. */ 649 if (TREE_PURPOSE (mem_init_list)) 650 { 651 name = TREE_PURPOSE (mem_init_list); 652 init = TREE_VALUE (mem_init_list); 653 from_init_list = 1; 654 |
655#if 0 656 if (TREE_CODE (name) == COMPONENT_REF) 657 name = DECL_NAME (TREE_OPERAND (name, 1)); 658#else |
659 /* Also see if it's ever a COMPONENT_REF here. If it is, we 660 need to do `expand_assignment (name, init, 0, 0);' and 661 a continue. */ 662 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349); |
663#endif |
664 } 665 else 666 { 667 name = DECL_NAME (member); 668 init = DECL_INITIAL (member); 669 670 from_init_list = 0; |
671 672 /* Effective C++ rule 12. */ 673 if (warn_ecpp && init == NULL_TREE 674 && !DECL_ARTIFICIAL (member) 675 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE) 676 cp_warning ("`%D' should be initialized in the member initialization list", member); |
677 } 678 |
679 perform_member_init (member, name, init, from_init_list); |
680 mem_init_list = TREE_CHAIN (mem_init_list); 681 } 682 683 /* Now initialize any members from our bases. */ 684 while (mem_init_list) 685 { 686 tree name, init, field; 687 --- 21 unchanged lines hidden (view full) --- 709 710 /* The code in this for loop is derived from a general loop 711 which had this check in it. Theoretically, we've hit 712 every initialization for the list of members in T, so 713 we shouldn't have anything but these left in this list. */ 714 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351); 715#endif 716 |
717 perform_member_init (field, name, init, 1); |
718 } 719 mem_init_list = TREE_CHAIN (mem_init_list); 720 } 721 722 if (! immediately) 723 { 724 do_pending_stack_adjust (); 725 my_friendly_assert (base_init_expr == 0, 207); --- 7 unchanged lines hidden (view full) --- 733 } 734 735 /* All the implicit try blocks we built up will be zapped 736 when we come to a real binding contour boundary. */ 737} 738 739/* Check that all fields are properly initialized after 740 an assignment to `this'. */ |
741 |
742void 743check_base_init (t) 744 tree t; 745{ 746 tree member; 747 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member)) 748 if (DECL_NAME (member) && TREE_USED (member)) 749 cp_error ("field `%D' used before initialized (after assignment to `this')", 750 member); 751} 752 753/* This code sets up the virtual function tables appropriate for 754 the pointer DECL. It is a one-ply initialization. 755 756 BINFO is the exact type that DECL is supposed to be. In 757 multiple inheritance, this might mean "C's A" if C : A, B. */ |
758 |
759static void 760expand_virtual_init (binfo, decl) 761 tree binfo, decl; 762{ 763 tree type = BINFO_TYPE (binfo); 764 tree vtbl, vtbl_ptr; 765 tree vtype, vtype_binfo; 766 --- 14 unchanged lines hidden (view full) --- 781 /* Have to convert VTBL since array sizes may be different. */ 782 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0); 783 expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl)); 784} 785 786/* Subroutine of `expand_aggr_vbase_init'. 787 BINFO is the binfo of the type that is being initialized. 788 INIT_LIST is the list of initializers for the virtual baseclass. */ |
789 |
790static void 791expand_aggr_vbase_init_1 (binfo, exp, addr, init_list) 792 tree binfo, exp, addr, init_list; 793{ 794 tree init = purpose_member (binfo, init_list); 795 tree ref = build_indirect_ref (addr, NULL_PTR); |
796 797 expand_start_target_temps (); 798 |
799 if (init) 800 init = TREE_VALUE (init); 801 /* Call constructors, but don't set up vtables. */ 802 expand_aggr_init_1 (binfo, exp, ref, init, 0, LOOKUP_COMPLAIN); |
803 804 expand_end_target_temps (); 805 free_temp_slots (); |
806} 807 808/* Initialize this object's virtual base class pointers. This must be 809 done only at the top-level of the object being constructed. 810 811 INIT_LIST is list of initialization for constructor to perform. */ |
812 |
813static void 814expand_aggr_vbase_init (binfo, exp, addr, init_list) 815 tree binfo; 816 tree exp; 817 tree addr; 818 tree init_list; 819{ 820 tree type = BINFO_TYPE (binfo); --- 12 unchanged lines hidden (view full) --- 833 tree tmp = purpose_member (vbases, result); 834 expand_aggr_vbase_init_1 (vbases, exp, 835 TREE_OPERAND (TREE_VALUE (tmp), 0), 836 init_list); 837 } 838 } 839} 840 |
841/* Find the context in which this FIELD can be initialized. */ 842 843static tree 844initializing_context (field) 845 tree field; |
846{ |
847 tree t = DECL_CONTEXT (field); |
848 |
849 /* Anonymous union members can be initialized in the first enclosing 850 non-anonymous union context. */ 851 while (t && ANON_UNION_TYPE_P (t)) 852 t = TYPE_CONTEXT (t); 853 return t; |
854} 855 856/* Function to give error message if member initialization specification 857 is erroneous. FIELD is the member we decided to initialize. 858 TYPE is the type for which the initialization is being performed. |
859 FIELD must be a member of TYPE. |
860 861 MEMBER_NAME is the name of the member. */ 862 863static int 864member_init_ok_or_else (field, type, member_name) 865 tree field; 866 tree type; 867 char *member_name; 868{ 869 if (field == error_mark_node) 870 return 0; |
871 if (field == NULL_TREE || initializing_context (field) != type) |
872 { 873 cp_error ("class `%T' does not have any field named `%s'", type, 874 member_name); 875 return 0; 876 } |
877 if (TREE_STATIC (field)) 878 { 879 cp_error ("field `%#D' is static; only point of initialization is its declaration", 880 field); 881 return 0; 882 } 883 884 return 1; --- 9 unchanged lines hidden (view full) --- 894 895 We do not yet have a fixed-point finder to instantiate types 896 being fed to overloaded constructors. If there is a unique 897 constructor, then argument types can be got from that one. 898 899 If INIT is non-NULL, then it the initialization should 900 be placed in `current_base_init_list', where it will be processed 901 by `emit_base_init'. */ |
902 |
903void 904expand_member_init (exp, name, init) 905 tree exp, name, init; 906{ |
907 tree basetype = NULL_TREE, field; |
908 tree type; |
909 910 if (exp == NULL_TREE) 911 return; /* complain about this later */ 912 913 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); 914 |
915 if (name && TREE_CODE (name) == TYPE_DECL) 916 { 917 basetype = TREE_TYPE (name); 918 name = DECL_NAME (name); 919 } 920 |
921 if (name == NULL_TREE && IS_AGGR_TYPE (type)) 922 switch (CLASSTYPE_N_BASECLASSES (type)) 923 { 924 case 0: 925 error ("base class initializer specified, but no base class to initialize"); 926 return; 927 case 1: 928 basetype = TYPE_BINFO_BASETYPE (type, 0); 929 break; 930 default: 931 error ("initializer for unnamed base class ambiguous"); 932 cp_error ("(type `%T' uses multiple inheritance)", type); 933 return; 934 } 935 |
936 my_friendly_assert (init != NULL_TREE, 0); |
937 |
938 /* The grammar should not allow fields which have names that are 939 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we 940 may assume that this is an attempt to initialize a base class 941 member of the current type. Otherwise, it is an attempt to 942 initialize a member field. */ |
943 |
944 if (init == void_type_node) 945 init = NULL_TREE; |
946 |
947 if (name == NULL_TREE || basetype) 948 { 949 tree base_init; 950 951 if (name == NULL_TREE) 952 { 953#if 0 954 if (basetype) 955 name = TYPE_IDENTIFIER (basetype); |
956 else 957 { |
958 error ("no base class to initialize"); |
959 return; 960 } |
961#endif |
962 } |
963 else if (basetype != type 964 && ! current_template_parms 965 && ! vec_binfo_member (basetype, 966 TYPE_BINFO_BASETYPES (type)) 967 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type))) |
968 { |
969 if (IDENTIFIER_CLASS_VALUE (name)) 970 goto try_member; 971 if (TYPE_USES_VIRTUAL_BASECLASSES (type)) 972 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'", 973 basetype, type); 974 else 975 cp_error ("type `%T' is not an immediate basetype for `%T'", 976 basetype, type); 977 return; |
978 } |
979 |
980 if (purpose_member (basetype, current_base_init_list)) |
981 { |
982 cp_error ("base class `%T' already initialized", basetype); 983 return; |
984 } |
985 |
986 if (warn_reorder && current_member_init_list) |
987 { |
988 cp_warning ("base initializer for `%T'", basetype); 989 warning (" will be re-ordered to precede member initializations"); |
990 } |
991 |
992 base_init = build_tree_list (basetype, init); 993 current_base_init_list = chainon (current_base_init_list, base_init); 994 } 995 else 996 { 997 tree member_init; 998 999 try_member: 1000 field = lookup_field (type, name, 1, 0); 1001 1002 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name))) |
1003 return; 1004 |
1005 if (purpose_member (name, current_member_init_list)) |
1006 { |
1007 cp_error ("field `%D' already initialized", field); 1008 return; |
1009 } |
1010 |
1011 member_init = build_tree_list (name, init); 1012 current_member_init_list = chainon (current_member_init_list, member_init); 1013 } |
1014} 1015 1016/* This is like `expand_member_init', only it stores one aggregate 1017 value into another. 1018 1019 INIT comes in two flavors: it is either a value which 1020 is to be stored in EXP, or it is a parameter list 1021 to go to a constructor, which will operate on EXP. 1022 If INIT is not a parameter list for a constructor, then set 1023 LOOKUP_ONLYCONVERTING. 1024 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of 1025 the initializer, if FLAGS is 0, then it is the (init) form. 1026 If `init' is a CONSTRUCTOR, then we emit a warning message, 1027 explaining that such initializations are invalid. 1028 1029 ALIAS_THIS is nonzero iff we are initializing something which is |
1030 essentially an alias for current_class_ref. In this case, the base 1031 constructor may move it on us, and we must keep track of such 1032 deviations. |
1033 1034 If INIT resolves to a CALL_EXPR which happens to return 1035 something of the type we are looking for, then we know 1036 that we can safely use that call to perform the 1037 initialization. 1038 1039 The virtual function table pointer cannot be set up here, because 1040 we do not really know its type. 1041 1042 Virtual baseclass pointers are also set up here. 1043 1044 This never calls operator=(). 1045 1046 When initializing, nothing is CONST. 1047 1048 A default copy constructor may have to be used to perform the 1049 initialization. 1050 1051 A constructor or a conversion operator may have to be used to |
1052 perform the initialization, but not both, as it would be ambiguous. */ |
1053 1054void 1055expand_aggr_init (exp, init, alias_this, flags) 1056 tree exp, init; 1057 int alias_this; 1058 int flags; 1059{ 1060 tree type = TREE_TYPE (exp); --- 64 unchanged lines hidden (view full) --- 1125 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp, 1126 init, alias_this, LOOKUP_NORMAL|flags); 1127 TREE_TYPE (exp) = type; 1128 TREE_READONLY (exp) = was_const; 1129 TREE_THIS_VOLATILE (exp) = was_volatile; 1130} 1131 1132static void |
1133expand_default_init (binfo, true_exp, exp, init, alias_this, flags) |
1134 tree binfo; 1135 tree true_exp, exp; |
1136 tree init; 1137 int alias_this; 1138 int flags; 1139{ |
1140 tree type = TREE_TYPE (exp); 1141 |
1142 /* It fails because there may not be a constructor which takes 1143 its own type as the first (or only parameter), but which does 1144 take other types via a conversion. So, if the thing initializing 1145 the expression is a unit element of type X, first try X(X&), 1146 followed by initialization by X. If neither of these work 1147 out, then look hard. */ 1148 tree rval; 1149 tree parms; 1150 |
1151 if (init && TREE_CODE (init) != TREE_LIST 1152 && (flags & LOOKUP_ONLYCONVERTING)) 1153 { 1154 /* Base subobjects should only get direct-initialization. */ 1155 if (true_exp != exp) 1156 abort (); 1157 1158 /* We special-case TARGET_EXPRs here to avoid an error about 1159 private copy constructors for temporaries bound to reference vars. 1160 If the TARGET_EXPR represents a call to a function that has 1161 permission to create such objects, a reference can bind directly 1162 to the return value. An object variable must be initialized 1163 via the copy constructor, even if the call is elided. */ 1164 if (! (TREE_CODE (exp) == VAR_DECL && DECL_ARTIFICIAL (exp) 1165 && TREE_CODE (init) == TARGET_EXPR && TREE_TYPE (init) == type)) 1166 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags); 1167 1168 if (TREE_CODE (init) == TRY_CATCH_EXPR) 1169 /* We need to protect the initialization of a catch parm 1170 with a call to terminate(), which shows up as a TRY_CATCH_EXPR 1171 around the TARGET_EXPR for the copy constructor. See 1172 expand_start_catch_block. */ 1173 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp, 1174 TREE_OPERAND (init, 0)); 1175 else 1176 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init); 1177 TREE_SIDE_EFFECTS (init) = 1; 1178 expand_expr_stmt (init); 1179 return; 1180 } 1181 |
1182 if (init == NULL_TREE 1183 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init))) 1184 { 1185 parms = init; 1186 if (parms) 1187 init = TREE_VALUE (parms); 1188 } |
1189 else |
1190 parms = build_expr_list (NULL_TREE, init); |
1191 1192 if (TYPE_USES_VIRTUAL_BASECLASSES (type)) 1193 { 1194 if (true_exp == exp) |
1195 parms = expr_tree_cons (NULL_TREE, integer_one_node, parms); |
1196 else |
1197 parms = expr_tree_cons (NULL_TREE, integer_zero_node, parms); |
1198 flags |= LOOKUP_HAS_IN_CHARGE; 1199 } 1200 |
1201 rval = build_method_call (exp, ctor_identifier, 1202 parms, binfo, flags); 1203 if (TREE_SIDE_EFFECTS (rval)) 1204 expand_expr_stmt (rval); |
1205} 1206 1207/* This function is responsible for initializing EXP with INIT 1208 (if any). 1209 1210 BINFO is the binfo of the type for who we are performing the 1211 initialization. For example, if W is a virtual base class of A and B, 1212 and C : A, B. --- 17 unchanged lines hidden (view full) --- 1230expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags) 1231 tree binfo; 1232 tree true_exp, exp; 1233 tree init; 1234 int alias_this; 1235 int flags; 1236{ 1237 tree type = TREE_TYPE (exp); |
1238 1239 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211); 1240 1241 /* Use a function returning the desired type to initialize EXP for us. 1242 If the function is a constructor, and its first argument is 1243 NULL_TREE, know that it was meant for us--just slide exp on 1244 in and expand the constructor. Constructors now come 1245 as TARGET_EXPRs. */ |
1246 |
1247 if (init && TREE_CODE (exp) == VAR_DECL 1248 && TREE_CODE (init) == CONSTRUCTOR 1249 && TREE_HAS_CONSTRUCTOR (init)) 1250 { 1251 tree t = store_init_value (exp, init); 1252 if (!t) |
1253 { |
1254 expand_decl_init (exp); |
1255 return; 1256 } |
1257 t = build (INIT_EXPR, type, exp, init); 1258 TREE_SIDE_EFFECTS (t) = 1; 1259 expand_expr_stmt (t); 1260 return; |
1261 } 1262 |
1263 /* We know that expand_default_init can handle everything we want 1264 at this point. */ 1265 expand_default_init (binfo, true_exp, exp, init, alias_this, flags); |
1266} 1267 |
1268/* Report an error if NAME is not the name of a user-defined, 1269 aggregate type. If OR_ELSE is nonzero, give an error message. */ |
1270 |
1271int 1272is_aggr_typedef (name, or_else) 1273 tree name; 1274 int or_else; 1275{ 1276 tree type; 1277 1278 if (name == error_mark_node) --- 4 unchanged lines hidden (view full) --- 1283 else 1284 { 1285 if (or_else) 1286 cp_error ("`%T' is not an aggregate typedef", name); 1287 return 0; 1288 } 1289 1290 if (! IS_AGGR_TYPE (type) |
1291 && TREE_CODE (type) != TEMPLATE_TYPE_PARM 1292 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM) |
1293 { 1294 if (or_else) 1295 cp_error ("`%T' is not an aggregate type", type); 1296 return 0; 1297 } 1298 return 1; 1299} 1300 |
1301/* Report an error if TYPE is not a user-defined, aggregate type. If 1302 OR_ELSE is nonzero, give an error message. */ 1303 1304int 1305is_aggr_type (type, or_else) 1306 tree type; 1307 int or_else; 1308{ 1309 if (type == error_mark_node) 1310 return 0; 1311 1312 if (! IS_AGGR_TYPE (type) 1313 && TREE_CODE (type) != TEMPLATE_TYPE_PARM 1314 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM) 1315 { 1316 if (or_else) 1317 cp_error ("`%T' is not an aggregate type", type); 1318 return 0; 1319 } 1320 return 1; 1321} 1322 |
1323/* Like is_aggr_typedef, but returns typedef if successful. */ |
1324 |
1325tree 1326get_aggr_from_typedef (name, or_else) 1327 tree name; 1328 int or_else; 1329{ 1330 tree type; 1331 1332 if (name == error_mark_node) --- 4 unchanged lines hidden (view full) --- 1337 else 1338 { 1339 if (or_else) 1340 cp_error ("`%T' fails to be an aggregate typedef", name); 1341 return NULL_TREE; 1342 } 1343 1344 if (! IS_AGGR_TYPE (type) |
1345 && TREE_CODE (type) != TEMPLATE_TYPE_PARM 1346 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM) |
1347 { 1348 if (or_else) 1349 cp_error ("type `%T' is of non-aggregate type", type); 1350 return NULL_TREE; 1351 } 1352 return type; 1353} 1354 --- 9 unchanged lines hidden (view full) --- 1364 else 1365 return NULL_TREE; 1366} 1367 1368 1369/* This code could just as well go in `class.c', but is placed here for 1370 modularity. */ 1371 |
1372/* For an expression of the form TYPE :: NAME (PARMLIST), build |
1373 the appropriate function call. */ |
1374 |
1375tree |
1376build_member_call (type, name, parmlist) 1377 tree type, name, parmlist; |
1378{ |
1379 tree t; 1380 tree method_name; |
1381 int dtor = 0; 1382 int dont_use_this = 0; 1383 tree basetype_path, decl; 1384 |
1385 if (TREE_CODE (name) == TEMPLATE_ID_EXPR 1386 && TREE_CODE (type) == NAMESPACE_DECL) 1387 { 1388 /* 'name' already refers to the decls from the namespace, since we 1389 hit do_identifier for template_ids. */ 1390 my_friendly_assert (is_overloaded_fn (TREE_OPERAND (name, 0)), 980519); 1391 return build_x_function_call (name, parmlist, current_class_ref); 1392 } 1393 1394 if (type == std_node) 1395 return build_x_function_call (do_scoped_id (name, 0), parmlist, 1396 current_class_ref); 1397 if (TREE_CODE (type) == NAMESPACE_DECL) 1398 return build_x_function_call (lookup_namespace_name (type, name), 1399 parmlist, current_class_ref); 1400 1401 if (TREE_CODE (name) != TEMPLATE_ID_EXPR) 1402 method_name = name; 1403 else 1404 method_name = TREE_OPERAND (name, 0); 1405 |
1406 if (TREE_CODE (method_name) == BIT_NOT_EXPR) 1407 { 1408 method_name = TREE_OPERAND (method_name, 0); 1409 dtor = 1; 1410 } 1411 |
1412 /* This shouldn't be here, and build_member_call shouldn't appear in 1413 parse.y! (mrs) */ |
1414 if (type && TREE_CODE (type) == IDENTIFIER_NODE 1415 && get_aggr_from_typedef (type, 0) == 0) |
1416 { |
1417 tree ns = lookup_name (type, 0); |
1418 if (ns && TREE_CODE (ns) == NAMESPACE_DECL) 1419 { |
1420 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref); |
1421 } 1422 } 1423 |
1424 if (type == NULL_TREE || ! is_aggr_type (type, 1)) |
1425 return error_mark_node; 1426 1427 /* An operator we did not like. */ 1428 if (name == NULL_TREE) 1429 return error_mark_node; 1430 1431 if (dtor) 1432 { |
1433 cp_error ("cannot call destructor `%T::~%T' without object", type, 1434 method_name); 1435 return error_mark_node; 1436 } 1437 1438 /* No object? Then just fake one up, and let build_method_call 1439 figure out what to do. */ 1440 if (current_class_type == 0 1441 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1) 1442 dont_use_this = 1; 1443 1444 if (dont_use_this) 1445 { 1446 basetype_path = TYPE_BINFO (type); 1447 decl = build1 (NOP_EXPR, build_pointer_type (type), error_mark_node); 1448 } |
1449 else if (current_class_ptr == 0) |
1450 { 1451 dont_use_this = 1; 1452 decl = build1 (NOP_EXPR, build_pointer_type (type), error_mark_node); 1453 } 1454 else 1455 { |
1456 tree olddecl = current_class_ptr; |
1457 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl)); 1458 if (oldtype != type) 1459 { 1460 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype), 1461 TYPE_VOLATILE (oldtype)); 1462 decl = convert_force (build_pointer_type (newtype), olddecl, 0); 1463 } 1464 else 1465 decl = olddecl; 1466 } 1467 1468 decl = build_indirect_ref (decl, NULL_PTR); 1469 1470 if (method_name == constructor_name (type) 1471 || method_name == constructor_name_full (type)) 1472 return build_functional_cast (type, parmlist); |
1473 if ((t = lookup_fnfields (basetype_path, method_name, 0))) 1474 return build_method_call (decl, 1475 TREE_CODE (name) == TEMPLATE_ID_EXPR 1476 ? name : method_name, 1477 parmlist, basetype_path, |
1478 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL); 1479 if (TREE_CODE (name) == IDENTIFIER_NODE 1480 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0)))) 1481 { 1482 if (t == error_mark_node) 1483 return error_mark_node; 1484 if (TREE_CODE (t) == FIELD_DECL) 1485 { --- 6 unchanged lines hidden (view full) --- 1492 } 1493 else if (TREE_CODE (t) == VAR_DECL) 1494 decl = t; 1495 else 1496 { 1497 cp_error ("invalid use of member `%D'", t); 1498 return error_mark_node; 1499 } |
1500 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))) 1501 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, 1502 parmlist, NULL_TREE); |
1503 return build_function_call (decl, parmlist); 1504 } 1505 else 1506 { 1507 cp_error ("no method `%T::%D'", type, name); 1508 return error_mark_node; 1509 } 1510} 1511 1512/* Build a reference to a member of an aggregate. This is not a 1513 C++ `&', but really something which can have its address taken, |
1514 and then act as a pointer to member, for example TYPE :: FIELD 1515 can have its address taken by saying & TYPE :: FIELD. |
1516 1517 @@ Prints out lousy diagnostics for operator <typename> 1518 @@ fields. 1519 1520 @@ This function should be rewritten and placed in search.c. */ |
1521 |
1522tree |
1523build_offset_ref (type, name) 1524 tree type, name; |
1525{ |
1526 tree decl, fnfields, fields, t = error_mark_node; 1527 tree basebinfo = NULL_TREE; 1528 tree orig_name = name; |
1529 |
1530 /* class templates can come in as TEMPLATE_DECLs here. */ 1531 if (TREE_CODE (name) == TEMPLATE_DECL) 1532 return name; |
1533 |
1534 if (type == std_node) 1535 return do_scoped_id (name, 0); 1536 1537 if (processing_template_decl || uses_template_parms (type)) 1538 return build_min_nt (SCOPE_REF, type, name); 1539 |
1540 /* Handle namespace names fully here. */ |
1541 if (TREE_CODE (type) == NAMESPACE_DECL) |
1542 { |
1543 t = lookup_namespace_name (type, name); 1544 if (! type_unknown_p (t)) |
1545 { |
1546 mark_used (t); 1547 t = convert_from_reference (t); |
1548 } |
1549 return t; |
1550 } 1551 |
1552 if (type == NULL_TREE || ! is_aggr_type (type, 1)) |
1553 return error_mark_node; 1554 |
1555 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 1556 { 1557 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at 1558 something like `a.template f<int>' or the like. For the most 1559 part, we treat this just like a.f. We do remember, however, 1560 the template-id that was used. */ 1561 name = TREE_OPERAND (orig_name, 0); 1562 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0); 1563 } |
1564 1565 if (TREE_CODE (name) == BIT_NOT_EXPR) 1566 { |
1567 if (! check_dtor_name (type, name)) 1568 cp_error ("qualified type `%T' does not match destructor name `~%T'", 1569 type, TREE_OPERAND (name, 0)); 1570 name = dtor_identifier; |
1571 } |
1572#if 0 1573 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */ 1574 else if (name == constructor_name_full (type) 1575 || name == constructor_name (type)) 1576 name = ctor_identifier; 1577#endif |
1578 |
1579 if (TYPE_SIZE (complete_type (type)) == 0) |
1580 { |
1581 if (type == current_class_type) 1582 t = IDENTIFIER_CLASS_VALUE (name); 1583 else 1584 t = NULL_TREE; |
1585 if (t == 0) 1586 { 1587 cp_error ("incomplete type `%T' does not have member `%D'", type, 1588 name); 1589 return error_mark_node; 1590 } 1591 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL 1592 || TREE_CODE (t) == CONST_DECL) 1593 { |
1594 mark_used (t); |
1595 return t; 1596 } 1597 if (TREE_CODE (t) == FIELD_DECL) 1598 sorry ("use of member in incomplete aggregate type"); 1599 else if (TREE_CODE (t) == FUNCTION_DECL) 1600 sorry ("use of member function in incomplete aggregate type"); 1601 else 1602 my_friendly_abort (52); 1603 return error_mark_node; 1604 } 1605 |
1606 if (current_class_type == 0 |
1607 || get_base_distance (type, current_class_type, 0, &basebinfo) == -1) |
1608 { |
1609 basebinfo = TYPE_BINFO (type); 1610 decl = build1 (NOP_EXPR, type, error_mark_node); |
1611 } |
1612 else if (current_class_ptr == 0) 1613 decl = build1 (NOP_EXPR, type, error_mark_node); |
1614 else |
1615 decl = current_class_ref; |
1616 |
1617 fnfields = lookup_fnfields (basebinfo, name, 1); 1618 fields = lookup_field (basebinfo, name, 0, 0); |
1619 1620 if (fields == error_mark_node || fnfields == error_mark_node) 1621 return error_mark_node; 1622 1623 /* A lot of this logic is now handled in lookup_field and |
1624 lookup_fnfield. */ |
1625 if (fnfields) 1626 { |
1627 extern int flag_save_memoized_contexts; |
1628 1629 /* Go from the TREE_BASELINK to the member function info. */ 1630 t = TREE_VALUE (fnfields); 1631 |
1632 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR) |
1633 { |
1634 /* The FNFIELDS are going to contain functions that aren't 1635 necessarily templates, and templates that don't 1636 necessarily match the explicit template parameters. We 1637 save all the functions, and the explicit parameters, and 1638 then figure out exactly what to instantiate with what 1639 arguments in instantiate_type. */ 1640 1641 if (TREE_CODE (t) != OVERLOAD) 1642 /* The code in instantiate_type which will process this 1643 expects to encounter OVERLOADs, not raw functions. */ 1644 t = ovl_cons (t, NULL_TREE); 1645 1646 return build (OFFSET_REF, 1647 build_offset_type (type, unknown_type_node), 1648 decl, 1649 build (TEMPLATE_ID_EXPR, 1650 TREE_TYPE (t), 1651 t, 1652 TREE_OPERAND (orig_name, 1))); |
1653 } 1654 |
1655 if (!really_overloaded_fn (t)) |
1656 { |
1657 tree access; |
1658 |
1659 /* Get rid of a potential OVERLOAD around it */ 1660 t = OVL_CURRENT (t); |
1661 |
1662 /* unique functions are handled easily. */ 1663 basebinfo = TREE_PURPOSE (fnfields); 1664 access = compute_access (basebinfo, t); 1665 if (access == access_protected_node) 1666 { 1667 cp_error_at ("member function `%#D' is protected", t); 1668 error ("in this context"); 1669 return error_mark_node; |
1670 } |
1671 if (access == access_private_node) |
1672 { |
1673 cp_error_at ("member function `%#D' is private", t); 1674 error ("in this context"); 1675 return error_mark_node; |
1676 } |
1677 mark_used (t); 1678 return build (OFFSET_REF, TREE_TYPE (t), decl, t); 1679 } |
1680 |
1681 /* FNFIELDS is most likely allocated on the search_obstack, 1682 which will go away after this class scope. If we need 1683 to save this value for later (either for memoization 1684 or for use as an initializer for a static variable), then 1685 do so here. |
1686 |
1687 ??? The smart thing to do for the case of saving initializers 1688 is to resolve them before we're done with this scope. */ 1689 if (!TREE_PERMANENT (fnfields) 1690 && ((flag_save_memoized_contexts && toplevel_bindings_p ()) 1691 || ! allocation_temporary_p ())) 1692 fnfields = copy_list (fnfields); |
1693 |
1694 t = build_tree_list (error_mark_node, fnfields); 1695 TREE_TYPE (t) = build_offset_type (type, unknown_type_node); 1696 return t; |
1697 } 1698 1699 /* Now that we know we are looking for a field, see if we 1700 have access to that field. Lookup_field will give us the 1701 error message. */ 1702 |
1703 t = lookup_field (basebinfo, name, 1, 0); |
1704 1705 if (t == error_mark_node) 1706 return error_mark_node; 1707 1708 if (t == NULL_TREE) 1709 { 1710 cp_error ("`%D' is not a member of type `%T'", name, type); 1711 return error_mark_node; 1712 } 1713 1714 if (TREE_CODE (t) == TYPE_DECL) 1715 { 1716 TREE_USED (t) = 1; 1717 return t; 1718 } 1719 /* static class members and class-specific enum 1720 values can be returned without further ado. */ 1721 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL) 1722 { |
1723 mark_used (t); 1724 return convert_from_reference (t); |
1725 } 1726 1727 if (TREE_CODE (t) == FIELD_DECL && DECL_BIT_FIELD (t)) 1728 { 1729 cp_error ("illegal pointer to bit field `%D'", t); 1730 return error_mark_node; 1731 } 1732 1733 /* static class functions too. */ |
1734 if (TREE_CODE (t) == FUNCTION_DECL 1735 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE) |
1736 my_friendly_abort (53); 1737 |
1738 /* In member functions, the form `type::name' is no longer 1739 equivalent to `this->type::name', at least not until 1740 resolve_offset_ref. */ |
1741 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t); 1742} 1743 |
1744/* If a OFFSET_REF made it through to here, then it did 1745 not have its address taken. */ 1746 1747tree 1748resolve_offset_ref (exp) 1749 tree exp; 1750{ 1751 tree type = TREE_TYPE (exp); 1752 tree base = NULL_TREE; 1753 tree member; 1754 tree basetype, addr; 1755 1756 if (TREE_CODE (exp) == TREE_LIST) |
1757 { 1758 cp_pedwarn ("assuming & on overloaded member function"); 1759 return build_unary_op (ADDR_EXPR, exp, 0); 1760 } |
1761 |
1762 if (TREE_CODE (exp) == OFFSET_REF) |
1763 { |
1764 member = TREE_OPERAND (exp, 1); 1765 base = TREE_OPERAND (exp, 0); 1766 } 1767 else 1768 { |
1769 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214); 1770 if (TYPE_OFFSET_BASETYPE (type) != current_class_type) 1771 { 1772 error ("object missing in use of pointer-to-member construct"); 1773 return error_mark_node; 1774 } 1775 member = exp; 1776 type = TREE_TYPE (type); |
1777 base = current_class_ref; |
1778 } |
1779 1780 if ((TREE_CODE (member) == VAR_DECL 1781 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))) |
1782 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE 1783 || TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE) |
1784 { 1785 /* These were static members. */ 1786 if (mark_addressable (member) == 0) 1787 return error_mark_node; 1788 return member; 1789 } 1790 |
1791 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE 1792 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE) 1793 return member; 1794 |
1795 /* Syntax error can cause a member which should 1796 have been seen as static to be grok'd as non-static. */ |
1797 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE) |
1798 { 1799 if (TREE_ADDRESSABLE (member) == 0) 1800 { 1801 cp_error_at ("member `%D' is non-static but referenced as a static member", 1802 member); 1803 error ("at this point in file"); 1804 TREE_ADDRESSABLE (member) = 1; 1805 } 1806 return error_mark_node; 1807 } 1808 1809 /* The first case is really just a reference to a member of `this'. */ 1810 if (TREE_CODE (member) == FIELD_DECL |
1811 && (base == current_class_ref |
1812 || (TREE_CODE (base) == NOP_EXPR 1813 && TREE_OPERAND (base, 0) == error_mark_node))) 1814 { |
1815 tree basetype_path, access; |
1816 1817 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE) 1818 basetype = TYPE_OFFSET_BASETYPE (type); 1819 else 1820 basetype = DECL_CONTEXT (member); 1821 |
1822 base = current_class_ptr; |
1823 1824 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0) 1825 { 1826 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base))); 1827 return error_mark_node; 1828 } |
1829 /* Kludge: we need to use basetype_path now, because 1830 convert_pointer_to will bash it. */ |
1831 access = compute_access (basetype_path, member); |
1832 addr = convert_pointer_to (basetype, base); 1833 if (access == access_public_node) |
1834 return build (COMPONENT_REF, TREE_TYPE (member), 1835 build_indirect_ref (addr, NULL_PTR), member); |
1836 if (access == access_protected_node) |
1837 { 1838 cp_error_at ("member `%D' is protected", member); 1839 error ("in this context"); 1840 return error_mark_node; 1841 } |
1842 if (access == access_private_node) |
1843 { 1844 cp_error_at ("member `%D' is private", member); 1845 error ("in this context"); 1846 return error_mark_node; 1847 } 1848 my_friendly_abort (55); 1849 } 1850 |
1851 /* Ensure that we have an object. */ 1852 if (TREE_CODE (base) == NOP_EXPR 1853 && TREE_OPERAND (base, 0) == error_mark_node) 1854 addr = error_mark_node; 1855 else |
1856 { |
1857 /* If this is a reference to a member function, then return the 1858 address of the member function (which may involve going 1859 through the object's vtable), otherwise, return an expression 1860 for the dereferenced pointer-to-member construct. */ 1861 addr = build_unary_op (ADDR_EXPR, base, 0); |
1862 } |
1863 1864 if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE) |
1865 { |
1866 if (addr == error_mark_node) 1867 { 1868 cp_error ("object missing in `%E'", exp); 1869 return error_mark_node; 1870 } 1871 |
1872 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member)); 1873 addr = convert_pointer_to (basetype, addr); |
1874 member = cp_convert (ptrdiff_type_node, 1875 build_unary_op (ADDR_EXPR, member, 0)); 1876 1877 /* Pointer to data members are offset by one, so that a null 1878 pointer with a real value of 0 is distinguishable from an 1879 offset of the first member of a structure. */ 1880 member = build_binary_op (MINUS_EXPR, member, 1881 cp_convert (ptrdiff_type_node, integer_one_node), 1882 0); 1883 |
1884 return build1 (INDIRECT_REF, type, 1885 build (PLUS_EXPR, build_pointer_type (type), 1886 addr, member)); 1887 } 1888 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member))) 1889 { 1890 return get_member_function_from_ptrfunc (&addr, member); 1891 } --- 12 unchanged lines hidden (view full) --- 1904#if 0 1905 /* These may be necessary for C, but they break C++. */ 1906 ! TREE_PUBLIC (decl) 1907 /* Don't change a variable array bound or initial value to a constant 1908 in a place where a variable is invalid. */ 1909 && ! pedantic 1910#endif /* 0 */ 1911 && DECL_INITIAL (decl) != 0 |
1912 && DECL_INITIAL (decl) != error_mark_node |
1913 /* This is invalid if initial value is not constant. 1914 If it has either a function call, a memory reference, 1915 or a variable, then re-evaluating it could give different results. */ 1916 && TREE_CONSTANT (DECL_INITIAL (decl)) 1917 /* Check for cases where this is sub-optimal, even though valid. */ 1918 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR 1919#if 0 1920 /* We must allow this to work outside of functions so that 1921 static constants can be used for array sizes. */ 1922 && current_function_decl != 0 1923 && DECL_MODE (decl) != BLKmode 1924#endif 1925 ) 1926 return DECL_INITIAL (decl); 1927 return decl; 1928} 1929 |
1930/* Common subroutines of build_new and build_vec_delete. */ 1931 1932/* Common interface for calling "builtin" functions that are not 1933 really builtin. */ 1934 |
1935static tree |
1936build_builtin_call (type, node, arglist) 1937 tree type; 1938 tree node; 1939 tree arglist; 1940{ |
1941 tree rval = build (CALL_EXPR, type, node, arglist, NULL_TREE); |
1942 TREE_SIDE_EFFECTS (rval) = 1; 1943 assemble_external (TREE_OPERAND (node, 0)); 1944 TREE_USED (TREE_OPERAND (node, 0)) = 1; 1945 return rval; 1946} 1947 1948/* Generate a C++ "new" expression. DECL is either a TREE_LIST 1949 (which needs to go through some sort of groktypename) or it --- 24 unchanged lines hidden (view full) --- 1974extern int flag_check_new; 1975 1976tree 1977build_new (placement, decl, init, use_global_new) 1978 tree placement; 1979 tree decl, init; 1980 int use_global_new; 1981{ |
1982 tree type, rval; 1983 tree nelts = NULL_TREE, t; |
1984 int has_array = 0; |
1985 1986 tree pending_sizes = NULL_TREE; 1987 1988 if (decl == error_mark_node) 1989 return error_mark_node; 1990 1991 if (TREE_CODE (decl) == TREE_LIST) 1992 { 1993 tree absdcl = TREE_VALUE (decl); 1994 tree last_absdcl = NULL_TREE; |
1995 int old_immediate_size_expand = 0; |
1996 1997 if (current_function_decl 1998 && DECL_CONSTRUCTOR_P (current_function_decl)) 1999 { 2000 old_immediate_size_expand = immediate_size_expand; 2001 immediate_size_expand = 0; 2002 } 2003 --- 20 unchanged lines hidden (view full) --- 2024 } 2025 2026 has_array = 1; 2027 this_nelts = TREE_OPERAND (absdcl, 1); 2028 if (this_nelts != error_mark_node) 2029 { 2030 if (this_nelts == NULL_TREE) 2031 error ("new of array type fails to specify size"); |
2032 else if (processing_template_decl) 2033 { 2034 nelts = this_nelts; 2035 absdcl = TREE_OPERAND (absdcl, 0); 2036 } |
2037 else 2038 { |
2039 this_nelts = save_expr (cp_convert (sizetype, this_nelts)); |
2040 absdcl = TREE_OPERAND (absdcl, 0); 2041 if (this_nelts == integer_zero_node) 2042 { 2043 warning ("zero size array reserves no space"); 2044 nelts = integer_zero_node; 2045 } 2046 else 2047 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1); 2048 } 2049 } 2050 else 2051 nelts = integer_zero_node; 2052 } 2053 2054 if (last_absdcl) 2055 TREE_OPERAND (last_absdcl, 0) = absdcl; 2056 else 2057 TREE_VALUE (decl) = absdcl; 2058 |
2059 type = groktypename (decl); |
2060 if (! type || type == error_mark_node) 2061 { 2062 immediate_size_expand = old_immediate_size_expand; 2063 return error_mark_node; 2064 } 2065 2066 if (current_function_decl 2067 && DECL_CONSTRUCTOR_P (current_function_decl)) 2068 { 2069 pending_sizes = get_pending_sizes (); 2070 immediate_size_expand = old_immediate_size_expand; 2071 } 2072 } 2073 else if (TREE_CODE (decl) == IDENTIFIER_NODE) 2074 { 2075 if (IDENTIFIER_HAS_TYPE_VALUE (decl)) 2076 { 2077 /* An aggregate type. */ 2078 type = IDENTIFIER_TYPE_VALUE (decl); |
2079 decl = TYPE_MAIN_DECL (type); |
2080 } 2081 else 2082 { 2083 /* A builtin type. */ 2084 decl = lookup_name (decl, 1); 2085 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215); 2086 type = TREE_TYPE (decl); 2087 } |
2088 } 2089 else if (TREE_CODE (decl) == TYPE_DECL) 2090 { 2091 type = TREE_TYPE (decl); |
2092 } 2093 else 2094 { 2095 type = decl; |
2096 decl = TYPE_MAIN_DECL (type); |
2097 } 2098 |
2099 if (processing_template_decl) 2100 { 2101 if (has_array) 2102 t = min_tree_cons (min_tree_cons (NULL_TREE, type, NULL_TREE), 2103 build_min_nt (ARRAY_REF, NULL_TREE, nelts), 2104 NULL_TREE); 2105 else 2106 t = type; 2107 2108 rval = build_min_nt (NEW_EXPR, placement, t, init); 2109 NEW_EXPR_USE_GLOBAL (rval) = use_global_new; 2110 return rval; 2111 } 2112 |
2113 /* ``A reference cannot be created by the new operator. A reference 2114 is not an object (8.2.2, 8.4.3), so a pointer to it could not be 2115 returned by new.'' ARM 5.3.3 */ 2116 if (TREE_CODE (type) == REFERENCE_TYPE) 2117 { 2118 error ("new cannot be applied to a reference type"); |
2119 type = TREE_TYPE (type); |
2120 } 2121 2122 if (TREE_CODE (type) == FUNCTION_TYPE) 2123 { 2124 error ("new cannot be applied to a function type"); 2125 return error_mark_node; 2126 } 2127 2128 /* When the object being created is an array, the new-expression yields a 2129 pointer to the initial element (if any) of the array. For example, 2130 both new int and new int[10] return an int*. 5.3.4. */ 2131 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0) 2132 { 2133 nelts = array_type_nelts_top (type); 2134 has_array = 1; |
2135 type = TREE_TYPE (type); |
2136 } 2137 |
2138 if (has_array) 2139 t = build_nt (ARRAY_REF, type, nelts); 2140 else 2141 t = type; 2142 2143 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init); 2144 NEW_EXPR_USE_GLOBAL (rval) = use_global_new; 2145 TREE_SIDE_EFFECTS (rval) = 1; 2146 2147 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */ 2148 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval); 2149 TREE_NO_UNUSED_WARNING (rval) = 1; 2150 2151 if (pending_sizes) 2152 rval = build_compound_expr (chainon (pending_sizes, 2153 build_expr_list (NULL_TREE, rval))); 2154 2155 return rval; 2156} 2157 2158/* Called from cplus_expand_expr when expanding a NEW_EXPR. The return 2159 value is immediately handed to expand_expr. */ 2160 2161tree 2162build_new_1 (exp) 2163 tree exp; 2164{ 2165 tree placement, init; 2166 tree type, true_type, size, rval; 2167 tree nelts = NULL_TREE; 2168 tree alloc_expr, alloc_node = NULL_TREE; 2169 int has_array = 0; 2170 enum tree_code code = NEW_EXPR; 2171 int use_cookie, nothrow, check_new; 2172 int use_global_new; 2173 2174 placement = TREE_OPERAND (exp, 0); 2175 type = TREE_OPERAND (exp, 1); 2176 init = TREE_OPERAND (exp, 2); 2177 use_global_new = NEW_EXPR_USE_GLOBAL (exp); 2178 2179 if (TREE_CODE (type) == ARRAY_REF) 2180 { 2181 has_array = 1; 2182 nelts = TREE_OPERAND (type, 1); 2183 type = TREE_OPERAND (type, 0); 2184 } 2185 true_type = type; 2186 |
2187 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)) 2188 type = TYPE_MAIN_VARIANT (type); 2189 2190 /* If our base type is an array, then make sure we know how many elements 2191 it has. */ 2192 while (TREE_CODE (true_type) == ARRAY_TYPE) 2193 { 2194 tree this_nelts = array_type_nelts_top (true_type); 2195 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1); 2196 true_type = TREE_TYPE (true_type); 2197 } |
2198 2199 if (!complete_type_or_else (true_type)) 2200 return error_mark_node; 2201 |
2202 if (has_array) 2203 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type), 2204 nelts, 1)); 2205 else 2206 size = size_in_bytes (type); 2207 |
2208 if (TREE_CODE (true_type) == VOID_TYPE) |
2209 { 2210 error ("invalid type `void' for new"); 2211 return error_mark_node; 2212 } 2213 |
2214 if (TYPE_LANG_SPECIFIC (true_type) 2215 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type)) 2216 { 2217 abstract_virtuals_error (NULL_TREE, true_type); 2218 return error_mark_node; 2219 } 2220 2221 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type)) 2222 { 2223 signature_error (NULL_TREE, true_type); 2224 return error_mark_node; 2225 } 2226 |
2227#if 1 |
2228 /* Get a little extra space to store a couple of things before the new'ed |
2229 array, if this isn't the default placement new. */ 2230 2231 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type) 2232 && ! (placement && ! TREE_CHAIN (placement) 2233 && TREE_TYPE (TREE_VALUE (placement)) == ptr_type_node)); 2234#else 2235 /* Get a little extra space to store a couple of things before the new'ed 2236 array, if this is either non-placement new or new (nothrow). */ 2237 2238 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type) 2239 && (! placement || nothrow)); 2240#endif 2241 2242 if (use_cookie) |
2243 { 2244 tree extra = BI_header_size; 2245 2246 size = size_binop (PLUS_EXPR, size, extra); 2247 } 2248 2249 if (has_array) |
2250 { |
2251 code = VEC_NEW_EXPR; 2252 2253 if (init && pedantic) 2254 cp_pedwarn ("initialization in array new"); |
2255 } |
2256 2257 /* Allocate the object. */ 2258 2259 if (! has_array && ! placement && flag_this_is_variable > 0 2260 && TYPE_NEEDS_CONSTRUCTING (true_type) && init != void_type_node) |
2261 { 2262 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST) 2263 rval = NULL_TREE; 2264 else 2265 { 2266 error ("constructors take parameter lists"); 2267 return error_mark_node; 2268 } 2269 } 2270 else 2271 { |
2272 int susp; 2273 2274 if (flag_exceptions) 2275 /* We will use RVAL when generating an exception handler for 2276 this new-expression, so we must save it. */ 2277 susp = suspend_momentary (); 2278 2279 rval = build_op_new_call 2280 (code, true_type, expr_tree_cons (NULL_TREE, size, placement), 2281 LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL)); 2282 rval = cp_convert (build_pointer_type (true_type), rval); 2283 2284 if (flag_exceptions) 2285 resume_momentary (susp); |
2286 } 2287 |
2288 /* unless an allocation function is declared with an empty excep- 2289 tion-specification (_except.spec_), throw(), it indicates failure to 2290 allocate storage by throwing a bad_alloc exception (clause _except_, 2291 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo- 2292 cation function is declared with an empty exception-specification, 2293 throw(), it returns null to indicate failure to allocate storage and a 2294 non-null pointer otherwise. 2295 2296 So check for a null exception spec on the op new we just called. */ 2297 2298 nothrow = 0; 2299 if (rval) |
2300 { |
2301 /* The CALL_EXPR. */ 2302 tree t = TREE_OPERAND (rval, 0); 2303 /* The function. */ 2304 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0); 2305 t = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t)); |
2306 |
2307 if (t && TREE_VALUE (t) == NULL_TREE) 2308 nothrow = 1; |
2309 } |
2310 check_new = flag_check_new || nothrow; 2311 2312 if ((check_new || flag_exceptions) && rval) 2313 { 2314 alloc_expr = get_target_expr (rval); 2315 alloc_node = rval = TREE_OPERAND (alloc_expr, 0); 2316 } |
2317 else 2318 alloc_expr = NULL_TREE; 2319 2320 /* if rval is NULL_TREE I don't have to allocate it, but are we totally 2321 sure we have some extra bytes in that case for the BI_header_size 2322 cookies? And how does that interact with the code below? (mrs) */ 2323 /* Finish up some magic for new'ed arrays */ |
2324 if (use_cookie && rval != NULL_TREE) |
2325 { 2326 tree extra = BI_header_size; 2327 tree cookie, exp1; |
2328 rval = convert (string_type_node, rval); /* for ptr arithmetic */ |
2329 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1)); 2330 /* Store header info. */ |
2331 cookie = build_indirect_ref (build (MINUS_EXPR, 2332 build_pointer_type (BI_header_type), |
2333 rval, extra), NULL_PTR); 2334 exp1 = build (MODIFY_EXPR, void_type_node, |
2335 build_component_ref (cookie, nc_nelts_field_id, 2336 NULL_TREE, 0), |
2337 nelts); 2338 TREE_SIDE_EFFECTS (exp1) = 1; |
2339 rval = cp_convert (build_pointer_type (true_type), rval); 2340 rval = build_compound_expr 2341 (expr_tree_cons (NULL_TREE, exp1, 2342 build_expr_list (NULL_TREE, rval))); |
2343 } 2344 2345 if (rval == error_mark_node) 2346 return error_mark_node; 2347 2348 /* Don't call any constructors or do any initialization. */ 2349 if (init == void_type_node) 2350 goto done; --- 22 unchanged lines hidden (view full) --- 2373 2374 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL, 2375 "new", NULL_TREE, 0); 2376 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), 2377 build_modify_expr (deref, NOP_EXPR, init), 2378 rval); 2379 TREE_NO_UNUSED_WARNING (rval) = 1; 2380 TREE_SIDE_EFFECTS (rval) = 1; |
2381 } 2382 else if (! has_array) 2383 { 2384 tree newrval; 2385 /* Constructors are never virtual. If it has an initialization, we 2386 need to complain if we aren't allowed to use the ctor that took 2387 that argument. */ 2388 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN; 2389 2390 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type)) 2391 { |
2392 init = expr_tree_cons (NULL_TREE, integer_one_node, init); |
2393 flags |= LOOKUP_HAS_IN_CHARGE; 2394 } 2395 2396 newrval = rval; 2397 2398 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE) 2399 newrval = build_indirect_ref (newrval, NULL_PTR); 2400 |
2401 newrval = build_method_call (newrval, ctor_identifier, 2402 init, TYPE_BINFO (true_type), flags); |
2403 |
2404 if (newrval == NULL_TREE || newrval == error_mark_node) 2405 return error_mark_node; |
2406 |
2407 rval = newrval; 2408 TREE_HAS_CONSTRUCTOR (rval) = 1; |
2409 } 2410 else |
2411 rval = build (VEC_INIT_EXPR, TREE_TYPE (rval), 2412 save_expr (rval), init, nelts); 2413 2414 /* If any part of the object initialization terminates by throwing 2415 an exception and the new-expression does not contain a 2416 new-placement, then the deallocation function is called to free 2417 the memory in which the object was being constructed. */ 2418 if (flag_exceptions && alloc_expr) |
2419 { |
2420 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR; 2421 tree cleanup, fn = NULL_TREE; 2422 int flags = LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL); |
2423 |
2424 /* All cleanups must last longer than normal. */ 2425 int yes = suspend_momentary (); 2426 2427 if (placement) |
2428 { |
2429 flags |= LOOKUP_SPECULATIVELY; 2430 2431 /* We expect alloc_expr to look like a TARGET_EXPR around 2432 a NOP_EXPR around the CALL_EXPR we want. */ 2433 fn = TREE_OPERAND (alloc_expr, 1); 2434 fn = TREE_OPERAND (fn, 0); |
2435 } 2436 |
2437 /* Copy size to the saveable obstack. */ 2438 size = copy_node (size); |
2439 |
2440 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn); |
2441 |
2442 resume_momentary (yes); |
2443 |
2444 /* Ack! First we allocate the memory. Then we set our sentry 2445 variable to true, and expand a cleanup that deletes the memory 2446 if sentry is true. Then we run the constructor and store the 2447 returned pointer in buf. Then we clear sentry and return buf. */ |
2448 |
2449 if (cleanup) |
2450 { |
2451#if 0 2452 /* Disable this until flow is fixed so that it doesn't 2453 think the initialization of sentry is a dead write. */ 2454 tree end, sentry, begin, buf, t = TREE_TYPE (rval); 2455 2456 begin = get_target_expr (boolean_true_node); 2457 sentry = TREE_OPERAND (begin, 0); 2458 2459 yes = suspend_momentary (); 2460 TREE_OPERAND (begin, 2) 2461 = build (COND_EXPR, void_type_node, sentry, 2462 cleanup, void_zero_node); 2463 resume_momentary (yes); 2464 2465 rval = get_target_expr (rval); 2466 2467 end = build (MODIFY_EXPR, TREE_TYPE (sentry), 2468 sentry, boolean_false_node); 2469 TREE_SIDE_EFFECTS (end) = 1; 2470 2471 buf = TREE_OPERAND (rval, 0); 2472 2473 rval = build (COMPOUND_EXPR, t, begin, 2474 build (COMPOUND_EXPR, t, rval, 2475 build (COMPOUND_EXPR, t, end, buf))); 2476#else 2477 /* FIXME: this is a workaround for a crash due to overlapping 2478 exception regions. Cleanups shouldn't really happen here. */ 2479 rval = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (rval), rval); 2480 2481 rval = build (TRY_CATCH_EXPR, TREE_TYPE (rval), rval, cleanup); 2482 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval); 2483#endif |
2484 } |
2485 } 2486 } 2487 else if (TYPE_READONLY (true_type)) 2488 cp_error ("uninitialized const in `new' of `%#T'", true_type); 2489 2490 done: 2491 |
2492 if (alloc_expr && rval == alloc_node) |
2493 { |
2494 rval = TREE_OPERAND (alloc_expr, 1); 2495 alloc_expr = NULL_TREE; |
2496 } 2497 |
2498 if (check_new && alloc_expr) |
2499 { |
2500 /* Did we modify the storage? */ 2501 tree ifexp = build_binary_op (NE_EXPR, alloc_node, 2502 integer_zero_node, 1); 2503 rval = build_conditional_expr (ifexp, rval, alloc_node); |
2504 } 2505 |
2506 if (alloc_expr) 2507 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval); |
2508 |
2509 if (rval && TREE_TYPE (rval) != build_pointer_type (type)) |
2510 { |
2511 /* The type of new int [3][3] is not int *, but int [3] * */ 2512 rval = build_c_cast (build_pointer_type (type), rval); |
2513 } 2514 2515 return rval; 2516} 2517 2518static tree 2519build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete, 2520 use_global_delete) 2521 tree base, maxindex, type; 2522 tree auto_delete_vec, auto_delete; 2523 int use_global_delete; 2524{ 2525 tree virtual_size; |
2526 tree ptype = build_pointer_type (type = complete_type (type)); |
2527 tree size_exp = size_in_bytes (type); 2528 2529 /* Temporary variables used by the loop. */ 2530 tree tbase, tbase_init; 2531 2532 /* This is the body of the loop that implements the deletion of a 2533 single element, and moves temp variables to next elements. */ 2534 tree body; --- 5 unchanged lines hidden (view full) --- 2540 tree deallocate_expr = 0; 2541 2542 /* This is the BIND_EXPR which holds the outermost iterator of the 2543 loop. It is convenient to set this variable up and test it before 2544 executing any other code in the loop. 2545 This is also the containing expression returned by this function. */ 2546 tree controller = NULL_TREE; 2547 |
2548 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type)) 2549 { 2550 loop = integer_zero_node; 2551 goto no_destructor; 2552 } 2553 2554 /* The below is short by BI_header_size */ 2555 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex)); 2556 2557 tbase = build_decl (VAR_DECL, NULL_TREE, ptype); 2558 tbase_init = build_modify_expr (tbase, NOP_EXPR, 2559 fold (build (PLUS_EXPR, ptype, 2560 base, 2561 virtual_size))); 2562 DECL_REGISTER (tbase) = 1; |
2563 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE); |
2564 TREE_SIDE_EFFECTS (controller) = 1; |
2565 2566 if (auto_delete != integer_zero_node 2567 && auto_delete != integer_two_node) 2568 { |
2569 tree base_tbd = cp_convert (ptype, 2570 build_binary_op (MINUS_EXPR, 2571 cp_convert (ptr_type_node, base), 2572 BI_header_size, 2573 1)); |
2574 /* This is the real size */ 2575 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size); |
2576 body = build_expr_list (NULL_TREE, |
2577 build_x_delete (ptype, base_tbd, 2578 2 | use_global_delete, 2579 virtual_size)); 2580 body = build (COND_EXPR, void_type_node, 2581 build (BIT_AND_EXPR, integer_type_node, 2582 auto_delete, integer_one_node), 2583 body, integer_zero_node); 2584 } 2585 else 2586 body = NULL_TREE; 2587 |
2588 body = expr_tree_cons (NULL_TREE, |
2589 build_delete (ptype, tbase, auto_delete, 2590 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1), 2591 body); 2592 |
2593 body = expr_tree_cons (NULL_TREE, |
2594 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)), 2595 body); 2596 |
2597 body = expr_tree_cons (NULL_TREE, |
2598 build (EXIT_EXPR, void_type_node, 2599 build (EQ_EXPR, boolean_type_node, base, tbase)), 2600 body); 2601 2602 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body)); 2603 |
2604 loop = expr_tree_cons (NULL_TREE, tbase_init, 2605 expr_tree_cons (NULL_TREE, loop, NULL_TREE)); |
2606 loop = build_compound_expr (loop); 2607 2608 no_destructor: 2609 /* If the delete flag is one, or anything else with the low bit set, 2610 delete the storage. */ 2611 if (auto_delete_vec == integer_zero_node 2612 || auto_delete_vec == integer_two_node) 2613 deallocate_expr = integer_zero_node; --- 4 unchanged lines hidden (view full) --- 2618 /* The below is short by BI_header_size */ 2619 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex)); 2620 2621 if (! TYPE_VEC_NEW_USES_COOKIE (type)) 2622 /* no header */ 2623 base_tbd = base; 2624 else 2625 { |
2626 base_tbd = cp_convert (ptype, 2627 build_binary_op (MINUS_EXPR, 2628 cp_convert (string_type_node, base), 2629 BI_header_size, 2630 1)); 2631 /* True size with header. */ |
2632 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size); 2633 } 2634 deallocate_expr = build_x_delete (ptype, base_tbd, 2635 2 | use_global_delete, 2636 virtual_size); 2637 if (auto_delete_vec != integer_one_node) 2638 deallocate_expr = build (COND_EXPR, void_type_node, 2639 build (BIT_AND_EXPR, integer_type_node, 2640 auto_delete_vec, integer_one_node), 2641 deallocate_expr, integer_zero_node); 2642 } 2643 2644 if (loop && deallocate_expr != integer_zero_node) 2645 { |
2646 body = expr_tree_cons (NULL_TREE, loop, 2647 expr_tree_cons (NULL_TREE, deallocate_expr, NULL_TREE)); |
2648 body = build_compound_expr (body); 2649 } 2650 else 2651 body = loop; 2652 2653 /* Outermost wrapper: If pointer is null, punt. */ 2654 body = build (COND_EXPR, void_type_node, 2655 build (NE_EXPR, boolean_type_node, base, integer_zero_node), 2656 body, integer_zero_node); 2657 body = build1 (NOP_EXPR, void_type_node, body); 2658 2659 if (controller) 2660 { 2661 TREE_OPERAND (controller, 1) = body; 2662 return controller; 2663 } 2664 else |
2665 return cp_convert (void_type_node, body); |
2666} 2667 2668/* Build a tree to cleanup partially built arrays. 2669 BASE is that starting address of the array. 2670 COUNT is the count of objects that have been built, that need destroying. 2671 TYPE is the type of elements in the array. */ |
2672 |
2673static tree 2674build_array_eh_cleanup (base, count, type) 2675 tree base, count, type; 2676{ 2677 tree expr = build_vec_delete_1 (base, count, type, integer_two_node, 2678 integer_zero_node, 0); 2679 return expr; 2680} --- 20 unchanged lines hidden (view full) --- 2701 tree decl, base, maxindex, init; 2702 int from_array; 2703{ 2704 tree rval; 2705 tree iterator, base2 = NULL_TREE; 2706 tree type = TREE_TYPE (TREE_TYPE (base)); 2707 tree size; 2708 |
2709 maxindex = cp_convert (ptrdiff_type_node, maxindex); |
2710 if (maxindex == error_mark_node) 2711 return error_mark_node; 2712 2713 if (current_function_decl == NULL_TREE) 2714 { 2715 rval = make_tree_vec (3); 2716 TREE_VEC_ELT (rval, 0) = base; 2717 TREE_VEC_ELT (rval, 1) = maxindex; 2718 TREE_VEC_ELT (rval, 2) = init; 2719 return rval; 2720 } 2721 2722 size = size_in_bytes (type); 2723 2724 /* Set to zero in case size is <= 0. Optimizer will delete this if 2725 it is not needed. */ 2726 rval = get_temp_regvar (build_pointer_type (type), |
2727 cp_convert (build_pointer_type (type), null_pointer_node)); |
2728 base = default_conversion (base); |
2729 base = cp_convert (build_pointer_type (type), base); |
2730 expand_assignment (rval, base, 0, 0); 2731 base = get_temp_regvar (build_pointer_type (type), base); 2732 2733 if (init != NULL_TREE 2734 && TREE_CODE (init) == CONSTRUCTOR |
2735 && (! decl || TREE_TYPE (init) == TREE_TYPE (decl))) |
2736 { 2737 /* Initialization of array from {...}. */ 2738 tree elts = CONSTRUCTOR_ELTS (init); 2739 tree baseref = build1 (INDIRECT_REF, type, base); 2740 tree baseinc = build (PLUS_EXPR, build_pointer_type (type), base, size); 2741 int host_i = TREE_INT_CST_LOW (maxindex); 2742 2743 if (IS_AGGR_TYPE (type)) --- 11 unchanged lines hidden (view full) --- 2755 { 2756 if (TYPE_NEEDS_CONSTRUCTING (type) == 0) 2757 { 2758 if (obey_regdecls) 2759 use_variable (DECL_RTL (base)); 2760 goto done_init; 2761 } 2762 |
2763 iterator = get_temp_regvar (ptrdiff_type_node, |
2764 build_int_2 (host_i, 0)); 2765 init = NULL_TREE; 2766 goto init_by_default; 2767 } 2768 } 2769 else 2770 while (elts) 2771 { --- 5 unchanged lines hidden (view full) --- 2777 2778 if (obey_regdecls) 2779 use_variable (DECL_RTL (base)); 2780 } 2781 else 2782 { 2783 tree itype; 2784 |
2785 iterator = get_temp_regvar (ptrdiff_type_node, maxindex); |
2786 2787 init_by_default: |
2788 itype = NULL_TREE; |
2789 2790 /* If initializing one array from another, 2791 initialize element by element. */ 2792 if (from_array) 2793 { 2794 /* We rely upon the below calls the do argument checking */ 2795 if (decl == NULL_TREE) 2796 { --- 14 unchanged lines hidden (view full) --- 2811 error ("initializer ends prematurely"); 2812 return error_mark_node; 2813 } 2814 } 2815 2816 expand_start_cond (build (GE_EXPR, boolean_type_node, 2817 iterator, integer_zero_node), 0); 2818 if (TYPE_NEEDS_DESTRUCTOR (type)) |
2819 expand_eh_region_start (); |
2820 expand_start_loop_continue_elsewhere (1); 2821 |
2822 /* The initialization of each array element is a full-expression. */ 2823 expand_start_target_temps (); 2824 |
2825 if (from_array) 2826 { 2827 tree to = build1 (INDIRECT_REF, type, base); 2828 tree from; 2829 2830 if (base2) 2831 from = build1 (INDIRECT_REF, itype, base2); 2832 else --- 19 unchanged lines hidden (view full) --- 2852 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0, 0); 2853 2854 expand_assignment (base, 2855 build (PLUS_EXPR, build_pointer_type (type), base, size), 2856 0, 0); 2857 if (base2) 2858 expand_assignment (base2, 2859 build (PLUS_EXPR, build_pointer_type (type), base2, size), 0, 0); |
2860 2861 /* Cleanup any temporaries needed for the initial value. */ 2862 expand_end_target_temps (); 2863 |
2864 expand_loop_continue_here (); 2865 expand_exit_loop_if_false (0, build (NE_EXPR, boolean_type_node, |
2866 build (PREDECREMENT_EXPR, ptrdiff_type_node, iterator, integer_one_node), minus_one)); |
2867 2868 if (obey_regdecls) 2869 { 2870 use_variable (DECL_RTL (base)); 2871 if (base2) 2872 use_variable (DECL_RTL (base2)); 2873 } 2874 expand_end_loop (); |
2875 if (TYPE_NEEDS_DESTRUCTOR (type) && flag_exceptions) 2876 { 2877 /* We have to ensure that this can live to the cleanup 2878 expansion time, since we know it is only ever needed 2879 once, generate code now. */ 2880 push_obstacks_nochange (); 2881 resume_temporary_allocation (); 2882 { 2883 tree e1, cleanup = make_node (RTL_EXPR); 2884 TREE_TYPE (cleanup) = void_type_node; 2885 RTL_EXPR_RTL (cleanup) = const0_rtx; 2886 TREE_SIDE_EFFECTS (cleanup) = 1; 2887 do_pending_stack_adjust (); 2888 start_sequence_for_rtl_expr (cleanup); 2889 2890 e1 = build_array_eh_cleanup 2891 (rval, 2892 build_binary_op (MINUS_EXPR, maxindex, iterator, 1), 2893 type); 2894 expand_expr (e1, const0_rtx, VOIDmode, EXPAND_NORMAL); 2895 do_pending_stack_adjust (); 2896 RTL_EXPR_SEQUENCE (cleanup) = get_insns (); 2897 end_sequence (); 2898 2899 cleanup = protect_with_terminate (cleanup); 2900 expand_eh_region_end (cleanup); 2901 } 2902 pop_obstacks (); 2903 } |
2904 expand_end_cond (); 2905 if (obey_regdecls) 2906 use_variable (DECL_RTL (iterator)); 2907 } 2908 done_init: 2909 2910 if (obey_regdecls) 2911 use_variable (DECL_RTL (rval)); --- 8 unchanged lines hidden (view full) --- 2920 VIRTUAL_SIZE is the amount of storage that was allocated, and is 2921 used as the second argument to operator delete. It can include 2922 things like padding and magic size cookies. It has virtual in it, 2923 because if you have a base pointer and you delete through a virtual 2924 destructor, it should be the size of the dynamic object, not the 2925 static object, see Free Store 12.5 ANSI C++ WP. 2926 2927 This does not call any destructors. */ |
2928 |
2929tree 2930build_x_delete (type, addr, which_delete, virtual_size) 2931 tree type, addr; 2932 int which_delete; 2933 tree virtual_size; 2934{ 2935 int use_global_delete = which_delete & 1; 2936 int use_vec_delete = !!(which_delete & 2); |
2937 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR; |
2938 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL); |
2939 |
2940 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE); |
2941} 2942 2943/* Generate a call to a destructor. TYPE is the type to cast ADDR to. 2944 ADDR is an expression which yields the store to be destroyed. 2945 AUTO_DELETE is nonzero if a call to DELETE should be made or not. 2946 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the 2947 virtual baseclasses. 2948 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate. 2949 2950 FLAGS is the logical disjunction of zero or more LOOKUP_ 2951 flags. See cp-tree.h for more info. 2952 2953 This function does not delete an object's virtual base classes. */ |
2954 |
2955tree 2956build_delete (type, addr, auto_delete, flags, use_global_delete) 2957 tree type, addr; 2958 tree auto_delete; 2959 int flags; 2960 int use_global_delete; 2961{ |
2962 tree member; 2963 tree expr; 2964 tree ref; |
2965 2966 if (addr == error_mark_node) 2967 return error_mark_node; 2968 2969 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type 2970 set to `error_mark_node' before it gets properly cleaned up. */ 2971 if (type == error_mark_node) 2972 return error_mark_node; 2973 2974 type = TYPE_MAIN_VARIANT (type); 2975 2976 if (TREE_CODE (type) == POINTER_TYPE) 2977 { 2978 type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
2979 if (!complete_type_or_else (type)) 2980 return error_mark_node; |
2981 if (TREE_CODE (type) == ARRAY_TYPE) 2982 goto handle_array; 2983 if (! IS_AGGR_TYPE (type)) 2984 { 2985 /* Call the builtin operator delete. */ 2986 return build_builtin_call (void_type_node, BID, |
2987 build_expr_list (NULL_TREE, addr)); |
2988 } 2989 if (TREE_SIDE_EFFECTS (addr)) 2990 addr = save_expr (addr); 2991 2992 /* throw away const and volatile on target type of addr */ 2993 addr = convert_force (build_pointer_type (type), addr, 0); 2994 ref = build_indirect_ref (addr, NULL_PTR); |
2995 } 2996 else if (TREE_CODE (type) == ARRAY_TYPE) 2997 { 2998 handle_array: 2999 if (TREE_SIDE_EFFECTS (addr)) 3000 addr = save_expr (addr); 3001 if (TYPE_DOMAIN (type) == NULL_TREE) 3002 { 3003 error ("unknown array size in delete"); 3004 return error_mark_node; 3005 } 3006 return build_vec_delete (addr, array_type_nelts (type), |
3007 auto_delete, integer_two_node, 3008 use_global_delete); 3009 } 3010 else 3011 { 3012 /* Don't check PROTECT here; leave that decision to the 3013 destructor. If the destructor is accessible, call it, 3014 else report error. */ 3015 addr = build_unary_op (ADDR_EXPR, addr, 0); 3016 if (TREE_SIDE_EFFECTS (addr)) 3017 addr = save_expr (addr); 3018 3019 if (TREE_CONSTANT (addr)) 3020 addr = convert_pointer_to (type, addr); 3021 else 3022 addr = convert_force (build_pointer_type (type), addr, 0); 3023 |
3024 ref = build_indirect_ref (addr, NULL_PTR); |
3025 } 3026 3027 my_friendly_assert (IS_AGGR_TYPE (type), 220); 3028 3029 if (! TYPE_NEEDS_DESTRUCTOR (type)) 3030 { 3031 if (auto_delete == integer_zero_node) 3032 return void_zero_node; 3033 |
3034 return build_op_delete_call 3035 (DELETE_EXPR, addr, c_sizeof_nowarn (type), 3036 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL), 3037 NULL_TREE); |
3038 } |
3039 3040 /* Below, we will reverse the order in which these calls are made. 3041 If we have a destructor, then that destructor will take care 3042 of the base classes; otherwise, we must do that here. */ 3043 if (TYPE_HAS_DESTRUCTOR (type)) 3044 { |
3045 tree passed_auto_delete; 3046 tree do_delete = NULL_TREE; |
3047 tree ifexp; |
3048 3049 if (use_global_delete) 3050 { 3051 tree cond = fold (build (BIT_AND_EXPR, integer_type_node, 3052 auto_delete, integer_one_node)); 3053 tree call = build_builtin_call |
3054 (void_type_node, BID, build_expr_list (NULL_TREE, addr)); |
3055 3056 cond = fold (build (COND_EXPR, void_type_node, cond, 3057 call, void_zero_node)); 3058 if (cond != void_zero_node) 3059 do_delete = cond; 3060 3061 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node, 3062 auto_delete, integer_two_node)); 3063 } 3064 else 3065 passed_auto_delete = auto_delete; 3066 |
3067 expr = build_method_call 3068 (ref, dtor_identifier, build_expr_list (NULL_TREE, passed_auto_delete), 3069 NULL_TREE, flags); |
3070 |
3071 if (do_delete) 3072 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete); |
3073 |
3074 if (flags & LOOKUP_DESTRUCTOR) 3075 /* Explicit destructor call; don't check for null pointer. */ 3076 ifexp = integer_one_node; |
3077 else |
3078 /* Handle deleting a null pointer. */ 3079 ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node, 1)); |
3080 |
3081 if (ifexp != integer_one_node) 3082 expr = build (COND_EXPR, void_type_node, 3083 ifexp, expr, void_zero_node); |
3084 |
3085 return expr; 3086 } 3087 else 3088 { |
3089 /* We only get here from finish_function for a destructor. */ |
3090 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type)); 3091 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0; 3092 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE; 3093 tree exprstmt = NULL_TREE; 3094 tree parent_auto_delete = auto_delete; 3095 tree cond; 3096 |
3097 /* If we have member delete or vbases, we call delete in 3098 finish_function. */ 3099 if (auto_delete == integer_zero_node) 3100 cond = NULL_TREE; |
3101 else if (base_binfo == NULL_TREE |
3102 || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))) |
3103 { |
3104 cond = build (COND_EXPR, void_type_node, 3105 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node), 3106 build_builtin_call (void_type_node, BID, |
3107 build_expr_list (NULL_TREE, addr)), |
3108 void_zero_node); 3109 } 3110 else 3111 cond = NULL_TREE; 3112 3113 if (cond) |
3114 exprstmt = build_expr_list (NULL_TREE, cond); |
3115 3116 if (base_binfo 3117 && ! TREE_VIA_VIRTUAL (base_binfo) 3118 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))) 3119 { 3120 tree this_auto_delete; 3121 3122 if (BINFO_OFFSET_ZEROP (base_binfo)) 3123 this_auto_delete = parent_auto_delete; 3124 else 3125 this_auto_delete = integer_zero_node; 3126 |
3127 expr = build_scoped_method_call 3128 (ref, base_binfo, dtor_identifier, 3129 build_expr_list (NULL_TREE, this_auto_delete)); 3130 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt); |
3131 } 3132 3133 /* Take care of the remaining baseclasses. */ 3134 for (i = 1; i < n_baseclasses; i++) 3135 { 3136 base_binfo = TREE_VEC_ELT (binfos, i); 3137 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)) 3138 || TREE_VIA_VIRTUAL (base_binfo)) 3139 continue; 3140 |
3141 expr = build_scoped_method_call 3142 (ref, base_binfo, dtor_identifier, 3143 build_expr_list (NULL_TREE, integer_zero_node)); |
3144 |
3145 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt); |
3146 } 3147 3148 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member)) 3149 { 3150 if (TREE_CODE (member) != FIELD_DECL) 3151 continue; 3152 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member))) 3153 { |
3154 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0); |
3155 tree this_type = TREE_TYPE (member); 3156 expr = build_delete (this_type, this_member, integer_two_node, flags, 0); |
3157 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt); |
3158 } 3159 } 3160 3161 if (exprstmt) 3162 return build_compound_expr (exprstmt); 3163 /* Virtual base classes make this function do nothing. */ 3164 return void_zero_node; 3165 } --- 10 unchanged lines hidden (view full) --- 3176 tree addr = build_unary_op (ADDR_EXPR, decl, 0); 3177 3178 my_friendly_assert (addr != error_mark_node, 222); 3179 3180 while (vbases) 3181 { 3182 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)), 3183 addr, 0); |
3184 result = expr_tree_cons (NULL_TREE, |
3185 build_delete (TREE_TYPE (this_addr), this_addr, 3186 integer_zero_node, 3187 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0), 3188 result); 3189 vbases = TREE_CHAIN (vbases); 3190 } 3191 return build_compound_expr (nreverse (result)); 3192} --- 9 unchanged lines hidden (view full) --- 3202 This also calls delete for virtual baseclasses of elements of the vector. 3203 3204 Update: MAXINDEX is no longer needed. The size can be extracted from the 3205 start of the vector for pointers, and from the type for arrays. We still 3206 use MAXINDEX for arrays because it happens to already have one of the 3207 values we'd have to extract. (We could use MAXINDEX with pointers to 3208 confirm the size, and trap if the numbers differ; not clear that it'd 3209 be worth bothering.) */ |
3210 |
3211tree |
3212build_vec_delete (base, maxindex, auto_delete_vec, auto_delete, |
3213 use_global_delete) |
3214 tree base, maxindex; |
3215 tree auto_delete_vec, auto_delete; 3216 int use_global_delete; 3217{ 3218 tree type; 3219 3220 if (TREE_CODE (base) == OFFSET_REF) 3221 base = resolve_offset_ref (base); 3222 3223 type = TREE_TYPE (base); 3224 3225 base = stabilize_reference (base); 3226 |
3227 /* Since we can use base many times, save_expr it. */ |
3228 if (TREE_SIDE_EFFECTS (base)) 3229 base = save_expr (base); 3230 3231 if (TREE_CODE (type) == POINTER_TYPE) 3232 { 3233 /* Step back one from start of vector, and read dimension. */ 3234 tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type), 3235 base, BI_header_size); 3236 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR); |
3237 maxindex = build_component_ref (cookie, nc_nelts_field_id, NULL_TREE, 0); |
3238 do 3239 type = TREE_TYPE (type); 3240 while (TREE_CODE (type) == ARRAY_TYPE); 3241 } 3242 else if (TREE_CODE (type) == ARRAY_TYPE) 3243 { 3244 /* get the total number of things in the array, maxindex is a bad name */ 3245 maxindex = array_type_nelts_total (type); 3246 while (TREE_CODE (type) == ARRAY_TYPE) 3247 type = TREE_TYPE (type); 3248 base = build_unary_op (ADDR_EXPR, base, 1); 3249 } 3250 else 3251 { |
3252 if (base != error_mark_node) 3253 error ("type to vector delete is neither pointer or array type"); |
3254 return error_mark_node; 3255 } 3256 3257 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete, 3258 use_global_delete); 3259} |