1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 89, 92, 93, 94, 1995 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
35 /* In C++, structures with well-defined constructors are initialized by
36 those constructors, unasked. CURRENT_BASE_INIT_LIST
37 holds a list of stmts for a BASE_INIT term in the grammar.
38 This list has one element for each base class which must be
39 initialized. The list elements are [basename, init], with
40 type basetype. This allows the possibly anachronistic form
41 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
42 where each successive term can be handed down the constructor
43 line. Perhaps this was not intended. */
44 tree current_base_init_list, current_member_init_list;
46 void emit_base_init ();
47 void check_base_init ();
48 static void expand_aggr_vbase_init ();
49 void expand_member_init ();
50 void expand_aggr_init ();
52 static void expand_aggr_init_1 ();
53 static void expand_recursive_init_1 ();
54 static void expand_recursive_init ();
55 static void expand_virtual_init PROTO((tree, tree));
56 tree expand_vec_init ();
58 static void add_friend (), add_friends ();
60 /* Cache _builtin_new and _builtin_delete exprs. */
61 static tree BIN, BID, BIVN, BIVD;
63 /* Cache the identifier nodes for the two magic field of a new cookie. */
64 static tree nc_nelts_field_id;
66 static tree nc_ptr_2comp_field_id;
69 static tree minus_one;
71 /* Set up local variable for this file. MUST BE CALLED AFTER
72 INIT_DECL_PROCESSING. */
74 tree BI_header_type, BI_header_size;
76 void init_init_processing ()
80 /* Define implicit `operator new' and `operator delete' functions. */
81 BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR])));
82 TREE_USED (TREE_OPERAND (BIN, 0)) = 0;
83 BID = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR])));
84 TREE_USED (TREE_OPERAND (BID, 0)) = 0;
85 BIVN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_NEW_EXPR])));
86 TREE_USED (TREE_OPERAND (BIVN, 0)) = 0;
87 BIVD = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_DELETE_EXPR])));
88 TREE_USED (TREE_OPERAND (BIVD, 0)) = 0;
89 minus_one = build_int_2 (-1, -1);
91 /* Define the structure that holds header information for
92 arrays allocated via operator new. */
93 BI_header_type = make_lang_type (RECORD_TYPE);
94 nc_nelts_field_id = get_identifier ("nelts");
95 fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
96 finish_builtin_type (BI_header_type, "__new_cookie", fields,
98 BI_header_size = size_in_bytes (BI_header_type);
101 /* Subroutine of emit_base_init. For BINFO, initialize all the
102 virtual function table pointers, except those that come from
103 virtual base classes. Initialize binfo's vtable pointer, if
104 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual
105 function table pointers in all bases have been initialized already,
106 probably because their constructors have just be run. ADDR is the
107 pointer to the object whos vtables we are going to initialize.
109 REAL_BINFO is usually the same as BINFO, except when addr is not of
110 pointer to the type of the real derived type that we want to
111 initialize for. This is the case when addr is a pointer to a sub
112 object of a complete object, and we only want to do part of the
113 complete object's initialization of vtable pointers. This is done
114 for all virtual table pointers in virtual base classes. REAL_BINFO
115 is used to find the BINFO_VTABLE that we initialize with. BINFO is
116 used for conversions of addr to subobjects.
118 BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo).
120 Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE
123 expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr)
124 tree real_binfo, binfo, addr;
125 int init_self, can_elide;
127 tree real_binfos = BINFO_BASETYPES (real_binfo);
128 tree binfos = BINFO_BASETYPES (binfo);
129 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
131 for (i = 0; i < n_baselinks; i++)
133 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
134 tree base_binfo = TREE_VEC_ELT (binfos, i);
135 int is_not_base_vtable =
136 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
137 if (! TREE_VIA_VIRTUAL (real_base_binfo))
138 expand_direct_vtbls_init (real_base_binfo, base_binfo,
139 is_not_base_vtable, can_elide, addr);
142 /* Before turning this on, make sure it is correct. */
143 if (can_elide && ! BINFO_MODIFIED (binfo))
146 /* Should we use something besides CLASSTYPE_VFIELDS? */
147 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
149 tree base_ptr = convert_pointer_to_real (binfo, addr);
150 expand_virtual_init (real_binfo, base_ptr);
155 /* Subroutine of emit_base_init. */
157 perform_member_init (member, name, init, explicit, protect_list)
158 tree member, name, init, *protect_list;
162 tree type = TREE_TYPE (member);
164 if (TYPE_NEEDS_CONSTRUCTING (type)
165 || (init && TYPE_HAS_CONSTRUCTOR (type)))
167 /* Since `init' is already a TREE_LIST on the current_member_init_list,
168 only build it into one if we aren't already a list. */
169 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
170 init = build_tree_list (NULL_TREE, init);
172 decl = build_component_ref (C_C_D, name, 0, explicit);
175 && TREE_CODE (type) == ARRAY_TYPE
177 && TREE_CHAIN (init) == NULL_TREE
178 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
180 /* Initialization of one array from another. */
181 expand_vec_init (TREE_OPERAND (decl, 1), decl,
182 array_type_nelts (type), TREE_VALUE (init), 1);
185 expand_aggr_init (decl, init, 0, 0);
189 if (init == NULL_TREE)
193 cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor",
194 member, current_class_type);
195 init = error_mark_node;
197 /* member traversal: note it leaves init NULL */
198 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
199 cp_pedwarn ("uninitialized reference member `%D'", member);
201 else if (TREE_CODE (init) == TREE_LIST)
203 /* There was an explicit member initialization. Do some
204 work in that case. */
205 if (TREE_CHAIN (init))
207 warning ("initializer list treated as compound expression");
208 init = build_compound_expr (init);
211 init = TREE_VALUE (init);
214 /* We only build this with a null init if we got it from the
215 current_member_init_list. */
216 if (init || explicit)
218 decl = build_component_ref (C_C_D, name, 0, explicit);
219 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
222 expand_cleanups_to (NULL_TREE);
224 if (TYPE_NEEDS_DESTRUCTOR (type))
226 tree expr = build_component_ref (C_C_D, name, 0, explicit);
227 expr = build_delete (type, expr, integer_zero_node,
228 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
230 if (expr != error_mark_node)
233 *protect_list = tree_cons (NULL_TREE, expr, *protect_list);
238 extern int warn_reorder;
240 /* Subroutine of emit_member_init. */
245 tree x, member, name, field, init;
246 tree init_list = NULL_TREE;
247 tree fields_to_unmark = NULL_TREE;
251 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
255 /* member could be, for example, a CONST_DECL for an enumerated
256 tag; we don't want to try to initialize that, since it already
258 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
261 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
263 /* If we cleared this out, then pay no attention to it. */
264 if (TREE_PURPOSE (x) == NULL_TREE)
266 name = TREE_PURPOSE (x);
269 field = (TREE_CODE (name) == COMPONENT_REF
270 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
272 /* Let's find out when this happens. */
273 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
274 field = IDENTIFIER_CLASS_VALUE (name);
277 /* If one member shadows another, get the outermost one. */
278 if (TREE_CODE (field) == TREE_LIST)
279 field = TREE_VALUE (field);
287 cp_warning_at ("member initializers for `%#D'", last_field);
288 cp_warning_at (" and `%#D'", field);
289 warning (" will be re-ordered to match declaration order");
295 /* Make sure we won't try to work on this init again. */
296 TREE_PURPOSE (x) = NULL_TREE;
297 x = build_tree_list (name, TREE_VALUE (x));
302 /* If we didn't find MEMBER in the list, create a dummy entry
303 so the two lists (INIT_LIST and the list of members) will be
305 x = build_tree_list (NULL_TREE, NULL_TREE);
307 init_list = chainon (init_list, x);
310 /* Initializers for base members go at the end. */
311 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
313 name = TREE_PURPOSE (x);
316 if (purpose_member (name, init_list))
318 cp_error ("multiple initializations given for member `%D'",
319 IDENTIFIER_CLASS_VALUE (name));
323 init_list = chainon (init_list,
324 build_tree_list (name, TREE_VALUE (x)));
325 TREE_PURPOSE (x) = NULL_TREE;
333 sort_base_init (t, rbase_ptr, vbase_ptr)
334 tree t, *rbase_ptr, *vbase_ptr;
336 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
337 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
343 /* For warn_reorder. */
345 tree last_base = NULL_TREE;
347 tree rbases = NULL_TREE;
348 tree vbases = NULL_TREE;
350 /* First walk through and splice out vbase and invalid initializers.
351 Also replace names with binfos. */
353 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
354 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
356 tree basename = TREE_PURPOSE (x);
359 if (basename == NULL_TREE)
361 /* Initializer for single base class. Must not
362 use multiple inheritance or this is ambiguous. */
363 switch (n_baseclasses)
366 cp_error ("`%T' does not have a base class to initialize",
372 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
376 binfo = TREE_VEC_ELT (binfos, 0);
378 else if (is_aggr_typedef (basename, 1))
380 binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t);
381 if (binfo == NULL_TREE)
384 /* Virtual base classes are special cases. Their initializers
385 are recorded with this constructor, and they are used when
386 this constructor is the top-level constructor called. */
387 if (TREE_VIA_VIRTUAL (binfo))
389 tree v = CLASSTYPE_VBASECLASSES (t);
390 while (BINFO_TYPE (v) != BINFO_TYPE (binfo))
393 vbases = tree_cons (v, TREE_VALUE (x), vbases);
398 /* Otherwise, if it is not an immediate base class, complain. */
399 for (i = n_baseclasses-1; i >= 0; i--)
400 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
404 cp_error ("`%T' is not an immediate base class of `%T'",
405 IDENTIFIER_TYPE_VALUE (basename),
412 my_friendly_abort (365);
414 TREE_PURPOSE (x) = binfo;
415 TREE_CHAIN (last) = x;
418 TREE_CHAIN (last) = NULL_TREE;
420 /* Now walk through our regular bases and make sure they're initialized. */
422 for (i = 0; i < n_baseclasses; ++i)
424 tree base_binfo = TREE_VEC_ELT (binfos, i);
427 if (TREE_VIA_VIRTUAL (base_binfo))
430 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
432 tree binfo = TREE_PURPOSE (x);
434 if (binfo == NULL_TREE)
437 if (binfo == base_binfo)
443 cp_warning_at ("base initializers for `%#T'", last_base);
444 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
445 warning (" will be re-ordered to match inheritance order");
448 last_base = BINFO_TYPE (binfo);
451 /* Make sure we won't try to work on this init again. */
452 TREE_PURPOSE (x) = NULL_TREE;
453 x = build_tree_list (binfo, TREE_VALUE (x));
458 /* If we didn't find BASE_BINFO in the list, create a dummy entry
459 so the two lists (RBASES and the list of bases) will be
461 x = build_tree_list (NULL_TREE, NULL_TREE);
463 rbases = chainon (rbases, x);
470 /* Perform partial cleanups for a base for exception handling. */
472 build_partial_cleanup_for (binfo)
475 tree expr = convert_pointer_to_real (binfo,
476 build_unary_op (ADDR_EXPR, C_C_D, 0));
478 return build_delete (TREE_TYPE (expr),
481 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
484 /* Perform whatever initializations have yet to be done on the base
485 class of the class variable. These actions are in the global
486 variable CURRENT_BASE_INIT_LIST. Such an action could be
487 NULL_TREE, meaning that the user has explicitly called the base
488 class constructor with no arguments.
490 If there is a need for a call to a constructor, we must surround
491 that call with a pushlevel/poplevel pair, since we are technically
492 at the PARM level of scope.
494 Argument IMMEDIATELY, if zero, forces a new sequence to be
495 generated to contain these new insns, so it can be emitted later.
496 This sequence is saved in the global variable BASE_INIT_EXPR.
497 Otherwise, the insns are emitted into the current sequence.
499 Note that emit_base_init does *not* initialize virtual base
500 classes. That is done specially, elsewhere. */
502 extern tree base_init_expr, rtl_expr_chain;
505 emit_base_init (t, immediately)
509 extern tree in_charge_identifier;
513 tree rbase_init_list, vbase_init_list;
514 tree t_binfo = TYPE_BINFO (t);
515 tree binfos = BINFO_BASETYPES (t_binfo);
516 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
517 tree expr = NULL_TREE;
519 my_friendly_assert (protect_list == NULL_TREE, 999);
524 do_pending_stack_adjust ();
525 /* Make the RTL_EXPR node temporary, not momentary,
526 so that rtl_expr_chain doesn't become garbage. */
527 momentary = suspend_momentary ();
528 expr = make_node (RTL_EXPR);
529 resume_momentary (momentary);
530 start_sequence_for_rtl_expr (expr);
533 if (write_symbols == NO_DEBUG)
534 /* As a matter of principle, `start_sequence' should do this. */
537 /* Always emit a line number note so we can step into constructors. */
538 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
539 DECL_SOURCE_LINE (current_function_decl));
541 mem_init_list = sort_member_init (t);
542 current_member_init_list = NULL_TREE;
544 sort_base_init (t, &rbase_init_list, &vbase_init_list);
545 current_base_init_list = NULL_TREE;
547 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
549 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
551 expand_start_cond (first_arg, 0);
552 expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl,
557 /* Now, perform initialization of non-virtual base classes. */
558 for (i = 0; i < n_baseclasses; i++)
560 tree base = current_class_decl;
561 tree base_binfo = TREE_VEC_ELT (binfos, i);
562 tree init = void_list_node;
564 if (TREE_VIA_VIRTUAL (base_binfo))
567 #if 0 /* Once unsharing happens soon enough. */
568 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo);
570 BINFO_INHERITANCE_CHAIN (base_binfo) = t_binfo;
573 if (TREE_PURPOSE (rbase_init_list))
574 init = TREE_VALUE (rbase_init_list);
575 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
578 if (init != void_list_node)
580 member = convert_pointer_to_real (base_binfo, current_class_decl);
581 expand_aggr_init_1 (base_binfo, 0,
582 build_indirect_ref (member, NULL_PTR), init,
583 BINFO_OFFSET_ZEROP (base_binfo), LOOKUP_NORMAL);
584 expand_cleanups_to (NULL_TREE);
587 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
590 protect_list = tree_cons (NULL_TREE,
591 build_partial_cleanup_for (base_binfo),
595 rbase_init_list = TREE_CHAIN (rbase_init_list);
598 /* Initialize all the virtual function table fields that
599 do come from virtual base classes. */
600 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
601 expand_indirect_vtbls_init (t_binfo, C_C_D, current_class_decl, 0);
603 /* Initialize all the virtual function table fields that
604 do not come from virtual base classes. */
605 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_decl);
607 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
612 /* member could be, for example, a CONST_DECL for an enumerated
613 tag; we don't want to try to initialize that, since it already
615 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
618 /* See if we had a user-specified member initialization. */
619 if (TREE_PURPOSE (mem_init_list))
621 name = TREE_PURPOSE (mem_init_list);
622 init = TREE_VALUE (mem_init_list);
625 /* Also see if it's ever a COMPONENT_REF here. If it is, we
626 need to do `expand_assignment (name, init, 0, 0);' and
628 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
632 name = DECL_NAME (member);
633 init = DECL_INITIAL (member);
638 perform_member_init (member, name, init, from_init_list, &protect_list);
639 mem_init_list = TREE_CHAIN (mem_init_list);
642 /* Now initialize any members from our bases. */
643 while (mem_init_list)
645 tree name, init, field;
647 if (TREE_PURPOSE (mem_init_list))
649 name = TREE_PURPOSE (mem_init_list);
650 init = TREE_VALUE (mem_init_list);
651 /* XXX: this may need the COMPONENT_REF operand 0 check if
652 it turns out we actually get them. */
653 field = IDENTIFIER_CLASS_VALUE (name);
655 /* If one member shadows another, get the outermost one. */
656 if (TREE_CODE (field) == TREE_LIST)
658 field = TREE_VALUE (field);
659 if (decl_type_context (field) != current_class_type)
660 cp_error ("field `%D' not in immediate context", field);
664 /* It turns out if you have an anonymous union in the
665 class, a member from it can end up not being on the
666 list of fields (rather, the type is), and therefore
667 won't be seen by the for loop above. */
669 /* The code in this for loop is derived from a general loop
670 which had this check in it. Theoretically, we've hit
671 every initialization for the list of members in T, so
672 we shouldn't have anything but these left in this list. */
673 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
676 perform_member_init (field, name, init, 1, &protect_list);
678 mem_init_list = TREE_CHAIN (mem_init_list);
683 do_pending_stack_adjust ();
684 my_friendly_assert (base_init_expr == 0, 207);
685 base_init_expr = expr;
686 TREE_TYPE (expr) = void_type_node;
687 RTL_EXPR_RTL (expr) = const0_rtx;
688 RTL_EXPR_SEQUENCE (expr) = get_insns ();
689 rtl_expr_chain = tree_cons (NULL_TREE, expr, rtl_expr_chain);
691 TREE_SIDE_EFFECTS (expr) = 1;
694 /* All the implicit try blocks we built up will be zapped
695 when we come to a real binding contour boundary. */
698 /* Check that all fields are properly initialized after
699 an assignment to `this'. */
705 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
706 if (DECL_NAME (member) && TREE_USED (member))
707 cp_error ("field `%D' used before initialized (after assignment to `this')",
711 /* This code sets up the virtual function tables appropriate for
712 the pointer DECL. It is a one-ply initialization.
714 BINFO is the exact type that DECL is supposed to be. In
715 multiple inheritance, this might mean "C's A" if C : A, B. */
717 expand_virtual_init (binfo, decl)
720 tree type = BINFO_TYPE (binfo);
722 tree vtype, vtype_binfo;
724 /* This code is crusty. Should be simple, like:
725 vtbl = BINFO_VTABLE (binfo);
727 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
728 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
729 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
730 assemble_external (vtbl);
731 TREE_USED (vtbl) = 1;
732 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
733 decl = convert_pointer_to_real (vtype_binfo, decl);
734 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
735 if (vtbl_ptr == error_mark_node)
738 /* Have to convert VTBL since array sizes may be different. */
739 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
740 expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
743 /* Subroutine of `expand_aggr_vbase_init'.
744 BINFO is the binfo of the type that is being initialized.
745 INIT_LIST is the list of initializers for the virtual baseclass. */
747 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
748 tree binfo, exp, addr, init_list;
750 tree init = purpose_member (binfo, init_list);
751 tree ref = build_indirect_ref (addr, NULL_PTR);
753 init = TREE_VALUE (init);
754 /* Call constructors, but don't set up vtables. */
755 expand_aggr_init_1 (binfo, exp, ref, init, 0, LOOKUP_COMPLAIN);
756 expand_cleanups_to (NULL_TREE);
759 /* Initialize this object's virtual base class pointers. This must be
760 done only at the top-level of the object being constructed.
762 INIT_LIST is list of initialization for constructor to perform. */
764 expand_aggr_vbase_init (binfo, exp, addr, init_list)
770 tree type = BINFO_TYPE (binfo);
772 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
774 tree result = init_vbase_pointers (type, addr);
778 expand_expr_stmt (build_compound_expr (result));
780 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
781 vbases = TREE_CHAIN (vbases))
783 tree tmp = purpose_member (vbases, result);
784 expand_aggr_vbase_init_1 (vbases, exp,
785 TREE_OPERAND (TREE_VALUE (tmp), 0),
791 /* Subroutine to perform parser actions for member initialization.
792 S_ID is the scoped identifier.
793 NAME is the name of the member.
794 INIT is the initializer, or `void_type_node' if none. */
796 do_member_init (s_id, name, init)
797 tree s_id, name, init;
801 if (current_class_type == NULL_TREE
802 || ! is_aggr_typedef (s_id, 1))
804 binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id),
805 current_class_type, 1);
806 if (binfo == error_mark_node)
810 error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type);
814 base = convert_pointer_to (binfo, current_class_decl);
815 expand_member_init (build_indirect_ref (base, NULL_PTR), name, init);
818 /* Function to give error message if member initialization specification
819 is erroneous. FIELD is the member we decided to initialize.
820 TYPE is the type for which the initialization is being performed.
821 FIELD must be a member of TYPE, or the base type from which FIELD
822 comes must not need a constructor.
824 MEMBER_NAME is the name of the member. */
827 member_init_ok_or_else (field, type, member_name)
832 if (field == error_mark_node)
834 if (field == NULL_TREE)
836 cp_error ("class `%T' does not have any field named `%s'", type,
840 if (DECL_CONTEXT (field) != type
841 && TYPE_NEEDS_CONSTRUCTING (DECL_CONTEXT (field)))
843 if (current_function_decl && DECL_CONSTRUCTOR_P (current_function_decl))
844 cp_error ("initialization of `%D' inside constructor for `%T'",
847 cp_error ("member `%D' comes from base class needing constructor",
851 if (TREE_STATIC (field))
853 cp_error ("field `%#D' is static; only point of initialization is its declaration",
861 /* If NAME is a viable field name for the aggregate DECL,
862 and PARMS is a viable parameter list, then expand an _EXPR
863 which describes this initialization.
865 Note that we do not need to chase through the class's base classes
866 to look for NAME, because if it's in that list, it will be handled
867 by the constructor for that base class.
869 We do not yet have a fixed-point finder to instantiate types
870 being fed to overloaded constructors. If there is a unique
871 constructor, then argument types can be got from that one.
873 If INIT is non-NULL, then it the initialization should
874 be placed in `current_base_init_list', where it will be processed
875 by `emit_base_init'. */
877 expand_member_init (exp, name, init)
878 tree exp, name, init;
880 extern tree ptr_type_node; /* should be in tree.h */
882 tree basetype = NULL_TREE, field;
887 if (exp == NULL_TREE)
888 return; /* complain about this later */
890 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
892 if (name == NULL_TREE && IS_AGGR_TYPE (type))
893 switch (CLASSTYPE_N_BASECLASSES (type))
896 error ("base class initializer specified, but no base class to initialize");
899 basetype = TYPE_BINFO_BASETYPE (type, 0);
902 error ("initializer for unnamed base class ambiguous");
903 cp_error ("(type `%T' uses multiple inheritance)", type);
909 /* The grammar should not allow fields which have names
910 that are TYPENAMEs. Therefore, if the field has
911 a non-NULL TREE_TYPE, we may assume that this is an
912 attempt to initialize a base class member of the current
913 type. Otherwise, it is an attempt to initialize a
916 if (init == void_type_node)
919 if (name == NULL_TREE || IDENTIFIER_HAS_TYPE_VALUE (name))
923 if (name == NULL_TREE)
927 name = TYPE_IDENTIFIER (basetype);
930 error ("no base class to initialize");
937 basetype = IDENTIFIER_TYPE_VALUE (name);
939 && ! binfo_member (basetype, TYPE_BINFO (type))
940 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
942 if (IDENTIFIER_CLASS_VALUE (name))
944 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
945 error ("type `%s' is not an immediate or virtual basetype for `%s'",
946 IDENTIFIER_POINTER (name),
947 TYPE_NAME_STRING (type));
949 error ("type `%s' is not an immediate basetype for `%s'",
950 IDENTIFIER_POINTER (name),
951 TYPE_NAME_STRING (type));
956 if (purpose_member (name, current_base_init_list))
958 error ("base class `%s' already initialized",
959 IDENTIFIER_POINTER (name));
963 base_init = build_tree_list (name, init);
964 TREE_TYPE (base_init) = basetype;
965 current_base_init_list = chainon (current_base_init_list, base_init);
972 field = lookup_field (type, name, 1, 0);
974 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
977 if (purpose_member (name, current_member_init_list))
979 error ("field `%s' already initialized", IDENTIFIER_POINTER (name));
983 member_init = build_tree_list (name, init);
984 TREE_TYPE (member_init) = TREE_TYPE (field);
985 current_member_init_list = chainon (current_member_init_list, member_init);
989 else if (name == NULL_TREE)
991 compiler_error ("expand_member_init: name == NULL_TREE");
996 field = lookup_field (basetype, name, 0, 0);
998 if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
1001 /* now see if there is a constructor for this type
1002 which will take these args. */
1004 if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
1006 tree parmtypes, fndecl;
1008 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1010 /* just know that we've seen something for this node */
1011 DECL_INITIAL (exp) = error_mark_node;
1012 TREE_USED (exp) = 1;
1014 type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
1015 actual_name = TYPE_IDENTIFIER (type);
1016 parm = build_component_ref (exp, name, 0, 0);
1018 /* Now get to the constructor. */
1019 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1020 /* Get past destructor, if any. */
1021 if (TYPE_HAS_DESTRUCTOR (type))
1022 fndecl = DECL_CHAIN (fndecl);
1025 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209);
1027 /* If the field is unique, we can use the parameter
1028 types to guide possible type instantiation. */
1029 if (DECL_CHAIN (fndecl) == NULL_TREE)
1031 /* There was a confusion here between
1032 FIELD and FNDECL. The following code
1033 should be correct, but abort is here
1035 my_friendly_abort (48);
1036 parmtypes = FUNCTION_ARG_CHAIN (fndecl);
1040 parmtypes = NULL_TREE;
1044 init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
1045 if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
1046 rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
1050 if (rval != error_mark_node)
1052 /* Now, fill in the first parm with our guy */
1053 TREE_VALUE (TREE_OPERAND (rval, 1))
1054 = build_unary_op (ADDR_EXPR, parm, 0);
1055 TREE_TYPE (rval) = ptr_type_node;
1056 TREE_SIDE_EFFECTS (rval) = 1;
1059 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1061 parm = build_component_ref (exp, name, 0, 0);
1062 expand_aggr_init (parm, NULL_TREE, 0, 0);
1063 rval = error_mark_node;
1066 /* Now initialize the member. It does not have to
1067 be of aggregate type to receive initialization. */
1068 if (rval != error_mark_node)
1069 expand_expr_stmt (rval);
1072 /* This is like `expand_member_init', only it stores one aggregate
1075 INIT comes in two flavors: it is either a value which
1076 is to be stored in EXP, or it is a parameter list
1077 to go to a constructor, which will operate on EXP.
1078 If INIT is not a parameter list for a constructor, then set
1079 LOOKUP_ONLYCONVERTING.
1080 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1081 the initializer, if FLAGS is 0, then it is the (init) form.
1082 If `init' is a CONSTRUCTOR, then we emit a warning message,
1083 explaining that such initializations are invalid.
1085 ALIAS_THIS is nonzero iff we are initializing something which is
1086 essentially an alias for C_C_D. In this case, the base constructor
1087 may move it on us, and we must keep track of such deviations.
1089 If INIT resolves to a CALL_EXPR which happens to return
1090 something of the type we are looking for, then we know
1091 that we can safely use that call to perform the
1094 The virtual function table pointer cannot be set up here, because
1095 we do not really know its type.
1097 Virtual baseclass pointers are also set up here.
1099 This never calls operator=().
1101 When initializing, nothing is CONST.
1103 A default copy constructor may have to be used to perform the
1106 A constructor or a conversion operator may have to be used to
1107 perform the initialization, but not both, as it would be ambiguous.
1111 expand_aggr_init (exp, init, alias_this, flags)
1116 tree type = TREE_TYPE (exp);
1117 int was_const = TREE_READONLY (exp);
1118 int was_volatile = TREE_THIS_VOLATILE (exp);
1120 if (init == error_mark_node)
1123 TREE_READONLY (exp) = 0;
1124 TREE_THIS_VOLATILE (exp) = 0;
1126 if (init && TREE_CODE (init) != TREE_LIST)
1127 flags |= LOOKUP_ONLYCONVERTING;
1129 if (TREE_CODE (type) == ARRAY_TYPE)
1131 /* Must arrange to initialize each element of EXP
1132 from elements of INIT. */
1133 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1134 if (TYPE_READONLY (TREE_TYPE (type)) || TYPE_VOLATILE (TREE_TYPE (type)))
1136 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1138 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1140 if (init && TREE_TYPE (init) == NULL_TREE)
1142 /* Handle bad initializers like:
1146 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1150 int main(int argc, char **argv) {
1151 COMPLEX zees(1.0, 0.0)[10];
1154 error ("bad array initializer");
1157 expand_vec_init (exp, exp, array_type_nelts (type), init,
1158 init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
1159 TREE_READONLY (exp) = was_const;
1160 TREE_THIS_VOLATILE (exp) = was_volatile;
1161 TREE_TYPE (exp) = type;
1163 TREE_TYPE (init) = itype;
1167 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1168 /* just know that we've seen something for this node */
1169 TREE_USED (exp) = 1;
1172 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1173 constructor as parameters to an implicit GNU C++ constructor. */
1174 if (init && TREE_CODE (init) == CONSTRUCTOR
1175 && TYPE_HAS_CONSTRUCTOR (type)
1176 && TREE_TYPE (init) == type)
1177 init = CONSTRUCTOR_ELTS (init);
1180 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1181 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1182 init, alias_this, LOOKUP_NORMAL|flags);
1183 TREE_TYPE (exp) = type;
1184 TREE_READONLY (exp) = was_const;
1185 TREE_THIS_VOLATILE (exp) = was_volatile;
1189 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags)
1197 /* It fails because there may not be a constructor which takes
1198 its own type as the first (or only parameter), but which does
1199 take other types via a conversion. So, if the thing initializing
1200 the expression is a unit element of type X, first try X(X&),
1201 followed by initialization by X. If neither of these work
1202 out, then look hard. */
1206 if (init == NULL_TREE
1207 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1211 init = TREE_VALUE (parms);
1213 else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init)
1214 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (init)))
1216 rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
1217 TREE_USED (rval) = 1;
1218 expand_expr_stmt (rval);
1222 parms = build_tree_list (NULL_TREE, init);
1224 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1226 if (true_exp == exp)
1227 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1229 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1230 flags |= LOOKUP_HAS_IN_CHARGE;
1233 if (init && TREE_CHAIN (parms) == NULL_TREE
1234 && TYPE_HAS_TRIVIAL_INIT_REF (type)
1235 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (init)))
1237 rval = build (INIT_EXPR, type, exp, init);
1238 TREE_SIDE_EFFECTS (rval) = 1;
1239 expand_expr_stmt (rval);
1243 if (flags & LOOKUP_ONLYCONVERTING)
1244 flags |= LOOKUP_NO_CONVERSION;
1245 rval = build_method_call (exp, constructor_name_full (type),
1246 parms, binfo, flags);
1248 /* Private, protected, or otherwise unavailable. */
1249 if (rval == error_mark_node)
1251 if (flags & LOOKUP_COMPLAIN)
1252 cp_error ("in base initialization for %sclass `%T'",
1253 TREE_VIA_VIRTUAL (binfo) ? "virtual base " : "",
1256 else if (rval == NULL_TREE)
1257 my_friendly_abort (361);
1260 /* p. 222: if the base class assigns to `this', then that
1261 value is used in the derived class. */
1262 if ((flag_this_is_variable & 1) && alias_this)
1264 TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1265 expand_assignment (current_class_decl, rval, 0, 0);
1268 expand_expr_stmt (rval);
1273 /* This function is responsible for initializing EXP with INIT
1276 BINFO is the binfo of the type for who we are performing the
1277 initialization. For example, if W is a virtual base class of A and B,
1279 If we are initializing B, then W must contain B's W vtable, whereas
1280 were we initializing C, W must contain C's W vtable.
1282 TRUE_EXP is nonzero if it is the true expression being initialized.
1283 In this case, it may be EXP, or may just contain EXP. The reason we
1284 need this is because if EXP is a base element of TRUE_EXP, we
1285 don't necessarily know by looking at EXP where its virtual
1286 baseclass fields should really be pointing. But we do know
1287 from TRUE_EXP. In constructors, we don't know anything about
1288 the value being initialized.
1290 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1292 FLAGS is just passes to `build_method_call'. See that function for
1296 expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1303 tree type = TREE_TYPE (exp);
1304 tree init_type = NULL_TREE;
1306 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1308 /* Use a function returning the desired type to initialize EXP for us.
1309 If the function is a constructor, and its first argument is
1310 NULL_TREE, know that it was meant for us--just slide exp on
1311 in and expand the constructor. Constructors now come
1315 tree init_list = NULL_TREE;
1317 if (TREE_CODE (init) == TREE_LIST)
1320 if (TREE_CHAIN (init) == NULL_TREE)
1321 init = TREE_VALUE (init);
1324 init_type = TREE_TYPE (init);
1326 if (TREE_CODE (init) != TREE_LIST)
1328 if (TREE_CODE (init_type) == ERROR_MARK)
1332 /* These lines are found troublesome 5/11/89. */
1333 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1334 init_type = TREE_TYPE (init_type);
1337 /* This happens when we use C++'s functional cast notation.
1338 If the types match, then just use the TARGET_EXPR
1339 directly. Otherwise, we need to create the initializer
1340 separately from the object being initialized. */
1341 if (TREE_CODE (init) == TARGET_EXPR)
1343 if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type))
1345 if (TREE_CODE (exp) == VAR_DECL
1346 || TREE_CODE (exp) == RESULT_DECL)
1347 /* Unify the initialization targets. */
1348 DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1350 DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
1352 expand_expr_stmt (init);
1357 init = TREE_OPERAND (init, 1);
1358 init = build (CALL_EXPR, init_type,
1359 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1360 TREE_SIDE_EFFECTS (init) = 1;
1362 TREE_VALUE (init_list) = init;
1366 if (init_type == type && TREE_CODE (init) == CALL_EXPR
1368 /* It is valid to directly initialize from a CALL_EXPR
1369 without going through X(X&), apparently. */
1370 && ! TYPE_GETS_INIT_REF (type)
1374 /* A CALL_EXPR is a legitimate form of initialization, so
1375 we should not print this warning message. */
1377 /* Should have gone away due to 5/11/89 change. */
1378 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
1379 init = convert_from_reference (init);
1381 expand_assignment (exp, init, 0, 0);
1382 if (exp == DECL_RESULT (current_function_decl))
1384 /* Failing this assertion means that the return value
1385 from receives multiple initializations. */
1386 my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
1387 || DECL_INITIAL (exp) == error_mark_node,
1389 DECL_INITIAL (exp) = init;
1393 else if (init_type == type
1394 && TREE_CODE (init) == COND_EXPR)
1396 /* Push value to be initialized into the cond, where possible.
1397 Avoid spurious warning messages when initializing the
1398 result of this function. */
1399 TREE_OPERAND (init, 1)
1400 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1401 if (exp == DECL_RESULT (current_function_decl))
1402 DECL_INITIAL (exp) = NULL_TREE;
1403 TREE_OPERAND (init, 2)
1404 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1405 if (exp == DECL_RESULT (current_function_decl))
1406 DECL_INITIAL (exp) = init;
1407 TREE_SIDE_EFFECTS (init) = 1;
1408 expand_expr (init, const0_rtx, VOIDmode, 0);
1414 /* We did not know what we were initializing before. Now we do. */
1415 if (TREE_CODE (init) == TARGET_EXPR)
1417 tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1419 if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1420 && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1422 /* In order for this to work for RESULT_DECLs, if their
1423 type has a constructor, then they must be BLKmode
1424 so that they will be meaningfully addressable. */
1425 tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1426 init = TREE_OPERAND (init, 1);
1427 init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1428 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1429 TREE_SIDE_EFFECTS (init) = 1;
1430 TREE_VALUE (TREE_OPERAND (init, 1))
1431 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1435 expand_assignment (current_function_decl, init, 0, 0);
1438 if (exp == DECL_RESULT (current_function_decl))
1440 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1441 fatal ("return value from function receives multiple initializations");
1442 DECL_INITIAL (exp) = init;
1444 expand_expr_stmt (init);
1449 if (TREE_CODE (exp) == VAR_DECL
1450 && TREE_CODE (init) == CONSTRUCTOR
1451 && TREE_HAS_CONSTRUCTOR (init))
1453 tree t = store_init_value (exp, init);
1456 expand_decl_init (exp);
1459 t = build (INIT_EXPR, type, exp, init);
1460 TREE_SIDE_EFFECTS (t) = 1;
1461 expand_expr_stmt (t);
1465 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1466 which really means: xyzzy foo = bar; Ugh!
1468 More useful for this case: xyzzy *foo = new xyzzy (bar); */
1470 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1472 if (init_list && TREE_CHAIN (init_list))
1474 warning ("initializer list being treated as compound expression");
1475 init = convert (type, build_compound_expr (init_list));
1476 if (init == error_mark_node)
1480 expand_assignment (exp, init, 0, 0);
1484 /* See whether we can go through a type conversion operator.
1485 This wins over going through a non-existent constructor. If
1486 there is a constructor, it is ambiguous. */
1487 if (TREE_CODE (init) != TREE_LIST)
1489 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1490 ? TREE_TYPE (init_type) : init_type;
1492 if (ttype != type && IS_AGGR_TYPE (ttype))
1494 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1498 /* See if there is a constructor for``type'' that takes a
1499 ``ttype''-typed object. */
1500 tree parms = build_tree_list (NULL_TREE, init);
1501 tree as_cons = NULL_TREE;
1502 if (TYPE_HAS_CONSTRUCTOR (type))
1503 as_cons = build_method_call (exp, constructor_name_full (type),
1505 LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1506 if (as_cons != NULL_TREE && as_cons != error_mark_node)
1507 /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1508 cp_error ("ambiguity between conversion to `%T' and constructor",
1511 expand_assignment (exp, rval, 0, 0);
1518 /* Handle default copy constructors here, does not matter if there is
1519 a constructor or not. */
1520 if (type == init_type && IS_AGGR_TYPE (type)
1521 && init && TREE_CODE (init) != TREE_LIST)
1522 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1523 /* Not sure why this is here... */
1524 else if (TYPE_HAS_CONSTRUCTOR (type))
1525 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1526 else if (TREE_CODE (type) == ARRAY_TYPE)
1528 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1529 expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1530 else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1531 sorry ("arrays of objects with virtual functions but no constructors");
1534 expand_recursive_init (binfo, true_exp, exp, init,
1535 CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1538 /* A pointer which holds the initializer. First call to
1539 expand_aggr_init gets this value pointed to, and sets it to init_null. */
1540 static tree *init_ptr, init_null;
1542 /* Subroutine of expand_recursive_init:
1544 ADDR is the address of the expression being initialized.
1545 INIT_LIST is the cons-list of initializations to be performed.
1546 ALIAS_THIS is its same, lovable self. */
1548 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
1549 tree binfo, true_exp, addr;
1555 if (TREE_PURPOSE (init_list))
1557 if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1559 tree member = TREE_PURPOSE (init_list);
1560 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1561 tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1562 if (IS_AGGR_TYPE (TREE_TYPE (member)))
1563 expand_aggr_init (member_base, DECL_INITIAL (member), 0, 0);
1564 else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1565 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1567 member_base = save_expr (default_conversion (member_base));
1568 expand_vec_init (member, member_base,
1569 array_type_nelts (TREE_TYPE (member)),
1570 DECL_INITIAL (member), 0);
1573 expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1575 else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1577 expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1578 expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
1580 else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1582 /* Only initialize the virtual function tables if we
1583 are initializing the ultimate users of those vtables. */
1584 if (TREE_VALUE (init_list))
1586 /* We have to ensure that the first argment to
1587 expand_virtual_init is in binfo's hierarchy. */
1588 /* Is it the case that this is exactly the right binfo? */
1589 /* If it is ok, then fixup expand_virtual_init, to make
1591 expand_virtual_init (get_binfo (TREE_VALUE (init_list), binfo, 0),
1593 if (TREE_VALUE (init_list) == binfo
1594 && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1595 expand_indirect_vtbls_init (binfo, true_exp, addr, 1);
1599 my_friendly_abort (49);
1601 else if (TREE_VALUE (init_list)
1602 && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
1604 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1605 expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
1606 alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
1609 /* INIT_PTR is used up. */
1610 init_ptr = &init_null;
1613 my_friendly_abort (50);
1614 init_list = TREE_CHAIN (init_list);
1618 /* Initialize EXP with INIT. Type EXP does not have a constructor,
1619 but it has a baseclass with a constructor or a virtual function
1620 table which needs initializing.
1622 INIT_LIST is a cons-list describing what parts of EXP actually
1623 need to be initialized. INIT is given to the *unique*, first
1624 constructor within INIT_LIST. If there are multiple first
1625 constructors, such as with multiple inheritance, INIT must
1626 be zero or an ambiguity error is reported.
1628 ALIAS_THIS is passed from `expand_aggr_init'. See comments
1632 expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
1633 tree binfo, true_exp, exp, init;
1637 tree *old_init_ptr = init_ptr;
1638 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1641 if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1643 expand_aggr_vbase_init (binfo, exp, addr, init_list);
1644 expand_indirect_vtbls_init (binfo, true_exp, addr, 1);
1646 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
1650 tree type = TREE_TYPE (exp);
1652 if (TREE_CODE (type) == REFERENCE_TYPE)
1653 type = TREE_TYPE (type);
1654 if (IS_AGGR_TYPE (type))
1655 cp_error ("unexpected argument to constructor `%T'", type);
1657 error ("unexpected argument to constructor");
1659 init_ptr = old_init_ptr;
1662 /* Report an error if NAME is not the name of a user-defined,
1663 aggregate type. If OR_ELSE is nonzero, give an error message. */
1665 is_aggr_typedef (name, or_else)
1671 if (name == error_mark_node)
1674 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1675 type = IDENTIFIER_TYPE_VALUE (name);
1679 cp_error ("`%T' is not an aggregate typedef", name);
1683 if (! IS_AGGR_TYPE (type)
1684 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1687 cp_error ("`%T' is not an aggregate type", type);
1693 /* Like is_aggr_typedef, but returns typedef if successful. */
1695 get_aggr_from_typedef (name, or_else)
1701 if (name == error_mark_node)
1704 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1705 type = IDENTIFIER_TYPE_VALUE (name);
1709 cp_error ("`%T' fails to be an aggregate typedef", name);
1713 if (! IS_AGGR_TYPE (type)
1714 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1717 cp_error ("type `%T' is of non-aggregate type", type);
1724 get_type_value (name)
1727 if (name == error_mark_node)
1730 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1731 return IDENTIFIER_TYPE_VALUE (name);
1737 /* This code could just as well go in `class.c', but is placed here for
1740 /* For an expression of the form CNAME :: NAME (PARMLIST), build
1741 the appropriate function call. */
1743 build_member_call (cname, name, parmlist)
1744 tree cname, name, parmlist;
1747 tree method_name = name;
1749 int dont_use_this = 0;
1750 tree basetype_path, decl;
1752 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1754 method_name = TREE_OPERAND (method_name, 0);
1758 if (TREE_CODE (cname) == SCOPE_REF)
1759 cname = resolve_scope_to_name (NULL_TREE, cname);
1761 /* This shouldn't be here, and build_member_call shouldn't appear in
1763 if (cname && get_aggr_from_typedef (cname, 0) == 0
1764 && TREE_CODE (cname) == IDENTIFIER_NODE)
1766 tree ns = lookup_name (cname, 0);
1767 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1769 return build_x_function_call (build_offset_ref (cname, name), parmlist, current_class_decl);
1773 if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1)))
1774 return error_mark_node;
1776 /* An operator we did not like. */
1777 if (name == NULL_TREE)
1778 return error_mark_node;
1783 /* Everything can explicitly call a destructor; see 12.4 */
1784 if (! TYPE_HAS_DESTRUCTOR (type))
1785 cp_error ("type `%#T' does not have a destructor", type);
1788 cp_error ("cannot call destructor `%T::~%T' without object", type,
1790 return error_mark_node;
1793 /* No object? Then just fake one up, and let build_method_call
1794 figure out what to do. */
1795 if (current_class_type == 0
1796 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1801 basetype_path = TYPE_BINFO (type);
1802 decl = build1 (NOP_EXPR, build_pointer_type (type), error_mark_node);
1804 else if (current_class_decl == 0)
1807 decl = build1 (NOP_EXPR, build_pointer_type (type), error_mark_node);
1811 tree olddecl = current_class_decl;
1812 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1813 if (oldtype != type)
1815 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1816 TYPE_VOLATILE (oldtype));
1817 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
1823 decl = build_indirect_ref (decl, NULL_PTR);
1825 if (method_name == constructor_name (type)
1826 || method_name == constructor_name_full (type))
1827 return build_functional_cast (type, parmlist);
1828 if (t = lookup_fnfields (basetype_path, method_name, 0))
1829 return build_method_call (decl, method_name, parmlist, basetype_path,
1830 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1831 if (TREE_CODE (name) == IDENTIFIER_NODE
1832 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1834 if (t == error_mark_node)
1835 return error_mark_node;
1836 if (TREE_CODE (t) == FIELD_DECL)
1840 cp_error ("invalid use of non-static field `%D'", t);
1841 return error_mark_node;
1843 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1845 else if (TREE_CODE (t) == VAR_DECL)
1849 cp_error ("invalid use of member `%D'", t);
1850 return error_mark_node;
1852 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1853 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1854 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1855 return build_function_call (decl, parmlist);
1859 cp_error ("no method `%T::%D'", type, name);
1860 return error_mark_node;
1864 /* Build a reference to a member of an aggregate. This is not a
1865 C++ `&', but really something which can have its address taken,
1866 and then act as a pointer to member, for example CNAME :: FIELD
1867 can have its address taken by saying & CNAME :: FIELD.
1869 @@ Prints out lousy diagnostics for operator <typename>
1872 @@ This function should be rewritten and placed in search.c. */
1874 build_offset_ref (cname, name)
1877 tree decl, type, fnfields, fields, t = error_mark_node;
1878 tree basetypes = NULL_TREE;
1881 if (TREE_CODE (cname) == SCOPE_REF)
1882 cname = resolve_scope_to_name (NULL_TREE, cname);
1884 /* Handle namespace names fully here. */
1885 if (TREE_CODE (cname) == IDENTIFIER_NODE
1886 && get_aggr_from_typedef (cname, 0) == 0)
1888 tree ns = lookup_name (cname, 0);
1890 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1892 val = lookup_namespace_name (ns, name);
1895 cp_error ("namespace `%D' has no member named `%D'", ns, name);
1896 return error_mark_node;
1900 if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
1901 return error_mark_node;
1903 type = IDENTIFIER_TYPE_VALUE (cname);
1905 if (TREE_CODE (name) == BIT_NOT_EXPR)
1908 name = TREE_OPERAND (name, 0);
1911 if (TYPE_SIZE (type) == 0)
1913 t = IDENTIFIER_CLASS_VALUE (name);
1916 cp_error ("incomplete type `%T' does not have member `%D'", type,
1918 return error_mark_node;
1920 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
1921 || TREE_CODE (t) == CONST_DECL)
1926 if (TREE_CODE (t) == FIELD_DECL)
1927 sorry ("use of member in incomplete aggregate type");
1928 else if (TREE_CODE (t) == FUNCTION_DECL)
1929 sorry ("use of member function in incomplete aggregate type");
1931 my_friendly_abort (52);
1932 return error_mark_node;
1936 if (TREE_CODE (name) == TYPE_EXPR)
1937 /* Pass a TYPE_DECL to build_component_type_expr. */
1938 return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
1939 name, NULL_TREE, 1);
1942 if (current_class_type == 0
1943 || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
1945 basetypes = TYPE_BINFO (type);
1946 decl = build1 (NOP_EXPR,
1947 IDENTIFIER_TYPE_VALUE (cname),
1950 else if (current_class_decl == 0)
1951 decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
1956 fnfields = lookup_fnfields (basetypes, name, 1);
1957 fields = lookup_field (basetypes, name, 0, 0);
1959 if (fields == error_mark_node || fnfields == error_mark_node)
1960 return error_mark_node;
1962 /* A lot of this logic is now handled in lookup_field and
1966 basetypes = TREE_PURPOSE (fnfields);
1968 /* Go from the TREE_BASELINK to the member function info. */
1969 t = TREE_VALUE (fnfields);
1973 if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
1975 error ("ambiguous member reference: member `%s' defined as both field and function",
1976 IDENTIFIER_POINTER (name));
1977 return error_mark_node;
1979 if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
1981 else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
1985 error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
1986 return error_mark_node;
1990 if (t == TREE_VALUE (fnfields))
1992 extern int flag_save_memoized_contexts;
1994 if (DECL_CHAIN (t) == NULL_TREE || dtor)
1996 enum access_type access;
1998 /* unique functions are handled easily. */
2000 access = compute_access (basetypes, t);
2001 if (access == access_protected)
2003 cp_error_at ("member function `%#D' is protected", t);
2004 error ("in this context");
2005 return error_mark_node;
2007 if (access == access_private)
2009 cp_error_at ("member function `%#D' is private", t);
2010 error ("in this context");
2011 return error_mark_node;
2013 assemble_external (t);
2014 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
2017 /* overloaded functions may need more work. */
2020 if (TYPE_HAS_DESTRUCTOR (type)
2021 && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
2027 /* FNFIELDS is most likely allocated on the search_obstack,
2028 which will go away after this class scope. If we need
2029 to save this value for later (either for memoization
2030 or for use as an initializer for a static variable), then
2033 ??? The smart thing to do for the case of saving initializers
2034 is to resolve them before we're done with this scope. */
2035 if (!TREE_PERMANENT (fnfields)
2036 && ((flag_save_memoized_contexts && global_bindings_p ())
2037 || ! allocation_temporary_p ()))
2038 fnfields = copy_list (fnfields);
2040 for (t = TREE_VALUE (fnfields); t; t = DECL_CHAIN (t))
2041 assemble_external (t);
2043 t = build_tree_list (error_mark_node, fnfields);
2044 TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
2049 /* Now that we know we are looking for a field, see if we
2050 have access to that field. Lookup_field will give us the
2053 t = lookup_field (basetypes, name, 1, 0);
2055 if (t == error_mark_node)
2056 return error_mark_node;
2060 cp_error ("`%D' is not a member of type `%T'", name, type);
2061 return error_mark_node;
2064 if (TREE_CODE (t) == TYPE_DECL)
2069 /* static class members and class-specific enum
2070 values can be returned without further ado. */
2071 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
2073 assemble_external (t);
2078 if (TREE_CODE (t) == FIELD_DECL && DECL_BIT_FIELD (t))
2080 cp_error ("illegal pointer to bit field `%D'", t);
2081 return error_mark_node;
2084 /* static class functions too. */
2085 if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2086 my_friendly_abort (53);
2088 /* In member functions, the form `cname::name' is no longer
2089 equivalent to `this->cname::name'. */
2090 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2093 /* Given an object EXP and a member function reference MEMBER,
2094 return the address of the actual member function. */
2096 get_member_function (exp_addr_ptr, exp, member)
2100 tree ctype = TREE_TYPE (exp);
2101 tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
2103 if (TYPE_VIRTUAL_P (ctype)
2104 || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
2109 /* Save away the unadulterated `this' pointer. */
2110 exp_addr = save_expr (*exp_addr_ptr);
2112 /* Cast function to signed integer. */
2113 e0 = build1 (NOP_EXPR, integer_type_node, function);
2115 /* There is a hack here that takes advantage of
2116 twos complement arithmetic, and the fact that
2117 there are more than one UNITS to the WORD.
2118 If the high bit is set for the `function',
2119 then we pretend it is a virtual function,
2120 and the array indexing will knock this bit
2121 out the top, leaving a valid index. */
2122 if (UNITS_PER_WORD <= 1)
2123 my_friendly_abort (54);
2125 e1 = build (GT_EXPR, boolean_type_node, e0, integer_zero_node);
2126 e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
2127 build_tree_list (NULL_TREE, e1)));
2128 e1 = save_expr (e1);
2130 if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
2132 exp = build_indirect_ref (exp_addr, NULL_PTR);
2133 *exp_addr_ptr = exp_addr;
2136 /* This is really hairy: if the function pointer is a pointer
2137 to a non-virtual member function, then we can't go mucking
2138 with the `this' pointer (any more than we already have to
2139 this point). If it is a pointer to a virtual member function,
2140 then we have to adjust the `this' pointer according to
2141 what the virtual function table tells us. */
2143 e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
2144 my_friendly_assert (e3 != error_mark_node, 213);
2146 /* Change this pointer type from `void *' to the
2147 type it is really supposed to be. */
2148 TREE_TYPE (e3) = TREE_TYPE (function);
2150 /* If non-virtual, use what we had originally. Otherwise,
2151 use the value we get from the virtual function table. */
2152 *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
2154 function = build_conditional_expr (e1, function, e3);
2156 return build_indirect_ref (function, NULL_PTR);
2159 /* If a OFFSET_REF made it through to here, then it did
2160 not have its address taken. */
2163 resolve_offset_ref (exp)
2166 tree type = TREE_TYPE (exp);
2167 tree base = NULL_TREE;
2169 tree basetype, addr;
2171 if (TREE_CODE (exp) == TREE_LIST)
2172 return build_unary_op (ADDR_EXPR, exp, 0);
2174 if (TREE_CODE (exp) != OFFSET_REF)
2176 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2177 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2179 error ("object missing in use of pointer-to-member construct");
2180 return error_mark_node;
2183 type = TREE_TYPE (type);
2188 member = TREE_OPERAND (exp, 1);
2189 base = TREE_OPERAND (exp, 0);
2192 if ((TREE_CODE (member) == VAR_DECL
2193 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2194 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
2196 /* These were static members. */
2197 if (mark_addressable (member) == 0)
2198 return error_mark_node;
2202 /* Syntax error can cause a member which should
2203 have been seen as static to be grok'd as non-static. */
2204 if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
2206 if (TREE_ADDRESSABLE (member) == 0)
2208 cp_error_at ("member `%D' is non-static but referenced as a static member",
2210 error ("at this point in file");
2211 TREE_ADDRESSABLE (member) = 1;
2213 return error_mark_node;
2216 /* The first case is really just a reference to a member of `this'. */
2217 if (TREE_CODE (member) == FIELD_DECL
2219 || (TREE_CODE (base) == NOP_EXPR
2220 && TREE_OPERAND (base, 0) == error_mark_node)))
2223 enum access_type access;
2225 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
2226 basetype = TYPE_OFFSET_BASETYPE (type);
2228 basetype = DECL_CONTEXT (member);
2230 base = current_class_decl;
2232 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2234 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2235 return error_mark_node;
2237 addr = convert_pointer_to (basetype, base);
2238 access = compute_access (basetype_path, member);
2239 if (access == access_public)
2240 return build (COMPONENT_REF, TREE_TYPE (member),
2241 build_indirect_ref (addr, NULL_PTR), member);
2242 if (access == access_protected)
2244 cp_error_at ("member `%D' is protected", member);
2245 error ("in this context");
2246 return error_mark_node;
2248 if (access == access_private)
2250 cp_error_at ("member `%D' is private", member);
2251 error ("in this context");
2252 return error_mark_node;
2254 my_friendly_abort (55);
2257 /* If this is a reference to a member function, then return
2258 the address of the member function (which may involve going
2259 through the object's vtable), otherwise, return an expression
2260 for the dereferenced pointer-to-member construct. */
2261 addr = build_unary_op (ADDR_EXPR, base, 0);
2263 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2265 basetype = DECL_CLASS_CONTEXT (member);
2266 addr = convert_pointer_to (basetype, addr);
2267 return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0);
2269 else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2271 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2272 addr = convert_pointer_to (basetype, addr);
2273 member = convert (ptrdiff_type_node,
2274 build_unary_op (ADDR_EXPR, member, 0));
2275 return build1 (INDIRECT_REF, type,
2276 build (PLUS_EXPR, build_pointer_type (type),
2279 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2281 return get_member_function_from_ptrfunc (&addr, member);
2283 my_friendly_abort (56);
2288 /* Return either DECL or its known constant value (if it has one). */
2291 decl_constant_value (decl)
2294 if (! TREE_THIS_VOLATILE (decl)
2296 /* These may be necessary for C, but they break C++. */
2297 ! TREE_PUBLIC (decl)
2298 /* Don't change a variable array bound or initial value to a constant
2299 in a place where a variable is invalid. */
2302 && DECL_INITIAL (decl) != 0
2303 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2304 /* This is invalid if initial value is not constant.
2305 If it has either a function call, a memory reference,
2306 or a variable, then re-evaluating it could give different results. */
2307 && TREE_CONSTANT (DECL_INITIAL (decl))
2308 /* Check for cases where this is sub-optimal, even though valid. */
2309 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2311 /* We must allow this to work outside of functions so that
2312 static constants can be used for array sizes. */
2313 && current_function_decl != 0
2314 && DECL_MODE (decl) != BLKmode
2317 return DECL_INITIAL (decl);
2321 /* Friend handling routines. */
2322 /* Friend data structures:
2324 Lists of friend functions come from TYPE_DECL nodes. Since all
2325 aggregate types are automatically typedef'd, these nodes are guaranteed
2328 The TREE_PURPOSE of a friend list is the name of the friend,
2329 and its TREE_VALUE is another list.
2331 For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2332 will be filled in, but not both. The TREE_VALUE of that list is an
2333 individual function which is a friend. The TREE_PURPOSE of that list
2334 indicates a type in which all functions by that name are friends.
2336 Lists of friend classes come from _TYPE nodes. Love that consistency
2340 is_friend_type (type1, type2)
2343 return is_friend (type1, type2);
2347 is_friend (type, supplicant)
2348 tree type, supplicant;
2353 if (supplicant == NULL_TREE || type == NULL_TREE)
2356 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2359 /* It's a function decl. */
2361 tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2362 tree name = DECL_NAME (supplicant);
2365 if (DECL_FUNCTION_MEMBER_P (supplicant))
2366 ctype = DECL_CLASS_CONTEXT (supplicant);
2370 for (; list ; list = TREE_CHAIN (list))
2372 if (name == TREE_PURPOSE (list))
2374 tree friends = TREE_VALUE (list);
2375 name = DECL_ASSEMBLER_NAME (supplicant);
2376 for (; friends ; friends = TREE_CHAIN (friends))
2378 if (ctype == TREE_PURPOSE (friends))
2380 if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
2390 if (type == supplicant)
2393 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2394 for (; list ; list = TREE_CHAIN (list))
2395 if (supplicant == TREE_VALUE (list))
2403 context = DECL_CONTEXT (TYPE_NAME (supplicant));
2404 else if (DECL_FUNCTION_MEMBER_P (supplicant))
2405 context = DECL_CLASS_CONTEXT (supplicant);
2407 context = NULL_TREE;
2410 return is_friend (type, context);
2416 /* Add a new friend to the friends of the aggregate type TYPE.
2417 DECL is the FUNCTION_DECL of the friend being added. */
2419 add_friend (type, decl)
2422 tree typedecl = TYPE_NAME (type);
2423 tree list = DECL_FRIENDLIST (typedecl);
2424 tree name = DECL_NAME (decl);
2428 if (name == TREE_PURPOSE (list))
2430 tree friends = TREE_VALUE (list);
2431 for (; friends ; friends = TREE_CHAIN (friends))
2433 if (decl == TREE_VALUE (friends))
2435 cp_warning ("`%D' is already a friend of class `%T'",
2437 cp_warning_at ("previous friend declaration of `%D'",
2438 TREE_VALUE (friends));
2442 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2446 list = TREE_CHAIN (list);
2448 DECL_FRIENDLIST (typedecl)
2449 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2450 DECL_FRIENDLIST (typedecl));
2451 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2453 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2454 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2455 if (parmtypes && TREE_CHAIN (parmtypes))
2457 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2458 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2459 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2460 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2465 /* Declare that every member function NAME in FRIEND_TYPE
2466 (which may be NULL_TREE) is a friend of type TYPE. */
2468 add_friends (type, name, friend_type)
2469 tree type, name, friend_type;
2471 tree typedecl = TYPE_NAME (type);
2472 tree list = DECL_FRIENDLIST (typedecl);
2476 if (name == TREE_PURPOSE (list))
2478 tree friends = TREE_VALUE (list);
2479 while (friends && TREE_PURPOSE (friends) != friend_type)
2480 friends = TREE_CHAIN (friends);
2483 warning ("method `%s::%s' is already a friend of class",
2484 TYPE_NAME_STRING (friend_type),
2485 IDENTIFIER_POINTER (name));
2487 warning ("function `%s' is already a friend of class `%s'",
2488 IDENTIFIER_POINTER (name),
2489 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2491 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2495 list = TREE_CHAIN (list);
2497 DECL_FRIENDLIST (typedecl) =
2499 build_tree_list (friend_type, NULL_TREE),
2500 DECL_FRIENDLIST (typedecl));
2501 if (! strncmp (IDENTIFIER_POINTER (name),
2502 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2503 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2505 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2506 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2510 /* Set up a cross reference so that type TYPE will make member function
2511 CTYPE::DECL a friend when CTYPE is finally defined. For more than
2512 one, set up a cross reference so that functions with the name DECL
2513 and type CTYPE know that they are friends of TYPE. */
2515 xref_friend (type, decl, ctype)
2516 tree type, decl, ctype;
2518 tree friend_decl = TYPE_NAME (ctype);
2520 tree typedecl = TYPE_NAME (type);
2521 tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2523 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2527 SET_DECL_WAITING_FRIENDS (friend_decl,
2529 DECL_WAITING_FRIENDS (friend_decl)));
2530 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2533 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2534 been defined, we make all of its member functions friends of
2535 TYPE. If not, we make it a pending friend, which can later be added
2536 when its definition is seen. If a type is defined, then its TYPE_DECL's
2537 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2538 classes that are not defined. If a type has not yet been defined,
2539 then the DECL_WAITING_FRIENDS contains a list of types
2540 waiting to make it their friend. Note that these two can both
2541 be in use at the same time! */
2543 make_friend_class (type, friend_type)
2544 tree type, friend_type;
2548 if (IS_SIGNATURE (type))
2550 error ("`friend' declaration in signature definition");
2553 if (IS_SIGNATURE (friend_type))
2555 error ("signature type `%s' declared `friend'",
2556 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2559 if (type == friend_type)
2561 pedwarn ("class `%s' is implicitly friends with itself",
2562 TYPE_NAME_STRING (type));
2566 GNU_xref_hier (TYPE_NAME_STRING (type),
2567 TYPE_NAME_STRING (friend_type), 0, 0, 1);
2569 classes = CLASSTYPE_FRIEND_CLASSES (type);
2570 while (classes && TREE_VALUE (classes) != friend_type)
2571 classes = TREE_CHAIN (classes);
2573 warning ("class `%s' is already friends with class `%s'",
2574 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2577 CLASSTYPE_FRIEND_CLASSES (type)
2578 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2582 /* Main friend processor. This is large, and for modularity purposes,
2583 has been removed from grokdeclarator. It returns `void_type_node'
2584 to indicate that something happened, though a FIELD_DECL is
2587 CTYPE is the class this friend belongs to.
2589 DECLARATOR is the name of the friend.
2591 DECL is the FUNCTION_DECL that the friend is.
2593 In case we are parsing a friend which is part of an inline
2594 definition, we will need to store PARM_DECL chain that comes
2595 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2597 FLAGS is just used for `grokclassfn'.
2599 QUALS say what special qualifies should apply to the object
2600 pointed to by `this'. */
2602 do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2603 tree ctype, declarator, decl, parmdecls;
2604 enum overload_flags flags;
2607 /* Every decl that gets here is a friend of something. */
2608 DECL_FRIEND_P (decl) = 1;
2612 tree cname = TYPE_NAME (ctype);
2613 if (TREE_CODE (cname) == TYPE_DECL)
2614 cname = DECL_NAME (cname);
2616 /* A method friend. */
2617 if (TREE_CODE (decl) == FUNCTION_DECL)
2619 if (flags == NO_SPECIAL && ctype && declarator == cname)
2620 DECL_CONSTRUCTOR_P (decl) = 1;
2622 /* This will set up DECL_ARGUMENTS for us. */
2623 grokclassfn (ctype, cname, decl, flags, quals);
2624 if (TYPE_SIZE (ctype) != 0)
2625 check_classfn (ctype, cname, decl);
2627 if (TREE_TYPE (decl) != error_mark_node)
2629 if (TYPE_SIZE (ctype))
2631 /* We don't call pushdecl here yet, or ever on this
2632 actual FUNCTION_DECL. We must preserve its TREE_CHAIN
2634 make_decl_rtl (decl, NULL_PTR, 1);
2635 add_friend (current_class_type, decl);
2639 register char *classname
2640 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype)));
2642 error ("member declared as friend before type `%s' defined",
2649 /* Possibly a bunch of method friends. */
2651 /* Get the class they belong to. */
2652 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2654 /* This class is defined, use its methods now. */
2655 if (TYPE_SIZE (ctype))
2657 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2659 add_friends (current_class_type, declarator, ctype);
2661 error ("method `%s' is not a member of class `%s'",
2662 IDENTIFIER_POINTER (declarator),
2663 IDENTIFIER_POINTER (cname));
2666 /* Note: DECLARATOR actually has more than one; in this
2667 case, we're making sure that fns with the name DECLARATOR
2668 and type CTYPE know they are friends of the current
2670 xref_friend (current_class_type, declarator, ctype);
2671 decl = void_type_node;
2674 else if (TREE_CODE (decl) == FUNCTION_DECL
2675 && ((IDENTIFIER_LENGTH (declarator) == 4
2676 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2677 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2678 || (IDENTIFIER_LENGTH (declarator) > 10
2679 && IDENTIFIER_POINTER (declarator)[0] == '_'
2680 && IDENTIFIER_POINTER (declarator)[1] == '_'
2681 && strncmp (IDENTIFIER_POINTER (declarator)+2,
2682 "builtin_", 8) == 0)))
2684 /* raw "main", and builtin functions never gets overloaded,
2685 but they can become friends. */
2686 add_friend (current_class_type, decl);
2687 DECL_FRIEND_P (decl) = 1;
2688 decl = void_type_node;
2691 @@ or possibly a friend from a base class ?!? */
2692 else if (TREE_CODE (decl) == FUNCTION_DECL)
2694 /* Friends must all go through the overload machinery,
2695 even though they may not technically be overloaded.
2697 Note that because classes all wind up being top-level
2698 in their scope, their friend wind up in top-level scope as well. */
2699 DECL_ASSEMBLER_NAME (decl)
2700 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2701 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2702 DECL_ARGUMENTS (decl) = parmdecls;
2703 DECL_CLASS_CONTEXT (decl) = current_class_type;
2705 /* We can call pushdecl here, because the TREE_CHAIN of this
2706 FUNCTION_DECL is not needed for other purposes. */
2707 decl = pushdecl (decl);
2709 make_decl_rtl (decl, NULL_PTR, 1);
2710 add_friend (current_class_type, decl);
2712 DECL_FRIEND_P (decl) = 1;
2714 TREE_OVERLOADED (declarator) = 1;
2719 /* @@ Should be able to ingest later definitions of this function
2721 tree decl = lookup_name_nonclass (declarator);
2722 if (decl == NULL_TREE)
2724 warning ("implicitly declaring `%s' as struct",
2725 IDENTIFIER_POINTER (declarator));
2726 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2727 decl = TYPE_NAME (decl);
2730 /* Allow abbreviated declarations of overloaded functions,
2731 but not if those functions are really class names. */
2732 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2734 warning ("`friend %s' archaic, use `friend class %s' instead",
2735 IDENTIFIER_POINTER (declarator),
2736 IDENTIFIER_POINTER (declarator));
2737 decl = TREE_TYPE (TREE_PURPOSE (decl));
2740 if (TREE_CODE (decl) == TREE_LIST)
2741 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2743 make_friend_class (current_class_type, TREE_TYPE (decl));
2744 decl = void_type_node;
2749 /* TYPE has now been defined. It may, however, have a number of things
2750 waiting make make it their friend. We resolve these references
2753 embrace_waiting_friends (type)
2756 tree decl = TYPE_NAME (type);
2759 if (TREE_CODE (decl) != TYPE_DECL)
2762 for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2763 waiters = TREE_CHAIN (waiters))
2765 tree waiter = TREE_PURPOSE (waiters);
2767 tree waiter_prev = TREE_VALUE (waiters);
2769 tree decl = TREE_TYPE (waiters);
2770 tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2771 ? decl : DECL_NAME (decl)) : NULL_TREE;
2774 /* @@ There may be work to be done since we have not verified
2775 @@ consistency between original and friend declarations
2776 @@ of the functions waiting to become friends. */
2777 tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
2780 add_friends (waiter, name, type);
2782 add_friend (waiter, decl);
2784 error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2785 DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2786 "no method `%s' defined in class `%s' to be friend",
2787 IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
2788 TYPE_NAME_STRING (type));
2791 make_friend_class (type, waiter);
2794 if (TREE_CHAIN (waiter_prev))
2795 TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2797 DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2802 /* Common subroutines of build_new and build_vec_delete. */
2804 /* Common interface for calling "builtin" functions that are not
2808 build_builtin_call (type, node, arglist)
2813 tree rval = build (CALL_EXPR, type, node, arglist, 0);
2814 TREE_SIDE_EFFECTS (rval) = 1;
2815 assemble_external (TREE_OPERAND (node, 0));
2816 TREE_USED (TREE_OPERAND (node, 0)) = 1;
2820 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2821 (which needs to go through some sort of groktypename) or it
2822 is the name of the class we are newing. INIT is an initialization value.
2823 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2824 If INIT is void_type_node, it means do *not* call a constructor
2827 For types with constructors, the data returned is initialized
2828 by the appropriate constructor.
2830 Whether the type has a constructor or not, if it has a pointer
2831 to a virtual function table, then that pointer is set up
2834 Unless I am mistaken, a call to new () will return initialized
2835 data regardless of whether the constructor itself is private or
2836 not. NOPE; new fails if the constructor is private (jcm).
2838 Note that build_new does nothing to assure that any special
2839 alignment requirements of the type are met. Rather, it leaves
2840 it up to malloc to do the right thing. Otherwise, folding to
2841 the right alignment cal cause problems if the user tries to later
2842 free the memory returned by `new'.
2844 PLACEMENT is the `placement' list for user-defined operator new (). */
2846 extern int flag_check_new;
2849 build_new (placement, decl, init, use_global_new)
2854 tree type, true_type, size, rval;
2856 tree alloc_expr, alloc_temp;
2858 enum tree_code code = NEW_EXPR;
2860 tree pending_sizes = NULL_TREE;
2862 if (decl == error_mark_node)
2863 return error_mark_node;
2865 if (TREE_CODE (decl) == TREE_LIST)
2867 tree absdcl = TREE_VALUE (decl);
2868 tree last_absdcl = NULL_TREE;
2869 int old_immediate_size_expand;
2871 if (current_function_decl
2872 && DECL_CONSTRUCTOR_P (current_function_decl))
2874 old_immediate_size_expand = immediate_size_expand;
2875 immediate_size_expand = 0;
2878 nelts = integer_one_node;
2880 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
2881 my_friendly_abort (215);
2882 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2884 last_absdcl = absdcl;
2885 absdcl = TREE_OPERAND (absdcl, 0);
2888 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
2890 /* probably meant to be a vec new */
2893 while (TREE_OPERAND (absdcl, 0)
2894 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
2896 last_absdcl = absdcl;
2897 absdcl = TREE_OPERAND (absdcl, 0);
2901 this_nelts = TREE_OPERAND (absdcl, 1);
2902 if (this_nelts != error_mark_node)
2904 if (this_nelts == NULL_TREE)
2905 error ("new of array type fails to specify size");
2908 this_nelts = save_expr (convert (sizetype, this_nelts));
2909 absdcl = TREE_OPERAND (absdcl, 0);
2910 if (this_nelts == integer_zero_node)
2912 warning ("zero size array reserves no space");
2913 nelts = integer_zero_node;
2916 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
2920 nelts = integer_zero_node;
2924 TREE_OPERAND (last_absdcl, 0) = absdcl;
2926 TREE_VALUE (decl) = absdcl;
2928 type = true_type = groktypename (decl);
2929 if (! type || type == error_mark_node)
2931 immediate_size_expand = old_immediate_size_expand;
2932 return error_mark_node;
2935 if (current_function_decl
2936 && DECL_CONSTRUCTOR_P (current_function_decl))
2938 pending_sizes = get_pending_sizes ();
2939 immediate_size_expand = old_immediate_size_expand;
2942 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2944 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2946 /* An aggregate type. */
2947 type = IDENTIFIER_TYPE_VALUE (decl);
2948 decl = TYPE_NAME (type);
2952 /* A builtin type. */
2953 decl = lookup_name (decl, 1);
2954 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2955 type = TREE_TYPE (decl);
2959 else if (TREE_CODE (decl) == TYPE_DECL)
2961 type = TREE_TYPE (decl);
2968 decl = TYPE_NAME (type);
2971 /* ``A reference cannot be created by the new operator. A reference
2972 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2973 returned by new.'' ARM 5.3.3 */
2974 if (TREE_CODE (type) == REFERENCE_TYPE)
2976 error ("new cannot be applied to a reference type");
2977 type = true_type = TREE_TYPE (type);
2980 if (TREE_CODE (type) == FUNCTION_TYPE)
2982 error ("new cannot be applied to a function type");
2983 return error_mark_node;
2986 /* When the object being created is an array, the new-expression yields a
2987 pointer to the initial element (if any) of the array. For example,
2988 both new int and new int[10] return an int*. 5.3.4. */
2989 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2991 nelts = array_type_nelts_top (type);
2993 type = true_type = TREE_TYPE (type);
2996 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
2997 type = TYPE_MAIN_VARIANT (type);
2999 /* If our base type is an array, then make sure we know how many elements
3001 while (TREE_CODE (true_type) == ARRAY_TYPE)
3003 tree this_nelts = array_type_nelts_top (true_type);
3004 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3005 true_type = TREE_TYPE (true_type);
3008 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
3011 size = size_in_bytes (type);
3013 if (true_type == void_type_node)
3015 error ("invalid type `void' for new");
3016 return error_mark_node;
3019 if (TYPE_SIZE (true_type) == 0)
3021 incomplete_type_error (0, true_type);
3022 return error_mark_node;
3025 if (TYPE_LANG_SPECIFIC (true_type)
3026 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
3028 abstract_virtuals_error (NULL_TREE, true_type);
3029 return error_mark_node;
3032 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
3034 signature_error (NULL_TREE, true_type);
3035 return error_mark_node;
3038 /* Get a little extra space to store a couple of things before the new'ed
3040 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
3042 tree extra = BI_header_size;
3044 size = size_binop (PLUS_EXPR, size, extra);
3048 code = VEC_NEW_EXPR;
3050 /* Allocate the object. */
3051 if (! use_global_new && TYPE_LANG_SPECIFIC (true_type)
3052 && (TYPE_GETS_NEW (true_type) & (1 << has_array)))
3053 rval = build_opfncall (code, LOOKUP_NORMAL,
3054 build_pointer_type (true_type), size, placement);
3057 rval = build_opfncall (code, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
3058 ptr_type_node, size, placement);
3059 rval = convert (build_pointer_type (true_type), rval);
3061 else if (! has_array && flag_this_is_variable > 0
3062 && TYPE_NEEDS_CONSTRUCTING (true_type) && init != void_type_node)
3064 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
3068 error ("constructors take parameter lists");
3069 return error_mark_node;
3074 rval = build_builtin_call (build_pointer_type (true_type),
3075 has_array ? BIVN : BIN,
3076 build_tree_list (NULL_TREE, size));
3078 /* See comment above as to why this is disabled. */
3081 rval = build (PLUS_EXPR, build_pointer_type (true_type), rval,
3083 rval = build (BIT_AND_EXPR, build_pointer_type (true_type),
3084 rval, build1 (BIT_NOT_EXPR, integer_type_node,
3088 TREE_CALLS_NEW (rval) = 1;
3091 if (flag_check_new && rval)
3093 /* For array new, we need to make sure that the call to new is
3094 not expanded as part of the RTL_EXPR for the initialization,
3095 so we can't just use save_expr here. */
3097 alloc_temp = get_temp_name (TREE_TYPE (rval), 0);
3098 alloc_expr = build (MODIFY_EXPR, TREE_TYPE (rval), alloc_temp, rval);
3099 TREE_SIDE_EFFECTS (alloc_expr) = 1;
3103 alloc_expr = NULL_TREE;
3105 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
3106 sure we have some extra bytes in that case for the BI_header_size
3107 cookies? And how does that interact with the code below? (mrs) */
3108 /* Finish up some magic for new'ed arrays */
3109 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type) && rval != NULL_TREE)
3111 tree extra = BI_header_size;
3113 rval = convert (ptr_type_node, rval); /* convert to void * first */
3114 rval = convert (string_type_node, rval); /* lets not add void* and ints */
3115 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
3116 /* Store header info. */
3117 cookie = build_indirect_ref (build (MINUS_EXPR, build_pointer_type (BI_header_type),
3118 rval, extra), NULL_PTR);
3119 exp1 = build (MODIFY_EXPR, void_type_node,
3120 build_component_ref (cookie, nc_nelts_field_id, 0, 0),
3122 TREE_SIDE_EFFECTS (exp1) = 1;
3123 rval = convert (build_pointer_type (true_type), rval);
3124 TREE_CALLS_NEW (rval) = 1;
3125 TREE_SIDE_EFFECTS (rval) = 1;
3126 rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
3127 build_tree_list (NULL_TREE, rval)));
3130 if (rval == error_mark_node)
3131 return error_mark_node;
3133 /* Don't call any constructors or do any initialization. */
3134 if (init == void_type_node)
3137 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
3139 if (! TYPE_NEEDS_CONSTRUCTING (type)
3140 && ! IS_AGGR_TYPE (type) && ! has_array)
3142 /* New 2.0 interpretation: `new int (10)' means
3143 allocate an int, and initialize it with 10. */
3146 rval = save_expr (rval);
3147 deref = build_indirect_ref (rval, NULL_PTR);
3148 TREE_READONLY (deref) = 0;
3150 if (TREE_CHAIN (init) != NULL_TREE)
3151 pedwarn ("initializer list being treated as compound expression");
3152 else if (TREE_CODE (init) == CONSTRUCTOR)
3154 pedwarn ("initializer list appears where operand should be used");
3155 init = TREE_OPERAND (init, 1);
3157 init = build_compound_expr (init);
3159 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
3160 "new", NULL_TREE, 0);
3161 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
3162 build_modify_expr (deref, NOP_EXPR, init),
3164 TREE_NO_UNUSED_WARNING (rval) = 1;
3165 TREE_SIDE_EFFECTS (rval) = 1;
3166 TREE_CALLS_NEW (rval) = 1;
3168 else if (! has_array)
3171 /* Constructors are never virtual. If it has an initialization, we
3172 need to complain if we aren't allowed to use the ctor that took
3174 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
3176 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
3178 init = tree_cons (NULL_TREE, integer_one_node, init);
3179 flags |= LOOKUP_HAS_IN_CHARGE;
3184 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
3185 newrval = build_indirect_ref (newrval, NULL_PTR);
3187 newrval = build_method_call (newrval, constructor_name_full (true_type),
3188 init, NULL_TREE, flags);
3193 TREE_HAS_CONSTRUCTOR (rval) = 1;
3196 rval = error_mark_node;
3198 else if (current_function_decl == NULL_TREE)
3200 extern tree static_aggregates;
3202 /* In case of static initialization, SAVE_EXPR is good enough. */
3203 rval = save_expr (rval);
3204 rval = copy_to_permanent (rval);
3205 init = copy_to_permanent (init);
3206 init = expand_vec_init (decl, rval,
3207 build_binary_op (MINUS_EXPR, nelts,
3208 integer_one_node, 1),
3210 init = copy_to_permanent (init);
3211 static_aggregates = perm_tree_cons (init, rval, static_aggregates);
3215 /* Have to wrap this in RTL_EXPR for two cases:
3216 in base or member initialization and if we
3217 are a branch of a ?: operator. Since we
3218 can't easily know the latter, just do it always. */
3219 tree xval = make_node (RTL_EXPR);
3221 /* If we want to check the value of the allocation expression,
3222 and the number of elements in the array is not a constant, we
3223 *must* expand the SAVE_EXPR for nelts in alloc_expr before we
3224 expand it in the actual initialization. So we need to build up
3225 an RTL_EXPR for alloc_expr. Sigh. */
3226 if (alloc_expr && ! TREE_CONSTANT (nelts))
3228 tree xval = make_node (RTL_EXPR);
3230 TREE_TYPE (xval) = TREE_TYPE (alloc_expr);
3231 do_pending_stack_adjust ();
3232 start_sequence_for_rtl_expr (xval);
3234 rtxval = expand_expr (alloc_expr, NULL, VOIDmode, 0);
3235 do_pending_stack_adjust ();
3236 TREE_SIDE_EFFECTS (xval) = 1;
3237 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3239 RTL_EXPR_RTL (xval) = rtxval;
3240 TREE_TYPE (xval) = TREE_TYPE (alloc_expr);
3244 TREE_TYPE (xval) = TREE_TYPE (rval);
3245 do_pending_stack_adjust ();
3246 start_sequence_for_rtl_expr (xval);
3248 /* As a matter of principle, `start_sequence' should do this. */
3251 rval = save_expr (rval);
3252 rval = expand_vec_init (decl, rval,
3253 build_binary_op (MINUS_EXPR, nelts,
3254 integer_one_node, 1),
3257 do_pending_stack_adjust ();
3259 TREE_SIDE_EFFECTS (xval) = 1;
3260 TREE_CALLS_NEW (xval) = 1;
3261 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3264 if (TREE_CODE (rval) == SAVE_EXPR)
3266 /* Errors may cause this to not get evaluated. */
3267 if (SAVE_EXPR_RTL (rval) == 0)
3268 SAVE_EXPR_RTL (rval) = const0_rtx;
3269 RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
3273 my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
3274 RTL_EXPR_RTL (xval) = DECL_RTL (rval);
3279 else if (TYPE_READONLY (true_type))
3280 cp_error ("uninitialized const in `new' of `%#T'", true_type);
3286 /* Did we modify the storage? */
3287 if (rval != alloc_temp)
3289 tree ifexp = build_binary_op (NE_EXPR, alloc_expr,
3290 integer_zero_node, 1);
3291 rval = build_conditional_expr (ifexp, rval, alloc_temp);
3297 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
3299 /* The type of new int [3][3] is not int *, but int [3] * */
3300 rval = build_c_cast (build_pointer_type (type), rval, 0);
3304 rval = build_compound_expr (chainon (pending_sizes,
3305 build_tree_list (NULL_TREE, rval)));
3309 extern tree gc_visible;
3313 rval = save_expr (rval);
3314 /* We don't need a `headof' operation to do this because
3315 we know where the object starts. */
3316 objbits = build1 (INDIRECT_REF, unsigned_type_node,
3317 build (MINUS_EXPR, ptr_type_node,
3318 rval, c_sizeof_nowarn (unsigned_type_node)));
3319 update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible);
3320 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
3321 tree_cons (NULL_TREE, update_expr,
3322 build_tree_list (NULL_TREE, rval))));
3329 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
3331 tree base, maxindex, type;
3332 tree auto_delete_vec, auto_delete;
3333 int use_global_delete;
3336 tree ptype = build_pointer_type (type);
3337 tree size_exp = size_in_bytes (type);
3339 /* Temporary variables used by the loop. */
3340 tree tbase, tbase_init;
3342 /* This is the body of the loop that implements the deletion of a
3343 single element, and moves temp variables to next elements. */
3346 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3349 /* This is the thing that governs what to do after the loop has run. */
3350 tree deallocate_expr = 0;
3352 /* This is the BIND_EXPR which holds the outermost iterator of the
3353 loop. It is convenient to set this variable up and test it before
3354 executing any other code in the loop.
3355 This is also the containing expression returned by this function. */
3356 tree controller = NULL_TREE;
3358 /* This is the BLOCK to record the symbol binding for debugging. */
3361 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
3363 loop = integer_zero_node;
3367 /* The below is short by BI_header_size */
3368 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
3370 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
3371 tbase_init = build_modify_expr (tbase, NOP_EXPR,
3372 fold (build (PLUS_EXPR, ptype,
3375 DECL_REGISTER (tbase) = 1;
3376 controller = build (BIND_EXPR, void_type_node, tbase, 0, 0);
3377 TREE_SIDE_EFFECTS (controller) = 1;
3378 block = build_block (tbase, 0, 0, 0, 0);
3379 add_block_current_level (block);
3381 if (auto_delete != integer_zero_node
3382 && auto_delete != integer_two_node)
3384 tree base_tbd = convert (ptype,
3385 build_binary_op (MINUS_EXPR,
3386 convert (ptr_type_node, base),
3389 /* This is the real size */
3390 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
3391 body = build_tree_list (NULL_TREE,
3392 build_x_delete (ptype, base_tbd,
3393 2 | use_global_delete,
3395 body = build (COND_EXPR, void_type_node,
3396 build (BIT_AND_EXPR, integer_type_node,
3397 auto_delete, integer_one_node),
3398 body, integer_zero_node);
3403 body = tree_cons (NULL_TREE,
3404 build_delete (ptype, tbase, auto_delete,
3405 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
3408 body = tree_cons (NULL_TREE,
3409 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
3412 body = tree_cons (NULL_TREE,
3413 build (EXIT_EXPR, void_type_node,
3414 build (EQ_EXPR, boolean_type_node, base, tbase)),
3417 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
3419 loop = tree_cons (NULL_TREE, tbase_init,
3420 tree_cons (NULL_TREE, loop, NULL_TREE));
3421 loop = build_compound_expr (loop);
3424 /* If the delete flag is one, or anything else with the low bit set,
3425 delete the storage. */
3426 if (auto_delete_vec == integer_zero_node
3427 || auto_delete_vec == integer_two_node)
3428 deallocate_expr = integer_zero_node;
3433 /* The below is short by BI_header_size */
3434 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
3436 if (! TYPE_VEC_NEW_USES_COOKIE (type))
3441 base_tbd = convert (ptype,
3442 build_binary_op (MINUS_EXPR,
3443 convert (string_type_node, base),
3446 /* True size with header. */
3447 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
3449 deallocate_expr = build_x_delete (ptype, base_tbd,
3450 2 | use_global_delete,
3452 if (auto_delete_vec != integer_one_node)
3453 deallocate_expr = build (COND_EXPR, void_type_node,
3454 build (BIT_AND_EXPR, integer_type_node,
3455 auto_delete_vec, integer_one_node),
3456 deallocate_expr, integer_zero_node);
3459 if (loop && deallocate_expr != integer_zero_node)
3461 body = tree_cons (NULL_TREE, loop,
3462 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
3463 body = build_compound_expr (body);
3468 /* Outermost wrapper: If pointer is null, punt. */
3469 body = build (COND_EXPR, void_type_node,
3470 build (NE_EXPR, boolean_type_node, base, integer_zero_node),
3471 body, integer_zero_node);
3472 body = build1 (NOP_EXPR, void_type_node, body);
3476 TREE_OPERAND (controller, 1) = body;
3480 return convert (void_type_node, body);
3483 /* Build a tree to cleanup partially built arrays.
3484 BASE is that starting address of the array.
3485 COUNT is the count of objects that have been built, that need destroying.
3486 TYPE is the type of elements in the array. */
3488 build_array_eh_cleanup (base, count, type)
3489 tree base, count, type;
3491 tree expr = build_vec_delete_1 (base, count, type, integer_two_node,
3492 integer_zero_node, 0);
3496 /* `expand_vec_init' performs initialization of a vector of aggregate
3499 DECL is passed only for error reporting, and provides line number
3500 and source file name information.
3501 BASE is the space where the vector will be.
3502 MAXINDEX is the maximum index of the array (one less than the
3503 number of elements).
3504 INIT is the (possibly NULL) initializer.
3506 FROM_ARRAY is 0 if we should init everything with INIT
3507 (i.e., every element initialized from INIT).
3508 FROM_ARRAY is 1 if we should index into INIT in parallel
3509 with initialization of DECL.
3510 FROM_ARRAY is 2 if we should index into INIT in parallel,
3511 but use assignment instead of initialization. */
3514 expand_vec_init (decl, base, maxindex, init, from_array)
3515 tree decl, base, maxindex, init;
3519 tree iterator, base2 = NULL_TREE;
3520 tree type = TREE_TYPE (TREE_TYPE (base));
3523 maxindex = convert (integer_type_node, maxindex);
3524 if (maxindex == error_mark_node)
3525 return error_mark_node;
3527 if (current_function_decl == NULL_TREE)
3529 rval = make_tree_vec (3);
3530 TREE_VEC_ELT (rval, 0) = base;
3531 TREE_VEC_ELT (rval, 1) = maxindex;
3532 TREE_VEC_ELT (rval, 2) = init;
3536 size = size_in_bytes (type);
3538 /* Set to zero in case size is <= 0. Optimizer will delete this if
3539 it is not needed. */
3540 rval = get_temp_regvar (build_pointer_type (type),
3541 convert (build_pointer_type (type), null_pointer_node));
3542 base = default_conversion (base);
3543 base = convert (build_pointer_type (type), base);
3544 expand_assignment (rval, base, 0, 0);
3545 base = get_temp_regvar (build_pointer_type (type), base);
3547 if (init != NULL_TREE
3548 && TREE_CODE (init) == CONSTRUCTOR
3549 && TREE_TYPE (init) == TREE_TYPE (decl))
3551 /* Initialization of array from {...}. */
3552 tree elts = CONSTRUCTOR_ELTS (init);
3553 tree baseref = build1 (INDIRECT_REF, type, base);
3554 tree baseinc = build (PLUS_EXPR, build_pointer_type (type), base, size);
3555 int host_i = TREE_INT_CST_LOW (maxindex);
3557 if (IS_AGGR_TYPE (type))
3562 expand_aggr_init (baseref, TREE_VALUE (elts), 0, 0);
3564 expand_assignment (base, baseinc, 0, 0);
3565 elts = TREE_CHAIN (elts);
3567 /* Initialize any elements by default if possible. */
3570 if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3573 use_variable (DECL_RTL (base));
3577 iterator = get_temp_regvar (integer_type_node,
3578 build_int_2 (host_i, 0));
3580 goto init_by_default;
3586 expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3588 expand_assignment (base, baseinc, 0, 0);
3589 elts = TREE_CHAIN (elts);
3593 use_variable (DECL_RTL (base));
3599 iterator = get_temp_regvar (integer_type_node, maxindex);
3603 /* If initializing one array from another,
3604 initialize element by element. */
3607 /* We rely upon the below calls the do argument checking */
3608 if (decl == NULL_TREE)
3610 sorry ("initialization of array from dissimilar array type");
3611 return error_mark_node;
3615 base2 = default_conversion (init);
3616 itype = TREE_TYPE (base2);
3617 base2 = get_temp_regvar (itype, base2);
3618 itype = TREE_TYPE (itype);
3620 else if (TYPE_LANG_SPECIFIC (type)
3621 && TYPE_NEEDS_CONSTRUCTING (type)
3622 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3624 error ("initializer ends prematurely");
3625 return error_mark_node;
3629 expand_start_cond (build (GE_EXPR, boolean_type_node,
3630 iterator, integer_zero_node), 0);
3631 if (TYPE_NEEDS_DESTRUCTOR (type))
3633 expand_start_loop_continue_elsewhere (1);
3637 tree to = build1 (INDIRECT_REF, type, base);
3641 from = build1 (INDIRECT_REF, itype, base2);
3645 if (from_array == 2)
3646 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3647 else if (TYPE_NEEDS_CONSTRUCTING (type))
3648 expand_aggr_init (to, from, 0, 0);
3650 expand_assignment (to, from, 0, 0);
3652 my_friendly_abort (57);
3654 else if (TREE_CODE (type) == ARRAY_TYPE)
3657 sorry ("cannot initialize multi-dimensional array with initializer");
3658 expand_vec_init (decl, build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), base),
3659 array_type_nelts (type), 0, 0);
3662 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0, 0);
3664 expand_assignment (base,
3665 build (PLUS_EXPR, build_pointer_type (type), base, size),
3668 expand_assignment (base2,
3669 build (PLUS_EXPR, build_pointer_type (type), base2, size), 0, 0);
3670 expand_loop_continue_here ();
3671 expand_exit_loop_if_false (0, build (NE_EXPR, boolean_type_node,
3672 build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
3676 use_variable (DECL_RTL (base));
3678 use_variable (DECL_RTL (base2));
3681 if (TYPE_NEEDS_DESTRUCTOR (type))
3682 end_protect (build_array_eh_cleanup (rval,
3683 build_binary_op (MINUS_EXPR,
3690 use_variable (DECL_RTL (iterator));
3695 use_variable (DECL_RTL (rval));
3699 /* Free up storage of type TYPE, at address ADDR.
3701 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3704 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3705 used as the second argument to operator delete. It can include
3706 things like padding and magic size cookies. It has virtual in it,
3707 because if you have a base pointer and you delete through a virtual
3708 destructor, it should be the size of the dynamic object, not the
3709 static object, see Free Store 12.5 ANSI C++ WP.
3711 This does not call any destructors. */
3713 build_x_delete (type, addr, which_delete, virtual_size)
3718 int use_global_delete = which_delete & 1;
3719 int use_vec_delete = !!(which_delete & 2);
3721 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
3723 if (! use_global_delete && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3724 && (TYPE_GETS_DELETE (TREE_TYPE (type)) & (1 << use_vec_delete)))
3725 rval = build_opfncall (code, LOOKUP_NORMAL, addr, virtual_size, NULL_TREE);
3727 rval = build_builtin_call (void_type_node, use_vec_delete ? BIVD : BID,
3728 build_tree_list (NULL_TREE, addr));
3732 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3733 ADDR is an expression which yields the store to be destroyed.
3734 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3735 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3736 virtual baseclasses.
3737 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3739 FLAGS is the logical disjunction of zero or more LOOKUP_
3740 flags. See cp-tree.h for more info.
3742 This function does not delete an object's virtual base classes. */
3744 build_delete (type, addr, auto_delete, flags, use_global_delete)
3748 int use_global_delete;
3750 tree function, parms;
3756 if (addr == error_mark_node)
3757 return error_mark_node;
3759 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3760 set to `error_mark_node' before it gets properly cleaned up. */
3761 if (type == error_mark_node)
3762 return error_mark_node;
3764 type = TYPE_MAIN_VARIANT (type);
3766 if (TREE_CODE (type) == POINTER_TYPE)
3768 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3769 if (TYPE_SIZE (type) == 0)
3771 incomplete_type_error (0, type);
3772 return error_mark_node;
3774 if (TREE_CODE (type) == ARRAY_TYPE)
3776 if (! IS_AGGR_TYPE (type))
3778 /* Call the builtin operator delete. */
3779 return build_builtin_call (void_type_node, BID,
3780 build_tree_list (NULL_TREE, addr));
3782 if (TREE_SIDE_EFFECTS (addr))
3783 addr = save_expr (addr);
3785 /* throw away const and volatile on target type of addr */
3786 addr = convert_force (build_pointer_type (type), addr, 0);
3787 ref = build_indirect_ref (addr, NULL_PTR);
3790 else if (TREE_CODE (type) == ARRAY_TYPE)
3793 if (TREE_SIDE_EFFECTS (addr))
3794 addr = save_expr (addr);
3795 if (TYPE_DOMAIN (type) == NULL_TREE)
3797 error ("unknown array size in delete");
3798 return error_mark_node;
3800 return build_vec_delete (addr, array_type_nelts (type),
3801 c_sizeof_nowarn (TREE_TYPE (type)),
3802 auto_delete, integer_two_node,
3807 /* Don't check PROTECT here; leave that decision to the
3808 destructor. If the destructor is accessible, call it,
3809 else report error. */
3810 addr = build_unary_op (ADDR_EXPR, addr, 0);
3811 if (TREE_SIDE_EFFECTS (addr))
3812 addr = save_expr (addr);
3814 if (TREE_CONSTANT (addr))
3815 addr = convert_pointer_to (type, addr);
3817 addr = convert_force (build_pointer_type (type), addr, 0);
3819 if (TREE_CODE (addr) == NOP_EXPR
3820 && TREE_OPERAND (addr, 0) == current_class_decl)
3823 ref = build_indirect_ref (addr, NULL_PTR);
3827 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3829 if (! TYPE_NEEDS_DESTRUCTOR (type))
3831 if (auto_delete == integer_zero_node)
3832 return void_zero_node;
3834 /* Pass the size of the object down to the operator delete() in
3835 addition to the ADDR. */
3836 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
3838 tree virtual_size = c_sizeof_nowarn (type);
3839 return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3840 virtual_size, NULL_TREE);
3843 /* Call the builtin operator delete. */
3844 return build_builtin_call (void_type_node, BID,
3845 build_tree_list (NULL_TREE, addr));
3847 parms = build_tree_list (NULL_TREE, addr);
3849 /* Below, we will reverse the order in which these calls are made.
3850 If we have a destructor, then that destructor will take care
3851 of the base classes; otherwise, we must do that here. */
3852 if (TYPE_HAS_DESTRUCTOR (type))
3854 tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0));
3855 tree basetypes = TYPE_BINFO (type);
3856 tree passed_auto_delete;
3857 tree do_delete = NULL_TREE;
3859 if (use_global_delete)
3861 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3862 auto_delete, integer_one_node));
3863 tree call = build_builtin_call
3864 (void_type_node, BID, build_tree_list (NULL_TREE, addr));
3866 cond = fold (build (COND_EXPR, void_type_node, cond,
3867 call, void_zero_node));
3868 if (cond != void_zero_node)
3871 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3872 auto_delete, integer_two_node));
3875 passed_auto_delete = auto_delete;
3877 if (flags & LOOKUP_PROTECT)
3879 enum access_type access = compute_access (basetypes, dtor);
3881 if (access == access_private)
3883 if (flags & LOOKUP_COMPLAIN)
3884 cp_error ("destructor for type `%T' is private in this scope", type);
3885 return error_mark_node;
3887 else if (access == access_protected)
3889 if (flags & LOOKUP_COMPLAIN)
3890 cp_error ("destructor for type `%T' is protected in this scope", type);
3891 return error_mark_node;
3895 /* Once we are in a destructor, try not going through
3896 the virtual function table to find the next destructor. */
3897 if (DECL_VINDEX (dtor)
3898 && ! (flags & LOOKUP_NONVIRTUAL)
3899 && TREE_CODE (auto_delete) != PARM_DECL
3900 && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
3902 tree binfo, basetype;
3903 /* The code below is probably all broken. See call.c for the
3904 complete right way to do this. this offsets may not be right
3905 in the below. (mrs) */
3906 /* This destructor must be called via virtual function table. */
3907 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0);
3908 basetype = DECL_CLASS_CONTEXT (dtor);
3909 binfo = get_binfo (basetype,
3910 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3912 expr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3913 if (expr != TREE_VALUE (parms))
3916 ref = build_indirect_ref (expr, NULL_PTR);
3917 TREE_VALUE (parms) = expr;
3919 function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
3920 if (function == error_mark_node)
3921 return error_mark_node;
3922 TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
3923 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, passed_auto_delete);
3924 expr = build_function_call (function, parms);
3926 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3927 if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3929 /* Handle the case where a virtual destructor is
3930 being called on an item that is 0.
3932 @@ Does this really need to be done? */
3933 tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
3935 if (TREE_CODE (ref) == VAR_DECL
3936 || TREE_CODE (ref) == COMPONENT_REF)
3937 warning ("losing in build_delete");
3939 expr = build (COND_EXPR, void_type_node,
3940 ifexp, expr, void_zero_node);
3947 if ((flags & LOOKUP_DESTRUCTOR)
3948 || TREE_CODE (ref) == VAR_DECL
3949 || TREE_CODE (ref) == PARM_DECL
3950 || TREE_CODE (ref) == COMPONENT_REF
3951 || TREE_CODE (ref) == ARRAY_REF)
3952 /* These can't be 0. */
3953 ifexp = integer_one_node;
3955 /* Handle the case where a non-virtual destructor is
3956 being called on an item that is 0. */
3957 ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
3959 /* Used to mean that this destructor was known to be empty,
3960 but that's now obsolete. */
3961 my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
3963 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, passed_auto_delete);
3964 expr = build_function_call (dtor, parms);
3966 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3968 if (ifexp != integer_one_node)
3969 expr = build (COND_EXPR, void_type_node,
3970 ifexp, expr, void_zero_node);
3976 /* This can get visibilities wrong. */
3977 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3978 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3979 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3980 tree exprstmt = NULL_TREE;
3981 tree parent_auto_delete = auto_delete;
3984 /* If this type does not have a destructor, but does have
3985 operator delete, call the parent parent destructor (if any),
3986 but let this node do the deleting. Otherwise, it is ok
3987 to let the parent destructor do the deleting. */
3988 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
3990 parent_auto_delete = integer_zero_node;
3991 if (auto_delete == integer_zero_node)
3997 /* This is probably wrong. It should be the size of the
3998 virtual object being deleted. */
3999 virtual_size = c_sizeof_nowarn (type);
4001 expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
4002 virtual_size, NULL_TREE);
4003 if (expr == error_mark_node)
4004 return error_mark_node;
4005 if (auto_delete != integer_one_node)
4006 cond = build (COND_EXPR, void_type_node,
4007 build (BIT_AND_EXPR, integer_type_node,
4008 auto_delete, integer_one_node),
4009 expr, void_zero_node);
4014 else if (base_binfo == NULL_TREE
4015 || (TREE_VIA_VIRTUAL (base_binfo) == 0
4016 && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
4020 /* This is probably wrong. It should be the size of the virtual
4021 object being deleted. */
4022 virtual_size = c_sizeof_nowarn (type);
4024 cond = build (COND_EXPR, void_type_node,
4025 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
4026 build_builtin_call (void_type_node, BID,
4027 build_tree_list (NULL_TREE, addr)),
4034 exprstmt = build_tree_list (NULL_TREE, cond);
4037 && ! TREE_VIA_VIRTUAL (base_binfo)
4038 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
4040 tree this_auto_delete;
4042 if (BINFO_OFFSET_ZEROP (base_binfo))
4043 this_auto_delete = parent_auto_delete;
4045 this_auto_delete = integer_zero_node;
4047 expr = build_delete (build_pointer_type (BINFO_TYPE (base_binfo)), addr,
4048 this_auto_delete, flags, 0);
4049 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
4052 /* Take care of the remaining baseclasses. */
4053 for (i = 1; i < n_baseclasses; i++)
4055 base_binfo = TREE_VEC_ELT (binfos, i);
4056 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
4057 || TREE_VIA_VIRTUAL (base_binfo))
4060 /* May be zero offset if other baseclasses are virtual. */
4061 expr = fold (build (PLUS_EXPR, build_pointer_type (BINFO_TYPE (base_binfo)),
4062 addr, BINFO_OFFSET (base_binfo)));
4064 expr = build_delete (build_pointer_type (BINFO_TYPE (base_binfo)), expr,
4068 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
4071 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
4073 if (TREE_CODE (member) != FIELD_DECL)
4075 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
4077 tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
4078 tree this_type = TREE_TYPE (member);
4079 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
4080 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
4085 return build_compound_expr (exprstmt);
4086 /* Virtual base classes make this function do nothing. */
4087 return void_zero_node;
4091 /* For type TYPE, delete the virtual baseclass objects of DECL. */
4094 build_vbase_delete (type, decl)
4097 tree vbases = CLASSTYPE_VBASECLASSES (type);
4098 tree result = NULL_TREE;
4099 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
4101 my_friendly_assert (addr != error_mark_node, 222);
4105 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
4107 result = tree_cons (NULL_TREE,
4108 build_delete (TREE_TYPE (this_addr), this_addr,
4110 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
4112 vbases = TREE_CHAIN (vbases);
4114 return build_compound_expr (nreverse (result));
4117 /* Build a C++ vector delete expression.
4118 MAXINDEX is the number of elements to be deleted.
4119 ELT_SIZE is the nominal size of each element in the vector.
4120 BASE is the expression that should yield the store to be deleted.
4121 This function expands (or synthesizes) these calls itself.
4122 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
4123 AUTO_DELETE say whether each item in the container should be deallocated.
4125 This also calls delete for virtual baseclasses of elements of the vector.
4127 Update: MAXINDEX is no longer needed. The size can be extracted from the
4128 start of the vector for pointers, and from the type for arrays. We still
4129 use MAXINDEX for arrays because it happens to already have one of the
4130 values we'd have to extract. (We could use MAXINDEX with pointers to
4131 confirm the size, and trap if the numbers differ; not clear that it'd
4132 be worth bothering.) */
4134 build_vec_delete (base, maxindex, elt_size, auto_delete_vec, auto_delete,
4136 tree base, maxindex, elt_size;
4137 tree auto_delete_vec, auto_delete;
4138 int use_global_delete;
4142 if (TREE_CODE (base) == OFFSET_REF)
4143 base = resolve_offset_ref (base);
4145 type = TREE_TYPE (base);
4147 base = stabilize_reference (base);
4149 /* Since we can use base many times, save_expr it. */
4150 if (TREE_SIDE_EFFECTS (base))
4151 base = save_expr (base);
4153 if (TREE_CODE (type) == POINTER_TYPE)
4155 /* Step back one from start of vector, and read dimension. */
4156 tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
4157 base, BI_header_size);
4158 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
4159 maxindex = build_component_ref (cookie, nc_nelts_field_id, 0, 0);
4161 type = TREE_TYPE (type);
4162 while (TREE_CODE (type) == ARRAY_TYPE);
4164 else if (TREE_CODE (type) == ARRAY_TYPE)
4166 /* get the total number of things in the array, maxindex is a bad name */
4167 maxindex = array_type_nelts_total (type);
4168 while (TREE_CODE (type) == ARRAY_TYPE)
4169 type = TREE_TYPE (type);
4170 base = build_unary_op (ADDR_EXPR, base, 1);
4174 error ("type to vector delete is neither pointer or array type");
4175 return error_mark_node;
4178 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,