1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 89, 92-98, 1999, 2000 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. */
22 /* High-level class interface. */
36 static void expand_aggr_vbase_init_1
PARAMS ((tree
, tree
, tree
, tree
));
37 static void construct_virtual_bases
PARAMS ((tree
, tree
, tree
, tree
, tree
));
38 static void expand_aggr_init_1
PARAMS ((tree
, tree
, tree
, tree
, int));
39 static void expand_default_init
PARAMS ((tree
, tree
, tree
, tree
, int));
40 static tree build_vec_delete_1
PARAMS ((tree
, tree
, tree
, tree
, int));
41 static void perform_member_init
PARAMS ((tree
, tree
, tree
, int));
42 static void sort_base_init
PARAMS ((tree
, tree
*, tree
*));
43 static tree build_builtin_delete_call
PARAMS ((tree
));
44 static int member_init_ok_or_else
PARAMS ((tree
, tree
, const char *));
45 static void expand_virtual_init
PARAMS ((tree
, tree
));
46 static tree sort_member_init
PARAMS ((tree
));
47 static tree initializing_context
PARAMS ((tree
));
48 static tree build_java_class_ref
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 *));
53 /* Set up local variable for this file. MUST BE CALLED AFTER
54 INIT_DECL_PROCESSING. */
56 static tree BI_header_type
, BI_header_size
;
58 void init_init_processing ()
62 minus_one_node
= build_int_2 (-1, -1);
64 /* Define the structure that holds header information for
65 arrays allocated via operator new. */
66 BI_header_type
= make_aggr_type (RECORD_TYPE
);
67 nelts_identifier
= get_identifier ("nelts");
68 fields
[0] = build_lang_decl (FIELD_DECL
, nelts_identifier
, sizetype
);
70 /* Use the biggest alignment supported by the target to prevent operator
71 new from returning misaligned pointers. */
72 TYPE_ALIGN (BI_header_type
) = BIGGEST_ALIGNMENT
;
73 finish_builtin_type (BI_header_type
, "__new_cookie", fields
,
75 BI_header_size
= size_in_bytes (BI_header_type
);
77 ggc_add_tree_root (&BI_header_type
, 1);
78 ggc_add_tree_root (&BI_header_size
, 1);
81 /* Called from initialize_vtbl_ptrs via dfs_walk. */
84 dfs_initialize_vtbl_ptrs (binfo
, data
)
88 if (!BINFO_PRIMARY_MARKED_P (binfo
)
89 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
91 tree base_ptr
= TREE_VALUE ((tree
) data
);
93 if (TREE_VIA_VIRTUAL (binfo
))
94 base_ptr
= convert_pointer_to_vbase (BINFO_TYPE (binfo
),
98 = build_vbase_path (PLUS_EXPR
,
99 build_pointer_type (BINFO_TYPE (binfo
)),
104 expand_virtual_init (binfo
, base_ptr
);
107 SET_BINFO_MARKED (binfo
);
112 /* Initialize all the vtable pointers for the hierarchy dominated by
116 initialize_vtbl_ptrs (type
, addr
)
120 tree list
= build_tree_list (type
, addr
);
122 /* Walk through the hierarchy, initializing the vptr in each base
123 class. We do these in pre-order because under the new ABI we
124 can't find the virtual bases for a class until we've initialized
125 the vtbl for that class. */
126 dfs_walk_real (TYPE_BINFO (type
), dfs_initialize_vtbl_ptrs
,
127 NULL
, dfs_unmarked_real_bases_queue_p
, list
);
128 dfs_walk (TYPE_BINFO (type
), dfs_unmark
,
129 dfs_marked_real_bases_queue_p
, type
);
130 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
131 expand_indirect_vtbls_init (TYPE_BINFO (type
), addr
);
136 /* Subroutine of emit_base_init. */
139 perform_member_init (member
, name
, init
, explicit)
140 tree member
, name
, init
;
144 tree type
= TREE_TYPE (member
);
146 decl
= build_component_ref (current_class_ref
, name
, NULL_TREE
, explicit);
148 if (decl
== error_mark_node
)
151 /* Deal with this here, as we will get confused if we try to call the
152 assignment op for an anonymous union. This can happen in a
153 synthesized copy constructor. */
154 if (ANON_AGGR_TYPE_P (type
))
156 init
= build (INIT_EXPR
, type
, decl
, TREE_VALUE (init
));
157 finish_expr_stmt (init
);
159 else if (TYPE_NEEDS_CONSTRUCTING (type
)
160 || (init
&& TYPE_HAS_CONSTRUCTOR (type
)))
162 /* Since `init' is already a TREE_LIST on the current_member_init_list,
163 only build it into one if we aren't already a list. */
164 if (init
!= NULL_TREE
&& TREE_CODE (init
) != TREE_LIST
)
165 init
= build_tree_list (NULL_TREE
, init
);
168 && TREE_CODE (type
) == ARRAY_TYPE
170 && TREE_CHAIN (init
) == NULL_TREE
171 && TREE_CODE (TREE_TYPE (TREE_VALUE (init
))) == ARRAY_TYPE
)
173 /* Initialization of one array from another. */
175 (build_vec_init (TREE_OPERAND (decl
, 1), decl
,
176 array_type_nelts (type
), TREE_VALUE (init
), 1));
179 finish_expr_stmt (build_aggr_init (decl
, init
, 0));
183 if (init
== NULL_TREE
)
187 /* default-initialization. */
188 if (AGGREGATE_TYPE_P (type
))
190 /* This is a default initialization of an aggregate,
191 but not one of non-POD class type. We cleverly
192 notice that the initialization rules in such a
193 case are the same as for initialization with an
194 empty brace-initialization list. We don't want
195 to call build_modify_expr as that will go looking
196 for constructors and such. */
197 tree e
= build (CONSTRUCTOR
, type
, NULL_TREE
, NULL_TREE
);
198 TREE_SIDE_EFFECTS (e
) = 1;
199 finish_expr_stmt (build (INIT_EXPR
, type
, decl
, e
));
201 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
202 cp_error ("default-initialization of `%#D', which has reference type",
205 init
= integer_zero_node
;
207 /* member traversal: note it leaves init NULL */
208 else if (TREE_CODE (TREE_TYPE (member
)) == REFERENCE_TYPE
)
209 cp_pedwarn ("uninitialized reference member `%D'", member
);
211 else if (TREE_CODE (init
) == TREE_LIST
)
213 /* There was an explicit member initialization. Do some
214 work in that case. */
215 if (TREE_CHAIN (init
))
217 warning ("initializer list treated as compound expression");
218 init
= build_compound_expr (init
);
221 init
= TREE_VALUE (init
);
225 finish_expr_stmt (build_modify_expr (decl
, INIT_EXPR
, init
));
228 if (TYPE_NEEDS_DESTRUCTOR (type
))
232 expr
= build_component_ref (current_class_ref
, name
, NULL_TREE
,
234 expr
= build_delete (type
, expr
, integer_zero_node
,
235 LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0);
237 if (expr
!= error_mark_node
)
238 finish_subobject (expr
);
242 extern int warn_reorder
;
244 /* Subroutine of emit_member_init. */
250 tree x
, member
, name
, field
;
251 tree init_list
= NULL_TREE
;
253 tree last_field
= NULL_TREE
;
255 for (member
= TYPE_FIELDS (t
); member
; member
= TREE_CHAIN (member
))
259 /* member could be, for example, a CONST_DECL for an enumerated
260 tag; we don't want to try to initialize that, since it already
262 if (TREE_CODE (member
) != FIELD_DECL
|| !DECL_NAME (member
))
265 for (x
= current_member_init_list
, pos
= 0; x
; x
= TREE_CHAIN (x
), ++pos
)
267 /* If we cleared this out, then pay no attention to it. */
268 if (TREE_PURPOSE (x
) == NULL_TREE
)
270 name
= TREE_PURPOSE (x
);
272 if (TREE_CODE (name
) == IDENTIFIER_NODE
)
273 field
= IDENTIFIER_CLASS_VALUE (name
);
276 my_friendly_assert (TREE_CODE (name
) == FIELD_DECL
, 348);
280 /* If one member shadows another, get the outermost one. */
281 if (TREE_CODE (field
) == TREE_LIST
)
282 field
= TREE_VALUE (field
);
290 cp_warning_at ("member initializers for `%#D'", last_field
);
291 cp_warning_at (" and `%#D'", field
);
292 warning (" will be re-ordered to match declaration order");
298 /* Make sure we won't try to work on this init again. */
299 TREE_PURPOSE (x
) = NULL_TREE
;
300 x
= build_tree_list (name
, TREE_VALUE (x
));
305 /* If we didn't find MEMBER in the list, create a dummy entry
306 so the two lists (INIT_LIST and the list of members) will be
308 x
= build_tree_list (NULL_TREE
, NULL_TREE
);
310 init_list
= chainon (init_list
, x
);
313 /* Initializers for base members go at the end. */
314 for (x
= current_member_init_list
; x
; x
= TREE_CHAIN (x
))
316 name
= TREE_PURPOSE (x
);
319 if (purpose_member (name
, init_list
))
321 cp_error ("multiple initializations given for member `%D'",
322 IDENTIFIER_CLASS_VALUE (name
));
326 init_list
= chainon (init_list
,
327 build_tree_list (name
, TREE_VALUE (x
)));
328 TREE_PURPOSE (x
) = NULL_TREE
;
336 sort_base_init (t
, rbase_ptr
, vbase_ptr
)
337 tree t
, *rbase_ptr
, *vbase_ptr
;
339 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
340 int n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
346 /* For warn_reorder. */
348 tree last_base
= NULL_TREE
;
350 tree rbases
= NULL_TREE
;
351 tree vbases
= NULL_TREE
;
353 /* First walk through and splice out vbase and invalid initializers.
354 Also replace names with binfos. */
356 last
= tree_cons (NULL_TREE
, NULL_TREE
, current_base_init_list
);
357 for (x
= TREE_CHAIN (last
); x
; x
= TREE_CHAIN (x
))
359 tree basetype
= TREE_PURPOSE (x
);
360 tree binfo
= NULL_TREE
;
362 if (basetype
== NULL_TREE
)
364 /* Initializer for single base class. Must not
365 use multiple inheritance or this is ambiguous. */
366 switch (n_baseclasses
)
369 cp_error ("`%T' does not have a base class to initialize",
375 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
379 binfo
= TREE_VEC_ELT (binfos
, 0);
381 else if (is_aggr_type (basetype
, 1))
383 binfo
= binfo_or_else (basetype
, t
);
384 if (binfo
== NULL_TREE
)
387 /* Virtual base classes are special cases. Their initializers
388 are recorded with this constructor, and they are used when
389 this constructor is the top-level constructor called. */
390 if (TREE_VIA_VIRTUAL (binfo
))
392 tree v
= BINFO_FOR_VBASE (BINFO_TYPE (binfo
), t
);
393 vbases
= tree_cons (v
, TREE_VALUE (x
), vbases
);
398 /* Otherwise, if it is not an immediate base class, complain. */
399 for (i
= n_baseclasses
-1; i
>= 0; i
--)
400 if (BINFO_TYPE (binfo
) == BINFO_TYPE (TREE_VEC_ELT (binfos
, i
)))
404 cp_error ("`%T' is not an immediate base class of `%T'",
405 basetype
, current_class_type
);
411 my_friendly_abort (365);
413 TREE_PURPOSE (x
) = binfo
;
414 TREE_CHAIN (last
) = x
;
417 TREE_CHAIN (last
) = NULL_TREE
;
419 /* Now walk through our regular bases and make sure they're initialized. */
421 for (i
= 0; i
< n_baseclasses
; ++i
)
423 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
426 if (TREE_VIA_VIRTUAL (base_binfo
))
429 for (x
= current_base_init_list
, pos
= 0; x
; x
= TREE_CHAIN (x
), ++pos
)
431 tree binfo
= TREE_PURPOSE (x
);
433 if (binfo
== NULL_TREE
)
436 if (binfo
== base_binfo
)
442 cp_warning_at ("base initializers for `%#T'", last_base
);
443 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo
));
444 warning (" will be re-ordered to match inheritance order");
447 last_base
= BINFO_TYPE (binfo
);
450 /* Make sure we won't try to work on this init again. */
451 TREE_PURPOSE (x
) = NULL_TREE
;
452 x
= build_tree_list (binfo
, TREE_VALUE (x
));
457 /* If we didn't find BASE_BINFO in the list, create a dummy entry
458 so the two lists (RBASES and the list of bases) will be
460 x
= build_tree_list (NULL_TREE
, NULL_TREE
);
462 rbases
= chainon (rbases
, x
);
469 /* Perform whatever initializations have yet to be done on the base
470 class of the class variable. These actions are in the global
471 variable CURRENT_BASE_INIT_LIST. Such an action could be
472 NULL_TREE, meaning that the user has explicitly called the base
473 class constructor with no arguments.
475 If there is a need for a call to a constructor, we must surround
476 that call with a pushlevel/poplevel pair, since we are technically
477 at the PARM level of scope.
479 Argument IMMEDIATELY, if zero, forces a new sequence to be
480 generated to contain these new insns, so it can be emitted later.
481 This sequence is saved in the global variable BASE_INIT_EXPR.
482 Otherwise, the insns are emitted into the current sequence.
484 Note that emit_base_init does *not* initialize virtual base
485 classes. That is done specially, elsewhere. */
493 tree rbase_init_list
, vbase_init_list
;
494 tree t_binfo
= TYPE_BINFO (t
);
495 tree binfos
= BINFO_BASETYPES (t_binfo
);
496 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
500 mem_init_list
= sort_member_init (t
);
501 current_member_init_list
= NULL_TREE
;
503 sort_base_init (t
, &rbase_init_list
, &vbase_init_list
);
504 current_base_init_list
= NULL_TREE
;
506 begin_init_stmts (&stmt_expr
, &compound_stmt
);
508 /* First, initialize the virtual base classes, if we are
509 constructing the most-derived object. */
510 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
512 tree first_arg
= TREE_CHAIN (DECL_ARGUMENTS (current_function_decl
));
513 construct_virtual_bases (t
, current_class_ref
, current_class_ptr
,
514 vbase_init_list
, first_arg
);
517 /* Now, perform initialization of non-virtual base classes. */
518 for (i
= 0; i
< n_baseclasses
; i
++)
520 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
521 tree init
= void_list_node
;
523 if (TREE_VIA_VIRTUAL (base_binfo
))
526 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo
) == t_binfo
,
529 if (TREE_PURPOSE (rbase_init_list
))
530 init
= TREE_VALUE (rbase_init_list
);
531 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo
)))
534 if (extra_warnings
&& copy_args_p (current_function_decl
))
535 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
536 BINFO_TYPE (base_binfo
));
539 if (init
!= void_list_node
)
541 member
= convert_pointer_to_real (base_binfo
, current_class_ptr
);
542 expand_aggr_init_1 (base_binfo
, NULL_TREE
,
543 build_indirect_ref (member
, NULL_PTR
), init
,
547 expand_cleanup_for_base (base_binfo
, NULL_TREE
);
548 rbase_init_list
= TREE_CHAIN (rbase_init_list
);
551 /* Initialize the vtable pointers for the class. */
552 initialize_vtbl_ptrs (t
, current_class_ptr
);
554 for (member
= TYPE_FIELDS (t
); member
; member
= TREE_CHAIN (member
))
559 /* member could be, for example, a CONST_DECL for an enumerated
560 tag; we don't want to try to initialize that, since it already
562 if (TREE_CODE (member
) != FIELD_DECL
|| !DECL_NAME (member
))
565 /* See if we had a user-specified member initialization. */
566 if (TREE_PURPOSE (mem_init_list
))
568 name
= TREE_PURPOSE (mem_init_list
);
569 init
= TREE_VALUE (mem_init_list
);
572 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
573 || TREE_CODE (name
) == FIELD_DECL
, 349);
577 name
= DECL_NAME (member
);
578 init
= DECL_INITIAL (member
);
582 /* Effective C++ rule 12. */
583 if (warn_ecpp
&& init
== NULL_TREE
584 && !DECL_ARTIFICIAL (member
)
585 && TREE_CODE (TREE_TYPE (member
)) != ARRAY_TYPE
)
586 cp_warning ("`%D' should be initialized in the member initialization list", member
);
589 perform_member_init (member
, name
, init
, from_init_list
);
590 mem_init_list
= TREE_CHAIN (mem_init_list
);
593 /* Now initialize any members from our bases. */
594 while (mem_init_list
)
596 tree name
, init
, field
;
598 if (TREE_PURPOSE (mem_init_list
))
600 name
= TREE_PURPOSE (mem_init_list
);
601 init
= TREE_VALUE (mem_init_list
);
603 if (TREE_CODE (name
) == IDENTIFIER_NODE
)
604 field
= IDENTIFIER_CLASS_VALUE (name
);
608 /* If one member shadows another, get the outermost one. */
609 if (TREE_CODE (field
) == TREE_LIST
)
611 field
= TREE_VALUE (field
);
612 if (decl_type_context (field
) != current_class_type
)
613 cp_error ("field `%D' not in immediate context", field
);
616 perform_member_init (field
, name
, init
, 1);
618 mem_init_list
= TREE_CHAIN (mem_init_list
);
621 /* All the implicit try blocks we built up will be zapped
622 when we come to a real binding contour boundary. */
623 return finish_init_stmts (stmt_expr
, compound_stmt
);
626 /* Check that all fields are properly initialized after
627 an assignment to `this'. Called only when such an assignment
628 is actually noted. */
635 for (member
= TYPE_FIELDS (t
); member
; member
= TREE_CHAIN (member
))
636 if (DECL_NAME (member
) && TREE_USED (member
))
637 cp_error ("field `%D' used before initialized (after assignment to `this')",
641 /* This code sets up the virtual function tables appropriate for
642 the pointer DECL. It is a one-ply initialization.
644 BINFO is the exact type that DECL is supposed to be. In
645 multiple inheritance, this might mean "C's A" if C : A, B. */
648 expand_virtual_init (binfo
, decl
)
651 tree type
= BINFO_TYPE (binfo
);
653 tree vtype
, vtype_binfo
;
655 /* Compute the location of the vtable. */
656 vtype
= DECL_CONTEXT (TYPE_VFIELD (type
));
657 vtype_binfo
= get_binfo (vtype
, TREE_TYPE (TREE_TYPE (decl
)), 0);
658 vtbl
= BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (TYPE_VFIELD (type
)), binfo
));
660 if (TREE_CODE (vtbl
) == VAR_DECL
)
662 assemble_external (vtbl
);
663 TREE_USED (vtbl
) = 1;
664 vtbl
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (vtbl
)), vtbl
);
667 /* Under the new ABI, secondary vtables are stored with the
668 primary vtable. So, the BINFO_VTABLE may be an expression for
669 computing the secondary vtable, rather than the secondary
671 my_friendly_assert (merge_primary_and_secondary_vtables_p (),
674 /* Under the new ABI, we need to point into the middle of the
676 if (vbase_offsets_in_vtable_p ())
677 vtbl
= build (PLUS_EXPR
, TREE_TYPE (vtbl
), vtbl
,
678 size_extra_vtbl_entries (binfo
));
680 /* Compute the location of the vtpr. */
681 decl
= convert_pointer_to_real (vtype_binfo
, decl
);
682 vtbl_ptr
= build_vfield_ref (build_indirect_ref (decl
, NULL_PTR
), vtype
);
683 if (vtbl_ptr
== error_mark_node
)
686 /* Assign the vtable to the vptr. */
687 vtbl
= convert_force (TREE_TYPE (vtbl_ptr
), vtbl
, 0);
688 finish_expr_stmt (build_modify_expr (vtbl_ptr
, NOP_EXPR
, vtbl
));
691 /* If an exception is thrown in a constructor, those base classes already
692 constructed must be destroyed. This function creates the cleanup
693 for BINFO, which has just been constructed. If FLAG is non-NULL,
694 it is a DECL which is non-zero when this base needs to be
698 expand_cleanup_for_base (binfo
, flag
)
704 if (!TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo
)))
707 /* Call the destructor. */
708 expr
= (build_scoped_method_call
709 (current_class_ref
, binfo
, dtor_identifier
,
710 build_tree_list (NULL_TREE
, integer_zero_node
)));
712 expr
= fold (build (COND_EXPR
, void_type_node
,
713 truthvalue_conversion (flag
),
714 expr
, integer_zero_node
));
716 finish_subobject (expr
);
719 /* Subroutine of `expand_aggr_vbase_init'.
720 BINFO is the binfo of the type that is being initialized.
721 INIT_LIST is the list of initializers for the virtual baseclass. */
724 expand_aggr_vbase_init_1 (binfo
, exp
, addr
, init_list
)
725 tree binfo
, exp
, addr
, init_list
;
727 tree init
= purpose_member (binfo
, init_list
);
728 tree ref
= build_indirect_ref (addr
, NULL_PTR
);
731 init
= TREE_VALUE (init
);
732 /* Call constructors, but don't set up vtables. */
733 expand_aggr_init_1 (binfo
, exp
, ref
, init
, LOOKUP_COMPLAIN
);
736 /* Construct the virtual base-classes of THIS_REF (whose address is
737 THIS_PTR). The object has the indicated TYPE. The construction
738 actually takes place only if FLAG is non-zero. INIT_LIST is list
739 of initializations for constructors to perform. */
742 construct_virtual_bases (type
, this_ref
, this_ptr
, init_list
, flag
)
751 /* If there are no virtual baseclasses, we shouldn't even be here. */
752 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type
), 19990621);
754 /* First set the pointers in our object that tell us where to find
755 our virtual baseclasses. */
756 if (!vbase_offsets_in_vtable_p ())
761 if_stmt
= begin_if_stmt ();
762 finish_if_stmt_cond (flag
, if_stmt
);
763 result
= init_vbase_pointers (type
, this_ptr
);
765 finish_expr_stmt (build_compound_expr (result
));
766 finish_then_clause (if_stmt
);
770 /* Now, run through the baseclasses, initializing each. */
771 for (vbases
= CLASSTYPE_VBASECLASSES (type
); vbases
;
772 vbases
= TREE_CHAIN (vbases
))
778 /* If there are virtual base classes with destructors, we need to
779 emit cleanups to destroy them if an exception is thrown during
780 the construction process. These exception regions (i.e., the
781 period during which the cleanups must occur) begin from the time
782 the construction is complete to the end of the function. If we
783 create a conditional block in which to initialize the
784 base-classes, then the cleanup region for the virtual base begins
785 inside a block, and ends outside of that block. This situation
786 confuses the sjlj exception-handling code. Therefore, we do not
787 create a single conditional block, but one for each
788 initialization. (That way the cleanup regions always begin
789 in the outer block.) We trust the back-end to figure out
790 that the FLAG will not change across initializations, and
791 avoid doing multiple tests. */
792 inner_if_stmt
= begin_if_stmt ();
793 finish_if_stmt_cond (flag
, inner_if_stmt
);
794 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/1);
796 /* Compute the location of the virtual base. If we're
797 constructing virtual bases, then we must be the most derived
798 class. Therefore, we don't have to look up the virtual base;
799 we already know where it is. */
800 exp
= build (PLUS_EXPR
,
801 TREE_TYPE (this_ptr
),
803 BINFO_OFFSET (vbases
));
804 exp
= build1 (NOP_EXPR
,
805 build_pointer_type (BINFO_TYPE (vbases
)),
808 expand_aggr_vbase_init_1 (vbases
, this_ref
, exp
, init_list
);
809 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt
);
810 finish_then_clause (inner_if_stmt
);
813 expand_cleanup_for_base (vbases
, flag
);
817 /* Find the context in which this FIELD can be initialized. */
820 initializing_context (field
)
823 tree t
= DECL_CONTEXT (field
);
825 /* Anonymous union members can be initialized in the first enclosing
826 non-anonymous union context. */
827 while (t
&& ANON_AGGR_TYPE_P (t
))
828 t
= TYPE_CONTEXT (t
);
832 /* Function to give error message if member initialization specification
833 is erroneous. FIELD is the member we decided to initialize.
834 TYPE is the type for which the initialization is being performed.
835 FIELD must be a member of TYPE.
837 MEMBER_NAME is the name of the member. */
840 member_init_ok_or_else (field
, type
, member_name
)
843 const char *member_name
;
845 if (field
== error_mark_node
)
847 if (field
== NULL_TREE
|| initializing_context (field
) != type
)
849 cp_error ("class `%T' does not have any field named `%s'", type
,
853 if (TREE_STATIC (field
))
855 cp_error ("field `%#D' is static; only point of initialization is its declaration",
863 /* If NAME is a viable field name for the aggregate DECL,
864 and PARMS is a viable parameter list, then expand an _EXPR
865 which describes this initialization.
867 Note that we do not need to chase through the class's base classes
868 to look for NAME, because if it's in that list, it will be handled
869 by the constructor for that base class.
871 We do not yet have a fixed-point finder to instantiate types
872 being fed to overloaded constructors. If there is a unique
873 constructor, then argument types can be got from that one.
875 If INIT is non-NULL, then it the initialization should
876 be placed in `current_base_init_list', where it will be processed
877 by `emit_base_init'. */
880 expand_member_init (exp
, name
, init
)
881 tree exp
, name
, init
;
883 tree basetype
= NULL_TREE
, field
;
886 if (exp
== NULL_TREE
)
887 return; /* complain about this later */
889 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
891 if (name
&& TREE_CODE (name
) == TYPE_DECL
)
893 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (name
));
894 name
= DECL_NAME (name
);
897 if (name
== NULL_TREE
&& IS_AGGR_TYPE (type
))
898 switch (CLASSTYPE_N_BASECLASSES (type
))
901 error ("base class initializer specified, but no base class to initialize");
904 basetype
= TYPE_BINFO_BASETYPE (type
, 0);
907 error ("initializer for unnamed base class ambiguous");
908 cp_error ("(type `%T' uses multiple inheritance)", type
);
912 my_friendly_assert (init
!= NULL_TREE
, 0);
914 /* The grammar should not allow fields which have names that are
915 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
916 may assume that this is an attempt to initialize a base class
917 member of the current type. Otherwise, it is an attempt to
918 initialize a member field. */
920 if (init
== void_type_node
)
923 if (name
== NULL_TREE
|| basetype
)
927 if (name
== NULL_TREE
)
931 name
= TYPE_IDENTIFIER (basetype
);
934 error ("no base class to initialize");
939 else if (basetype
!= type
940 && ! current_template_parms
941 && ! vec_binfo_member (basetype
,
942 TYPE_BINFO_BASETYPES (type
))
943 && ! BINFO_FOR_VBASE (basetype
, type
))
945 if (IDENTIFIER_CLASS_VALUE (name
))
947 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
948 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
951 cp_error ("type `%T' is not an immediate basetype for `%T'",
956 if (purpose_member (basetype
, current_base_init_list
))
958 cp_error ("base class `%T' already initialized", basetype
);
962 if (warn_reorder
&& current_member_init_list
)
964 cp_warning ("base initializer for `%T'", basetype
);
965 warning (" will be re-ordered to precede member initializations");
968 base_init
= build_tree_list (basetype
, init
);
969 current_base_init_list
= chainon (current_base_init_list
, base_init
);
976 field
= lookup_field (type
, name
, 1, 0);
978 if (! member_init_ok_or_else (field
, type
, IDENTIFIER_POINTER (name
)))
981 if (purpose_member (name
, current_member_init_list
))
983 cp_error ("field `%D' already initialized", field
);
987 member_init
= build_tree_list (name
, init
);
988 current_member_init_list
= chainon (current_member_init_list
, member_init
);
992 /* We are about to generate some complex initialization code.
993 Conceptually, it is all a single expression. However, we may want
994 to include conditionals, loops, and other such statement-level
995 constructs. Therefore, we build the initialization code inside a
996 statement-expression. This function starts such an expression.
997 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
998 pass them back to finish_init_stmts when the expression is
1002 begin_init_stmts (stmt_expr_p
, compound_stmt_p
)
1004 tree
*compound_stmt_p
;
1006 *stmt_expr_p
= begin_stmt_expr ();
1007 *compound_stmt_p
= begin_compound_stmt (/*has_no_scope=*/1);
1010 /* Finish out the statement-expression begun by the previous call to
1011 begin_init_stmts. Returns the statement-expression itself. */
1014 finish_init_stmts (stmt_expr
, compound_stmt
)
1018 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt
);
1019 stmt_expr
= finish_stmt_expr (stmt_expr
);
1021 /* To avoid spurious warnings about unused values, we set
1024 TREE_USED (stmt_expr
) = 1;
1029 /* This is like `expand_member_init', only it stores one aggregate
1032 INIT comes in two flavors: it is either a value which
1033 is to be stored in EXP, or it is a parameter list
1034 to go to a constructor, which will operate on EXP.
1035 If INIT is not a parameter list for a constructor, then set
1036 LOOKUP_ONLYCONVERTING.
1037 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1038 the initializer, if FLAGS is 0, then it is the (init) form.
1039 If `init' is a CONSTRUCTOR, then we emit a warning message,
1040 explaining that such initializations are invalid.
1042 If INIT resolves to a CALL_EXPR which happens to return
1043 something of the type we are looking for, then we know
1044 that we can safely use that call to perform the
1047 The virtual function table pointer cannot be set up here, because
1048 we do not really know its type.
1050 Virtual baseclass pointers are also set up here.
1052 This never calls operator=().
1054 When initializing, nothing is CONST.
1056 A default copy constructor may have to be used to perform the
1059 A constructor or a conversion operator may have to be used to
1060 perform the initialization, but not both, as it would be ambiguous. */
1063 build_aggr_init (exp
, init
, flags
)
1070 tree type
= TREE_TYPE (exp
);
1071 int was_const
= TREE_READONLY (exp
);
1072 int was_volatile
= TREE_THIS_VOLATILE (exp
);
1074 if (init
== error_mark_node
)
1075 return error_mark_node
;
1077 TREE_READONLY (exp
) = 0;
1078 TREE_THIS_VOLATILE (exp
) = 0;
1080 if (init
&& TREE_CODE (init
) != TREE_LIST
)
1081 flags
|= LOOKUP_ONLYCONVERTING
;
1083 if (TREE_CODE (type
) == ARRAY_TYPE
)
1085 /* Must arrange to initialize each element of EXP
1086 from elements of INIT. */
1087 tree itype
= init
? TREE_TYPE (init
) : NULL_TREE
;
1088 if (CP_TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
1090 TREE_TYPE (exp
) = TYPE_MAIN_VARIANT (type
);
1092 TREE_TYPE (init
) = TYPE_MAIN_VARIANT (itype
);
1094 if (init
&& TREE_TYPE (init
) == NULL_TREE
)
1096 /* Handle bad initializers like:
1100 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1104 int main(int argc, char **argv) {
1105 COMPLEX zees(1.0, 0.0)[10];
1108 error ("bad array initializer");
1109 return error_mark_node
;
1111 stmt_expr
= build_vec_init (exp
, exp
, array_type_nelts (type
), init
,
1112 init
&& same_type_p (TREE_TYPE (init
),
1114 TREE_READONLY (exp
) = was_const
;
1115 TREE_THIS_VOLATILE (exp
) = was_volatile
;
1116 TREE_TYPE (exp
) = type
;
1118 TREE_TYPE (init
) = itype
;
1122 if (TREE_CODE (exp
) == VAR_DECL
|| TREE_CODE (exp
) == PARM_DECL
)
1123 /* just know that we've seen something for this node */
1124 TREE_USED (exp
) = 1;
1126 TREE_TYPE (exp
) = TYPE_MAIN_VARIANT (type
);
1127 begin_init_stmts (&stmt_expr
, &compound_stmt
);
1128 destroy_temps
= stmts_are_full_exprs_p
;
1129 stmts_are_full_exprs_p
= 0;
1130 expand_aggr_init_1 (TYPE_BINFO (type
), exp
, exp
,
1131 init
, LOOKUP_NORMAL
|flags
);
1132 stmt_expr
= finish_init_stmts (stmt_expr
, compound_stmt
);
1133 stmts_are_full_exprs_p
= destroy_temps
;
1134 TREE_TYPE (exp
) = type
;
1135 TREE_READONLY (exp
) = was_const
;
1136 TREE_THIS_VOLATILE (exp
) = was_volatile
;
1142 expand_default_init (binfo
, true_exp
, exp
, init
, flags
)
1148 tree type
= TREE_TYPE (exp
);
1150 /* It fails because there may not be a constructor which takes
1151 its own type as the first (or only parameter), but which does
1152 take other types via a conversion. So, if the thing initializing
1153 the expression is a unit element of type X, first try X(X&),
1154 followed by initialization by X. If neither of these work
1155 out, then look hard. */
1159 if (init
&& TREE_CODE (init
) != TREE_LIST
1160 && (flags
& LOOKUP_ONLYCONVERTING
))
1162 /* Base subobjects should only get direct-initialization. */
1163 if (true_exp
!= exp
)
1166 if (flags
& DIRECT_BIND
)
1167 /* Do nothing. We hit this in two cases: Reference initialization,
1168 where we aren't initializing a real variable, so we don't want
1169 to run a new constructor; and catching an exception, where we
1170 have already built up the constructor call so we could wrap it
1171 in an exception region. */;
1172 else if (TREE_CODE (init
) == CONSTRUCTOR
)
1173 /* A brace-enclosed initializer has whatever type is
1174 required. There's no need to convert it. */
1177 init
= ocp_convert (type
, init
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
1179 if (TREE_CODE (init
) == TRY_CATCH_EXPR
)
1180 /* We need to protect the initialization of a catch parm
1181 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1182 around the TARGET_EXPR for the copy constructor. See
1183 expand_start_catch_block. */
1184 TREE_OPERAND (init
, 0) = build (INIT_EXPR
, TREE_TYPE (exp
), exp
,
1185 TREE_OPERAND (init
, 0));
1187 init
= build (INIT_EXPR
, TREE_TYPE (exp
), exp
, init
);
1188 TREE_SIDE_EFFECTS (init
) = 1;
1189 finish_expr_stmt (init
);
1193 if (init
== NULL_TREE
1194 || (TREE_CODE (init
) == TREE_LIST
&& ! TREE_TYPE (init
)))
1198 init
= TREE_VALUE (parms
);
1201 parms
= build_tree_list (NULL_TREE
, init
);
1203 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
1205 if (true_exp
== exp
)
1206 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
1208 parms
= tree_cons (NULL_TREE
, integer_zero_node
, parms
);
1209 flags
|= LOOKUP_HAS_IN_CHARGE
;
1212 rval
= build_method_call (exp
, ctor_identifier
,
1213 parms
, binfo
, flags
);
1214 if (TREE_SIDE_EFFECTS (rval
))
1215 finish_expr_stmt (rval
);
1218 /* This function is responsible for initializing EXP with INIT
1221 BINFO is the binfo of the type for who we are performing the
1222 initialization. For example, if W is a virtual base class of A and B,
1224 If we are initializing B, then W must contain B's W vtable, whereas
1225 were we initializing C, W must contain C's W vtable.
1227 TRUE_EXP is nonzero if it is the true expression being initialized.
1228 In this case, it may be EXP, or may just contain EXP. The reason we
1229 need this is because if EXP is a base element of TRUE_EXP, we
1230 don't necessarily know by looking at EXP where its virtual
1231 baseclass fields should really be pointing. But we do know
1232 from TRUE_EXP. In constructors, we don't know anything about
1233 the value being initialized.
1235 FLAGS is just passes to `build_method_call'. See that function for
1239 expand_aggr_init_1 (binfo
, true_exp
, exp
, init
, flags
)
1245 tree type
= TREE_TYPE (exp
);
1247 my_friendly_assert (init
!= error_mark_node
&& type
!= error_mark_node
, 211);
1249 /* Use a function returning the desired type to initialize EXP for us.
1250 If the function is a constructor, and its first argument is
1251 NULL_TREE, know that it was meant for us--just slide exp on
1252 in and expand the constructor. Constructors now come
1255 if (init
&& TREE_CODE (exp
) == VAR_DECL
1256 && TREE_CODE (init
) == CONSTRUCTOR
1257 && TREE_HAS_CONSTRUCTOR (init
))
1259 /* If store_init_value returns NULL_TREE, the INIT has been
1260 record in the DECL_INITIAL for EXP. That means there's
1261 nothing more we have to do. */
1262 if (!store_init_value (exp
, init
))
1264 if (!building_stmt_tree ())
1265 expand_decl_init (exp
);
1268 finish_expr_stmt (build (INIT_EXPR
, type
, exp
, init
));
1272 /* We know that expand_default_init can handle everything we want
1274 expand_default_init (binfo
, true_exp
, exp
, init
, flags
);
1277 /* Report an error if NAME is not the name of a user-defined,
1278 aggregate type. If OR_ELSE is nonzero, give an error message. */
1281 is_aggr_typedef (name
, or_else
)
1287 if (name
== error_mark_node
)
1290 if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1291 type
= IDENTIFIER_TYPE_VALUE (name
);
1295 cp_error ("`%T' is not an aggregate typedef", name
);
1299 if (! IS_AGGR_TYPE (type
)
1300 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
1301 && TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
)
1304 cp_error ("`%T' is not an aggregate type", type
);
1310 /* Report an error if TYPE is not a user-defined, aggregate type. If
1311 OR_ELSE is nonzero, give an error message. */
1314 is_aggr_type (type
, or_else
)
1318 if (type
== error_mark_node
)
1321 if (! IS_AGGR_TYPE (type
)
1322 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
1323 && TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
)
1326 cp_error ("`%T' is not an aggregate type", type
);
1332 /* Like is_aggr_typedef, but returns typedef if successful. */
1335 get_aggr_from_typedef (name
, or_else
)
1341 if (name
== error_mark_node
)
1344 if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1345 type
= IDENTIFIER_TYPE_VALUE (name
);
1349 cp_error ("`%T' fails to be an aggregate typedef", name
);
1353 if (! IS_AGGR_TYPE (type
)
1354 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
1355 && TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
)
1358 cp_error ("type `%T' is of non-aggregate type", type
);
1365 get_type_value (name
)
1368 if (name
== error_mark_node
)
1371 if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1372 return IDENTIFIER_TYPE_VALUE (name
);
1378 /* This code could just as well go in `class.c', but is placed here for
1381 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1382 the appropriate function call. */
1385 build_member_call (type
, name
, parmlist
)
1386 tree type
, name
, parmlist
;
1391 tree basetype_path
, decl
;
1393 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
1394 && TREE_CODE (type
) == NAMESPACE_DECL
)
1396 /* 'name' already refers to the decls from the namespace, since we
1397 hit do_identifier for template_ids. */
1398 method_name
= TREE_OPERAND (name
, 0);
1399 /* FIXME: Since we don't do independent names right yet, the
1400 name might also be a LOOKUP_EXPR. Once we resolve this to a
1401 real decl earlier, this can go. This may happen during
1403 if (TREE_CODE (method_name
) == LOOKUP_EXPR
)
1405 method_name
= lookup_namespace_name
1406 (type
, TREE_OPERAND (method_name
, 0));
1407 TREE_OPERAND (name
, 0) = method_name
;
1409 my_friendly_assert (is_overloaded_fn (method_name
), 980519);
1410 return build_x_function_call (name
, parmlist
, current_class_ref
);
1413 if (type
== std_node
)
1414 return build_x_function_call (do_scoped_id (name
, 0), parmlist
,
1416 if (TREE_CODE (type
) == NAMESPACE_DECL
)
1417 return build_x_function_call (lookup_namespace_name (type
, name
),
1418 parmlist
, current_class_ref
);
1420 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
1422 method_name
= TREE_OPERAND (name
, 0);
1423 if (TREE_CODE (method_name
) == COMPONENT_REF
)
1424 method_name
= TREE_OPERAND (method_name
, 1);
1425 if (is_overloaded_fn (method_name
))
1426 method_name
= DECL_NAME (OVL_CURRENT (method_name
));
1427 TREE_OPERAND (name
, 0) = method_name
;
1432 if (TREE_CODE (method_name
) == BIT_NOT_EXPR
)
1434 method_name
= TREE_OPERAND (method_name
, 0);
1438 /* This shouldn't be here, and build_member_call shouldn't appear in
1440 if (type
&& TREE_CODE (type
) == IDENTIFIER_NODE
1441 && get_aggr_from_typedef (type
, 0) == 0)
1443 tree ns
= lookup_name (type
, 0);
1444 if (ns
&& TREE_CODE (ns
) == NAMESPACE_DECL
)
1446 return build_x_function_call (build_offset_ref (type
, name
), parmlist
, current_class_ref
);
1450 if (type
== NULL_TREE
|| ! is_aggr_type (type
, 1))
1451 return error_mark_node
;
1453 /* An operator we did not like. */
1454 if (name
== NULL_TREE
)
1455 return error_mark_node
;
1459 cp_error ("cannot call destructor `%T::~%T' without object", type
,
1461 return error_mark_node
;
1464 decl
= maybe_dummy_object (type
, &basetype_path
);
1466 /* Convert 'this' to the specified type to disambiguate conversion
1467 to the function's context. Apparently Standard C++ says that we
1468 shouldn't do this. */
1469 if (decl
== current_class_ref
1471 && ACCESSIBLY_UNIQUELY_DERIVED_P (type
, current_class_type
))
1473 tree olddecl
= current_class_ptr
;
1474 tree oldtype
= TREE_TYPE (TREE_TYPE (olddecl
));
1475 if (oldtype
!= type
)
1477 tree newtype
= build_qualified_type (type
, TYPE_QUALS (oldtype
));
1478 decl
= convert_force (build_pointer_type (newtype
), olddecl
, 0);
1479 decl
= build_indirect_ref (decl
, NULL_PTR
);
1483 if (method_name
== constructor_name (type
)
1484 || method_name
== constructor_name_full (type
))
1485 return build_functional_cast (type
, parmlist
);
1486 if (lookup_fnfields (basetype_path
, method_name
, 0))
1487 return build_method_call (decl
,
1488 TREE_CODE (name
) == TEMPLATE_ID_EXPR
1489 ? name
: method_name
,
1490 parmlist
, basetype_path
,
1491 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
1492 if (TREE_CODE (name
) == IDENTIFIER_NODE
1493 && ((t
= lookup_field (TYPE_BINFO (type
), name
, 1, 0))))
1495 if (t
== error_mark_node
)
1496 return error_mark_node
;
1497 if (TREE_CODE (t
) == FIELD_DECL
)
1499 if (is_dummy_object (decl
))
1501 cp_error ("invalid use of non-static field `%D'", t
);
1502 return error_mark_node
;
1504 decl
= build (COMPONENT_REF
, TREE_TYPE (t
), decl
, t
);
1506 else if (TREE_CODE (t
) == VAR_DECL
)
1510 cp_error ("invalid use of member `%D'", t
);
1511 return error_mark_node
;
1513 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl
)))
1514 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, decl
,
1515 parmlist
, NULL_TREE
);
1516 return build_function_call (decl
, parmlist
);
1520 cp_error ("no method `%T::%D'", type
, name
);
1521 return error_mark_node
;
1525 /* Build a reference to a member of an aggregate. This is not a
1526 C++ `&', but really something which can have its address taken,
1527 and then act as a pointer to member, for example TYPE :: FIELD
1528 can have its address taken by saying & TYPE :: FIELD.
1530 @@ Prints out lousy diagnostics for operator <typename>
1533 @@ This function should be rewritten and placed in search.c. */
1536 build_offset_ref (type
, name
)
1539 tree decl
, t
= error_mark_node
;
1541 tree basebinfo
= NULL_TREE
;
1542 tree orig_name
= name
;
1544 /* class templates can come in as TEMPLATE_DECLs here. */
1545 if (TREE_CODE (name
) == TEMPLATE_DECL
)
1548 if (type
== std_node
)
1549 return do_scoped_id (name
, 0);
1551 if (processing_template_decl
|| uses_template_parms (type
))
1552 return build_min_nt (SCOPE_REF
, type
, name
);
1554 /* Handle namespace names fully here. */
1555 if (TREE_CODE (type
) == NAMESPACE_DECL
)
1557 t
= lookup_namespace_name (type
, name
);
1558 if (t
!= error_mark_node
&& ! type_unknown_p (t
))
1561 t
= convert_from_reference (t
);
1566 if (type
== NULL_TREE
|| ! is_aggr_type (type
, 1))
1567 return error_mark_node
;
1569 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
1571 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1572 something like `a.template f<int>' or the like. For the most
1573 part, we treat this just like a.f. We do remember, however,
1574 the template-id that was used. */
1575 name
= TREE_OPERAND (orig_name
, 0);
1577 if (TREE_CODE (name
) == LOOKUP_EXPR
)
1578 /* This can happen during tsubst'ing. */
1579 name
= TREE_OPERAND (name
, 0);
1581 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 0);
1584 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1586 if (! check_dtor_name (type
, name
))
1587 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1588 type
, TREE_OPERAND (name
, 0));
1589 name
= dtor_identifier
;
1592 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */
1593 else if (name
== constructor_name_full (type
)
1594 || name
== constructor_name (type
))
1595 name
= ctor_identifier
;
1598 if (TYPE_SIZE (complete_type (type
)) == 0
1599 && !TYPE_BEING_DEFINED (type
))
1601 cp_error ("incomplete type `%T' does not have member `%D'", type
,
1603 return error_mark_node
;
1606 decl
= maybe_dummy_object (type
, &basebinfo
);
1608 member
= lookup_member (basebinfo
, name
, 1, 0);
1610 if (member
== error_mark_node
)
1611 return error_mark_node
;
1613 /* A lot of this logic is now handled in lookup_field and
1615 if (member
&& BASELINK_P (member
))
1617 /* Go from the TREE_BASELINK to the member function info. */
1618 tree fnfields
= member
;
1619 t
= TREE_VALUE (fnfields
);
1621 if (TREE_CODE (orig_name
) == TEMPLATE_ID_EXPR
)
1623 /* The FNFIELDS are going to contain functions that aren't
1624 necessarily templates, and templates that don't
1625 necessarily match the explicit template parameters. We
1626 save all the functions, and the explicit parameters, and
1627 then figure out exactly what to instantiate with what
1628 arguments in instantiate_type. */
1630 if (TREE_CODE (t
) != OVERLOAD
)
1631 /* The code in instantiate_type which will process this
1632 expects to encounter OVERLOADs, not raw functions. */
1633 t
= ovl_cons (t
, NULL_TREE
);
1635 return build (OFFSET_REF
,
1638 build (TEMPLATE_ID_EXPR
,
1641 TREE_OPERAND (orig_name
, 1)));
1644 if (!really_overloaded_fn (t
))
1646 /* Get rid of a potential OVERLOAD around it */
1647 t
= OVL_CURRENT (t
);
1649 /* unique functions are handled easily. */
1650 basebinfo
= TREE_PURPOSE (fnfields
);
1651 if (!enforce_access (basebinfo
, t
))
1652 return error_mark_node
;
1654 if (DECL_STATIC_FUNCTION_P (t
))
1656 return build (OFFSET_REF
, TREE_TYPE (t
), decl
, t
);
1659 TREE_TYPE (fnfields
) = unknown_type_node
;
1660 return build (OFFSET_REF
, unknown_type_node
, decl
, fnfields
);
1667 cp_error ("`%D' is not a member of type `%T'", name
, type
);
1668 return error_mark_node
;
1671 if (TREE_CODE (t
) == TYPE_DECL
)
1676 /* static class members and class-specific enum
1677 values can be returned without further ado. */
1678 if (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == CONST_DECL
)
1681 return convert_from_reference (t
);
1684 if (TREE_CODE (t
) == FIELD_DECL
&& DECL_C_BIT_FIELD (t
))
1686 cp_error ("illegal pointer to bit field `%D'", t
);
1687 return error_mark_node
;
1690 /* static class functions too. */
1691 if (TREE_CODE (t
) == FUNCTION_DECL
1692 && TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1693 my_friendly_abort (53);
1695 /* In member functions, the form `type::name' is no longer
1696 equivalent to `this->type::name', at least not until
1697 resolve_offset_ref. */
1698 return build (OFFSET_REF
, build_offset_type (type
, TREE_TYPE (t
)), decl
, t
);
1701 /* If a OFFSET_REF made it through to here, then it did
1702 not have its address taken. */
1705 resolve_offset_ref (exp
)
1708 tree type
= TREE_TYPE (exp
);
1709 tree base
= NULL_TREE
;
1711 tree basetype
, addr
;
1713 if (TREE_CODE (exp
) == OFFSET_REF
)
1715 member
= TREE_OPERAND (exp
, 1);
1716 base
= TREE_OPERAND (exp
, 0);
1720 my_friendly_assert (TREE_CODE (type
) == OFFSET_TYPE
, 214);
1721 if (TYPE_OFFSET_BASETYPE (type
) != current_class_type
)
1723 error ("object missing in use of pointer-to-member construct");
1724 return error_mark_node
;
1727 type
= TREE_TYPE (type
);
1728 base
= current_class_ref
;
1731 if (BASELINK_P (member
))
1733 if (! flag_ms_extensions
)
1734 cp_pedwarn ("assuming & on overloaded member function");
1735 return build_unary_op (ADDR_EXPR
, exp
, 0);
1738 if (TREE_CODE (TREE_TYPE (member
)) == METHOD_TYPE
)
1740 if (! flag_ms_extensions
)
1741 cp_pedwarn ("assuming & on `%E'", member
);
1742 return build_unary_op (ADDR_EXPR
, exp
, 0);
1745 if ((TREE_CODE (member
) == VAR_DECL
1746 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member
))
1747 && ! TYPE_PTRMEM_P (TREE_TYPE (member
)))
1748 || TREE_CODE (TREE_TYPE (member
)) == FUNCTION_TYPE
)
1750 /* These were static members. */
1751 if (mark_addressable (member
) == 0)
1752 return error_mark_node
;
1756 if (TREE_CODE (TREE_TYPE (member
)) == POINTER_TYPE
1757 && TREE_CODE (TREE_TYPE (TREE_TYPE (member
))) == METHOD_TYPE
)
1760 /* Syntax error can cause a member which should
1761 have been seen as static to be grok'd as non-static. */
1762 if (TREE_CODE (member
) == FIELD_DECL
&& current_class_ref
== NULL_TREE
)
1764 if (TREE_ADDRESSABLE (member
) == 0)
1766 cp_error_at ("member `%D' is non-static but referenced as a static member",
1768 error ("at this point in file");
1769 TREE_ADDRESSABLE (member
) = 1;
1771 return error_mark_node
;
1774 /* The first case is really just a reference to a member of `this'. */
1775 if (TREE_CODE (member
) == FIELD_DECL
1776 && (base
== current_class_ref
|| is_dummy_object (base
)))
1781 if (TREE_CODE (exp
) == OFFSET_REF
&& TREE_CODE (type
) == OFFSET_TYPE
)
1782 basetype
= TYPE_OFFSET_BASETYPE (type
);
1784 basetype
= DECL_CONTEXT (member
);
1786 base
= current_class_ptr
;
1788 if (get_base_distance (basetype
, TREE_TYPE (TREE_TYPE (base
)), 0, &basetype_path
) < 0)
1790 error_not_base_type (basetype
, TREE_TYPE (TREE_TYPE (base
)));
1791 return error_mark_node
;
1793 /* Kludge: we need to use basetype_path now, because
1794 convert_pointer_to will bash it. */
1795 enforce_access (basetype_path
, member
);
1796 addr
= convert_pointer_to (basetype
, base
);
1798 /* Even in the case of illegal access, we form the
1799 COMPONENT_REF; that will allow better error recovery than
1800 just feeding back error_mark_node. */
1801 expr
= build (COMPONENT_REF
, TREE_TYPE (member
),
1802 build_indirect_ref (addr
, NULL_PTR
), member
);
1803 return convert_from_reference (expr
);
1806 /* Ensure that we have an object. */
1807 if (is_dummy_object (base
))
1808 addr
= error_mark_node
;
1810 /* If this is a reference to a member function, then return the
1811 address of the member function (which may involve going
1812 through the object's vtable), otherwise, return an expression
1813 for the dereferenced pointer-to-member construct. */
1814 addr
= build_unary_op (ADDR_EXPR
, base
, 0);
1816 if (TYPE_PTRMEM_P (TREE_TYPE (member
)))
1818 if (addr
== error_mark_node
)
1820 cp_error ("object missing in `%E'", exp
);
1821 return error_mark_node
;
1824 basetype
= TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member
)));
1825 addr
= convert_pointer_to (basetype
, addr
);
1826 member
= cp_convert (ptrdiff_type_node
, member
);
1828 /* Pointer to data members are offset by one, so that a null
1829 pointer with a real value of 0 is distinguishable from an
1830 offset of the first member of a structure. */
1831 member
= build_binary_op (MINUS_EXPR
, member
,
1832 cp_convert (ptrdiff_type_node
, integer_one_node
));
1834 return build1 (INDIRECT_REF
, type
,
1835 build (PLUS_EXPR
, build_pointer_type (type
),
1838 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member
)))
1840 return get_member_function_from_ptrfunc (&addr
, member
);
1842 my_friendly_abort (56);
1847 /* Return either DECL or its known constant value (if it has one). */
1850 decl_constant_value (decl
)
1853 if (! TREE_THIS_VOLATILE (decl
)
1854 && DECL_INITIAL (decl
)
1855 && DECL_INITIAL (decl
) != error_mark_node
1856 /* This is invalid if initial value is not constant.
1857 If it has either a function call, a memory reference,
1858 or a variable, then re-evaluating it could give different results. */
1859 && TREE_CONSTANT (DECL_INITIAL (decl
))
1860 /* Check for cases where this is sub-optimal, even though valid. */
1861 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1862 return DECL_INITIAL (decl
);
1866 /* Common subroutines of build_new and build_vec_delete. */
1868 /* Call the global __builtin_delete to delete ADDR. */
1871 build_builtin_delete_call (addr
)
1874 mark_used (global_delete_fndecl
);
1875 return build_call (global_delete_fndecl
,
1876 void_type_node
, build_tree_list (NULL_TREE
, addr
));
1879 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1880 (which needs to go through some sort of groktypename) or it
1881 is the name of the class we are newing. INIT is an initialization value.
1882 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1883 If INIT is void_type_node, it means do *not* call a constructor
1886 For types with constructors, the data returned is initialized
1887 by the appropriate constructor.
1889 Whether the type has a constructor or not, if it has a pointer
1890 to a virtual function table, then that pointer is set up
1893 Unless I am mistaken, a call to new () will return initialized
1894 data regardless of whether the constructor itself is private or
1895 not. NOPE; new fails if the constructor is private (jcm).
1897 Note that build_new does nothing to assure that any special
1898 alignment requirements of the type are met. Rather, it leaves
1899 it up to malloc to do the right thing. Otherwise, folding to
1900 the right alignment cal cause problems if the user tries to later
1901 free the memory returned by `new'.
1903 PLACEMENT is the `placement' list for user-defined operator new (). */
1905 extern int flag_check_new
;
1908 build_new (placement
, decl
, init
, use_global_new
)
1914 tree nelts
= NULL_TREE
, t
;
1917 if (decl
== error_mark_node
)
1918 return error_mark_node
;
1920 if (TREE_CODE (decl
) == TREE_LIST
)
1922 tree absdcl
= TREE_VALUE (decl
);
1923 tree last_absdcl
= NULL_TREE
;
1925 if (current_function_decl
1926 && DECL_CONSTRUCTOR_P (current_function_decl
))
1927 my_friendly_assert (immediate_size_expand
== 0, 19990926);
1929 nelts
= integer_one_node
;
1931 if (absdcl
&& TREE_CODE (absdcl
) == CALL_EXPR
)
1932 my_friendly_abort (215);
1933 while (absdcl
&& TREE_CODE (absdcl
) == INDIRECT_REF
)
1935 last_absdcl
= absdcl
;
1936 absdcl
= TREE_OPERAND (absdcl
, 0);
1939 if (absdcl
&& TREE_CODE (absdcl
) == ARRAY_REF
)
1941 /* probably meant to be a vec new */
1944 while (TREE_OPERAND (absdcl
, 0)
1945 && TREE_CODE (TREE_OPERAND (absdcl
, 0)) == ARRAY_REF
)
1947 last_absdcl
= absdcl
;
1948 absdcl
= TREE_OPERAND (absdcl
, 0);
1952 this_nelts
= TREE_OPERAND (absdcl
, 1);
1953 if (this_nelts
!= error_mark_node
)
1955 if (this_nelts
== NULL_TREE
)
1956 error ("new of array type fails to specify size");
1957 else if (processing_template_decl
)
1960 absdcl
= TREE_OPERAND (absdcl
, 0);
1964 int flags
= pedantic
? WANT_INT
: (WANT_INT
| WANT_ENUM
);
1965 if (build_expr_type_conversion (flags
, this_nelts
, 0)
1967 pedwarn ("size in array new must have integral type");
1969 this_nelts
= save_expr (cp_convert (sizetype
, this_nelts
));
1970 absdcl
= TREE_OPERAND (absdcl
, 0);
1971 if (this_nelts
== integer_zero_node
)
1973 warning ("zero size array reserves no space");
1974 nelts
= integer_zero_node
;
1977 nelts
= build_binary_op (MULT_EXPR
, nelts
, this_nelts
);
1981 nelts
= integer_zero_node
;
1985 TREE_OPERAND (last_absdcl
, 0) = absdcl
;
1987 TREE_VALUE (decl
) = absdcl
;
1989 type
= groktypename (decl
);
1990 if (! type
|| type
== error_mark_node
)
1991 return error_mark_node
;
1993 else if (TREE_CODE (decl
) == IDENTIFIER_NODE
)
1995 if (IDENTIFIER_HAS_TYPE_VALUE (decl
))
1997 /* An aggregate type. */
1998 type
= IDENTIFIER_TYPE_VALUE (decl
);
1999 decl
= TYPE_MAIN_DECL (type
);
2003 /* A builtin type. */
2004 decl
= lookup_name (decl
, 1);
2005 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 215);
2006 type
= TREE_TYPE (decl
);
2009 else if (TREE_CODE (decl
) == TYPE_DECL
)
2011 type
= TREE_TYPE (decl
);
2016 decl
= TYPE_MAIN_DECL (type
);
2019 if (processing_template_decl
)
2022 t
= tree_cons (tree_cons (NULL_TREE
, type
, NULL_TREE
),
2023 build_min_nt (ARRAY_REF
, NULL_TREE
, nelts
),
2028 rval
= build_min_nt (NEW_EXPR
, placement
, t
, init
);
2029 NEW_EXPR_USE_GLOBAL (rval
) = use_global_new
;
2033 /* ``A reference cannot be created by the new operator. A reference
2034 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2035 returned by new.'' ARM 5.3.3 */
2036 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2038 error ("new cannot be applied to a reference type");
2039 type
= TREE_TYPE (type
);
2042 if (TREE_CODE (type
) == FUNCTION_TYPE
)
2044 error ("new cannot be applied to a function type");
2045 return error_mark_node
;
2048 /* When the object being created is an array, the new-expression yields a
2049 pointer to the initial element (if any) of the array. For example,
2050 both new int and new int[10] return an int*. 5.3.4. */
2051 if (TREE_CODE (type
) == ARRAY_TYPE
&& has_array
== 0)
2053 nelts
= array_type_nelts_top (type
);
2055 type
= TREE_TYPE (type
);
2059 t
= build_nt (ARRAY_REF
, type
, nelts
);
2063 rval
= build (NEW_EXPR
, build_pointer_type (type
), placement
, t
, init
);
2064 NEW_EXPR_USE_GLOBAL (rval
) = use_global_new
;
2065 TREE_SIDE_EFFECTS (rval
) = 1;
2066 rval
= build_new_1 (rval
);
2067 if (rval
== error_mark_node
)
2068 return error_mark_node
;
2070 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2071 rval
= build1 (NOP_EXPR
, TREE_TYPE (rval
), rval
);
2072 TREE_NO_UNUSED_WARNING (rval
) = 1;
2077 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2080 build_java_class_ref (type
)
2083 tree name
, class_decl
;
2084 static tree CL_prefix
= NULL_TREE
;
2085 if (CL_prefix
== NULL_TREE
)
2086 CL_prefix
= get_identifier("_CL_");
2087 if (jclass_node
== NULL_TREE
)
2089 jclass_node
= IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2090 if (jclass_node
== NULL_TREE
)
2091 fatal("call to Java constructor, while `jclass' undefined");
2092 jclass_node
= TREE_TYPE (jclass_node
);
2094 name
= build_overload_with_type (CL_prefix
, type
);
2095 class_decl
= IDENTIFIER_GLOBAL_VALUE (name
);
2096 if (class_decl
== NULL_TREE
)
2098 class_decl
= build_decl (VAR_DECL
, name
, TREE_TYPE (jclass_node
));
2099 TREE_STATIC (class_decl
) = 1;
2100 DECL_EXTERNAL (class_decl
) = 1;
2101 TREE_PUBLIC (class_decl
) = 1;
2102 DECL_ARTIFICIAL (class_decl
) = 1;
2103 DECL_IGNORED_P (class_decl
) = 1;
2104 pushdecl_top_level (class_decl
);
2105 make_decl_rtl (class_decl
, NULL_PTR
, 1);
2110 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2111 value is immediately handed to expand_expr. */
2117 tree placement
, init
;
2118 tree type
, true_type
, size
, rval
;
2119 tree nelts
= NULL_TREE
;
2120 tree alloc_expr
, alloc_node
= NULL_TREE
;
2122 enum tree_code code
= NEW_EXPR
;
2123 int use_cookie
, nothrow
, check_new
;
2125 int use_java_new
= 0;
2127 placement
= TREE_OPERAND (exp
, 0);
2128 type
= TREE_OPERAND (exp
, 1);
2129 init
= TREE_OPERAND (exp
, 2);
2130 use_global_new
= NEW_EXPR_USE_GLOBAL (exp
);
2132 if (TREE_CODE (type
) == ARRAY_REF
)
2135 nelts
= TREE_OPERAND (type
, 1);
2136 type
= TREE_OPERAND (type
, 0);
2140 if (CP_TYPE_QUALS (type
))
2141 type
= TYPE_MAIN_VARIANT (type
);
2143 /* If our base type is an array, then make sure we know how many elements
2145 while (TREE_CODE (true_type
) == ARRAY_TYPE
)
2147 tree this_nelts
= array_type_nelts_top (true_type
);
2148 nelts
= build_binary_op (MULT_EXPR
, nelts
, this_nelts
);
2149 true_type
= TREE_TYPE (true_type
);
2152 if (!complete_type_or_else (true_type
, exp
))
2153 return error_mark_node
;
2156 size
= fold (build_binary_op (MULT_EXPR
, size_in_bytes (true_type
),
2159 size
= size_in_bytes (type
);
2161 if (TREE_CODE (true_type
) == VOID_TYPE
)
2163 error ("invalid type `void' for new");
2164 return error_mark_node
;
2167 if (abstract_virtuals_error (NULL_TREE
, true_type
))
2168 return error_mark_node
;
2170 /* When we allocate an array, and the corresponding deallocation
2171 function takes a second argument of type size_t, and that's the
2172 "usual deallocation function", we allocate some extra space at
2173 the beginning of the array to store the size of the array.
2175 Well, that's what we should do. For backwards compatibility, we
2176 have to do this whenever there's a two-argument array-delete
2179 FIXME: For -fnew-abi, we don't have to maintain backwards
2180 compatibility and we should fix this. */
2181 use_cookie
= (has_array
&& TYPE_VEC_NEW_USES_COOKIE (true_type
)
2182 && ! (placement
&& ! TREE_CHAIN (placement
)
2183 && TREE_TYPE (TREE_VALUE (placement
)) == ptr_type_node
));
2186 size
= size_binop (PLUS_EXPR
, size
, BI_header_size
);
2190 code
= VEC_NEW_EXPR
;
2192 if (init
&& pedantic
)
2193 cp_pedwarn ("initialization in array new");
2196 /* Allocate the object. */
2198 if (! placement
&& TYPE_FOR_JAVA (true_type
))
2200 tree class_addr
, alloc_decl
;
2201 tree class_decl
= build_java_class_ref (true_type
);
2202 tree class_size
= size_in_bytes (true_type
);
2203 static char alloc_name
[] = "_Jv_AllocObject";
2205 alloc_decl
= IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name
));
2206 if (alloc_decl
== NULL_TREE
)
2207 fatal("call to Java constructor, while `%s' undefined", alloc_name
);
2208 class_addr
= build1 (ADDR_EXPR
, jclass_node
, class_decl
);
2209 rval
= build_function_call (alloc_decl
,
2210 tree_cons (NULL_TREE
, class_addr
,
2211 build_tree_list (NULL_TREE
,
2213 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2217 rval
= build_op_new_call
2218 (code
, true_type
, tree_cons (NULL_TREE
, size
, placement
),
2219 LOOKUP_NORMAL
| (use_global_new
* LOOKUP_GLOBAL
));
2220 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2223 /* unless an allocation function is declared with an empty excep-
2224 tion-specification (_except.spec_), throw(), it indicates failure to
2225 allocate storage by throwing a bad_alloc exception (clause _except_,
2226 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2227 cation function is declared with an empty exception-specification,
2228 throw(), it returns null to indicate failure to allocate storage and a
2229 non-null pointer otherwise.
2231 So check for a null exception spec on the op new we just called. */
2236 /* The CALL_EXPR. */
2237 tree t
= TREE_OPERAND (rval
, 0);
2239 t
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
2240 nothrow
= TYPE_NOTHROW_P (TREE_TYPE (t
));
2242 check_new
= (flag_check_new
|| nothrow
) && ! use_java_new
;
2244 if ((check_new
|| flag_exceptions
) && rval
)
2246 alloc_expr
= get_target_expr (rval
);
2247 alloc_node
= rval
= TREE_OPERAND (alloc_expr
, 0);
2250 alloc_expr
= NULL_TREE
;
2252 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2253 sure we have some extra bytes in that case for the BI_header_size
2254 cookies? And how does that interact with the code below? (mrs) */
2255 /* Finish up some magic for new'ed arrays */
2256 if (use_cookie
&& rval
!= NULL_TREE
)
2258 tree extra
= BI_header_size
;
2260 rval
= convert (string_type_node
, rval
); /* for ptr arithmetic */
2261 rval
= save_expr (build_binary_op (PLUS_EXPR
, rval
, extra
));
2262 /* Store header info. */
2263 cookie
= build_indirect_ref (build (MINUS_EXPR
,
2264 build_pointer_type (BI_header_type
),
2265 rval
, extra
), NULL_PTR
);
2266 exp1
= build (MODIFY_EXPR
, void_type_node
,
2267 build_component_ref (cookie
, nelts_identifier
,
2270 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2271 rval
= build_compound_expr
2272 (tree_cons (NULL_TREE
, exp1
,
2273 build_tree_list (NULL_TREE
, rval
)));
2276 if (rval
== error_mark_node
)
2277 return error_mark_node
;
2279 /* Don't call any constructors or do any initialization. */
2280 if (init
== void_type_node
)
2283 if (TYPE_NEEDS_CONSTRUCTING (type
) || init
)
2285 if (! TYPE_NEEDS_CONSTRUCTING (type
)
2286 && ! IS_AGGR_TYPE (type
) && ! has_array
)
2288 /* We are processing something like `new int (10)', which
2289 means allocate an int, and initialize it with 10. */
2293 /* At present RVAL is a temporary variable, created to hold
2294 the value from the call to `operator new'. We transform
2295 it to (*RVAL = INIT, RVAL). */
2296 rval
= save_expr (rval
);
2297 deref
= build_indirect_ref (rval
, NULL_PTR
);
2299 /* Even for something like `new const int (10)' we must
2300 allow the expression to be non-const while we do the
2302 deref_type
= TREE_TYPE (deref
);
2303 if (CP_TYPE_CONST_P (deref_type
))
2305 = cp_build_qualified_type (deref_type
,
2306 CP_TYPE_QUALS (deref_type
)
2307 & ~TYPE_QUAL_CONST
);
2308 TREE_READONLY (deref
) = 0;
2310 if (TREE_CHAIN (init
) != NULL_TREE
)
2311 pedwarn ("initializer list being treated as compound expression");
2312 else if (TREE_CODE (init
) == CONSTRUCTOR
)
2314 pedwarn ("initializer list appears where operand should be used");
2315 init
= TREE_OPERAND (init
, 1);
2317 init
= build_compound_expr (init
);
2319 init
= convert_for_initialization (deref
, type
, init
, LOOKUP_NORMAL
,
2320 "new", NULL_TREE
, 0);
2321 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
),
2322 build_modify_expr (deref
, NOP_EXPR
, init
),
2324 TREE_NO_UNUSED_WARNING (rval
) = 1;
2325 TREE_SIDE_EFFECTS (rval
) = 1;
2327 else if (! has_array
)
2330 /* Constructors are never virtual. If it has an initialization, we
2331 need to complain if we aren't allowed to use the ctor that took
2333 int flags
= LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_COMPLAIN
;
2335 if (rval
&& TYPE_USES_VIRTUAL_BASECLASSES (true_type
))
2337 init
= tree_cons (NULL_TREE
, integer_one_node
, init
);
2338 flags
|= LOOKUP_HAS_IN_CHARGE
;
2342 rval
= save_expr (rval
);
2345 if (newrval
&& TREE_CODE (TREE_TYPE (newrval
)) == POINTER_TYPE
)
2346 newrval
= build_indirect_ref (newrval
, NULL_PTR
);
2348 newrval
= build_method_call (newrval
, ctor_identifier
,
2349 init
, TYPE_BINFO (true_type
), flags
);
2351 if (newrval
== NULL_TREE
|| newrval
== error_mark_node
)
2352 return error_mark_node
;
2354 /* Java constructors compiled by jc1 do not return this. */
2356 newrval
= build (COMPOUND_EXPR
, TREE_TYPE (newrval
),
2359 TREE_HAS_CONSTRUCTOR (rval
) = 1;
2362 rval
= (build_vec_init
2365 build_binary_op (MINUS_EXPR
, nelts
, integer_one_node
),
2369 /* If any part of the object initialization terminates by throwing an
2370 exception and a suitable deallocation function can be found, the
2371 deallocation function is called to free the memory in which the
2372 object was being constructed, after which the exception continues
2373 to propagate in the context of the new-expression. If no
2374 unambiguous matching deallocation function can be found,
2375 propagating the exception does not cause the object's memory to be
2377 if (flag_exceptions
&& alloc_expr
&& ! use_java_new
)
2379 enum tree_code dcode
= has_array
? VEC_DELETE_EXPR
: DELETE_EXPR
;
2380 tree cleanup
, fn
= NULL_TREE
;
2381 int flags
= LOOKUP_NORMAL
| (use_global_new
* LOOKUP_GLOBAL
);
2383 /* The Standard is unclear here, but the right thing to do
2384 is to use the same method for finding deallocation
2385 functions that we use for finding allocation functions. */
2386 flags
|= LOOKUP_SPECULATIVELY
;
2388 /* We expect alloc_expr to look like a TARGET_EXPR around
2389 a NOP_EXPR around the CALL_EXPR we want. */
2390 fn
= TREE_OPERAND (alloc_expr
, 1);
2391 fn
= TREE_OPERAND (fn
, 0);
2393 cleanup
= build_op_delete_call (dcode
, alloc_node
, size
, flags
, fn
);
2395 /* Ack! First we allocate the memory. Then we set our sentry
2396 variable to true, and expand a cleanup that deletes the memory
2397 if sentry is true. Then we run the constructor and store the
2398 returned pointer in buf. Then we clear sentry and return buf. */
2402 tree end
, sentry
, begin
, buf
, t
= TREE_TYPE (rval
);
2404 begin
= get_target_expr (boolean_true_node
);
2405 sentry
= TREE_OPERAND (begin
, 0);
2407 TREE_OPERAND (begin
, 2)
2408 = build (COND_EXPR
, void_type_node
, sentry
,
2409 cleanup
, void_zero_node
);
2411 rval
= get_target_expr (rval
);
2413 end
= build (MODIFY_EXPR
, TREE_TYPE (sentry
),
2414 sentry
, boolean_false_node
);
2416 buf
= TREE_OPERAND (rval
, 0);
2418 rval
= build (COMPOUND_EXPR
, t
, begin
,
2419 build (COMPOUND_EXPR
, t
, rval
,
2420 build (COMPOUND_EXPR
, t
, end
, buf
)));
2424 else if (CP_TYPE_CONST_P (true_type
))
2425 cp_error ("uninitialized const in `new' of `%#T'", true_type
);
2429 if (alloc_expr
&& rval
== alloc_node
)
2431 rval
= TREE_OPERAND (alloc_expr
, 1);
2432 alloc_expr
= NULL_TREE
;
2435 if (check_new
&& alloc_expr
)
2437 /* Did we modify the storage? */
2438 tree ifexp
= build_binary_op (NE_EXPR
, alloc_node
,
2440 rval
= build_conditional_expr (ifexp
, rval
, alloc_node
);
2444 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
), alloc_expr
, rval
);
2446 if (rval
&& TREE_TYPE (rval
) != build_pointer_type (type
))
2448 /* The type of new int [3][3] is not int *, but int [3] * */
2449 rval
= build_c_cast (build_pointer_type (type
), rval
);
2456 build_vec_delete_1 (base
, maxindex
, type
, auto_delete_vec
, use_global_delete
)
2457 tree base
, maxindex
, type
;
2458 tree auto_delete_vec
;
2459 int use_global_delete
;
2462 tree ptype
= build_pointer_type (type
= complete_type (type
));
2463 tree size_exp
= size_in_bytes (type
);
2465 /* Temporary variables used by the loop. */
2466 tree tbase
, tbase_init
;
2468 /* This is the body of the loop that implements the deletion of a
2469 single element, and moves temp variables to next elements. */
2472 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2475 /* This is the thing that governs what to do after the loop has run. */
2476 tree deallocate_expr
= 0;
2478 /* This is the BIND_EXPR which holds the outermost iterator of the
2479 loop. It is convenient to set this variable up and test it before
2480 executing any other code in the loop.
2481 This is also the containing expression returned by this function. */
2482 tree controller
= NULL_TREE
;
2484 if (! IS_AGGR_TYPE (type
) || ! TYPE_NEEDS_DESTRUCTOR (type
))
2486 loop
= integer_zero_node
;
2490 /* The below is short by BI_header_size */
2491 virtual_size
= fold (size_binop (MULT_EXPR
, size_exp
, maxindex
));
2493 tbase
= create_temporary_var (ptype
);
2494 tbase_init
= build_modify_expr (tbase
, NOP_EXPR
,
2495 fold (build (PLUS_EXPR
, ptype
,
2498 DECL_REGISTER (tbase
) = 1;
2499 controller
= build (BIND_EXPR
, void_type_node
, tbase
, NULL_TREE
, NULL_TREE
);
2500 TREE_SIDE_EFFECTS (controller
) = 1;
2504 body
= tree_cons (NULL_TREE
,
2505 build_delete (ptype
, tbase
, integer_two_node
,
2506 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 1),
2509 body
= tree_cons (NULL_TREE
,
2510 build_modify_expr (tbase
, NOP_EXPR
, build (MINUS_EXPR
, ptype
, tbase
, size_exp
)),
2513 body
= tree_cons (NULL_TREE
,
2514 build (EXIT_EXPR
, void_type_node
,
2515 build (EQ_EXPR
, boolean_type_node
, base
, tbase
)),
2518 loop
= build (LOOP_EXPR
, void_type_node
, build_compound_expr (body
));
2520 loop
= tree_cons (NULL_TREE
, tbase_init
,
2521 tree_cons (NULL_TREE
, loop
, NULL_TREE
));
2522 loop
= build_compound_expr (loop
);
2525 /* If the delete flag is one, or anything else with the low bit set,
2526 delete the storage. */
2527 if (auto_delete_vec
== integer_zero_node
)
2528 deallocate_expr
= integer_zero_node
;
2533 /* The below is short by BI_header_size */
2534 virtual_size
= fold (size_binop (MULT_EXPR
, size_exp
, maxindex
));
2536 if (! TYPE_VEC_NEW_USES_COOKIE (type
))
2541 base_tbd
= cp_convert (ptype
,
2542 build_binary_op (MINUS_EXPR
,
2543 cp_convert (string_type_node
, base
),
2545 /* True size with header. */
2546 virtual_size
= size_binop (PLUS_EXPR
, virtual_size
, BI_header_size
);
2548 deallocate_expr
= build_x_delete (base_tbd
,
2549 2 | use_global_delete
,
2551 deallocate_expr
= fold (build (COND_EXPR
, void_type_node
,
2552 fold (build (BIT_AND_EXPR
,
2556 deallocate_expr
, integer_zero_node
));
2559 if (loop
&& deallocate_expr
!= integer_zero_node
)
2561 body
= tree_cons (NULL_TREE
, loop
,
2562 tree_cons (NULL_TREE
, deallocate_expr
, NULL_TREE
));
2563 body
= build_compound_expr (body
);
2568 /* Outermost wrapper: If pointer is null, punt. */
2569 body
= fold (build (COND_EXPR
, void_type_node
,
2570 fold (build (NE_EXPR
, boolean_type_node
, base
,
2571 integer_zero_node
)),
2572 body
, integer_zero_node
));
2573 body
= build1 (NOP_EXPR
, void_type_node
, body
);
2577 TREE_OPERAND (controller
, 1) = body
;
2581 return cp_convert (void_type_node
, body
);
2585 create_temporary_var (type
)
2590 decl
= build_decl (VAR_DECL
, NULL_TREE
, type
);
2591 TREE_USED (decl
) = 1;
2592 DECL_ARTIFICIAL (decl
) = 1;
2593 DECL_SOURCE_FILE (decl
) = input_filename
;
2594 DECL_SOURCE_LINE (decl
) = lineno
;
2595 DECL_IGNORED_P (decl
) = 1;
2596 DECL_CONTEXT (decl
) = current_function_decl
;
2601 /* Create a new temporary variable of the indicated TYPE, initialized
2604 It is not entered into current_binding_level, because that breaks
2605 things when it comes time to do final cleanups (which take place
2606 "outside" the binding contour of the function). */
2609 get_temp_regvar (type
, init
)
2614 decl
= create_temporary_var (type
);
2615 if (building_stmt_tree ())
2616 add_decl_stmt (decl
);
2617 if (!building_stmt_tree ())
2618 DECL_RTL (decl
) = assign_temp (type
, 2, 0, 1);
2619 finish_expr_stmt (build_modify_expr (decl
, INIT_EXPR
, init
));
2624 /* `build_vec_init' returns tree structure that performs
2625 initialization of a vector of aggregate types.
2627 DECL is passed only for error reporting, and provides line number
2628 and source file name information.
2629 BASE is the space where the vector will be. For a vector of Ts,
2630 the type of BASE is `T*'.
2631 MAXINDEX is the maximum index of the array (one less than the
2632 number of elements).
2633 INIT is the (possibly NULL) initializer.
2635 FROM_ARRAY is 0 if we should init everything with INIT
2636 (i.e., every element initialized from INIT).
2637 FROM_ARRAY is 1 if we should index into INIT in parallel
2638 with initialization of DECL.
2639 FROM_ARRAY is 2 if we should index into INIT in parallel,
2640 but use assignment instead of initialization. */
2643 build_vec_init (decl
, base
, maxindex
, init
, from_array
)
2644 tree decl
, base
, maxindex
, init
;
2648 tree base2
= NULL_TREE
;
2650 tree itype
= NULL_TREE
;
2652 /* The type of an element in the array. */
2654 /* The type of a pointer to an element in the array. */
2659 tree try_block
= NULL_TREE
;
2660 tree try_body
= NULL_TREE
;
2661 int num_initialized_elts
= 0;
2663 maxindex
= cp_convert (ptrdiff_type_node
, maxindex
);
2664 if (maxindex
== error_mark_node
)
2665 return error_mark_node
;
2667 type
= TREE_TYPE (TREE_TYPE (base
));
2668 ptype
= build_pointer_type (type
);
2669 size
= size_in_bytes (type
);
2671 /* The code we are generating looks like:
2675 ptrdiff_t iterator = maxindex;
2677 ... initializations from CONSTRUCTOR ...
2678 if (iterator != -1) {
2680 ... initialize *base ...
2682 } while (--iterator != -1);
2685 ... destroy elements that were constructed ...
2688 We can omit the try and catch blocks if we know that the
2689 initialization will never throw an exception, or if the array
2690 elements do not have destructors. If we have a CONSTRUCTOR to
2691 give us initialization information, we emit code to initialize
2692 each of the elements before the loop in the try block, and then
2693 iterate over fewer elements. We can omit the loop completely if
2694 the elements of the array do not have constructors.
2696 We actually wrap the entire body of the above in a STMT_EXPR, for
2699 When copying from array to another, when the array elements have
2700 only trivial copy constructors, we should use __builtin_memcpy
2701 rather than generating a loop. That way, we could take advantage
2702 of whatever cleverness the back-end has for dealing with copies
2703 of blocks of memory. */
2705 begin_init_stmts (&stmt_expr
, &compound_stmt
);
2706 destroy_temps
= stmts_are_full_exprs_p
;
2707 stmts_are_full_exprs_p
= 0;
2708 rval
= get_temp_regvar (ptype
,
2709 cp_convert (ptype
, default_conversion (base
)));
2710 base
= get_temp_regvar (ptype
, rval
);
2711 iterator
= get_temp_regvar (ptrdiff_type_node
, maxindex
);
2713 /* Protect the entire array initialization so that we can destroy
2714 the partially constructed array if an exception is thrown. */
2715 if (flag_exceptions
&& TYPE_NEEDS_DESTRUCTOR (type
))
2717 try_block
= begin_try_block ();
2718 try_body
= begin_compound_stmt (/*has_no_scope=*/1);
2721 if (init
!= NULL_TREE
&& TREE_CODE (init
) == CONSTRUCTOR
2722 && (!decl
|| same_type_p (TREE_TYPE (init
), TREE_TYPE (decl
))))
2724 /* Do non-default initialization resulting from brace-enclosed
2730 for (elts
= CONSTRUCTOR_ELTS (init
); elts
; elts
= TREE_CHAIN (elts
))
2732 tree elt
= TREE_VALUE (elts
);
2733 tree baseref
= build1 (INDIRECT_REF
, type
, base
);
2735 num_initialized_elts
++;
2737 if (IS_AGGR_TYPE (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
2738 finish_expr_stmt (build_aggr_init (baseref
, elt
, 0));
2740 finish_expr_stmt (build_modify_expr (baseref
, NOP_EXPR
,
2743 finish_expr_stmt (build_modify_expr
2746 build (PLUS_EXPR
, build_pointer_type (type
),
2748 finish_expr_stmt (build_modify_expr
2751 build (MINUS_EXPR
, ptrdiff_type_node
,
2752 iterator
, integer_one_node
)));
2755 /* Clear out INIT so that we don't get confused below. */
2758 else if (from_array
)
2760 /* If initializing one array from another, initialize element by
2761 element. We rely upon the below calls the do argument
2763 if (decl
== NULL_TREE
)
2765 sorry ("initialization of array from dissimilar array type");
2766 return error_mark_node
;
2770 base2
= default_conversion (init
);
2771 itype
= TREE_TYPE (base2
);
2772 base2
= get_temp_regvar (itype
, base2
);
2773 itype
= TREE_TYPE (itype
);
2775 else if (TYPE_LANG_SPECIFIC (type
)
2776 && TYPE_NEEDS_CONSTRUCTING (type
)
2777 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
2779 error ("initializer ends prematurely");
2780 return error_mark_node
;
2784 /* Now, default-initialize any remaining elements. We don't need to
2785 do that if a) the type does not need constructing, or b) we've
2786 already initialized all the elements.
2788 We do need to keep going if we're copying an array. */
2791 || (TYPE_NEEDS_CONSTRUCTING (type
)
2792 && !(TREE_CODE (maxindex
) == INTEGER_CST
2793 && num_initialized_elts
== TREE_INT_CST_LOW (maxindex
) + 1)))
2795 /* If the ITERATOR is equal to -1, then we don't have to loop;
2796 we've already initialized all the elements. */
2802 if_stmt
= begin_if_stmt ();
2803 finish_if_stmt_cond (build (NE_EXPR
, boolean_type_node
,
2804 iterator
, minus_one_node
),
2807 /* Otherwise, loop through the elements. */
2808 do_stmt
= begin_do_stmt ();
2809 do_body
= begin_compound_stmt (/*has_no_scope=*/1);
2811 /* When we're not building a statement-tree, things are a little
2812 complicated. If, when we recursively call build_aggr_init,
2813 an expression containing a TARGET_EXPR is expanded, then it
2814 may get a cleanup. Then, the result of that expression is
2815 passed to finish_expr_stmt, which will call
2816 expand_start_target_temps/expand_end_target_temps. However,
2817 the latter call will not cause the cleanup to run because
2818 that block will still be on the block stack. So, we call
2819 expand_start_target_temps here manually; the corresponding
2820 call to expand_end_target_temps below will cause the cleanup
2822 if (!building_stmt_tree ())
2823 expand_start_target_temps ();
2827 tree to
= build1 (INDIRECT_REF
, type
, base
);
2831 from
= build1 (INDIRECT_REF
, itype
, base2
);
2835 if (from_array
== 2)
2836 elt_init
= build_modify_expr (to
, NOP_EXPR
, from
);
2837 else if (TYPE_NEEDS_CONSTRUCTING (type
))
2838 elt_init
= build_aggr_init (to
, from
, 0);
2840 elt_init
= build_modify_expr (to
, NOP_EXPR
, from
);
2842 my_friendly_abort (57);
2844 else if (TREE_CODE (type
) == ARRAY_TYPE
)
2847 sorry ("cannot initialize multi-dimensional array with initializer");
2848 elt_init
= (build_vec_init
2851 build_pointer_type (TREE_TYPE (type
)),
2853 array_type_nelts (type
), 0, 0));
2856 elt_init
= build_aggr_init (build1 (INDIRECT_REF
, type
, base
),
2859 /* The initialization of each array element is a
2861 if (!building_stmt_tree ())
2863 finish_expr_stmt (elt_init
);
2864 expand_end_target_temps ();
2868 stmts_are_full_exprs_p
= 1;
2869 finish_expr_stmt (elt_init
);
2870 stmts_are_full_exprs_p
= 0;
2873 finish_expr_stmt (build_modify_expr
2876 build (PLUS_EXPR
, build_pointer_type (type
),
2879 finish_expr_stmt (build_modify_expr
2882 build (PLUS_EXPR
, build_pointer_type (type
),
2885 finish_compound_stmt (/*has_no_scope=*/1, do_body
);
2886 finish_do_body (do_stmt
);
2887 finish_do_stmt (build (NE_EXPR
, boolean_type_node
,
2888 build (PREDECREMENT_EXPR
,
2895 finish_then_clause (if_stmt
);
2899 /* Make sure to cleanup any partially constructed elements. */
2900 if (flag_exceptions
&& TYPE_NEEDS_DESTRUCTOR (type
))
2904 finish_compound_stmt (/*has_no_scope=*/1, try_body
);
2905 finish_cleanup_try_block (try_block
);
2906 e
= build_vec_delete_1 (rval
,
2907 build_binary_op (MINUS_EXPR
, maxindex
,
2910 /*auto_delete_vec=*/integer_zero_node
,
2911 /*use_global_delete=*/0);
2912 finish_cleanup (e
, try_block
);
2915 /* The value of the array initialization is the address of the
2916 first element in the array. */
2917 finish_expr_stmt (rval
);
2919 stmt_expr
= finish_init_stmts (stmt_expr
, compound_stmt
);
2920 stmts_are_full_exprs_p
= destroy_temps
;
2924 /* Free up storage of type TYPE, at address ADDR.
2926 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2929 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2930 used as the second argument to operator delete. It can include
2931 things like padding and magic size cookies. It has virtual in it,
2932 because if you have a base pointer and you delete through a virtual
2933 destructor, it should be the size of the dynamic object, not the
2934 static object, see Free Store 12.5 ISO C++.
2936 This does not call any destructors. */
2939 build_x_delete (addr
, which_delete
, virtual_size
)
2944 int use_global_delete
= which_delete
& 1;
2945 int use_vec_delete
= !!(which_delete
& 2);
2946 enum tree_code code
= use_vec_delete
? VEC_DELETE_EXPR
: DELETE_EXPR
;
2947 int flags
= LOOKUP_NORMAL
| (use_global_delete
* LOOKUP_GLOBAL
);
2949 return build_op_delete_call (code
, addr
, virtual_size
, flags
, NULL_TREE
);
2952 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
2953 ADDR is an expression which yields the store to be destroyed.
2954 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
2955 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
2956 virtual baseclasses.
2957 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
2959 FLAGS is the logical disjunction of zero or more LOOKUP_
2960 flags. See cp-tree.h for more info.
2962 This function does not delete an object's virtual base classes. */
2965 build_delete (type
, addr
, auto_delete
, flags
, use_global_delete
)
2969 int use_global_delete
;
2975 if (addr
== error_mark_node
)
2976 return error_mark_node
;
2978 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
2979 set to `error_mark_node' before it gets properly cleaned up. */
2980 if (type
== error_mark_node
)
2981 return error_mark_node
;
2983 type
= TYPE_MAIN_VARIANT (type
);
2985 if (TREE_CODE (type
) == POINTER_TYPE
)
2987 type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
2988 if (type
!= void_type_node
&& !complete_type_or_else (type
, addr
))
2989 return error_mark_node
;
2990 if (TREE_CODE (type
) == ARRAY_TYPE
)
2992 if (! IS_AGGR_TYPE (type
))
2994 /* Call the builtin operator delete. */
2995 return build_builtin_delete_call (addr
);
2997 if (TREE_SIDE_EFFECTS (addr
))
2998 addr
= save_expr (addr
);
3000 /* throw away const and volatile on target type of addr */
3001 addr
= convert_force (build_pointer_type (type
), addr
, 0);
3002 ref
= build_indirect_ref (addr
, NULL_PTR
);
3004 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3007 if (TREE_SIDE_EFFECTS (addr
))
3008 addr
= save_expr (addr
);
3009 if (TYPE_DOMAIN (type
) == NULL_TREE
)
3011 error ("unknown array size in delete");
3012 return error_mark_node
;
3014 return build_vec_delete (addr
, array_type_nelts (type
),
3015 auto_delete
, use_global_delete
);
3019 /* Don't check PROTECT here; leave that decision to the
3020 destructor. If the destructor is accessible, call it,
3021 else report error. */
3022 addr
= build_unary_op (ADDR_EXPR
, addr
, 0);
3023 if (TREE_SIDE_EFFECTS (addr
))
3024 addr
= save_expr (addr
);
3026 if (TREE_CONSTANT (addr
))
3027 addr
= convert_pointer_to (type
, addr
);
3029 addr
= convert_force (build_pointer_type (type
), addr
, 0);
3031 ref
= build_indirect_ref (addr
, NULL_PTR
);
3034 my_friendly_assert (IS_AGGR_TYPE (type
), 220);
3036 if (! TYPE_NEEDS_DESTRUCTOR (type
))
3038 if (auto_delete
== integer_zero_node
)
3039 return void_zero_node
;
3041 return build_op_delete_call
3042 (DELETE_EXPR
, addr
, c_sizeof_nowarn (type
),
3043 LOOKUP_NORMAL
| (use_global_delete
* LOOKUP_GLOBAL
),
3047 /* Below, we will reverse the order in which these calls are made.
3048 If we have a destructor, then that destructor will take care
3049 of the base classes; otherwise, we must do that here. */
3050 if (TYPE_HAS_DESTRUCTOR (type
))
3052 tree passed_auto_delete
;
3053 tree do_delete
= NULL_TREE
;
3056 if (use_global_delete
)
3058 tree cond
= fold (build (BIT_AND_EXPR
, integer_type_node
,
3059 auto_delete
, integer_one_node
));
3060 tree call
= build_builtin_delete_call (addr
);
3062 cond
= fold (build (COND_EXPR
, void_type_node
, cond
,
3063 call
, void_zero_node
));
3064 if (cond
!= void_zero_node
)
3067 passed_auto_delete
= fold (build (BIT_AND_EXPR
, integer_type_node
,
3068 auto_delete
, integer_two_node
));
3071 passed_auto_delete
= auto_delete
;
3073 expr
= build_method_call
3074 (ref
, dtor_identifier
, build_tree_list (NULL_TREE
, passed_auto_delete
),
3078 expr
= build (COMPOUND_EXPR
, void_type_node
, expr
, do_delete
);
3080 if (flags
& LOOKUP_DESTRUCTOR
)
3081 /* Explicit destructor call; don't check for null pointer. */
3082 ifexp
= integer_one_node
;
3084 /* Handle deleting a null pointer. */
3085 ifexp
= fold (build_binary_op (NE_EXPR
, addr
, integer_zero_node
));
3087 if (ifexp
!= integer_one_node
)
3088 expr
= build (COND_EXPR
, void_type_node
,
3089 ifexp
, expr
, void_zero_node
);
3095 /* We only get here from finish_function for a destructor. */
3096 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (type
));
3097 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3098 tree base_binfo
= n_baseclasses
> 0 ? TREE_VEC_ELT (binfos
, 0) : NULL_TREE
;
3099 tree exprstmt
= NULL_TREE
;
3100 tree parent_auto_delete
= auto_delete
;
3103 /* Set this again before we call anything, as we might get called
3105 TYPE_HAS_DESTRUCTOR (type
) = 1;
3107 /* If we have member delete or vbases, we call delete in
3109 if (auto_delete
== integer_zero_node
)
3111 else if (base_binfo
== NULL_TREE
3112 || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo
)))
3114 cond
= build (COND_EXPR
, void_type_node
,
3115 build (BIT_AND_EXPR
, integer_type_node
, auto_delete
, integer_one_node
),
3116 build_builtin_delete_call (addr
),
3123 exprstmt
= build_tree_list (NULL_TREE
, cond
);
3126 && ! TREE_VIA_VIRTUAL (base_binfo
)
3127 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo
)))
3129 tree this_auto_delete
;
3131 if (BINFO_OFFSET_ZEROP (base_binfo
))
3132 this_auto_delete
= parent_auto_delete
;
3134 this_auto_delete
= integer_zero_node
;
3136 expr
= build_scoped_method_call
3137 (ref
, base_binfo
, dtor_identifier
,
3138 build_tree_list (NULL_TREE
, this_auto_delete
));
3139 exprstmt
= tree_cons (NULL_TREE
, expr
, exprstmt
);
3142 /* Take care of the remaining baseclasses. */
3143 for (i
= 1; i
< n_baseclasses
; i
++)
3145 base_binfo
= TREE_VEC_ELT (binfos
, i
);
3146 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo
))
3147 || TREE_VIA_VIRTUAL (base_binfo
))
3150 expr
= build_scoped_method_call
3151 (ref
, base_binfo
, dtor_identifier
,
3152 build_tree_list (NULL_TREE
, integer_zero_node
));
3154 exprstmt
= tree_cons (NULL_TREE
, expr
, exprstmt
);
3157 for (member
= TYPE_FIELDS (type
); member
; member
= TREE_CHAIN (member
))
3159 if (TREE_CODE (member
) != FIELD_DECL
)
3161 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member
)))
3163 tree this_member
= build_component_ref (ref
, DECL_NAME (member
), NULL_TREE
, 0);
3164 tree this_type
= TREE_TYPE (member
);
3165 expr
= build_delete (this_type
, this_member
, integer_two_node
, flags
, 0);
3166 exprstmt
= tree_cons (NULL_TREE
, expr
, exprstmt
);
3171 return build_compound_expr (exprstmt
);
3172 /* Virtual base classes make this function do nothing. */
3173 return void_zero_node
;
3177 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3180 build_vbase_delete (type
, decl
)
3183 tree vbases
= CLASSTYPE_VBASECLASSES (type
);
3184 tree result
= NULL_TREE
;
3185 tree addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
3187 my_friendly_assert (addr
!= error_mark_node
, 222);
3191 tree this_addr
= convert_force (build_pointer_type (BINFO_TYPE (vbases
)),
3193 result
= tree_cons (NULL_TREE
,
3194 build_delete (TREE_TYPE (this_addr
), this_addr
,
3196 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0),
3198 vbases
= TREE_CHAIN (vbases
);
3200 return build_compound_expr (nreverse (result
));
3203 /* Build a C++ vector delete expression.
3204 MAXINDEX is the number of elements to be deleted.
3205 ELT_SIZE is the nominal size of each element in the vector.
3206 BASE is the expression that should yield the store to be deleted.
3207 This function expands (or synthesizes) these calls itself.
3208 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3210 This also calls delete for virtual baseclasses of elements of the vector.
3212 Update: MAXINDEX is no longer needed. The size can be extracted from the
3213 start of the vector for pointers, and from the type for arrays. We still
3214 use MAXINDEX for arrays because it happens to already have one of the
3215 values we'd have to extract. (We could use MAXINDEX with pointers to
3216 confirm the size, and trap if the numbers differ; not clear that it'd
3217 be worth bothering.) */
3220 build_vec_delete (base
, maxindex
, auto_delete_vec
, use_global_delete
)
3221 tree base
, maxindex
;
3222 tree auto_delete_vec
;
3223 int use_global_delete
;
3227 if (TREE_CODE (base
) == OFFSET_REF
)
3228 base
= resolve_offset_ref (base
);
3230 type
= TREE_TYPE (base
);
3232 base
= stabilize_reference (base
);
3234 /* Since we can use base many times, save_expr it. */
3235 if (TREE_SIDE_EFFECTS (base
))
3236 base
= save_expr (base
);
3238 if (TREE_CODE (type
) == POINTER_TYPE
)
3240 /* Step back one from start of vector, and read dimension. */
3241 tree cookie_addr
= build (MINUS_EXPR
, build_pointer_type (BI_header_type
),
3242 base
, BI_header_size
);
3243 tree cookie
= build_indirect_ref (cookie_addr
, NULL_PTR
);
3244 maxindex
= build_component_ref (cookie
, nelts_identifier
, NULL_TREE
, 0);
3246 type
= TREE_TYPE (type
);
3247 while (TREE_CODE (type
) == ARRAY_TYPE
);
3249 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3251 /* get the total number of things in the array, maxindex is a bad name */
3252 maxindex
= array_type_nelts_total (type
);
3253 while (TREE_CODE (type
) == ARRAY_TYPE
)
3254 type
= TREE_TYPE (type
);
3255 base
= build_unary_op (ADDR_EXPR
, base
, 1);
3259 if (base
!= error_mark_node
)
3260 error ("type to vector delete is neither pointer or array type");
3261 return error_mark_node
;
3264 return build_vec_delete_1 (base
, maxindex
, type
, auto_delete_vec
,