1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 89, 92, 93, 94, 95, 1996 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. */
32 /* In C++, structures with well-defined constructors are initialized by
33 those constructors, unasked. CURRENT_BASE_INIT_LIST
34 holds a list of stmts for a BASE_INIT term in the grammar.
35 This list has one element for each base class which must be
36 initialized. The list elements are [basename, init], with
37 type basetype. This allows the possibly anachronistic form
38 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
39 where each successive term can be handed down the constructor
40 line. Perhaps this was not intended. */
41 tree current_base_init_list, current_member_init_list;
43 extern tree cleanups_this_call;
45 void emit_base_init ();
46 void check_base_init ();
47 static void expand_aggr_vbase_init ();
48 void expand_member_init ();
49 void expand_aggr_init ();
51 static void expand_aggr_init_1 PROTO((tree, tree, tree, tree, int, int));
52 static void expand_virtual_init PROTO((tree, tree));
53 tree expand_vec_init ();
55 static void add_friend (), add_friends ();
57 /* Cache _builtin_new and _builtin_delete exprs. */
58 static tree BIN, BID, BIVN, BIVD;
60 /* Cache the identifier nodes for the magic field of a new cookie. */
61 static tree nc_nelts_field_id;
63 static tree minus_one;
65 /* Set up local variable for this file. MUST BE CALLED AFTER
66 INIT_DECL_PROCESSING. */
68 static tree BI_header_type, BI_header_size;
70 void init_init_processing ()
74 /* Define implicit `operator new' and `operator delete' functions. */
75 BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR])));
76 TREE_USED (TREE_OPERAND (BIN, 0)) = 0;
77 BID = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR])));
78 TREE_USED (TREE_OPERAND (BID, 0)) = 0;
79 BIVN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_NEW_EXPR])));
80 TREE_USED (TREE_OPERAND (BIVN, 0)) = 0;
81 BIVD = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_DELETE_EXPR])));
82 TREE_USED (TREE_OPERAND (BIVD, 0)) = 0;
83 minus_one = build_int_2 (-1, -1);
85 /* Define the structure that holds header information for
86 arrays allocated via operator new. */
87 BI_header_type = make_lang_type (RECORD_TYPE);
88 nc_nelts_field_id = get_identifier ("nelts");
89 fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
90 finish_builtin_type (BI_header_type, "__new_cookie", fields,
92 BI_header_size = size_in_bytes (BI_header_type);
95 /* Subroutine of emit_base_init. For BINFO, initialize all the
96 virtual function table pointers, except those that come from
97 virtual base classes. Initialize binfo's vtable pointer, if
98 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual
99 function table pointers in all bases have been initialized already,
100 probably because their constructors have just be run. ADDR is the
101 pointer to the object whos vtables we are going to initialize.
103 REAL_BINFO is usually the same as BINFO, except when addr is not of
104 pointer to the type of the real derived type that we want to
105 initialize for. This is the case when addr is a pointer to a sub
106 object of a complete object, and we only want to do part of the
107 complete object's initialization of vtable pointers. This is done
108 for all virtual table pointers in virtual base classes. REAL_BINFO
109 is used to find the BINFO_VTABLE that we initialize with. BINFO is
110 used for conversions of addr to subobjects.
112 BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo).
114 Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE
118 expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr)
119 tree real_binfo, binfo, addr;
120 int init_self, can_elide;
122 tree real_binfos = BINFO_BASETYPES (real_binfo);
123 tree binfos = BINFO_BASETYPES (binfo);
124 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
126 for (i = 0; i < n_baselinks; i++)
128 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
129 tree base_binfo = TREE_VEC_ELT (binfos, i);
130 int is_not_base_vtable =
131 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
132 if (! TREE_VIA_VIRTUAL (real_base_binfo))
133 expand_direct_vtbls_init (real_base_binfo, base_binfo,
134 is_not_base_vtable, can_elide, addr);
137 /* Before turning this on, make sure it is correct. */
138 if (can_elide && ! BINFO_MODIFIED (binfo))
141 /* Should we use something besides CLASSTYPE_VFIELDS? */
142 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
144 tree base_ptr = convert_pointer_to_real (binfo, addr);
145 expand_virtual_init (real_binfo, base_ptr);
150 /* Subroutine of emit_base_init. */
153 perform_member_init (member, name, init, explicit)
154 tree member, name, init;
158 tree type = TREE_TYPE (member);
159 extern int temp_slot_level;
160 extern int target_temp_slot_level;
161 tree old_cleanups = cleanups_this_call;
162 int old_temp_level = target_temp_slot_level;
165 target_temp_slot_level = temp_slot_level;
167 if (TYPE_NEEDS_CONSTRUCTING (type)
168 || (init && TYPE_HAS_CONSTRUCTOR (type)))
170 /* Since `init' is already a TREE_LIST on the current_member_init_list,
171 only build it into one if we aren't already a list. */
172 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
173 init = build_tree_list (NULL_TREE, init);
175 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
178 && TREE_CODE (type) == ARRAY_TYPE
180 && TREE_CHAIN (init) == NULL_TREE
181 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
183 /* Initialization of one array from another. */
184 expand_vec_init (TREE_OPERAND (decl, 1), decl,
185 array_type_nelts (type), TREE_VALUE (init), 1);
188 expand_aggr_init (decl, init, 0, 0);
192 if (init == NULL_TREE)
196 cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor",
197 member, current_class_type);
198 init = error_mark_node;
200 /* member traversal: note it leaves init NULL */
201 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
202 cp_pedwarn ("uninitialized reference member `%D'", member);
204 else if (TREE_CODE (init) == TREE_LIST)
206 /* There was an explicit member initialization. Do some
207 work in that case. */
208 if (TREE_CHAIN (init))
210 warning ("initializer list treated as compound expression");
211 init = build_compound_expr (init);
214 init = TREE_VALUE (init);
217 /* We only build this with a null init if we got it from the
218 current_member_init_list. */
219 if (init || explicit)
221 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
222 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
225 expand_cleanups_to (old_cleanups);
228 target_temp_slot_level = old_temp_level;
229 /* There might something left from building the trees. */
230 if (cleanups_this_call)
232 expand_cleanups_to (NULL_TREE);
236 if (TYPE_NEEDS_DESTRUCTOR (type))
238 tree expr = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
239 expr = build_delete (type, expr, integer_zero_node,
240 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
242 if (expr != error_mark_node)
243 add_partial_entry (expr);
247 extern int warn_reorder;
249 /* Subroutine of emit_member_init. */
255 tree x, member, name, field;
256 tree init_list = NULL_TREE;
260 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
264 /* member could be, for example, a CONST_DECL for an enumerated
265 tag; we don't want to try to initialize that, since it already
267 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
270 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
272 /* If we cleared this out, then pay no attention to it. */
273 if (TREE_PURPOSE (x) == NULL_TREE)
275 name = TREE_PURPOSE (x);
278 /* This happens in templates, since the IDENTIFIER is replaced
279 with the COMPONENT_REF in tsubst_expr. */
280 field = (TREE_CODE (name) == COMPONENT_REF
281 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
283 /* Let's find out when this happens. */
284 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
285 field = IDENTIFIER_CLASS_VALUE (name);
288 /* If one member shadows another, get the outermost one. */
289 if (TREE_CODE (field) == TREE_LIST)
290 field = TREE_VALUE (field);
298 cp_warning_at ("member initializers for `%#D'", last_field);
299 cp_warning_at (" and `%#D'", field);
300 warning (" will be re-ordered to match declaration order");
306 /* Make sure we won't try to work on this init again. */
307 TREE_PURPOSE (x) = NULL_TREE;
308 x = build_tree_list (name, TREE_VALUE (x));
313 /* If we didn't find MEMBER in the list, create a dummy entry
314 so the two lists (INIT_LIST and the list of members) will be
316 x = build_tree_list (NULL_TREE, NULL_TREE);
318 init_list = chainon (init_list, x);
321 /* Initializers for base members go at the end. */
322 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
324 name = TREE_PURPOSE (x);
327 if (purpose_member (name, init_list))
329 cp_error ("multiple initializations given for member `%D'",
330 IDENTIFIER_CLASS_VALUE (name));
334 init_list = chainon (init_list,
335 build_tree_list (name, TREE_VALUE (x)));
336 TREE_PURPOSE (x) = NULL_TREE;
344 sort_base_init (t, rbase_ptr, vbase_ptr)
345 tree t, *rbase_ptr, *vbase_ptr;
347 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
348 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
354 /* For warn_reorder. */
356 tree last_base = NULL_TREE;
358 tree rbases = NULL_TREE;
359 tree vbases = NULL_TREE;
361 /* First walk through and splice out vbase and invalid initializers.
362 Also replace names with binfos. */
364 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
365 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
367 tree basetype = TREE_PURPOSE (x);
370 if (basetype == NULL_TREE)
372 /* Initializer for single base class. Must not
373 use multiple inheritance or this is ambiguous. */
374 switch (n_baseclasses)
377 cp_error ("`%T' does not have a base class to initialize",
383 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
387 binfo = TREE_VEC_ELT (binfos, 0);
389 else if (is_aggr_type (basetype, 1))
391 binfo = binfo_or_else (basetype, t);
392 if (binfo == NULL_TREE)
395 /* Virtual base classes are special cases. Their initializers
396 are recorded with this constructor, and they are used when
397 this constructor is the top-level constructor called. */
398 if (TREE_VIA_VIRTUAL (binfo))
400 tree v = CLASSTYPE_VBASECLASSES (t);
401 while (BINFO_TYPE (v) != BINFO_TYPE (binfo))
404 vbases = tree_cons (v, TREE_VALUE (x), vbases);
409 /* Otherwise, if it is not an immediate base class, complain. */
410 for (i = n_baseclasses-1; i >= 0; i--)
411 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
415 cp_error ("`%T' is not an immediate base class of `%T'",
416 basetype, current_class_type);
422 my_friendly_abort (365);
424 TREE_PURPOSE (x) = binfo;
425 TREE_CHAIN (last) = x;
428 TREE_CHAIN (last) = NULL_TREE;
430 /* Now walk through our regular bases and make sure they're initialized. */
432 for (i = 0; i < n_baseclasses; ++i)
434 tree base_binfo = TREE_VEC_ELT (binfos, i);
437 if (TREE_VIA_VIRTUAL (base_binfo))
440 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
442 tree binfo = TREE_PURPOSE (x);
444 if (binfo == NULL_TREE)
447 if (binfo == base_binfo)
453 cp_warning_at ("base initializers for `%#T'", last_base);
454 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
455 warning (" will be re-ordered to match inheritance order");
458 last_base = BINFO_TYPE (binfo);
461 /* Make sure we won't try to work on this init again. */
462 TREE_PURPOSE (x) = NULL_TREE;
463 x = build_tree_list (binfo, TREE_VALUE (x));
468 /* If we didn't find BASE_BINFO in the list, create a dummy entry
469 so the two lists (RBASES and the list of bases) will be
471 x = build_tree_list (NULL_TREE, NULL_TREE);
473 rbases = chainon (rbases, x);
480 /* Perform partial cleanups for a base for exception handling. */
483 build_partial_cleanup_for (binfo)
486 tree expr = convert_pointer_to_real (binfo,
487 build_unary_op (ADDR_EXPR, current_class_ref, 0));
489 return build_delete (TREE_TYPE (expr),
492 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
495 /* Perform whatever initializations have yet to be done on the base
496 class of the class variable. These actions are in the global
497 variable CURRENT_BASE_INIT_LIST. Such an action could be
498 NULL_TREE, meaning that the user has explicitly called the base
499 class constructor with no arguments.
501 If there is a need for a call to a constructor, we must surround
502 that call with a pushlevel/poplevel pair, since we are technically
503 at the PARM level of scope.
505 Argument IMMEDIATELY, if zero, forces a new sequence to be
506 generated to contain these new insns, so it can be emitted later.
507 This sequence is saved in the global variable BASE_INIT_EXPR.
508 Otherwise, the insns are emitted into the current sequence.
510 Note that emit_base_init does *not* initialize virtual base
511 classes. That is done specially, elsewhere. */
513 extern tree base_init_expr, rtl_expr_chain;
516 emit_base_init (t, immediately)
522 tree rbase_init_list, vbase_init_list;
523 tree t_binfo = TYPE_BINFO (t);
524 tree binfos = BINFO_BASETYPES (t_binfo);
525 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
526 tree expr = NULL_TREE;
531 do_pending_stack_adjust ();
532 /* Make the RTL_EXPR node temporary, not momentary,
533 so that rtl_expr_chain doesn't become garbage. */
534 momentary = suspend_momentary ();
535 expr = make_node (RTL_EXPR);
536 resume_momentary (momentary);
537 start_sequence_for_rtl_expr (expr);
540 if (write_symbols == NO_DEBUG)
541 /* As a matter of principle, `start_sequence' should do this. */
544 /* Always emit a line number note so we can step into constructors. */
545 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
546 DECL_SOURCE_LINE (current_function_decl));
548 mem_init_list = sort_member_init (t);
549 current_member_init_list = NULL_TREE;
551 sort_base_init (t, &rbase_init_list, &vbase_init_list);
552 current_base_init_list = NULL_TREE;
554 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
556 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
558 expand_start_cond (first_arg, 0);
559 expand_aggr_vbase_init (t_binfo, current_class_ref, current_class_ptr,
564 /* Now, perform initialization of non-virtual base classes. */
565 for (i = 0; i < n_baseclasses; i++)
567 tree base_binfo = TREE_VEC_ELT (binfos, i);
568 tree init = void_list_node;
570 if (TREE_VIA_VIRTUAL (base_binfo))
573 #if 0 /* Once unsharing happens soon enough. */
574 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo, 999);
576 BINFO_INHERITANCE_CHAIN (base_binfo) = t_binfo;
579 if (TREE_PURPOSE (rbase_init_list))
580 init = TREE_VALUE (rbase_init_list);
581 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
584 if (init != void_list_node)
586 extern int temp_slot_level;
587 extern int target_temp_slot_level;
588 tree old_cleanups = cleanups_this_call;
589 int old_temp_level = target_temp_slot_level;
592 target_temp_slot_level = temp_slot_level;
594 member = convert_pointer_to_real (base_binfo, current_class_ptr);
595 expand_aggr_init_1 (base_binfo, NULL_TREE,
596 build_indirect_ref (member, NULL_PTR), init,
597 BINFO_OFFSET_ZEROP (base_binfo), LOOKUP_NORMAL);
598 expand_cleanups_to (old_cleanups);
601 target_temp_slot_level = old_temp_level;
602 /* There might something left from building the trees. */
603 if (cleanups_this_call)
605 expand_cleanups_to (NULL_TREE);
610 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
614 /* All cleanups must be on the function_obstack. */
615 push_obstacks_nochange ();
616 resume_temporary_allocation ();
617 expr = build_partial_cleanup_for (base_binfo);
619 add_partial_entry (expr);
622 rbase_init_list = TREE_CHAIN (rbase_init_list);
625 /* Initialize all the virtual function table fields that
626 do come from virtual base classes. */
627 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
628 expand_indirect_vtbls_init (t_binfo, current_class_ref, current_class_ptr);
630 /* Initialize all the virtual function table fields that
631 do not come from virtual base classes. */
632 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_ptr);
634 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
639 /* member could be, for example, a CONST_DECL for an enumerated
640 tag; we don't want to try to initialize that, since it already
642 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
645 /* See if we had a user-specified member initialization. */
646 if (TREE_PURPOSE (mem_init_list))
648 name = TREE_PURPOSE (mem_init_list);
649 init = TREE_VALUE (mem_init_list);
653 if (TREE_CODE (name) == COMPONENT_REF)
654 name = DECL_NAME (TREE_OPERAND (name, 1));
656 /* Also see if it's ever a COMPONENT_REF here. If it is, we
657 need to do `expand_assignment (name, init, 0, 0);' and
659 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
664 name = DECL_NAME (member);
665 init = DECL_INITIAL (member);
670 perform_member_init (member, name, init, from_init_list);
671 mem_init_list = TREE_CHAIN (mem_init_list);
674 /* Now initialize any members from our bases. */
675 while (mem_init_list)
677 tree name, init, field;
679 if (TREE_PURPOSE (mem_init_list))
681 name = TREE_PURPOSE (mem_init_list);
682 init = TREE_VALUE (mem_init_list);
683 /* XXX: this may need the COMPONENT_REF operand 0 check if
684 it turns out we actually get them. */
685 field = IDENTIFIER_CLASS_VALUE (name);
687 /* If one member shadows another, get the outermost one. */
688 if (TREE_CODE (field) == TREE_LIST)
690 field = TREE_VALUE (field);
691 if (decl_type_context (field) != current_class_type)
692 cp_error ("field `%D' not in immediate context", field);
696 /* It turns out if you have an anonymous union in the
697 class, a member from it can end up not being on the
698 list of fields (rather, the type is), and therefore
699 won't be seen by the for loop above. */
701 /* The code in this for loop is derived from a general loop
702 which had this check in it. Theoretically, we've hit
703 every initialization for the list of members in T, so
704 we shouldn't have anything but these left in this list. */
705 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
708 perform_member_init (field, name, init, 1);
710 mem_init_list = TREE_CHAIN (mem_init_list);
715 do_pending_stack_adjust ();
716 my_friendly_assert (base_init_expr == 0, 207);
717 base_init_expr = expr;
718 TREE_TYPE (expr) = void_type_node;
719 RTL_EXPR_RTL (expr) = const0_rtx;
720 RTL_EXPR_SEQUENCE (expr) = get_insns ();
721 rtl_expr_chain = tree_cons (NULL_TREE, expr, rtl_expr_chain);
723 TREE_SIDE_EFFECTS (expr) = 1;
726 /* All the implicit try blocks we built up will be zapped
727 when we come to a real binding contour boundary. */
730 /* Check that all fields are properly initialized after
731 an assignment to `this'. */
738 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
739 if (DECL_NAME (member) && TREE_USED (member))
740 cp_error ("field `%D' used before initialized (after assignment to `this')",
744 /* This code sets up the virtual function tables appropriate for
745 the pointer DECL. It is a one-ply initialization.
747 BINFO is the exact type that DECL is supposed to be. In
748 multiple inheritance, this might mean "C's A" if C : A, B. */
751 expand_virtual_init (binfo, decl)
754 tree type = BINFO_TYPE (binfo);
756 tree vtype, vtype_binfo;
758 /* This code is crusty. Should be simple, like:
759 vtbl = BINFO_VTABLE (binfo);
761 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
762 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
763 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
764 assemble_external (vtbl);
765 TREE_USED (vtbl) = 1;
766 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
767 decl = convert_pointer_to_real (vtype_binfo, decl);
768 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
769 if (vtbl_ptr == error_mark_node)
772 /* Have to convert VTBL since array sizes may be different. */
773 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
774 expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
777 /* Subroutine of `expand_aggr_vbase_init'.
778 BINFO is the binfo of the type that is being initialized.
779 INIT_LIST is the list of initializers for the virtual baseclass. */
782 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
783 tree binfo, exp, addr, init_list;
785 tree init = purpose_member (binfo, init_list);
786 tree ref = build_indirect_ref (addr, NULL_PTR);
788 extern int temp_slot_level;
789 extern int target_temp_slot_level;
790 tree old_cleanups = cleanups_this_call;
791 int old_temp_level = target_temp_slot_level;
794 target_temp_slot_level = temp_slot_level;
797 init = TREE_VALUE (init);
798 /* Call constructors, but don't set up vtables. */
799 expand_aggr_init_1 (binfo, exp, ref, init, 0, LOOKUP_COMPLAIN);
801 expand_cleanups_to (old_cleanups);
804 target_temp_slot_level = old_temp_level;
805 /* There might something left from building the trees. */
806 if (cleanups_this_call)
808 expand_cleanups_to (NULL_TREE);
813 /* Initialize this object's virtual base class pointers. This must be
814 done only at the top-level of the object being constructed.
816 INIT_LIST is list of initialization for constructor to perform. */
819 expand_aggr_vbase_init (binfo, exp, addr, init_list)
825 tree type = BINFO_TYPE (binfo);
827 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
829 tree result = init_vbase_pointers (type, addr);
833 expand_expr_stmt (build_compound_expr (result));
835 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
836 vbases = TREE_CHAIN (vbases))
838 tree tmp = purpose_member (vbases, result);
839 expand_aggr_vbase_init_1 (vbases, exp,
840 TREE_OPERAND (TREE_VALUE (tmp), 0),
846 /* Subroutine to perform parser actions for member initialization.
847 S_ID is the scoped identifier.
848 NAME is the name of the member.
849 INIT is the initializer, or `void_type_node' if none. */
852 do_member_init (s_id, name, init)
853 tree s_id, name, init;
857 if (current_class_type == NULL_TREE
858 || ! is_aggr_typedef (s_id, 1))
860 binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id),
861 current_class_type, 1);
862 if (binfo == error_mark_node)
866 error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type);
870 base = convert_pointer_to (binfo, current_class_ptr);
871 expand_member_init (build_indirect_ref (base, NULL_PTR), name, init);
874 /* Find the context in which this FIELD can be initialized. */
877 initializing_context (field)
880 tree t = DECL_CONTEXT (field);
882 /* Anonymous union members can be initialized in the first enclosing
883 non-anonymous union context. */
884 while (t && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
885 t = TYPE_CONTEXT (t);
889 /* Function to give error message if member initialization specification
890 is erroneous. FIELD is the member we decided to initialize.
891 TYPE is the type for which the initialization is being performed.
892 FIELD must be a member of TYPE.
894 MEMBER_NAME is the name of the member. */
897 member_init_ok_or_else (field, type, member_name)
902 if (field == error_mark_node)
904 if (field == NULL_TREE || initializing_context (field) != type)
906 cp_error ("class `%T' does not have any field named `%s'", type,
910 if (TREE_STATIC (field))
912 cp_error ("field `%#D' is static; only point of initialization is its declaration",
920 /* If NAME is a viable field name for the aggregate DECL,
921 and PARMS is a viable parameter list, then expand an _EXPR
922 which describes this initialization.
924 Note that we do not need to chase through the class's base classes
925 to look for NAME, because if it's in that list, it will be handled
926 by the constructor for that base class.
928 We do not yet have a fixed-point finder to instantiate types
929 being fed to overloaded constructors. If there is a unique
930 constructor, then argument types can be got from that one.
932 If INIT is non-NULL, then it the initialization should
933 be placed in `current_base_init_list', where it will be processed
934 by `emit_base_init'. */
937 expand_member_init (exp, name, init)
938 tree exp, name, init;
940 extern tree ptr_type_node; /* should be in tree.h */
942 tree basetype = NULL_TREE, field;
946 if (exp == NULL_TREE)
947 return; /* complain about this later */
949 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
951 if (name && TREE_CODE (name) == TYPE_DECL)
953 basetype = TREE_TYPE (name);
954 name = DECL_NAME (name);
957 if (name == NULL_TREE && IS_AGGR_TYPE (type))
958 switch (CLASSTYPE_N_BASECLASSES (type))
961 error ("base class initializer specified, but no base class to initialize");
964 basetype = TYPE_BINFO_BASETYPE (type, 0);
967 error ("initializer for unnamed base class ambiguous");
968 cp_error ("(type `%T' uses multiple inheritance)", type);
974 /* The grammar should not allow fields which have names
975 that are TYPENAMEs. Therefore, if the field has
976 a non-NULL TREE_TYPE, we may assume that this is an
977 attempt to initialize a base class member of the current
978 type. Otherwise, it is an attempt to initialize a
981 if (init == void_type_node)
984 if (name == NULL_TREE || basetype)
988 if (name == NULL_TREE)
992 name = TYPE_IDENTIFIER (basetype);
995 error ("no base class to initialize");
1002 if (basetype != type
1003 && ! vec_binfo_member (basetype, TYPE_BINFO_BASETYPES (type))
1004 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
1006 if (IDENTIFIER_CLASS_VALUE (name))
1008 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1009 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
1012 cp_error ("type `%T' is not an immediate basetype for `%T'",
1018 if (purpose_member (basetype, current_base_init_list))
1020 cp_error ("base class `%T' already initialized", basetype);
1024 if (warn_reorder && current_member_init_list)
1026 cp_warning ("base initializer for `%T'", basetype);
1027 warning (" will be re-ordered to precede member initializations");
1030 base_init = build_tree_list (basetype, init);
1031 current_base_init_list = chainon (current_base_init_list, base_init);
1038 field = lookup_field (type, name, 1, 0);
1040 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
1043 if (purpose_member (name, current_member_init_list))
1045 cp_error ("field `%D' already initialized", field);
1049 member_init = build_tree_list (name, init);
1050 current_member_init_list = chainon (current_member_init_list, member_init);
1054 else if (name == NULL_TREE)
1056 compiler_error ("expand_member_init: name == NULL_TREE");
1061 field = lookup_field (basetype, name, 0, 0);
1063 if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
1066 /* now see if there is a constructor for this type
1067 which will take these args. */
1069 if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
1071 tree parmtypes, fndecl;
1073 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1075 /* just know that we've seen something for this node */
1076 DECL_INITIAL (exp) = error_mark_node;
1077 TREE_USED (exp) = 1;
1079 type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
1080 parm = build_component_ref (exp, name, NULL_TREE, 0);
1082 /* Now get to the constructors. */
1083 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1086 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209);
1088 /* If the field is unique, we can use the parameter
1089 types to guide possible type instantiation. */
1090 if (DECL_CHAIN (fndecl) == NULL_TREE)
1092 /* There was a confusion here between
1093 FIELD and FNDECL. The following code
1094 should be correct, but abort is here
1096 my_friendly_abort (48);
1097 parmtypes = FUNCTION_ARG_CHAIN (fndecl);
1101 parmtypes = NULL_TREE;
1105 init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
1106 if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
1107 rval = build_method_call (NULL_TREE, ctor_identifier, init,
1108 TYPE_BINFO (type), LOOKUP_NORMAL);
1112 if (rval != error_mark_node)
1114 /* Now, fill in the first parm with our guy */
1115 TREE_VALUE (TREE_OPERAND (rval, 1))
1116 = build_unary_op (ADDR_EXPR, parm, 0);
1117 TREE_TYPE (rval) = ptr_type_node;
1118 TREE_SIDE_EFFECTS (rval) = 1;
1121 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1123 parm = build_component_ref (exp, name, NULL_TREE, 0);
1124 expand_aggr_init (parm, NULL_TREE, 0, 0);
1125 rval = error_mark_node;
1128 /* Now initialize the member. It does not have to
1129 be of aggregate type to receive initialization. */
1130 if (rval != error_mark_node)
1131 expand_expr_stmt (rval);
1134 /* This is like `expand_member_init', only it stores one aggregate
1137 INIT comes in two flavors: it is either a value which
1138 is to be stored in EXP, or it is a parameter list
1139 to go to a constructor, which will operate on EXP.
1140 If INIT is not a parameter list for a constructor, then set
1141 LOOKUP_ONLYCONVERTING.
1142 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1143 the initializer, if FLAGS is 0, then it is the (init) form.
1144 If `init' is a CONSTRUCTOR, then we emit a warning message,
1145 explaining that such initializations are invalid.
1147 ALIAS_THIS is nonzero iff we are initializing something which is
1148 essentially an alias for current_class_ref. In this case, the base
1149 constructor may move it on us, and we must keep track of such
1152 If INIT resolves to a CALL_EXPR which happens to return
1153 something of the type we are looking for, then we know
1154 that we can safely use that call to perform the
1157 The virtual function table pointer cannot be set up here, because
1158 we do not really know its type.
1160 Virtual baseclass pointers are also set up here.
1162 This never calls operator=().
1164 When initializing, nothing is CONST.
1166 A default copy constructor may have to be used to perform the
1169 A constructor or a conversion operator may have to be used to
1170 perform the initialization, but not both, as it would be ambiguous. */
1173 expand_aggr_init (exp, init, alias_this, flags)
1178 tree type = TREE_TYPE (exp);
1179 int was_const = TREE_READONLY (exp);
1180 int was_volatile = TREE_THIS_VOLATILE (exp);
1182 if (init == error_mark_node)
1185 TREE_READONLY (exp) = 0;
1186 TREE_THIS_VOLATILE (exp) = 0;
1188 if (init && TREE_CODE (init) != TREE_LIST)
1189 flags |= LOOKUP_ONLYCONVERTING;
1191 if (TREE_CODE (type) == ARRAY_TYPE)
1193 /* Must arrange to initialize each element of EXP
1194 from elements of INIT. */
1195 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1196 if (TYPE_READONLY (TREE_TYPE (type)) || TYPE_VOLATILE (TREE_TYPE (type)))
1198 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1200 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1202 if (init && TREE_TYPE (init) == NULL_TREE)
1204 /* Handle bad initializers like:
1208 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1212 int main(int argc, char **argv) {
1213 COMPLEX zees(1.0, 0.0)[10];
1216 error ("bad array initializer");
1219 expand_vec_init (exp, exp, array_type_nelts (type), init,
1220 init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
1221 TREE_READONLY (exp) = was_const;
1222 TREE_THIS_VOLATILE (exp) = was_volatile;
1223 TREE_TYPE (exp) = type;
1225 TREE_TYPE (init) = itype;
1229 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1230 /* just know that we've seen something for this node */
1231 TREE_USED (exp) = 1;
1234 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1235 constructor as parameters to an implicit GNU C++ constructor. */
1236 if (init && TREE_CODE (init) == CONSTRUCTOR
1237 && TYPE_HAS_CONSTRUCTOR (type)
1238 && TREE_TYPE (init) == type)
1239 init = CONSTRUCTOR_ELTS (init);
1242 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1243 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1244 init, alias_this, LOOKUP_NORMAL|flags);
1245 TREE_TYPE (exp) = type;
1246 TREE_READONLY (exp) = was_const;
1247 TREE_THIS_VOLATILE (exp) = was_volatile;
1251 expand_default_init (binfo, true_exp, exp, init, alias_this, flags)
1258 tree type = TREE_TYPE (exp);
1260 /* It fails because there may not be a constructor which takes
1261 its own type as the first (or only parameter), but which does
1262 take other types via a conversion. So, if the thing initializing
1263 the expression is a unit element of type X, first try X(X&),
1264 followed by initialization by X. If neither of these work
1265 out, then look hard. */
1269 if (flag_ansi_overloading && init && TREE_CODE (init) != TREE_LIST
1270 && (flags & LOOKUP_ONLYCONVERTING))
1272 /* Base subobjects should only get direct-initialization. */
1273 if (true_exp != exp)
1276 /* We special-case TARGET_EXPRs here to avoid an error about
1277 private copy constructors for temporaries bound to reference vars.
1278 If the TARGET_EXPR represents a call to a function that has
1279 permission to create such objects, a reference can bind directly
1280 to the return value. An object variable must be initialized
1281 via the copy constructor, even if the call is elided. */
1282 if (! (TREE_CODE (exp) == VAR_DECL && DECL_ARTIFICIAL (exp)
1283 && TREE_CODE (init) == TARGET_EXPR && TREE_TYPE (init) == type))
1284 init = cp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1286 expand_assignment (exp, init, 0, 0);
1290 if (init == NULL_TREE
1291 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1295 init = TREE_VALUE (parms);
1297 else if (! flag_ansi_overloading
1298 && TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init)
1299 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (init)))
1301 rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
1302 TREE_USED (rval) = 1;
1303 expand_expr_stmt (rval);
1307 parms = build_tree_list (NULL_TREE, init);
1309 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1311 if (true_exp == exp)
1312 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1314 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1315 flags |= LOOKUP_HAS_IN_CHARGE;
1318 if (flag_ansi_overloading)
1320 rval = build_method_call (exp, ctor_identifier,
1321 parms, binfo, flags);
1322 expand_expr_stmt (rval);
1326 if (init && TREE_CHAIN (parms) == NULL_TREE
1327 && TYPE_HAS_TRIVIAL_INIT_REF (type)
1328 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (init)))
1330 rval = build (INIT_EXPR, type, exp, init);
1331 TREE_SIDE_EFFECTS (rval) = 1;
1332 expand_expr_stmt (rval);
1336 if (flags & LOOKUP_ONLYCONVERTING)
1337 flags |= LOOKUP_NO_CONVERSION;
1338 rval = build_method_call (exp, ctor_identifier,
1339 parms, binfo, flags);
1341 /* Private, protected, or otherwise unavailable. */
1342 if (rval == error_mark_node)
1344 if (flags & LOOKUP_COMPLAIN)
1345 cp_error ("in base initialization for %sclass `%T'",
1346 TREE_VIA_VIRTUAL (binfo) ? "virtual base " : "",
1349 else if (rval == NULL_TREE)
1350 my_friendly_abort (361);
1353 /* p. 222: if the base class assigns to `this', then that
1354 value is used in the derived class. */
1355 if ((flag_this_is_variable & 1) && alias_this)
1357 TREE_TYPE (rval) = TREE_TYPE (current_class_ptr);
1358 expand_assignment (current_class_ptr, rval, 0, 0);
1361 expand_expr_stmt (rval);
1366 /* This function is responsible for initializing EXP with INIT
1369 BINFO is the binfo of the type for who we are performing the
1370 initialization. For example, if W is a virtual base class of A and B,
1372 If we are initializing B, then W must contain B's W vtable, whereas
1373 were we initializing C, W must contain C's W vtable.
1375 TRUE_EXP is nonzero if it is the true expression being initialized.
1376 In this case, it may be EXP, or may just contain EXP. The reason we
1377 need this is because if EXP is a base element of TRUE_EXP, we
1378 don't necessarily know by looking at EXP where its virtual
1379 baseclass fields should really be pointing. But we do know
1380 from TRUE_EXP. In constructors, we don't know anything about
1381 the value being initialized.
1383 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1385 FLAGS is just passes to `build_method_call'. See that function for
1389 expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1396 tree type = TREE_TYPE (exp);
1397 tree init_type = NULL_TREE;
1399 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1401 /* Use a function returning the desired type to initialize EXP for us.
1402 If the function is a constructor, and its first argument is
1403 NULL_TREE, know that it was meant for us--just slide exp on
1404 in and expand the constructor. Constructors now come
1407 if (init && TREE_CODE (exp) == VAR_DECL
1408 && TREE_CODE (init) == CONSTRUCTOR
1409 && TREE_HAS_CONSTRUCTOR (init))
1411 tree t = store_init_value (exp, init);
1414 expand_decl_init (exp);
1417 t = build (INIT_EXPR, type, exp, init);
1418 TREE_SIDE_EFFECTS (t) = 1;
1419 expand_expr_stmt (t);
1423 if (init && ! flag_ansi_overloading)
1425 tree init_list = NULL_TREE;
1427 if (TREE_CODE (init) == TREE_LIST)
1430 if (TREE_CHAIN (init) == NULL_TREE)
1431 init = TREE_VALUE (init);
1434 init_type = TREE_TYPE (init);
1436 if (TREE_CODE (init) != TREE_LIST)
1438 if (TREE_CODE (init_type) == ERROR_MARK)
1441 /* This happens when we use C++'s functional cast notation.
1442 If the types match, then just use the TARGET_EXPR
1443 directly. Otherwise, we need to create the initializer
1444 separately from the object being initialized. */
1445 if (TREE_CODE (init) == TARGET_EXPR)
1447 if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type))
1449 if (TREE_CODE (exp) == VAR_DECL
1450 || TREE_CODE (exp) == RESULT_DECL)
1451 /* Unify the initialization targets. */
1452 DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1454 DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, VOIDmode, 0);
1456 expand_expr_stmt (init);
1461 if (init_type == type && TREE_CODE (init) == CALL_EXPR)
1463 /* A CALL_EXPR is a legitimate form of initialization, so
1464 we should not print this warning message. */
1466 expand_assignment (exp, init, 0, 0);
1467 if (exp == DECL_RESULT (current_function_decl))
1469 /* Failing this assertion means that the return value
1470 from receives multiple initializations. */
1471 my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
1472 || DECL_INITIAL (exp) == error_mark_node,
1474 DECL_INITIAL (exp) = init;
1478 else if (init_type == type
1479 && TREE_CODE (init) == COND_EXPR)
1481 /* Push value to be initialized into the cond, where possible.
1482 Avoid spurious warning messages when initializing the
1483 result of this function. */
1484 TREE_OPERAND (init, 1)
1485 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1486 if (exp == DECL_RESULT (current_function_decl))
1487 DECL_INITIAL (exp) = NULL_TREE;
1488 TREE_OPERAND (init, 2)
1489 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1490 if (exp == DECL_RESULT (current_function_decl))
1491 DECL_INITIAL (exp) = init;
1492 TREE_SIDE_EFFECTS (init) = 1;
1493 expand_expr (init, const0_rtx, VOIDmode, 0);
1499 /* We did not know what we were initializing before. Now we do. */
1500 if (TREE_CODE (init) == TARGET_EXPR)
1502 tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1504 if (tmp && TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1505 && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1507 /* In order for this to work for RESULT_DECLs, if their
1508 type has a constructor, then they must be BLKmode
1509 so that they will be meaningfully addressable. */
1510 tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1511 init = TREE_OPERAND (init, 1);
1512 init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1513 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), NULL_TREE);
1514 TREE_SIDE_EFFECTS (init) = 1;
1515 TREE_VALUE (TREE_OPERAND (init, 1))
1516 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1520 expand_assignment (current_function_decl, init, 0, 0);
1523 if (exp == DECL_RESULT (current_function_decl))
1525 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1526 fatal ("return value from function receives multiple initializations");
1527 DECL_INITIAL (exp) = init;
1529 expand_expr_stmt (init);
1534 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1535 which really means: xyzzy foo = bar; Ugh!
1537 More useful for this case: xyzzy *foo = new xyzzy (bar); */
1539 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1541 if (init_list && TREE_CHAIN (init_list))
1543 warning ("initializer list being treated as compound expression");
1544 init = convert (type, build_compound_expr (init_list));
1545 if (init == error_mark_node)
1549 expand_assignment (exp, init, 0, 0);
1554 /* If this is copy-initialization, see whether we can go through a
1555 type conversion operator. */
1556 if (TREE_CODE (init) != TREE_LIST && (flags & LOOKUP_ONLYCONVERTING))
1558 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1559 ? TREE_TYPE (init_type) : init_type;
1561 if (ttype != type && IS_AGGR_TYPE (ttype))
1563 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 1);
1567 /* See if there is a constructor for``type'' that takes a
1568 ``ttype''-typed object. */
1569 tree parms = build_tree_list (NULL_TREE, init);
1570 tree as_cons = NULL_TREE;
1571 if (TYPE_HAS_CONSTRUCTOR (type))
1572 as_cons = build_method_call (exp, ctor_identifier,
1574 LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1575 if (as_cons != NULL_TREE && as_cons != error_mark_node)
1576 /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1577 cp_error ("ambiguity between conversion to `%T' and constructor",
1580 if (rval != error_mark_node)
1581 expand_aggr_init_1 (binfo, true_exp, exp, rval, alias_this, flags);
1588 /* We know that expand_default_init can handle everything we want
1590 expand_default_init (binfo, true_exp, exp, init, alias_this, flags);
1593 /* Report an error if NAME is not the name of a user-defined,
1594 aggregate type. If OR_ELSE is nonzero, give an error message. */
1597 is_aggr_typedef (name, or_else)
1603 if (name == error_mark_node)
1606 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1607 type = IDENTIFIER_TYPE_VALUE (name);
1611 cp_error ("`%T' is not an aggregate typedef", name);
1615 if (! IS_AGGR_TYPE (type)
1616 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1619 cp_error ("`%T' is not an aggregate type", type);
1625 /* Report an error if TYPE is not a user-defined, aggregate type. If
1626 OR_ELSE is nonzero, give an error message. */
1629 is_aggr_type (type, or_else)
1633 if (type == error_mark_node)
1636 if (! IS_AGGR_TYPE (type)
1637 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1640 cp_error ("`%T' is not an aggregate type", type);
1646 /* Like is_aggr_typedef, but returns typedef if successful. */
1649 get_aggr_from_typedef (name, or_else)
1655 if (name == error_mark_node)
1658 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1659 type = IDENTIFIER_TYPE_VALUE (name);
1663 cp_error ("`%T' fails to be an aggregate typedef", name);
1667 if (! IS_AGGR_TYPE (type)
1668 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1671 cp_error ("type `%T' is of non-aggregate type", type);
1678 get_type_value (name)
1681 if (name == error_mark_node)
1684 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1685 return IDENTIFIER_TYPE_VALUE (name);
1691 /* This code could just as well go in `class.c', but is placed here for
1694 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1695 the appropriate function call. */
1698 build_member_call (type, name, parmlist)
1699 tree type, name, parmlist;
1702 tree method_name = name;
1704 int dont_use_this = 0;
1705 tree basetype_path, decl;
1707 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1709 method_name = TREE_OPERAND (method_name, 0);
1713 /* This shouldn't be here, and build_member_call shouldn't appear in
1715 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1716 && get_aggr_from_typedef (type, 0) == 0)
1718 tree ns = lookup_name (type, 0);
1719 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1721 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
1725 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1726 return error_mark_node;
1728 /* An operator we did not like. */
1729 if (name == NULL_TREE)
1730 return error_mark_node;
1734 cp_error ("cannot call destructor `%T::~%T' without object", type,
1736 return error_mark_node;
1739 /* No object? Then just fake one up, and let build_method_call
1740 figure out what to do. */
1741 if (current_class_type == 0
1742 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1747 basetype_path = TYPE_BINFO (type);
1748 decl = build1 (NOP_EXPR, build_pointer_type (type), error_mark_node);
1750 else if (current_class_ptr == 0)
1753 decl = build1 (NOP_EXPR, build_pointer_type (type), error_mark_node);
1757 tree olddecl = current_class_ptr;
1758 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1759 if (oldtype != type)
1761 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1762 TYPE_VOLATILE (oldtype));
1763 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
1769 decl = build_indirect_ref (decl, NULL_PTR);
1771 if (method_name == constructor_name (type)
1772 || method_name == constructor_name_full (type))
1773 return build_functional_cast (type, parmlist);
1774 if (t = lookup_fnfields (basetype_path, method_name, 0))
1775 return build_method_call (decl, method_name, parmlist, basetype_path,
1776 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1777 if (TREE_CODE (name) == IDENTIFIER_NODE
1778 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1780 if (t == error_mark_node)
1781 return error_mark_node;
1782 if (TREE_CODE (t) == FIELD_DECL)
1786 cp_error ("invalid use of non-static field `%D'", t);
1787 return error_mark_node;
1789 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1791 else if (TREE_CODE (t) == VAR_DECL)
1795 cp_error ("invalid use of member `%D'", t);
1796 return error_mark_node;
1798 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1799 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1800 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1801 return build_function_call (decl, parmlist);
1805 cp_error ("no method `%T::%D'", type, name);
1806 return error_mark_node;
1810 /* Build a reference to a member of an aggregate. This is not a
1811 C++ `&', but really something which can have its address taken,
1812 and then act as a pointer to member, for example TYPE :: FIELD
1813 can have its address taken by saying & TYPE :: FIELD.
1815 @@ Prints out lousy diagnostics for operator <typename>
1818 @@ This function should be rewritten and placed in search.c. */
1821 build_offset_ref (type, name)
1824 tree decl, fnfields, fields, t = error_mark_node;
1825 tree basebinfo = NULL_TREE;
1828 if (processing_template_decl)
1829 return build_min_nt (SCOPE_REF, type, name);
1831 /* Handle namespace names fully here. */
1832 if (TREE_CODE (type) == IDENTIFIER_NODE
1833 && get_aggr_from_typedef (type, 0) == 0)
1835 tree ns = lookup_name (type, 0);
1837 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1839 val = lookup_namespace_name (ns, name);
1842 cp_error ("namespace `%D' has no member named `%D'", ns, name);
1843 return error_mark_node;
1847 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1848 return error_mark_node;
1850 if (TREE_CODE (name) == BIT_NOT_EXPR)
1853 name = TREE_OPERAND (name, 0);
1856 if (name == constructor_name_full (type))
1857 name = constructor_name (type);
1859 if (TYPE_SIZE (complete_type (type)) == 0)
1861 if (type == current_class_type)
1862 t = IDENTIFIER_CLASS_VALUE (name);
1867 cp_error ("incomplete type `%T' does not have member `%D'", type,
1869 return error_mark_node;
1871 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
1872 || TREE_CODE (t) == CONST_DECL)
1877 if (TREE_CODE (t) == FIELD_DECL)
1878 sorry ("use of member in incomplete aggregate type");
1879 else if (TREE_CODE (t) == FUNCTION_DECL)
1880 sorry ("use of member function in incomplete aggregate type");
1882 my_friendly_abort (52);
1883 return error_mark_node;
1886 if (current_class_type == 0
1887 || get_base_distance (type, current_class_type, 0, &basebinfo) == -1)
1889 basebinfo = TYPE_BINFO (type);
1890 decl = build1 (NOP_EXPR, type, error_mark_node);
1892 else if (current_class_ptr == 0)
1893 decl = build1 (NOP_EXPR, type, error_mark_node);
1895 decl = current_class_ref;
1897 if (constructor_name (BINFO_TYPE (basebinfo)) == name)
1899 name = dtor_identifier;
1901 name = ctor_identifier;
1904 my_friendly_abort (999);
1907 fnfields = lookup_fnfields (basebinfo, name, 1);
1908 fields = lookup_field (basebinfo, name, 0, 0);
1910 if (fields == error_mark_node || fnfields == error_mark_node)
1911 return error_mark_node;
1913 /* A lot of this logic is now handled in lookup_field and
1917 extern int flag_save_memoized_contexts;
1918 basebinfo = TREE_PURPOSE (fnfields);
1920 /* Go from the TREE_BASELINK to the member function info. */
1921 t = TREE_VALUE (fnfields);
1923 if (DECL_CHAIN (t) == NULL_TREE)
1927 /* unique functions are handled easily. */
1929 access = compute_access (basebinfo, t);
1930 if (access == access_protected_node)
1932 cp_error_at ("member function `%#D' is protected", t);
1933 error ("in this context");
1934 return error_mark_node;
1936 if (access == access_private_node)
1938 cp_error_at ("member function `%#D' is private", t);
1939 error ("in this context");
1940 return error_mark_node;
1943 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1946 /* FNFIELDS is most likely allocated on the search_obstack,
1947 which will go away after this class scope. If we need
1948 to save this value for later (either for memoization
1949 or for use as an initializer for a static variable), then
1952 ??? The smart thing to do for the case of saving initializers
1953 is to resolve them before we're done with this scope. */
1954 if (!TREE_PERMANENT (fnfields)
1955 && ((flag_save_memoized_contexts && global_bindings_p ())
1956 || ! allocation_temporary_p ()))
1957 fnfields = copy_list (fnfields);
1959 t = build_tree_list (error_mark_node, fnfields);
1960 TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
1964 /* Now that we know we are looking for a field, see if we
1965 have access to that field. Lookup_field will give us the
1968 t = lookup_field (basebinfo, name, 1, 0);
1970 if (t == error_mark_node)
1971 return error_mark_node;
1975 cp_error ("`%D' is not a member of type `%T'", name, type);
1976 return error_mark_node;
1979 if (TREE_CODE (t) == TYPE_DECL)
1984 /* static class members and class-specific enum
1985 values can be returned without further ado. */
1986 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1992 if (TREE_CODE (t) == FIELD_DECL && DECL_BIT_FIELD (t))
1994 cp_error ("illegal pointer to bit field `%D'", t);
1995 return error_mark_node;
1998 /* static class functions too. */
1999 if (TREE_CODE (t) == FUNCTION_DECL
2000 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2001 my_friendly_abort (53);
2003 /* In member functions, the form `type::name' is no longer
2004 equivalent to `this->type::name', at least not until
2005 resolve_offset_ref. */
2006 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2009 /* If a OFFSET_REF made it through to here, then it did
2010 not have its address taken. */
2013 resolve_offset_ref (exp)
2016 tree type = TREE_TYPE (exp);
2017 tree base = NULL_TREE;
2019 tree basetype, addr;
2021 if (TREE_CODE (exp) == TREE_LIST)
2022 return build_unary_op (ADDR_EXPR, exp, 0);
2024 if (TREE_CODE (exp) == OFFSET_REF)
2026 member = TREE_OPERAND (exp, 1);
2027 base = TREE_OPERAND (exp, 0);
2031 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2032 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2034 error ("object missing in use of pointer-to-member construct");
2035 return error_mark_node;
2038 type = TREE_TYPE (type);
2039 base = current_class_ref;
2042 if ((TREE_CODE (member) == VAR_DECL
2043 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2044 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE
2045 || TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2047 /* These were static members. */
2048 if (mark_addressable (member) == 0)
2049 return error_mark_node;
2053 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
2054 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
2057 /* Syntax error can cause a member which should
2058 have been seen as static to be grok'd as non-static. */
2059 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
2061 if (TREE_ADDRESSABLE (member) == 0)
2063 cp_error_at ("member `%D' is non-static but referenced as a static member",
2065 error ("at this point in file");
2066 TREE_ADDRESSABLE (member) = 1;
2068 return error_mark_node;
2071 /* The first case is really just a reference to a member of `this'. */
2072 if (TREE_CODE (member) == FIELD_DECL
2073 && (base == current_class_ref
2074 || (TREE_CODE (base) == NOP_EXPR
2075 && TREE_OPERAND (base, 0) == error_mark_node)))
2077 tree basetype_path, access;
2079 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
2080 basetype = TYPE_OFFSET_BASETYPE (type);
2082 basetype = DECL_CONTEXT (member);
2084 base = current_class_ptr;
2086 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2088 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2089 return error_mark_node;
2091 addr = convert_pointer_to (basetype, base);
2092 access = compute_access (basetype_path, member);
2093 if (access == access_public_node)
2094 return build (COMPONENT_REF, TREE_TYPE (member),
2095 build_indirect_ref (addr, NULL_PTR), member);
2096 if (access == access_protected_node)
2098 cp_error_at ("member `%D' is protected", member);
2099 error ("in this context");
2100 return error_mark_node;
2102 if (access == access_private_node)
2104 cp_error_at ("member `%D' is private", member);
2105 error ("in this context");
2106 return error_mark_node;
2108 my_friendly_abort (55);
2111 /* Ensure that we have an object. */
2112 if (TREE_CODE (base) == NOP_EXPR
2113 && TREE_OPERAND (base, 0) == error_mark_node)
2114 addr = error_mark_node;
2117 /* If this is a reference to a member function, then return the
2118 address of the member function (which may involve going
2119 through the object's vtable), otherwise, return an expression
2120 for the dereferenced pointer-to-member construct. */
2121 addr = build_unary_op (ADDR_EXPR, base, 0);
2124 if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2126 if (addr == error_mark_node)
2128 cp_error ("object missing in `%E'", exp);
2129 return error_mark_node;
2132 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2133 addr = convert_pointer_to (basetype, addr);
2134 member = convert (ptrdiff_type_node,
2135 build_unary_op (ADDR_EXPR, member, 0));
2137 /* Pointer to data mebers are offset by one, so that a null
2138 pointer with a real value of 0 is distinguishable from an
2139 offset of the first member of a structure. */
2140 member = build_binary_op (MINUS_EXPR, member,
2141 convert (ptrdiff_type_node, integer_one_node),
2144 return build1 (INDIRECT_REF, type,
2145 build (PLUS_EXPR, build_pointer_type (type),
2148 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2150 return get_member_function_from_ptrfunc (&addr, member);
2152 my_friendly_abort (56);
2157 /* Return either DECL or its known constant value (if it has one). */
2160 decl_constant_value (decl)
2163 if (! TREE_THIS_VOLATILE (decl)
2165 /* These may be necessary for C, but they break C++. */
2166 ! TREE_PUBLIC (decl)
2167 /* Don't change a variable array bound or initial value to a constant
2168 in a place where a variable is invalid. */
2171 && DECL_INITIAL (decl) != 0
2172 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2173 /* This is invalid if initial value is not constant.
2174 If it has either a function call, a memory reference,
2175 or a variable, then re-evaluating it could give different results. */
2176 && TREE_CONSTANT (DECL_INITIAL (decl))
2177 /* Check for cases where this is sub-optimal, even though valid. */
2178 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2180 /* We must allow this to work outside of functions so that
2181 static constants can be used for array sizes. */
2182 && current_function_decl != 0
2183 && DECL_MODE (decl) != BLKmode
2186 return DECL_INITIAL (decl);
2190 /* Friend handling routines. */
2191 /* Friend data structures:
2193 Lists of friend functions come from TYPE_DECL nodes. Since all
2194 aggregate types are automatically typedef'd, these nodes are guaranteed
2197 The TREE_PURPOSE of a friend list is the name of the friend,
2198 and its TREE_VALUE is another list.
2200 For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2201 will be filled in, but not both. The TREE_VALUE of that list is an
2202 individual function which is a friend. The TREE_PURPOSE of that list
2203 indicates a type in which all functions by that name are friends.
2205 Lists of friend classes come from _TYPE nodes. Love that consistency
2209 is_friend_type (type1, type2)
2212 return is_friend (type1, type2);
2216 is_friend (type, supplicant)
2217 tree type, supplicant;
2222 if (supplicant == NULL_TREE || type == NULL_TREE)
2225 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2228 /* It's a function decl. */
2230 tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2231 tree name = DECL_NAME (supplicant);
2234 if (DECL_FUNCTION_MEMBER_P (supplicant))
2235 ctype = DECL_CLASS_CONTEXT (supplicant);
2239 for (; list ; list = TREE_CHAIN (list))
2241 if (name == TREE_PURPOSE (list))
2243 tree friends = TREE_VALUE (list);
2244 for (; friends ; friends = TREE_CHAIN (friends))
2246 if (ctype == TREE_PURPOSE (friends))
2248 if (comptypes (TREE_TYPE (supplicant),
2249 TREE_TYPE (TREE_VALUE (friends)), 1))
2259 if (type == supplicant)
2262 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2263 for (; list ; list = TREE_CHAIN (list))
2264 if (supplicant == TREE_VALUE (list))
2273 /* Are we a nested or local class? If so, we aren't friends
2274 with the CONTEXT. */
2275 if (IS_AGGR_TYPE (supplicant))
2276 context = NULL_TREE;
2278 context = DECL_CONTEXT (TYPE_NAME (supplicant));
2280 else if (DECL_FUNCTION_MEMBER_P (supplicant))
2281 context = DECL_CLASS_CONTEXT (supplicant);
2283 context = NULL_TREE;
2286 return is_friend (type, context);
2292 /* Add a new friend to the friends of the aggregate type TYPE.
2293 DECL is the FUNCTION_DECL of the friend being added. */
2296 add_friend (type, decl)
2299 tree typedecl = TYPE_NAME (type);
2300 tree list = DECL_FRIENDLIST (typedecl);
2301 tree name = DECL_NAME (decl);
2305 if (name == TREE_PURPOSE (list))
2307 tree friends = TREE_VALUE (list);
2308 for (; friends ; friends = TREE_CHAIN (friends))
2310 if (decl == TREE_VALUE (friends))
2312 cp_warning ("`%D' is already a friend of class `%T'",
2314 cp_warning_at ("previous friend declaration of `%D'",
2315 TREE_VALUE (friends));
2319 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2323 list = TREE_CHAIN (list);
2325 DECL_FRIENDLIST (typedecl)
2326 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2327 DECL_FRIENDLIST (typedecl));
2328 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2330 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2331 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2332 if (parmtypes && TREE_CHAIN (parmtypes))
2334 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2335 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2336 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2337 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2342 /* Declare that every member function NAME in FRIEND_TYPE
2343 (which may be NULL_TREE) is a friend of type TYPE. */
2346 add_friends (type, name, friend_type)
2347 tree type, name, friend_type;
2349 tree typedecl = TYPE_NAME (type);
2350 tree list = DECL_FRIENDLIST (typedecl);
2354 if (name == TREE_PURPOSE (list))
2356 tree friends = TREE_VALUE (list);
2357 while (friends && TREE_PURPOSE (friends) != friend_type)
2358 friends = TREE_CHAIN (friends);
2361 warning ("method `%s::%s' is already a friend of class",
2362 TYPE_NAME_STRING (friend_type),
2363 IDENTIFIER_POINTER (name));
2365 warning ("function `%s' is already a friend of class `%s'",
2366 IDENTIFIER_POINTER (name),
2367 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2369 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2373 list = TREE_CHAIN (list);
2375 DECL_FRIENDLIST (typedecl) =
2377 build_tree_list (friend_type, NULL_TREE),
2378 DECL_FRIENDLIST (typedecl));
2379 if (! strncmp (IDENTIFIER_POINTER (name),
2380 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2381 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2383 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2384 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2388 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2389 been defined, we make all of its member functions friends of
2390 TYPE. If not, we make it a pending friend, which can later be added
2391 when its definition is seen. If a type is defined, then its TYPE_DECL's
2392 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2393 classes that are not defined. If a type has not yet been defined,
2394 then the DECL_WAITING_FRIENDS contains a list of types
2395 waiting to make it their friend. Note that these two can both
2396 be in use at the same time! */
2399 make_friend_class (type, friend_type)
2400 tree type, friend_type;
2404 if (IS_SIGNATURE (type))
2406 error ("`friend' declaration in signature definition");
2409 if (IS_SIGNATURE (friend_type))
2411 error ("signature type `%s' declared `friend'",
2412 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2415 if (type == friend_type)
2417 pedwarn ("class `%s' is implicitly friends with itself",
2418 TYPE_NAME_STRING (type));
2422 GNU_xref_hier (TYPE_NAME_STRING (type),
2423 TYPE_NAME_STRING (friend_type), 0, 0, 1);
2425 classes = CLASSTYPE_FRIEND_CLASSES (type);
2426 while (classes && TREE_VALUE (classes) != friend_type)
2427 classes = TREE_CHAIN (classes);
2429 warning ("class `%s' is already friends with class `%s'",
2430 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2433 CLASSTYPE_FRIEND_CLASSES (type)
2434 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2438 /* Main friend processor. This is large, and for modularity purposes,
2439 has been removed from grokdeclarator. It returns `void_type_node'
2440 to indicate that something happened, though a FIELD_DECL is
2443 CTYPE is the class this friend belongs to.
2445 DECLARATOR is the name of the friend.
2447 DECL is the FUNCTION_DECL that the friend is.
2449 In case we are parsing a friend which is part of an inline
2450 definition, we will need to store PARM_DECL chain that comes
2451 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2453 FLAGS is just used for `grokclassfn'.
2455 QUALS say what special qualifies should apply to the object
2456 pointed to by `this'. */
2459 do_friend (ctype, declarator, decl, parmdecls, flags, quals, funcdef_flag)
2460 tree ctype, declarator, decl, parmdecls;
2461 enum overload_flags flags;
2465 /* Every decl that gets here is a friend of something. */
2466 DECL_FRIEND_P (decl) = 1;
2470 tree cname = TYPE_NAME (ctype);
2471 if (TREE_CODE (cname) == TYPE_DECL)
2472 cname = DECL_NAME (cname);
2474 /* A method friend. */
2475 if (TREE_CODE (decl) == FUNCTION_DECL)
2477 if (flags == NO_SPECIAL && ctype && declarator == cname)
2478 DECL_CONSTRUCTOR_P (decl) = 1;
2480 /* This will set up DECL_ARGUMENTS for us. */
2481 grokclassfn (ctype, cname, decl, flags, quals);
2482 if (TYPE_SIZE (ctype) != 0)
2483 decl = check_classfn (ctype, decl);
2485 if (TREE_TYPE (decl) != error_mark_node)
2487 if (TYPE_SIZE (ctype))
2488 add_friend (current_class_type, decl);
2491 cp_error ("member `%D' declared as friend before type `%T' defined",
2498 /* Possibly a bunch of method friends. */
2500 /* Get the class they belong to. */
2501 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2502 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2505 add_friends (current_class_type, declarator, ctype);
2507 error ("method `%s' is not a member of class `%s'",
2508 IDENTIFIER_POINTER (declarator),
2509 IDENTIFIER_POINTER (cname));
2510 decl = void_type_node;
2513 else if (TREE_CODE (decl) == FUNCTION_DECL
2514 && ((IDENTIFIER_LENGTH (declarator) == 4
2515 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2516 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2517 || (IDENTIFIER_LENGTH (declarator) > 10
2518 && IDENTIFIER_POINTER (declarator)[0] == '_'
2519 && IDENTIFIER_POINTER (declarator)[1] == '_'
2520 && strncmp (IDENTIFIER_POINTER (declarator)+2,
2521 "builtin_", 8) == 0)))
2523 /* raw "main", and builtin functions never gets overloaded,
2524 but they can become friends. */
2525 add_friend (current_class_type, decl);
2526 DECL_FRIEND_P (decl) = 1;
2527 decl = void_type_node;
2530 @@ or possibly a friend from a base class ?!? */
2531 else if (TREE_CODE (decl) == FUNCTION_DECL)
2533 /* Friends must all go through the overload machinery,
2534 even though they may not technically be overloaded.
2536 Note that because classes all wind up being top-level
2537 in their scope, their friend wind up in top-level scope as well. */
2538 DECL_ASSEMBLER_NAME (decl)
2539 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2540 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2541 DECL_ARGUMENTS (decl) = parmdecls;
2543 DECL_CLASS_CONTEXT (decl) = current_class_type;
2545 /* We can call pushdecl here, because the TREE_CHAIN of this
2546 FUNCTION_DECL is not needed for other purposes. */
2547 decl = pushdecl (decl);
2549 make_decl_rtl (decl, NULL_PTR, 1);
2550 add_friend (current_class_type, decl);
2552 DECL_FRIEND_P (decl) = 1;
2556 /* @@ Should be able to ingest later definitions of this function
2558 tree decl = lookup_name_nonclass (declarator);
2559 if (decl == NULL_TREE)
2561 warning ("implicitly declaring `%s' as struct",
2562 IDENTIFIER_POINTER (declarator));
2563 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2564 decl = TYPE_NAME (decl);
2567 /* Allow abbreviated declarations of overloaded functions,
2568 but not if those functions are really class names. */
2569 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2571 warning ("`friend %s' archaic, use `friend class %s' instead",
2572 IDENTIFIER_POINTER (declarator),
2573 IDENTIFIER_POINTER (declarator));
2574 decl = TREE_TYPE (TREE_PURPOSE (decl));
2577 if (TREE_CODE (decl) == TREE_LIST)
2578 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2580 make_friend_class (current_class_type, TREE_TYPE (decl));
2581 decl = void_type_node;
2586 /* Common subroutines of build_new and build_vec_delete. */
2588 /* Common interface for calling "builtin" functions that are not
2592 build_builtin_call (type, node, arglist)
2597 tree rval = build (CALL_EXPR, type, node, arglist, NULL_TREE);
2598 TREE_SIDE_EFFECTS (rval) = 1;
2599 assemble_external (TREE_OPERAND (node, 0));
2600 TREE_USED (TREE_OPERAND (node, 0)) = 1;
2604 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2605 (which needs to go through some sort of groktypename) or it
2606 is the name of the class we are newing. INIT is an initialization value.
2607 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2608 If INIT is void_type_node, it means do *not* call a constructor
2611 For types with constructors, the data returned is initialized
2612 by the appropriate constructor.
2614 Whether the type has a constructor or not, if it has a pointer
2615 to a virtual function table, then that pointer is set up
2618 Unless I am mistaken, a call to new () will return initialized
2619 data regardless of whether the constructor itself is private or
2620 not. NOPE; new fails if the constructor is private (jcm).
2622 Note that build_new does nothing to assure that any special
2623 alignment requirements of the type are met. Rather, it leaves
2624 it up to malloc to do the right thing. Otherwise, folding to
2625 the right alignment cal cause problems if the user tries to later
2626 free the memory returned by `new'.
2628 PLACEMENT is the `placement' list for user-defined operator new (). */
2630 extern int flag_check_new;
2633 build_new (placement, decl, init, use_global_new)
2638 tree type, true_type, size, rval;
2640 tree alloc_expr, alloc_temp;
2642 enum tree_code code = NEW_EXPR;
2645 tree pending_sizes = NULL_TREE;
2647 if (decl == error_mark_node)
2648 return error_mark_node;
2650 if (TREE_CODE (decl) == TREE_LIST)
2652 tree absdcl = TREE_VALUE (decl);
2653 tree last_absdcl = NULL_TREE;
2654 int old_immediate_size_expand;
2656 if (current_function_decl
2657 && DECL_CONSTRUCTOR_P (current_function_decl))
2659 old_immediate_size_expand = immediate_size_expand;
2660 immediate_size_expand = 0;
2663 nelts = integer_one_node;
2665 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
2666 my_friendly_abort (215);
2667 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2669 last_absdcl = absdcl;
2670 absdcl = TREE_OPERAND (absdcl, 0);
2673 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
2675 /* probably meant to be a vec new */
2678 while (TREE_OPERAND (absdcl, 0)
2679 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
2681 last_absdcl = absdcl;
2682 absdcl = TREE_OPERAND (absdcl, 0);
2686 this_nelts = TREE_OPERAND (absdcl, 1);
2687 if (this_nelts != error_mark_node)
2689 if (this_nelts == NULL_TREE)
2690 error ("new of array type fails to specify size");
2691 else if (processing_template_decl)
2694 absdcl = TREE_OPERAND (absdcl, 0);
2698 this_nelts = save_expr (convert (sizetype, this_nelts));
2699 absdcl = TREE_OPERAND (absdcl, 0);
2700 if (this_nelts == integer_zero_node)
2702 warning ("zero size array reserves no space");
2703 nelts = integer_zero_node;
2706 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
2710 nelts = integer_zero_node;
2714 TREE_OPERAND (last_absdcl, 0) = absdcl;
2716 TREE_VALUE (decl) = absdcl;
2718 type = true_type = groktypename (decl);
2719 if (! type || type == error_mark_node)
2721 immediate_size_expand = old_immediate_size_expand;
2722 return error_mark_node;
2725 if (current_function_decl
2726 && DECL_CONSTRUCTOR_P (current_function_decl))
2728 pending_sizes = get_pending_sizes ();
2729 immediate_size_expand = old_immediate_size_expand;
2732 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2734 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2736 /* An aggregate type. */
2737 type = IDENTIFIER_TYPE_VALUE (decl);
2738 decl = TYPE_NAME (type);
2742 /* A builtin type. */
2743 decl = lookup_name (decl, 1);
2744 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2745 type = TREE_TYPE (decl);
2749 else if (TREE_CODE (decl) == TYPE_DECL)
2751 type = TREE_TYPE (decl);
2758 decl = TYPE_NAME (type);
2761 if (processing_template_decl)
2765 t = min_tree_cons (min_tree_cons (NULL_TREE, type, NULL_TREE),
2766 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2771 rval = build_min_nt (NEW_EXPR, placement, t, init);
2772 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2776 /* ``A reference cannot be created by the new operator. A reference
2777 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2778 returned by new.'' ARM 5.3.3 */
2779 if (TREE_CODE (type) == REFERENCE_TYPE)
2781 error ("new cannot be applied to a reference type");
2782 type = true_type = TREE_TYPE (type);
2785 if (TREE_CODE (type) == FUNCTION_TYPE)
2787 error ("new cannot be applied to a function type");
2788 return error_mark_node;
2791 /* When the object being created is an array, the new-expression yields a
2792 pointer to the initial element (if any) of the array. For example,
2793 both new int and new int[10] return an int*. 5.3.4. */
2794 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2796 nelts = array_type_nelts_top (type);
2798 type = true_type = TREE_TYPE (type);
2801 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
2802 type = TYPE_MAIN_VARIANT (type);
2804 /* If our base type is an array, then make sure we know how many elements
2806 while (TREE_CODE (true_type) == ARRAY_TYPE)
2808 tree this_nelts = array_type_nelts_top (true_type);
2809 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
2810 true_type = TREE_TYPE (true_type);
2813 if (TYPE_SIZE (complete_type (true_type)) == 0)
2815 incomplete_type_error (0, true_type);
2816 return error_mark_node;
2820 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2823 size = size_in_bytes (type);
2825 if (true_type == void_type_node)
2827 error ("invalid type `void' for new");
2828 return error_mark_node;
2831 if (TYPE_LANG_SPECIFIC (true_type)
2832 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
2834 abstract_virtuals_error (NULL_TREE, true_type);
2835 return error_mark_node;
2838 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
2840 signature_error (NULL_TREE, true_type);
2841 return error_mark_node;
2845 /* Get a little extra space to store a couple of things before the new'ed
2846 array, if this isn't the default placement new. */
2848 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)
2849 && ! (placement && ! TREE_CHAIN (placement)
2850 && TREE_TYPE (TREE_VALUE (placement)) == ptr_type_node));
2852 /* Get a little extra space to store a couple of things before the new'ed
2853 array, if this is either non-placement new or new (nothrow). */
2855 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)
2857 || (IS_AGGR_TYPE (TREE_TYPE (placement))
2858 && (TYPE_IDENTIFIER (TREE_TYPE (placement))
2859 == get_identifier ("nothrow_t")))));
2864 tree extra = BI_header_size;
2866 size = size_binop (PLUS_EXPR, size, extra);
2871 code = VEC_NEW_EXPR;
2873 if (init && pedantic)
2874 cp_pedwarn ("initialization in array new");
2877 /* Allocate the object. */
2878 if (! use_global_new && TYPE_LANG_SPECIFIC (true_type)
2879 && (TYPE_GETS_NEW (true_type) & (1 << has_array)))
2880 rval = build_opfncall (code, LOOKUP_NORMAL,
2881 build_pointer_type (true_type), size, placement);
2884 rval = build_opfncall (code, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
2885 ptr_type_node, size, placement);
2886 rval = convert (build_pointer_type (true_type), rval);
2888 else if (! has_array && flag_this_is_variable > 0
2889 && TYPE_NEEDS_CONSTRUCTING (true_type) && init != void_type_node)
2891 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
2895 error ("constructors take parameter lists");
2896 return error_mark_node;
2901 rval = build_builtin_call (build_pointer_type (true_type),
2902 has_array ? BIVN : BIN,
2903 build_tree_list (NULL_TREE, size));
2904 TREE_CALLS_NEW (rval) = 1;
2907 if (flag_check_new && rval)
2908 alloc_expr = rval = save_expr (rval);
2910 alloc_expr = NULL_TREE;
2912 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2913 sure we have some extra bytes in that case for the BI_header_size
2914 cookies? And how does that interact with the code below? (mrs) */
2915 /* Finish up some magic for new'ed arrays */
2916 if (use_cookie && rval != NULL_TREE)
2918 tree extra = BI_header_size;
2920 rval = convert (ptr_type_node, rval); /* convert to void * first */
2921 rval = convert (string_type_node, rval); /* lets not add void* and ints */
2922 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
2923 /* Store header info. */
2924 cookie = build_indirect_ref (build (MINUS_EXPR, build_pointer_type (BI_header_type),
2925 rval, extra), NULL_PTR);
2926 exp1 = build (MODIFY_EXPR, void_type_node,
2927 build_component_ref (cookie, nc_nelts_field_id, NULL_TREE, 0),
2929 TREE_SIDE_EFFECTS (exp1) = 1;
2930 rval = convert (build_pointer_type (true_type), rval);
2931 TREE_CALLS_NEW (rval) = 1;
2932 TREE_SIDE_EFFECTS (rval) = 1;
2933 rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
2934 build_tree_list (NULL_TREE, rval)));
2937 if (rval == error_mark_node)
2938 return error_mark_node;
2940 /* Don't call any constructors or do any initialization. */
2941 if (init == void_type_node)
2944 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2946 if (! TYPE_NEEDS_CONSTRUCTING (type)
2947 && ! IS_AGGR_TYPE (type) && ! has_array)
2949 /* New 2.0 interpretation: `new int (10)' means
2950 allocate an int, and initialize it with 10. */
2953 rval = save_expr (rval);
2954 deref = build_indirect_ref (rval, NULL_PTR);
2955 TREE_READONLY (deref) = 0;
2957 if (TREE_CHAIN (init) != NULL_TREE)
2958 pedwarn ("initializer list being treated as compound expression");
2959 else if (TREE_CODE (init) == CONSTRUCTOR)
2961 pedwarn ("initializer list appears where operand should be used");
2962 init = TREE_OPERAND (init, 1);
2964 init = build_compound_expr (init);
2966 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2967 "new", NULL_TREE, 0);
2968 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2969 build_modify_expr (deref, NOP_EXPR, init),
2971 TREE_NO_UNUSED_WARNING (rval) = 1;
2972 TREE_SIDE_EFFECTS (rval) = 1;
2973 TREE_CALLS_NEW (rval) = 1;
2975 else if (! has_array)
2978 /* Constructors are never virtual. If it has an initialization, we
2979 need to complain if we aren't allowed to use the ctor that took
2981 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2983 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
2985 init = tree_cons (NULL_TREE, integer_one_node, init);
2986 flags |= LOOKUP_HAS_IN_CHARGE;
2991 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2992 newrval = build_indirect_ref (newrval, NULL_PTR);
2994 newrval = build_method_call (newrval, ctor_identifier,
2995 init, TYPE_BINFO (true_type), flags);
3000 TREE_HAS_CONSTRUCTOR (rval) = 1;
3003 rval = error_mark_node;
3006 rval = build (VEC_INIT_EXPR, TREE_TYPE (rval),
3007 save_expr (rval), init, nelts);
3009 else if (current_function_decl == NULL_TREE)
3011 extern tree static_aggregates;
3013 /* In case of static initialization, SAVE_EXPR is good enough. */
3014 rval = save_expr (rval);
3015 rval = copy_to_permanent (rval);
3016 init = copy_to_permanent (init);
3017 init = expand_vec_init (decl, rval,
3018 build_binary_op (MINUS_EXPR, nelts,
3019 integer_one_node, 1),
3021 init = copy_to_permanent (init);
3022 static_aggregates = perm_tree_cons (init, rval, static_aggregates);
3026 /* Have to wrap this in RTL_EXPR for two cases:
3027 in base or member initialization and if we
3028 are a branch of a ?: operator. Since we
3029 can't easily know the latter, just do it always. */
3030 tree xval = make_node (RTL_EXPR);
3032 /* If we want to check the value of the allocation expression,
3033 and the number of elements in the array is not a constant, we
3034 *must* expand the SAVE_EXPR for nelts in alloc_expr before we
3035 expand it in the actual initialization. So we need to build up
3036 an RTL_EXPR for alloc_expr. Sigh. */
3037 if (alloc_expr && ! TREE_CONSTANT (nelts))
3039 tree xval = make_node (RTL_EXPR);
3041 TREE_TYPE (xval) = TREE_TYPE (alloc_expr);
3042 do_pending_stack_adjust ();
3043 start_sequence_for_rtl_expr (xval);
3045 rtxval = expand_expr (alloc_expr, NULL_RTX, VOIDmode, 0);
3046 do_pending_stack_adjust ();
3047 TREE_SIDE_EFFECTS (xval) = 1;
3048 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3050 RTL_EXPR_RTL (xval) = rtxval;
3051 TREE_TYPE (xval) = TREE_TYPE (alloc_expr);
3055 TREE_TYPE (xval) = TREE_TYPE (rval);
3056 do_pending_stack_adjust ();
3057 start_sequence_for_rtl_expr (xval);
3059 /* As a matter of principle, `start_sequence' should do this. */
3062 rval = save_expr (rval);
3063 rval = expand_vec_init (decl, rval,
3064 build_binary_op (MINUS_EXPR, nelts,
3065 integer_one_node, 1),
3068 do_pending_stack_adjust ();
3070 TREE_SIDE_EFFECTS (xval) = 1;
3071 TREE_CALLS_NEW (xval) = 1;
3072 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3075 if (TREE_CODE (rval) == SAVE_EXPR)
3077 /* Errors may cause this to not get evaluated. */
3078 if (SAVE_EXPR_RTL (rval) == 0)
3079 SAVE_EXPR_RTL (rval) = const0_rtx;
3080 RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
3084 my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
3085 RTL_EXPR_RTL (xval) = DECL_RTL (rval);
3091 else if (TYPE_READONLY (true_type))
3092 cp_error ("uninitialized const in `new' of `%#T'", true_type);
3096 if (alloc_expr && rval != alloc_expr)
3098 /* Did we modify the storage? */
3099 tree ifexp = build_binary_op (NE_EXPR, alloc_expr,
3100 integer_zero_node, 1);
3101 rval = build_conditional_expr (ifexp, rval, alloc_expr);
3104 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
3106 /* The type of new int [3][3] is not int *, but int [3] * */
3107 rval = build_c_cast (build_pointer_type (type), rval);
3111 rval = build_compound_expr (chainon (pending_sizes,
3112 build_tree_list (NULL_TREE, rval)));
3118 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
3120 tree base, maxindex, type;
3121 tree auto_delete_vec, auto_delete;
3122 int use_global_delete;
3125 tree ptype = build_pointer_type (type = complete_type (type));
3126 tree size_exp = size_in_bytes (type);
3128 /* Temporary variables used by the loop. */
3129 tree tbase, tbase_init;
3131 /* This is the body of the loop that implements the deletion of a
3132 single element, and moves temp variables to next elements. */
3135 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3138 /* This is the thing that governs what to do after the loop has run. */
3139 tree deallocate_expr = 0;
3141 /* This is the BIND_EXPR which holds the outermost iterator of the
3142 loop. It is convenient to set this variable up and test it before
3143 executing any other code in the loop.
3144 This is also the containing expression returned by this function. */
3145 tree controller = NULL_TREE;
3147 /* This is the BLOCK to record the symbol binding for debugging. */
3150 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
3152 loop = integer_zero_node;
3156 /* The below is short by BI_header_size */
3157 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
3159 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
3160 tbase_init = build_modify_expr (tbase, NOP_EXPR,
3161 fold (build (PLUS_EXPR, ptype,
3164 DECL_REGISTER (tbase) = 1;
3165 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
3166 TREE_SIDE_EFFECTS (controller) = 1;
3167 block = build_block (tbase, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
3168 add_block_current_level (block);
3170 if (auto_delete != integer_zero_node
3171 && auto_delete != integer_two_node)
3173 tree base_tbd = convert (ptype,
3174 build_binary_op (MINUS_EXPR,
3175 convert (ptr_type_node, base),
3178 /* This is the real size */
3179 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
3180 body = build_tree_list (NULL_TREE,
3181 build_x_delete (ptype, base_tbd,
3182 2 | use_global_delete,
3184 body = build (COND_EXPR, void_type_node,
3185 build (BIT_AND_EXPR, integer_type_node,
3186 auto_delete, integer_one_node),
3187 body, integer_zero_node);
3192 body = tree_cons (NULL_TREE,
3193 build_delete (ptype, tbase, auto_delete,
3194 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
3197 body = tree_cons (NULL_TREE,
3198 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
3201 body = tree_cons (NULL_TREE,
3202 build (EXIT_EXPR, void_type_node,
3203 build (EQ_EXPR, boolean_type_node, base, tbase)),
3206 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
3208 loop = tree_cons (NULL_TREE, tbase_init,
3209 tree_cons (NULL_TREE, loop, NULL_TREE));
3210 loop = build_compound_expr (loop);
3213 /* If the delete flag is one, or anything else with the low bit set,
3214 delete the storage. */
3215 if (auto_delete_vec == integer_zero_node
3216 || auto_delete_vec == integer_two_node)
3217 deallocate_expr = integer_zero_node;
3222 /* The below is short by BI_header_size */
3223 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
3225 if (! TYPE_VEC_NEW_USES_COOKIE (type))
3230 base_tbd = convert (ptype,
3231 build_binary_op (MINUS_EXPR,
3232 convert (string_type_node, base),
3235 /* True size with header. */
3236 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
3238 deallocate_expr = build_x_delete (ptype, base_tbd,
3239 2 | use_global_delete,
3241 if (auto_delete_vec != integer_one_node)
3242 deallocate_expr = build (COND_EXPR, void_type_node,
3243 build (BIT_AND_EXPR, integer_type_node,
3244 auto_delete_vec, integer_one_node),
3245 deallocate_expr, integer_zero_node);
3248 if (loop && deallocate_expr != integer_zero_node)
3250 body = tree_cons (NULL_TREE, loop,
3251 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
3252 body = build_compound_expr (body);
3257 /* Outermost wrapper: If pointer is null, punt. */
3258 body = build (COND_EXPR, void_type_node,
3259 build (NE_EXPR, boolean_type_node, base, integer_zero_node),
3260 body, integer_zero_node);
3261 body = build1 (NOP_EXPR, void_type_node, body);
3265 TREE_OPERAND (controller, 1) = body;
3269 return convert (void_type_node, body);
3272 /* Build a tree to cleanup partially built arrays.
3273 BASE is that starting address of the array.
3274 COUNT is the count of objects that have been built, that need destroying.
3275 TYPE is the type of elements in the array. */
3278 build_array_eh_cleanup (base, count, type)
3279 tree base, count, type;
3281 tree expr = build_vec_delete_1 (base, count, type, integer_two_node,
3282 integer_zero_node, 0);
3286 /* `expand_vec_init' performs initialization of a vector of aggregate
3289 DECL is passed only for error reporting, and provides line number
3290 and source file name information.
3291 BASE is the space where the vector will be.
3292 MAXINDEX is the maximum index of the array (one less than the
3293 number of elements).
3294 INIT is the (possibly NULL) initializer.
3296 FROM_ARRAY is 0 if we should init everything with INIT
3297 (i.e., every element initialized from INIT).
3298 FROM_ARRAY is 1 if we should index into INIT in parallel
3299 with initialization of DECL.
3300 FROM_ARRAY is 2 if we should index into INIT in parallel,
3301 but use assignment instead of initialization. */
3304 expand_vec_init (decl, base, maxindex, init, from_array)
3305 tree decl, base, maxindex, init;
3309 tree iterator, base2 = NULL_TREE;
3310 tree type = TREE_TYPE (TREE_TYPE (base));
3313 maxindex = convert (ptrdiff_type_node, maxindex);
3314 if (maxindex == error_mark_node)
3315 return error_mark_node;
3317 if (current_function_decl == NULL_TREE)
3319 rval = make_tree_vec (3);
3320 TREE_VEC_ELT (rval, 0) = base;
3321 TREE_VEC_ELT (rval, 1) = maxindex;
3322 TREE_VEC_ELT (rval, 2) = init;
3326 size = size_in_bytes (type);
3328 /* Set to zero in case size is <= 0. Optimizer will delete this if
3329 it is not needed. */
3330 rval = get_temp_regvar (build_pointer_type (type),
3331 convert (build_pointer_type (type), null_pointer_node));
3332 base = default_conversion (base);
3333 base = convert (build_pointer_type (type), base);
3334 expand_assignment (rval, base, 0, 0);
3335 base = get_temp_regvar (build_pointer_type (type), base);
3337 if (init != NULL_TREE && TREE_CODE (init) == TREE_LIST)
3338 init = build_compound_expr (init);
3340 if (init != NULL_TREE
3341 && TREE_CODE (init) == CONSTRUCTOR
3342 && (! decl || TREE_TYPE (init) == TREE_TYPE (decl)))
3344 /* Initialization of array from {...}. */
3345 tree elts = CONSTRUCTOR_ELTS (init);
3346 tree baseref = build1 (INDIRECT_REF, type, base);
3347 tree baseinc = build (PLUS_EXPR, build_pointer_type (type), base, size);
3348 int host_i = TREE_INT_CST_LOW (maxindex);
3350 if (IS_AGGR_TYPE (type))
3355 expand_aggr_init (baseref, TREE_VALUE (elts), 0, 0);
3357 expand_assignment (base, baseinc, 0, 0);
3358 elts = TREE_CHAIN (elts);
3360 /* Initialize any elements by default if possible. */
3363 if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3366 use_variable (DECL_RTL (base));
3370 iterator = get_temp_regvar (ptrdiff_type_node,
3371 build_int_2 (host_i, 0));
3373 goto init_by_default;
3379 expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3381 expand_assignment (base, baseinc, 0, 0);
3382 elts = TREE_CHAIN (elts);
3386 use_variable (DECL_RTL (base));
3392 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
3396 /* If initializing one array from another,
3397 initialize element by element. */
3400 /* We rely upon the below calls the do argument checking */
3401 if (decl == NULL_TREE)
3403 sorry ("initialization of array from dissimilar array type");
3404 return error_mark_node;
3408 base2 = default_conversion (init);
3409 itype = TREE_TYPE (base2);
3410 base2 = get_temp_regvar (itype, base2);
3411 itype = TREE_TYPE (itype);
3413 else if (TYPE_LANG_SPECIFIC (type)
3414 && TYPE_NEEDS_CONSTRUCTING (type)
3415 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3417 error ("initializer ends prematurely");
3418 return error_mark_node;
3422 expand_start_cond (build (GE_EXPR, boolean_type_node,
3423 iterator, integer_zero_node), 0);
3424 if (TYPE_NEEDS_DESTRUCTOR (type))
3425 expand_eh_region_start ();
3426 expand_start_loop_continue_elsewhere (1);
3430 tree to = build1 (INDIRECT_REF, type, base);
3434 from = build1 (INDIRECT_REF, itype, base2);
3438 if (from_array == 2)
3439 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3440 else if (TYPE_NEEDS_CONSTRUCTING (type))
3441 expand_aggr_init (to, from, 0, 0);
3443 expand_assignment (to, from, 0, 0);
3445 my_friendly_abort (57);
3447 else if (TREE_CODE (type) == ARRAY_TYPE)
3450 sorry ("cannot initialize multi-dimensional array with initializer");
3451 expand_vec_init (decl, build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), base),
3452 array_type_nelts (type), 0, 0);
3456 tree targ = build1 (INDIRECT_REF, type, base);
3460 rhs = convert_for_initialization (targ, type, init, LOOKUP_NORMAL,
3461 "initialization", NULL_TREE, 0);
3465 expand_aggr_init (targ, rhs, 0, 0);
3468 expand_assignment (base,
3469 build (PLUS_EXPR, build_pointer_type (type), base, size),
3472 expand_assignment (base2,
3473 build (PLUS_EXPR, build_pointer_type (type), base2, size), 0, 0);
3474 expand_loop_continue_here ();
3475 expand_exit_loop_if_false (0, build (NE_EXPR, boolean_type_node,
3476 build (PREDECREMENT_EXPR, ptrdiff_type_node, iterator, integer_one_node), minus_one));
3480 use_variable (DECL_RTL (base));
3482 use_variable (DECL_RTL (base2));
3485 if (TYPE_NEEDS_DESTRUCTOR (type) && flag_exceptions)
3487 /* We have to ensure that this can live to the cleanup
3488 expansion time, since we know it is only ever needed
3489 once, generate code now. */
3490 push_obstacks_nochange ();
3491 resume_temporary_allocation ();
3493 tree e1, e2 = make_node (RTL_EXPR);
3494 TREE_TYPE (e2) = void_type_node;
3495 RTL_EXPR_RTL (e2) = const0_rtx;
3496 TREE_SIDE_EFFECTS (e2) = 1;
3497 start_sequence_for_rtl_expr (e2);
3499 e1 = build_array_eh_cleanup
3501 build_binary_op (MINUS_EXPR, maxindex, iterator, 1),
3503 expand_expr (e1, const0_rtx, VOIDmode, 0);
3504 RTL_EXPR_SEQUENCE (e2) = get_insns ();
3506 expand_eh_region_end (e2);
3512 use_variable (DECL_RTL (iterator));
3517 use_variable (DECL_RTL (rval));
3521 /* Free up storage of type TYPE, at address ADDR.
3523 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3526 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3527 used as the second argument to operator delete. It can include
3528 things like padding and magic size cookies. It has virtual in it,
3529 because if you have a base pointer and you delete through a virtual
3530 destructor, it should be the size of the dynamic object, not the
3531 static object, see Free Store 12.5 ANSI C++ WP.
3533 This does not call any destructors. */
3536 build_x_delete (type, addr, which_delete, virtual_size)
3541 int use_global_delete = which_delete & 1;
3542 int use_vec_delete = !!(which_delete & 2);
3544 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
3546 if (! use_global_delete && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3547 && (TYPE_GETS_DELETE (TREE_TYPE (type)) & (1 << use_vec_delete)))
3548 rval = build_opfncall (code, LOOKUP_NORMAL, addr, virtual_size, NULL_TREE);
3550 rval = build_builtin_call (void_type_node, use_vec_delete ? BIVD : BID,
3551 build_tree_list (NULL_TREE, addr));
3555 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3556 ADDR is an expression which yields the store to be destroyed.
3557 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3558 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3559 virtual baseclasses.
3560 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3562 FLAGS is the logical disjunction of zero or more LOOKUP_
3563 flags. See cp-tree.h for more info.
3565 This function does not delete an object's virtual base classes. */
3568 build_delete (type, addr, auto_delete, flags, use_global_delete)
3572 int use_global_delete;
3580 if (addr == error_mark_node)
3581 return error_mark_node;
3583 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3584 set to `error_mark_node' before it gets properly cleaned up. */
3585 if (type == error_mark_node)
3586 return error_mark_node;
3588 type = TYPE_MAIN_VARIANT (type);
3590 if (TREE_CODE (type) == POINTER_TYPE)
3592 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3593 if (TYPE_SIZE (complete_type (type)) == 0)
3595 incomplete_type_error (0, type);
3596 return error_mark_node;
3598 if (TREE_CODE (type) == ARRAY_TYPE)
3600 if (! IS_AGGR_TYPE (type))
3602 /* Call the builtin operator delete. */
3603 return build_builtin_call (void_type_node, BID,
3604 build_tree_list (NULL_TREE, addr));
3606 if (TREE_SIDE_EFFECTS (addr))
3607 addr = save_expr (addr);
3609 /* throw away const and volatile on target type of addr */
3610 addr = convert_force (build_pointer_type (type), addr, 0);
3611 ref = build_indirect_ref (addr, NULL_PTR);
3614 else if (TREE_CODE (type) == ARRAY_TYPE)
3617 if (TREE_SIDE_EFFECTS (addr))
3618 addr = save_expr (addr);
3619 if (TYPE_DOMAIN (type) == NULL_TREE)
3621 error ("unknown array size in delete");
3622 return error_mark_node;
3624 return build_vec_delete (addr, array_type_nelts (type),
3625 auto_delete, integer_two_node,
3630 /* Don't check PROTECT here; leave that decision to the
3631 destructor. If the destructor is accessible, call it,
3632 else report error. */
3633 addr = build_unary_op (ADDR_EXPR, addr, 0);
3634 if (TREE_SIDE_EFFECTS (addr))
3635 addr = save_expr (addr);
3637 if (TREE_CONSTANT (addr))
3638 addr = convert_pointer_to (type, addr);
3640 addr = convert_force (build_pointer_type (type), addr, 0);
3642 if (TREE_CODE (addr) == NOP_EXPR
3643 && TREE_OPERAND (addr, 0) == current_class_ptr)
3644 ref = current_class_ref;
3646 ref = build_indirect_ref (addr, NULL_PTR);
3650 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3652 if (! TYPE_NEEDS_DESTRUCTOR (type))
3654 if (auto_delete == integer_zero_node)
3655 return void_zero_node;
3657 /* Pass the size of the object down to the operator delete() in
3658 addition to the ADDR. */
3659 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
3661 tree virtual_size = c_sizeof_nowarn (type);
3662 return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3663 virtual_size, NULL_TREE);
3666 /* Call the builtin operator delete. */
3667 return build_builtin_call (void_type_node, BID,
3668 build_tree_list (NULL_TREE, addr));
3671 /* Below, we will reverse the order in which these calls are made.
3672 If we have a destructor, then that destructor will take care
3673 of the base classes; otherwise, we must do that here. */
3674 if (TYPE_HAS_DESTRUCTOR (type))
3676 tree parms = build_tree_list (NULL_TREE, addr);
3677 tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 1));
3678 tree passed_auto_delete;
3679 tree do_delete = NULL_TREE;
3681 if (use_global_delete)
3683 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3684 auto_delete, integer_one_node));
3685 tree call = build_builtin_call
3686 (void_type_node, BID, build_tree_list (NULL_TREE, addr));
3688 cond = fold (build (COND_EXPR, void_type_node, cond,
3689 call, void_zero_node));
3690 if (cond != void_zero_node)
3693 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3694 auto_delete, integer_two_node));
3697 passed_auto_delete = auto_delete;
3699 if (flags & LOOKUP_PROTECT)
3702 tree basetypes = NULL_TREE;
3703 if (current_class_type != NULL_TREE)
3704 basetypes = get_binfo (type, current_class_type, 0);
3705 if (basetypes == NULL_TREE)
3706 basetypes = TYPE_BINFO (type);
3707 access = compute_access (basetypes, dtor);
3709 if (access == access_private_node)
3711 if (flags & LOOKUP_COMPLAIN)
3712 cp_error ("destructor for type `%T' is private in this scope", type);
3713 return error_mark_node;
3715 else if (access == access_protected_node)
3717 if (flags & LOOKUP_COMPLAIN)
3718 cp_error ("destructor for type `%T' is protected in this scope", type);
3719 return error_mark_node;
3723 /* Once we are in a destructor, try not going through
3724 the virtual function table to find the next destructor. */
3725 if (DECL_VINDEX (dtor)
3726 && ! (flags & LOOKUP_NONVIRTUAL)
3727 && TREE_CODE (auto_delete) != PARM_DECL
3728 && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
3730 tree binfo, basetype;
3731 /* The code below is probably all broken. See call.c for the
3732 complete right way to do this. this offsets may not be right
3733 in the below. (mrs) */
3734 /* This destructor must be called via virtual function table. */
3735 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 1);
3736 basetype = DECL_CLASS_CONTEXT (dtor);
3737 binfo = get_binfo (basetype,
3738 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3740 expr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3741 if (expr != TREE_VALUE (parms))
3744 ref = build_indirect_ref (expr, NULL_PTR);
3745 TREE_VALUE (parms) = expr;
3747 function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
3748 if (function == error_mark_node)
3749 return error_mark_node;
3750 TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
3751 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, passed_auto_delete);
3752 expr = build_function_call (function, parms);
3754 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3755 if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3757 /* Handle the case where a virtual destructor is
3758 being called on an item that is 0.
3760 @@ Does this really need to be done? */
3761 tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
3763 expr = build (COND_EXPR, void_type_node,
3764 ifexp, expr, void_zero_node);
3771 if ((flags & LOOKUP_DESTRUCTOR)
3772 || TREE_CODE (ref) == VAR_DECL
3773 || TREE_CODE (ref) == PARM_DECL
3774 || TREE_CODE (ref) == COMPONENT_REF
3775 || TREE_CODE (ref) == ARRAY_REF)
3776 /* These can't be 0. */
3777 ifexp = integer_one_node;
3779 /* Handle the case where a non-virtual destructor is
3780 being called on an item that is 0. */
3781 ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
3783 /* Used to mean that this destructor was known to be empty,
3784 but that's now obsolete. */
3785 my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
3787 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, passed_auto_delete);
3788 expr = build_function_call (dtor, parms);
3790 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3792 if (ifexp != integer_one_node)
3793 expr = build (COND_EXPR, void_type_node,
3794 ifexp, expr, void_zero_node);
3800 /* This can get visibilities wrong. */
3801 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3802 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3803 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3804 tree exprstmt = NULL_TREE;
3805 tree parent_auto_delete = auto_delete;
3808 /* If this type does not have a destructor, but does have
3809 operator delete, call the parent parent destructor (if any),
3810 but let this node do the deleting. Otherwise, it is ok
3811 to let the parent destructor do the deleting. */
3812 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
3814 parent_auto_delete = integer_zero_node;
3815 if (auto_delete == integer_zero_node)
3821 /* This is probably wrong. It should be the size of the
3822 virtual object being deleted. */
3823 virtual_size = c_sizeof_nowarn (type);
3825 expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3826 virtual_size, NULL_TREE);
3827 if (expr == error_mark_node)
3828 return error_mark_node;
3829 if (auto_delete != integer_one_node)
3830 cond = build (COND_EXPR, void_type_node,
3831 build (BIT_AND_EXPR, integer_type_node,
3832 auto_delete, integer_one_node),
3833 expr, void_zero_node);
3838 else if (base_binfo == NULL_TREE
3839 || (TREE_VIA_VIRTUAL (base_binfo) == 0
3840 && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
3842 cond = build (COND_EXPR, void_type_node,
3843 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3844 build_builtin_call (void_type_node, BID,
3845 build_tree_list (NULL_TREE, addr)),
3852 exprstmt = build_tree_list (NULL_TREE, cond);
3855 && ! TREE_VIA_VIRTUAL (base_binfo)
3856 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3858 tree this_auto_delete;
3860 if (BINFO_OFFSET_ZEROP (base_binfo))
3861 this_auto_delete = parent_auto_delete;
3863 this_auto_delete = integer_zero_node;
3865 expr = build_delete (build_pointer_type (BINFO_TYPE (base_binfo)), addr,
3866 this_auto_delete, flags, 0);
3867 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3870 /* Take care of the remaining baseclasses. */
3871 for (i = 1; i < n_baseclasses; i++)
3873 base_binfo = TREE_VEC_ELT (binfos, i);
3874 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3875 || TREE_VIA_VIRTUAL (base_binfo))
3878 /* May be zero offset if other baseclasses are virtual. */
3879 expr = fold (build (PLUS_EXPR, build_pointer_type (BINFO_TYPE (base_binfo)),
3880 addr, BINFO_OFFSET (base_binfo)));
3882 expr = build_delete (build_pointer_type (BINFO_TYPE (base_binfo)), expr,
3886 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3889 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3891 if (TREE_CODE (member) != FIELD_DECL)
3893 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3895 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
3896 tree this_type = TREE_TYPE (member);
3897 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3898 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3903 return build_compound_expr (exprstmt);
3904 /* Virtual base classes make this function do nothing. */
3905 return void_zero_node;
3909 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3912 build_vbase_delete (type, decl)
3915 tree vbases = CLASSTYPE_VBASECLASSES (type);
3916 tree result = NULL_TREE;
3917 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3919 my_friendly_assert (addr != error_mark_node, 222);
3923 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
3925 result = tree_cons (NULL_TREE,
3926 build_delete (TREE_TYPE (this_addr), this_addr,
3928 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3930 vbases = TREE_CHAIN (vbases);
3932 return build_compound_expr (nreverse (result));
3935 /* Build a C++ vector delete expression.
3936 MAXINDEX is the number of elements to be deleted.
3937 ELT_SIZE is the nominal size of each element in the vector.
3938 BASE is the expression that should yield the store to be deleted.
3939 This function expands (or synthesizes) these calls itself.
3940 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3941 AUTO_DELETE say whether each item in the container should be deallocated.
3943 This also calls delete for virtual baseclasses of elements of the vector.
3945 Update: MAXINDEX is no longer needed. The size can be extracted from the
3946 start of the vector for pointers, and from the type for arrays. We still
3947 use MAXINDEX for arrays because it happens to already have one of the
3948 values we'd have to extract. (We could use MAXINDEX with pointers to
3949 confirm the size, and trap if the numbers differ; not clear that it'd
3950 be worth bothering.) */
3953 build_vec_delete (base, maxindex, auto_delete_vec, auto_delete,
3955 tree base, maxindex;
3956 tree auto_delete_vec, auto_delete;
3957 int use_global_delete;
3961 if (TREE_CODE (base) == OFFSET_REF)
3962 base = resolve_offset_ref (base);
3964 type = TREE_TYPE (base);
3966 base = stabilize_reference (base);
3968 /* Since we can use base many times, save_expr it. */
3969 if (TREE_SIDE_EFFECTS (base))
3970 base = save_expr (base);
3972 if (TREE_CODE (type) == POINTER_TYPE)
3974 /* Step back one from start of vector, and read dimension. */
3975 tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
3976 base, BI_header_size);
3977 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3978 maxindex = build_component_ref (cookie, nc_nelts_field_id, NULL_TREE, 0);
3980 type = TREE_TYPE (type);
3981 while (TREE_CODE (type) == ARRAY_TYPE);
3983 else if (TREE_CODE (type) == ARRAY_TYPE)
3985 /* get the total number of things in the array, maxindex is a bad name */
3986 maxindex = array_type_nelts_total (type);
3987 while (TREE_CODE (type) == ARRAY_TYPE)
3988 type = TREE_TYPE (type);
3989 base = build_unary_op (ADDR_EXPR, base, 1);
3993 if (base != error_mark_node)
3994 error ("type to vector delete is neither pointer or array type");
3995 return error_mark_node;
3998 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,