1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* High-level class interface. */
33 /* In C++, structures with well-defined constructors are initialized by
34 those constructors, unasked. CURRENT_BASE_INIT_LIST
35 holds a list of stmts for a BASE_INIT term in the grammar.
36 This list has one element for each base class which must be
37 initialized. The list elements are [basename, init], with
38 type basetype. This allows the possibly anachronistic form
39 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
40 where each successive term can be handed down the constructor
41 line. Perhaps this was not intended. */
42 tree current_base_init_list, current_member_init_list;
44 void emit_base_init ();
45 void check_base_init ();
46 static void expand_aggr_vbase_init ();
47 void expand_member_init ();
48 void expand_aggr_init ();
50 static void expand_aggr_init_1 ();
51 static void expand_recursive_init_1 ();
52 static void expand_recursive_init ();
53 tree expand_vec_init ();
54 tree build_vec_delete ();
56 static void add_friend (), add_friends ();
58 /* Cache _builtin_new and _builtin_delete exprs. */
61 /* Cache the identifier nodes for the two magic field of a new cookie. */
62 static tree nc_nelts_field_id;
63 static tree nc_ptr_2comp_field_id;
65 static tree minus_one;
67 /* Set up local variable for this file. MUST BE CALLED AFTER
68 INIT_DECL_PROCESSING. */
70 tree BI_header_type, BI_header_size;
72 void init_init_processing ()
76 /* Define implicit `operator new' and `operator delete' functions. */
77 BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR])));
78 TREE_USED (TREE_OPERAND (BIN, 0)) = 0;
79 BID = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR])));
80 TREE_USED (TREE_OPERAND (BID, 0)) = 0;
81 minus_one = build_int_2 (-1, -1);
83 /* Define the structure that holds header information for
84 arrays allocated via operator new. */
85 BI_header_type = make_lang_type (RECORD_TYPE);
86 nc_nelts_field_id = get_identifier ("nelts");
87 fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
88 finish_builtin_type (BI_header_type, "__new_cookie", fields,
90 BI_header_size = size_in_bytes (BI_header_type);
93 /* Subroutine of emit_base_init. For BINFO, initialize all the
94 virtual function table pointers, except those that come from
95 virtual base classes. Initialize binfo's vtable pointer, if
96 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual
97 function table pointers in all bases have been initialized already,
98 probably because their constructors have just be run. */
100 init_vtbl_ptrs (binfo, init_self, can_elide)
102 int init_self, can_elide;
105 tree binfos = BINFO_BASETYPES (binfo);
106 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
108 for (i = 0; i < n_baselinks; i++)
110 tree base_binfo = TREE_VEC_ELT (binfos, i);
111 int is_not_base_vtable =
112 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
113 if (! TREE_VIA_VIRTUAL (base_binfo))
114 init_vtbl_ptrs (base_binfo, is_not_base_vtable, can_elide);
117 /* Before turning this on, make sure it is correct. */
118 if (can_elide && ! BINFO_MODIFIED (binfo))
121 /* Should we use something besides CLASSTYPE_VFIELDS? */
122 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
124 tree base_ptr = convert_pointer_to_real (binfo, current_class_decl);
125 expand_expr_stmt (build_virtual_init (binfo, binfo, base_ptr));
130 /* Subroutine of emit_base_init. */
132 perform_member_init (member, name, init, explicit)
133 tree member, name, init;
137 tree type = TREE_TYPE (member);
139 if (TYPE_NEEDS_CONSTRUCTING (type)
140 || (init && TYPE_HAS_CONSTRUCTOR (type)))
142 /* Since `init' is already a TREE_LIST on the current_member_init_list,
143 only build it into one if we aren't already a list. */
144 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
145 init = build_tree_list (NULL_TREE, init);
147 decl = build_component_ref (C_C_D, name, 0, explicit);
150 && TREE_CODE (type) == ARRAY_TYPE
152 && TREE_CHAIN (init) == NULL_TREE
153 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
155 /* Initialization of one array from another. */
156 expand_vec_init (TREE_OPERAND (decl, 1), decl,
157 array_type_nelts (type), TREE_VALUE (init), 1);
160 expand_aggr_init (decl, init, 0);
164 if (init == NULL_TREE)
168 cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor",
169 member, current_class_type);
170 init = error_mark_node;
172 /* member traversal: note it leaves init NULL */
173 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
174 cp_pedwarn ("uninitialized reference member `%D'", member);
176 else if (TREE_CODE (init) == TREE_LIST)
178 /* There was an explicit member initialization. Do some
179 work in that case. */
180 if (TREE_CHAIN (init))
182 warning ("initializer list treated as compound expression");
183 init = build_compound_expr (init);
186 init = TREE_VALUE (init);
189 /* We only build this with a null init if we got it from the
190 current_member_init_list. */
191 if (init || explicit)
193 decl = build_component_ref (C_C_D, name, 0, explicit);
194 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
198 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
200 cplus_expand_start_try (1);
201 push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
205 /* Subroutine of emit_member_init. */
210 tree x, member, name, field, init;
211 tree init_list = NULL_TREE;
212 tree fields_to_unmark = NULL_TREE;
215 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
218 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
220 /* If we cleared this out, then pay no attention to it. */
221 if (TREE_PURPOSE (x) == NULL_TREE)
223 name = TREE_PURPOSE (x);
226 field = (TREE_CODE (name) == COMPONENT_REF
227 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
229 /* Let's find out when this happens. */
230 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
231 field = IDENTIFIER_CLASS_VALUE (name);
234 /* If one member shadows another, get the outermost one. */
235 if (TREE_CODE (field) == TREE_LIST)
236 field = TREE_VALUE (field);
240 /* See if we already found an initializer for this field. */
243 if (DECL_NAME (field))
244 cp_error ("multiple initializations given for member `%D'",
249 init_list = chainon (init_list,
250 build_tree_list (name, TREE_VALUE (x)));
251 /* Make sure we won't try to work on this init again. */
252 TREE_PURPOSE (x) = NULL_TREE;
258 /* If we didn't find MEMBER in the list, create a dummy entry
259 so the two lists (INIT_LIST and the list of members) will be
262 init_list = chainon (init_list, build_tree_list (NULL_TREE, NULL_TREE));
265 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
267 if (TREE_PURPOSE (x))
269 name = TREE_PURPOSE (x);
270 init = TREE_VALUE (x);
271 /* XXX: this may need the COMPONENT_REF operand 0 check if
272 it turns out we actually get them. */
273 field = IDENTIFIER_CLASS_VALUE (name);
275 /* If one member shadows another, get the outermost one. */
276 if (TREE_CODE (field) == TREE_LIST)
278 field = TREE_VALUE (field);
279 if (decl_type_context (field) != current_class_type)
280 cp_error ("field `%D' not in immediate context", field);
284 /* It turns out if you have an anonymous union in the
285 class, a member from it can end up not being on the
286 list of fields (rather, the type is), and therefore
287 won't be seen by the for loop above. */
289 /* The code in this for loop is derived from a general loop
290 which had this check in it. Theoretically, we've hit
291 every initialization for the list of members in T, so
292 we shouldn't have anything but these left in this list. */
293 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
296 if (TREE_HAS_CONSTRUCTOR (field))
298 if (DECL_NAME (field))
299 error ("multiple initializations given for member `%s'",
300 IDENTIFIER_POINTER (DECL_NAME (field)));
304 TREE_HAS_CONSTRUCTOR (field) = 1;
305 fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark);
307 perform_member_init (field, name, init, 1);
308 TREE_PURPOSE (x) = NULL_TREE;
312 /* Unmark fields which are initialized for the base class. */
313 while (fields_to_unmark)
315 TREE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0;
316 /* XXX is this a memory leak? */
317 fields_to_unmark = TREE_CHAIN (fields_to_unmark);
323 /* Perform whatever initializations have yet to be done on the base
324 class of the class variable. These actions are in the global
325 variable CURRENT_BASE_INIT_LIST. Such an action could be
326 NULL_TREE, meaning that the user has explicitly called the base
327 class constructor with no arguments.
329 If there is a need for a call to a constructor, we must surround
330 that call with a pushlevel/poplevel pair, since we are technically
331 at the PARM level of scope.
333 Argument IMMEDIATELY, if zero, forces a new sequence to be
334 generated to contain these new insns, so it can be emitted later.
335 This sequence is saved in the global variable BASE_INIT_INSNS.
336 Otherwise, the insns are emitted into the current sequence.
338 Note that emit_base_init does *not* initialize virtual base
339 classes. That is done specially, elsewhere. */
342 emit_base_init (t, immediately)
346 extern tree in_charge_identifier;
348 tree member, decl, vbases;
349 tree init_list, member_init;
351 tree t_binfo = TYPE_BINFO (t);
352 tree binfos = BINFO_BASETYPES (t_binfo);
353 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
354 tree fields_to_unmark = NULL_TREE;
355 int have_init_list = 0, from_init_list;
359 do_pending_stack_adjust ();
363 if (write_symbols == NO_DEBUG)
364 /* As a matter of principle, `start_sequence' should do this. */
367 /* Always emit a line number note so we can step into constructors. */
368 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
369 DECL_SOURCE_LINE (current_function_decl));
371 /* In this case, we always need IN_CHARGE_NODE, because we have
372 to know whether to deallocate or not before exiting. */
373 if (flag_handle_exceptions == 2
374 && lookup_name (in_charge_identifier, 0) == NULL_TREE)
376 tree in_charge_node = pushdecl (build_decl (VAR_DECL, in_charge_identifier,
378 store_init_value (in_charge_node, build (EQ_EXPR, integer_type_node,
381 expand_decl (in_charge_node);
382 expand_decl_init (in_charge_node);
385 start = ! TYPE_USES_VIRTUAL_BASECLASSES (t);
386 for (pass = start; pass < 2; pass++)
388 tree vbase_init_list = NULL_TREE;
390 for (init_list = current_base_init_list; init_list;
391 init_list = TREE_CHAIN (init_list))
393 tree basename = TREE_PURPOSE (init_list);
395 tree init = TREE_VALUE (init_list);
397 if (basename == NULL_TREE)
399 /* Initializer for single base class. Must not
400 use multiple inheritance or this is ambiguous. */
401 switch (n_baseclasses)
404 error ("type `%s' does not have a base class to initialize",
405 IDENTIFIER_POINTER (current_class_name));
410 error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name));
413 binfo = TREE_VEC_ELT (binfos, 0);
415 else if (is_aggr_typedef (basename, 1))
417 binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t);
418 if (binfo == NULL_TREE)
421 /* Virtual base classes are special cases. Their initializers
422 are recorded with this constructor, and they are used when
423 this constructor is the top-level constructor called. */
424 if (! TREE_VIA_VIRTUAL (binfo))
426 /* Otherwise, if it is not an immediate base class, complain. */
427 for (i = n_baseclasses-1; i >= 0; i--)
428 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
432 error ("type `%s' is not an immediate base class of type `%s'",
433 IDENTIFIER_POINTER (basename),
434 IDENTIFIER_POINTER (current_class_name));
442 /* The base initialization list goes up to the first
443 base class which can actually use it. */
447 char *msgp = (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
448 ? "cannot pass initialization up to class `%s'" : 0;
450 while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo))
451 && BINFO_BASETYPES (binfo) != NULL_TREE
452 && TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 1)
454 /* ?? This should be fixed in RENO by forcing
455 default constructors to exist. */
456 SET_BINFO_BASEINIT_MARKED (binfo);
457 binfo = BINFO_BASETYPE (binfo, 0);
460 /* We used to give an error if this wasn't true, saying that
461 there's no constructor for the initialization of basename.
462 This turned out to be incorrect---it should use the
463 default constructor, since a user could try to initialize
464 the class in a derived class's base initializer list. */
465 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
470 error_with_aggr_type (binfo, msgp);
476 if (BINFO_BASEINIT_MARKED (binfo))
478 msgp = "class `%s' initializer already specified";
479 error (msgp, IDENTIFIER_POINTER (basename));
485 SET_BINFO_BASEINIT_MARKED (binfo);
486 if (TREE_VIA_VIRTUAL (binfo))
488 vbase_init_list = tree_cons (init, BINFO_TYPE (binfo),
495 else if (TREE_VIA_VIRTUAL (binfo))
498 member = convert_pointer_to (binfo, current_class_decl);
499 expand_aggr_init_1 (t_binfo, 0,
500 build_indirect_ref (member, NULL_PTR), init,
501 BINFO_OFFSET_ZEROP (binfo), LOOKUP_COMPLAIN);
502 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
504 cplus_expand_start_try (1);
505 push_exception_cleanup (member);
511 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
514 if (DECL_NAME (current_function_decl) == NULL_TREE
515 && TREE_CHAIN (first_arg) != NULL_TREE)
517 /* If there are virtual baseclasses without initialization
518 specified, and this is a default X(X&) constructor,
519 build the initialization list so that each virtual baseclass
520 of the new object is initialized from the virtual baseclass
521 of the incoming arg. */
522 tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0);
523 for (vbases = CLASSTYPE_VBASECLASSES (t);
524 vbases; vbases = TREE_CHAIN (vbases))
526 if (BINFO_BASEINIT_MARKED (vbases) == 0)
528 member = convert_pointer_to (vbases, init_arg);
529 if (member == init_arg)
530 member = TREE_CHAIN (first_arg);
532 TREE_TYPE (member) = build_reference_type (BINFO_TYPE (vbases));
533 vbase_init_list = tree_cons (convert_from_reference (member),
534 vbases, vbase_init_list);
535 SET_BINFO_BASEINIT_MARKED (vbases);
539 expand_start_cond (first_arg, 0);
540 expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl,
545 current_base_init_list = NULL_TREE;
547 /* Now, perform default initialization of all base classes which
548 have not yet been initialized, and unmark baseclasses which
549 have been initialized. */
550 for (i = 0; i < n_baseclasses; i++)
552 tree base = current_class_decl;
553 tree base_binfo = TREE_VEC_ELT (binfos, i);
555 if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
557 if (! TREE_VIA_VIRTUAL (base_binfo)
558 && ! BINFO_BASEINIT_MARKED (base_binfo))
562 if (BINFO_OFFSET_ZEROP (base_binfo))
563 base = build1 (NOP_EXPR,
564 TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
567 base = build (PLUS_EXPR,
568 TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
569 current_class_decl, BINFO_OFFSET (base_binfo));
571 ref = build_indirect_ref (base, NULL_PTR);
572 expand_aggr_init_1 (t_binfo, 0, ref, NULL_TREE,
573 BINFO_OFFSET_ZEROP (base_binfo),
575 if (flag_handle_exceptions == 2
576 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
578 cplus_expand_start_try (1);
579 push_exception_cleanup (base);
583 CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
585 if (! TYPE_USES_VIRTUAL_BASECLASSES (t))
587 while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo))
588 && BINFO_BASETYPES (base_binfo) != NULL_TREE
589 && TREE_VEC_LENGTH (BINFO_BASETYPES (base_binfo)) == 1)
591 /* ?? This should be fixed in RENO by forcing
592 default constructors to exist. It is needed for symmetry
594 base_binfo = BINFO_BASETYPE (base_binfo, 0);
595 CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
600 /* Initialize all the virtual function table fields that
601 do come from virtual base classes. */
602 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
603 expand_expr_stmt (build_vbase_vtables_init (t_binfo, t_binfo,
604 C_C_D, current_class_decl, 0));
605 for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
606 CLEAR_BINFO_BASEINIT_MARKED (vbases);
608 /* Initialize all the virtual function table fields that
609 do not come from virtual base classes. */
610 init_vtbl_ptrs (t_binfo, 0, 1);
612 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (t))
613 expand_expr_stmt (build_virtual_init (TYPE_BINFO (t), t,
614 current_class_decl));
616 if (current_member_init_list)
618 init_list = sort_member_init (t);
622 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
627 /* See if we had a user-specified member initialization. */
630 if (TREE_PURPOSE (init_list))
632 name = TREE_PURPOSE (init_list);
633 init = TREE_VALUE (init_list);
636 if (TREE_STATIC (member))
638 error_with_aggr_type (DECL_FIELD_CONTEXT (member),
639 "field `%s::%s' is static; only point of initialization is its declaration",
640 IDENTIFIER_POINTER (TREE_PURPOSE (init_list)));
644 /* Also see if it's ever a COMPONENT_REF here. If it is, we
645 need to do `expand_assignment (name, init, 0, 0);' and
647 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
650 init_list = TREE_CHAIN (init_list);
653 if (! from_init_list)
655 /* member could be, for example, a CONST_DECL for an enumerated
656 tag; we don't want to try to initialize that, since it already
658 if (TREE_CODE (member) != FIELD_DECL)
661 name = DECL_NAME (member);
662 init = DECL_INITIAL (member);
665 perform_member_init (member, name, init, from_init_list);
668 current_member_init_list = NULL_TREE;
670 /* It is possible for the initializers to need cleanups.
671 Expand those cleanups now that all the initialization
673 expand_cleanups_to (NULL_TREE);
677 extern rtx base_init_insns;
679 do_pending_stack_adjust ();
680 my_friendly_assert (base_init_insns == 0, 207);
681 base_init_insns = get_insns ();
685 /* All the implicit try blocks we built up will be zapped
686 when we come to a real binding contour boundary. */
689 /* Check that all fields are properly initialized after
690 an assignment to `this'. */
696 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
697 if (DECL_NAME (member) && TREE_USED (member))
698 cp_error ("field `%D' used before initialized (after assignment to `this')",
702 /* This code sets up the virtual function tables appropriate for
703 the pointer DECL. It is a one-ply initialization.
705 BINFO is the exact type that DECL is supposed to be. In
706 multiple inheritance, this might mean "C's A" if C : A, B. */
708 build_virtual_init (main_binfo, binfo, decl)
709 tree main_binfo, binfo;
714 tree vtype, vtype_binfo;
716 if (TREE_CODE (binfo) == TREE_VEC)
717 type = BINFO_TYPE (binfo);
718 else if (TREE_CODE (binfo) == RECORD_TYPE)
721 binfo = TYPE_BINFO (type);
724 my_friendly_abort (46);
726 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
727 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
729 /* This code suggests that it's time to rewrite how we handle
730 replicated baseclasses in G++. */
731 if (get_base_distance (vtype, TREE_TYPE (TREE_TYPE (decl)),
732 0, (tree *) 0) == -2)
734 tree binfos = TYPE_BINFO_BASETYPES (TREE_TYPE (TREE_TYPE (decl)));
735 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
736 tree result = NULL_TREE;
738 for (i = n_baselinks-1; i >= 0; i--)
740 tree base_binfo = TREE_VEC_ELT (binfos, i);
743 if (get_base_distance (vtype, BINFO_TYPE (base_binfo), 0, 0) == -1)
746 if (TREE_VIA_VIRTUAL (base_binfo))
747 this_decl = build_vbase_pointer (build_indirect_ref (decl, NULL_PTR), BINFO_TYPE (base_binfo));
748 else if (BINFO_OFFSET_ZEROP (base_binfo))
749 this_decl = build1 (NOP_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
752 this_decl = build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
753 decl, BINFO_OFFSET (base_binfo));
754 result = tree_cons (NULL_TREE, build_virtual_init (main_binfo, base_binfo, this_decl), result);
756 return build_compound_expr (result);
763 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
765 /* The below does not work when we have to step through the
766 vfield, on our way down to the most base class for the
768 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)),
769 BINFO_TYPE (main_binfo)));
772 my_friendly_assert (BINFO_TYPE (main_binfo) == BINFO_TYPE (binfo), 208);
773 vtbl = BINFO_VTABLE (main_binfo);
775 assemble_external (vtbl);
776 TREE_USED (vtbl) = 1;
777 vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl);
779 decl = convert_pointer_to_real (vtype_binfo, decl);
780 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
781 if (vtbl_ptr == error_mark_node)
782 return error_mark_node;
784 /* Have to convert VTBL since array sizes may be different. */
785 return build_modify_expr (vtbl_ptr, NOP_EXPR,
786 convert (TREE_TYPE (vtbl_ptr), vtbl));
789 /* Subroutine of `expand_aggr_vbase_init'.
790 BINFO is the binfo of the type that is being initialized.
791 INIT_LIST is the list of initializers for the virtual baseclass. */
793 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
794 tree binfo, exp, addr, init_list;
796 tree init = value_member (BINFO_TYPE (binfo), init_list);
797 tree ref = build_indirect_ref (addr, NULL_PTR);
799 init = TREE_PURPOSE (init);
800 /* Call constructors, but don't set up vtables. */
801 expand_aggr_init_1 (binfo, exp, ref, init, 0,
802 LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY);
803 CLEAR_BINFO_VBASE_INIT_MARKED (binfo);
806 /* Initialize this object's virtual base class pointers. This must be
807 done only at the top-level of the object being constructed.
809 INIT_LIST is list of initialization for constructor to perform. */
811 expand_aggr_vbase_init (binfo, exp, addr, init_list)
817 tree type = BINFO_TYPE (binfo);
819 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
821 tree result = init_vbase_pointers (type, addr);
825 expand_expr_stmt (build_compound_expr (result));
827 /* Mark everything as having an initializer
828 (either explicit or default). */
829 for (vbases = CLASSTYPE_VBASECLASSES (type);
830 vbases; vbases = TREE_CHAIN (vbases))
831 SET_BINFO_VBASE_INIT_MARKED (vbases);
833 /* First, initialize baseclasses which could be baseclasses
834 for other virtual baseclasses. */
835 for (vbases = CLASSTYPE_VBASECLASSES (type);
836 vbases; vbases = TREE_CHAIN (vbases))
837 /* Don't initialize twice. */
838 if (BINFO_VBASE_INIT_MARKED (vbases))
842 while (BINFO_TYPE (vbases) != BINFO_TYPE (TREE_PURPOSE (tmp)))
843 tmp = TREE_CHAIN (tmp);
844 expand_aggr_vbase_init_1 (vbases, exp,
845 TREE_OPERAND (TREE_VALUE (tmp), 0),
849 /* Now initialize the baseclasses which don't have virtual baseclasses. */
850 for (; result; result = TREE_CHAIN (result))
851 /* Don't initialize twice. */
852 if (BINFO_VBASE_INIT_MARKED (TREE_PURPOSE (result)))
854 my_friendly_abort (47);
855 expand_aggr_vbase_init_1 (TREE_PURPOSE (result), exp,
856 TREE_OPERAND (TREE_VALUE (result), 0),
862 /* Subroutine to perform parser actions for member initialization.
863 S_ID is the scoped identifier.
864 NAME is the name of the member.
865 INIT is the initializer, or `void_type_node' if none. */
867 do_member_init (s_id, name, init)
868 tree s_id, name, init;
872 if (current_class_type == NULL_TREE
873 || ! is_aggr_typedef (s_id, 1))
875 binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id),
876 current_class_type, 1);
877 if (binfo == error_mark_node)
881 error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type);
885 base = convert_pointer_to (binfo, current_class_decl);
886 expand_member_init (build_indirect_ref (base, NULL_PTR), name, init);
889 /* Function to give error message if member initialization specification
890 is erroneous. FIELD is the member we decided to initialize.
891 TYPE is the type for which the initialization is being performed.
892 FIELD must be a member of TYPE, or the base type from which FIELD
893 comes must not need a constructor.
895 MEMBER_NAME is the name of the member. */
898 member_init_ok_or_else (field, type, member_name)
903 if (field == error_mark_node)
905 if (field == NULL_TREE)
907 cp_error ("class `%T' does not have any field named `%s'", type,
911 if (DECL_CONTEXT (field) != type
912 && TYPE_NEEDS_CONSTRUCTING (DECL_CONTEXT (field)))
914 cp_error ("member `%D' comes from base class needing constructor",
921 /* If NAME is a viable field name for the aggregate DECL,
922 and PARMS is a viable parameter list, then expand an _EXPR
923 which describes this initialization.
925 Note that we do not need to chase through the class's base classes
926 to look for NAME, because if it's in that list, it will be handled
927 by the constructor for that base class.
929 We do not yet have a fixed-point finder to instantiate types
930 being fed to overloaded constructors. If there is a unique
931 constructor, then argument types can be got from that one.
933 If INIT is non-NULL, then it the initialization should
934 be placed in `current_base_init_list', where it will be processed
935 by `emit_base_init'. */
937 expand_member_init (exp, name, init)
938 tree exp, name, init;
940 extern tree ptr_type_node; /* should be in tree.h */
942 tree basetype = NULL_TREE, field;
947 if (exp == NULL_TREE)
948 return; /* complain about this later */
950 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
952 if (name == NULL_TREE && IS_AGGR_TYPE (type))
953 switch (CLASSTYPE_N_BASECLASSES (type))
956 error ("base class initializer specified, but no base class to initialize");
959 basetype = TYPE_BINFO_BASETYPE (type, 0);
962 error ("initializer for unnamed base class ambiguous");
963 cp_error ("(type `%T' uses multiple inheritance)", type);
969 /* The grammar should not allow fields which have names
970 that are TYPENAMEs. Therefore, if the field has
971 a non-NULL TREE_TYPE, we may assume that this is an
972 attempt to initialize a base class member of the current
973 type. Otherwise, it is an attempt to initialize a
976 if (init == void_type_node)
979 if (name == NULL_TREE || IDENTIFIER_HAS_TYPE_VALUE (name))
983 if (name == NULL_TREE)
987 name = TYPE_IDENTIFIER (basetype);
990 error ("no base class to initialize");
997 basetype = IDENTIFIER_TYPE_VALUE (name);
999 && ! binfo_member (basetype, TYPE_BINFO (type))
1000 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
1002 if (IDENTIFIER_CLASS_VALUE (name))
1004 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1005 error ("type `%s' is not an immediate or virtual basetype for `%s'",
1006 IDENTIFIER_POINTER (name),
1007 TYPE_NAME_STRING (type));
1009 error ("type `%s' is not an immediate basetype for `%s'",
1010 IDENTIFIER_POINTER (name),
1011 TYPE_NAME_STRING (type));
1016 if (purpose_member (name, current_base_init_list))
1018 error ("base class `%s' already initialized",
1019 IDENTIFIER_POINTER (name));
1023 base_init = build_tree_list (name, init);
1024 TREE_TYPE (base_init) = basetype;
1025 current_base_init_list = chainon (current_base_init_list, base_init);
1032 field = lookup_field (type, name, 1, 0);
1034 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
1037 if (purpose_member (name, current_member_init_list))
1039 error ("field `%s' already initialized", IDENTIFIER_POINTER (name));
1043 member_init = build_tree_list (name, init);
1044 TREE_TYPE (member_init) = TREE_TYPE (field);
1045 current_member_init_list = chainon (current_member_init_list, member_init);
1049 else if (name == NULL_TREE)
1051 compiler_error ("expand_member_init: name == NULL_TREE");
1056 field = lookup_field (basetype, name, 0, 0);
1058 if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
1061 /* now see if there is a constructor for this type
1062 which will take these args. */
1064 if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
1066 tree parmtypes, fndecl;
1068 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1070 /* just know that we've seen something for this node */
1071 DECL_INITIAL (exp) = error_mark_node;
1072 TREE_USED (exp) = 1;
1074 type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
1075 actual_name = TYPE_IDENTIFIER (type);
1076 parm = build_component_ref (exp, name, 0, 0);
1078 /* Now get to the constructor. */
1079 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1080 /* Get past destructor, if any. */
1081 if (TYPE_HAS_DESTRUCTOR (type))
1082 fndecl = DECL_CHAIN (fndecl);
1085 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209);
1087 /* If the field is unique, we can use the parameter
1088 types to guide possible type instantiation. */
1089 if (DECL_CHAIN (fndecl) == NULL_TREE)
1091 /* There was a confusion here between
1092 FIELD and FNDECL. The following code
1093 should be correct, but abort is here
1095 my_friendly_abort (48);
1096 parmtypes = FUNCTION_ARG_CHAIN (fndecl);
1100 parmtypes = NULL_TREE;
1104 init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
1105 if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
1106 rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
1110 if (rval != error_mark_node)
1112 /* Now, fill in the first parm with our guy */
1113 TREE_VALUE (TREE_OPERAND (rval, 1))
1114 = build_unary_op (ADDR_EXPR, parm, 0);
1115 TREE_TYPE (rval) = ptr_type_node;
1116 TREE_SIDE_EFFECTS (rval) = 1;
1119 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1121 parm = build_component_ref (exp, name, 0, 0);
1122 expand_aggr_init (parm, NULL_TREE, 0);
1123 rval = error_mark_node;
1126 /* Now initialize the member. It does not have to
1127 be of aggregate type to receive initialization. */
1128 if (rval != error_mark_node)
1129 expand_expr_stmt (rval);
1132 /* This is like `expand_member_init', only it stores one aggregate
1135 INIT comes in two flavors: it is either a value which
1136 is to be stored in EXP, or it is a parameter list
1137 to go to a constructor, which will operate on EXP.
1138 If `init' is a CONSTRUCTOR, then we emit a warning message,
1139 explaining that such initializations are illegal.
1141 ALIAS_THIS is nonzero iff we are initializing something which is
1142 essentially an alias for C_C_D. In this case, the base constructor
1143 may move it on us, and we must keep track of such deviations.
1145 If INIT resolves to a CALL_EXPR which happens to return
1146 something of the type we are looking for, then we know
1147 that we can safely use that call to perform the
1150 The virtual function table pointer cannot be set up here, because
1151 we do not really know its type.
1153 Virtual baseclass pointers are also set up here.
1155 This never calls operator=().
1157 When initializing, nothing is CONST.
1159 A default copy constructor may have to be used to perform the
1162 A constructor or a conversion operator may have to be used to
1163 perform the initialization, but not both, as it would be ambiguous.
1167 expand_aggr_init (exp, init, alias_this)
1171 tree type = TREE_TYPE (exp);
1172 int was_const = TREE_READONLY (exp);
1174 if (init == error_mark_node)
1177 TREE_READONLY (exp) = 0;
1179 if (TREE_CODE (type) == ARRAY_TYPE)
1181 /* Must arrange to initialize each element of EXP
1182 from elements of INIT. */
1183 int was_const_elts = TYPE_READONLY (TREE_TYPE (type));
1184 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1187 tree atype = build_cplus_array_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
1188 TYPE_DOMAIN (type));
1189 if (init && (TREE_TYPE (exp) == TREE_TYPE (init)))
1190 TREE_TYPE (init) = atype;
1191 TREE_TYPE (exp) = atype;
1193 if (init && TREE_TYPE (init) == NULL_TREE)
1195 /* Handle bad initializers like:
1199 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1203 int main(int argc, char **argv) {
1204 COMPLEX zees(1.0, 0.0)[10];
1207 error ("bad array initializer");
1210 expand_vec_init (exp, exp, array_type_nelts (type), init,
1211 init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
1212 TREE_READONLY (exp) = was_const;
1213 TREE_TYPE (exp) = type;
1214 if (init) TREE_TYPE (init) = itype;
1218 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1219 /* just know that we've seen something for this node */
1220 TREE_USED (exp) = 1;
1223 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1224 constructor as parameters to an implicit GNU C++ constructor. */
1225 if (init && TREE_CODE (init) == CONSTRUCTOR
1226 && TYPE_HAS_CONSTRUCTOR (type)
1227 && TREE_TYPE (init) == type)
1228 init = CONSTRUCTOR_ELTS (init);
1230 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1231 init, alias_this, LOOKUP_NORMAL);
1232 TREE_READONLY (exp) = was_const;
1236 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags)
1244 /* It fails because there may not be a constructor which takes
1245 its own type as the first (or only parameter), but which does
1246 take other types via a conversion. So, if the thing initializing
1247 the expression is a unit element of type X, first try X(X&),
1248 followed by initialization by X. If neither of these work
1249 out, then look hard. */
1252 int xxref_init_possible;
1254 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
1257 if (parms) init = TREE_VALUE (parms);
1259 else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init))
1261 rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
1262 expand_expr_stmt (rval);
1266 parms = build_tree_list (NULL_TREE, init);
1268 if (TYPE_HAS_INIT_REF (type)
1269 || init == NULL_TREE
1270 || TREE_CHAIN (parms) != NULL_TREE)
1271 xxref_init_possible = 0;
1274 xxref_init_possible = LOOKUP_SPECULATIVELY;
1275 flags &= ~LOOKUP_COMPLAIN;
1278 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1280 if (true_exp == exp)
1281 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1283 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1284 flags |= LOOKUP_HAS_IN_CHARGE;
1287 rval = build_method_call (exp, constructor_name_full (type),
1288 parms, binfo, flags|xxref_init_possible);
1289 if (rval == NULL_TREE && xxref_init_possible)
1291 /* It is an error to implement a default copy constructor if
1292 (see ARM 12.8 for details) ... one case being if another
1293 copy constructor already exists. */
1294 tree init_type = TREE_TYPE (init);
1295 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1296 init_type = TREE_TYPE (init_type);
1297 if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type)
1298 || (IS_AGGR_TYPE (init_type)
1299 && UNIQUELY_DERIVED_FROM_P (type, init_type)))
1301 if (type == BINFO_TYPE (binfo)
1302 && TYPE_USES_VIRTUAL_BASECLASSES (type))
1304 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1305 expand_aggr_vbase_init (binfo, exp, addr, NULL_TREE);
1307 expand_expr_stmt (build_vbase_vtables_init (binfo, binfo,
1310 expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
1314 rval = build_method_call (exp, constructor_name_full (type), parms,
1318 /* Private, protected, or otherwise unavailable. */
1319 if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN))
1320 cp_error ("in base initialization for class `%T'", binfo);
1321 /* A valid initialization using constructor. */
1322 else if (rval != error_mark_node && rval != NULL_TREE)
1324 /* p. 222: if the base class assigns to `this', then that
1325 value is used in the derived class. */
1326 if ((flag_this_is_variable & 1) && alias_this)
1328 TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1329 expand_assignment (current_class_decl, rval, 0, 0);
1332 expand_expr_stmt (rval);
1334 else if (parms && TREE_CHAIN (parms) == NULL_TREE)
1336 /* If we are initializing one aggregate value
1337 from another, and though there are constructors,
1338 and none accept the initializer, just do a bitwise
1341 The above sounds wrong, ``If a class has any copy
1342 constructor defined, the default copy constructor will
1343 not be generated.'' 12.8 Copying Class Objects (mrs)
1345 @@ This should reject initializer which a constructor
1346 @@ rejected on access gounds, but there is
1347 @@ no way right now to recognize that case with
1348 @@ just `error_mark_node'. */
1350 init = TREE_VALUE (parms);
1351 itype = TREE_TYPE (init);
1352 if (TREE_CODE (itype) == REFERENCE_TYPE)
1354 init = convert_from_reference (init);
1355 itype = TREE_TYPE (init);
1357 itype = TYPE_MAIN_VARIANT (itype);
1359 /* This is currently how the default X(X&) constructor
1361 if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0))
1364 warning ("bitwise copy in initialization of type `%s'",
1365 TYPE_NAME_STRING (type));
1367 rval = build (INIT_EXPR, type, exp, init);
1368 expand_expr_stmt (rval);
1372 cp_error ("in base initialization for class `%T',", binfo);
1373 cp_error ("invalid initializer to constructor for type `%T'", type);
1379 if (init == NULL_TREE)
1380 my_friendly_assert (parms == NULL_TREE, 210);
1381 if (parms == NULL_TREE && TREE_VIA_VIRTUAL (binfo))
1382 cp_error ("virtual baseclass `%T' does not have default initializer", binfo);
1385 cp_error ("in base initialization for class `%T',", binfo);
1386 /* This will make an error message for us. */
1387 build_method_call (exp, constructor_name_full (type), parms, binfo,
1388 (TYPE_USES_VIRTUAL_BASECLASSES (type)
1389 ? LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE
1394 /* Constructor has been called, but vtables may be for TYPE
1395 rather than for FOR_TYPE. */
1398 /* This function is responsible for initializing EXP with INIT
1401 BINFO is the binfo of the type for who we are performing the
1402 initialization. For example, if W is a virtual base class of A and B,
1404 If we are initializing B, then W must contain B's W vtable, whereas
1405 were we initializing C, W must contain C's W vtable.
1407 TRUE_EXP is nonzero if it is the true expression being initialized.
1408 In this case, it may be EXP, or may just contain EXP. The reason we
1409 need this is because if EXP is a base element of TRUE_EXP, we
1410 don't necessarily know by looking at EXP where its virtual
1411 baseclass fields should really be pointing. But we do know
1412 from TRUE_EXP. In constructors, we don't know anything about
1413 the value being initialized.
1415 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1417 FLAGS is just passes to `build_method_call'. See that function for
1421 expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1428 tree type = TREE_TYPE (exp);
1429 tree init_type = NULL_TREE;
1432 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1434 /* Use a function returning the desired type to initialize EXP for us.
1435 If the function is a constructor, and its first argument is
1436 NULL_TREE, know that it was meant for us--just slide exp on
1437 in and expand the constructor. Constructors now come
1441 tree init_list = NULL_TREE;
1443 if (TREE_CODE (init) == TREE_LIST)
1446 if (TREE_CHAIN (init) == NULL_TREE)
1447 init = TREE_VALUE (init);
1450 init_type = TREE_TYPE (init);
1452 if (TREE_CODE (init) != TREE_LIST)
1454 if (TREE_CODE (init_type) == ERROR_MARK)
1458 /* These lines are found troublesome 5/11/89. */
1459 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1460 init_type = TREE_TYPE (init_type);
1463 /* This happens when we use C++'s functional cast notation.
1464 If the types match, then just use the TARGET_EXPR
1465 directly. Otherwise, we need to create the initializer
1466 separately from the object being initialized. */
1467 if (TREE_CODE (init) == TARGET_EXPR)
1469 if (init_type == type)
1471 if (TREE_CODE (exp) == VAR_DECL
1472 || TREE_CODE (exp) == RESULT_DECL)
1473 /* Unify the initialization targets. */
1474 DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1476 DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
1478 expand_expr_stmt (init);
1483 init = TREE_OPERAND (init, 1);
1484 init = build (CALL_EXPR, init_type,
1485 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1486 TREE_SIDE_EFFECTS (init) = 1;
1488 TREE_RAISES (init) = ??
1491 TREE_VALUE (init_list) = init;
1495 if (init_type == type && TREE_CODE (init) == CALL_EXPR
1497 /* It is legal to directly initialize from a CALL_EXPR
1498 without going through X(X&), apparently. */
1499 && ! TYPE_GETS_INIT_REF (type)
1503 /* A CALL_EXPR is a legitimate form of initialization, so
1504 we should not print this warning message. */
1506 /* Should have gone away due to 5/11/89 change. */
1507 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
1508 init = convert_from_reference (init);
1510 expand_assignment (exp, init, 0, 0);
1511 if (exp == DECL_RESULT (current_function_decl))
1513 /* Failing this assertion means that the return value
1514 from receives multiple initializations. */
1515 my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
1516 || DECL_INITIAL (exp) == error_mark_node,
1518 DECL_INITIAL (exp) = init;
1522 else if (init_type == type
1523 && TREE_CODE (init) == COND_EXPR)
1525 /* Push value to be initialized into the cond, where possible.
1526 Avoid spurious warning messages when initializing the
1527 result of this function. */
1528 TREE_OPERAND (init, 1)
1529 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1530 if (exp == DECL_RESULT (current_function_decl))
1531 DECL_INITIAL (exp) = NULL_TREE;
1532 TREE_OPERAND (init, 2)
1533 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1534 if (exp == DECL_RESULT (current_function_decl))
1535 DECL_INITIAL (exp) = init;
1536 TREE_SIDE_EFFECTS (init) = 1;
1537 expand_expr (init, const0_rtx, VOIDmode, 0);
1543 /* We did not know what we were initializing before. Now we do. */
1544 if (TREE_CODE (init) == TARGET_EXPR)
1546 tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1548 if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1549 && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1551 /* In order for this to work for RESULT_DECLs, if their
1552 type has a constructor, then they must be BLKmode
1553 so that they will be meaningfully addressable. */
1554 tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1555 init = TREE_OPERAND (init, 1);
1556 init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1557 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1558 TREE_SIDE_EFFECTS (init) = 1;
1560 TREE_RAISES (init) = ??
1562 TREE_VALUE (TREE_OPERAND (init, 1))
1563 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1567 expand_assignment (current_function_decl, init, 0, 0);
1570 if (exp == DECL_RESULT (current_function_decl))
1572 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1573 fatal ("return value from function receives multiple initializations");
1574 DECL_INITIAL (exp) = init;
1576 expand_expr_stmt (init);
1581 if (TREE_CODE (exp) == VAR_DECL
1582 && TREE_CODE (init) == CONSTRUCTOR
1583 && TREE_HAS_CONSTRUCTOR (init))
1585 tree t = store_init_value (exp, init);
1588 expand_decl_init (exp);
1591 t = build (INIT_EXPR, type, exp, init);
1592 TREE_SIDE_EFFECTS (t) = 1;
1593 expand_expr_stmt (t);
1597 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1598 which really means: xyzzy foo = bar; Ugh!
1600 More useful for this case: xyzzy *foo = new xyzzy (bar); */
1602 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1604 if (init_list && TREE_CHAIN (init_list))
1606 warning ("initializer list being treated as compound expression");
1607 init = convert (type, build_compound_expr (init_list));
1608 if (init == error_mark_node)
1612 expand_assignment (exp, init, 0, 0);
1616 /* See whether we can go through a type conversion operator.
1617 This wins over going through a non-existent constructor. If
1618 there is a constructor, it is ambiguous. */
1619 if (TREE_CODE (init) != TREE_LIST)
1621 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1622 ? TREE_TYPE (init_type) : init_type;
1624 if (ttype != type && IS_AGGR_TYPE (ttype))
1626 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1630 /* See if there is a constructor for``type'' that takes a
1631 ``ttype''-typed object. */
1632 tree parms = build_tree_list (NULL_TREE, init);
1633 tree as_cons = NULL_TREE;
1634 if (TYPE_HAS_CONSTRUCTOR (type))
1635 as_cons = build_method_call (exp, constructor_name_full (type),
1637 LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1638 if (as_cons != NULL_TREE && as_cons != error_mark_node)
1639 /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1640 cp_error ("ambiguity between conversion to `%T' and constructor",
1643 expand_assignment (exp, rval, 0, 0);
1650 /* Handle default copy constructors here, does not matter if there is
1651 a constructor or not. */
1652 if (type == init_type && IS_AGGR_TYPE (type)
1653 && init && TREE_CODE (init) != TREE_LIST)
1654 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1655 /* Not sure why this is here... */
1656 else if (TYPE_HAS_CONSTRUCTOR (type))
1657 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1658 else if (TREE_CODE (type) == ARRAY_TYPE)
1660 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1661 expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1662 else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1663 sorry ("arrays of objects with virtual functions but no constructors");
1666 expand_recursive_init (binfo, true_exp, exp, init,
1667 CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1670 /* A pointer which holds the initializer. First call to
1671 expand_aggr_init gets this value pointed to, and sets it to init_null. */
1672 static tree *init_ptr, init_null;
1674 /* Subroutine of expand_recursive_init:
1676 ADDR is the address of the expression being initialized.
1677 INIT_LIST is the cons-list of initializations to be performed.
1678 ALIAS_THIS is its same, lovable self. */
1680 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
1681 tree binfo, true_exp, addr;
1687 if (TREE_PURPOSE (init_list))
1689 if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1691 tree member = TREE_PURPOSE (init_list);
1692 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1693 tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1694 if (IS_AGGR_TYPE (TREE_TYPE (member)))
1695 expand_aggr_init (member_base, DECL_INITIAL (member), 0);
1696 else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1697 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1699 member_base = save_expr (default_conversion (member_base));
1700 expand_vec_init (member, member_base,
1701 array_type_nelts (TREE_TYPE (member)),
1702 DECL_INITIAL (member), 0);
1705 expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1707 else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1709 expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1710 expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
1712 else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1714 /* Only initialize the virtual function tables if we
1715 are initializing the ultimate users of those vtables. */
1716 if (TREE_VALUE (init_list))
1718 /* We have to ensure that the second argment to
1719 build_virtual_init is in binfo's hierarchy. */
1720 expand_expr_stmt (build_virtual_init (binfo,
1721 get_binfo (TREE_VALUE (init_list), binfo, 0),
1723 if (TREE_VALUE (init_list) == binfo
1724 && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1725 expand_expr_stmt (build_vbase_vtables_init (binfo, binfo, true_exp, addr, 1));
1729 my_friendly_abort (49);
1731 else if (TREE_VALUE (init_list)
1732 && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
1734 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1735 expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
1736 alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
1739 /* INIT_PTR is used up. */
1740 init_ptr = &init_null;
1743 my_friendly_abort (50);
1744 init_list = TREE_CHAIN (init_list);
1748 /* Initialize EXP with INIT. Type EXP does not have a constructor,
1749 but it has a baseclass with a constructor or a virtual function
1750 table which needs initializing.
1752 INIT_LIST is a cons-list describing what parts of EXP actually
1753 need to be initialized. INIT is given to the *unique*, first
1754 constructor within INIT_LIST. If there are multiple first
1755 constructors, such as with multiple inheritance, INIT must
1756 be zero or an ambiguity error is reported.
1758 ALIAS_THIS is passed from `expand_aggr_init'. See comments
1762 expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
1763 tree binfo, true_exp, exp, init;
1767 tree *old_init_ptr = init_ptr;
1768 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1771 if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1773 expand_aggr_vbase_init (binfo, exp, addr, init_list);
1774 expand_expr_stmt (build_vbase_vtables_init (binfo, binfo, true_exp, addr, 1));
1776 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
1780 tree type = TREE_TYPE (exp);
1782 if (TREE_CODE (type) == REFERENCE_TYPE)
1783 type = TREE_TYPE (type);
1784 if (IS_AGGR_TYPE (type))
1785 cp_error ("unexpected argument to constructor `%T'", type);
1787 error ("unexpected argument to constructor");
1789 init_ptr = old_init_ptr;
1792 /* Report an error if NAME is not the name of a user-defined,
1793 aggregate type. If OR_ELSE is nonzero, give an error message. */
1795 is_aggr_typedef (name, or_else)
1801 if (name == error_mark_node)
1804 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1805 type = IDENTIFIER_TYPE_VALUE (name);
1806 else if (IDENTIFIER_HAS_CLASS_TYPE_VALUE (name))
1807 type = IDENTIFIER_CLASS_TYPE_VALUE (name);
1811 cp_error ("`%T' fails to be an aggregate typedef", name);
1815 if (! IS_AGGR_TYPE (type)
1816 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1819 cp_error ("type `%T' is of non-aggregate type", type);
1825 /* Like is_aggr_typedef, but returns typedef if successful. */
1827 get_aggr_from_typedef (name, or_else)
1833 if (name == error_mark_node)
1836 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1837 type = IDENTIFIER_TYPE_VALUE (name);
1838 else if (IDENTIFIER_HAS_CLASS_TYPE_VALUE (name))
1839 type = IDENTIFIER_CLASS_TYPE_VALUE (name);
1843 cp_error ("`%T' fails to be an aggregate typedef", name);
1847 if (! IS_AGGR_TYPE (type)
1848 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1851 cp_error ("type `%T' is of non-aggregate type", type);
1858 get_type_value (name)
1863 if (name == error_mark_node)
1866 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1867 return IDENTIFIER_TYPE_VALUE (name);
1868 else if (IDENTIFIER_CLASS_VALUE (name))
1869 return IDENTIFIER_CLASS_TYPE_VALUE (name);
1875 /* This code could just as well go in `class.c', but is placed here for
1878 /* For an expression of the form CNAME :: NAME (PARMLIST), build
1879 the appropriate function call. */
1881 build_member_call (cname, name, parmlist)
1882 tree cname, name, parmlist;
1885 tree method_name = name;
1887 int dont_use_this = 0;
1888 tree basetype_path, decl;
1890 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1892 method_name = TREE_OPERAND (method_name, 0);
1896 if (TREE_CODE (cname) == SCOPE_REF)
1897 cname = resolve_scope_to_name (NULL_TREE, cname);
1899 if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1)))
1900 return error_mark_node;
1902 /* An operator we did not like. */
1903 if (name == NULL_TREE)
1904 return error_mark_node;
1909 /* Everything can explicitly call a destructor; see 12.4 */
1910 if (! TYPE_HAS_DESTRUCTOR (type))
1911 cp_error ("type `%#T' does not have a destructor", type);
1914 cp_error ("cannot call destructor `%T::~%T' without object", type,
1916 return error_mark_node;
1919 /* No object? Then just fake one up, and let build_method_call
1920 figure out what to do. */
1921 if (current_class_type == 0
1922 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1927 basetype_path = NULL_TREE;
1928 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1930 else if (current_class_decl == 0)
1933 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1937 tree olddecl = current_class_decl;
1938 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1939 if (oldtype != type)
1941 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1942 TYPE_VOLATILE (oldtype));
1943 decl = convert_force (TYPE_POINTER_TO (newtype), olddecl);
1949 decl = build_indirect_ref (decl, NULL_PTR);
1951 if (t = lookup_fnfields (TYPE_BINFO (type), method_name, 0))
1952 return build_method_call (decl, method_name, parmlist, basetype_path,
1953 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1954 if (TREE_CODE (name) == IDENTIFIER_NODE
1955 && (t = lookup_field (TYPE_BINFO (type), name, 1, 0)))
1957 if (t == error_mark_node)
1958 return error_mark_node;
1959 if (TREE_CODE (t) == FIELD_DECL)
1963 cp_error ("invalid use of non-static field `%D'", t);
1964 return error_mark_node;
1966 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1968 else if (TREE_CODE (t) == VAR_DECL)
1972 cp_error ("invalid use of member `%D'", t);
1973 return error_mark_node;
1975 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1976 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1977 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1978 return build_function_call (decl, parmlist);
1982 cp_error ("no method `%T::%D'", type, name);
1983 return error_mark_node;
1987 /* Build a reference to a member of an aggregate. This is not a
1988 C++ `&', but really something which can have its address taken,
1989 and then act as a pointer to member, for example CNAME :: FIELD
1990 can have its address taken by saying & CNAME :: FIELD.
1992 @@ Prints out lousy diagnostics for operator <typename>
1995 @@ This function should be rewritten and placed in search.c. */
1997 build_offset_ref (cname, name)
2000 tree decl, type, fnfields, fields, t = error_mark_node;
2001 tree basetypes = NULL_TREE;
2004 if (TREE_CODE (cname) == SCOPE_REF)
2005 cname = resolve_scope_to_name (NULL_TREE, cname);
2007 if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
2008 return error_mark_node;
2010 type = IDENTIFIER_TYPE_VALUE (cname);
2012 if (TREE_CODE (name) == BIT_NOT_EXPR)
2015 name = TREE_OPERAND (name, 0);
2018 if (TYPE_SIZE (type) == 0)
2020 t = IDENTIFIER_CLASS_VALUE (name);
2023 cp_error ("incomplete type `%T' does not have member `%D'", type,
2025 return error_mark_node;
2027 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
2028 || TREE_CODE (t) == CONST_DECL)
2033 if (TREE_CODE (t) == FIELD_DECL)
2034 sorry ("use of member in incomplete aggregate type");
2035 else if (TREE_CODE (t) == FUNCTION_DECL)
2036 sorry ("use of member function in incomplete aggregate type");
2038 my_friendly_abort (52);
2039 return error_mark_node;
2042 if (TREE_CODE (name) == TYPE_EXPR)
2043 /* Pass a TYPE_DECL to build_component_type_expr. */
2044 return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
2045 name, NULL_TREE, 1);
2047 fnfields = lookup_fnfields (TYPE_BINFO (type), name, 1);
2048 fields = lookup_field (type, name, 0, 0);
2050 if (fields == error_mark_node || fnfields == error_mark_node)
2051 return error_mark_node;
2053 if (current_class_type == 0
2054 || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
2056 basetypes = TYPE_BINFO (type);
2057 decl = build1 (NOP_EXPR,
2058 IDENTIFIER_TYPE_VALUE (cname),
2061 else if (current_class_decl == 0)
2062 decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
2067 /* A lot of this logic is now handled in lookup_field and
2071 basetypes = TREE_PURPOSE (fnfields);
2073 /* Go from the TREE_BASELINK to the member function info. */
2074 t = TREE_VALUE (fnfields);
2078 if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
2080 error ("ambiguous member reference: member `%s' defined as both field and function",
2081 IDENTIFIER_POINTER (name));
2082 return error_mark_node;
2084 if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
2086 else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
2090 error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
2091 return error_mark_node;
2095 if (t == TREE_VALUE (fnfields))
2097 extern int flag_save_memoized_contexts;
2099 /* This does not handle access checking yet. */
2100 if (DECL_CHAIN (t) == NULL_TREE || dtor)
2102 enum access_type access;
2104 /* unique functions are handled easily. */
2106 access = compute_access (basetypes, t);
2107 if (access == access_protected)
2109 cp_error_at ("member function `%#D' is protected", t);
2110 error ("in this context");
2111 return error_mark_node;
2113 if (access == access_private)
2115 cp_error_at ("member function `%#D' is private", t);
2116 error ("in this context");
2117 return error_mark_node;
2119 assemble_external (t);
2120 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
2123 /* overloaded functions may need more work. */
2126 if (TYPE_HAS_DESTRUCTOR (type)
2127 && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
2133 /* FNFIELDS is most likely allocated on the search_obstack,
2134 which will go away after this class scope. If we need
2135 to save this value for later (either for memoization
2136 or for use as an initializer for a static variable), then
2139 ??? The smart thing to do for the case of saving initializers
2140 is to resolve them before we're done with this scope. */
2141 if (!TREE_PERMANENT (fnfields)
2142 && ((flag_save_memoized_contexts && global_bindings_p ())
2143 || ! allocation_temporary_p ()))
2144 fnfields = copy_list (fnfields);
2145 t = build_tree_list (error_mark_node, fnfields);
2146 TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
2151 /* Now that we know we are looking for a field, see if we
2152 have access to that field. Lookup_field will give us the
2155 t = lookup_field (basetypes, name, 1, 0);
2157 if (t == error_mark_node)
2158 return error_mark_node;
2162 cp_error ("`%D' is not a member of type `%T'", name,
2163 IDENTIFIER_TYPE_VALUE (cname));
2164 return error_mark_node;
2167 if (TREE_CODE (t) == TYPE_DECL)
2172 /* static class members and class-specific enum
2173 values can be returned without further ado. */
2174 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
2176 assemble_external (t);
2181 /* static class functions too. */
2182 if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2183 my_friendly_abort (53);
2185 /* In member functions, the form `cname::name' is no longer
2186 equivalent to `this->cname::name'. */
2187 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2190 /* Given an object EXP and a member function reference MEMBER,
2191 return the address of the actual member function. */
2193 get_member_function (exp_addr_ptr, exp, member)
2197 tree ctype = TREE_TYPE (exp);
2198 tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
2200 if (TYPE_VIRTUAL_P (ctype)
2201 || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
2206 /* Save away the unadulterated `this' pointer. */
2207 exp_addr = save_expr (*exp_addr_ptr);
2209 /* Cast function to signed integer. */
2210 e0 = build1 (NOP_EXPR, integer_type_node, function);
2212 #ifdef VTABLE_USES_MASK
2213 /* If we are willing to limit the number of
2214 virtual functions a class may have to some
2215 *small* number, then if, for a function address,
2216 we are passed some small number, we know that
2217 it is a virtual function index, and work from there. */
2218 e1 = build (BIT_AND_EXPR, integer_type_node, e0, vtbl_mask);
2220 /* There is a hack here that takes advantage of
2221 twos complement arithmetic, and the fact that
2222 there are more than one UNITS to the WORD.
2223 If the high bit is set for the `function',
2224 then we pretend it is a virtual function,
2225 and the array indexing will knock this bit
2226 out the top, leaving a valid index. */
2227 if (UNITS_PER_WORD <= 1)
2228 my_friendly_abort (54);
2230 e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
2231 e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
2232 build_tree_list (NULL_TREE, e1)));
2233 e1 = save_expr (e1);
2236 if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
2238 exp = build_indirect_ref (exp_addr, NULL_PTR);
2239 *exp_addr_ptr = exp_addr;
2242 /* This is really hairy: if the function pointer is a pointer
2243 to a non-virtual member function, then we can't go mucking
2244 with the `this' pointer (any more than we already have to
2245 this point). If it is a pointer to a virtual member function,
2246 then we have to adjust the `this' pointer according to
2247 what the virtual function table tells us. */
2249 e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
2250 my_friendly_assert (e3 != error_mark_node, 213);
2252 /* Change this pointer type from `void *' to the
2253 type it is really supposed to be. */
2254 TREE_TYPE (e3) = TREE_TYPE (function);
2256 /* If non-virtual, use what we had originally. Otherwise,
2257 use the value we get from the virtual function table. */
2258 *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
2260 function = build_conditional_expr (e1, function, e3);
2262 return build_indirect_ref (function, NULL_PTR);
2265 /* If a OFFSET_REF made it through to here, then it did
2266 not have its address taken. */
2269 resolve_offset_ref (exp)
2272 tree type = TREE_TYPE (exp);
2273 tree base = NULL_TREE;
2275 tree basetype, addr;
2277 if (TREE_CODE (exp) == TREE_LIST)
2278 return build_unary_op (ADDR_EXPR, exp, 0);
2280 if (TREE_CODE (exp) != OFFSET_REF)
2282 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2283 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2285 error ("object missing in use of pointer-to-member construct");
2286 return error_mark_node;
2289 type = TREE_TYPE (type);
2294 member = TREE_OPERAND (exp, 1);
2295 base = TREE_OPERAND (exp, 0);
2298 if ((TREE_CODE (member) == VAR_DECL
2299 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2300 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
2302 /* These were static members. */
2303 if (mark_addressable (member) == 0)
2304 return error_mark_node;
2308 /* Syntax error can cause a member which should
2309 have been seen as static to be grok'd as non-static. */
2310 if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
2312 if (TREE_ADDRESSABLE (member) == 0)
2314 cp_error_at ("member `%D' is non-static in static member function context", member);
2315 error ("at this point in file");
2316 TREE_ADDRESSABLE (member) = 1;
2318 return error_mark_node;
2321 /* The first case is really just a reference to a member of `this'. */
2322 if (TREE_CODE (member) == FIELD_DECL
2324 || (TREE_CODE (base) == NOP_EXPR
2325 && TREE_OPERAND (base, 0) == error_mark_node)))
2328 enum access_type access;
2330 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
2332 basetype = TYPE_OFFSET_BASETYPE (type);
2333 base = convert_pointer_to (basetype, current_class_decl);
2336 base = current_class_decl;
2337 basetype = DECL_CONTEXT (member);
2339 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2341 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2342 return error_mark_node;
2344 addr = convert_pointer_to (basetype, base);
2345 access = compute_access (basetype_path, member);
2346 if (access == access_public)
2347 return build (COMPONENT_REF, TREE_TYPE (member),
2348 build_indirect_ref (addr, NULL_PTR), member);
2349 if (access == access_protected)
2351 cp_error_at ("member `%D' is protected", member);
2352 error ("in this context");
2353 return error_mark_node;
2355 if (access == access_private)
2357 cp_error_at ("member `%D' is private", member);
2358 error ("in this context");
2359 return error_mark_node;
2361 my_friendly_abort (55);
2364 /* If this is a reference to a member function, then return
2365 the address of the member function (which may involve going
2366 through the object's vtable), otherwise, return an expression
2367 for the dereferenced pointer-to-member construct. */
2368 addr = build_unary_op (ADDR_EXPR, base, 0);
2370 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2372 basetype = DECL_CLASS_CONTEXT (member);
2373 addr = convert_pointer_to (basetype, addr);
2374 return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0);
2376 else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2378 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2379 addr = convert_pointer_to (basetype, addr);
2380 member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0));
2381 return build1 (INDIRECT_REF, type,
2382 build (PLUS_EXPR, ptr_type_node, addr, member));
2384 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2386 return get_member_function_from_ptrfunc (&addr, base, member);
2388 my_friendly_abort (56);
2393 /* Return either DECL or its known constant value (if it has one). */
2396 decl_constant_value (decl)
2399 if (! TREE_THIS_VOLATILE (decl)
2401 /* These may be necessary for C, but they break C++. */
2402 ! TREE_PUBLIC (decl)
2403 /* Don't change a variable array bound or initial value to a constant
2404 in a place where a variable is invalid. */
2407 && DECL_INITIAL (decl) != 0
2408 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2409 /* This is invalid if initial value is not constant.
2410 If it has either a function call, a memory reference,
2411 or a variable, then re-evaluating it could give different results. */
2412 && TREE_CONSTANT (DECL_INITIAL (decl))
2413 /* Check for cases where this is sub-optimal, even though valid. */
2414 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2416 /* We must allow this to work outside of functions so that
2417 static constants can be used for array sizes. */
2418 && current_function_decl != 0
2419 && DECL_MODE (decl) != BLKmode
2422 return DECL_INITIAL (decl);
2426 /* Friend handling routines. */
2427 /* Friend data structures:
2429 Lists of friend functions come from TYPE_DECL nodes. Since all
2430 aggregate types are automatically typedef'd, these nodes are guaranteed
2433 The TREE_PURPOSE of a friend list is the name of the friend,
2434 and its TREE_VALUE is another list.
2436 For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2437 will be filled in, but not both. The TREE_VALUE of that list is an
2438 individual function which is a friend. The TREE_PURPOSE of that list
2439 indicates a type in which all functions by that name are friends.
2441 Lists of friend classes come from _TYPE nodes. Love that consistency
2445 is_friend_type (type1, type2)
2448 return is_friend (type1, type2);
2452 is_friend (type, supplicant)
2453 tree type, supplicant;
2458 if (supplicant == NULL_TREE || type == NULL_TREE)
2461 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2464 /* It's a function decl. */
2466 tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2467 tree name = DECL_NAME (supplicant);
2468 tree ctype = DECL_CLASS_CONTEXT (supplicant);
2469 for (; list ; list = TREE_CHAIN (list))
2471 if (name == TREE_PURPOSE (list))
2473 tree friends = TREE_VALUE (list);
2474 name = DECL_ASSEMBLER_NAME (supplicant);
2475 for (; friends ; friends = TREE_CHAIN (friends))
2477 if (ctype == TREE_PURPOSE (friends))
2479 if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
2489 if (type == supplicant)
2492 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2493 for (; list ; list = TREE_CHAIN (list))
2494 if (supplicant == TREE_VALUE (list))
2499 tree context = declp ? DECL_CLASS_CONTEXT (supplicant)
2500 : DECL_CONTEXT (TYPE_NAME (supplicant));
2503 return is_friend (type, context);
2509 /* Add a new friend to the friends of the aggregate type TYPE.
2510 DECL is the FUNCTION_DECL of the friend being added. */
2512 add_friend (type, decl)
2515 tree typedecl = TYPE_NAME (type);
2516 tree list = DECL_FRIENDLIST (typedecl);
2517 tree name = DECL_NAME (decl);
2521 if (name == TREE_PURPOSE (list))
2523 tree friends = TREE_VALUE (list);
2524 for (; friends ; friends = TREE_CHAIN (friends))
2526 if (decl == TREE_VALUE (friends))
2528 cp_pedwarn_at ("`%D' is already a friend of class `%T'",
2533 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2537 list = TREE_CHAIN (list);
2539 DECL_FRIENDLIST (typedecl)
2540 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2541 DECL_FRIENDLIST (typedecl));
2542 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2544 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2545 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2546 if (parmtypes && TREE_CHAIN (parmtypes))
2548 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2549 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2550 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2551 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2556 /* Declare that every member function NAME in FRIEND_TYPE
2557 (which may be NULL_TREE) is a friend of type TYPE. */
2559 add_friends (type, name, friend_type)
2560 tree type, name, friend_type;
2562 tree typedecl = TYPE_NAME (type);
2563 tree list = DECL_FRIENDLIST (typedecl);
2567 if (name == TREE_PURPOSE (list))
2569 tree friends = TREE_VALUE (list);
2570 while (friends && TREE_PURPOSE (friends) != friend_type)
2571 friends = TREE_CHAIN (friends);
2574 warning ("method `%s::%s' is already a friend of class",
2575 TYPE_NAME_STRING (friend_type),
2576 IDENTIFIER_POINTER (name));
2578 warning ("function `%s' is already a friend of class `%s'",
2579 IDENTIFIER_POINTER (name),
2580 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2582 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2586 list = TREE_CHAIN (list);
2588 DECL_FRIENDLIST (typedecl) =
2590 build_tree_list (friend_type, NULL_TREE),
2591 DECL_FRIENDLIST (typedecl));
2592 if (! strncmp (IDENTIFIER_POINTER (name),
2593 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2594 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2596 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2597 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2601 /* Set up a cross reference so that type TYPE will make member function
2602 CTYPE::DECL a friend when CTYPE is finally defined. For more than
2603 one, set up a cross reference so that functions with the name DECL
2604 and type CTYPE know that they are friends of TYPE. */
2606 xref_friend (type, decl, ctype)
2607 tree type, decl, ctype;
2609 tree typedecl = TYPE_NAME (type);
2610 tree friend_decl = TYPE_NAME (ctype);
2612 tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2614 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2618 SET_DECL_WAITING_FRIENDS (friend_decl,
2620 DECL_WAITING_FRIENDS (friend_decl)));
2621 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2624 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2625 been defined, we make all of its member functions friends of
2626 TYPE. If not, we make it a pending friend, which can later be added
2627 when its definition is seen. If a type is defined, then its TYPE_DECL's
2628 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2629 classes that are not defined. If a type has not yet been defined,
2630 then the DECL_WAITING_FRIENDS contains a list of types
2631 waiting to make it their friend. Note that these two can both
2632 be in use at the same time! */
2634 make_friend_class (type, friend_type)
2635 tree type, friend_type;
2639 if (IS_SIGNATURE (type))
2641 error ("`friend' declaration in signature definition");
2644 if (IS_SIGNATURE (friend_type))
2646 error ("signature type `%s' declared `friend'",
2647 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2650 if (type == friend_type)
2652 warning ("class `%s' is implicitly friends with itself",
2653 TYPE_NAME_STRING (type));
2657 GNU_xref_hier (TYPE_NAME_STRING (type),
2658 TYPE_NAME_STRING (friend_type), 0, 0, 1);
2660 classes = CLASSTYPE_FRIEND_CLASSES (type);
2661 while (classes && TREE_VALUE (classes) != friend_type)
2662 classes = TREE_CHAIN (classes);
2664 warning ("class `%s' is already friends with class `%s'",
2665 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2668 CLASSTYPE_FRIEND_CLASSES (type)
2669 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2673 /* Main friend processor. This is large, and for modularity purposes,
2674 has been removed from grokdeclarator. It returns `void_type_node'
2675 to indicate that something happened, though a FIELD_DECL is
2678 CTYPE is the class this friend belongs to.
2680 DECLARATOR is the name of the friend.
2682 DECL is the FUNCTION_DECL that the friend is.
2684 In case we are parsing a friend which is part of an inline
2685 definition, we will need to store PARM_DECL chain that comes
2686 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2688 FLAGS is just used for `grokclassfn'.
2690 QUALS say what special qualifies should apply to the object
2691 pointed to by `this'. */
2693 do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2694 tree ctype, declarator, decl, parmdecls;
2695 enum overload_flags flags;
2698 /* first, lets find out if what we are making a friend needs overloading */
2700 int was_c_linkage = 0;
2702 /* Every decl that gets here is a friend of something. */
2703 DECL_FRIEND_P (decl) = 1;
2705 /* If we find something in scope, let see if it has extern "C" linkage. */
2706 /* This code is pretty general and should be ripped out and reused
2707 as a separate function. */
2708 if (DECL_NAME (decl))
2710 previous_decl = lookup_name (DECL_NAME (decl), 0);
2711 if (previous_decl && TREE_CODE (previous_decl) == TREE_LIST)
2715 if (TREE_TYPE (TREE_VALUE (previous_decl)) == TREE_TYPE (decl))
2717 previous_decl = TREE_VALUE (previous_decl);
2720 previous_decl = TREE_CHAIN (previous_decl);
2722 while (previous_decl);
2725 /* It had extern "C" linkage, so don't overload this. */
2726 if (previous_decl && TREE_CODE (previous_decl) == FUNCTION_DECL
2727 && TREE_TYPE (decl) == TREE_TYPE (previous_decl)
2728 && DECL_LANGUAGE (previous_decl) == lang_c)
2734 tree cname = TYPE_NAME (ctype);
2735 if (TREE_CODE (cname) == TYPE_DECL)
2736 cname = DECL_NAME (cname);
2738 /* A method friend. */
2739 if (TREE_CODE (decl) == FUNCTION_DECL)
2741 if (flags == NO_SPECIAL && ctype && declarator == cname)
2742 DECL_CONSTRUCTOR_P (decl) = 1;
2744 /* This will set up DECL_ARGUMENTS for us. */
2745 grokclassfn (ctype, cname, decl, flags, quals);
2746 if (TYPE_SIZE (ctype) != 0)
2747 check_classfn (ctype, cname, decl);
2749 if (TREE_TYPE (decl) != error_mark_node)
2751 if (TYPE_SIZE (ctype))
2753 /* We don't call pushdecl here yet, or ever on this
2754 actual FUNCTION_DECL. We must preserve its TREE_CHAIN
2756 make_decl_rtl (decl, NULL_PTR, 1);
2757 add_friend (current_class_type, decl);
2761 register char *classname
2762 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype)));
2764 error ("member declared as friend before type `%s' defined",
2771 /* Possibly a bunch of method friends. */
2773 /* Get the class they belong to. */
2774 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2776 /* This class is defined, use its methods now. */
2777 if (TYPE_SIZE (ctype))
2779 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2781 add_friends (current_class_type, declarator, ctype);
2783 error ("method `%s' is not a member of class `%s'",
2784 IDENTIFIER_POINTER (declarator),
2785 IDENTIFIER_POINTER (cname));
2788 /* Note: DECLARATOR actually has more than one; in this
2789 case, we're making sure that fns with the name DECLARATOR
2790 and type CTYPE know they are friends of the current
2792 xref_friend (current_class_type, declarator, ctype);
2793 decl = void_type_node;
2796 /* never overload C functions */
2797 else if (TREE_CODE (decl) == FUNCTION_DECL
2798 && ((IDENTIFIER_LENGTH (declarator) == 4
2799 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2800 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2801 || (IDENTIFIER_LENGTH (declarator) > 10
2802 && IDENTIFIER_POINTER (declarator)[0] == '_'
2803 && IDENTIFIER_POINTER (declarator)[1] == '_'
2804 && strncmp (IDENTIFIER_POINTER (declarator)+2,
2805 "builtin_", 8) == 0)
2808 /* raw "main", and builtin functions never gets overloaded,
2809 but they can become friends. */
2810 TREE_PUBLIC (decl) = 1;
2811 add_friend (current_class_type, decl);
2812 DECL_FRIEND_P (decl) = 1;
2813 if (IDENTIFIER_POINTER (declarator)[0] == '_')
2815 if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "new"))
2816 TREE_GETS_NEW (current_class_type) = 0;
2817 else if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "delete"))
2818 TREE_GETS_DELETE (current_class_type) = 0;
2820 decl = void_type_node;
2823 @@ or possibly a friend from a base class ?!? */
2824 else if (TREE_CODE (decl) == FUNCTION_DECL)
2826 /* Friends must all go through the overload machinery,
2827 even though they may not technically be overloaded.
2829 Note that because classes all wind up being top-level
2830 in their scope, their friend wind up in top-level scope as well. */
2831 DECL_ASSEMBLER_NAME (decl)
2832 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2833 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2834 DECL_ARGUMENTS (decl) = parmdecls;
2835 DECL_CLASS_CONTEXT (decl) = current_class_type;
2837 /* We can call pushdecl here, because the TREE_CHAIN of this
2838 FUNCTION_DECL is not needed for other purposes. */
2839 decl = pushdecl_top_level (decl);
2841 make_decl_rtl (decl, NULL_PTR, 1);
2842 add_friend (current_class_type, decl);
2844 if (! TREE_OVERLOADED (declarator)
2845 && IDENTIFIER_GLOBAL_VALUE (declarator)
2846 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (declarator)) == FUNCTION_DECL)
2848 error ("friend `%s' implicitly overloaded",
2849 IDENTIFIER_POINTER (declarator));
2850 cp_error_at ("after declaration of non-overloaded `%D'", IDENTIFIER_GLOBAL_VALUE (declarator));
2852 DECL_FRIEND_P (decl) = 1;
2853 DECL_OVERLOADED (decl) = 1;
2854 TREE_OVERLOADED (declarator) = 1;
2855 decl = push_overloaded_decl (decl, 1);
2859 /* @@ Should be able to ingest later definitions of this function
2861 tree decl = IDENTIFIER_GLOBAL_VALUE (declarator);
2862 if (decl == NULL_TREE)
2864 warning ("implicitly declaring `%s' as struct",
2865 IDENTIFIER_POINTER (declarator));
2866 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2867 decl = TYPE_NAME (decl);
2870 /* Allow abbreviated declarations of overloaded functions,
2871 but not if those functions are really class names. */
2872 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2874 warning ("`friend %s' archaic, use `friend class %s' instead",
2875 IDENTIFIER_POINTER (declarator),
2876 IDENTIFIER_POINTER (declarator));
2877 decl = TREE_TYPE (TREE_PURPOSE (decl));
2880 if (TREE_CODE (decl) == TREE_LIST)
2881 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2883 make_friend_class (current_class_type, TREE_TYPE (decl));
2884 decl = void_type_node;
2889 /* TYPE has now been defined. It may, however, have a number of things
2890 waiting make make it their friend. We resolve these references
2893 embrace_waiting_friends (type)
2896 tree decl = TYPE_NAME (type);
2899 if (TREE_CODE (decl) != TYPE_DECL)
2902 for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2903 waiters = TREE_CHAIN (waiters))
2905 tree waiter = TREE_PURPOSE (waiters);
2907 tree waiter_prev = TREE_VALUE (waiters);
2909 tree decl = TREE_TYPE (waiters);
2910 tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2911 ? decl : DECL_NAME (decl)) : NULL_TREE;
2914 /* @@ There may be work to be done since we have not verified
2915 @@ consistency between original and friend declarations
2916 @@ of the functions waiting to become friends. */
2917 tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
2920 add_friends (waiter, name, type);
2922 add_friend (waiter, decl);
2924 error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2925 DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2926 "no method `%s' defined in class `%s' to be friend",
2927 IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
2928 TYPE_NAME_STRING (type));
2931 make_friend_class (type, waiter);
2934 if (TREE_CHAIN (waiter_prev))
2935 TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2937 DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2942 /* Common subroutines of build_new and build_vec_delete. */
2944 /* Common interface for calling "builtin" functions that are not
2948 build_builtin_call (type, node, arglist)
2953 tree rval = build (CALL_EXPR, type, node, arglist, 0);
2954 TREE_SIDE_EFFECTS (rval) = 1;
2955 assemble_external (TREE_OPERAND (node, 0));
2956 TREE_USED (TREE_OPERAND (node, 0)) = 1;
2960 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2961 (which needs to go through some sort of groktypename) or it
2962 is the name of the class we are newing. INIT is an initialization value.
2963 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2964 If INIT is void_type_node, it means do *not* call a constructor
2967 For types with constructors, the data returned is initialized
2968 by the appropriate constructor.
2970 Whether the type has a constructor or not, if it has a pointer
2971 to a virtual function table, then that pointer is set up
2974 Unless I am mistaken, a call to new () will return initialized
2975 data regardless of whether the constructor itself is private or
2978 Note that build_new does nothing to assure that any special
2979 alignment requirements of the type are met. Rather, it leaves
2980 it up to malloc to do the right thing. Otherwise, folding to
2981 the right alignment cal cause problems if the user tries to later
2982 free the memory returned by `new'.
2984 PLACEMENT is the `placement' list for user-defined operator new (). */
2987 build_new (placement, decl, init, use_global_new)
2992 tree type, true_type, size, rval;
2993 tree init1 = NULL_TREE, nelts;
2994 int has_call = 0, has_array = 0;
2996 tree pending_sizes = NULL_TREE;
2998 if (decl == error_mark_node)
2999 return error_mark_node;
3001 if (TREE_CODE (decl) == TREE_LIST)
3003 tree absdcl = TREE_VALUE (decl);
3004 tree last_absdcl = NULL_TREE;
3005 int old_immediate_size_expand;
3007 if (current_function_decl
3008 && DECL_CONSTRUCTOR_P (current_function_decl))
3010 old_immediate_size_expand = immediate_size_expand;
3011 immediate_size_expand = 0;
3014 nelts = integer_one_node;
3016 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
3018 /* probably meant to be a call */
3020 init1 = TREE_OPERAND (absdcl, 1);
3021 absdcl = TREE_OPERAND (absdcl, 0);
3022 TREE_VALUE (decl) = absdcl;
3024 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
3026 last_absdcl = absdcl;
3027 absdcl = TREE_OPERAND (absdcl, 0);
3030 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
3032 /* probably meant to be a vec new */
3035 while (TREE_OPERAND (absdcl, 0)
3036 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
3038 last_absdcl = absdcl;
3039 absdcl = TREE_OPERAND (absdcl, 0);
3043 this_nelts = TREE_OPERAND (absdcl, 1);
3044 if (this_nelts != error_mark_node)
3046 if (this_nelts == NULL_TREE)
3047 error ("new of array type fails to specify size");
3050 this_nelts = save_expr (this_nelts);
3051 absdcl = TREE_OPERAND (absdcl, 0);
3052 if (this_nelts == integer_zero_node)
3054 warning ("zero size array reserves no space");
3055 nelts = integer_zero_node;
3058 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3062 nelts = integer_zero_node;
3066 TREE_OPERAND (last_absdcl, 0) = absdcl;
3068 TREE_VALUE (decl) = absdcl;
3070 type = true_type = groktypename (decl);
3071 if (! type || type == error_mark_node
3072 || true_type == error_mark_node)
3074 immediate_size_expand = old_immediate_size_expand;
3075 return error_mark_node;
3078 /* ``A reference cannot be created by the new operator. A reference
3079 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3080 returned by new.'' ARM 5.3.3 */
3081 if (TREE_CODE (type) == REFERENCE_TYPE)
3082 error ("new cannot be applied to a reference type");
3084 type = TYPE_MAIN_VARIANT (type);
3085 if (type == void_type_node)
3087 error ("invalid type: `void []'");
3088 return error_mark_node;
3090 if (current_function_decl
3091 && DECL_CONSTRUCTOR_P (current_function_decl))
3093 pending_sizes = get_pending_sizes ();
3094 immediate_size_expand = old_immediate_size_expand;
3097 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
3099 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
3101 /* An aggregate type. */
3102 type = IDENTIFIER_TYPE_VALUE (decl);
3103 decl = TYPE_NAME (type);
3107 /* A builtin type. */
3108 decl = lookup_name (decl, 1);
3109 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
3110 type = TREE_TYPE (decl);
3114 else if (TREE_CODE (decl) == TYPE_DECL)
3116 type = TREE_TYPE (decl);
3123 decl = TYPE_NAME (type);
3126 if (TYPE_SIZE (type) == 0)
3128 if (type == void_type_node)
3129 error ("invalid type for new: `void'");
3131 incomplete_type_error (0, type);
3132 return error_mark_node;
3135 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_ABSTRACT_VIRTUALS (type))
3137 abstract_virtuals_error (NULL_TREE, type);
3138 return error_mark_node;
3141 if (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE (type))
3143 signature_error (NULL_TREE, type);
3144 return error_mark_node;
3147 /* If our base type is an array, then make sure we know how many elements
3149 while (TREE_CODE (true_type) == ARRAY_TYPE)
3151 tree this_nelts = array_type_nelts_top (true_type);
3152 if (nelts == integer_one_node)
3159 my_friendly_assert (has_array != 0, 216);
3160 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3162 true_type = TREE_TYPE (true_type);
3165 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
3168 size = size_in_bytes (type);
3173 /* Get a little extra space to store a couple of things before the new'ed
3175 if (has_array && TYPE_NEEDS_DESTRUCTOR (true_type))
3177 tree extra = BI_header_size;
3179 size = size_binop (PLUS_EXPR, size, extra);
3182 /* Allocate the object. */
3183 if (TYPE_LANG_SPECIFIC (true_type)
3184 && (TREE_GETS_NEW (true_type) || TREE_GETS_PLACED_NEW (true_type))
3186 rval = build_opfncall (NEW_EXPR, LOOKUP_NORMAL,
3187 TYPE_POINTER_TO (true_type), size, placement);
3190 rval = build_opfncall (NEW_EXPR, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
3191 ptr_type_node, size, placement);
3192 rval = convert (TYPE_POINTER_TO (true_type), rval);
3194 else if (flag_this_is_variable > 0
3195 && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3197 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
3201 error ("constructors take parameter lists");
3202 return error_mark_node;
3207 rval = build_builtin_call (build_pointer_type (true_type),
3208 BIN, build_tree_list (NULL_TREE, size));
3210 /* See comment above as to why this is disabled. */
3213 rval = build (PLUS_EXPR, TYPE_POINTER_TO (true_type), rval,
3215 rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (true_type),
3216 rval, build1 (BIT_NOT_EXPR, integer_type_node,
3220 TREE_CALLS_NEW (rval) = 1;
3221 TREE_SIDE_EFFECTS (rval) = 1;
3224 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
3225 sure we have some extra bytes in that case for the BI_header_size
3226 cookies? And how does that interact with the code below? (mrs) */
3227 /* Finish up some magic for new'ed arrays */
3228 if (has_array && TYPE_NEEDS_DESTRUCTOR (true_type) && rval != NULL_TREE)
3230 tree extra = BI_header_size;
3232 rval = convert (ptr_type_node, rval); /* convert to void * first */
3233 rval = convert (string_type_node, rval); /* lets not add void* and ints */
3234 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
3235 /* Store header info. */
3236 cookie = build_indirect_ref (build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
3237 rval, extra), NULL_PTR);
3238 exp1 = build (MODIFY_EXPR, void_type_node,
3239 build_component_ref (cookie, nc_nelts_field_id, 0, 0),
3241 TREE_SIDE_EFFECTS (exp1) = 1;
3242 rval = convert (build_pointer_type (true_type), rval);
3243 TREE_CALLS_NEW (rval) = 1;
3244 TREE_SIDE_EFFECTS (rval) = 1;
3245 rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
3246 build_tree_list (NULL_TREE, rval)));
3249 /* We've figured out where the allocation is to go.
3250 If we're not eliding constructors, then if a constructor
3251 is defined, we must go through it. */
3252 if (!has_array && (rval == NULL_TREE || !flag_elide_constructors)
3253 && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3256 /* Constructors are never virtual. If it has an initialization, we
3257 need to complain if we aren't allowed to use the ctor that took
3259 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
3261 /* If a copy constructor might work, set things up so that we can
3262 try that after this. We deliberately don't clear LOOKUP_COMPLAIN
3263 any more, since that would make it impossible to rationally use
3264 the access of a constructor that matches perfectly. */
3266 if (rval != NULL_TREE)
3267 flags |= LOOKUP_SPECULATIVELY;
3270 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
3272 init = tree_cons (NULL_TREE, integer_one_node, init);
3273 flags |= LOOKUP_HAS_IN_CHARGE;
3279 if (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE)
3280 tmp = build_indirect_ref (tmp, NULL_PTR);
3282 newrval = build_method_call (tmp, constructor_name_full (true_type),
3283 init, NULL_TREE, flags);
3289 TREE_HAS_CONSTRUCTOR (rval) = 1;
3292 rval = error_mark_node;
3296 if (rval == error_mark_node)
3297 return error_mark_node;
3298 rval = save_expr (rval);
3299 TREE_HAS_CONSTRUCTOR (rval) = 1;
3301 /* Don't call any constructors or do any initialization. */
3302 if (init == void_type_node)
3305 if (TYPE_NEEDS_CONSTRUCTING (type)
3306 || (has_call || init))
3308 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
3310 /* New 2.0 interpretation: `new int (10)' means
3311 allocate an int, and initialize it with 10. */
3313 init = build_c_cast (type, init);
3314 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
3315 build_modify_expr (build_indirect_ref (rval, NULL_PTR),
3318 TREE_SIDE_EFFECTS (rval) = 1;
3319 TREE_CALLS_NEW (rval) = 1;
3321 else if (current_function_decl == NULL_TREE)
3323 extern tree static_aggregates;
3325 /* In case of static initialization, SAVE_EXPR is good enough. */
3326 init = copy_to_permanent (init);
3327 rval = copy_to_permanent (rval);
3328 static_aggregates = perm_tree_cons (init, rval, static_aggregates);
3332 /* Have to wrap this in RTL_EXPR for two cases:
3333 in base or member initialization and if we
3334 are a branch of a ?: operator. Since we
3335 can't easily know the latter, just do it always. */
3336 tree xval = make_node (RTL_EXPR);
3338 TREE_TYPE (xval) = TREE_TYPE (rval);
3339 do_pending_stack_adjust ();
3340 start_sequence_for_rtl_expr (xval);
3342 /* As a matter of principle, `start_sequence' should do this. */
3346 rval = expand_vec_init (decl, rval,
3347 build_binary_op (MINUS_EXPR, nelts, integer_one_node, 1),
3350 expand_aggr_init (build_indirect_ref (rval, NULL_PTR), init, 0);
3352 do_pending_stack_adjust ();
3354 TREE_SIDE_EFFECTS (xval) = 1;
3355 TREE_CALLS_NEW (xval) = 1;
3356 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3359 if (TREE_CODE (rval) == SAVE_EXPR)
3361 /* Errors may cause this to not get evaluated. */
3362 if (SAVE_EXPR_RTL (rval) == 0)
3363 SAVE_EXPR_RTL (rval) = const0_rtx;
3364 RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
3368 my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
3369 RTL_EXPR_RTL (xval) = DECL_RTL (rval);
3375 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
3377 /* The type of new int [3][3] is not int *, but int [3] * */
3378 rval = build_c_cast (build_pointer_type (type), rval);
3382 rval = build_compound_expr (chainon (pending_sizes,
3383 build_tree_list (NULL_TREE, rval)));
3387 extern tree gc_visible;
3391 rval = save_expr (rval);
3392 /* We don't need a `headof' operation to do this because
3393 we know where the object starts. */
3394 objbits = build1 (INDIRECT_REF, unsigned_type_node,
3395 build (MINUS_EXPR, ptr_type_node,
3396 rval, c_sizeof_nowarn (unsigned_type_node)));
3397 update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible);
3398 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
3399 tree_cons (NULL_TREE, update_expr,
3400 build_tree_list (NULL_TREE, rval))));
3403 return save_expr (rval);
3406 /* `expand_vec_init' performs initialization of a vector of aggregate
3409 DECL is passed only for error reporting, and provides line number
3410 and source file name information.
3411 BASE is the space where the vector will be.
3412 MAXINDEX is the maximum index of the array (one less than the
3413 number of elements).
3414 INIT is the (possibly NULL) initializer.
3416 FROM_ARRAY is 0 if we should init everything with INIT
3417 (i.e., every element initialized from INIT).
3418 FROM_ARRAY is 1 if we should index into INIT in parallel
3419 with initialization of DECL.
3420 FROM_ARRAY is 2 if we should index into INIT in parallel,
3421 but use assignment instead of initialization. */
3424 expand_vec_init (decl, base, maxindex, init, from_array)
3425 tree decl, base, maxindex, init;
3429 tree iterator, base2 = NULL_TREE;
3430 tree type = TREE_TYPE (TREE_TYPE (base));
3433 maxindex = convert (integer_type_node, maxindex);
3434 if (maxindex == error_mark_node)
3435 return error_mark_node;
3437 if (current_function_decl == NULL_TREE)
3439 rval = make_tree_vec (3);
3440 TREE_VEC_ELT (rval, 0) = base;
3441 TREE_VEC_ELT (rval, 1) = maxindex;
3442 TREE_VEC_ELT (rval, 2) = init;
3446 size = size_in_bytes (type);
3448 /* Set to zero in case size is <= 0. Optimizer will delete this if
3449 it is not needed. */
3450 rval = get_temp_regvar (TYPE_POINTER_TO (type),
3451 convert (TYPE_POINTER_TO (type), null_pointer_node));
3452 base = default_conversion (base);
3453 base = convert (TYPE_POINTER_TO (type), base);
3454 expand_assignment (rval, base, 0, 0);
3455 base = get_temp_regvar (TYPE_POINTER_TO (type), base);
3457 if (init != NULL_TREE
3458 && TREE_CODE (init) == CONSTRUCTOR
3459 && TREE_TYPE (init) == TREE_TYPE (decl))
3461 /* Initialization of array from {...}. */
3462 tree elts = CONSTRUCTOR_ELTS (init);
3463 tree baseref = build1 (INDIRECT_REF, type, base);
3464 tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
3465 int host_i = TREE_INT_CST_LOW (maxindex);
3467 if (IS_AGGR_TYPE (type))
3472 expand_aggr_init (baseref, TREE_VALUE (elts), 0);
3474 expand_assignment (base, baseinc, 0, 0);
3475 elts = TREE_CHAIN (elts);
3477 /* Initialize any elements by default if possible. */
3480 if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3483 use_variable (DECL_RTL (base));
3487 iterator = get_temp_regvar (integer_type_node,
3488 build_int_2 (host_i, 0));
3490 goto init_by_default;
3496 expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3498 expand_assignment (base, baseinc, 0, 0);
3499 elts = TREE_CHAIN (elts);
3503 use_variable (DECL_RTL (base));
3509 iterator = get_temp_regvar (integer_type_node, maxindex);
3513 /* If initializing one array from another,
3514 initialize element by element. */
3517 /* We rely upon the below calls the do argument checking */
3518 if (decl == NULL_TREE)
3520 sorry ("initialization of array from dissimilar array type");
3521 return error_mark_node;
3525 base2 = default_conversion (init);
3526 itype = TREE_TYPE (base2);
3527 base2 = get_temp_regvar (itype, base2);
3528 itype = TREE_TYPE (itype);
3530 else if (TYPE_LANG_SPECIFIC (type)
3531 && TYPE_NEEDS_CONSTRUCTING (type)
3532 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3534 error ("initializer ends prematurely");
3535 return error_mark_node;
3539 expand_start_cond (build (GE_EXPR, integer_type_node,
3540 iterator, integer_zero_node), 0);
3541 expand_start_loop_continue_elsewhere (1);
3545 tree to = build1 (INDIRECT_REF, type, base);
3549 from = build1 (INDIRECT_REF, itype, base2);
3553 if (from_array == 2)
3554 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3555 else if (TYPE_NEEDS_CONSTRUCTING (type))
3556 expand_aggr_init (to, from, 0);
3558 expand_assignment (to, from, 0, 0);
3560 my_friendly_abort (57);
3562 else if (TREE_CODE (type) == ARRAY_TYPE)
3565 sorry ("cannot initialize multi-dimensional array with initializer");
3566 expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
3567 array_type_nelts (type), 0, 0);
3570 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
3572 expand_assignment (base,
3573 build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
3576 expand_assignment (base2,
3577 build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
3578 expand_loop_continue_here ();
3579 expand_exit_loop_if_false (0, build (NE_EXPR, integer_type_node,
3580 build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
3584 use_variable (DECL_RTL (base));
3586 use_variable (DECL_RTL (base2));
3591 use_variable (DECL_RTL (iterator));
3596 use_variable (DECL_RTL (rval));
3600 /* Free up storage of type TYPE, at address ADDR.
3602 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3605 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3606 used as the second argument to operator delete. It can include
3607 things like padding and magic size cookies. It has virtual in it,
3608 because if you have a base pointer and you delete through a virtual
3609 destructor, it should be the size of the dynamic object, not the
3610 static object, see Free Store 12.5 ANSI C++ WP.
3612 This does not call any destructors. */
3614 build_x_delete (type, addr, use_global_delete, virtual_size)
3616 int use_global_delete;
3621 if (!use_global_delete
3622 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3623 && TREE_GETS_DELETE (TREE_TYPE (type)))
3624 rval = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3625 virtual_size, NULL_TREE);
3627 rval = build_builtin_call (void_type_node, BID,
3628 build_tree_list (NULL_TREE, addr));
3632 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3633 ADDR is an expression which yields the store to be destroyed.
3634 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3635 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3636 virtual baseclasses.
3637 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3639 FLAGS is the logical disjunction of zero or more LOOKUP_
3640 flags. See cp-tree.h for more info.
3642 This function does not delete an object's virtual base classes. */
3644 build_delete (type, addr, auto_delete, flags, use_global_delete)
3648 int use_global_delete;
3650 tree function, parms;
3656 if (addr == error_mark_node)
3657 return error_mark_node;
3659 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3660 set to `error_mark_node' before it gets properly cleaned up. */
3661 if (type == error_mark_node)
3662 return error_mark_node;
3664 type = TYPE_MAIN_VARIANT (type);
3666 if (TREE_CODE (type) == POINTER_TYPE)
3668 type = TREE_TYPE (type);
3669 if (TYPE_SIZE (type) == 0)
3671 incomplete_type_error (0, type);
3672 return error_mark_node;
3674 if (TREE_CODE (type) == ARRAY_TYPE)
3676 if (! IS_AGGR_TYPE (type))
3678 /* Call the builtin operator delete. */
3679 return build_builtin_call (void_type_node, BID,
3680 build_tree_list (NULL_TREE, addr));
3682 if (TREE_SIDE_EFFECTS (addr))
3683 addr = save_expr (addr);
3684 ref = build_indirect_ref (addr, NULL_PTR);
3687 else if (TREE_CODE (type) == ARRAY_TYPE)
3690 if (TREE_SIDE_EFFECTS (addr))
3691 addr = save_expr (addr);
3692 return build_vec_delete (addr, array_type_nelts (type),
3693 c_sizeof_nowarn (TREE_TYPE (type)),
3694 NULL_TREE, auto_delete, integer_two_node);
3698 /* Don't check PROTECT here; leave that decision to the
3699 destructor. If the destructor is accessible, call it,
3700 else report error. */
3701 addr = build_unary_op (ADDR_EXPR, addr, 0);
3702 if (TREE_SIDE_EFFECTS (addr))
3703 addr = save_expr (addr);
3705 if (TREE_CONSTANT (addr))
3706 addr = convert_pointer_to (type, addr);
3708 addr = convert_force (build_pointer_type (type), addr);
3710 if (TREE_CODE (addr) == NOP_EXPR
3711 && TREE_OPERAND (addr, 0) == current_class_decl)
3714 ref = build_indirect_ref (addr, NULL_PTR);
3718 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3720 if (! TYPE_NEEDS_DESTRUCTOR (type))
3724 if (auto_delete == integer_zero_node)
3725 return void_zero_node;
3727 /* Pass the size of the object down to the operator delete() in
3728 addition to the ADDR. */
3729 if (TREE_GETS_DELETE (type) && !use_global_delete)
3731 /* This is probably wrong. It should be the size of the virtual
3732 object being deleted. */
3733 tree virtual_size = c_sizeof_nowarn (type);
3734 return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3735 virtual_size, NULL_TREE);
3738 /* Call the builtin operator delete. */
3739 return build_builtin_call (void_type_node, BID,
3740 build_tree_list (NULL_TREE, addr));
3742 parms = build_tree_list (NULL_TREE, addr);
3744 /* Below, we will reverse the order in which these calls are made.
3745 If we have a destructor, then that destructor will take care
3746 of the base classes; otherwise, we must do that here. */
3747 if (TYPE_HAS_DESTRUCTOR (type))
3749 tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0));
3750 tree basetypes = TYPE_BINFO (type);
3752 if (flags & LOOKUP_PROTECT)
3754 enum access_type access = compute_access (basetypes, dtor);
3756 if (access == access_private)
3758 if (flags & LOOKUP_COMPLAIN)
3759 cp_error ("destructor for type `%T' is private in this scope", type);
3760 return error_mark_node;
3762 else if (access == access_protected)
3764 if (flags & LOOKUP_COMPLAIN)
3765 cp_error ("destructor for type `%T' is protected in this scope", type);
3766 return error_mark_node;
3770 /* Once we are in a destructor, try not going through
3771 the virtual function table to find the next destructor. */
3772 if (DECL_VINDEX (dtor)
3773 && ! (flags & LOOKUP_NONVIRTUAL)
3774 && TREE_CODE (auto_delete) != PARM_DECL
3775 && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
3777 tree binfo, basetype;
3778 /* The code below is probably all broken. See call.c for the
3779 complete right way to do this. this offsets may not be right
3780 in the below. (mrs) */
3781 /* This destructor must be called via virtual function table. */
3782 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0);
3783 basetype = DECL_CLASS_CONTEXT (dtor);
3784 binfo = get_binfo (basetype,
3785 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3787 expr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3788 if (expr != TREE_VALUE (parms))
3791 ref = build_indirect_ref (expr, NULL_PTR);
3792 TREE_VALUE (parms) = expr;
3794 function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
3795 if (function == error_mark_node)
3796 return error_mark_node;
3797 TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
3798 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3799 expr = build_function_call (function, parms);
3800 if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3802 /* Handle the case where a virtual destructor is
3803 being called on an item that is 0.
3805 @@ Does this really need to be done? */
3806 tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
3808 if (TREE_CODE (ref) == VAR_DECL
3809 || TREE_CODE (ref) == COMPONENT_REF)
3810 warning ("losing in build_delete");
3812 expr = build (COND_EXPR, void_type_node,
3813 ifexp, expr, void_zero_node);
3820 if ((flags & LOOKUP_DESTRUCTOR)
3821 || TREE_CODE (ref) == VAR_DECL
3822 || TREE_CODE (ref) == PARM_DECL
3823 || TREE_CODE (ref) == COMPONENT_REF
3824 || TREE_CODE (ref) == ARRAY_REF)
3825 /* These can't be 0. */
3826 ifexp = integer_one_node;
3828 /* Handle the case where a non-virtual destructor is
3829 being called on an item that is 0. */
3830 ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
3832 /* Used to mean that this destructor was known to be empty,
3833 but that's now obsolete. */
3834 my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
3836 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3837 expr = build_function_call (dtor, parms);
3839 if (ifexp != integer_one_node)
3840 expr = build (COND_EXPR, void_type_node,
3841 ifexp, expr, void_zero_node);
3847 /* This can get visibilities wrong. */
3848 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3849 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3850 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3851 tree exprstmt = NULL_TREE;
3852 tree parent_auto_delete = auto_delete;
3855 /* If this type does not have a destructor, but does have
3856 operator delete, call the parent parent destructor (if any),
3857 but let this node do the deleting. Otherwise, it is ok
3858 to let the parent destructor do the deleting. */
3859 if (TREE_GETS_DELETE (type) && !use_global_delete)
3861 parent_auto_delete = integer_zero_node;
3862 if (auto_delete == integer_zero_node)
3868 /* This is probably wrong. It should be the size of the
3869 virtual object being deleted. */
3870 virtual_size = c_sizeof_nowarn (type);
3872 expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3873 virtual_size, NULL_TREE);
3874 if (expr == error_mark_node)
3875 return error_mark_node;
3876 if (auto_delete != integer_one_node)
3877 cond = build (COND_EXPR, void_type_node,
3878 build (BIT_AND_EXPR, integer_type_node,
3879 auto_delete, integer_one_node),
3880 expr, void_zero_node);
3885 else if (base_binfo == NULL_TREE
3886 || (TREE_VIA_VIRTUAL (base_binfo) == 0
3887 && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
3891 /* This is probably wrong. It should be the size of the virtual
3892 object being deleted. */
3893 virtual_size = c_sizeof_nowarn (type);
3895 cond = build (COND_EXPR, void_type_node,
3896 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3897 build_builtin_call (void_type_node, BID,
3898 build_tree_list (NULL_TREE, addr)),
3905 exprstmt = build_tree_list (NULL_TREE, cond);
3908 && ! TREE_VIA_VIRTUAL (base_binfo)
3909 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3911 tree this_auto_delete;
3913 if (BINFO_OFFSET_ZEROP (base_binfo))
3914 this_auto_delete = parent_auto_delete;
3916 this_auto_delete = integer_zero_node;
3918 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), addr,
3919 this_auto_delete, flags, 0);
3920 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3923 /* Take care of the remaining baseclasses. */
3924 for (i = 1; i < n_baseclasses; i++)
3926 base_binfo = TREE_VEC_ELT (binfos, i);
3927 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3928 || TREE_VIA_VIRTUAL (base_binfo))
3931 /* May be zero offset if other baseclasses are virtual. */
3932 expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
3933 addr, BINFO_OFFSET (base_binfo)));
3935 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), expr,
3939 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3942 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3944 if (TREE_CODE (member) != FIELD_DECL)
3946 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3948 tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
3949 tree this_type = TREE_TYPE (member);
3950 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3951 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3956 return build_compound_expr (exprstmt);
3957 /* Virtual base classes make this function do nothing. */
3958 return void_zero_node;
3962 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3965 build_vbase_delete (type, decl)
3968 tree vbases = CLASSTYPE_VBASECLASSES (type);
3969 tree result = NULL_TREE;
3970 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3972 my_friendly_assert (addr != error_mark_node, 222);
3976 tree this_addr = convert_force (TYPE_POINTER_TO (BINFO_TYPE (vbases)),
3978 result = tree_cons (NULL_TREE,
3979 build_delete (TREE_TYPE (this_addr), this_addr,
3981 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3983 vbases = TREE_CHAIN (vbases);
3985 return build_compound_expr (nreverse (result));
3988 /* Build a C++ vector delete expression.
3989 MAXINDEX is the number of elements to be deleted.
3990 ELT_SIZE is the nominal size of each element in the vector.
3991 BASE is the expression that should yield the store to be deleted.
3992 DTOR_DUMMY is a placeholder for a destructor. The library function
3993 __builtin_vec_delete has a pointer to function in this position.
3994 This function expands (or synthesizes) these calls itself.
3995 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3996 AUTO_DELETE say whether each item in the container should be deallocated.
3998 This also calls delete for virtual baseclasses of elements of the vector.
4000 Update: MAXINDEX is no longer needed. The size can be extracted from the
4001 start of the vector for pointers, and from the type for arrays. We still
4002 use MAXINDEX for arrays because it happens to already have one of the
4003 values we'd have to extract. (We could use MAXINDEX with pointers to
4004 confirm the size, and trap if the numbers differ; not clear that it'd
4005 be worth bothering.) */
4007 build_vec_delete (base, maxindex, elt_size, dtor_dummy, auto_delete_vec, auto_delete)
4008 tree base, maxindex, elt_size;
4010 tree auto_delete_vec, auto_delete;
4012 tree ptype = TREE_TYPE (base);
4015 /* Temporary variables used by the loop. */
4016 tree tbase, size_exp, tbase_init;
4018 /* This is the body of the loop that implements the deletion of a
4019 single element, and moves temp variables to next elements. */
4022 /* This is the LOOP_EXPR that governs the deletion of the elements. */
4025 /* This is the thing that governs what to do after the loop has run. */
4026 tree deallocate_expr = 0;
4028 /* This is the BIND_EXPR which holds the outermost iterator of the
4029 loop. It is convenient to set this variable up and test it before
4030 executing any other code in the loop.
4031 This is also the containing expression returned by this function. */
4032 tree controller = NULL_TREE;
4034 /* This is the BLOCK to record the symbol binding for debugging. */
4037 base = stabilize_reference (base);
4039 /* Since we can use base many times, save_expr it. */
4040 if (TREE_SIDE_EFFECTS (base))
4041 base = save_expr (base);
4043 if (TREE_CODE (ptype) == POINTER_TYPE)
4045 /* Step back one from start of vector, and read dimension. */
4046 tree cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
4047 base, BI_header_size);
4048 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
4049 maxindex = build_component_ref (cookie, nc_nelts_field_id, 0, 0);
4051 ptype = TREE_TYPE (ptype);
4052 while (TREE_CODE (ptype) == ARRAY_TYPE);
4054 else if (TREE_CODE (ptype) == ARRAY_TYPE)
4056 /* get the total number of things in the array, maxindex is a bad name */
4057 maxindex = array_type_nelts_total (ptype);
4058 while (TREE_CODE (ptype) == ARRAY_TYPE)
4059 ptype = TREE_TYPE (ptype);
4060 base = build_unary_op (ADDR_EXPR, base, 1);
4064 error ("type to vector delete is neither pointer or array type");
4065 return error_mark_node;
4068 ptype = TYPE_POINTER_TO (type);
4070 size_exp = size_in_bytes (type);
4072 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
4074 loop = integer_zero_node;
4078 /* The below is short by BI_header_size */
4079 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4081 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
4082 tbase_init = build_modify_expr (tbase, NOP_EXPR,
4083 fold (build (PLUS_EXPR, ptype,
4086 DECL_REGISTER (tbase) = 1;
4087 controller = build (BIND_EXPR, void_type_node, tbase, 0, 0);
4088 TREE_SIDE_EFFECTS (controller) = 1;
4089 block = build_block (tbase, 0, 0, 0, 0);
4090 add_block_current_level (block);
4092 if (auto_delete != integer_zero_node
4093 && auto_delete != integer_two_node)
4095 tree base_tbd = convert (ptype,
4096 build_binary_op (MINUS_EXPR,
4097 convert (ptr_type_node, base),
4100 /* This is the real size */
4101 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4102 body = build_tree_list (NULL_TREE,
4103 build_x_delete (ptr_type_node, base_tbd, 0,
4105 body = build (COND_EXPR, void_type_node,
4106 build (BIT_AND_EXPR, integer_type_node,
4107 auto_delete, integer_one_node),
4108 body, integer_zero_node);
4113 body = tree_cons (NULL_TREE,
4114 build_delete (ptype, tbase, auto_delete,
4115 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
4118 body = tree_cons (NULL_TREE,
4119 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
4122 body = tree_cons (NULL_TREE,
4123 build (EXIT_EXPR, void_type_node,
4124 build (EQ_EXPR, integer_type_node, base, tbase)),
4127 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
4129 loop = tree_cons (NULL_TREE, tbase_init,
4130 tree_cons (NULL_TREE, loop, NULL_TREE));
4131 loop = build_compound_expr (loop);
4134 /* If the delete flag is one, or anything else with the low bit set,
4135 delete the storage. */
4136 if (auto_delete_vec == integer_zero_node
4137 || auto_delete_vec == integer_two_node)
4138 deallocate_expr = integer_zero_node;
4143 /* The below is short by BI_header_size */
4144 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4146 if (loop == integer_zero_node)
4151 base_tbd = convert (ptype,
4152 build_binary_op (MINUS_EXPR,
4153 convert (string_type_node, base),
4156 /* True size with header. */
4157 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4159 deallocate_expr = build_x_delete (ptr_type_node, base_tbd, 1,
4161 if (auto_delete_vec != integer_one_node)
4162 deallocate_expr = build (COND_EXPR, void_type_node,
4163 build (BIT_AND_EXPR, integer_type_node,
4164 auto_delete_vec, integer_one_node),
4165 deallocate_expr, integer_zero_node);
4168 if (loop && deallocate_expr != integer_zero_node)
4170 body = tree_cons (NULL_TREE, loop,
4171 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
4172 body = build_compound_expr (body);
4177 /* Outermost wrapper: If pointer is null, punt. */
4178 body = build (COND_EXPR, void_type_node,
4179 build (NE_EXPR, integer_type_node, base, integer_zero_node),
4180 body, integer_zero_node);
4181 body = build1 (NOP_EXPR, void_type_node, body);
4185 TREE_OPERAND (controller, 1) = body;
4189 return convert (void_type_node, body);