1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
37 static void expand_aggr_vbase_init_1 PARAMS ((tree, tree, tree, tree));
38 static void construct_virtual_bases PARAMS ((tree, tree, tree, tree, tree));
39 static void expand_aggr_init_1 PARAMS ((tree, tree, tree, tree, int));
40 static void expand_default_init PARAMS ((tree, tree, tree, tree, int));
41 static tree build_vec_delete_1 PARAMS ((tree, tree, tree, tree, int));
42 static void perform_member_init PARAMS ((tree, tree, tree, int));
43 static void sort_base_init PARAMS ((tree, tree *, tree *));
44 static tree build_builtin_delete_call PARAMS ((tree));
45 static int member_init_ok_or_else PARAMS ((tree, tree, const char *));
46 static void expand_virtual_init PARAMS ((tree, tree));
47 static tree sort_member_init PARAMS ((tree));
48 static tree initializing_context PARAMS ((tree));
49 static void expand_cleanup_for_base PARAMS ((tree, tree));
50 static tree get_temp_regvar PARAMS ((tree, tree));
51 static tree dfs_initialize_vtbl_ptrs PARAMS ((tree, void *));
52 static tree build_new_1 PARAMS ((tree));
53 static tree get_cookie_size PARAMS ((tree));
54 static tree build_dtor_call PARAMS ((tree, tree, int));
56 /* Set up local variable for this file. MUST BE CALLED AFTER
57 INIT_DECL_PROCESSING. */
59 static tree BI_header_type, BI_header_size;
61 void init_init_processing ()
65 minus_one_node = build_int_2 (-1, -1);
67 /* Define the structure that holds header information for
68 arrays allocated via operator new. */
69 BI_header_type = make_aggr_type (RECORD_TYPE);
70 fields[0] = build_lang_decl (FIELD_DECL, nelts_identifier, sizetype);
72 /* Use the biggest alignment supported by the target to prevent operator
73 new from returning misaligned pointers. */
74 TYPE_ALIGN (BI_header_type) = BIGGEST_ALIGNMENT;
75 finish_builtin_type (BI_header_type, "__new_cookie", fields,
77 BI_header_size = size_in_bytes (BI_header_type);
79 ggc_add_tree_root (&BI_header_type, 1);
80 ggc_add_tree_root (&BI_header_size, 1);
83 /* We are about to generate some complex initialization code.
84 Conceptually, it is all a single expression. However, we may want
85 to include conditionals, loops, and other such statement-level
86 constructs. Therefore, we build the initialization code inside a
87 statement-expression. This function starts such an expression.
88 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
89 pass them back to finish_init_stmts when the expression is
93 begin_init_stmts (stmt_expr_p, compound_stmt_p)
95 tree *compound_stmt_p;
97 *stmt_expr_p = begin_stmt_expr ();
98 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
101 /* Finish out the statement-expression begun by the previous call to
102 begin_init_stmts. Returns the statement-expression itself. */
105 finish_init_stmts (stmt_expr, compound_stmt)
109 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
110 stmt_expr = finish_stmt_expr (stmt_expr);
112 /* To avoid spurious warnings about unused values, we set
115 TREE_USED (stmt_expr) = 1;
122 /* Called from initialize_vtbl_ptrs via dfs_walk. */
125 dfs_initialize_vtbl_ptrs (binfo, data)
129 if (!BINFO_PRIMARY_MARKED_P (binfo)
130 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
132 tree base_ptr = TREE_VALUE ((tree) data);
134 if (TREE_VIA_VIRTUAL (binfo))
135 base_ptr = convert_pointer_to_vbase (BINFO_TYPE (binfo),
139 = build_vbase_path (PLUS_EXPR,
140 build_pointer_type (BINFO_TYPE (binfo)),
145 expand_virtual_init (binfo, base_ptr);
148 SET_BINFO_MARKED (binfo);
153 /* Initialize all the vtable pointers for the hierarchy dominated by
157 initialize_vtbl_ptrs (type, addr)
161 tree list = build_tree_list (type, addr);
163 /* Walk through the hierarchy, initializing the vptr in each base
164 class. We do these in pre-order because under the new ABI we
165 can't find the virtual bases for a class until we've initialized
166 the vtbl for that class. */
167 dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs,
168 NULL, dfs_unmarked_real_bases_queue_p, list);
169 dfs_walk (TYPE_BINFO (type), dfs_unmark,
170 dfs_marked_real_bases_queue_p, type);
171 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
172 expand_indirect_vtbls_init (TYPE_BINFO (type), addr);
175 /* Subroutine of emit_base_init. */
178 perform_member_init (member, name, init, explicit)
179 tree member, name, init;
183 tree type = TREE_TYPE (member);
185 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
187 if (decl == error_mark_node)
190 /* Deal with this here, as we will get confused if we try to call the
191 assignment op for an anonymous union. This can happen in a
192 synthesized copy constructor. */
193 if (ANON_AGGR_TYPE_P (type))
195 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
196 finish_expr_stmt (init);
198 else if (TYPE_NEEDS_CONSTRUCTING (type)
199 || (init && TYPE_HAS_CONSTRUCTOR (type)))
201 /* Since `init' is already a TREE_LIST on the current_member_init_list,
202 only build it into one if we aren't already a list. */
203 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
204 init = build_tree_list (NULL_TREE, init);
207 && TREE_CODE (type) == ARRAY_TYPE
209 && TREE_CHAIN (init) == NULL_TREE
210 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
212 /* Initialization of one array from another. */
214 (build_vec_init (TREE_OPERAND (decl, 1), decl,
215 array_type_nelts (type), TREE_VALUE (init), 1));
218 finish_expr_stmt (build_aggr_init (decl, init, 0));
222 if (init == NULL_TREE)
226 /* default-initialization. */
227 if (AGGREGATE_TYPE_P (type))
229 /* This is a default initialization of an aggregate,
230 but not one of non-POD class type. We cleverly
231 notice that the initialization rules in such a
232 case are the same as for initialization with an
233 empty brace-initialization list. We don't want
234 to call build_modify_expr as that will go looking
235 for constructors and such. */
236 tree e = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
237 TREE_SIDE_EFFECTS (e) = 1;
238 finish_expr_stmt (build (INIT_EXPR, type, decl, e));
240 else if (TREE_CODE (type) == REFERENCE_TYPE)
241 cp_error ("default-initialization of `%#D', which has reference type",
244 init = integer_zero_node;
246 /* member traversal: note it leaves init NULL */
247 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
248 cp_pedwarn ("uninitialized reference member `%D'", member);
250 else if (TREE_CODE (init) == TREE_LIST)
252 /* There was an explicit member initialization. Do some
253 work in that case. */
254 if (TREE_CHAIN (init))
256 warning ("initializer list treated as compound expression");
257 init = build_compound_expr (init);
260 init = TREE_VALUE (init);
264 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
267 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
271 expr = build_component_ref (current_class_ref, name, NULL_TREE,
273 expr = build_delete (type, expr, integer_zero_node,
274 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
276 if (expr != error_mark_node)
277 finish_subobject (expr);
281 /* Subroutine of emit_member_init. */
287 tree x, member, name, field;
288 tree init_list = NULL_TREE;
290 tree last_field = NULL_TREE;
292 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
296 /* member could be, for example, a CONST_DECL for an enumerated
297 tag; we don't want to try to initialize that, since it already
299 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
302 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
304 /* If we cleared this out, then pay no attention to it. */
305 if (TREE_PURPOSE (x) == NULL_TREE)
307 name = TREE_PURPOSE (x);
309 if (TREE_CODE (name) == IDENTIFIER_NODE)
310 field = IDENTIFIER_CLASS_VALUE (name);
313 my_friendly_assert (TREE_CODE (name) == FIELD_DECL, 348);
317 /* If one member shadows another, get the outermost one. */
318 if (TREE_CODE (field) == TREE_LIST)
319 field = TREE_VALUE (field);
327 cp_warning_at ("member initializers for `%#D'", last_field);
328 cp_warning_at (" and `%#D'", field);
329 warning (" will be re-ordered to match declaration order");
335 /* Make sure we won't try to work on this init again. */
336 TREE_PURPOSE (x) = NULL_TREE;
337 x = build_tree_list (name, TREE_VALUE (x));
342 /* If we didn't find MEMBER in the list, create a dummy entry
343 so the two lists (INIT_LIST and the list of members) will be
345 x = build_tree_list (NULL_TREE, NULL_TREE);
347 init_list = chainon (init_list, x);
350 /* Initializers for base members go at the end. */
351 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
353 name = TREE_PURPOSE (x);
356 if (purpose_member (name, init_list))
358 cp_error ("multiple initializations given for member `%D'",
359 IDENTIFIER_CLASS_VALUE (name));
363 init_list = chainon (init_list,
364 build_tree_list (name, TREE_VALUE (x)));
365 TREE_PURPOSE (x) = NULL_TREE;
373 sort_base_init (t, rbase_ptr, vbase_ptr)
374 tree t, *rbase_ptr, *vbase_ptr;
376 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
377 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
383 /* For warn_reorder. */
385 tree last_base = NULL_TREE;
387 tree rbases = NULL_TREE;
388 tree vbases = NULL_TREE;
390 /* First walk through and splice out vbase and invalid initializers.
391 Also replace names with binfos. */
393 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
394 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
396 tree basetype = TREE_PURPOSE (x);
397 tree binfo = NULL_TREE;
399 if (basetype == NULL_TREE)
401 /* Initializer for single base class. Must not
402 use multiple inheritance or this is ambiguous. */
403 switch (n_baseclasses)
406 cp_error ("`%T' does not have a base class to initialize",
412 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
416 binfo = TREE_VEC_ELT (binfos, 0);
418 else if (is_aggr_type (basetype, 1))
420 binfo = binfo_or_else (basetype, t);
421 if (binfo == NULL_TREE)
424 /* Virtual base classes are special cases. Their initializers
425 are recorded with this constructor, and they are used when
426 this constructor is the top-level constructor called. */
427 if (TREE_VIA_VIRTUAL (binfo))
429 tree v = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
430 vbases = tree_cons (v, TREE_VALUE (x), vbases);
435 /* Otherwise, if it is not an immediate base class, complain. */
436 for (i = n_baseclasses-1; i >= 0; i--)
437 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
441 cp_error ("`%T' is not an immediate base class of `%T'",
442 basetype, current_class_type);
448 my_friendly_abort (365);
450 TREE_PURPOSE (x) = binfo;
451 TREE_CHAIN (last) = x;
454 TREE_CHAIN (last) = NULL_TREE;
456 /* Now walk through our regular bases and make sure they're initialized. */
458 for (i = 0; i < n_baseclasses; ++i)
460 tree base_binfo = TREE_VEC_ELT (binfos, i);
463 if (TREE_VIA_VIRTUAL (base_binfo))
466 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
468 tree binfo = TREE_PURPOSE (x);
470 if (binfo == NULL_TREE)
473 if (binfo == base_binfo)
479 cp_warning_at ("base initializers for `%#T'", last_base);
480 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
481 warning (" will be re-ordered to match inheritance order");
484 last_base = BINFO_TYPE (binfo);
487 /* Make sure we won't try to work on this init again. */
488 TREE_PURPOSE (x) = NULL_TREE;
489 x = build_tree_list (binfo, TREE_VALUE (x));
494 /* If we didn't find BASE_BINFO in the list, create a dummy entry
495 so the two lists (RBASES and the list of bases) will be
497 x = build_tree_list (NULL_TREE, NULL_TREE);
499 rbases = chainon (rbases, x);
506 /* Perform whatever initializations have yet to be done on the base
507 class of the class variable. These actions are in the global
508 variable CURRENT_BASE_INIT_LIST. Such an action could be
509 NULL_TREE, meaning that the user has explicitly called the base
510 class constructor with no arguments.
512 If there is a need for a call to a constructor, we must surround
513 that call with a pushlevel/poplevel pair, since we are technically
514 at the PARM level of scope.
516 Argument IMMEDIATELY, if zero, forces a new sequence to be
517 generated to contain these new insns, so it can be emitted later.
518 This sequence is saved in the global variable BASE_INIT_EXPR.
519 Otherwise, the insns are emitted into the current sequence.
521 Note that emit_base_init does *not* initialize virtual base
522 classes. That is done specially, elsewhere. */
530 tree rbase_init_list, vbase_init_list;
531 tree t_binfo = TYPE_BINFO (t);
532 tree binfos = BINFO_BASETYPES (t_binfo);
534 int n_baseclasses = BINFO_N_BASETYPES (t_binfo);
536 mem_init_list = sort_member_init (t);
537 current_member_init_list = NULL_TREE;
539 sort_base_init (t, &rbase_init_list, &vbase_init_list);
540 current_base_init_list = NULL_TREE;
542 /* First, initialize the virtual base classes, if we are
543 constructing the most-derived object. */
544 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
546 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
547 construct_virtual_bases (t, current_class_ref, current_class_ptr,
548 vbase_init_list, first_arg);
551 /* Now, perform initialization of non-virtual base classes. */
552 for (i = 0; i < n_baseclasses; i++)
554 tree base_binfo = TREE_VEC_ELT (binfos, i);
555 tree init = void_list_node;
557 if (TREE_VIA_VIRTUAL (base_binfo))
560 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
563 if (TREE_PURPOSE (rbase_init_list))
564 init = TREE_VALUE (rbase_init_list);
565 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
569 && DECL_COPY_CONSTRUCTOR_P (current_function_decl))
570 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
571 BINFO_TYPE (base_binfo));
574 if (init != void_list_node)
576 member = convert_pointer_to_real (base_binfo, current_class_ptr);
577 expand_aggr_init_1 (base_binfo, NULL_TREE,
578 build_indirect_ref (member, NULL_PTR), init,
582 expand_cleanup_for_base (base_binfo, NULL_TREE);
583 rbase_init_list = TREE_CHAIN (rbase_init_list);
586 /* Initialize the vtable pointers for the class. */
587 initialize_vtbl_ptrs (t, current_class_ptr);
589 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
594 /* member could be, for example, a CONST_DECL for an enumerated
595 tag; we don't want to try to initialize that, since it already
597 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
600 /* See if we had a user-specified member initialization. */
601 if (TREE_PURPOSE (mem_init_list))
603 name = TREE_PURPOSE (mem_init_list);
604 init = TREE_VALUE (mem_init_list);
607 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE
608 || TREE_CODE (name) == FIELD_DECL, 349);
612 name = DECL_NAME (member);
613 init = DECL_INITIAL (member);
617 /* Effective C++ rule 12. */
618 if (warn_ecpp && init == NULL_TREE
619 && !DECL_ARTIFICIAL (member)
620 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
621 cp_warning ("`%D' should be initialized in the member initialization list", member);
624 perform_member_init (member, name, init, from_init_list);
625 mem_init_list = TREE_CHAIN (mem_init_list);
628 /* Now initialize any members from our bases. */
629 while (mem_init_list)
631 tree name, init, field;
633 if (TREE_PURPOSE (mem_init_list))
635 name = TREE_PURPOSE (mem_init_list);
636 init = TREE_VALUE (mem_init_list);
638 if (TREE_CODE (name) == IDENTIFIER_NODE)
639 field = IDENTIFIER_CLASS_VALUE (name);
643 /* If one member shadows another, get the outermost one. */
644 if (TREE_CODE (field) == TREE_LIST)
646 field = TREE_VALUE (field);
647 if (decl_type_context (field) != current_class_type)
648 cp_error ("field `%D' not in immediate context", field);
651 perform_member_init (field, name, init, 1);
653 mem_init_list = TREE_CHAIN (mem_init_list);
657 /* This code sets up the virtual function tables appropriate for
658 the pointer DECL. It is a one-ply initialization.
660 BINFO is the exact type that DECL is supposed to be. In
661 multiple inheritance, this might mean "C's A" if C : A, B. */
664 expand_virtual_init (binfo, decl)
667 tree type = BINFO_TYPE (binfo);
669 tree vtype, vtype_binfo;
672 /* Compute the location of the vtable. */
673 vtype = DECL_CONTEXT (TYPE_VFIELD (type));
674 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
675 b = binfo_value (DECL_FIELD_CONTEXT (TYPE_VFIELD (type)), binfo);
677 /* Figure out what vtable BINFO's vtable is based on, and mark it as
679 vtbl = get_vtbl_decl_for_binfo (b);
680 assemble_external (vtbl);
681 TREE_USED (vtbl) = 1;
683 /* Now compute the address to use when initializing the vptr. */
684 vtbl = BINFO_VTABLE (b);
685 if (TREE_CODE (vtbl) == VAR_DECL)
686 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
688 /* Compute the location of the vtpr. */
689 decl = convert_pointer_to_real (vtype_binfo, decl);
690 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
691 if (vtbl_ptr == error_mark_node)
694 /* Assign the vtable to the vptr. */
695 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
696 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
699 /* If an exception is thrown in a constructor, those base classes already
700 constructed must be destroyed. This function creates the cleanup
701 for BINFO, which has just been constructed. If FLAG is non-NULL,
702 it is a DECL which is non-zero when this base needs to be
706 expand_cleanup_for_base (binfo, flag)
712 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
715 /* Call the destructor. */
716 expr = (build_scoped_method_call
717 (current_class_ref, binfo, base_dtor_identifier, NULL_TREE));
719 expr = fold (build (COND_EXPR, void_type_node,
720 truthvalue_conversion (flag),
721 expr, integer_zero_node));
723 finish_subobject (expr);
726 /* Subroutine of `expand_aggr_vbase_init'.
727 BINFO is the binfo of the type that is being initialized.
728 INIT_LIST is the list of initializers for the virtual baseclass. */
731 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
732 tree binfo, exp, addr, init_list;
734 tree init = purpose_member (binfo, init_list);
735 tree ref = build_indirect_ref (addr, NULL_PTR);
738 init = TREE_VALUE (init);
739 /* Call constructors, but don't set up vtables. */
740 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
743 /* Construct the virtual base-classes of THIS_REF (whose address is
744 THIS_PTR). The object has the indicated TYPE. The construction
745 actually takes place only if FLAG is non-zero. INIT_LIST is list
746 of initializations for constructors to perform. */
749 construct_virtual_bases (type, this_ref, this_ptr, init_list, flag)
758 /* If there are no virtual baseclasses, we shouldn't even be here. */
759 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
761 /* First set the pointers in our object that tell us where to find
762 our virtual baseclasses. */
763 if (!vbase_offsets_in_vtable_p ())
768 if_stmt = begin_if_stmt ();
769 finish_if_stmt_cond (flag, if_stmt);
770 result = init_vbase_pointers (type, this_ptr);
772 finish_expr_stmt (build_compound_expr (result));
773 finish_then_clause (if_stmt);
777 /* Now, run through the baseclasses, initializing each. */
778 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
779 vbases = TREE_CHAIN (vbases))
785 /* If there are virtual base classes with destructors, we need to
786 emit cleanups to destroy them if an exception is thrown during
787 the construction process. These exception regions (i.e., the
788 period during which the cleanups must occur) begin from the time
789 the construction is complete to the end of the function. If we
790 create a conditional block in which to initialize the
791 base-classes, then the cleanup region for the virtual base begins
792 inside a block, and ends outside of that block. This situation
793 confuses the sjlj exception-handling code. Therefore, we do not
794 create a single conditional block, but one for each
795 initialization. (That way the cleanup regions always begin
796 in the outer block.) We trust the back-end to figure out
797 that the FLAG will not change across initializations, and
798 avoid doing multiple tests. */
799 inner_if_stmt = begin_if_stmt ();
800 finish_if_stmt_cond (flag, inner_if_stmt);
801 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
803 /* Compute the location of the virtual base. If we're
804 constructing virtual bases, then we must be the most derived
805 class. Therefore, we don't have to look up the virtual base;
806 we already know where it is. */
807 exp = build (PLUS_EXPR,
808 TREE_TYPE (this_ptr),
810 fold (build1 (NOP_EXPR, TREE_TYPE (this_ptr),
811 BINFO_OFFSET (vbases))));
812 exp = build1 (NOP_EXPR,
813 build_pointer_type (BINFO_TYPE (vbases)),
816 expand_aggr_vbase_init_1 (vbases, this_ref, exp, init_list);
817 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
818 finish_then_clause (inner_if_stmt);
821 expand_cleanup_for_base (vbases, flag);
825 /* Find the context in which this FIELD can be initialized. */
828 initializing_context (field)
831 tree t = DECL_CONTEXT (field);
833 /* Anonymous union members can be initialized in the first enclosing
834 non-anonymous union context. */
835 while (t && ANON_AGGR_TYPE_P (t))
836 t = TYPE_CONTEXT (t);
840 /* Function to give error message if member initialization specification
841 is erroneous. FIELD is the member we decided to initialize.
842 TYPE is the type for which the initialization is being performed.
843 FIELD must be a member of TYPE.
845 MEMBER_NAME is the name of the member. */
848 member_init_ok_or_else (field, type, member_name)
851 const char *member_name;
853 if (field == error_mark_node)
855 if (field == NULL_TREE || initializing_context (field) != type)
857 cp_error ("class `%T' does not have any field named `%s'", type,
861 if (TREE_STATIC (field))
863 cp_error ("field `%#D' is static; only point of initialization is its declaration",
871 /* If NAME is a viable field name for the aggregate DECL,
872 and PARMS is a viable parameter list, then expand an _EXPR
873 which describes this initialization.
875 Note that we do not need to chase through the class's base classes
876 to look for NAME, because if it's in that list, it will be handled
877 by the constructor for that base class.
879 We do not yet have a fixed-point finder to instantiate types
880 being fed to overloaded constructors. If there is a unique
881 constructor, then argument types can be got from that one.
883 If INIT is non-NULL, then it the initialization should
884 be placed in `current_base_init_list', where it will be processed
885 by `emit_base_init'. */
888 expand_member_init (exp, name, init)
889 tree exp, name, init;
891 tree basetype = NULL_TREE, field;
894 if (exp == NULL_TREE)
895 return; /* complain about this later */
897 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
899 if (name && TREE_CODE (name) == TYPE_DECL)
901 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
902 name = DECL_NAME (name);
905 if (name == NULL_TREE && IS_AGGR_TYPE (type))
906 switch (CLASSTYPE_N_BASECLASSES (type))
909 error ("base class initializer specified, but no base class to initialize");
912 basetype = TYPE_BINFO_BASETYPE (type, 0);
915 error ("initializer for unnamed base class ambiguous");
916 cp_error ("(type `%T' uses multiple inheritance)", type);
920 my_friendly_assert (init != NULL_TREE, 0);
922 /* The grammar should not allow fields which have names that are
923 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
924 may assume that this is an attempt to initialize a base class
925 member of the current type. Otherwise, it is an attempt to
926 initialize a member field. */
928 if (init == void_type_node)
931 if (name == NULL_TREE || basetype)
935 if (name == NULL_TREE)
939 name = TYPE_IDENTIFIER (basetype);
942 error ("no base class to initialize");
947 else if (basetype != type
948 && ! current_template_parms
949 && ! vec_binfo_member (basetype,
950 TYPE_BINFO_BASETYPES (type))
951 && ! BINFO_FOR_VBASE (basetype, type))
953 if (IDENTIFIER_CLASS_VALUE (name))
955 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
956 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
959 cp_error ("type `%T' is not an immediate basetype for `%T'",
964 if (purpose_member (basetype, current_base_init_list))
966 cp_error ("base class `%T' already initialized", basetype);
970 if (warn_reorder && current_member_init_list)
972 cp_warning ("base initializer for `%T'", basetype);
973 warning (" will be re-ordered to precede member initializations");
976 base_init = build_tree_list (basetype, init);
977 current_base_init_list = chainon (current_base_init_list, base_init);
984 field = lookup_field (type, name, 1, 0);
986 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
989 if (purpose_member (name, current_member_init_list))
991 cp_error ("field `%D' already initialized", field);
995 member_init = build_tree_list (name, init);
996 current_member_init_list = chainon (current_member_init_list, member_init);
1000 /* This is like `expand_member_init', only it stores one aggregate
1003 INIT comes in two flavors: it is either a value which
1004 is to be stored in EXP, or it is a parameter list
1005 to go to a constructor, which will operate on EXP.
1006 If INIT is not a parameter list for a constructor, then set
1007 LOOKUP_ONLYCONVERTING.
1008 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1009 the initializer, if FLAGS is 0, then it is the (init) form.
1010 If `init' is a CONSTRUCTOR, then we emit a warning message,
1011 explaining that such initializations are invalid.
1013 If INIT resolves to a CALL_EXPR which happens to return
1014 something of the type we are looking for, then we know
1015 that we can safely use that call to perform the
1018 The virtual function table pointer cannot be set up here, because
1019 we do not really know its type.
1021 Virtual baseclass pointers are also set up here.
1023 This never calls operator=().
1025 When initializing, nothing is CONST.
1027 A default copy constructor may have to be used to perform the
1030 A constructor or a conversion operator may have to be used to
1031 perform the initialization, but not both, as it would be ambiguous. */
1034 build_aggr_init (exp, init, flags)
1041 tree type = TREE_TYPE (exp);
1042 int was_const = TREE_READONLY (exp);
1043 int was_volatile = TREE_THIS_VOLATILE (exp);
1045 if (init == error_mark_node)
1046 return error_mark_node;
1048 TREE_READONLY (exp) = 0;
1049 TREE_THIS_VOLATILE (exp) = 0;
1051 if (init && TREE_CODE (init) != TREE_LIST)
1052 flags |= LOOKUP_ONLYCONVERTING;
1054 if (TREE_CODE (type) == ARRAY_TYPE)
1056 /* Must arrange to initialize each element of EXP
1057 from elements of INIT. */
1058 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1059 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1061 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1063 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1065 if (init && TREE_TYPE (init) == NULL_TREE)
1067 /* Handle bad initializers like:
1071 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1075 int main(int argc, char **argv) {
1076 COMPLEX zees(1.0, 0.0)[10];
1079 error ("bad array initializer");
1080 return error_mark_node;
1082 stmt_expr = build_vec_init (exp, exp, array_type_nelts (type), init,
1083 init && same_type_p (TREE_TYPE (init),
1085 TREE_READONLY (exp) = was_const;
1086 TREE_THIS_VOLATILE (exp) = was_volatile;
1087 TREE_TYPE (exp) = type;
1089 TREE_TYPE (init) = itype;
1093 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1094 /* just know that we've seen something for this node */
1095 TREE_USED (exp) = 1;
1097 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1098 begin_init_stmts (&stmt_expr, &compound_stmt);
1099 destroy_temps = stmts_are_full_exprs_p;
1100 stmts_are_full_exprs_p = 0;
1101 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1102 init, LOOKUP_NORMAL|flags);
1103 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
1104 stmts_are_full_exprs_p = destroy_temps;
1105 TREE_TYPE (exp) = type;
1106 TREE_READONLY (exp) = was_const;
1107 TREE_THIS_VOLATILE (exp) = was_volatile;
1113 expand_default_init (binfo, true_exp, exp, init, flags)
1119 tree type = TREE_TYPE (exp);
1122 /* It fails because there may not be a constructor which takes
1123 its own type as the first (or only parameter), but which does
1124 take other types via a conversion. So, if the thing initializing
1125 the expression is a unit element of type X, first try X(X&),
1126 followed by initialization by X. If neither of these work
1127 out, then look hard. */
1131 if (init && TREE_CODE (init) != TREE_LIST
1132 && (flags & LOOKUP_ONLYCONVERTING))
1134 /* Base subobjects should only get direct-initialization. */
1135 if (true_exp != exp)
1138 if (flags & DIRECT_BIND)
1139 /* Do nothing. We hit this in two cases: Reference initialization,
1140 where we aren't initializing a real variable, so we don't want
1141 to run a new constructor; and catching an exception, where we
1142 have already built up the constructor call so we could wrap it
1143 in an exception region. */;
1144 else if (TREE_CODE (init) == CONSTRUCTOR)
1145 /* A brace-enclosed initializer has whatever type is
1146 required. There's no need to convert it. */
1149 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1151 if (TREE_CODE (init) == TRY_CATCH_EXPR)
1152 /* We need to protect the initialization of a catch parm
1153 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1154 around the TARGET_EXPR for the copy constructor. See
1155 expand_start_catch_block. */
1156 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1157 TREE_OPERAND (init, 0));
1159 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1160 TREE_SIDE_EFFECTS (init) = 1;
1161 finish_expr_stmt (init);
1165 if (init == NULL_TREE
1166 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1170 init = TREE_VALUE (parms);
1173 parms = build_tree_list (NULL_TREE, init);
1175 if (true_exp == exp)
1176 ctor_name = complete_ctor_identifier;
1178 ctor_name = base_ctor_identifier;
1180 rval = build_method_call (exp, ctor_name, parms, binfo, flags);
1181 if (TREE_SIDE_EFFECTS (rval))
1182 finish_expr_stmt (rval);
1185 /* This function is responsible for initializing EXP with INIT
1188 BINFO is the binfo of the type for who we are performing the
1189 initialization. For example, if W is a virtual base class of A and B,
1191 If we are initializing B, then W must contain B's W vtable, whereas
1192 were we initializing C, W must contain C's W vtable.
1194 TRUE_EXP is nonzero if it is the true expression being initialized.
1195 In this case, it may be EXP, or may just contain EXP. The reason we
1196 need this is because if EXP is a base element of TRUE_EXP, we
1197 don't necessarily know by looking at EXP where its virtual
1198 baseclass fields should really be pointing. But we do know
1199 from TRUE_EXP. In constructors, we don't know anything about
1200 the value being initialized.
1202 FLAGS is just passes to `build_method_call'. See that function for
1206 expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
1212 tree type = TREE_TYPE (exp);
1214 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1216 /* Use a function returning the desired type to initialize EXP for us.
1217 If the function is a constructor, and its first argument is
1218 NULL_TREE, know that it was meant for us--just slide exp on
1219 in and expand the constructor. Constructors now come
1222 if (init && TREE_CODE (exp) == VAR_DECL
1223 && TREE_CODE (init) == CONSTRUCTOR
1224 && TREE_HAS_CONSTRUCTOR (init))
1226 /* If store_init_value returns NULL_TREE, the INIT has been
1227 record in the DECL_INITIAL for EXP. That means there's
1228 nothing more we have to do. */
1229 if (!store_init_value (exp, init))
1231 if (!building_stmt_tree ())
1232 expand_decl_init (exp);
1235 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1239 /* We know that expand_default_init can handle everything we want
1241 expand_default_init (binfo, true_exp, exp, init, flags);
1244 /* Report an error if TYPE is not a user-defined, aggregate type. If
1245 OR_ELSE is nonzero, give an error message. */
1248 is_aggr_type (type, or_else)
1252 if (type == error_mark_node)
1255 if (! IS_AGGR_TYPE (type)
1256 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1257 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1260 cp_error ("`%T' is not an aggregate type", type);
1266 /* Like is_aggr_typedef, but returns typedef if successful. */
1269 get_aggr_from_typedef (name, or_else)
1275 if (name == error_mark_node)
1278 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1279 type = IDENTIFIER_TYPE_VALUE (name);
1283 cp_error ("`%T' fails to be an aggregate typedef", name);
1287 if (! IS_AGGR_TYPE (type)
1288 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1289 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1292 cp_error ("type `%T' is of non-aggregate type", type);
1299 get_type_value (name)
1302 if (name == error_mark_node)
1305 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1306 return IDENTIFIER_TYPE_VALUE (name);
1312 /* This code could just as well go in `class.c', but is placed here for
1315 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1316 the appropriate function call. */
1319 build_member_call (type, name, parmlist)
1320 tree type, name, parmlist;
1325 tree basetype_path, decl;
1327 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1328 && TREE_CODE (type) == NAMESPACE_DECL)
1330 /* 'name' already refers to the decls from the namespace, since we
1331 hit do_identifier for template_ids. */
1332 method_name = TREE_OPERAND (name, 0);
1333 /* FIXME: Since we don't do independent names right yet, the
1334 name might also be a LOOKUP_EXPR. Once we resolve this to a
1335 real decl earlier, this can go. This may happen during
1337 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1339 method_name = lookup_namespace_name
1340 (type, TREE_OPERAND (method_name, 0));
1341 TREE_OPERAND (name, 0) = method_name;
1343 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1344 return build_x_function_call (name, parmlist, current_class_ref);
1347 if (type == std_node)
1348 return build_x_function_call (do_scoped_id (name, 0), parmlist,
1350 if (TREE_CODE (type) == NAMESPACE_DECL)
1351 return build_x_function_call (lookup_namespace_name (type, name),
1352 parmlist, current_class_ref);
1354 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1356 method_name = TREE_OPERAND (name, 0);
1357 if (TREE_CODE (method_name) == COMPONENT_REF)
1358 method_name = TREE_OPERAND (method_name, 1);
1359 if (is_overloaded_fn (method_name))
1360 method_name = DECL_NAME (OVL_CURRENT (method_name));
1361 TREE_OPERAND (name, 0) = method_name;
1366 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1368 method_name = TREE_OPERAND (method_name, 0);
1372 /* This shouldn't be here, and build_member_call shouldn't appear in
1374 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1375 && get_aggr_from_typedef (type, 0) == 0)
1377 tree ns = lookup_name (type, 0);
1378 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1380 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
1384 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1385 return error_mark_node;
1387 /* An operator we did not like. */
1388 if (name == NULL_TREE)
1389 return error_mark_node;
1393 cp_error ("cannot call destructor `%T::~%T' without object", type,
1395 return error_mark_node;
1398 decl = maybe_dummy_object (type, &basetype_path);
1400 /* Convert 'this' to the specified type to disambiguate conversion
1401 to the function's context. Apparently Standard C++ says that we
1402 shouldn't do this. */
1403 if (decl == current_class_ref
1405 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1407 tree olddecl = current_class_ptr;
1408 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1409 if (oldtype != type)
1411 tree newtype = build_qualified_type (type, TYPE_QUALS (oldtype));
1412 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
1413 decl = build_indirect_ref (decl, NULL_PTR);
1417 if (method_name == constructor_name (type)
1418 || method_name == constructor_name_full (type))
1419 return build_functional_cast (type, parmlist);
1420 if (lookup_fnfields (basetype_path, method_name, 0))
1421 return build_method_call (decl,
1422 TREE_CODE (name) == TEMPLATE_ID_EXPR
1423 ? name : method_name,
1424 parmlist, basetype_path,
1425 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1426 if (TREE_CODE (name) == IDENTIFIER_NODE
1427 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1429 if (t == error_mark_node)
1430 return error_mark_node;
1431 if (TREE_CODE (t) == FIELD_DECL)
1433 if (is_dummy_object (decl))
1435 cp_error ("invalid use of non-static field `%D'", t);
1436 return error_mark_node;
1438 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1440 else if (TREE_CODE (t) == VAR_DECL)
1444 cp_error ("invalid use of member `%D'", t);
1445 return error_mark_node;
1447 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1448 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1449 parmlist, NULL_TREE);
1450 return build_function_call (decl, parmlist);
1454 cp_error ("no method `%T::%D'", type, name);
1455 return error_mark_node;
1459 /* Build a reference to a member of an aggregate. This is not a
1460 C++ `&', but really something which can have its address taken,
1461 and then act as a pointer to member, for example TYPE :: FIELD
1462 can have its address taken by saying & TYPE :: FIELD.
1464 @@ Prints out lousy diagnostics for operator <typename>
1467 @@ This function should be rewritten and placed in search.c. */
1470 build_offset_ref (type, name)
1473 tree decl, t = error_mark_node;
1475 tree basebinfo = NULL_TREE;
1476 tree orig_name = name;
1478 /* class templates can come in as TEMPLATE_DECLs here. */
1479 if (TREE_CODE (name) == TEMPLATE_DECL)
1482 if (type == std_node)
1483 return do_scoped_id (name, 0);
1485 if (processing_template_decl || uses_template_parms (type))
1486 return build_min_nt (SCOPE_REF, type, name);
1488 /* Handle namespace names fully here. */
1489 if (TREE_CODE (type) == NAMESPACE_DECL)
1491 t = lookup_namespace_name (type, name);
1492 if (t != error_mark_node && ! type_unknown_p (t))
1495 t = convert_from_reference (t);
1500 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1501 return error_mark_node;
1503 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1505 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1506 something like `a.template f<int>' or the like. For the most
1507 part, we treat this just like a.f. We do remember, however,
1508 the template-id that was used. */
1509 name = TREE_OPERAND (orig_name, 0);
1512 name = DECL_NAME (name);
1515 if (TREE_CODE (name) == LOOKUP_EXPR)
1516 /* This can happen during tsubst'ing. */
1517 name = TREE_OPERAND (name, 0);
1520 if (TREE_CODE (name) == COMPONENT_REF)
1521 name = TREE_OPERAND (name, 1);
1522 if (TREE_CODE (name) == OVERLOAD)
1523 name = DECL_NAME (OVL_CURRENT (name));
1527 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1530 if (TREE_CODE (name) == BIT_NOT_EXPR)
1532 if (! check_dtor_name (type, name))
1533 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1534 type, TREE_OPERAND (name, 0));
1535 name = dtor_identifier;
1538 if (!COMPLETE_TYPE_P (complete_type (type))
1539 && !TYPE_BEING_DEFINED (type))
1541 cp_error ("incomplete type `%T' does not have member `%D'", type,
1543 return error_mark_node;
1546 decl = maybe_dummy_object (type, &basebinfo);
1548 member = lookup_member (basebinfo, name, 1, 0);
1550 if (member == error_mark_node)
1551 return error_mark_node;
1553 /* A lot of this logic is now handled in lookup_member. */
1554 if (member && BASELINK_P (member))
1556 /* Go from the TREE_BASELINK to the member function info. */
1557 tree fnfields = member;
1558 t = TREE_VALUE (fnfields);
1560 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1562 /* The FNFIELDS are going to contain functions that aren't
1563 necessarily templates, and templates that don't
1564 necessarily match the explicit template parameters. We
1565 save all the functions, and the explicit parameters, and
1566 then figure out exactly what to instantiate with what
1567 arguments in instantiate_type. */
1569 if (TREE_CODE (t) != OVERLOAD)
1570 /* The code in instantiate_type which will process this
1571 expects to encounter OVERLOADs, not raw functions. */
1572 t = ovl_cons (t, NULL_TREE);
1574 return build (OFFSET_REF,
1577 build (TEMPLATE_ID_EXPR,
1580 TREE_OPERAND (orig_name, 1)));
1583 if (!really_overloaded_fn (t))
1585 /* Get rid of a potential OVERLOAD around it */
1586 t = OVL_CURRENT (t);
1588 /* unique functions are handled easily. */
1589 if (!enforce_access (basebinfo, t))
1590 return error_mark_node;
1592 if (DECL_STATIC_FUNCTION_P (t))
1594 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1597 TREE_TYPE (fnfields) = unknown_type_node;
1598 return build (OFFSET_REF, unknown_type_node, decl, fnfields);
1605 cp_error ("`%D' is not a member of type `%T'", name, type);
1606 return error_mark_node;
1609 if (TREE_CODE (t) == TYPE_DECL)
1614 /* static class members and class-specific enum
1615 values can be returned without further ado. */
1616 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1619 return convert_from_reference (t);
1622 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1624 cp_error ("illegal pointer to bit field `%D'", t);
1625 return error_mark_node;
1628 /* static class functions too. */
1629 if (TREE_CODE (t) == FUNCTION_DECL
1630 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1631 my_friendly_abort (53);
1633 /* In member functions, the form `type::name' is no longer
1634 equivalent to `this->type::name', at least not until
1635 resolve_offset_ref. */
1636 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1639 /* If a OFFSET_REF made it through to here, then it did
1640 not have its address taken. */
1643 resolve_offset_ref (exp)
1646 tree type = TREE_TYPE (exp);
1647 tree base = NULL_TREE;
1649 tree basetype, addr;
1651 if (TREE_CODE (exp) == OFFSET_REF)
1653 member = TREE_OPERAND (exp, 1);
1654 base = TREE_OPERAND (exp, 0);
1658 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1659 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1661 error ("object missing in use of pointer-to-member construct");
1662 return error_mark_node;
1665 type = TREE_TYPE (type);
1666 base = current_class_ref;
1669 if (BASELINK_P (member))
1671 if (! flag_ms_extensions)
1672 cp_pedwarn ("assuming & on overloaded member function");
1673 return build_unary_op (ADDR_EXPR, exp, 0);
1676 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1678 if (! flag_ms_extensions)
1679 cp_pedwarn ("assuming & on `%E'", member);
1680 return build_unary_op (ADDR_EXPR, exp, 0);
1683 if ((TREE_CODE (member) == VAR_DECL
1684 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1685 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1686 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1688 /* These were static members. */
1689 if (mark_addressable (member) == 0)
1690 return error_mark_node;
1694 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1695 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1698 /* Syntax error can cause a member which should
1699 have been seen as static to be grok'd as non-static. */
1700 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1702 if (TREE_ADDRESSABLE (member) == 0)
1704 cp_error_at ("member `%D' is non-static but referenced as a static member",
1706 error ("at this point in file");
1707 TREE_ADDRESSABLE (member) = 1;
1709 return error_mark_node;
1712 /* The first case is really just a reference to a member of `this'. */
1713 if (TREE_CODE (member) == FIELD_DECL
1714 && (base == current_class_ref || is_dummy_object (base)))
1718 basetype = DECL_CONTEXT (member);
1720 /* Try to get to basetype from 'this'; if that doesn't work,
1722 base = current_class_ref;
1724 /* First convert to the intermediate base specified, if appropriate. */
1725 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1726 base = build_scoped_ref (base, TYPE_OFFSET_BASETYPE (type));
1728 addr = build_unary_op (ADDR_EXPR, base, 0);
1729 addr = convert_pointer_to (basetype, addr);
1731 if (addr == error_mark_node)
1732 return error_mark_node;
1734 expr = build (COMPONENT_REF, TREE_TYPE (member),
1735 build_indirect_ref (addr, NULL_PTR), member);
1736 return convert_from_reference (expr);
1739 /* Ensure that we have an object. */
1740 if (is_dummy_object (base))
1741 addr = error_mark_node;
1743 /* If this is a reference to a member function, then return the
1744 address of the member function (which may involve going
1745 through the object's vtable), otherwise, return an expression
1746 for the dereferenced pointer-to-member construct. */
1747 addr = build_unary_op (ADDR_EXPR, base, 0);
1749 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1751 if (addr == error_mark_node)
1753 cp_error ("object missing in `%E'", exp);
1754 return error_mark_node;
1757 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1758 addr = convert_pointer_to (basetype, addr);
1759 member = cp_convert (ptrdiff_type_node, member);
1762 /* Pointer to data members are offset by one, so that a null
1763 pointer with a real value of 0 is distinguishable from an
1764 offset of the first member of a structure. */
1765 member = build_binary_op (MINUS_EXPR, member,
1766 cp_convert (ptrdiff_type_node,
1769 return build1 (INDIRECT_REF, type,
1770 build (PLUS_EXPR, build_pointer_type (type),
1773 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1775 return get_member_function_from_ptrfunc (&addr, member);
1777 my_friendly_abort (56);
1782 /* Return either DECL or its known constant value (if it has one). */
1785 decl_constant_value (decl)
1788 if (! TREE_THIS_VOLATILE (decl)
1789 && DECL_INITIAL (decl)
1790 && DECL_INITIAL (decl) != error_mark_node
1791 /* This is invalid if initial value is not constant.
1792 If it has either a function call, a memory reference,
1793 or a variable, then re-evaluating it could give different results. */
1794 && TREE_CONSTANT (DECL_INITIAL (decl))
1795 /* Check for cases where this is sub-optimal, even though valid. */
1796 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1797 return DECL_INITIAL (decl);
1801 /* Common subroutines of build_new and build_vec_delete. */
1803 /* Call the global __builtin_delete to delete ADDR. */
1806 build_builtin_delete_call (addr)
1809 mark_used (global_delete_fndecl);
1810 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
1813 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1814 (which needs to go through some sort of groktypename) or it
1815 is the name of the class we are newing. INIT is an initialization value.
1816 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1817 If INIT is void_type_node, it means do *not* call a constructor
1820 For types with constructors, the data returned is initialized
1821 by the appropriate constructor.
1823 Whether the type has a constructor or not, if it has a pointer
1824 to a virtual function table, then that pointer is set up
1827 Unless I am mistaken, a call to new () will return initialized
1828 data regardless of whether the constructor itself is private or
1829 not. NOPE; new fails if the constructor is private (jcm).
1831 Note that build_new does nothing to assure that any special
1832 alignment requirements of the type are met. Rather, it leaves
1833 it up to malloc to do the right thing. Otherwise, folding to
1834 the right alignment cal cause problems if the user tries to later
1835 free the memory returned by `new'.
1837 PLACEMENT is the `placement' list for user-defined operator new (). */
1839 extern int flag_check_new;
1842 build_new (placement, decl, init, use_global_new)
1848 tree nelts = NULL_TREE, t;
1851 if (decl == error_mark_node)
1852 return error_mark_node;
1854 if (TREE_CODE (decl) == TREE_LIST)
1856 tree absdcl = TREE_VALUE (decl);
1857 tree last_absdcl = NULL_TREE;
1859 if (current_function_decl
1860 && DECL_CONSTRUCTOR_P (current_function_decl))
1861 my_friendly_assert (immediate_size_expand == 0, 19990926);
1863 nelts = integer_one_node;
1865 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1866 my_friendly_abort (215);
1867 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1869 last_absdcl = absdcl;
1870 absdcl = TREE_OPERAND (absdcl, 0);
1873 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1875 /* probably meant to be a vec new */
1878 while (TREE_OPERAND (absdcl, 0)
1879 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1881 last_absdcl = absdcl;
1882 absdcl = TREE_OPERAND (absdcl, 0);
1886 this_nelts = TREE_OPERAND (absdcl, 1);
1887 if (this_nelts != error_mark_node)
1889 if (this_nelts == NULL_TREE)
1890 error ("new of array type fails to specify size");
1891 else if (processing_template_decl)
1894 absdcl = TREE_OPERAND (absdcl, 0);
1898 int flags = pedantic ? WANT_INT : (WANT_INT | WANT_ENUM);
1899 if (build_expr_type_conversion (flags, this_nelts, 0)
1901 pedwarn ("size in array new must have integral type");
1903 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1904 absdcl = TREE_OPERAND (absdcl, 0);
1905 if (this_nelts == integer_zero_node)
1907 warning ("zero size array reserves no space");
1908 nelts = integer_zero_node;
1911 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
1915 nelts = integer_zero_node;
1919 TREE_OPERAND (last_absdcl, 0) = absdcl;
1921 TREE_VALUE (decl) = absdcl;
1923 type = groktypename (decl);
1924 if (! type || type == error_mark_node)
1925 return error_mark_node;
1927 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
1929 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
1931 /* An aggregate type. */
1932 type = IDENTIFIER_TYPE_VALUE (decl);
1933 decl = TYPE_MAIN_DECL (type);
1937 /* A builtin type. */
1938 decl = lookup_name (decl, 1);
1939 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
1940 type = TREE_TYPE (decl);
1943 else if (TREE_CODE (decl) == TYPE_DECL)
1945 type = TREE_TYPE (decl);
1950 decl = TYPE_MAIN_DECL (type);
1953 if (processing_template_decl)
1956 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
1957 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
1962 rval = build_min_nt (NEW_EXPR, placement, t, init);
1963 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
1967 /* ``A reference cannot be created by the new operator. A reference
1968 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
1969 returned by new.'' ARM 5.3.3 */
1970 if (TREE_CODE (type) == REFERENCE_TYPE)
1972 error ("new cannot be applied to a reference type");
1973 type = TREE_TYPE (type);
1976 if (TREE_CODE (type) == FUNCTION_TYPE)
1978 error ("new cannot be applied to a function type");
1979 return error_mark_node;
1982 /* When the object being created is an array, the new-expression yields a
1983 pointer to the initial element (if any) of the array. For example,
1984 both new int and new int[10] return an int*. 5.3.4. */
1985 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
1987 nelts = array_type_nelts_top (type);
1989 type = TREE_TYPE (type);
1993 t = build_nt (ARRAY_REF, type, nelts);
1997 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
1998 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
1999 TREE_SIDE_EFFECTS (rval) = 1;
2000 rval = build_new_1 (rval);
2001 if (rval == error_mark_node)
2002 return error_mark_node;
2004 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2005 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2006 TREE_NO_UNUSED_WARNING (rval) = 1;
2011 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2014 build_java_class_ref (type)
2017 tree name, class_decl;
2018 static tree CL_prefix = NULL_TREE;
2019 if (CL_prefix == NULL_TREE)
2020 CL_prefix = get_identifier("_CL_");
2021 if (jclass_node == NULL_TREE)
2023 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2024 if (jclass_node == NULL_TREE)
2025 fatal("call to Java constructor, while `jclass' undefined");
2026 jclass_node = TREE_TYPE (jclass_node);
2028 name = build_overload_with_type (CL_prefix, type);
2029 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2030 if (class_decl == NULL_TREE)
2032 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2033 TREE_STATIC (class_decl) = 1;
2034 DECL_EXTERNAL (class_decl) = 1;
2035 TREE_PUBLIC (class_decl) = 1;
2036 DECL_ARTIFICIAL (class_decl) = 1;
2037 DECL_IGNORED_P (class_decl) = 1;
2038 pushdecl_top_level (class_decl);
2039 make_decl_rtl (class_decl, NULL_PTR, 1);
2044 /* Returns teh size of the cookie to use when allocating an array
2045 whose elements have the indicated TYPE. Assumes that it is already
2046 known that a cookie is needed. */
2049 get_cookie_size (type)
2056 /* Under the new ABI, we need to allocate an additional max
2057 (sizeof (size_t), alignof (true_type)) bytes. */
2061 sizetype_size = size_in_bytes (sizetype);
2062 type_align = size_int (TYPE_ALIGN_UNIT (type));
2063 if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
2064 cookie_size = sizetype_size;
2066 cookie_size = type_align;
2069 cookie_size = BI_header_size;
2074 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2075 value is immediately handed to expand_expr. */
2081 tree placement, init;
2082 tree type, true_type, size, rval;
2083 tree nelts = NULL_TREE;
2084 tree alloc_expr, alloc_node = NULL_TREE;
2086 enum tree_code code;
2087 int use_cookie, nothrow, check_new;
2088 /* Nonzero if the user wrote `::new' rather than just `new'. */
2089 int globally_qualified_p;
2090 /* Nonzero if we're going to call a global operator new, rather than
2091 a class-specific version. */
2093 int use_java_new = 0;
2094 /* If non-NULL, the number of extra bytes to allocate at the
2095 beginning of the storage allocated for an array-new expression in
2096 order to store the number of elements. */
2097 tree cookie_size = NULL_TREE;
2099 placement = TREE_OPERAND (exp, 0);
2100 type = TREE_OPERAND (exp, 1);
2101 init = TREE_OPERAND (exp, 2);
2102 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
2104 if (TREE_CODE (type) == ARRAY_REF)
2107 nelts = TREE_OPERAND (type, 1);
2108 type = TREE_OPERAND (type, 0);
2112 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
2114 if (CP_TYPE_QUALS (type))
2115 type = TYPE_MAIN_VARIANT (type);
2117 /* If our base type is an array, then make sure we know how many elements
2119 while (TREE_CODE (true_type) == ARRAY_TYPE)
2121 tree this_nelts = array_type_nelts_top (true_type);
2122 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
2123 true_type = TREE_TYPE (true_type);
2126 if (!complete_type_or_else (true_type, exp))
2127 return error_mark_node;
2130 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2133 size = size_in_bytes (type);
2135 if (TREE_CODE (true_type) == VOID_TYPE)
2137 error ("invalid type `void' for new");
2138 return error_mark_node;
2141 if (abstract_virtuals_error (NULL_TREE, true_type))
2142 return error_mark_node;
2144 /* Figure out whether or not we're going to use the global operator
2146 if (!globally_qualified_p
2147 && IS_AGGR_TYPE (true_type)
2148 && ((!has_array && TYPE_HAS_NEW_OPERATOR (true_type))
2149 || (has_array && TYPE_HAS_ARRAY_NEW_OPERATOR (true_type))))
2154 /* We only need cookies for arrays containing types for which we
2156 if (!has_array || !TYPE_VEC_NEW_USES_COOKIE (true_type))
2158 /* When using placement new, users may not realize that they need
2159 the extra storage. Under the old ABI, we don't allocate the
2160 cookie whenever they use one placement argument of type `void
2161 *'. Under the new ABI, we require that the operator called be
2162 the global placement operator delete[]. */
2163 else if (placement && !TREE_CHAIN (placement)
2164 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2166 use_cookie = (!flag_new_abi || !use_global_new);
2167 /* Otherwise, we need the cookie. */
2171 /* Compute the number of extra bytes to allocate, now that we know
2172 whether or not we need the cookie. */
2175 cookie_size = get_cookie_size (true_type);
2176 size = size_binop (PLUS_EXPR, size, cookie_size);
2179 if (has_array && init && pedantic)
2180 cp_pedwarn ("initialization in array new");
2182 /* Allocate the object. */
2184 if (! placement && TYPE_FOR_JAVA (true_type))
2186 tree class_addr, alloc_decl;
2187 tree class_decl = build_java_class_ref (true_type);
2188 tree class_size = size_in_bytes (true_type);
2189 static char alloc_name[] = "_Jv_AllocObject";
2191 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2192 if (alloc_decl == NULL_TREE)
2193 fatal("call to Java constructor, while `%s' undefined", alloc_name);
2194 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2195 rval = build_function_call (alloc_decl,
2196 tree_cons (NULL_TREE, class_addr,
2197 build_tree_list (NULL_TREE,
2199 rval = cp_convert (build_pointer_type (true_type), rval);
2206 args = tree_cons (NULL_TREE, size, placement);
2207 fnname = ansi_opname[code];
2210 rval = (build_new_function_call
2211 (lookup_function_nonclass (fnname, args),
2214 rval = build_method_call (build_dummy_object (true_type),
2215 fnname, args, NULL_TREE,
2217 rval = cp_convert (build_pointer_type (true_type), rval);
2220 /* unless an allocation function is declared with an empty excep-
2221 tion-specification (_except.spec_), throw(), it indicates failure to
2222 allocate storage by throwing a bad_alloc exception (clause _except_,
2223 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2224 cation function is declared with an empty exception-specification,
2225 throw(), it returns null to indicate failure to allocate storage and a
2226 non-null pointer otherwise.
2228 So check for a null exception spec on the op new we just called. */
2233 /* The CALL_EXPR. */
2234 tree t = TREE_OPERAND (rval, 0);
2236 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2237 nothrow = TYPE_NOTHROW_P (TREE_TYPE (t));
2239 check_new = (flag_check_new || nothrow) && ! use_java_new;
2241 if ((check_new || flag_exceptions) && rval)
2243 alloc_expr = get_target_expr (rval);
2244 alloc_node = rval = TREE_OPERAND (alloc_expr, 0);
2247 alloc_expr = NULL_TREE;
2249 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2250 sure we have some extra bytes in that case for the BI_header_size
2251 cookies? And how does that interact with the code below? (mrs) */
2252 /* Finish up some magic for new'ed arrays */
2253 if (use_cookie && rval != NULL_TREE)
2256 rval = convert (string_type_node, rval); /* for ptr arithmetic */
2257 rval = save_expr (build_binary_op (PLUS_EXPR, rval, cookie_size));
2258 /* Store the number of bytes allocated so that we can know how
2259 many elements to destroy later. */
2262 /* Under the new ABI, we use the last sizeof (size_t) bytes
2263 to store the number of elements. */
2264 cookie = build_indirect_ref (build (MINUS_EXPR,
2265 build_pointer_type (sizetype),
2267 size_in_bytes (sizetype)),
2269 exp1 = build (MODIFY_EXPR, void_type_node, cookie, nelts);
2274 = build_indirect_ref (build (MINUS_EXPR,
2275 build_pointer_type (BI_header_type),
2276 rval, cookie_size), NULL_PTR);
2277 exp1 = build (MODIFY_EXPR, void_type_node,
2278 build_component_ref (cookie, nelts_identifier,
2283 /* Build `(cookie = nelts, rval)' and use that as the complete
2285 rval = cp_convert (build_pointer_type (true_type), rval);
2286 rval = build_compound_expr
2287 (tree_cons (NULL_TREE, exp1,
2288 build_tree_list (NULL_TREE, rval)));
2291 if (rval == error_mark_node)
2292 return error_mark_node;
2294 /* Don't call any constructors or do any initialization. */
2295 if (init == void_type_node)
2298 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2300 if (! TYPE_NEEDS_CONSTRUCTING (type)
2301 && ! IS_AGGR_TYPE (type) && ! has_array)
2303 /* We are processing something like `new int (10)', which
2304 means allocate an int, and initialize it with 10. */
2308 /* At present RVAL is a temporary variable, created to hold
2309 the value from the call to `operator new'. We transform
2310 it to (*RVAL = INIT, RVAL). */
2311 rval = save_expr (rval);
2312 deref = build_indirect_ref (rval, NULL_PTR);
2314 /* Even for something like `new const int (10)' we must
2315 allow the expression to be non-const while we do the
2317 deref_type = TREE_TYPE (deref);
2318 if (CP_TYPE_CONST_P (deref_type))
2320 = cp_build_qualified_type (deref_type,
2321 CP_TYPE_QUALS (deref_type)
2322 & ~TYPE_QUAL_CONST);
2323 TREE_READONLY (deref) = 0;
2325 if (TREE_CHAIN (init) != NULL_TREE)
2326 pedwarn ("initializer list being treated as compound expression");
2327 else if (TREE_CODE (init) == CONSTRUCTOR)
2329 pedwarn ("initializer list appears where operand should be used");
2330 init = TREE_OPERAND (init, 1);
2332 init = build_compound_expr (init);
2334 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2335 "new", NULL_TREE, 0);
2336 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2337 build_modify_expr (deref, NOP_EXPR, init),
2339 TREE_NO_UNUSED_WARNING (rval) = 1;
2340 TREE_SIDE_EFFECTS (rval) = 1;
2342 else if (! has_array)
2345 /* Constructors are never virtual. If it has an initialization, we
2346 need to complain if we aren't allowed to use the ctor that took
2348 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2350 rval = save_expr (rval);
2353 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2354 newrval = build_indirect_ref (newrval, NULL_PTR);
2356 newrval = build_method_call (newrval,
2357 complete_ctor_identifier,
2358 init, TYPE_BINFO (true_type), flags);
2360 if (newrval == NULL_TREE || newrval == error_mark_node)
2361 return error_mark_node;
2363 newrval = build (COMPOUND_EXPR, TREE_TYPE (rval), newrval, rval);
2365 TREE_HAS_CONSTRUCTOR (rval) = 1;
2368 rval = (build_vec_init
2371 build_binary_op (MINUS_EXPR, nelts, integer_one_node),
2375 /* If any part of the object initialization terminates by throwing an
2376 exception and a suitable deallocation function can be found, the
2377 deallocation function is called to free the memory in which the
2378 object was being constructed, after which the exception continues
2379 to propagate in the context of the new-expression. If no
2380 unambiguous matching deallocation function can be found,
2381 propagating the exception does not cause the object's memory to be
2383 if (flag_exceptions && alloc_expr && ! use_java_new)
2385 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2386 tree cleanup, fn = NULL_TREE;
2387 int flags = (LOOKUP_NORMAL
2388 | (globally_qualified_p * LOOKUP_GLOBAL));
2390 /* The Standard is unclear here, but the right thing to do
2391 is to use the same method for finding deallocation
2392 functions that we use for finding allocation functions. */
2393 flags |= LOOKUP_SPECULATIVELY;
2395 /* We expect alloc_expr to look like a TARGET_EXPR around
2396 a NOP_EXPR around the CALL_EXPR we want. */
2397 fn = TREE_OPERAND (alloc_expr, 1);
2398 fn = TREE_OPERAND (fn, 0);
2400 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn);
2402 /* Ack! First we allocate the memory. Then we set our sentry
2403 variable to true, and expand a cleanup that deletes the memory
2404 if sentry is true. Then we run the constructor and store the
2405 returned pointer in buf. Then we clear sentry and return buf. */
2409 tree end, sentry, begin, buf, t = TREE_TYPE (rval);
2411 begin = get_target_expr (boolean_true_node);
2412 sentry = TREE_OPERAND (begin, 0);
2414 TREE_OPERAND (begin, 2)
2415 = build (COND_EXPR, void_type_node, sentry,
2416 cleanup, void_zero_node);
2418 rval = get_target_expr (rval);
2420 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2421 sentry, boolean_false_node);
2423 buf = TREE_OPERAND (rval, 0);
2425 rval = build (COMPOUND_EXPR, t, begin,
2426 build (COMPOUND_EXPR, t, rval,
2427 build (COMPOUND_EXPR, t, end, buf)));
2431 else if (CP_TYPE_CONST_P (true_type))
2432 cp_error ("uninitialized const in `new' of `%#T'", true_type);
2436 if (alloc_expr && rval == alloc_node)
2438 rval = TREE_OPERAND (alloc_expr, 1);
2439 alloc_expr = NULL_TREE;
2442 if (check_new && alloc_expr)
2444 /* Did we modify the storage? */
2445 tree ifexp = build_binary_op (NE_EXPR, alloc_node,
2447 rval = build_conditional_expr (ifexp, rval, alloc_node);
2451 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2453 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
2455 /* The type of new int [3][3] is not int *, but int [3] * */
2456 rval = build_c_cast (build_pointer_type (type), rval);
2463 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, use_global_delete)
2464 tree base, maxindex, type;
2465 tree auto_delete_vec;
2466 int use_global_delete;
2469 tree ptype = build_pointer_type (type = complete_type (type));
2470 tree size_exp = size_in_bytes (type);
2472 /* Temporary variables used by the loop. */
2473 tree tbase, tbase_init;
2475 /* This is the body of the loop that implements the deletion of a
2476 single element, and moves temp variables to next elements. */
2479 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2482 /* This is the thing that governs what to do after the loop has run. */
2483 tree deallocate_expr = 0;
2485 /* This is the BIND_EXPR which holds the outermost iterator of the
2486 loop. It is convenient to set this variable up and test it before
2487 executing any other code in the loop.
2488 This is also the containing expression returned by this function. */
2489 tree controller = NULL_TREE;
2491 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2493 loop = integer_zero_node;
2497 /* The below is short by BI_header_size */
2498 virtual_size = size_binop (MULT_EXPR, size_exp,
2499 convert (sizetype, maxindex));
2501 tbase = create_temporary_var (ptype);
2502 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2503 fold (build (PLUS_EXPR, ptype,
2506 DECL_REGISTER (tbase) = 1;
2507 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2508 TREE_SIDE_EFFECTS (controller) = 1;
2512 body = tree_cons (NULL_TREE,
2513 build_delete (ptype, tbase, integer_two_node,
2514 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2517 body = tree_cons (NULL_TREE,
2518 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2521 body = tree_cons (NULL_TREE,
2522 build (EXIT_EXPR, void_type_node,
2523 build (EQ_EXPR, boolean_type_node, base, tbase)),
2526 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2528 loop = tree_cons (NULL_TREE, tbase_init,
2529 tree_cons (NULL_TREE, loop, NULL_TREE));
2530 loop = build_compound_expr (loop);
2533 /* If the delete flag is one, or anything else with the low bit set,
2534 delete the storage. */
2535 if (auto_delete_vec == integer_zero_node)
2536 deallocate_expr = integer_zero_node;
2541 /* The below is short by BI_header_size */
2542 virtual_size = size_binop (MULT_EXPR, size_exp,
2543 convert (sizetype, maxindex));
2545 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2552 cookie_size = get_cookie_size (type);
2554 = cp_convert (ptype,
2555 build_binary_op (MINUS_EXPR,
2556 cp_convert (string_type_node, base),
2558 /* True size with header. */
2559 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2561 deallocate_expr = build_x_delete (base_tbd,
2562 2 | use_global_delete,
2564 deallocate_expr = fold (build (COND_EXPR, void_type_node,
2565 fold (build (BIT_AND_EXPR,
2569 deallocate_expr, integer_zero_node));
2572 if (loop && deallocate_expr != integer_zero_node)
2574 body = tree_cons (NULL_TREE, loop,
2575 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2576 body = build_compound_expr (body);
2581 /* Outermost wrapper: If pointer is null, punt. */
2582 body = fold (build (COND_EXPR, void_type_node,
2583 fold (build (NE_EXPR, boolean_type_node, base,
2584 integer_zero_node)),
2585 body, integer_zero_node));
2586 body = build1 (NOP_EXPR, void_type_node, body);
2590 TREE_OPERAND (controller, 1) = body;
2594 return cp_convert (void_type_node, body);
2598 create_temporary_var (type)
2603 decl = build_decl (VAR_DECL, NULL_TREE, type);
2604 TREE_USED (decl) = 1;
2605 DECL_ARTIFICIAL (decl) = 1;
2606 DECL_SOURCE_FILE (decl) = input_filename;
2607 DECL_SOURCE_LINE (decl) = lineno;
2608 DECL_IGNORED_P (decl) = 1;
2609 DECL_CONTEXT (decl) = current_function_decl;
2614 /* Create a new temporary variable of the indicated TYPE, initialized
2617 It is not entered into current_binding_level, because that breaks
2618 things when it comes time to do final cleanups (which take place
2619 "outside" the binding contour of the function). */
2622 get_temp_regvar (type, init)
2627 decl = create_temporary_var (type);
2628 if (building_stmt_tree ())
2629 add_decl_stmt (decl);
2630 if (!building_stmt_tree ())
2631 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
2632 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2637 /* `build_vec_init' returns tree structure that performs
2638 initialization of a vector of aggregate types.
2640 DECL is passed only for error reporting, and provides line number
2641 and source file name information.
2642 BASE is the space where the vector will be. For a vector of Ts,
2643 the type of BASE is `T*'.
2644 MAXINDEX is the maximum index of the array (one less than the
2645 number of elements).
2646 INIT is the (possibly NULL) initializer.
2648 FROM_ARRAY is 0 if we should init everything with INIT
2649 (i.e., every element initialized from INIT).
2650 FROM_ARRAY is 1 if we should index into INIT in parallel
2651 with initialization of DECL.
2652 FROM_ARRAY is 2 if we should index into INIT in parallel,
2653 but use assignment instead of initialization. */
2656 build_vec_init (decl, base, maxindex, init, from_array)
2657 tree decl, base, maxindex, init;
2661 tree base2 = NULL_TREE;
2663 tree itype = NULL_TREE;
2665 /* The type of an element in the array. */
2667 /* The type of a pointer to an element in the array. */
2672 tree try_block = NULL_TREE;
2673 tree try_body = NULL_TREE;
2674 int num_initialized_elts = 0;
2676 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2677 if (maxindex == error_mark_node)
2678 return error_mark_node;
2680 type = TREE_TYPE (TREE_TYPE (base));
2681 ptype = build_pointer_type (type);
2682 size = size_in_bytes (type);
2684 /* The code we are generating looks like:
2688 ptrdiff_t iterator = maxindex;
2690 ... initializations from CONSTRUCTOR ...
2691 if (iterator != -1) {
2693 ... initialize *base ...
2695 } while (--iterator != -1);
2698 ... destroy elements that were constructed ...
2701 We can omit the try and catch blocks if we know that the
2702 initialization will never throw an exception, or if the array
2703 elements do not have destructors. If we have a CONSTRUCTOR to
2704 give us initialization information, we emit code to initialize
2705 each of the elements before the loop in the try block, and then
2706 iterate over fewer elements. We can omit the loop completely if
2707 the elements of the array do not have constructors.
2709 We actually wrap the entire body of the above in a STMT_EXPR, for
2712 When copying from array to another, when the array elements have
2713 only trivial copy constructors, we should use __builtin_memcpy
2714 rather than generating a loop. That way, we could take advantage
2715 of whatever cleverness the back-end has for dealing with copies
2716 of blocks of memory. */
2718 begin_init_stmts (&stmt_expr, &compound_stmt);
2719 destroy_temps = stmts_are_full_exprs_p;
2720 stmts_are_full_exprs_p = 0;
2721 rval = get_temp_regvar (ptype,
2722 cp_convert (ptype, default_conversion (base)));
2723 base = get_temp_regvar (ptype, rval);
2724 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2726 /* Protect the entire array initialization so that we can destroy
2727 the partially constructed array if an exception is thrown. */
2728 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2730 try_block = begin_try_block ();
2731 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2734 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR
2735 && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl))))
2737 /* Do non-default initialization resulting from brace-enclosed
2743 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2745 tree elt = TREE_VALUE (elts);
2746 tree baseref = build1 (INDIRECT_REF, type, base);
2748 num_initialized_elts++;
2750 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2751 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2753 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2756 finish_expr_stmt (build_modify_expr
2759 build (PLUS_EXPR, build_pointer_type (type),
2761 finish_expr_stmt (build_modify_expr
2764 build (MINUS_EXPR, ptrdiff_type_node,
2765 iterator, integer_one_node)));
2768 /* Clear out INIT so that we don't get confused below. */
2771 else if (from_array)
2773 /* If initializing one array from another, initialize element by
2774 element. We rely upon the below calls the do argument
2776 if (decl == NULL_TREE)
2778 sorry ("initialization of array from dissimilar array type");
2779 return error_mark_node;
2783 base2 = default_conversion (init);
2784 itype = TREE_TYPE (base2);
2785 base2 = get_temp_regvar (itype, base2);
2786 itype = TREE_TYPE (itype);
2788 else if (TYPE_LANG_SPECIFIC (type)
2789 && TYPE_NEEDS_CONSTRUCTING (type)
2790 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2792 error ("initializer ends prematurely");
2793 return error_mark_node;
2797 /* Now, default-initialize any remaining elements. We don't need to
2798 do that if a) the type does not need constructing, or b) we've
2799 already initialized all the elements.
2801 We do need to keep going if we're copying an array. */
2804 || (TYPE_NEEDS_CONSTRUCTING (type)
2805 && ! (host_integerp (maxindex, 0)
2806 && (num_initialized_elts
2807 == tree_low_cst (maxindex, 0) + 1))))
2809 /* If the ITERATOR is equal to -1, then we don't have to loop;
2810 we've already initialized all the elements. */
2816 if_stmt = begin_if_stmt ();
2817 finish_if_stmt_cond (build (NE_EXPR, boolean_type_node,
2818 iterator, minus_one_node),
2821 /* Otherwise, loop through the elements. */
2822 do_stmt = begin_do_stmt ();
2823 do_body = begin_compound_stmt (/*has_no_scope=*/1);
2825 /* When we're not building a statement-tree, things are a little
2826 complicated. If, when we recursively call build_aggr_init,
2827 an expression containing a TARGET_EXPR is expanded, then it
2828 may get a cleanup. Then, the result of that expression is
2829 passed to finish_expr_stmt, which will call
2830 expand_start_target_temps/expand_end_target_temps. However,
2831 the latter call will not cause the cleanup to run because
2832 that block will still be on the block stack. So, we call
2833 expand_start_target_temps here manually; the corresponding
2834 call to expand_end_target_temps below will cause the cleanup
2836 if (!building_stmt_tree ())
2837 expand_start_target_temps ();
2841 tree to = build1 (INDIRECT_REF, type, base);
2845 from = build1 (INDIRECT_REF, itype, base2);
2849 if (from_array == 2)
2850 elt_init = build_modify_expr (to, NOP_EXPR, from);
2851 else if (TYPE_NEEDS_CONSTRUCTING (type))
2852 elt_init = build_aggr_init (to, from, 0);
2854 elt_init = build_modify_expr (to, NOP_EXPR, from);
2856 my_friendly_abort (57);
2858 else if (TREE_CODE (type) == ARRAY_TYPE)
2861 sorry ("cannot initialize multi-dimensional array with initializer");
2862 elt_init = (build_vec_init
2865 build_pointer_type (TREE_TYPE (type)),
2867 array_type_nelts (type), 0, 0));
2870 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2873 /* The initialization of each array element is a
2875 if (!building_stmt_tree ())
2877 finish_expr_stmt (elt_init);
2878 expand_end_target_temps ();
2882 stmts_are_full_exprs_p = 1;
2883 finish_expr_stmt (elt_init);
2884 stmts_are_full_exprs_p = 0;
2887 finish_expr_stmt (build_modify_expr
2890 build (PLUS_EXPR, build_pointer_type (type),
2893 finish_expr_stmt (build_modify_expr
2896 build (PLUS_EXPR, build_pointer_type (type),
2899 finish_compound_stmt (/*has_no_scope=*/1, do_body);
2900 finish_do_body (do_stmt);
2901 finish_do_stmt (build (NE_EXPR, boolean_type_node,
2902 build (PREDECREMENT_EXPR,
2909 finish_then_clause (if_stmt);
2913 /* Make sure to cleanup any partially constructed elements. */
2914 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2918 finish_compound_stmt (/*has_no_scope=*/1, try_body);
2919 finish_cleanup_try_block (try_block);
2920 e = build_vec_delete_1 (rval,
2921 build_binary_op (MINUS_EXPR, maxindex,
2924 /*auto_delete_vec=*/integer_zero_node,
2925 /*use_global_delete=*/0);
2926 finish_cleanup (e, try_block);
2929 /* The value of the array initialization is the address of the
2930 first element in the array. */
2931 finish_expr_stmt (rval);
2933 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
2934 stmts_are_full_exprs_p = destroy_temps;
2938 /* Free up storage of type TYPE, at address ADDR.
2940 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2943 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2944 used as the second argument to operator delete. It can include
2945 things like padding and magic size cookies. It has virtual in it,
2946 because if you have a base pointer and you delete through a virtual
2947 destructor, it should be the size of the dynamic object, not the
2948 static object, see Free Store 12.5 ISO C++.
2950 This does not call any destructors. */
2953 build_x_delete (addr, which_delete, virtual_size)
2958 int use_global_delete = which_delete & 1;
2959 int use_vec_delete = !!(which_delete & 2);
2960 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
2961 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
2963 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
2966 /* Call the destructor for EXP using the IN_CHARGE parameter. FLAGS
2967 are as for build_delete. */
2970 build_dtor_call (exp, in_charge, flags)
2975 tree name = NULL_TREE;
2981 /* First, try to figure out statically which function to call. */
2982 in_charge = fold (in_charge);
2983 if (tree_int_cst_equal (in_charge, integer_zero_node))
2984 name = base_dtor_identifier;
2985 else if (tree_int_cst_equal (in_charge, integer_one_node))
2986 name = deleting_dtor_identifier;
2987 else if (tree_int_cst_equal (in_charge, integer_two_node))
2988 name = complete_dtor_identifier;
2992 return build_method_call (exp, name, NULL_TREE, NULL_TREE, flags);
2994 return build_scoped_method_call (exp, binfo, name, NULL_TREE);
2997 /* If that didn't work, build the various alternatives. */
3000 call1 = build_method_call (exp, complete_dtor_identifier,
3001 NULL_TREE, NULL_TREE, flags);
3002 call2 = build_method_call (exp, deleting_dtor_identifier,
3003 NULL_TREE, NULL_TREE, flags);
3004 call3 = build_method_call (exp, base_dtor_identifier,
3005 NULL_TREE, NULL_TREE, flags);
3009 call1 = build_scoped_method_call (exp, binfo,
3010 complete_dtor_identifier, NULL_TREE);
3011 call2 = build_scoped_method_call (exp, binfo,
3012 deleting_dtor_identifier, NULL_TREE);
3013 call3 = build_scoped_method_call (exp, binfo,
3014 base_dtor_identifier, NULL_TREE);
3017 /* Build the conditionals. */
3018 result = build (COND_EXPR, void_type_node,
3019 fold (build (BIT_AND_EXPR, integer_type_node,
3020 in_charge, integer_two_node)),
3023 result = build (COND_EXPR, void_type_node,
3024 fold (build (BIT_AND_EXPR, integer_type_node,
3025 in_charge, integer_one_node)),
3031 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3032 ADDR is an expression which yields the store to be destroyed.
3033 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3034 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3035 virtual baseclasses.
3036 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3038 FLAGS is the logical disjunction of zero or more LOOKUP_
3039 flags. See cp-tree.h for more info.
3041 This function does not delete an object's virtual base classes. */
3044 build_delete (type, addr, auto_delete, flags, use_global_delete)
3048 int use_global_delete;
3054 if (addr == error_mark_node)
3055 return error_mark_node;
3057 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3058 set to `error_mark_node' before it gets properly cleaned up. */
3059 if (type == error_mark_node)
3060 return error_mark_node;
3062 type = TYPE_MAIN_VARIANT (type);
3064 if (TREE_CODE (type) == POINTER_TYPE)
3066 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3067 if (type != void_type_node && !complete_type_or_else (type, addr))
3068 return error_mark_node;
3069 if (TREE_CODE (type) == ARRAY_TYPE)
3071 if (! IS_AGGR_TYPE (type))
3073 /* Call the builtin operator delete. */
3074 return build_builtin_delete_call (addr);
3076 if (TREE_SIDE_EFFECTS (addr))
3077 addr = save_expr (addr);
3079 /* throw away const and volatile on target type of addr */
3080 addr = convert_force (build_pointer_type (type), addr, 0);
3081 ref = build_indirect_ref (addr, NULL_PTR);
3083 else if (TREE_CODE (type) == ARRAY_TYPE)
3086 if (TREE_SIDE_EFFECTS (addr))
3087 addr = save_expr (addr);
3088 if (TYPE_DOMAIN (type) == NULL_TREE)
3090 error ("unknown array size in delete");
3091 return error_mark_node;
3093 return build_vec_delete (addr, array_type_nelts (type),
3094 auto_delete, use_global_delete);
3098 /* Don't check PROTECT here; leave that decision to the
3099 destructor. If the destructor is accessible, call it,
3100 else report error. */
3101 addr = build_unary_op (ADDR_EXPR, addr, 0);
3102 if (TREE_SIDE_EFFECTS (addr))
3103 addr = save_expr (addr);
3105 if (TREE_CONSTANT (addr))
3106 addr = convert_pointer_to (type, addr);
3108 addr = convert_force (build_pointer_type (type), addr, 0);
3110 ref = build_indirect_ref (addr, NULL_PTR);
3113 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3115 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3117 if (auto_delete == integer_zero_node)
3118 return void_zero_node;
3120 return build_op_delete_call
3121 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3122 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3126 /* Below, we will reverse the order in which these calls are made.
3127 If we have a destructor, then that destructor will take care
3128 of the base classes; otherwise, we must do that here. */
3129 if (TYPE_HAS_DESTRUCTOR (type))
3131 tree passed_auto_delete;
3132 tree do_delete = NULL_TREE;
3135 if (use_global_delete)
3137 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3138 auto_delete, integer_one_node));
3139 tree call = build_builtin_delete_call (addr);
3141 cond = fold (build (COND_EXPR, void_type_node, cond,
3142 call, void_zero_node));
3143 if (cond != void_zero_node)
3146 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3147 auto_delete, integer_two_node));
3150 passed_auto_delete = auto_delete;
3152 expr = build_dtor_call (ref, passed_auto_delete, NULL_TREE, flags);
3154 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3156 if (flags & LOOKUP_DESTRUCTOR)
3157 /* Explicit destructor call; don't check for null pointer. */
3158 ifexp = integer_one_node;
3160 /* Handle deleting a null pointer. */
3161 ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node));
3163 if (ifexp != integer_one_node)
3164 expr = build (COND_EXPR, void_type_node,
3165 ifexp, expr, void_zero_node);
3171 /* We only get here from finish_function for a destructor. */
3172 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3173 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
3174 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3175 tree exprstmt = NULL_TREE;
3177 /* Set this again before we call anything, as we might get called
3179 TYPE_HAS_DESTRUCTOR (type) = 1;
3181 /* If we have member delete or vbases, we call delete in
3183 my_friendly_assert (auto_delete == integer_zero_node, 20000411);
3185 /* Take care of the remaining baseclasses. */
3186 for (i = 0; i < n_baseclasses; i++)
3188 base_binfo = TREE_VEC_ELT (binfos, i);
3189 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3190 || TREE_VIA_VIRTUAL (base_binfo))
3193 expr = build_scoped_method_call (ref, base_binfo,
3194 base_dtor_identifier,
3197 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3200 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3202 if (TREE_CODE (member) != FIELD_DECL)
3204 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
3206 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
3207 tree this_type = TREE_TYPE (member);
3208 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3209 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3214 return build_compound_expr (exprstmt);
3215 /* Virtual base classes make this function do nothing. */
3216 return void_zero_node;
3220 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3223 build_vbase_delete (type, decl)
3226 tree vbases = CLASSTYPE_VBASECLASSES (type);
3227 tree result = NULL_TREE;
3228 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3230 my_friendly_assert (addr != error_mark_node, 222);
3234 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
3236 result = tree_cons (NULL_TREE,
3237 build_delete (TREE_TYPE (this_addr), this_addr,
3239 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3241 vbases = TREE_CHAIN (vbases);
3243 return build_compound_expr (nreverse (result));
3246 /* Build a C++ vector delete expression.
3247 MAXINDEX is the number of elements to be deleted.
3248 ELT_SIZE is the nominal size of each element in the vector.
3249 BASE is the expression that should yield the store to be deleted.
3250 This function expands (or synthesizes) these calls itself.
3251 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3253 This also calls delete for virtual baseclasses of elements of the vector.
3255 Update: MAXINDEX is no longer needed. The size can be extracted from the
3256 start of the vector for pointers, and from the type for arrays. We still
3257 use MAXINDEX for arrays because it happens to already have one of the
3258 values we'd have to extract. (We could use MAXINDEX with pointers to
3259 confirm the size, and trap if the numbers differ; not clear that it'd
3260 be worth bothering.) */
3263 build_vec_delete (base, maxindex, auto_delete_vec, use_global_delete)
3264 tree base, maxindex;
3265 tree auto_delete_vec;
3266 int use_global_delete;
3270 if (TREE_CODE (base) == OFFSET_REF)
3271 base = resolve_offset_ref (base);
3273 type = TREE_TYPE (base);
3275 base = stabilize_reference (base);
3277 /* Since we can use base many times, save_expr it. */
3278 if (TREE_SIDE_EFFECTS (base))
3279 base = save_expr (base);
3281 if (TREE_CODE (type) == POINTER_TYPE)
3283 /* Step back one from start of vector, and read dimension. */
3288 cookie_addr = build (MINUS_EXPR,
3289 build_pointer_type (sizetype),
3291 TYPE_SIZE_UNIT (sizetype));
3292 maxindex = build_indirect_ref (cookie_addr, NULL_PTR);
3298 cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
3299 base, BI_header_size);
3300 cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3301 maxindex = build_component_ref (cookie, nelts_identifier,
3305 type = strip_array_types (TREE_TYPE (type));
3307 else if (TREE_CODE (type) == ARRAY_TYPE)
3309 /* get the total number of things in the array, maxindex is a bad name */
3310 maxindex = array_type_nelts_total (type);
3311 type = strip_array_types (type);
3312 base = build_unary_op (ADDR_EXPR, base, 1);
3316 if (base != error_mark_node)
3317 error ("type to vector delete is neither pointer or array type");
3318 return error_mark_node;
3321 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec,