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 static void expand_virtual_init PROTO((tree, tree, tree));
54 tree expand_vec_init ();
55 tree build_vec_delete ();
57 static void add_friend (), add_friends ();
59 /* Cache _builtin_new and _builtin_delete exprs. */
62 /* Cache the identifier nodes for the two magic field of a new cookie. */
63 static tree nc_nelts_field_id;
65 static tree nc_ptr_2comp_field_id;
68 static tree minus_one;
70 /* Set up local variable for this file. MUST BE CALLED AFTER
71 INIT_DECL_PROCESSING. */
73 tree BI_header_type, BI_header_size;
75 void init_init_processing ()
79 /* Define implicit `operator new' and `operator delete' functions. */
80 BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR])));
81 TREE_USED (TREE_OPERAND (BIN, 0)) = 0;
82 BID = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR])));
83 TREE_USED (TREE_OPERAND (BID, 0)) = 0;
84 minus_one = build_int_2 (-1, -1);
86 /* Define the structure that holds header information for
87 arrays allocated via operator new. */
88 BI_header_type = make_lang_type (RECORD_TYPE);
89 nc_nelts_field_id = get_identifier ("nelts");
90 fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
91 finish_builtin_type (BI_header_type, "__new_cookie", fields,
93 BI_header_size = size_in_bytes (BI_header_type);
96 /* Subroutine of emit_base_init. For BINFO, initialize all the
97 virtual function table pointers, except those that come from
98 virtual base classes. Initialize binfo's vtable pointer, if
99 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual
100 function table pointers in all bases have been initialized already,
101 probably because their constructors have just be run. */
103 init_vtbl_ptrs (binfo, init_self, can_elide)
105 int init_self, can_elide;
108 tree binfos = BINFO_BASETYPES (binfo);
109 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
111 for (i = 0; i < n_baselinks; i++)
113 tree base_binfo = TREE_VEC_ELT (binfos, i);
114 int is_not_base_vtable =
115 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
116 if (! TREE_VIA_VIRTUAL (base_binfo))
117 init_vtbl_ptrs (base_binfo, is_not_base_vtable, can_elide);
120 /* Before turning this on, make sure it is correct. */
121 if (can_elide && ! BINFO_MODIFIED (binfo))
124 /* Should we use something besides CLASSTYPE_VFIELDS? */
125 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
127 tree base_ptr = convert_pointer_to_real (binfo, current_class_decl);
128 expand_virtual_init (binfo, binfo, base_ptr);
133 /* Subroutine of emit_base_init. */
135 perform_member_init (member, name, init, explicit)
136 tree member, name, init;
140 tree type = TREE_TYPE (member);
142 if (TYPE_NEEDS_CONSTRUCTING (type)
143 || (init && TYPE_HAS_CONSTRUCTOR (type)))
145 /* Since `init' is already a TREE_LIST on the current_member_init_list,
146 only build it into one if we aren't already a list. */
147 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
148 init = build_tree_list (NULL_TREE, init);
150 decl = build_component_ref (C_C_D, name, 0, explicit);
153 && TREE_CODE (type) == ARRAY_TYPE
155 && TREE_CHAIN (init) == NULL_TREE
156 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
158 /* Initialization of one array from another. */
159 expand_vec_init (TREE_OPERAND (decl, 1), decl,
160 array_type_nelts (type), TREE_VALUE (init), 1);
163 expand_aggr_init (decl, init, 0);
167 if (init == NULL_TREE)
171 cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor",
172 member, current_class_type);
173 init = error_mark_node;
175 /* member traversal: note it leaves init NULL */
176 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
177 cp_pedwarn ("uninitialized reference member `%D'", member);
179 else if (TREE_CODE (init) == TREE_LIST)
181 /* There was an explicit member initialization. Do some
182 work in that case. */
183 if (TREE_CHAIN (init))
185 warning ("initializer list treated as compound expression");
186 init = build_compound_expr (init);
189 init = TREE_VALUE (init);
192 /* We only build this with a null init if we got it from the
193 current_member_init_list. */
194 if (init || explicit)
196 decl = build_component_ref (C_C_D, name, 0, explicit);
197 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
201 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
203 cplus_expand_start_try (1);
204 push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
208 /* Subroutine of emit_member_init. */
213 tree x, member, name, field, init;
214 tree init_list = NULL_TREE;
215 tree fields_to_unmark = NULL_TREE;
218 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
221 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
223 /* If we cleared this out, then pay no attention to it. */
224 if (TREE_PURPOSE (x) == NULL_TREE)
226 name = TREE_PURPOSE (x);
229 field = (TREE_CODE (name) == COMPONENT_REF
230 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
232 /* Let's find out when this happens. */
233 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
234 field = IDENTIFIER_CLASS_VALUE (name);
237 /* If one member shadows another, get the outermost one. */
238 if (TREE_CODE (field) == TREE_LIST)
239 field = TREE_VALUE (field);
243 /* See if we already found an initializer for this field. */
246 if (DECL_NAME (field))
247 cp_error ("multiple initializations given for member `%D'",
252 init_list = chainon (init_list,
253 build_tree_list (name, TREE_VALUE (x)));
254 /* Make sure we won't try to work on this init again. */
255 TREE_PURPOSE (x) = NULL_TREE;
261 /* If we didn't find MEMBER in the list, create a dummy entry
262 so the two lists (INIT_LIST and the list of members) will be
265 init_list = chainon (init_list, build_tree_list (NULL_TREE, NULL_TREE));
268 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
270 if (TREE_PURPOSE (x))
272 name = TREE_PURPOSE (x);
273 init = TREE_VALUE (x);
274 /* XXX: this may need the COMPONENT_REF operand 0 check if
275 it turns out we actually get them. */
276 field = IDENTIFIER_CLASS_VALUE (name);
278 /* If one member shadows another, get the outermost one. */
279 if (TREE_CODE (field) == TREE_LIST)
281 field = TREE_VALUE (field);
282 if (decl_type_context (field) != current_class_type)
283 cp_error ("field `%D' not in immediate context", field);
287 /* It turns out if you have an anonymous union in the
288 class, a member from it can end up not being on the
289 list of fields (rather, the type is), and therefore
290 won't be seen by the for loop above. */
292 /* The code in this for loop is derived from a general loop
293 which had this check in it. Theoretically, we've hit
294 every initialization for the list of members in T, so
295 we shouldn't have anything but these left in this list. */
296 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
299 if (TREE_HAS_CONSTRUCTOR (field))
301 if (DECL_NAME (field))
302 error ("multiple initializations given for member `%s'",
303 IDENTIFIER_POINTER (DECL_NAME (field)));
307 TREE_HAS_CONSTRUCTOR (field) = 1;
308 fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark);
310 perform_member_init (field, name, init, 1);
311 TREE_PURPOSE (x) = NULL_TREE;
315 /* Unmark fields which are initialized for the base class. */
316 while (fields_to_unmark)
318 TREE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0;
319 /* XXX is this a memory leak? */
320 fields_to_unmark = TREE_CHAIN (fields_to_unmark);
326 /* Perform whatever initializations have yet to be done on the base
327 class of the class variable. These actions are in the global
328 variable CURRENT_BASE_INIT_LIST. Such an action could be
329 NULL_TREE, meaning that the user has explicitly called the base
330 class constructor with no arguments.
332 If there is a need for a call to a constructor, we must surround
333 that call with a pushlevel/poplevel pair, since we are technically
334 at the PARM level of scope.
336 Argument IMMEDIATELY, if zero, forces a new sequence to be
337 generated to contain these new insns, so it can be emitted later.
338 This sequence is saved in the global variable BASE_INIT_INSNS.
339 Otherwise, the insns are emitted into the current sequence.
341 Note that emit_base_init does *not* initialize virtual base
342 classes. That is done specially, elsewhere. */
345 emit_base_init (t, immediately)
349 extern tree in_charge_identifier;
354 tree t_binfo = TYPE_BINFO (t);
355 tree binfos = BINFO_BASETYPES (t_binfo);
356 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
357 int have_init_list = 0, from_init_list;
361 do_pending_stack_adjust ();
365 if (write_symbols == NO_DEBUG)
366 /* As a matter of principle, `start_sequence' should do this. */
369 /* Always emit a line number note so we can step into constructors. */
370 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
371 DECL_SOURCE_LINE (current_function_decl));
373 /* In this case, we always need IN_CHARGE_NODE, because we have
374 to know whether to deallocate or not before exiting. */
375 if (flag_handle_exceptions == 2
376 && lookup_name (in_charge_identifier, 0) == NULL_TREE)
378 tree in_charge_node = pushdecl (build_decl (VAR_DECL, in_charge_identifier,
380 store_init_value (in_charge_node, build (EQ_EXPR, integer_type_node,
383 expand_decl (in_charge_node);
384 expand_decl_init (in_charge_node);
387 start = ! TYPE_USES_VIRTUAL_BASECLASSES (t);
388 for (pass = start; pass < 2; pass++)
390 tree vbase_init_list = NULL_TREE;
392 for (init_list = current_base_init_list; init_list;
393 init_list = TREE_CHAIN (init_list))
395 tree basename = TREE_PURPOSE (init_list);
397 tree init = TREE_VALUE (init_list);
399 if (basename == NULL_TREE)
401 /* Initializer for single base class. Must not
402 use multiple inheritance or this is ambiguous. */
403 switch (n_baseclasses)
406 error ("type `%s' does not have a base class to initialize",
407 IDENTIFIER_POINTER (current_class_name));
412 error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name));
415 binfo = TREE_VEC_ELT (binfos, 0);
417 else if (is_aggr_typedef (basename, 1))
419 binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t);
420 if (binfo == NULL_TREE)
423 /* Virtual base classes are special cases. Their initializers
424 are recorded with this constructor, and they are used when
425 this constructor is the top-level constructor called. */
426 if (! TREE_VIA_VIRTUAL (binfo))
428 /* Otherwise, if it is not an immediate base class, complain. */
429 for (i = n_baseclasses-1; i >= 0; i--)
430 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
434 error ("type `%s' is not an immediate base class of type `%s'",
435 IDENTIFIER_POINTER (basename),
436 IDENTIFIER_POINTER (current_class_name));
444 /* The base initialization list goes up to the first
445 base class which can actually use it. */
449 char *msgp = (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
450 ? "cannot pass initialization up to class `%s'" : 0;
452 while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo))
453 && BINFO_BASETYPES (binfo) != NULL_TREE
454 && TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 1)
456 /* ?? This should be fixed in RENO by forcing
457 default constructors to exist. */
458 SET_BINFO_BASEINIT_MARKED (binfo);
459 binfo = BINFO_BASETYPE (binfo, 0);
462 /* We used to give an error if this wasn't true, saying that
463 there's no constructor for the initialization of basename.
464 This turned out to be incorrect---it should use the
465 default constructor, since a user could try to initialize
466 the class in a derived class's base initializer list. */
467 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
472 error_with_aggr_type (binfo, msgp);
478 if (BINFO_BASEINIT_MARKED (binfo))
480 msgp = "class `%s' initializer already specified";
481 error (msgp, IDENTIFIER_POINTER (basename));
487 SET_BINFO_BASEINIT_MARKED (binfo);
488 if (TREE_VIA_VIRTUAL (binfo))
490 vbase_init_list = tree_cons (init, BINFO_TYPE (binfo),
497 else if (TREE_VIA_VIRTUAL (binfo))
500 member = convert_pointer_to (binfo, current_class_decl);
501 expand_aggr_init_1 (t_binfo, 0,
502 build_indirect_ref (member, NULL_PTR), init,
503 BINFO_OFFSET_ZEROP (binfo), LOOKUP_COMPLAIN);
504 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
506 cplus_expand_start_try (1);
507 push_exception_cleanup (member);
513 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
516 if (DECL_NAME (current_function_decl) == NULL_TREE
517 && TREE_CHAIN (first_arg) != NULL_TREE)
519 /* If there are virtual baseclasses without initialization
520 specified, and this is a default X(X&) constructor,
521 build the initialization list so that each virtual baseclass
522 of the new object is initialized from the virtual baseclass
523 of the incoming arg. */
524 tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0);
525 for (vbases = CLASSTYPE_VBASECLASSES (t);
526 vbases; vbases = TREE_CHAIN (vbases))
528 if (BINFO_BASEINIT_MARKED (vbases) == 0)
530 member = convert_pointer_to (vbases, init_arg);
531 if (member == init_arg)
532 member = TREE_CHAIN (first_arg);
534 TREE_TYPE (member) = build_reference_type (BINFO_TYPE (vbases));
535 vbase_init_list = tree_cons (convert_from_reference (member),
536 vbases, vbase_init_list);
537 SET_BINFO_BASEINIT_MARKED (vbases);
541 expand_start_cond (first_arg, 0);
542 expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl,
547 current_base_init_list = NULL_TREE;
549 /* Now, perform default initialization of all base classes which
550 have not yet been initialized, and unmark baseclasses which
551 have been initialized. */
552 for (i = 0; i < n_baseclasses; i++)
554 tree base = current_class_decl;
555 tree base_binfo = TREE_VEC_ELT (binfos, i);
557 if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
559 if (! TREE_VIA_VIRTUAL (base_binfo)
560 && ! BINFO_BASEINIT_MARKED (base_binfo))
564 if (BINFO_OFFSET_ZEROP (base_binfo))
565 base = build1 (NOP_EXPR,
566 TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
569 base = build (PLUS_EXPR,
570 TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
571 current_class_decl, BINFO_OFFSET (base_binfo));
573 ref = build_indirect_ref (base, NULL_PTR);
574 expand_aggr_init_1 (t_binfo, 0, ref, NULL_TREE,
575 BINFO_OFFSET_ZEROP (base_binfo),
577 if (flag_handle_exceptions == 2
578 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
580 cplus_expand_start_try (1);
581 push_exception_cleanup (base);
585 CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
587 if (! TYPE_USES_VIRTUAL_BASECLASSES (t))
589 while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo))
590 && BINFO_BASETYPES (base_binfo) != NULL_TREE
591 && TREE_VEC_LENGTH (BINFO_BASETYPES (base_binfo)) == 1)
593 /* ?? This should be fixed in RENO by forcing
594 default constructors to exist. It is needed for symmetry
596 base_binfo = BINFO_BASETYPE (base_binfo, 0);
597 CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
602 /* Initialize all the virtual function table fields that
603 do come from virtual base classes. */
604 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
605 expand_vbase_vtables_init (t_binfo, t_binfo,
606 C_C_D, current_class_decl, 0);
607 for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
608 CLEAR_BINFO_BASEINIT_MARKED (vbases);
610 /* Initialize all the virtual function table fields that
611 do not come from virtual base classes. */
612 init_vtbl_ptrs (t_binfo, 0, 1);
614 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (t))
615 expand_virtual_init (TYPE_BINFO (t), t, current_class_decl);
617 if (current_member_init_list)
619 init_list = sort_member_init (t);
623 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
628 /* See if we had a user-specified member initialization. */
631 if (TREE_PURPOSE (init_list))
633 name = TREE_PURPOSE (init_list);
634 init = TREE_VALUE (init_list);
637 if (TREE_STATIC (member))
639 error_with_aggr_type (DECL_FIELD_CONTEXT (member),
640 "field `%s::%s' is static; only point of initialization is its declaration",
641 IDENTIFIER_POINTER (TREE_PURPOSE (init_list)));
645 /* Also see if it's ever a COMPONENT_REF here. If it is, we
646 need to do `expand_assignment (name, init, 0, 0);' and
648 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
651 init_list = TREE_CHAIN (init_list);
654 if (! from_init_list)
656 /* member could be, for example, a CONST_DECL for an enumerated
657 tag; we don't want to try to initialize that, since it already
659 if (TREE_CODE (member) != FIELD_DECL)
662 name = DECL_NAME (member);
663 init = DECL_INITIAL (member);
666 perform_member_init (member, name, init, from_init_list);
669 current_member_init_list = NULL_TREE;
671 /* It is possible for the initializers to need cleanups.
672 Expand those cleanups now that all the initialization
674 expand_cleanups_to (NULL_TREE);
678 extern rtx base_init_insns;
680 do_pending_stack_adjust ();
681 my_friendly_assert (base_init_insns == 0, 207);
682 base_init_insns = get_insns ();
686 /* All the implicit try blocks we built up will be zapped
687 when we come to a real binding contour boundary. */
690 /* Check that all fields are properly initialized after
691 an assignment to `this'. */
697 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
698 if (DECL_NAME (member) && TREE_USED (member))
699 cp_error ("field `%D' used before initialized (after assignment to `this')",
703 /* This code sets up the virtual function tables appropriate for
704 the pointer DECL. It is a one-ply initialization.
706 BINFO is the exact type that DECL is supposed to be. In
707 multiple inheritance, this might mean "C's A" if C : A, B. */
709 expand_virtual_init (main_binfo, binfo, decl)
710 tree main_binfo, binfo;
715 tree vtype, vtype_binfo;
717 if (TREE_CODE (binfo) == TREE_VEC)
718 type = BINFO_TYPE (binfo);
719 else if (TREE_CODE (binfo) == RECORD_TYPE)
722 binfo = TYPE_BINFO (type);
725 my_friendly_abort (46);
727 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
728 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
730 /* This code suggests that it's time to rewrite how we handle
731 replicated baseclasses in G++. */
732 if (get_base_distance (vtype, TREE_TYPE (TREE_TYPE (decl)),
733 0, (tree *) 0) == -2)
735 tree binfos = TYPE_BINFO_BASETYPES (TREE_TYPE (TREE_TYPE (decl)));
736 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
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 expand_virtual_init (main_binfo, base_binfo, this_decl);
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)
784 /* Have to convert VTBL since array sizes may be different. */
785 expand_expr_stmt (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_vbase_vtables_init (binfo, binfo, exp, addr, 1);
1309 expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
1313 rval = build_method_call (exp, constructor_name_full (type), parms,
1317 /* Private, protected, or otherwise unavailable. */
1318 if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN))
1319 cp_error ("in base initialization for class `%T'", binfo);
1320 /* A valid initialization using constructor. */
1321 else if (rval != error_mark_node && rval != NULL_TREE)
1323 /* p. 222: if the base class assigns to `this', then that
1324 value is used in the derived class. */
1325 if ((flag_this_is_variable & 1) && alias_this)
1327 TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1328 expand_assignment (current_class_decl, rval, 0, 0);
1331 expand_expr_stmt (rval);
1333 else if (parms && TREE_CHAIN (parms) == NULL_TREE)
1335 /* If we are initializing one aggregate value
1336 from another, and though there are constructors,
1337 and none accept the initializer, just do a bitwise
1340 The above sounds wrong, ``If a class has any copy
1341 constructor defined, the default copy constructor will
1342 not be generated.'' 12.8 Copying Class Objects (mrs)
1344 @@ This should reject initializer which a constructor
1345 @@ rejected on access gounds, but there is
1346 @@ no way right now to recognize that case with
1347 @@ just `error_mark_node'. */
1349 init = TREE_VALUE (parms);
1350 itype = TREE_TYPE (init);
1351 if (TREE_CODE (itype) == REFERENCE_TYPE)
1353 init = convert_from_reference (init);
1354 itype = TREE_TYPE (init);
1356 itype = TYPE_MAIN_VARIANT (itype);
1358 /* This is currently how the default X(X&) constructor
1360 if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0))
1363 warning ("bitwise copy in initialization of type `%s'",
1364 TYPE_NAME_STRING (type));
1366 rval = build (INIT_EXPR, type, exp, init);
1367 expand_expr_stmt (rval);
1371 cp_error ("in base initialization for class `%T',", binfo);
1372 cp_error ("invalid initializer to constructor for type `%T'", type);
1378 if (init == NULL_TREE)
1379 my_friendly_assert (parms == NULL_TREE, 210);
1380 if (parms == NULL_TREE && TREE_VIA_VIRTUAL (binfo))
1381 cp_error ("virtual baseclass `%T' does not have default initializer", binfo);
1384 cp_error ("in base initialization for class `%T',", binfo);
1385 /* This will make an error message for us. */
1386 build_method_call (exp, constructor_name_full (type), parms, binfo,
1387 (TYPE_USES_VIRTUAL_BASECLASSES (type)
1388 ? LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE
1393 /* Constructor has been called, but vtables may be for TYPE
1394 rather than for FOR_TYPE. */
1397 /* This function is responsible for initializing EXP with INIT
1400 BINFO is the binfo of the type for who we are performing the
1401 initialization. For example, if W is a virtual base class of A and B,
1403 If we are initializing B, then W must contain B's W vtable, whereas
1404 were we initializing C, W must contain C's W vtable.
1406 TRUE_EXP is nonzero if it is the true expression being initialized.
1407 In this case, it may be EXP, or may just contain EXP. The reason we
1408 need this is because if EXP is a base element of TRUE_EXP, we
1409 don't necessarily know by looking at EXP where its virtual
1410 baseclass fields should really be pointing. But we do know
1411 from TRUE_EXP. In constructors, we don't know anything about
1412 the value being initialized.
1414 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1416 FLAGS is just passes to `build_method_call'. See that function for
1420 expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1427 tree type = TREE_TYPE (exp);
1428 tree init_type = NULL_TREE;
1430 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1432 /* Use a function returning the desired type to initialize EXP for us.
1433 If the function is a constructor, and its first argument is
1434 NULL_TREE, know that it was meant for us--just slide exp on
1435 in and expand the constructor. Constructors now come
1439 tree init_list = NULL_TREE;
1441 if (TREE_CODE (init) == TREE_LIST)
1444 if (TREE_CHAIN (init) == NULL_TREE)
1445 init = TREE_VALUE (init);
1448 init_type = TREE_TYPE (init);
1450 if (TREE_CODE (init) != TREE_LIST)
1452 if (TREE_CODE (init_type) == ERROR_MARK)
1456 /* These lines are found troublesome 5/11/89. */
1457 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1458 init_type = TREE_TYPE (init_type);
1461 /* This happens when we use C++'s functional cast notation.
1462 If the types match, then just use the TARGET_EXPR
1463 directly. Otherwise, we need to create the initializer
1464 separately from the object being initialized. */
1465 if (TREE_CODE (init) == TARGET_EXPR)
1467 if (init_type == type)
1469 if (TREE_CODE (exp) == VAR_DECL
1470 || TREE_CODE (exp) == RESULT_DECL)
1471 /* Unify the initialization targets. */
1472 DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1474 DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
1476 expand_expr_stmt (init);
1481 init = TREE_OPERAND (init, 1);
1482 init = build (CALL_EXPR, init_type,
1483 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1484 TREE_SIDE_EFFECTS (init) = 1;
1486 TREE_RAISES (init) = ??
1489 TREE_VALUE (init_list) = init;
1493 if (init_type == type && TREE_CODE (init) == CALL_EXPR
1495 /* It is legal to directly initialize from a CALL_EXPR
1496 without going through X(X&), apparently. */
1497 && ! TYPE_GETS_INIT_REF (type)
1501 /* A CALL_EXPR is a legitimate form of initialization, so
1502 we should not print this warning message. */
1504 /* Should have gone away due to 5/11/89 change. */
1505 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
1506 init = convert_from_reference (init);
1508 expand_assignment (exp, init, 0, 0);
1509 if (exp == DECL_RESULT (current_function_decl))
1511 /* Failing this assertion means that the return value
1512 from receives multiple initializations. */
1513 my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
1514 || DECL_INITIAL (exp) == error_mark_node,
1516 DECL_INITIAL (exp) = init;
1520 else if (init_type == type
1521 && TREE_CODE (init) == COND_EXPR)
1523 /* Push value to be initialized into the cond, where possible.
1524 Avoid spurious warning messages when initializing the
1525 result of this function. */
1526 TREE_OPERAND (init, 1)
1527 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1528 if (exp == DECL_RESULT (current_function_decl))
1529 DECL_INITIAL (exp) = NULL_TREE;
1530 TREE_OPERAND (init, 2)
1531 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1532 if (exp == DECL_RESULT (current_function_decl))
1533 DECL_INITIAL (exp) = init;
1534 TREE_SIDE_EFFECTS (init) = 1;
1535 expand_expr (init, const0_rtx, VOIDmode, 0);
1541 /* We did not know what we were initializing before. Now we do. */
1542 if (TREE_CODE (init) == TARGET_EXPR)
1544 tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1546 if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1547 && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1549 /* In order for this to work for RESULT_DECLs, if their
1550 type has a constructor, then they must be BLKmode
1551 so that they will be meaningfully addressable. */
1552 tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1553 init = TREE_OPERAND (init, 1);
1554 init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1555 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1556 TREE_SIDE_EFFECTS (init) = 1;
1558 TREE_RAISES (init) = ??
1560 TREE_VALUE (TREE_OPERAND (init, 1))
1561 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1565 expand_assignment (current_function_decl, init, 0, 0);
1568 if (exp == DECL_RESULT (current_function_decl))
1570 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1571 fatal ("return value from function receives multiple initializations");
1572 DECL_INITIAL (exp) = init;
1574 expand_expr_stmt (init);
1579 if (TREE_CODE (exp) == VAR_DECL
1580 && TREE_CODE (init) == CONSTRUCTOR
1581 && TREE_HAS_CONSTRUCTOR (init))
1583 tree t = store_init_value (exp, init);
1586 expand_decl_init (exp);
1589 t = build (INIT_EXPR, type, exp, init);
1590 TREE_SIDE_EFFECTS (t) = 1;
1591 expand_expr_stmt (t);
1595 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1596 which really means: xyzzy foo = bar; Ugh!
1598 More useful for this case: xyzzy *foo = new xyzzy (bar); */
1600 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1602 if (init_list && TREE_CHAIN (init_list))
1604 warning ("initializer list being treated as compound expression");
1605 init = convert (type, build_compound_expr (init_list));
1606 if (init == error_mark_node)
1610 expand_assignment (exp, init, 0, 0);
1614 /* See whether we can go through a type conversion operator.
1615 This wins over going through a non-existent constructor. If
1616 there is a constructor, it is ambiguous. */
1617 if (TREE_CODE (init) != TREE_LIST)
1619 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1620 ? TREE_TYPE (init_type) : init_type;
1622 if (ttype != type && IS_AGGR_TYPE (ttype))
1624 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1628 /* See if there is a constructor for``type'' that takes a
1629 ``ttype''-typed object. */
1630 tree parms = build_tree_list (NULL_TREE, init);
1631 tree as_cons = NULL_TREE;
1632 if (TYPE_HAS_CONSTRUCTOR (type))
1633 as_cons = build_method_call (exp, constructor_name_full (type),
1635 LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1636 if (as_cons != NULL_TREE && as_cons != error_mark_node)
1637 /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1638 cp_error ("ambiguity between conversion to `%T' and constructor",
1641 expand_assignment (exp, rval, 0, 0);
1648 /* Handle default copy constructors here, does not matter if there is
1649 a constructor or not. */
1650 if (type == init_type && IS_AGGR_TYPE (type)
1651 && init && TREE_CODE (init) != TREE_LIST)
1652 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1653 /* Not sure why this is here... */
1654 else if (TYPE_HAS_CONSTRUCTOR (type))
1655 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1656 else if (TREE_CODE (type) == ARRAY_TYPE)
1658 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1659 expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1660 else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1661 sorry ("arrays of objects with virtual functions but no constructors");
1664 expand_recursive_init (binfo, true_exp, exp, init,
1665 CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1668 /* A pointer which holds the initializer. First call to
1669 expand_aggr_init gets this value pointed to, and sets it to init_null. */
1670 static tree *init_ptr, init_null;
1672 /* Subroutine of expand_recursive_init:
1674 ADDR is the address of the expression being initialized.
1675 INIT_LIST is the cons-list of initializations to be performed.
1676 ALIAS_THIS is its same, lovable self. */
1678 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
1679 tree binfo, true_exp, addr;
1685 if (TREE_PURPOSE (init_list))
1687 if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1689 tree member = TREE_PURPOSE (init_list);
1690 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1691 tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1692 if (IS_AGGR_TYPE (TREE_TYPE (member)))
1693 expand_aggr_init (member_base, DECL_INITIAL (member), 0);
1694 else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1695 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1697 member_base = save_expr (default_conversion (member_base));
1698 expand_vec_init (member, member_base,
1699 array_type_nelts (TREE_TYPE (member)),
1700 DECL_INITIAL (member), 0);
1703 expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1705 else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1707 expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1708 expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
1710 else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1712 /* Only initialize the virtual function tables if we
1713 are initializing the ultimate users of those vtables. */
1714 if (TREE_VALUE (init_list))
1716 /* We have to ensure that the second argment to
1717 expand_virtual_init is in binfo's hierarchy. */
1718 expand_virtual_init (binfo,
1719 get_binfo (TREE_VALUE (init_list), binfo, 0),
1721 if (TREE_VALUE (init_list) == binfo
1722 && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1723 expand_vbase_vtables_init (binfo,binfo, true_exp, addr, 1);
1727 my_friendly_abort (49);
1729 else if (TREE_VALUE (init_list)
1730 && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
1732 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1733 expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
1734 alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
1737 /* INIT_PTR is used up. */
1738 init_ptr = &init_null;
1741 my_friendly_abort (50);
1742 init_list = TREE_CHAIN (init_list);
1746 /* Initialize EXP with INIT. Type EXP does not have a constructor,
1747 but it has a baseclass with a constructor or a virtual function
1748 table which needs initializing.
1750 INIT_LIST is a cons-list describing what parts of EXP actually
1751 need to be initialized. INIT is given to the *unique*, first
1752 constructor within INIT_LIST. If there are multiple first
1753 constructors, such as with multiple inheritance, INIT must
1754 be zero or an ambiguity error is reported.
1756 ALIAS_THIS is passed from `expand_aggr_init'. See comments
1760 expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
1761 tree binfo, true_exp, exp, init;
1765 tree *old_init_ptr = init_ptr;
1766 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1769 if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1771 expand_aggr_vbase_init (binfo, exp, addr, init_list);
1772 expand_vbase_vtables_init (binfo, binfo, true_exp, addr, 1);
1774 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
1778 tree type = TREE_TYPE (exp);
1780 if (TREE_CODE (type) == REFERENCE_TYPE)
1781 type = TREE_TYPE (type);
1782 if (IS_AGGR_TYPE (type))
1783 cp_error ("unexpected argument to constructor `%T'", type);
1785 error ("unexpected argument to constructor");
1787 init_ptr = old_init_ptr;
1790 /* Report an error if NAME is not the name of a user-defined,
1791 aggregate type. If OR_ELSE is nonzero, give an error message. */
1793 is_aggr_typedef (name, or_else)
1799 if (name == error_mark_node)
1802 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1803 type = IDENTIFIER_TYPE_VALUE (name);
1804 else if (IDENTIFIER_HAS_CLASS_TYPE_VALUE (name))
1805 type = IDENTIFIER_CLASS_TYPE_VALUE (name);
1809 cp_error ("`%T' fails to be an aggregate typedef", name);
1813 if (! IS_AGGR_TYPE (type)
1814 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1817 cp_error ("type `%T' is of non-aggregate type", type);
1823 /* Like is_aggr_typedef, but returns typedef if successful. */
1825 get_aggr_from_typedef (name, or_else)
1831 if (name == error_mark_node)
1834 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1835 type = IDENTIFIER_TYPE_VALUE (name);
1836 else if (IDENTIFIER_HAS_CLASS_TYPE_VALUE (name))
1837 type = IDENTIFIER_CLASS_TYPE_VALUE (name);
1841 cp_error ("`%T' fails to be an aggregate typedef", name);
1845 if (! IS_AGGR_TYPE (type)
1846 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1849 cp_error ("type `%T' is of non-aggregate type", type);
1856 get_type_value (name)
1859 if (name == error_mark_node)
1862 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1863 return IDENTIFIER_TYPE_VALUE (name);
1864 else if (IDENTIFIER_CLASS_VALUE (name))
1865 return IDENTIFIER_CLASS_TYPE_VALUE (name);
1871 /* This code could just as well go in `class.c', but is placed here for
1874 /* For an expression of the form CNAME :: NAME (PARMLIST), build
1875 the appropriate function call. */
1877 build_member_call (cname, name, parmlist)
1878 tree cname, name, parmlist;
1881 tree method_name = name;
1883 int dont_use_this = 0;
1884 tree basetype_path, decl;
1886 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1888 method_name = TREE_OPERAND (method_name, 0);
1892 if (TREE_CODE (cname) == SCOPE_REF)
1893 cname = resolve_scope_to_name (NULL_TREE, cname);
1895 if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1)))
1896 return error_mark_node;
1898 /* An operator we did not like. */
1899 if (name == NULL_TREE)
1900 return error_mark_node;
1905 /* Everything can explicitly call a destructor; see 12.4 */
1906 if (! TYPE_HAS_DESTRUCTOR (type))
1907 cp_error ("type `%#T' does not have a destructor", type);
1910 cp_error ("cannot call destructor `%T::~%T' without object", type,
1912 return error_mark_node;
1915 /* No object? Then just fake one up, and let build_method_call
1916 figure out what to do. */
1917 if (current_class_type == 0
1918 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1923 basetype_path = NULL_TREE;
1924 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1926 else if (current_class_decl == 0)
1929 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1933 tree olddecl = current_class_decl;
1934 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1935 if (oldtype != type)
1937 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1938 TYPE_VOLATILE (oldtype));
1939 decl = convert_force (TYPE_POINTER_TO (newtype), olddecl);
1945 decl = build_indirect_ref (decl, NULL_PTR);
1947 if (t = lookup_fnfields (TYPE_BINFO (type), method_name, 0))
1948 return build_method_call (decl, method_name, parmlist, basetype_path,
1949 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1950 if (TREE_CODE (name) == IDENTIFIER_NODE
1951 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1953 if (t == error_mark_node)
1954 return error_mark_node;
1955 if (TREE_CODE (t) == FIELD_DECL)
1959 cp_error ("invalid use of non-static field `%D'", t);
1960 return error_mark_node;
1962 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1964 else if (TREE_CODE (t) == VAR_DECL)
1968 cp_error ("invalid use of member `%D'", t);
1969 return error_mark_node;
1971 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1972 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1973 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1974 return build_function_call (decl, parmlist);
1978 cp_error ("no method `%T::%D'", type, name);
1979 return error_mark_node;
1983 /* Build a reference to a member of an aggregate. This is not a
1984 C++ `&', but really something which can have its address taken,
1985 and then act as a pointer to member, for example CNAME :: FIELD
1986 can have its address taken by saying & CNAME :: FIELD.
1988 @@ Prints out lousy diagnostics for operator <typename>
1991 @@ This function should be rewritten and placed in search.c. */
1993 build_offset_ref (cname, name)
1996 tree decl, type, fnfields, fields, t = error_mark_node;
1997 tree basetypes = NULL_TREE;
2000 if (TREE_CODE (cname) == SCOPE_REF)
2001 cname = resolve_scope_to_name (NULL_TREE, cname);
2003 if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
2004 return error_mark_node;
2006 type = IDENTIFIER_TYPE_VALUE (cname);
2008 if (TREE_CODE (name) == BIT_NOT_EXPR)
2011 name = TREE_OPERAND (name, 0);
2014 if (TYPE_SIZE (type) == 0)
2016 t = IDENTIFIER_CLASS_VALUE (name);
2019 cp_error ("incomplete type `%T' does not have member `%D'", type,
2021 return error_mark_node;
2023 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
2024 || TREE_CODE (t) == CONST_DECL)
2029 if (TREE_CODE (t) == FIELD_DECL)
2030 sorry ("use of member in incomplete aggregate type");
2031 else if (TREE_CODE (t) == FUNCTION_DECL)
2032 sorry ("use of member function in incomplete aggregate type");
2034 my_friendly_abort (52);
2035 return error_mark_node;
2038 if (TREE_CODE (name) == TYPE_EXPR)
2039 /* Pass a TYPE_DECL to build_component_type_expr. */
2040 return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
2041 name, NULL_TREE, 1);
2043 fnfields = lookup_fnfields (TYPE_BINFO (type), name, 1);
2044 fields = lookup_field (type, name, 0, 0);
2046 if (fields == error_mark_node || fnfields == error_mark_node)
2047 return error_mark_node;
2049 if (current_class_type == 0
2050 || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
2052 basetypes = TYPE_BINFO (type);
2053 decl = build1 (NOP_EXPR,
2054 IDENTIFIER_TYPE_VALUE (cname),
2057 else if (current_class_decl == 0)
2058 decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
2063 /* A lot of this logic is now handled in lookup_field and
2067 basetypes = TREE_PURPOSE (fnfields);
2069 /* Go from the TREE_BASELINK to the member function info. */
2070 t = TREE_VALUE (fnfields);
2074 if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
2076 error ("ambiguous member reference: member `%s' defined as both field and function",
2077 IDENTIFIER_POINTER (name));
2078 return error_mark_node;
2080 if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
2082 else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
2086 error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
2087 return error_mark_node;
2091 if (t == TREE_VALUE (fnfields))
2093 extern int flag_save_memoized_contexts;
2095 /* This does not handle access checking yet. */
2096 if (DECL_CHAIN (t) == NULL_TREE || dtor)
2098 enum access_type access;
2100 /* unique functions are handled easily. */
2102 access = compute_access (basetypes, t);
2103 if (access == access_protected)
2105 cp_error_at ("member function `%#D' is protected", t);
2106 error ("in this context");
2107 return error_mark_node;
2109 if (access == access_private)
2111 cp_error_at ("member function `%#D' is private", t);
2112 error ("in this context");
2113 return error_mark_node;
2115 assemble_external (t);
2116 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
2119 /* overloaded functions may need more work. */
2122 if (TYPE_HAS_DESTRUCTOR (type)
2123 && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
2129 /* FNFIELDS is most likely allocated on the search_obstack,
2130 which will go away after this class scope. If we need
2131 to save this value for later (either for memoization
2132 or for use as an initializer for a static variable), then
2135 ??? The smart thing to do for the case of saving initializers
2136 is to resolve them before we're done with this scope. */
2137 if (!TREE_PERMANENT (fnfields)
2138 && ((flag_save_memoized_contexts && global_bindings_p ())
2139 || ! allocation_temporary_p ()))
2140 fnfields = copy_list (fnfields);
2141 t = build_tree_list (error_mark_node, fnfields);
2142 TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
2147 /* Now that we know we are looking for a field, see if we
2148 have access to that field. Lookup_field will give us the
2151 t = lookup_field (basetypes, name, 1, 0);
2153 if (t == error_mark_node)
2154 return error_mark_node;
2158 cp_error ("`%D' is not a member of type `%T'", name,
2159 IDENTIFIER_TYPE_VALUE (cname));
2160 return error_mark_node;
2163 if (TREE_CODE (t) == TYPE_DECL)
2168 /* static class members and class-specific enum
2169 values can be returned without further ado. */
2170 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
2172 assemble_external (t);
2177 /* static class functions too. */
2178 if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2179 my_friendly_abort (53);
2181 /* In member functions, the form `cname::name' is no longer
2182 equivalent to `this->cname::name'. */
2183 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2186 /* Given an object EXP and a member function reference MEMBER,
2187 return the address of the actual member function. */
2189 get_member_function (exp_addr_ptr, exp, member)
2193 tree ctype = TREE_TYPE (exp);
2194 tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
2196 if (TYPE_VIRTUAL_P (ctype)
2197 || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
2202 /* Save away the unadulterated `this' pointer. */
2203 exp_addr = save_expr (*exp_addr_ptr);
2205 /* Cast function to signed integer. */
2206 e0 = build1 (NOP_EXPR, integer_type_node, function);
2208 #ifdef VTABLE_USES_MASK
2209 /* If we are willing to limit the number of
2210 virtual functions a class may have to some
2211 *small* number, then if, for a function address,
2212 we are passed some small number, we know that
2213 it is a virtual function index, and work from there. */
2214 e1 = build (BIT_AND_EXPR, integer_type_node, e0, vtbl_mask);
2216 /* There is a hack here that takes advantage of
2217 twos complement arithmetic, and the fact that
2218 there are more than one UNITS to the WORD.
2219 If the high bit is set for the `function',
2220 then we pretend it is a virtual function,
2221 and the array indexing will knock this bit
2222 out the top, leaving a valid index. */
2223 if (UNITS_PER_WORD <= 1)
2224 my_friendly_abort (54);
2226 e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
2227 e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
2228 build_tree_list (NULL_TREE, e1)));
2229 e1 = save_expr (e1);
2232 if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
2234 exp = build_indirect_ref (exp_addr, NULL_PTR);
2235 *exp_addr_ptr = exp_addr;
2238 /* This is really hairy: if the function pointer is a pointer
2239 to a non-virtual member function, then we can't go mucking
2240 with the `this' pointer (any more than we already have to
2241 this point). If it is a pointer to a virtual member function,
2242 then we have to adjust the `this' pointer according to
2243 what the virtual function table tells us. */
2245 e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
2246 my_friendly_assert (e3 != error_mark_node, 213);
2248 /* Change this pointer type from `void *' to the
2249 type it is really supposed to be. */
2250 TREE_TYPE (e3) = TREE_TYPE (function);
2252 /* If non-virtual, use what we had originally. Otherwise,
2253 use the value we get from the virtual function table. */
2254 *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
2256 function = build_conditional_expr (e1, function, e3);
2258 return build_indirect_ref (function, NULL_PTR);
2261 /* If a OFFSET_REF made it through to here, then it did
2262 not have its address taken. */
2265 resolve_offset_ref (exp)
2268 tree type = TREE_TYPE (exp);
2269 tree base = NULL_TREE;
2271 tree basetype, addr;
2273 if (TREE_CODE (exp) == TREE_LIST)
2274 return build_unary_op (ADDR_EXPR, exp, 0);
2276 if (TREE_CODE (exp) != OFFSET_REF)
2278 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2279 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2281 error ("object missing in use of pointer-to-member construct");
2282 return error_mark_node;
2285 type = TREE_TYPE (type);
2290 member = TREE_OPERAND (exp, 1);
2291 base = TREE_OPERAND (exp, 0);
2294 if ((TREE_CODE (member) == VAR_DECL
2295 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2296 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
2298 /* These were static members. */
2299 if (mark_addressable (member) == 0)
2300 return error_mark_node;
2304 /* Syntax error can cause a member which should
2305 have been seen as static to be grok'd as non-static. */
2306 if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
2308 if (TREE_ADDRESSABLE (member) == 0)
2310 cp_error_at ("member `%D' is non-static in static member function context", member);
2311 error ("at this point in file");
2312 TREE_ADDRESSABLE (member) = 1;
2314 return error_mark_node;
2317 /* The first case is really just a reference to a member of `this'. */
2318 if (TREE_CODE (member) == FIELD_DECL
2320 || (TREE_CODE (base) == NOP_EXPR
2321 && TREE_OPERAND (base, 0) == error_mark_node)))
2324 enum access_type access;
2326 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
2328 basetype = TYPE_OFFSET_BASETYPE (type);
2329 base = convert_pointer_to (basetype, current_class_decl);
2332 base = current_class_decl;
2333 basetype = DECL_CONTEXT (member);
2335 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2337 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2338 return error_mark_node;
2340 addr = convert_pointer_to (basetype, base);
2341 access = compute_access (basetype_path, member);
2342 if (access == access_public)
2343 return build (COMPONENT_REF, TREE_TYPE (member),
2344 build_indirect_ref (addr, NULL_PTR), member);
2345 if (access == access_protected)
2347 cp_error_at ("member `%D' is protected", member);
2348 error ("in this context");
2349 return error_mark_node;
2351 if (access == access_private)
2353 cp_error_at ("member `%D' is private", member);
2354 error ("in this context");
2355 return error_mark_node;
2357 my_friendly_abort (55);
2360 /* If this is a reference to a member function, then return
2361 the address of the member function (which may involve going
2362 through the object's vtable), otherwise, return an expression
2363 for the dereferenced pointer-to-member construct. */
2364 addr = build_unary_op (ADDR_EXPR, base, 0);
2366 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2368 basetype = DECL_CLASS_CONTEXT (member);
2369 addr = convert_pointer_to (basetype, addr);
2370 return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0);
2372 else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2374 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2375 addr = convert_pointer_to (basetype, addr);
2376 member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0));
2377 return build1 (INDIRECT_REF, type,
2378 build (PLUS_EXPR, ptr_type_node, addr, member));
2380 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2382 return get_member_function_from_ptrfunc (&addr, base, member);
2384 my_friendly_abort (56);
2389 /* Return either DECL or its known constant value (if it has one). */
2392 decl_constant_value (decl)
2395 if (! TREE_THIS_VOLATILE (decl)
2397 /* These may be necessary for C, but they break C++. */
2398 ! TREE_PUBLIC (decl)
2399 /* Don't change a variable array bound or initial value to a constant
2400 in a place where a variable is invalid. */
2403 && DECL_INITIAL (decl) != 0
2404 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2405 /* This is invalid if initial value is not constant.
2406 If it has either a function call, a memory reference,
2407 or a variable, then re-evaluating it could give different results. */
2408 && TREE_CONSTANT (DECL_INITIAL (decl))
2409 /* Check for cases where this is sub-optimal, even though valid. */
2410 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2412 /* We must allow this to work outside of functions so that
2413 static constants can be used for array sizes. */
2414 && current_function_decl != 0
2415 && DECL_MODE (decl) != BLKmode
2418 return DECL_INITIAL (decl);
2422 /* Friend handling routines. */
2423 /* Friend data structures:
2425 Lists of friend functions come from TYPE_DECL nodes. Since all
2426 aggregate types are automatically typedef'd, these nodes are guaranteed
2429 The TREE_PURPOSE of a friend list is the name of the friend,
2430 and its TREE_VALUE is another list.
2432 For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2433 will be filled in, but not both. The TREE_VALUE of that list is an
2434 individual function which is a friend. The TREE_PURPOSE of that list
2435 indicates a type in which all functions by that name are friends.
2437 Lists of friend classes come from _TYPE nodes. Love that consistency
2441 is_friend_type (type1, type2)
2444 return is_friend (type1, type2);
2448 is_friend (type, supplicant)
2449 tree type, supplicant;
2454 if (supplicant == NULL_TREE || type == NULL_TREE)
2457 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2460 /* It's a function decl. */
2462 tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2463 tree name = DECL_NAME (supplicant);
2464 tree ctype = DECL_CLASS_CONTEXT (supplicant);
2465 for (; list ; list = TREE_CHAIN (list))
2467 if (name == TREE_PURPOSE (list))
2469 tree friends = TREE_VALUE (list);
2470 name = DECL_ASSEMBLER_NAME (supplicant);
2471 for (; friends ; friends = TREE_CHAIN (friends))
2473 if (ctype == TREE_PURPOSE (friends))
2475 if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
2485 if (type == supplicant)
2488 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2489 for (; list ; list = TREE_CHAIN (list))
2490 if (supplicant == TREE_VALUE (list))
2495 tree context = declp ? DECL_CLASS_CONTEXT (supplicant)
2496 : DECL_CONTEXT (TYPE_NAME (supplicant));
2499 return is_friend (type, context);
2505 /* Add a new friend to the friends of the aggregate type TYPE.
2506 DECL is the FUNCTION_DECL of the friend being added. */
2508 add_friend (type, decl)
2511 tree typedecl = TYPE_NAME (type);
2512 tree list = DECL_FRIENDLIST (typedecl);
2513 tree name = DECL_NAME (decl);
2517 if (name == TREE_PURPOSE (list))
2519 tree friends = TREE_VALUE (list);
2520 for (; friends ; friends = TREE_CHAIN (friends))
2522 if (decl == TREE_VALUE (friends))
2524 cp_pedwarn ("`%D' is already a friend of class `%T'",
2526 cp_pedwarn_at ("previous friend declaration of `%D'",
2527 TREE_VALUE (friends));
2531 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2535 list = TREE_CHAIN (list);
2537 DECL_FRIENDLIST (typedecl)
2538 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2539 DECL_FRIENDLIST (typedecl));
2540 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2542 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2543 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2544 if (parmtypes && TREE_CHAIN (parmtypes))
2546 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2547 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2548 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2549 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2554 /* Declare that every member function NAME in FRIEND_TYPE
2555 (which may be NULL_TREE) is a friend of type TYPE. */
2557 add_friends (type, name, friend_type)
2558 tree type, name, friend_type;
2560 tree typedecl = TYPE_NAME (type);
2561 tree list = DECL_FRIENDLIST (typedecl);
2565 if (name == TREE_PURPOSE (list))
2567 tree friends = TREE_VALUE (list);
2568 while (friends && TREE_PURPOSE (friends) != friend_type)
2569 friends = TREE_CHAIN (friends);
2572 warning ("method `%s::%s' is already a friend of class",
2573 TYPE_NAME_STRING (friend_type),
2574 IDENTIFIER_POINTER (name));
2576 warning ("function `%s' is already a friend of class `%s'",
2577 IDENTIFIER_POINTER (name),
2578 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2580 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2584 list = TREE_CHAIN (list);
2586 DECL_FRIENDLIST (typedecl) =
2588 build_tree_list (friend_type, NULL_TREE),
2589 DECL_FRIENDLIST (typedecl));
2590 if (! strncmp (IDENTIFIER_POINTER (name),
2591 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2592 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2594 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2595 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2599 /* Set up a cross reference so that type TYPE will make member function
2600 CTYPE::DECL a friend when CTYPE is finally defined. For more than
2601 one, set up a cross reference so that functions with the name DECL
2602 and type CTYPE know that they are friends of TYPE. */
2604 xref_friend (type, decl, ctype)
2605 tree type, decl, ctype;
2607 tree friend_decl = TYPE_NAME (ctype);
2609 tree typedecl = TYPE_NAME (type);
2610 tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2612 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2616 SET_DECL_WAITING_FRIENDS (friend_decl,
2618 DECL_WAITING_FRIENDS (friend_decl)));
2619 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2622 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2623 been defined, we make all of its member functions friends of
2624 TYPE. If not, we make it a pending friend, which can later be added
2625 when its definition is seen. If a type is defined, then its TYPE_DECL's
2626 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2627 classes that are not defined. If a type has not yet been defined,
2628 then the DECL_WAITING_FRIENDS contains a list of types
2629 waiting to make it their friend. Note that these two can both
2630 be in use at the same time! */
2632 make_friend_class (type, friend_type)
2633 tree type, friend_type;
2637 if (IS_SIGNATURE (type))
2639 error ("`friend' declaration in signature definition");
2642 if (IS_SIGNATURE (friend_type))
2644 error ("signature type `%s' declared `friend'",
2645 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2648 if (type == friend_type)
2650 warning ("class `%s' is implicitly friends with itself",
2651 TYPE_NAME_STRING (type));
2655 GNU_xref_hier (TYPE_NAME_STRING (type),
2656 TYPE_NAME_STRING (friend_type), 0, 0, 1);
2658 classes = CLASSTYPE_FRIEND_CLASSES (type);
2659 while (classes && TREE_VALUE (classes) != friend_type)
2660 classes = TREE_CHAIN (classes);
2662 warning ("class `%s' is already friends with class `%s'",
2663 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2666 CLASSTYPE_FRIEND_CLASSES (type)
2667 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2671 /* Main friend processor. This is large, and for modularity purposes,
2672 has been removed from grokdeclarator. It returns `void_type_node'
2673 to indicate that something happened, though a FIELD_DECL is
2676 CTYPE is the class this friend belongs to.
2678 DECLARATOR is the name of the friend.
2680 DECL is the FUNCTION_DECL that the friend is.
2682 In case we are parsing a friend which is part of an inline
2683 definition, we will need to store PARM_DECL chain that comes
2684 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2686 FLAGS is just used for `grokclassfn'.
2688 QUALS say what special qualifies should apply to the object
2689 pointed to by `this'. */
2691 do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2692 tree ctype, declarator, decl, parmdecls;
2693 enum overload_flags flags;
2696 /* first, lets find out if what we are making a friend needs overloading */
2698 int was_c_linkage = 0;
2700 /* Every decl that gets here is a friend of something. */
2701 DECL_FRIEND_P (decl) = 1;
2703 /* If we find something in scope, let see if it has extern "C" linkage. */
2704 /* This code is pretty general and should be ripped out and reused
2705 as a separate function. */
2706 if (DECL_NAME (decl))
2708 previous_decl = lookup_name (DECL_NAME (decl), 0);
2709 if (previous_decl && TREE_CODE (previous_decl) == TREE_LIST)
2713 if (TREE_TYPE (TREE_VALUE (previous_decl)) == TREE_TYPE (decl))
2715 previous_decl = TREE_VALUE (previous_decl);
2718 previous_decl = TREE_CHAIN (previous_decl);
2720 while (previous_decl);
2723 /* It had extern "C" linkage, so don't overload this. */
2724 if (previous_decl && TREE_CODE (previous_decl) == FUNCTION_DECL
2725 && TREE_TYPE (decl) == TREE_TYPE (previous_decl)
2726 && DECL_LANGUAGE (previous_decl) == lang_c)
2732 tree cname = TYPE_NAME (ctype);
2733 if (TREE_CODE (cname) == TYPE_DECL)
2734 cname = DECL_NAME (cname);
2736 /* A method friend. */
2737 if (TREE_CODE (decl) == FUNCTION_DECL)
2739 if (flags == NO_SPECIAL && ctype && declarator == cname)
2740 DECL_CONSTRUCTOR_P (decl) = 1;
2742 /* This will set up DECL_ARGUMENTS for us. */
2743 grokclassfn (ctype, cname, decl, flags, quals);
2744 if (TYPE_SIZE (ctype) != 0)
2745 check_classfn (ctype, cname, decl);
2747 if (TREE_TYPE (decl) != error_mark_node)
2749 if (TYPE_SIZE (ctype))
2751 /* We don't call pushdecl here yet, or ever on this
2752 actual FUNCTION_DECL. We must preserve its TREE_CHAIN
2754 make_decl_rtl (decl, NULL_PTR, 1);
2755 add_friend (current_class_type, decl);
2759 register char *classname
2760 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype)));
2762 error ("member declared as friend before type `%s' defined",
2769 /* Possibly a bunch of method friends. */
2771 /* Get the class they belong to. */
2772 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2774 /* This class is defined, use its methods now. */
2775 if (TYPE_SIZE (ctype))
2777 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2779 add_friends (current_class_type, declarator, ctype);
2781 error ("method `%s' is not a member of class `%s'",
2782 IDENTIFIER_POINTER (declarator),
2783 IDENTIFIER_POINTER (cname));
2786 /* Note: DECLARATOR actually has more than one; in this
2787 case, we're making sure that fns with the name DECLARATOR
2788 and type CTYPE know they are friends of the current
2790 xref_friend (current_class_type, declarator, ctype);
2791 decl = void_type_node;
2794 /* never overload C functions */
2795 else if (TREE_CODE (decl) == FUNCTION_DECL
2796 && ((IDENTIFIER_LENGTH (declarator) == 4
2797 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2798 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2799 || (IDENTIFIER_LENGTH (declarator) > 10
2800 && IDENTIFIER_POINTER (declarator)[0] == '_'
2801 && IDENTIFIER_POINTER (declarator)[1] == '_'
2802 && strncmp (IDENTIFIER_POINTER (declarator)+2,
2803 "builtin_", 8) == 0)
2806 /* raw "main", and builtin functions never gets overloaded,
2807 but they can become friends. */
2808 TREE_PUBLIC (decl) = 1;
2809 add_friend (current_class_type, decl);
2810 DECL_FRIEND_P (decl) = 1;
2811 if (IDENTIFIER_POINTER (declarator)[0] == '_')
2813 if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "new"))
2814 TREE_GETS_NEW (current_class_type) = 0;
2815 else if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "delete"))
2816 TREE_GETS_DELETE (current_class_type) = 0;
2818 decl = void_type_node;
2821 @@ or possibly a friend from a base class ?!? */
2822 else if (TREE_CODE (decl) == FUNCTION_DECL)
2824 /* Friends must all go through the overload machinery,
2825 even though they may not technically be overloaded.
2827 Note that because classes all wind up being top-level
2828 in their scope, their friend wind up in top-level scope as well. */
2829 DECL_ASSEMBLER_NAME (decl)
2830 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2831 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2832 DECL_ARGUMENTS (decl) = parmdecls;
2833 DECL_CLASS_CONTEXT (decl) = current_class_type;
2835 /* We can call pushdecl here, because the TREE_CHAIN of this
2836 FUNCTION_DECL is not needed for other purposes. */
2837 decl = pushdecl_top_level (decl);
2839 make_decl_rtl (decl, NULL_PTR, 1);
2840 add_friend (current_class_type, decl);
2842 DECL_FRIEND_P (decl) = 1;
2843 TREE_OVERLOADED (declarator) = 1;
2847 /* @@ Should be able to ingest later definitions of this function
2849 tree decl = IDENTIFIER_GLOBAL_VALUE (declarator);
2850 if (decl == NULL_TREE)
2852 warning ("implicitly declaring `%s' as struct",
2853 IDENTIFIER_POINTER (declarator));
2854 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2855 decl = TYPE_NAME (decl);
2858 /* Allow abbreviated declarations of overloaded functions,
2859 but not if those functions are really class names. */
2860 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2862 warning ("`friend %s' archaic, use `friend class %s' instead",
2863 IDENTIFIER_POINTER (declarator),
2864 IDENTIFIER_POINTER (declarator));
2865 decl = TREE_TYPE (TREE_PURPOSE (decl));
2868 if (TREE_CODE (decl) == TREE_LIST)
2869 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2871 make_friend_class (current_class_type, TREE_TYPE (decl));
2872 decl = void_type_node;
2877 /* TYPE has now been defined. It may, however, have a number of things
2878 waiting make make it their friend. We resolve these references
2881 embrace_waiting_friends (type)
2884 tree decl = TYPE_NAME (type);
2887 if (TREE_CODE (decl) != TYPE_DECL)
2890 for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2891 waiters = TREE_CHAIN (waiters))
2893 tree waiter = TREE_PURPOSE (waiters);
2895 tree waiter_prev = TREE_VALUE (waiters);
2897 tree decl = TREE_TYPE (waiters);
2898 tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2899 ? decl : DECL_NAME (decl)) : NULL_TREE;
2902 /* @@ There may be work to be done since we have not verified
2903 @@ consistency between original and friend declarations
2904 @@ of the functions waiting to become friends. */
2905 tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
2908 add_friends (waiter, name, type);
2910 add_friend (waiter, decl);
2912 error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2913 DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2914 "no method `%s' defined in class `%s' to be friend",
2915 IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
2916 TYPE_NAME_STRING (type));
2919 make_friend_class (type, waiter);
2922 if (TREE_CHAIN (waiter_prev))
2923 TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2925 DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2930 /* Common subroutines of build_new and build_vec_delete. */
2932 /* Common interface for calling "builtin" functions that are not
2936 build_builtin_call (type, node, arglist)
2941 tree rval = build (CALL_EXPR, type, node, arglist, 0);
2942 TREE_SIDE_EFFECTS (rval) = 1;
2943 assemble_external (TREE_OPERAND (node, 0));
2944 TREE_USED (TREE_OPERAND (node, 0)) = 1;
2948 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2949 (which needs to go through some sort of groktypename) or it
2950 is the name of the class we are newing. INIT is an initialization value.
2951 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2952 If INIT is void_type_node, it means do *not* call a constructor
2955 For types with constructors, the data returned is initialized
2956 by the appropriate constructor.
2958 Whether the type has a constructor or not, if it has a pointer
2959 to a virtual function table, then that pointer is set up
2962 Unless I am mistaken, a call to new () will return initialized
2963 data regardless of whether the constructor itself is private or
2964 not. NOPE; new fails if the constructor is private (jcm).
2966 Note that build_new does nothing to assure that any special
2967 alignment requirements of the type are met. Rather, it leaves
2968 it up to malloc to do the right thing. Otherwise, folding to
2969 the right alignment cal cause problems if the user tries to later
2970 free the memory returned by `new'.
2972 PLACEMENT is the `placement' list for user-defined operator new (). */
2975 build_new (placement, decl, init, use_global_new)
2980 tree type, true_type, size, rval;
2984 tree pending_sizes = NULL_TREE;
2986 if (decl == error_mark_node)
2987 return error_mark_node;
2989 if (TREE_CODE (decl) == TREE_LIST)
2991 tree absdcl = TREE_VALUE (decl);
2992 tree last_absdcl = NULL_TREE;
2993 int old_immediate_size_expand;
2995 if (current_function_decl
2996 && DECL_CONSTRUCTOR_P (current_function_decl))
2998 old_immediate_size_expand = immediate_size_expand;
2999 immediate_size_expand = 0;
3002 nelts = integer_one_node;
3004 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
3005 my_friendly_abort (215);
3006 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
3008 last_absdcl = absdcl;
3009 absdcl = TREE_OPERAND (absdcl, 0);
3012 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
3014 /* probably meant to be a vec new */
3017 while (TREE_OPERAND (absdcl, 0)
3018 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
3020 last_absdcl = absdcl;
3021 absdcl = TREE_OPERAND (absdcl, 0);
3025 this_nelts = TREE_OPERAND (absdcl, 1);
3026 if (this_nelts != error_mark_node)
3028 if (this_nelts == NULL_TREE)
3029 error ("new of array type fails to specify size");
3032 this_nelts = save_expr (convert (sizetype, this_nelts));
3033 absdcl = TREE_OPERAND (absdcl, 0);
3034 if (this_nelts == integer_zero_node)
3036 warning ("zero size array reserves no space");
3037 nelts = integer_zero_node;
3040 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3044 nelts = integer_zero_node;
3048 TREE_OPERAND (last_absdcl, 0) = absdcl;
3050 TREE_VALUE (decl) = absdcl;
3052 type = true_type = groktypename (decl);
3053 if (! type || type == error_mark_node)
3055 immediate_size_expand = old_immediate_size_expand;
3056 return error_mark_node;
3059 if (current_function_decl
3060 && DECL_CONSTRUCTOR_P (current_function_decl))
3062 pending_sizes = get_pending_sizes ();
3063 immediate_size_expand = old_immediate_size_expand;
3066 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
3068 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
3070 /* An aggregate type. */
3071 type = IDENTIFIER_TYPE_VALUE (decl);
3072 decl = TYPE_NAME (type);
3076 /* A builtin type. */
3077 decl = lookup_name (decl, 1);
3078 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
3079 type = TREE_TYPE (decl);
3083 else if (TREE_CODE (decl) == TYPE_DECL)
3085 type = TREE_TYPE (decl);
3092 decl = TYPE_NAME (type);
3095 /* ``A reference cannot be created by the new operator. A reference
3096 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3097 returned by new.'' ARM 5.3.3 */
3098 if (TREE_CODE (type) == REFERENCE_TYPE)
3100 error ("new cannot be applied to a reference type");
3101 type = true_type = TREE_TYPE (type);
3104 /* When the object being created is an array, the new-expression yields a
3105 pointer to the initial element (if any) of the array. For example,
3106 both new int and new int[10] return an int*. 5.3.4. */
3107 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
3109 nelts = array_type_nelts_top (type);
3111 type = true_type = TREE_TYPE (type);
3114 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
3116 pedwarn ("const and volatile types cannot be created with operator new");
3117 type = true_type = TYPE_MAIN_VARIANT (type);
3120 /* If our base type is an array, then make sure we know how many elements
3122 while (TREE_CODE (true_type) == ARRAY_TYPE)
3124 tree this_nelts = array_type_nelts_top (true_type);
3125 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3126 true_type = TREE_TYPE (true_type);
3129 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
3132 size = size_in_bytes (type);
3134 if (TYPE_SIZE (true_type) == 0)
3136 if (true_type == void_type_node)
3137 error ("invalid type for new: `void'");
3139 incomplete_type_error (0, true_type);
3140 return error_mark_node;
3143 if (TYPE_LANG_SPECIFIC (true_type)
3144 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
3146 abstract_virtuals_error (NULL_TREE, true_type);
3147 return error_mark_node;
3150 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
3152 signature_error (NULL_TREE, true_type);
3153 return error_mark_node;
3156 /* Get a little extra space to store a couple of things before the new'ed
3158 if (has_array && TYPE_NEEDS_DESTRUCTOR (true_type))
3160 tree extra = BI_header_size;
3162 size = size_binop (PLUS_EXPR, size, extra);
3165 /* Allocate the object. */
3166 if (TYPE_LANG_SPECIFIC (true_type)
3167 && (TREE_GETS_NEW (true_type) || TREE_GETS_PLACED_NEW (true_type))
3170 rval = build_opfncall (NEW_EXPR, LOOKUP_NORMAL,
3171 TYPE_POINTER_TO (true_type), size, placement);
3174 rval = build_opfncall (NEW_EXPR, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
3175 ptr_type_node, size, placement);
3176 rval = convert (TYPE_POINTER_TO (true_type), rval);
3178 else if (flag_this_is_variable > 0
3179 && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3181 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
3185 error ("constructors take parameter lists");
3186 return error_mark_node;
3191 rval = build_builtin_call (build_pointer_type (true_type),
3192 BIN, build_tree_list (NULL_TREE, size));
3194 /* See comment above as to why this is disabled. */
3197 rval = build (PLUS_EXPR, TYPE_POINTER_TO (true_type), rval,
3199 rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (true_type),
3200 rval, build1 (BIT_NOT_EXPR, integer_type_node,
3204 TREE_CALLS_NEW (rval) = 1;
3205 TREE_SIDE_EFFECTS (rval) = 1;
3208 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
3209 sure we have some extra bytes in that case for the BI_header_size
3210 cookies? And how does that interact with the code below? (mrs) */
3211 /* Finish up some magic for new'ed arrays */
3212 if (has_array && TYPE_NEEDS_DESTRUCTOR (true_type) && rval != NULL_TREE)
3214 tree extra = BI_header_size;
3216 rval = convert (ptr_type_node, rval); /* convert to void * first */
3217 rval = convert (string_type_node, rval); /* lets not add void* and ints */
3218 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
3219 /* Store header info. */
3220 cookie = build_indirect_ref (build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
3221 rval, extra), NULL_PTR);
3222 exp1 = build (MODIFY_EXPR, void_type_node,
3223 build_component_ref (cookie, nc_nelts_field_id, 0, 0),
3225 TREE_SIDE_EFFECTS (exp1) = 1;
3226 rval = convert (build_pointer_type (true_type), rval);
3227 TREE_CALLS_NEW (rval) = 1;
3228 TREE_SIDE_EFFECTS (rval) = 1;
3229 rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
3230 build_tree_list (NULL_TREE, rval)));
3233 /* We've figured out where the allocation is to go.
3234 If we're not eliding constructors, then if a constructor
3235 is defined, we must go through it. */
3236 if (!has_array && (rval == NULL_TREE || !flag_elide_constructors)
3237 && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3240 /* Constructors are never virtual. If it has an initialization, we
3241 need to complain if we aren't allowed to use the ctor that took
3243 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
3245 /* If a copy constructor might work, set things up so that we can
3246 try that after this. We deliberately don't clear LOOKUP_COMPLAIN
3247 any more, since that would make it impossible to rationally use
3248 the access of a constructor that matches perfectly. */
3250 if (rval != NULL_TREE)
3251 flags |= LOOKUP_SPECULATIVELY;
3254 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
3256 init = tree_cons (NULL_TREE, integer_one_node, init);
3257 flags |= LOOKUP_HAS_IN_CHARGE;
3263 if (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE)
3264 tmp = build_indirect_ref (tmp, NULL_PTR);
3266 newrval = build_method_call (tmp, constructor_name_full (true_type),
3267 init, NULL_TREE, flags);
3273 TREE_HAS_CONSTRUCTOR (rval) = 1;
3276 rval = error_mark_node;
3280 if (rval == error_mark_node)
3281 return error_mark_node;
3282 rval = save_expr (rval);
3283 TREE_HAS_CONSTRUCTOR (rval) = 1;
3285 /* Don't call any constructors or do any initialization. */
3286 if (init == void_type_node)
3289 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
3291 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
3293 /* New 2.0 interpretation: `new int (10)' means
3294 allocate an int, and initialize it with 10. */
3296 init = build_c_cast (type, init);
3297 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
3298 build_modify_expr (build_indirect_ref (rval, NULL_PTR),
3301 TREE_SIDE_EFFECTS (rval) = 1;
3302 TREE_CALLS_NEW (rval) = 1;
3304 else if (current_function_decl == NULL_TREE)
3306 extern tree static_aggregates;
3308 /* In case of static initialization, SAVE_EXPR is good enough. */
3309 init = copy_to_permanent (init);
3310 rval = copy_to_permanent (rval);
3311 static_aggregates = perm_tree_cons (init, rval, static_aggregates);
3315 /* Have to wrap this in RTL_EXPR for two cases:
3316 in base or member initialization and if we
3317 are a branch of a ?: operator. Since we
3318 can't easily know the latter, just do it always. */
3319 tree xval = make_node (RTL_EXPR);
3321 TREE_TYPE (xval) = TREE_TYPE (rval);
3322 do_pending_stack_adjust ();
3323 start_sequence_for_rtl_expr (xval);
3325 /* As a matter of principle, `start_sequence' should do this. */
3329 rval = expand_vec_init (decl, rval,
3330 build_binary_op (MINUS_EXPR, nelts, integer_one_node, 1),
3333 expand_aggr_init (build_indirect_ref (rval, NULL_PTR), init, 0);
3335 do_pending_stack_adjust ();
3337 TREE_SIDE_EFFECTS (xval) = 1;
3338 TREE_CALLS_NEW (xval) = 1;
3339 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3342 if (TREE_CODE (rval) == SAVE_EXPR)
3344 /* Errors may cause this to not get evaluated. */
3345 if (SAVE_EXPR_RTL (rval) == 0)
3346 SAVE_EXPR_RTL (rval) = const0_rtx;
3347 RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
3351 my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
3352 RTL_EXPR_RTL (xval) = DECL_RTL (rval);
3358 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
3360 /* The type of new int [3][3] is not int *, but int [3] * */
3361 rval = build_c_cast (build_pointer_type (type), rval);
3365 rval = build_compound_expr (chainon (pending_sizes,
3366 build_tree_list (NULL_TREE, rval)));
3370 extern tree gc_visible;
3374 rval = save_expr (rval);
3375 /* We don't need a `headof' operation to do this because
3376 we know where the object starts. */
3377 objbits = build1 (INDIRECT_REF, unsigned_type_node,
3378 build (MINUS_EXPR, ptr_type_node,
3379 rval, c_sizeof_nowarn (unsigned_type_node)));
3380 update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible);
3381 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
3382 tree_cons (NULL_TREE, update_expr,
3383 build_tree_list (NULL_TREE, rval))));
3386 return save_expr (rval);
3389 /* `expand_vec_init' performs initialization of a vector of aggregate
3392 DECL is passed only for error reporting, and provides line number
3393 and source file name information.
3394 BASE is the space where the vector will be.
3395 MAXINDEX is the maximum index of the array (one less than the
3396 number of elements).
3397 INIT is the (possibly NULL) initializer.
3399 FROM_ARRAY is 0 if we should init everything with INIT
3400 (i.e., every element initialized from INIT).
3401 FROM_ARRAY is 1 if we should index into INIT in parallel
3402 with initialization of DECL.
3403 FROM_ARRAY is 2 if we should index into INIT in parallel,
3404 but use assignment instead of initialization. */
3407 expand_vec_init (decl, base, maxindex, init, from_array)
3408 tree decl, base, maxindex, init;
3412 tree iterator, base2 = NULL_TREE;
3413 tree type = TREE_TYPE (TREE_TYPE (base));
3416 maxindex = convert (integer_type_node, maxindex);
3417 if (maxindex == error_mark_node)
3418 return error_mark_node;
3420 if (current_function_decl == NULL_TREE)
3422 rval = make_tree_vec (3);
3423 TREE_VEC_ELT (rval, 0) = base;
3424 TREE_VEC_ELT (rval, 1) = maxindex;
3425 TREE_VEC_ELT (rval, 2) = init;
3429 size = size_in_bytes (type);
3431 /* Set to zero in case size is <= 0. Optimizer will delete this if
3432 it is not needed. */
3433 rval = get_temp_regvar (TYPE_POINTER_TO (type),
3434 convert (TYPE_POINTER_TO (type), null_pointer_node));
3435 base = default_conversion (base);
3436 base = convert (TYPE_POINTER_TO (type), base);
3437 expand_assignment (rval, base, 0, 0);
3438 base = get_temp_regvar (TYPE_POINTER_TO (type), base);
3440 if (init != NULL_TREE
3441 && TREE_CODE (init) == CONSTRUCTOR
3442 && TREE_TYPE (init) == TREE_TYPE (decl))
3444 /* Initialization of array from {...}. */
3445 tree elts = CONSTRUCTOR_ELTS (init);
3446 tree baseref = build1 (INDIRECT_REF, type, base);
3447 tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
3448 int host_i = TREE_INT_CST_LOW (maxindex);
3450 if (IS_AGGR_TYPE (type))
3455 expand_aggr_init (baseref, TREE_VALUE (elts), 0);
3457 expand_assignment (base, baseinc, 0, 0);
3458 elts = TREE_CHAIN (elts);
3460 /* Initialize any elements by default if possible. */
3463 if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3466 use_variable (DECL_RTL (base));
3470 iterator = get_temp_regvar (integer_type_node,
3471 build_int_2 (host_i, 0));
3473 goto init_by_default;
3479 expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3481 expand_assignment (base, baseinc, 0, 0);
3482 elts = TREE_CHAIN (elts);
3486 use_variable (DECL_RTL (base));
3492 iterator = get_temp_regvar (integer_type_node, maxindex);
3496 /* If initializing one array from another,
3497 initialize element by element. */
3500 /* We rely upon the below calls the do argument checking */
3501 if (decl == NULL_TREE)
3503 sorry ("initialization of array from dissimilar array type");
3504 return error_mark_node;
3508 base2 = default_conversion (init);
3509 itype = TREE_TYPE (base2);
3510 base2 = get_temp_regvar (itype, base2);
3511 itype = TREE_TYPE (itype);
3513 else if (TYPE_LANG_SPECIFIC (type)
3514 && TYPE_NEEDS_CONSTRUCTING (type)
3515 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3517 error ("initializer ends prematurely");
3518 return error_mark_node;
3522 expand_start_cond (build (GE_EXPR, integer_type_node,
3523 iterator, integer_zero_node), 0);
3524 expand_start_loop_continue_elsewhere (1);
3528 tree to = build1 (INDIRECT_REF, type, base);
3532 from = build1 (INDIRECT_REF, itype, base2);
3536 if (from_array == 2)
3537 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3538 else if (TYPE_NEEDS_CONSTRUCTING (type))
3539 expand_aggr_init (to, from, 0);
3541 expand_assignment (to, from, 0, 0);
3543 my_friendly_abort (57);
3545 else if (TREE_CODE (type) == ARRAY_TYPE)
3548 sorry ("cannot initialize multi-dimensional array with initializer");
3549 expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
3550 array_type_nelts (type), 0, 0);
3553 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
3555 expand_assignment (base,
3556 build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
3559 expand_assignment (base2,
3560 build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
3561 expand_loop_continue_here ();
3562 expand_exit_loop_if_false (0, build (NE_EXPR, integer_type_node,
3563 build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
3567 use_variable (DECL_RTL (base));
3569 use_variable (DECL_RTL (base2));
3574 use_variable (DECL_RTL (iterator));
3579 use_variable (DECL_RTL (rval));
3583 /* Free up storage of type TYPE, at address ADDR.
3585 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3588 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3589 used as the second argument to operator delete. It can include
3590 things like padding and magic size cookies. It has virtual in it,
3591 because if you have a base pointer and you delete through a virtual
3592 destructor, it should be the size of the dynamic object, not the
3593 static object, see Free Store 12.5 ANSI C++ WP.
3595 This does not call any destructors. */
3597 build_x_delete (type, addr, use_global_delete, virtual_size)
3599 int use_global_delete;
3604 if (!use_global_delete
3605 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3606 && TREE_GETS_DELETE (TREE_TYPE (type)))
3607 rval = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3608 virtual_size, NULL_TREE);
3610 rval = build_builtin_call (void_type_node, BID,
3611 build_tree_list (NULL_TREE, addr));
3615 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3616 ADDR is an expression which yields the store to be destroyed.
3617 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3618 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3619 virtual baseclasses.
3620 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3622 FLAGS is the logical disjunction of zero or more LOOKUP_
3623 flags. See cp-tree.h for more info.
3625 This function does not delete an object's virtual base classes. */
3627 build_delete (type, addr, auto_delete, flags, use_global_delete)
3631 int use_global_delete;
3633 tree function, parms;
3639 if (addr == error_mark_node)
3640 return error_mark_node;
3642 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3643 set to `error_mark_node' before it gets properly cleaned up. */
3644 if (type == error_mark_node)
3645 return error_mark_node;
3647 type = TYPE_MAIN_VARIANT (type);
3649 if (TREE_CODE (type) == POINTER_TYPE)
3651 type = TREE_TYPE (type);
3652 if (TYPE_SIZE (type) == 0)
3654 incomplete_type_error (0, type);
3655 return error_mark_node;
3657 if (TREE_CODE (type) == ARRAY_TYPE)
3659 if (! IS_AGGR_TYPE (type))
3661 /* Call the builtin operator delete. */
3662 return build_builtin_call (void_type_node, BID,
3663 build_tree_list (NULL_TREE, addr));
3665 if (TREE_SIDE_EFFECTS (addr))
3666 addr = save_expr (addr);
3667 ref = build_indirect_ref (addr, NULL_PTR);
3670 else if (TREE_CODE (type) == ARRAY_TYPE)
3673 if (TREE_SIDE_EFFECTS (addr))
3674 addr = save_expr (addr);
3675 return build_vec_delete (addr, array_type_nelts (type),
3676 c_sizeof_nowarn (TREE_TYPE (type)),
3677 NULL_TREE, auto_delete, integer_two_node);
3681 /* Don't check PROTECT here; leave that decision to the
3682 destructor. If the destructor is accessible, call it,
3683 else report error. */
3684 addr = build_unary_op (ADDR_EXPR, addr, 0);
3685 if (TREE_SIDE_EFFECTS (addr))
3686 addr = save_expr (addr);
3688 if (TREE_CONSTANT (addr))
3689 addr = convert_pointer_to (type, addr);
3691 addr = convert_force (build_pointer_type (type), addr);
3693 if (TREE_CODE (addr) == NOP_EXPR
3694 && TREE_OPERAND (addr, 0) == current_class_decl)
3697 ref = build_indirect_ref (addr, NULL_PTR);
3701 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3703 if (! TYPE_NEEDS_DESTRUCTOR (type))
3705 if (auto_delete == integer_zero_node)
3706 return void_zero_node;
3708 /* Pass the size of the object down to the operator delete() in
3709 addition to the ADDR. */
3710 if (TREE_GETS_DELETE (type) && !use_global_delete)
3712 /* This is probably wrong. It should be the size of the virtual
3713 object being deleted. */
3714 tree virtual_size = c_sizeof_nowarn (type);
3715 return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3716 virtual_size, NULL_TREE);
3719 /* Call the builtin operator delete. */
3720 return build_builtin_call (void_type_node, BID,
3721 build_tree_list (NULL_TREE, addr));
3723 parms = build_tree_list (NULL_TREE, addr);
3725 /* Below, we will reverse the order in which these calls are made.
3726 If we have a destructor, then that destructor will take care
3727 of the base classes; otherwise, we must do that here. */
3728 if (TYPE_HAS_DESTRUCTOR (type))
3730 tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0));
3731 tree basetypes = TYPE_BINFO (type);
3733 if (flags & LOOKUP_PROTECT)
3735 enum access_type access = compute_access (basetypes, dtor);
3737 if (access == access_private)
3739 if (flags & LOOKUP_COMPLAIN)
3740 cp_error ("destructor for type `%T' is private in this scope", type);
3741 return error_mark_node;
3743 else if (access == access_protected)
3745 if (flags & LOOKUP_COMPLAIN)
3746 cp_error ("destructor for type `%T' is protected in this scope", type);
3747 return error_mark_node;
3751 /* Once we are in a destructor, try not going through
3752 the virtual function table to find the next destructor. */
3753 if (DECL_VINDEX (dtor)
3754 && ! (flags & LOOKUP_NONVIRTUAL)
3755 && TREE_CODE (auto_delete) != PARM_DECL
3756 && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
3758 tree binfo, basetype;
3759 /* The code below is probably all broken. See call.c for the
3760 complete right way to do this. this offsets may not be right
3761 in the below. (mrs) */
3762 /* This destructor must be called via virtual function table. */
3763 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0);
3764 basetype = DECL_CLASS_CONTEXT (dtor);
3765 binfo = get_binfo (basetype,
3766 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3768 expr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3769 if (expr != TREE_VALUE (parms))
3772 ref = build_indirect_ref (expr, NULL_PTR);
3773 TREE_VALUE (parms) = expr;
3775 function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
3776 if (function == error_mark_node)
3777 return error_mark_node;
3778 TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
3779 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3780 expr = build_function_call (function, parms);
3781 if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3783 /* Handle the case where a virtual destructor is
3784 being called on an item that is 0.
3786 @@ Does this really need to be done? */
3787 tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
3789 if (TREE_CODE (ref) == VAR_DECL
3790 || TREE_CODE (ref) == COMPONENT_REF)
3791 warning ("losing in build_delete");
3793 expr = build (COND_EXPR, void_type_node,
3794 ifexp, expr, void_zero_node);
3801 if ((flags & LOOKUP_DESTRUCTOR)
3802 || TREE_CODE (ref) == VAR_DECL
3803 || TREE_CODE (ref) == PARM_DECL
3804 || TREE_CODE (ref) == COMPONENT_REF
3805 || TREE_CODE (ref) == ARRAY_REF)
3806 /* These can't be 0. */
3807 ifexp = integer_one_node;
3809 /* Handle the case where a non-virtual destructor is
3810 being called on an item that is 0. */
3811 ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
3813 /* Used to mean that this destructor was known to be empty,
3814 but that's now obsolete. */
3815 my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
3817 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3818 expr = build_function_call (dtor, parms);
3820 if (ifexp != integer_one_node)
3821 expr = build (COND_EXPR, void_type_node,
3822 ifexp, expr, void_zero_node);
3828 /* This can get visibilities wrong. */
3829 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3830 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3831 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3832 tree exprstmt = NULL_TREE;
3833 tree parent_auto_delete = auto_delete;
3836 /* If this type does not have a destructor, but does have
3837 operator delete, call the parent parent destructor (if any),
3838 but let this node do the deleting. Otherwise, it is ok
3839 to let the parent destructor do the deleting. */
3840 if (TREE_GETS_DELETE (type) && !use_global_delete)
3842 parent_auto_delete = integer_zero_node;
3843 if (auto_delete == integer_zero_node)
3849 /* This is probably wrong. It should be the size of the
3850 virtual object being deleted. */
3851 virtual_size = c_sizeof_nowarn (type);
3853 expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3854 virtual_size, NULL_TREE);
3855 if (expr == error_mark_node)
3856 return error_mark_node;
3857 if (auto_delete != integer_one_node)
3858 cond = build (COND_EXPR, void_type_node,
3859 build (BIT_AND_EXPR, integer_type_node,
3860 auto_delete, integer_one_node),
3861 expr, void_zero_node);
3866 else if (base_binfo == NULL_TREE
3867 || (TREE_VIA_VIRTUAL (base_binfo) == 0
3868 && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
3872 /* This is probably wrong. It should be the size of the virtual
3873 object being deleted. */
3874 virtual_size = c_sizeof_nowarn (type);
3876 cond = build (COND_EXPR, void_type_node,
3877 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3878 build_builtin_call (void_type_node, BID,
3879 build_tree_list (NULL_TREE, addr)),
3886 exprstmt = build_tree_list (NULL_TREE, cond);
3889 && ! TREE_VIA_VIRTUAL (base_binfo)
3890 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3892 tree this_auto_delete;
3894 if (BINFO_OFFSET_ZEROP (base_binfo))
3895 this_auto_delete = parent_auto_delete;
3897 this_auto_delete = integer_zero_node;
3899 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), addr,
3900 this_auto_delete, flags, 0);
3901 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3904 /* Take care of the remaining baseclasses. */
3905 for (i = 1; i < n_baseclasses; i++)
3907 base_binfo = TREE_VEC_ELT (binfos, i);
3908 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3909 || TREE_VIA_VIRTUAL (base_binfo))
3912 /* May be zero offset if other baseclasses are virtual. */
3913 expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
3914 addr, BINFO_OFFSET (base_binfo)));
3916 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), expr,
3920 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3923 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3925 if (TREE_CODE (member) != FIELD_DECL)
3927 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3929 tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
3930 tree this_type = TREE_TYPE (member);
3931 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3932 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3937 return build_compound_expr (exprstmt);
3938 /* Virtual base classes make this function do nothing. */
3939 return void_zero_node;
3943 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3946 build_vbase_delete (type, decl)
3949 tree vbases = CLASSTYPE_VBASECLASSES (type);
3950 tree result = NULL_TREE;
3951 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3953 my_friendly_assert (addr != error_mark_node, 222);
3957 tree this_addr = convert_force (TYPE_POINTER_TO (BINFO_TYPE (vbases)),
3959 result = tree_cons (NULL_TREE,
3960 build_delete (TREE_TYPE (this_addr), this_addr,
3962 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3964 vbases = TREE_CHAIN (vbases);
3966 return build_compound_expr (nreverse (result));
3969 /* Build a C++ vector delete expression.
3970 MAXINDEX is the number of elements to be deleted.
3971 ELT_SIZE is the nominal size of each element in the vector.
3972 BASE is the expression that should yield the store to be deleted.
3973 DTOR_DUMMY is a placeholder for a destructor. The library function
3974 __builtin_vec_delete has a pointer to function in this position.
3975 This function expands (or synthesizes) these calls itself.
3976 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3977 AUTO_DELETE say whether each item in the container should be deallocated.
3979 This also calls delete for virtual baseclasses of elements of the vector.
3981 Update: MAXINDEX is no longer needed. The size can be extracted from the
3982 start of the vector for pointers, and from the type for arrays. We still
3983 use MAXINDEX for arrays because it happens to already have one of the
3984 values we'd have to extract. (We could use MAXINDEX with pointers to
3985 confirm the size, and trap if the numbers differ; not clear that it'd
3986 be worth bothering.) */
3988 build_vec_delete (base, maxindex, elt_size, dtor_dummy, auto_delete_vec, auto_delete)
3989 tree base, maxindex, elt_size;
3991 tree auto_delete_vec, auto_delete;
3993 tree ptype = TREE_TYPE (base);
3996 /* Temporary variables used by the loop. */
3997 tree tbase, size_exp, tbase_init;
3999 /* This is the body of the loop that implements the deletion of a
4000 single element, and moves temp variables to next elements. */
4003 /* This is the LOOP_EXPR that governs the deletion of the elements. */
4006 /* This is the thing that governs what to do after the loop has run. */
4007 tree deallocate_expr = 0;
4009 /* This is the BIND_EXPR which holds the outermost iterator of the
4010 loop. It is convenient to set this variable up and test it before
4011 executing any other code in the loop.
4012 This is also the containing expression returned by this function. */
4013 tree controller = NULL_TREE;
4015 /* This is the BLOCK to record the symbol binding for debugging. */
4018 base = stabilize_reference (base);
4020 /* Since we can use base many times, save_expr it. */
4021 if (TREE_SIDE_EFFECTS (base))
4022 base = save_expr (base);
4024 if (TREE_CODE (ptype) == POINTER_TYPE)
4026 /* Step back one from start of vector, and read dimension. */
4027 tree cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
4028 base, BI_header_size);
4029 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
4030 maxindex = build_component_ref (cookie, nc_nelts_field_id, 0, 0);
4032 ptype = TREE_TYPE (ptype);
4033 while (TREE_CODE (ptype) == ARRAY_TYPE);
4035 else if (TREE_CODE (ptype) == ARRAY_TYPE)
4037 /* get the total number of things in the array, maxindex is a bad name */
4038 maxindex = array_type_nelts_total (ptype);
4039 while (TREE_CODE (ptype) == ARRAY_TYPE)
4040 ptype = TREE_TYPE (ptype);
4041 base = build_unary_op (ADDR_EXPR, base, 1);
4045 error ("type to vector delete is neither pointer or array type");
4046 return error_mark_node;
4049 ptype = TYPE_POINTER_TO (type);
4051 size_exp = size_in_bytes (type);
4053 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
4055 loop = integer_zero_node;
4059 /* The below is short by BI_header_size */
4060 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4062 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
4063 tbase_init = build_modify_expr (tbase, NOP_EXPR,
4064 fold (build (PLUS_EXPR, ptype,
4067 DECL_REGISTER (tbase) = 1;
4068 controller = build (BIND_EXPR, void_type_node, tbase, 0, 0);
4069 TREE_SIDE_EFFECTS (controller) = 1;
4070 block = build_block (tbase, 0, 0, 0, 0);
4071 add_block_current_level (block);
4073 if (auto_delete != integer_zero_node
4074 && auto_delete != integer_two_node)
4076 tree base_tbd = convert (ptype,
4077 build_binary_op (MINUS_EXPR,
4078 convert (ptr_type_node, base),
4081 /* This is the real size */
4082 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4083 body = build_tree_list (NULL_TREE,
4084 build_x_delete (ptr_type_node, base_tbd, 1,
4086 body = build (COND_EXPR, void_type_node,
4087 build (BIT_AND_EXPR, integer_type_node,
4088 auto_delete, integer_one_node),
4089 body, integer_zero_node);
4094 body = tree_cons (NULL_TREE,
4095 build_delete (ptype, tbase, auto_delete,
4096 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
4099 body = tree_cons (NULL_TREE,
4100 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
4103 body = tree_cons (NULL_TREE,
4104 build (EXIT_EXPR, void_type_node,
4105 build (EQ_EXPR, integer_type_node, base, tbase)),
4108 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
4110 loop = tree_cons (NULL_TREE, tbase_init,
4111 tree_cons (NULL_TREE, loop, NULL_TREE));
4112 loop = build_compound_expr (loop);
4115 /* If the delete flag is one, or anything else with the low bit set,
4116 delete the storage. */
4117 if (auto_delete_vec == integer_zero_node
4118 || auto_delete_vec == integer_two_node)
4119 deallocate_expr = integer_zero_node;
4124 /* The below is short by BI_header_size */
4125 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4127 if (loop == integer_zero_node)
4132 base_tbd = convert (ptype,
4133 build_binary_op (MINUS_EXPR,
4134 convert (string_type_node, base),
4137 /* True size with header. */
4138 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4140 deallocate_expr = build_x_delete (ptr_type_node, base_tbd, 1,
4142 if (auto_delete_vec != integer_one_node)
4143 deallocate_expr = build (COND_EXPR, void_type_node,
4144 build (BIT_AND_EXPR, integer_type_node,
4145 auto_delete_vec, integer_one_node),
4146 deallocate_expr, integer_zero_node);
4149 if (loop && deallocate_expr != integer_zero_node)
4151 body = tree_cons (NULL_TREE, loop,
4152 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
4153 body = build_compound_expr (body);
4158 /* Outermost wrapper: If pointer is null, punt. */
4159 body = build (COND_EXPR, void_type_node,
4160 build (NE_EXPR, integer_type_node, base, integer_zero_node),
4161 body, integer_zero_node);
4162 body = build1 (NOP_EXPR, void_type_node, body);
4166 TREE_OPERAND (controller, 1) = body;
4170 return convert (void_type_node, body);