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
));
55 /* Set up local variable for this file. MUST BE CALLED AFTER
56 INIT_DECL_PROCESSING. */
58 static tree BI_header_type
, BI_header_size
;
60 void init_init_processing ()
64 minus_one_node
= build_int_2 (-1, -1);
66 /* Define the structure that holds header information for
67 arrays allocated via operator new. */
68 BI_header_type
= make_aggr_type (RECORD_TYPE
);
69 nelts_identifier
= get_identifier ("nelts");
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 /* Called from initialize_vtbl_ptrs via dfs_walk. */
86 dfs_initialize_vtbl_ptrs (binfo
, data
)
90 if (!BINFO_PRIMARY_MARKED_P (binfo
)
91 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
93 tree base_ptr
= TREE_VALUE ((tree
) data
);
95 if (TREE_VIA_VIRTUAL (binfo
))
96 base_ptr
= convert_pointer_to_vbase (BINFO_TYPE (binfo
),
100 = build_vbase_path (PLUS_EXPR
,
101 build_pointer_type (BINFO_TYPE (binfo
)),
106 expand_virtual_init (binfo
, base_ptr
);
109 SET_BINFO_MARKED (binfo
);
114 /* Initialize all the vtable pointers for the hierarchy dominated by
118 initialize_vtbl_ptrs (type
, addr
)
122 tree list
= build_tree_list (type
, addr
);
124 /* Walk through the hierarchy, initializing the vptr in each base
125 class. We do these in pre-order because under the new ABI we
126 can't find the virtual bases for a class until we've initialized
127 the vtbl for that class. */
128 dfs_walk_real (TYPE_BINFO (type
), dfs_initialize_vtbl_ptrs
,
129 NULL
, dfs_unmarked_real_bases_queue_p
, list
);
130 dfs_walk (TYPE_BINFO (type
), dfs_unmark
,
131 dfs_marked_real_bases_queue_p
, type
);
132 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
133 expand_indirect_vtbls_init (TYPE_BINFO (type
), addr
);
138 /* Subroutine of emit_base_init. */
141 perform_member_init (member
, name
, init
, explicit)
142 tree member
, name
, init
;
146 tree type
= TREE_TYPE (member
);
148 decl
= build_component_ref (current_class_ref
, name
, NULL_TREE
, explicit);
150 if (decl
== error_mark_node
)
153 /* Deal with this here, as we will get confused if we try to call the
154 assignment op for an anonymous union. This can happen in a
155 synthesized copy constructor. */
156 if (ANON_AGGR_TYPE_P (type
))
158 init
= build (INIT_EXPR
, type
, decl
, TREE_VALUE (init
));
159 finish_expr_stmt (init
);
161 else if (TYPE_NEEDS_CONSTRUCTING (type
)
162 || (init
&& TYPE_HAS_CONSTRUCTOR (type
)))
164 /* Since `init' is already a TREE_LIST on the current_member_init_list,
165 only build it into one if we aren't already a list. */
166 if (init
!= NULL_TREE
&& TREE_CODE (init
) != TREE_LIST
)
167 init
= build_tree_list (NULL_TREE
, init
);
170 && TREE_CODE (type
) == ARRAY_TYPE
172 && TREE_CHAIN (init
) == NULL_TREE
173 && TREE_CODE (TREE_TYPE (TREE_VALUE (init
))) == ARRAY_TYPE
)
175 /* Initialization of one array from another. */
177 (build_vec_init (TREE_OPERAND (decl
, 1), decl
,
178 array_type_nelts (type
), TREE_VALUE (init
), 1));
181 finish_expr_stmt (build_aggr_init (decl
, init
, 0));
185 if (init
== NULL_TREE
)
189 /* default-initialization. */
190 if (AGGREGATE_TYPE_P (type
))
192 /* This is a default initialization of an aggregate,
193 but not one of non-POD class type. We cleverly
194 notice that the initialization rules in such a
195 case are the same as for initialization with an
196 empty brace-initialization list. We don't want
197 to call build_modify_expr as that will go looking
198 for constructors and such. */
199 tree e
= build (CONSTRUCTOR
, type
, NULL_TREE
, NULL_TREE
);
200 TREE_SIDE_EFFECTS (e
) = 1;
201 finish_expr_stmt (build (INIT_EXPR
, type
, decl
, e
));
203 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
204 cp_error ("default-initialization of `%#D', which has reference type",
207 init
= integer_zero_node
;
209 /* member traversal: note it leaves init NULL */
210 else if (TREE_CODE (TREE_TYPE (member
)) == REFERENCE_TYPE
)
211 cp_pedwarn ("uninitialized reference member `%D'", member
);
213 else if (TREE_CODE (init
) == TREE_LIST
)
215 /* There was an explicit member initialization. Do some
216 work in that case. */
217 if (TREE_CHAIN (init
))
219 warning ("initializer list treated as compound expression");
220 init
= build_compound_expr (init
);
223 init
= TREE_VALUE (init
);
227 finish_expr_stmt (build_modify_expr (decl
, INIT_EXPR
, init
));
230 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
234 expr
= build_component_ref (current_class_ref
, name
, NULL_TREE
,
236 expr
= build_delete (type
, expr
, integer_zero_node
,
237 LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0);
239 if (expr
!= error_mark_node
)
240 finish_subobject (expr
);
244 extern int warn_reorder
;
246 /* Subroutine of emit_member_init. */
252 tree x
, member
, name
, field
;
253 tree init_list
= NULL_TREE
;
255 tree last_field
= NULL_TREE
;
257 for (member
= TYPE_FIELDS (t
); member
; member
= TREE_CHAIN (member
))
261 /* member could be, for example, a CONST_DECL for an enumerated
262 tag; we don't want to try to initialize that, since it already
264 if (TREE_CODE (member
) != FIELD_DECL
|| !DECL_NAME (member
))
267 for (x
= current_member_init_list
, pos
= 0; x
; x
= TREE_CHAIN (x
), ++pos
)
269 /* If we cleared this out, then pay no attention to it. */
270 if (TREE_PURPOSE (x
) == NULL_TREE
)
272 name
= TREE_PURPOSE (x
);
274 if (TREE_CODE (name
) == IDENTIFIER_NODE
)
275 field
= IDENTIFIER_CLASS_VALUE (name
);
278 my_friendly_assert (TREE_CODE (name
) == FIELD_DECL
, 348);
282 /* If one member shadows another, get the outermost one. */
283 if (TREE_CODE (field
) == TREE_LIST
)
284 field
= TREE_VALUE (field
);
292 cp_warning_at ("member initializers for `%#D'", last_field
);
293 cp_warning_at (" and `%#D'", field
);
294 warning (" will be re-ordered to match declaration order");
300 /* Make sure we won't try to work on this init again. */
301 TREE_PURPOSE (x
) = NULL_TREE
;
302 x
= build_tree_list (name
, TREE_VALUE (x
));
307 /* If we didn't find MEMBER in the list, create a dummy entry
308 so the two lists (INIT_LIST and the list of members) will be
310 x
= build_tree_list (NULL_TREE
, NULL_TREE
);
312 init_list
= chainon (init_list
, x
);
315 /* Initializers for base members go at the end. */
316 for (x
= current_member_init_list
; x
; x
= TREE_CHAIN (x
))
318 name
= TREE_PURPOSE (x
);
321 if (purpose_member (name
, init_list
))
323 cp_error ("multiple initializations given for member `%D'",
324 IDENTIFIER_CLASS_VALUE (name
));
328 init_list
= chainon (init_list
,
329 build_tree_list (name
, TREE_VALUE (x
)));
330 TREE_PURPOSE (x
) = NULL_TREE
;
338 sort_base_init (t
, rbase_ptr
, vbase_ptr
)
339 tree t
, *rbase_ptr
, *vbase_ptr
;
341 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
342 int n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
348 /* For warn_reorder. */
350 tree last_base
= NULL_TREE
;
352 tree rbases
= NULL_TREE
;
353 tree vbases
= NULL_TREE
;
355 /* First walk through and splice out vbase and invalid initializers.
356 Also replace names with binfos. */
358 last
= tree_cons (NULL_TREE
, NULL_TREE
, current_base_init_list
);
359 for (x
= TREE_CHAIN (last
); x
; x
= TREE_CHAIN (x
))
361 tree basetype
= TREE_PURPOSE (x
);
362 tree binfo
= NULL_TREE
;
364 if (basetype
== NULL_TREE
)
366 /* Initializer for single base class. Must not
367 use multiple inheritance or this is ambiguous. */
368 switch (n_baseclasses
)
371 cp_error ("`%T' does not have a base class to initialize",
377 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
381 binfo
= TREE_VEC_ELT (binfos
, 0);
383 else if (is_aggr_type (basetype
, 1))
385 binfo
= binfo_or_else (basetype
, t
);
386 if (binfo
== NULL_TREE
)
389 /* Virtual base classes are special cases. Their initializers
390 are recorded with this constructor, and they are used when
391 this constructor is the top-level constructor called. */
392 if (TREE_VIA_VIRTUAL (binfo
))
394 tree v
= BINFO_FOR_VBASE (BINFO_TYPE (binfo
), t
);
395 vbases
= tree_cons (v
, TREE_VALUE (x
), vbases
);
400 /* Otherwise, if it is not an immediate base class, complain. */
401 for (i
= n_baseclasses
-1; i
>= 0; i
--)
402 if (BINFO_TYPE (binfo
) == BINFO_TYPE (TREE_VEC_ELT (binfos
, i
)))
406 cp_error ("`%T' is not an immediate base class of `%T'",
407 basetype
, current_class_type
);
413 my_friendly_abort (365);
415 TREE_PURPOSE (x
) = binfo
;
416 TREE_CHAIN (last
) = x
;
419 TREE_CHAIN (last
) = NULL_TREE
;
421 /* Now walk through our regular bases and make sure they're initialized. */
423 for (i
= 0; i
< n_baseclasses
; ++i
)
425 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
428 if (TREE_VIA_VIRTUAL (base_binfo
))
431 for (x
= current_base_init_list
, pos
= 0; x
; x
= TREE_CHAIN (x
), ++pos
)
433 tree binfo
= TREE_PURPOSE (x
);
435 if (binfo
== NULL_TREE
)
438 if (binfo
== base_binfo
)
444 cp_warning_at ("base initializers for `%#T'", last_base
);
445 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo
));
446 warning (" will be re-ordered to match inheritance order");
449 last_base
= BINFO_TYPE (binfo
);
452 /* Make sure we won't try to work on this init again. */
453 TREE_PURPOSE (x
) = NULL_TREE
;
454 x
= build_tree_list (binfo
, TREE_VALUE (x
));
459 /* If we didn't find BASE_BINFO in the list, create a dummy entry
460 so the two lists (RBASES and the list of bases) will be
462 x
= build_tree_list (NULL_TREE
, NULL_TREE
);
464 rbases
= chainon (rbases
, x
);
471 /* Perform whatever initializations have yet to be done on the base
472 class of the class variable. These actions are in the global
473 variable CURRENT_BASE_INIT_LIST. Such an action could be
474 NULL_TREE, meaning that the user has explicitly called the base
475 class constructor with no arguments.
477 If there is a need for a call to a constructor, we must surround
478 that call with a pushlevel/poplevel pair, since we are technically
479 at the PARM level of scope.
481 Argument IMMEDIATELY, if zero, forces a new sequence to be
482 generated to contain these new insns, so it can be emitted later.
483 This sequence is saved in the global variable BASE_INIT_EXPR.
484 Otherwise, the insns are emitted into the current sequence.
486 Note that emit_base_init does *not* initialize virtual base
487 classes. That is done specially, elsewhere. */
495 tree rbase_init_list
, vbase_init_list
;
496 tree t_binfo
= TYPE_BINFO (t
);
497 tree binfos
= BINFO_BASETYPES (t_binfo
);
498 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
502 mem_init_list
= sort_member_init (t
);
503 current_member_init_list
= NULL_TREE
;
505 sort_base_init (t
, &rbase_init_list
, &vbase_init_list
);
506 current_base_init_list
= NULL_TREE
;
508 begin_init_stmts (&stmt_expr
, &compound_stmt
);
510 /* First, initialize the virtual base classes, if we are
511 constructing the most-derived object. */
512 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
514 tree first_arg
= TREE_CHAIN (DECL_ARGUMENTS (current_function_decl
));
515 construct_virtual_bases (t
, current_class_ref
, current_class_ptr
,
516 vbase_init_list
, first_arg
);
519 /* Now, perform initialization of non-virtual base classes. */
520 for (i
= 0; i
< n_baseclasses
; i
++)
522 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
523 tree init
= void_list_node
;
525 if (TREE_VIA_VIRTUAL (base_binfo
))
528 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo
) == t_binfo
,
531 if (TREE_PURPOSE (rbase_init_list
))
532 init
= TREE_VALUE (rbase_init_list
);
533 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo
)))
536 if (extra_warnings
&& copy_args_p (current_function_decl
))
537 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
538 BINFO_TYPE (base_binfo
));
541 if (init
!= void_list_node
)
543 member
= convert_pointer_to_real (base_binfo
, current_class_ptr
);
544 expand_aggr_init_1 (base_binfo
, NULL_TREE
,
545 build_indirect_ref (member
, NULL_PTR
), init
,
549 expand_cleanup_for_base (base_binfo
, NULL_TREE
);
550 rbase_init_list
= TREE_CHAIN (rbase_init_list
);
553 /* Initialize the vtable pointers for the class. */
554 initialize_vtbl_ptrs (t
, current_class_ptr
);
556 for (member
= TYPE_FIELDS (t
); member
; member
= TREE_CHAIN (member
))
561 /* member could be, for example, a CONST_DECL for an enumerated
562 tag; we don't want to try to initialize that, since it already
564 if (TREE_CODE (member
) != FIELD_DECL
|| !DECL_NAME (member
))
567 /* See if we had a user-specified member initialization. */
568 if (TREE_PURPOSE (mem_init_list
))
570 name
= TREE_PURPOSE (mem_init_list
);
571 init
= TREE_VALUE (mem_init_list
);
574 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
575 || TREE_CODE (name
) == FIELD_DECL
, 349);
579 name
= DECL_NAME (member
);
580 init
= DECL_INITIAL (member
);
584 /* Effective C++ rule 12. */
585 if (warn_ecpp
&& init
== NULL_TREE
586 && !DECL_ARTIFICIAL (member
)
587 && TREE_CODE (TREE_TYPE (member
)) != ARRAY_TYPE
)
588 cp_warning ("`%D' should be initialized in the member initialization list", member
);
591 perform_member_init (member
, name
, init
, from_init_list
);
592 mem_init_list
= TREE_CHAIN (mem_init_list
);
595 /* Now initialize any members from our bases. */
596 while (mem_init_list
)
598 tree name
, init
, field
;
600 if (TREE_PURPOSE (mem_init_list
))
602 name
= TREE_PURPOSE (mem_init_list
);
603 init
= TREE_VALUE (mem_init_list
);
605 if (TREE_CODE (name
) == IDENTIFIER_NODE
)
606 field
= IDENTIFIER_CLASS_VALUE (name
);
610 /* If one member shadows another, get the outermost one. */
611 if (TREE_CODE (field
) == TREE_LIST
)
613 field
= TREE_VALUE (field
);
614 if (decl_type_context (field
) != current_class_type
)
615 cp_error ("field `%D' not in immediate context", field
);
618 perform_member_init (field
, name
, init
, 1);
620 mem_init_list
= TREE_CHAIN (mem_init_list
);
623 /* All the implicit try blocks we built up will be zapped
624 when we come to a real binding contour boundary. */
625 return finish_init_stmts (stmt_expr
, compound_stmt
);
628 /* Check that all fields are properly initialized after
629 an assignment to `this'. Called only when such an assignment
630 is actually noted. */
637 for (member
= TYPE_FIELDS (t
); member
; member
= TREE_CHAIN (member
))
638 if (DECL_NAME (member
) && TREE_USED (member
))
639 cp_error ("field `%D' used before initialized (after assignment to `this')",
643 /* This code sets up the virtual function tables appropriate for
644 the pointer DECL. It is a one-ply initialization.
646 BINFO is the exact type that DECL is supposed to be. In
647 multiple inheritance, this might mean "C's A" if C : A, B. */
650 expand_virtual_init (binfo
, decl
)
653 tree type
= BINFO_TYPE (binfo
);
655 tree vtype
, vtype_binfo
;
657 /* Compute the location of the vtable. */
658 vtype
= DECL_CONTEXT (TYPE_VFIELD (type
));
659 vtype_binfo
= get_binfo (vtype
, TREE_TYPE (TREE_TYPE (decl
)), 0);
660 vtbl
= BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (TYPE_VFIELD (type
)), binfo
));
662 if (TREE_CODE (vtbl
) == VAR_DECL
)
664 assemble_external (vtbl
);
665 TREE_USED (vtbl
) = 1;
666 vtbl
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (vtbl
)), vtbl
);
669 /* Under the new ABI, secondary vtables are stored with the
670 primary vtable. So, the BINFO_VTABLE may be an expression for
671 computing the secondary vtable, rather than the secondary
673 my_friendly_assert (merge_primary_and_secondary_vtables_p (),
676 /* Under the new ABI, we need to point into the middle of the
678 if (vbase_offsets_in_vtable_p ())
679 vtbl
= build (PLUS_EXPR
, TREE_TYPE (vtbl
), vtbl
,
680 size_extra_vtbl_entries (binfo
));
682 /* Compute the location of the vtpr. */
683 decl
= convert_pointer_to_real (vtype_binfo
, decl
);
684 vtbl_ptr
= build_vfield_ref (build_indirect_ref (decl
, NULL_PTR
), vtype
);
685 if (vtbl_ptr
== error_mark_node
)
688 /* Assign the vtable to the vptr. */
689 vtbl
= convert_force (TREE_TYPE (vtbl_ptr
), vtbl
, 0);
690 finish_expr_stmt (build_modify_expr (vtbl_ptr
, NOP_EXPR
, vtbl
));
693 /* If an exception is thrown in a constructor, those base classes already
694 constructed must be destroyed. This function creates the cleanup
695 for BINFO, which has just been constructed. If FLAG is non-NULL,
696 it is a DECL which is non-zero when this base needs to be
700 expand_cleanup_for_base (binfo
, flag
)
706 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo
)))
709 /* Call the destructor. */
710 expr
= (build_scoped_method_call
711 (current_class_ref
, binfo
, dtor_identifier
,
712 build_tree_list (NULL_TREE
, integer_zero_node
)));
714 expr
= fold (build (COND_EXPR
, void_type_node
,
715 truthvalue_conversion (flag
),
716 expr
, integer_zero_node
));
718 finish_subobject (expr
);
721 /* Subroutine of `expand_aggr_vbase_init'.
722 BINFO is the binfo of the type that is being initialized.
723 INIT_LIST is the list of initializers for the virtual baseclass. */
726 expand_aggr_vbase_init_1 (binfo
, exp
, addr
, init_list
)
727 tree binfo
, exp
, addr
, init_list
;
729 tree init
= purpose_member (binfo
, init_list
);
730 tree ref
= build_indirect_ref (addr
, NULL_PTR
);
733 init
= TREE_VALUE (init
);
734 /* Call constructors, but don't set up vtables. */
735 expand_aggr_init_1 (binfo
, exp
, ref
, init
, LOOKUP_COMPLAIN
);
738 /* Construct the virtual base-classes of THIS_REF (whose address is
739 THIS_PTR). The object has the indicated TYPE. The construction
740 actually takes place only if FLAG is non-zero. INIT_LIST is list
741 of initializations for constructors to perform. */
744 construct_virtual_bases (type
, this_ref
, this_ptr
, init_list
, flag
)
753 /* If there are no virtual baseclasses, we shouldn't even be here. */
754 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type
), 19990621);
756 /* First set the pointers in our object that tell us where to find
757 our virtual baseclasses. */
758 if (!vbase_offsets_in_vtable_p ())
763 if_stmt
= begin_if_stmt ();
764 finish_if_stmt_cond (flag
, if_stmt
);
765 result
= init_vbase_pointers (type
, this_ptr
);
767 finish_expr_stmt (build_compound_expr (result
));
768 finish_then_clause (if_stmt
);
772 /* Now, run through the baseclasses, initializing each. */
773 for (vbases
= CLASSTYPE_VBASECLASSES (type
); vbases
;
774 vbases
= TREE_CHAIN (vbases
))
780 /* If there are virtual base classes with destructors, we need to
781 emit cleanups to destroy them if an exception is thrown during
782 the construction process. These exception regions (i.e., the
783 period during which the cleanups must occur) begin from the time
784 the construction is complete to the end of the function. If we
785 create a conditional block in which to initialize the
786 base-classes, then the cleanup region for the virtual base begins
787 inside a block, and ends outside of that block. This situation
788 confuses the sjlj exception-handling code. Therefore, we do not
789 create a single conditional block, but one for each
790 initialization. (That way the cleanup regions always begin
791 in the outer block.) We trust the back-end to figure out
792 that the FLAG will not change across initializations, and
793 avoid doing multiple tests. */
794 inner_if_stmt
= begin_if_stmt ();
795 finish_if_stmt_cond (flag
, inner_if_stmt
);
796 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/1);
798 /* Compute the location of the virtual base. If we're
799 constructing virtual bases, then we must be the most derived
800 class. Therefore, we don't have to look up the virtual base;
801 we already know where it is. */
802 exp
= build (PLUS_EXPR
,
803 TREE_TYPE (this_ptr
),
805 fold (build1 (NOP_EXPR
, TREE_TYPE (this_ptr
),
806 BINFO_OFFSET (vbases
))));
807 exp
= build1 (NOP_EXPR
,
808 build_pointer_type (BINFO_TYPE (vbases
)),
811 expand_aggr_vbase_init_1 (vbases
, this_ref
, exp
, init_list
);
812 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt
);
813 finish_then_clause (inner_if_stmt
);
816 expand_cleanup_for_base (vbases
, flag
);
820 /* Find the context in which this FIELD can be initialized. */
823 initializing_context (field
)
826 tree t
= DECL_CONTEXT (field
);
828 /* Anonymous union members can be initialized in the first enclosing
829 non-anonymous union context. */
830 while (t
&& ANON_AGGR_TYPE_P (t
))
831 t
= TYPE_CONTEXT (t
);
835 /* Function to give error message if member initialization specification
836 is erroneous. FIELD is the member we decided to initialize.
837 TYPE is the type for which the initialization is being performed.
838 FIELD must be a member of TYPE.
840 MEMBER_NAME is the name of the member. */
843 member_init_ok_or_else (field
, type
, member_name
)
846 const char *member_name
;
848 if (field
== error_mark_node
)
850 if (field
== NULL_TREE
|| initializing_context (field
) != type
)
852 cp_error ("class `%T' does not have any field named `%s'", type
,
856 if (TREE_STATIC (field
))
858 cp_error ("field `%#D' is static; only point of initialization is its declaration",
866 /* If NAME is a viable field name for the aggregate DECL,
867 and PARMS is a viable parameter list, then expand an _EXPR
868 which describes this initialization.
870 Note that we do not need to chase through the class's base classes
871 to look for NAME, because if it's in that list, it will be handled
872 by the constructor for that base class.
874 We do not yet have a fixed-point finder to instantiate types
875 being fed to overloaded constructors. If there is a unique
876 constructor, then argument types can be got from that one.
878 If INIT is non-NULL, then it the initialization should
879 be placed in `current_base_init_list', where it will be processed
880 by `emit_base_init'. */
883 expand_member_init (exp
, name
, init
)
884 tree exp
, name
, init
;
886 tree basetype
= NULL_TREE
, field
;
889 if (exp
== NULL_TREE
)
890 return; /* complain about this later */
892 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
894 if (name
&& TREE_CODE (name
) == TYPE_DECL
)
896 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (name
));
897 name
= DECL_NAME (name
);
900 if (name
== NULL_TREE
&& IS_AGGR_TYPE (type
))
901 switch (CLASSTYPE_N_BASECLASSES (type
))
904 error ("base class initializer specified, but no base class to initialize");
907 basetype
= TYPE_BINFO_BASETYPE (type
, 0);
910 error ("initializer for unnamed base class ambiguous");
911 cp_error ("(type `%T' uses multiple inheritance)", type
);
915 my_friendly_assert (init
!= NULL_TREE
, 0);
917 /* The grammar should not allow fields which have names that are
918 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
919 may assume that this is an attempt to initialize a base class
920 member of the current type. Otherwise, it is an attempt to
921 initialize a member field. */
923 if (init
== void_type_node
)
926 if (name
== NULL_TREE
|| basetype
)
930 if (name
== NULL_TREE
)
934 name
= TYPE_IDENTIFIER (basetype
);
937 error ("no base class to initialize");
942 else if (basetype
!= type
943 && ! current_template_parms
944 && ! vec_binfo_member (basetype
,
945 TYPE_BINFO_BASETYPES (type
))
946 && ! BINFO_FOR_VBASE (basetype
, type
))
948 if (IDENTIFIER_CLASS_VALUE (name
))
950 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
951 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
954 cp_error ("type `%T' is not an immediate basetype for `%T'",
959 if (purpose_member (basetype
, current_base_init_list
))
961 cp_error ("base class `%T' already initialized", basetype
);
965 if (warn_reorder
&& current_member_init_list
)
967 cp_warning ("base initializer for `%T'", basetype
);
968 warning (" will be re-ordered to precede member initializations");
971 base_init
= build_tree_list (basetype
, init
);
972 current_base_init_list
= chainon (current_base_init_list
, base_init
);
979 field
= lookup_field (type
, name
, 1, 0);
981 if (! member_init_ok_or_else (field
, type
, IDENTIFIER_POINTER (name
)))
984 if (purpose_member (name
, current_member_init_list
))
986 cp_error ("field `%D' already initialized", field
);
990 member_init
= build_tree_list (name
, init
);
991 current_member_init_list
= chainon (current_member_init_list
, member_init
);
995 /* We are about to generate some complex initialization code.
996 Conceptually, it is all a single expression. However, we may want
997 to include conditionals, loops, and other such statement-level
998 constructs. Therefore, we build the initialization code inside a
999 statement-expression. This function starts such an expression.
1000 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
1001 pass them back to finish_init_stmts when the expression is
1005 begin_init_stmts (stmt_expr_p
, compound_stmt_p
)
1007 tree
*compound_stmt_p
;
1009 *stmt_expr_p
= begin_stmt_expr ();
1010 *compound_stmt_p
= begin_compound_stmt (/*has_no_scope=*/1);
1013 /* Finish out the statement-expression begun by the previous call to
1014 begin_init_stmts. Returns the statement-expression itself. */
1017 finish_init_stmts (stmt_expr
, compound_stmt
)
1021 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt
);
1022 stmt_expr
= finish_stmt_expr (stmt_expr
);
1024 /* To avoid spurious warnings about unused values, we set
1027 TREE_USED (stmt_expr
) = 1;
1032 /* This is like `expand_member_init', only it stores one aggregate
1035 INIT comes in two flavors: it is either a value which
1036 is to be stored in EXP, or it is a parameter list
1037 to go to a constructor, which will operate on EXP.
1038 If INIT is not a parameter list for a constructor, then set
1039 LOOKUP_ONLYCONVERTING.
1040 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1041 the initializer, if FLAGS is 0, then it is the (init) form.
1042 If `init' is a CONSTRUCTOR, then we emit a warning message,
1043 explaining that such initializations are invalid.
1045 If INIT resolves to a CALL_EXPR which happens to return
1046 something of the type we are looking for, then we know
1047 that we can safely use that call to perform the
1050 The virtual function table pointer cannot be set up here, because
1051 we do not really know its type.
1053 Virtual baseclass pointers are also set up here.
1055 This never calls operator=().
1057 When initializing, nothing is CONST.
1059 A default copy constructor may have to be used to perform the
1062 A constructor or a conversion operator may have to be used to
1063 perform the initialization, but not both, as it would be ambiguous. */
1066 build_aggr_init (exp
, init
, flags
)
1073 tree type
= TREE_TYPE (exp
);
1074 int was_const
= TREE_READONLY (exp
);
1075 int was_volatile
= TREE_THIS_VOLATILE (exp
);
1077 if (init
== error_mark_node
)
1078 return error_mark_node
;
1080 TREE_READONLY (exp
) = 0;
1081 TREE_THIS_VOLATILE (exp
) = 0;
1083 if (init
&& TREE_CODE (init
) != TREE_LIST
)
1084 flags
|= LOOKUP_ONLYCONVERTING
;
1086 if (TREE_CODE (type
) == ARRAY_TYPE
)
1088 /* Must arrange to initialize each element of EXP
1089 from elements of INIT. */
1090 tree itype
= init
? TREE_TYPE (init
) : NULL_TREE
;
1091 if (CP_TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
1093 TREE_TYPE (exp
) = TYPE_MAIN_VARIANT (type
);
1095 TREE_TYPE (init
) = TYPE_MAIN_VARIANT (itype
);
1097 if (init
&& TREE_TYPE (init
) == NULL_TREE
)
1099 /* Handle bad initializers like:
1103 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1107 int main(int argc, char **argv) {
1108 COMPLEX zees(1.0, 0.0)[10];
1111 error ("bad array initializer");
1112 return error_mark_node
;
1114 stmt_expr
= build_vec_init (exp
, exp
, array_type_nelts (type
), init
,
1115 init
&& same_type_p (TREE_TYPE (init
),
1117 TREE_READONLY (exp
) = was_const
;
1118 TREE_THIS_VOLATILE (exp
) = was_volatile
;
1119 TREE_TYPE (exp
) = type
;
1121 TREE_TYPE (init
) = itype
;
1125 if (TREE_CODE (exp
) == VAR_DECL
|| TREE_CODE (exp
) == PARM_DECL
)
1126 /* just know that we've seen something for this node */
1127 TREE_USED (exp
) = 1;
1129 TREE_TYPE (exp
) = TYPE_MAIN_VARIANT (type
);
1130 begin_init_stmts (&stmt_expr
, &compound_stmt
);
1131 destroy_temps
= stmts_are_full_exprs_p
;
1132 stmts_are_full_exprs_p
= 0;
1133 expand_aggr_init_1 (TYPE_BINFO (type
), exp
, exp
,
1134 init
, LOOKUP_NORMAL
|flags
);
1135 stmt_expr
= finish_init_stmts (stmt_expr
, compound_stmt
);
1136 stmts_are_full_exprs_p
= destroy_temps
;
1137 TREE_TYPE (exp
) = type
;
1138 TREE_READONLY (exp
) = was_const
;
1139 TREE_THIS_VOLATILE (exp
) = was_volatile
;
1145 expand_default_init (binfo
, true_exp
, exp
, init
, flags
)
1151 tree type
= TREE_TYPE (exp
);
1153 /* It fails because there may not be a constructor which takes
1154 its own type as the first (or only parameter), but which does
1155 take other types via a conversion. So, if the thing initializing
1156 the expression is a unit element of type X, first try X(X&),
1157 followed by initialization by X. If neither of these work
1158 out, then look hard. */
1162 if (init
&& TREE_CODE (init
) != TREE_LIST
1163 && (flags
& LOOKUP_ONLYCONVERTING
))
1165 /* Base subobjects should only get direct-initialization. */
1166 if (true_exp
!= exp
)
1169 if (flags
& DIRECT_BIND
)
1170 /* Do nothing. We hit this in two cases: Reference initialization,
1171 where we aren't initializing a real variable, so we don't want
1172 to run a new constructor; and catching an exception, where we
1173 have already built up the constructor call so we could wrap it
1174 in an exception region. */;
1175 else if (TREE_CODE (init
) == CONSTRUCTOR
)
1176 /* A brace-enclosed initializer has whatever type is
1177 required. There's no need to convert it. */
1180 init
= ocp_convert (type
, init
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
1182 if (TREE_CODE (init
) == TRY_CATCH_EXPR
)
1183 /* We need to protect the initialization of a catch parm
1184 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1185 around the TARGET_EXPR for the copy constructor. See
1186 expand_start_catch_block. */
1187 TREE_OPERAND (init
, 0) = build (INIT_EXPR
, TREE_TYPE (exp
), exp
,
1188 TREE_OPERAND (init
, 0));
1190 init
= build (INIT_EXPR
, TREE_TYPE (exp
), exp
, init
);
1191 TREE_SIDE_EFFECTS (init
) = 1;
1192 finish_expr_stmt (init
);
1196 if (init
== NULL_TREE
1197 || (TREE_CODE (init
) == TREE_LIST
&& ! TREE_TYPE (init
)))
1201 init
= TREE_VALUE (parms
);
1204 parms
= build_tree_list (NULL_TREE
, init
);
1206 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
1208 if (true_exp
== exp
)
1209 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
1211 parms
= tree_cons (NULL_TREE
, integer_zero_node
, parms
);
1212 flags
|= LOOKUP_HAS_IN_CHARGE
;
1215 rval
= build_method_call (exp
, ctor_identifier
,
1216 parms
, binfo
, flags
);
1217 if (TREE_SIDE_EFFECTS (rval
))
1218 finish_expr_stmt (rval
);
1221 /* This function is responsible for initializing EXP with INIT
1224 BINFO is the binfo of the type for who we are performing the
1225 initialization. For example, if W is a virtual base class of A and B,
1227 If we are initializing B, then W must contain B's W vtable, whereas
1228 were we initializing C, W must contain C's W vtable.
1230 TRUE_EXP is nonzero if it is the true expression being initialized.
1231 In this case, it may be EXP, or may just contain EXP. The reason we
1232 need this is because if EXP is a base element of TRUE_EXP, we
1233 don't necessarily know by looking at EXP where its virtual
1234 baseclass fields should really be pointing. But we do know
1235 from TRUE_EXP. In constructors, we don't know anything about
1236 the value being initialized.
1238 FLAGS is just passes to `build_method_call'. See that function for
1242 expand_aggr_init_1 (binfo
, true_exp
, exp
, init
, flags
)
1248 tree type
= TREE_TYPE (exp
);
1250 my_friendly_assert (init
!= error_mark_node
&& type
!= error_mark_node
, 211);
1252 /* Use a function returning the desired type to initialize EXP for us.
1253 If the function is a constructor, and its first argument is
1254 NULL_TREE, know that it was meant for us--just slide exp on
1255 in and expand the constructor. Constructors now come
1258 if (init
&& TREE_CODE (exp
) == VAR_DECL
1259 && TREE_CODE (init
) == CONSTRUCTOR
1260 && TREE_HAS_CONSTRUCTOR (init
))
1262 /* If store_init_value returns NULL_TREE, the INIT has been
1263 record in the DECL_INITIAL for EXP. That means there's
1264 nothing more we have to do. */
1265 if (!store_init_value (exp
, init
))
1267 if (!building_stmt_tree ())
1268 expand_decl_init (exp
);
1271 finish_expr_stmt (build (INIT_EXPR
, type
, exp
, init
));
1275 /* We know that expand_default_init can handle everything we want
1277 expand_default_init (binfo
, true_exp
, exp
, init
, flags
);
1280 /* Report an error if NAME is not the name of a user-defined,
1281 aggregate type. If OR_ELSE is nonzero, give an error message. */
1284 is_aggr_typedef (name
, or_else
)
1290 if (name
== error_mark_node
)
1293 if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1294 type
= IDENTIFIER_TYPE_VALUE (name
);
1298 cp_error ("`%T' is not an aggregate typedef", name
);
1302 if (! IS_AGGR_TYPE (type
)
1303 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
1304 && TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
)
1307 cp_error ("`%T' is not an aggregate type", type
);
1313 /* Report an error if TYPE is not a user-defined, aggregate type. If
1314 OR_ELSE is nonzero, give an error message. */
1317 is_aggr_type (type
, or_else
)
1321 if (type
== error_mark_node
)
1324 if (! IS_AGGR_TYPE (type
)
1325 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
1326 && TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
)
1329 cp_error ("`%T' is not an aggregate type", type
);
1335 /* Like is_aggr_typedef, but returns typedef if successful. */
1338 get_aggr_from_typedef (name
, or_else
)
1344 if (name
== error_mark_node
)
1347 if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1348 type
= IDENTIFIER_TYPE_VALUE (name
);
1352 cp_error ("`%T' fails to be an aggregate typedef", name
);
1356 if (! IS_AGGR_TYPE (type
)
1357 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
1358 && TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
)
1361 cp_error ("type `%T' is of non-aggregate type", type
);
1368 get_type_value (name
)
1371 if (name
== error_mark_node
)
1374 if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1375 return IDENTIFIER_TYPE_VALUE (name
);
1381 /* This code could just as well go in `class.c', but is placed here for
1384 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1385 the appropriate function call. */
1388 build_member_call (type
, name
, parmlist
)
1389 tree type
, name
, parmlist
;
1394 tree basetype_path
, decl
;
1396 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
1397 && TREE_CODE (type
) == NAMESPACE_DECL
)
1399 /* 'name' already refers to the decls from the namespace, since we
1400 hit do_identifier for template_ids. */
1401 method_name
= TREE_OPERAND (name
, 0);
1402 /* FIXME: Since we don't do independent names right yet, the
1403 name might also be a LOOKUP_EXPR. Once we resolve this to a
1404 real decl earlier, this can go. This may happen during
1406 if (TREE_CODE (method_name
) == LOOKUP_EXPR
)
1408 method_name
= lookup_namespace_name
1409 (type
, TREE_OPERAND (method_name
, 0));
1410 TREE_OPERAND (name
, 0) = method_name
;
1412 my_friendly_assert (is_overloaded_fn (method_name
), 980519);
1413 return build_x_function_call (name
, parmlist
, current_class_ref
);
1416 if (type
== std_node
)
1417 return build_x_function_call (do_scoped_id (name
, 0), parmlist
,
1419 if (TREE_CODE (type
) == NAMESPACE_DECL
)
1420 return build_x_function_call (lookup_namespace_name (type
, name
),
1421 parmlist
, current_class_ref
);
1423 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
1425 method_name
= TREE_OPERAND (name
, 0);
1426 if (TREE_CODE (method_name
) == COMPONENT_REF
)
1427 method_name
= TREE_OPERAND (method_name
, 1);
1428 if (is_overloaded_fn (method_name
))
1429 method_name
= DECL_NAME (OVL_CURRENT (method_name
));
1430 TREE_OPERAND (name
, 0) = method_name
;
1435 if (TREE_CODE (method_name
) == BIT_NOT_EXPR
)
1437 method_name
= TREE_OPERAND (method_name
, 0);
1441 /* This shouldn't be here, and build_member_call shouldn't appear in
1443 if (type
&& TREE_CODE (type
) == IDENTIFIER_NODE
1444 && get_aggr_from_typedef (type
, 0) == 0)
1446 tree ns
= lookup_name (type
, 0);
1447 if (ns
&& TREE_CODE (ns
) == NAMESPACE_DECL
)
1449 return build_x_function_call (build_offset_ref (type
, name
), parmlist
, current_class_ref
);
1453 if (type
== NULL_TREE
|| ! is_aggr_type (type
, 1))
1454 return error_mark_node
;
1456 /* An operator we did not like. */
1457 if (name
== NULL_TREE
)
1458 return error_mark_node
;
1462 cp_error ("cannot call destructor `%T::~%T' without object", type
,
1464 return error_mark_node
;
1467 decl
= maybe_dummy_object (type
, &basetype_path
);
1469 /* Convert 'this' to the specified type to disambiguate conversion
1470 to the function's context. Apparently Standard C++ says that we
1471 shouldn't do this. */
1472 if (decl
== current_class_ref
1474 && ACCESSIBLY_UNIQUELY_DERIVED_P (type
, current_class_type
))
1476 tree olddecl
= current_class_ptr
;
1477 tree oldtype
= TREE_TYPE (TREE_TYPE (olddecl
));
1478 if (oldtype
!= type
)
1480 tree newtype
= build_qualified_type (type
, TYPE_QUALS (oldtype
));
1481 decl
= convert_force (build_pointer_type (newtype
), olddecl
, 0);
1482 decl
= build_indirect_ref (decl
, NULL_PTR
);
1486 if (method_name
== constructor_name (type
)
1487 || method_name
== constructor_name_full (type
))
1488 return build_functional_cast (type
, parmlist
);
1489 if (lookup_fnfields (basetype_path
, method_name
, 0))
1490 return build_method_call (decl
,
1491 TREE_CODE (name
) == TEMPLATE_ID_EXPR
1492 ? name
: method_name
,
1493 parmlist
, basetype_path
,
1494 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
1495 if (TREE_CODE (name
) == IDENTIFIER_NODE
1496 && ((t
= lookup_field (TYPE_BINFO (type
), name
, 1, 0))))
1498 if (t
== error_mark_node
)
1499 return error_mark_node
;
1500 if (TREE_CODE (t
) == FIELD_DECL
)
1502 if (is_dummy_object (decl
))
1504 cp_error ("invalid use of non-static field `%D'", t
);
1505 return error_mark_node
;
1507 decl
= build (COMPONENT_REF
, TREE_TYPE (t
), decl
, t
);
1509 else if (TREE_CODE (t
) == VAR_DECL
)
1513 cp_error ("invalid use of member `%D'", t
);
1514 return error_mark_node
;
1516 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl
)))
1517 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, decl
,
1518 parmlist
, NULL_TREE
);
1519 return build_function_call (decl
, parmlist
);
1523 cp_error ("no method `%T::%D'", type
, name
);
1524 return error_mark_node
;
1528 /* Build a reference to a member of an aggregate. This is not a
1529 C++ `&', but really something which can have its address taken,
1530 and then act as a pointer to member, for example TYPE :: FIELD
1531 can have its address taken by saying & TYPE :: FIELD.
1533 @@ Prints out lousy diagnostics for operator <typename>
1536 @@ This function should be rewritten and placed in search.c. */
1539 build_offset_ref (type
, name
)
1542 tree decl
, t
= error_mark_node
;
1544 tree basebinfo
= NULL_TREE
;
1545 tree orig_name
= name
;
1547 /* class templates can come in as TEMPLATE_DECLs here. */
1548 if (TREE_CODE (name
) == TEMPLATE_DECL
)
1551 if (type
== std_node
)
1552 return do_scoped_id (name
, 0);
1554 if (processing_template_decl
|| uses_template_parms (type
))
1555 return build_min_nt (SCOPE_REF
, type
, name
);
1557 /* Handle namespace names fully here. */
1558 if (TREE_CODE (type
) == NAMESPACE_DECL
)
1560 t
= lookup_namespace_name (type
, name
);
1561 if (t
!= error_mark_node
&& ! type_unknown_p (t
))
1564 t
= convert_from_reference (t
);
1569 if (type
== NULL_TREE
|| ! is_aggr_type (type
, 1))
1570 return error_mark_node
;
1572 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
1574 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1575 something like `a.template f<int>' or the like. For the most
1576 part, we treat this just like a.f. We do remember, however,
1577 the template-id that was used. */
1578 name
= TREE_OPERAND (orig_name
, 0);
1580 if (TREE_CODE (name
) == LOOKUP_EXPR
)
1581 /* This can happen during tsubst'ing. */
1582 name
= TREE_OPERAND (name
, 0);
1584 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 0);
1587 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1589 if (! check_dtor_name (type
, name
))
1590 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1591 type
, TREE_OPERAND (name
, 0));
1592 name
= dtor_identifier
;
1595 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */
1596 else if (name
== constructor_name_full (type
)
1597 || name
== constructor_name (type
))
1598 name
= ctor_identifier
;
1601 if (TYPE_SIZE (complete_type (type
)) == 0
1602 && !TYPE_BEING_DEFINED (type
))
1604 cp_error ("incomplete type `%T' does not have member `%D'", type
,
1606 return error_mark_node
;
1609 decl
= maybe_dummy_object (type
, &basebinfo
);
1611 member
= lookup_member (basebinfo
, name
, 1, 0);
1613 if (member
== error_mark_node
)
1614 return error_mark_node
;
1616 /* A lot of this logic is now handled in lookup_member. */
1617 if (member
&& BASELINK_P (member
))
1619 /* Go from the TREE_BASELINK to the member function info. */
1620 tree fnfields
= member
;
1621 t
= TREE_VALUE (fnfields
);
1623 if (TREE_CODE (orig_name
) == TEMPLATE_ID_EXPR
)
1625 /* The FNFIELDS are going to contain functions that aren't
1626 necessarily templates, and templates that don't
1627 necessarily match the explicit template parameters. We
1628 save all the functions, and the explicit parameters, and
1629 then figure out exactly what to instantiate with what
1630 arguments in instantiate_type. */
1632 if (TREE_CODE (t
) != OVERLOAD
)
1633 /* The code in instantiate_type which will process this
1634 expects to encounter OVERLOADs, not raw functions. */
1635 t
= ovl_cons (t
, NULL_TREE
);
1637 return build (OFFSET_REF
,
1640 build (TEMPLATE_ID_EXPR
,
1643 TREE_OPERAND (orig_name
, 1)));
1646 if (!really_overloaded_fn (t
))
1648 /* Get rid of a potential OVERLOAD around it */
1649 t
= OVL_CURRENT (t
);
1651 /* unique functions are handled easily. */
1652 if (!enforce_access (basebinfo
, t
))
1653 return error_mark_node
;
1655 if (DECL_STATIC_FUNCTION_P (t
))
1657 return build (OFFSET_REF
, TREE_TYPE (t
), decl
, t
);
1660 TREE_TYPE (fnfields
) = unknown_type_node
;
1661 return build (OFFSET_REF
, unknown_type_node
, decl
, fnfields
);
1668 cp_error ("`%D' is not a member of type `%T'", name
, type
);
1669 return error_mark_node
;
1672 if (TREE_CODE (t
) == TYPE_DECL
)
1677 /* static class members and class-specific enum
1678 values can be returned without further ado. */
1679 if (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == CONST_DECL
)
1682 return convert_from_reference (t
);
1685 if (TREE_CODE (t
) == FIELD_DECL
&& DECL_C_BIT_FIELD (t
))
1687 cp_error ("illegal pointer to bit field `%D'", t
);
1688 return error_mark_node
;
1691 /* static class functions too. */
1692 if (TREE_CODE (t
) == FUNCTION_DECL
1693 && TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1694 my_friendly_abort (53);
1696 /* In member functions, the form `type::name' is no longer
1697 equivalent to `this->type::name', at least not until
1698 resolve_offset_ref. */
1699 return build (OFFSET_REF
, build_offset_type (type
, TREE_TYPE (t
)), decl
, t
);
1702 /* If a OFFSET_REF made it through to here, then it did
1703 not have its address taken. */
1706 resolve_offset_ref (exp
)
1709 tree type
= TREE_TYPE (exp
);
1710 tree base
= NULL_TREE
;
1712 tree basetype
, addr
;
1714 if (TREE_CODE (exp
) == OFFSET_REF
)
1716 member
= TREE_OPERAND (exp
, 1);
1717 base
= TREE_OPERAND (exp
, 0);
1721 my_friendly_assert (TREE_CODE (type
) == OFFSET_TYPE
, 214);
1722 if (TYPE_OFFSET_BASETYPE (type
) != current_class_type
)
1724 error ("object missing in use of pointer-to-member construct");
1725 return error_mark_node
;
1728 type
= TREE_TYPE (type
);
1729 base
= current_class_ref
;
1732 if (BASELINK_P (member
))
1734 if (! flag_ms_extensions
)
1735 cp_pedwarn ("assuming & on overloaded member function");
1736 return build_unary_op (ADDR_EXPR
, exp
, 0);
1739 if (TREE_CODE (TREE_TYPE (member
)) == METHOD_TYPE
)
1741 if (! flag_ms_extensions
)
1742 cp_pedwarn ("assuming & on `%E'", member
);
1743 return build_unary_op (ADDR_EXPR
, exp
, 0);
1746 if ((TREE_CODE (member
) == VAR_DECL
1747 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member
))
1748 && ! TYPE_PTRMEM_P (TREE_TYPE (member
)))
1749 || TREE_CODE (TREE_TYPE (member
)) == FUNCTION_TYPE
)
1751 /* These were static members. */
1752 if (mark_addressable (member
) == 0)
1753 return error_mark_node
;
1757 if (TREE_CODE (TREE_TYPE (member
)) == POINTER_TYPE
1758 && TREE_CODE (TREE_TYPE (TREE_TYPE (member
))) == METHOD_TYPE
)
1761 /* Syntax error can cause a member which should
1762 have been seen as static to be grok'd as non-static. */
1763 if (TREE_CODE (member
) == FIELD_DECL
&& current_class_ref
== NULL_TREE
)
1765 if (TREE_ADDRESSABLE (member
) == 0)
1767 cp_error_at ("member `%D' is non-static but referenced as a static member",
1769 error ("at this point in file");
1770 TREE_ADDRESSABLE (member
) = 1;
1772 return error_mark_node
;
1775 /* The first case is really just a reference to a member of `this'. */
1776 if (TREE_CODE (member
) == FIELD_DECL
1777 && (base
== current_class_ref
|| is_dummy_object (base
)))
1781 basetype
= DECL_CONTEXT (member
);
1783 /* Try to get to basetype from 'this'; if that doesn't work,
1785 base
= current_class_ref
;
1787 /* First convert to the intermediate base specified, if appropriate. */
1788 if (TREE_CODE (exp
) == OFFSET_REF
&& TREE_CODE (type
) == OFFSET_TYPE
)
1789 base
= build_scoped_ref (base
, TYPE_OFFSET_BASETYPE (type
));
1791 addr
= build_unary_op (ADDR_EXPR
, base
, 0);
1792 addr
= convert_pointer_to (basetype
, addr
);
1794 if (addr
== error_mark_node
)
1795 return error_mark_node
;
1797 expr
= build (COMPONENT_REF
, TREE_TYPE (member
),
1798 build_indirect_ref (addr
, NULL_PTR
), member
);
1799 return convert_from_reference (expr
);
1802 /* Ensure that we have an object. */
1803 if (is_dummy_object (base
))
1804 addr
= error_mark_node
;
1806 /* If this is a reference to a member function, then return the
1807 address of the member function (which may involve going
1808 through the object's vtable), otherwise, return an expression
1809 for the dereferenced pointer-to-member construct. */
1810 addr
= build_unary_op (ADDR_EXPR
, base
, 0);
1812 if (TYPE_PTRMEM_P (TREE_TYPE (member
)))
1814 if (addr
== error_mark_node
)
1816 cp_error ("object missing in `%E'", exp
);
1817 return error_mark_node
;
1820 basetype
= TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member
)));
1821 addr
= convert_pointer_to (basetype
, addr
);
1822 member
= cp_convert (ptrdiff_type_node
, member
);
1824 /* Pointer to data members are offset by one, so that a null
1825 pointer with a real value of 0 is distinguishable from an
1826 offset of the first member of a structure. */
1827 member
= build_binary_op (MINUS_EXPR
, member
,
1828 cp_convert (ptrdiff_type_node
, integer_one_node
));
1830 return build1 (INDIRECT_REF
, type
,
1831 build (PLUS_EXPR
, build_pointer_type (type
),
1834 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member
)))
1836 return get_member_function_from_ptrfunc (&addr
, member
);
1838 my_friendly_abort (56);
1843 /* Return either DECL or its known constant value (if it has one). */
1846 decl_constant_value (decl
)
1849 if (! TREE_THIS_VOLATILE (decl
)
1850 && DECL_INITIAL (decl
)
1851 && DECL_INITIAL (decl
) != error_mark_node
1852 /* This is invalid if initial value is not constant.
1853 If it has either a function call, a memory reference,
1854 or a variable, then re-evaluating it could give different results. */
1855 && TREE_CONSTANT (DECL_INITIAL (decl
))
1856 /* Check for cases where this is sub-optimal, even though valid. */
1857 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1858 return DECL_INITIAL (decl
);
1862 /* Common subroutines of build_new and build_vec_delete. */
1864 /* Call the global __builtin_delete to delete ADDR. */
1867 build_builtin_delete_call (addr
)
1870 mark_used (global_delete_fndecl
);
1871 return build_call (global_delete_fndecl
, build_tree_list (NULL_TREE
, addr
));
1874 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1875 (which needs to go through some sort of groktypename) or it
1876 is the name of the class we are newing. INIT is an initialization value.
1877 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1878 If INIT is void_type_node, it means do *not* call a constructor
1881 For types with constructors, the data returned is initialized
1882 by the appropriate constructor.
1884 Whether the type has a constructor or not, if it has a pointer
1885 to a virtual function table, then that pointer is set up
1888 Unless I am mistaken, a call to new () will return initialized
1889 data regardless of whether the constructor itself is private or
1890 not. NOPE; new fails if the constructor is private (jcm).
1892 Note that build_new does nothing to assure that any special
1893 alignment requirements of the type are met. Rather, it leaves
1894 it up to malloc to do the right thing. Otherwise, folding to
1895 the right alignment cal cause problems if the user tries to later
1896 free the memory returned by `new'.
1898 PLACEMENT is the `placement' list for user-defined operator new (). */
1900 extern int flag_check_new
;
1903 build_new (placement
, decl
, init
, use_global_new
)
1909 tree nelts
= NULL_TREE
, t
;
1912 if (decl
== error_mark_node
)
1913 return error_mark_node
;
1915 if (TREE_CODE (decl
) == TREE_LIST
)
1917 tree absdcl
= TREE_VALUE (decl
);
1918 tree last_absdcl
= NULL_TREE
;
1920 if (current_function_decl
1921 && DECL_CONSTRUCTOR_P (current_function_decl
))
1922 my_friendly_assert (immediate_size_expand
== 0, 19990926);
1924 nelts
= integer_one_node
;
1926 if (absdcl
&& TREE_CODE (absdcl
) == CALL_EXPR
)
1927 my_friendly_abort (215);
1928 while (absdcl
&& TREE_CODE (absdcl
) == INDIRECT_REF
)
1930 last_absdcl
= absdcl
;
1931 absdcl
= TREE_OPERAND (absdcl
, 0);
1934 if (absdcl
&& TREE_CODE (absdcl
) == ARRAY_REF
)
1936 /* probably meant to be a vec new */
1939 while (TREE_OPERAND (absdcl
, 0)
1940 && TREE_CODE (TREE_OPERAND (absdcl
, 0)) == ARRAY_REF
)
1942 last_absdcl
= absdcl
;
1943 absdcl
= TREE_OPERAND (absdcl
, 0);
1947 this_nelts
= TREE_OPERAND (absdcl
, 1);
1948 if (this_nelts
!= error_mark_node
)
1950 if (this_nelts
== NULL_TREE
)
1951 error ("new of array type fails to specify size");
1952 else if (processing_template_decl
)
1955 absdcl
= TREE_OPERAND (absdcl
, 0);
1959 int flags
= pedantic
? WANT_INT
: (WANT_INT
| WANT_ENUM
);
1960 if (build_expr_type_conversion (flags
, this_nelts
, 0)
1962 pedwarn ("size in array new must have integral type");
1964 this_nelts
= save_expr (cp_convert (sizetype
, this_nelts
));
1965 absdcl
= TREE_OPERAND (absdcl
, 0);
1966 if (this_nelts
== integer_zero_node
)
1968 warning ("zero size array reserves no space");
1969 nelts
= integer_zero_node
;
1972 nelts
= build_binary_op (MULT_EXPR
, nelts
, this_nelts
);
1976 nelts
= integer_zero_node
;
1980 TREE_OPERAND (last_absdcl
, 0) = absdcl
;
1982 TREE_VALUE (decl
) = absdcl
;
1984 type
= groktypename (decl
);
1985 if (! type
|| type
== error_mark_node
)
1986 return error_mark_node
;
1988 else if (TREE_CODE (decl
) == IDENTIFIER_NODE
)
1990 if (IDENTIFIER_HAS_TYPE_VALUE (decl
))
1992 /* An aggregate type. */
1993 type
= IDENTIFIER_TYPE_VALUE (decl
);
1994 decl
= TYPE_MAIN_DECL (type
);
1998 /* A builtin type. */
1999 decl
= lookup_name (decl
, 1);
2000 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 215);
2001 type
= TREE_TYPE (decl
);
2004 else if (TREE_CODE (decl
) == TYPE_DECL
)
2006 type
= TREE_TYPE (decl
);
2011 decl
= TYPE_MAIN_DECL (type
);
2014 if (processing_template_decl
)
2017 t
= tree_cons (tree_cons (NULL_TREE
, type
, NULL_TREE
),
2018 build_min_nt (ARRAY_REF
, NULL_TREE
, nelts
),
2023 rval
= build_min_nt (NEW_EXPR
, placement
, t
, init
);
2024 NEW_EXPR_USE_GLOBAL (rval
) = use_global_new
;
2028 /* ``A reference cannot be created by the new operator. A reference
2029 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2030 returned by new.'' ARM 5.3.3 */
2031 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2033 error ("new cannot be applied to a reference type");
2034 type
= TREE_TYPE (type
);
2037 if (TREE_CODE (type
) == FUNCTION_TYPE
)
2039 error ("new cannot be applied to a function type");
2040 return error_mark_node
;
2043 /* When the object being created is an array, the new-expression yields a
2044 pointer to the initial element (if any) of the array. For example,
2045 both new int and new int[10] return an int*. 5.3.4. */
2046 if (TREE_CODE (type
) == ARRAY_TYPE
&& has_array
== 0)
2048 nelts
= array_type_nelts_top (type
);
2050 type
= TREE_TYPE (type
);
2054 t
= build_nt (ARRAY_REF
, type
, nelts
);
2058 rval
= build (NEW_EXPR
, build_pointer_type (type
), placement
, t
, init
);
2059 NEW_EXPR_USE_GLOBAL (rval
) = use_global_new
;
2060 TREE_SIDE_EFFECTS (rval
) = 1;
2061 rval
= build_new_1 (rval
);
2062 if (rval
== error_mark_node
)
2063 return error_mark_node
;
2065 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2066 rval
= build1 (NOP_EXPR
, TREE_TYPE (rval
), rval
);
2067 TREE_NO_UNUSED_WARNING (rval
) = 1;
2072 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2075 build_java_class_ref (type
)
2078 tree name
, class_decl
;
2079 static tree CL_prefix
= NULL_TREE
;
2080 if (CL_prefix
== NULL_TREE
)
2081 CL_prefix
= get_identifier("_CL_");
2082 if (jclass_node
== NULL_TREE
)
2084 jclass_node
= IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2085 if (jclass_node
== NULL_TREE
)
2086 fatal("call to Java constructor, while `jclass' undefined");
2087 jclass_node
= TREE_TYPE (jclass_node
);
2089 name
= build_overload_with_type (CL_prefix
, type
);
2090 class_decl
= IDENTIFIER_GLOBAL_VALUE (name
);
2091 if (class_decl
== NULL_TREE
)
2093 class_decl
= build_decl (VAR_DECL
, name
, TREE_TYPE (jclass_node
));
2094 TREE_STATIC (class_decl
) = 1;
2095 DECL_EXTERNAL (class_decl
) = 1;
2096 TREE_PUBLIC (class_decl
) = 1;
2097 DECL_ARTIFICIAL (class_decl
) = 1;
2098 DECL_IGNORED_P (class_decl
) = 1;
2099 pushdecl_top_level (class_decl
);
2100 make_decl_rtl (class_decl
, NULL_PTR
, 1);
2105 /* Returns teh size of the cookie to use when allocating an array
2106 whose elements have the indicated TYPE. Assumes that it is already
2107 known that a cookie is needed. */
2110 get_cookie_size (type
)
2117 /* Under the new ABI, we need to allocate an additional max
2118 (sizeof (size_t), alignof (true_type)) bytes. */
2122 sizetype_size
= size_in_bytes (sizetype
);
2123 type_align
= size_int (TYPE_ALIGN_UNIT (type
));
2124 if (INT_CST_LT_UNSIGNED (type_align
, sizetype_size
))
2125 cookie_size
= sizetype_size
;
2127 cookie_size
= type_align
;
2130 cookie_size
= BI_header_size
;
2135 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2136 value is immediately handed to expand_expr. */
2142 tree placement
, init
;
2143 tree type
, true_type
, size
, rval
;
2144 tree nelts
= NULL_TREE
;
2145 tree alloc_expr
, alloc_node
= NULL_TREE
;
2147 enum tree_code code
;
2148 int use_cookie
, nothrow
, check_new
;
2149 /* Nonzero if the user wrote `::new' rather than just `new'. */
2150 int globally_qualified_p
;
2151 /* Nonzero if we're going to call a global operator new, rather than
2152 a class-specific version. */
2154 int use_java_new
= 0;
2155 /* If non-NULL, the number of extra bytes to allocate at the
2156 beginning of the storage allocated for an array-new expression in
2157 order to store the number of elements. */
2158 tree cookie_size
= NULL_TREE
;
2160 placement
= TREE_OPERAND (exp
, 0);
2161 type
= TREE_OPERAND (exp
, 1);
2162 init
= TREE_OPERAND (exp
, 2);
2163 globally_qualified_p
= NEW_EXPR_USE_GLOBAL (exp
);
2165 if (TREE_CODE (type
) == ARRAY_REF
)
2168 nelts
= TREE_OPERAND (type
, 1);
2169 type
= TREE_OPERAND (type
, 0);
2173 code
= has_array
? VEC_NEW_EXPR
: NEW_EXPR
;
2175 if (CP_TYPE_QUALS (type
))
2176 type
= TYPE_MAIN_VARIANT (type
);
2178 /* If our base type is an array, then make sure we know how many elements
2180 while (TREE_CODE (true_type
) == ARRAY_TYPE
)
2182 tree this_nelts
= array_type_nelts_top (true_type
);
2183 nelts
= build_binary_op (MULT_EXPR
, nelts
, this_nelts
);
2184 true_type
= TREE_TYPE (true_type
);
2187 if (!complete_type_or_else (true_type
, exp
))
2188 return error_mark_node
;
2191 size
= fold (build_binary_op (MULT_EXPR
, size_in_bytes (true_type
),
2194 size
= size_in_bytes (type
);
2196 if (TREE_CODE (true_type
) == VOID_TYPE
)
2198 error ("invalid type `void' for new");
2199 return error_mark_node
;
2202 if (abstract_virtuals_error (NULL_TREE
, true_type
))
2203 return error_mark_node
;
2205 /* Figure out whether or not we're going to use the global operator
2207 if (!globally_qualified_p
2208 && IS_AGGR_TYPE (true_type
)
2209 && ((!has_array
&& TYPE_HAS_NEW_OPERATOR (true_type
))
2210 || (has_array
&& TYPE_HAS_ARRAY_NEW_OPERATOR (true_type
))))
2215 /* We only need cookies for arrays containing types for which we
2217 if (!has_array
|| !TYPE_VEC_NEW_USES_COOKIE (true_type
))
2219 /* When using placement new, users may not realize that they need
2220 the extra storage. Under the old ABI, we don't allocate the
2221 cookie whenever they use one placement argument of type `void
2222 *'. Under the new ABI, we require that the operator called be
2223 the global placement operator delete[]. */
2224 else if (placement
&& !TREE_CHAIN (placement
)
2225 && same_type_p (TREE_TYPE (TREE_VALUE (placement
)),
2227 use_cookie
= (!flag_new_abi
|| !use_global_new
);
2228 /* Otherwise, we need the cookie. */
2232 /* Compute the number of extra bytes to allocate, now that we know
2233 whether or not we need the cookie. */
2236 cookie_size
= get_cookie_size (true_type
);
2237 size
= size_binop (PLUS_EXPR
, size
, cookie_size
);
2240 if (has_array
&& init
&& pedantic
)
2241 cp_pedwarn ("initialization in array new");
2243 /* Allocate the object. */
2245 if (! placement
&& TYPE_FOR_JAVA (true_type
))
2247 tree class_addr
, alloc_decl
;
2248 tree class_decl
= build_java_class_ref (true_type
);
2249 tree class_size
= size_in_bytes (true_type
);
2250 static char alloc_name
[] = "_Jv_AllocObject";
2252 alloc_decl
= IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name
));
2253 if (alloc_decl
== NULL_TREE
)
2254 fatal("call to Java constructor, while `%s' undefined", alloc_name
);
2255 class_addr
= build1 (ADDR_EXPR
, jclass_node
, class_decl
);
2256 rval
= build_function_call (alloc_decl
,
2257 tree_cons (NULL_TREE
, class_addr
,
2258 build_tree_list (NULL_TREE
,
2260 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2267 args
= tree_cons (NULL_TREE
, size
, placement
);
2268 fnname
= ansi_opname
[code
];
2271 rval
= (build_new_function_call
2272 (lookup_function_nonclass (fnname
, args
),
2275 rval
= build_method_call (build_dummy_object (true_type
),
2276 fnname
, args
, NULL_TREE
,
2278 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2281 /* unless an allocation function is declared with an empty excep-
2282 tion-specification (_except.spec_), throw(), it indicates failure to
2283 allocate storage by throwing a bad_alloc exception (clause _except_,
2284 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2285 cation function is declared with an empty exception-specification,
2286 throw(), it returns null to indicate failure to allocate storage and a
2287 non-null pointer otherwise.
2289 So check for a null exception spec on the op new we just called. */
2294 /* The CALL_EXPR. */
2295 tree t
= TREE_OPERAND (rval
, 0);
2297 t
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
2298 nothrow
= TYPE_NOTHROW_P (TREE_TYPE (t
));
2300 check_new
= (flag_check_new
|| nothrow
) && ! use_java_new
;
2302 if ((check_new
|| flag_exceptions
) && rval
)
2304 alloc_expr
= get_target_expr (rval
);
2305 alloc_node
= rval
= TREE_OPERAND (alloc_expr
, 0);
2308 alloc_expr
= NULL_TREE
;
2310 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2311 sure we have some extra bytes in that case for the BI_header_size
2312 cookies? And how does that interact with the code below? (mrs) */
2313 /* Finish up some magic for new'ed arrays */
2314 if (use_cookie
&& rval
!= NULL_TREE
)
2317 rval
= convert (string_type_node
, rval
); /* for ptr arithmetic */
2318 rval
= save_expr (build_binary_op (PLUS_EXPR
, rval
, cookie_size
));
2319 /* Store the number of bytes allocated so that we can know how
2320 many elements to destroy later. */
2323 /* Under the new ABI, we use the last sizeof (size_t) bytes
2324 to store the number of elements. */
2325 cookie
= build_indirect_ref (build (MINUS_EXPR
,
2326 build_pointer_type (sizetype
),
2328 size_in_bytes (sizetype
)),
2330 exp1
= build (MODIFY_EXPR
, void_type_node
, cookie
, nelts
);
2335 = build_indirect_ref (build (MINUS_EXPR
,
2336 build_pointer_type (BI_header_type
),
2337 rval
, cookie_size
), NULL_PTR
);
2338 exp1
= build (MODIFY_EXPR
, void_type_node
,
2339 build_component_ref (cookie
, nelts_identifier
,
2344 /* Build `(cookie = nelts, rval)' and use that as the complete
2346 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2347 rval
= build_compound_expr
2348 (tree_cons (NULL_TREE
, exp1
,
2349 build_tree_list (NULL_TREE
, rval
)));
2352 if (rval
== error_mark_node
)
2353 return error_mark_node
;
2355 /* Don't call any constructors or do any initialization. */
2356 if (init
== void_type_node
)
2359 if (TYPE_NEEDS_CONSTRUCTING (type
) || init
)
2361 if (! TYPE_NEEDS_CONSTRUCTING (type
)
2362 && ! IS_AGGR_TYPE (type
) && ! has_array
)
2364 /* We are processing something like `new int (10)', which
2365 means allocate an int, and initialize it with 10. */
2369 /* At present RVAL is a temporary variable, created to hold
2370 the value from the call to `operator new'. We transform
2371 it to (*RVAL = INIT, RVAL). */
2372 rval
= save_expr (rval
);
2373 deref
= build_indirect_ref (rval
, NULL_PTR
);
2375 /* Even for something like `new const int (10)' we must
2376 allow the expression to be non-const while we do the
2378 deref_type
= TREE_TYPE (deref
);
2379 if (CP_TYPE_CONST_P (deref_type
))
2381 = cp_build_qualified_type (deref_type
,
2382 CP_TYPE_QUALS (deref_type
)
2383 & ~TYPE_QUAL_CONST
);
2384 TREE_READONLY (deref
) = 0;
2386 if (TREE_CHAIN (init
) != NULL_TREE
)
2387 pedwarn ("initializer list being treated as compound expression");
2388 else if (TREE_CODE (init
) == CONSTRUCTOR
)
2390 pedwarn ("initializer list appears where operand should be used");
2391 init
= TREE_OPERAND (init
, 1);
2393 init
= build_compound_expr (init
);
2395 init
= convert_for_initialization (deref
, type
, init
, LOOKUP_NORMAL
,
2396 "new", NULL_TREE
, 0);
2397 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
),
2398 build_modify_expr (deref
, NOP_EXPR
, init
),
2400 TREE_NO_UNUSED_WARNING (rval
) = 1;
2401 TREE_SIDE_EFFECTS (rval
) = 1;
2403 else if (! has_array
)
2406 /* Constructors are never virtual. If it has an initialization, we
2407 need to complain if we aren't allowed to use the ctor that took
2409 int flags
= LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_COMPLAIN
;
2411 if (rval
&& TYPE_USES_VIRTUAL_BASECLASSES (true_type
))
2413 init
= tree_cons (NULL_TREE
, integer_one_node
, init
);
2414 flags
|= LOOKUP_HAS_IN_CHARGE
;
2418 rval
= save_expr (rval
);
2421 if (newrval
&& TREE_CODE (TREE_TYPE (newrval
)) == POINTER_TYPE
)
2422 newrval
= build_indirect_ref (newrval
, NULL_PTR
);
2424 newrval
= build_method_call (newrval
, ctor_identifier
,
2425 init
, TYPE_BINFO (true_type
), flags
);
2427 if (newrval
== NULL_TREE
|| newrval
== error_mark_node
)
2428 return error_mark_node
;
2430 /* Java constructors compiled by jc1 do not return this. */
2432 newrval
= build (COMPOUND_EXPR
, TREE_TYPE (newrval
),
2435 TREE_HAS_CONSTRUCTOR (rval
) = 1;
2438 rval
= (build_vec_init
2441 build_binary_op (MINUS_EXPR
, nelts
, integer_one_node
),
2445 /* If any part of the object initialization terminates by throwing an
2446 exception and a suitable deallocation function can be found, the
2447 deallocation function is called to free the memory in which the
2448 object was being constructed, after which the exception continues
2449 to propagate in the context of the new-expression. If no
2450 unambiguous matching deallocation function can be found,
2451 propagating the exception does not cause the object's memory to be
2453 if (flag_exceptions
&& alloc_expr
&& ! use_java_new
)
2455 enum tree_code dcode
= has_array
? VEC_DELETE_EXPR
: DELETE_EXPR
;
2456 tree cleanup
, fn
= NULL_TREE
;
2457 int flags
= (LOOKUP_NORMAL
2458 | (globally_qualified_p
* LOOKUP_GLOBAL
));
2460 /* The Standard is unclear here, but the right thing to do
2461 is to use the same method for finding deallocation
2462 functions that we use for finding allocation functions. */
2463 flags
|= LOOKUP_SPECULATIVELY
;
2465 /* We expect alloc_expr to look like a TARGET_EXPR around
2466 a NOP_EXPR around the CALL_EXPR we want. */
2467 fn
= TREE_OPERAND (alloc_expr
, 1);
2468 fn
= TREE_OPERAND (fn
, 0);
2470 cleanup
= build_op_delete_call (dcode
, alloc_node
, size
, flags
, fn
);
2472 /* Ack! First we allocate the memory. Then we set our sentry
2473 variable to true, and expand a cleanup that deletes the memory
2474 if sentry is true. Then we run the constructor and store the
2475 returned pointer in buf. Then we clear sentry and return buf. */
2479 tree end
, sentry
, begin
, buf
, t
= TREE_TYPE (rval
);
2481 begin
= get_target_expr (boolean_true_node
);
2482 sentry
= TREE_OPERAND (begin
, 0);
2484 TREE_OPERAND (begin
, 2)
2485 = build (COND_EXPR
, void_type_node
, sentry
,
2486 cleanup
, void_zero_node
);
2488 rval
= get_target_expr (rval
);
2490 end
= build (MODIFY_EXPR
, TREE_TYPE (sentry
),
2491 sentry
, boolean_false_node
);
2493 buf
= TREE_OPERAND (rval
, 0);
2495 rval
= build (COMPOUND_EXPR
, t
, begin
,
2496 build (COMPOUND_EXPR
, t
, rval
,
2497 build (COMPOUND_EXPR
, t
, end
, buf
)));
2501 else if (CP_TYPE_CONST_P (true_type
))
2502 cp_error ("uninitialized const in `new' of `%#T'", true_type
);
2506 if (alloc_expr
&& rval
== alloc_node
)
2508 rval
= TREE_OPERAND (alloc_expr
, 1);
2509 alloc_expr
= NULL_TREE
;
2512 if (check_new
&& alloc_expr
)
2514 /* Did we modify the storage? */
2515 tree ifexp
= build_binary_op (NE_EXPR
, alloc_node
,
2517 rval
= build_conditional_expr (ifexp
, rval
, alloc_node
);
2521 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
), alloc_expr
, rval
);
2523 if (rval
&& TREE_TYPE (rval
) != build_pointer_type (type
))
2525 /* The type of new int [3][3] is not int *, but int [3] * */
2526 rval
= build_c_cast (build_pointer_type (type
), rval
);
2533 build_vec_delete_1 (base
, maxindex
, type
, auto_delete_vec
, use_global_delete
)
2534 tree base
, maxindex
, type
;
2535 tree auto_delete_vec
;
2536 int use_global_delete
;
2539 tree ptype
= build_pointer_type (type
= complete_type (type
));
2540 tree size_exp
= size_in_bytes (type
);
2542 /* Temporary variables used by the loop. */
2543 tree tbase
, tbase_init
;
2545 /* This is the body of the loop that implements the deletion of a
2546 single element, and moves temp variables to next elements. */
2549 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2552 /* This is the thing that governs what to do after the loop has run. */
2553 tree deallocate_expr
= 0;
2555 /* This is the BIND_EXPR which holds the outermost iterator of the
2556 loop. It is convenient to set this variable up and test it before
2557 executing any other code in the loop.
2558 This is also the containing expression returned by this function. */
2559 tree controller
= NULL_TREE
;
2561 if (! IS_AGGR_TYPE (type
) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
2563 loop
= integer_zero_node
;
2567 /* The below is short by BI_header_size */
2568 virtual_size
= size_binop (MULT_EXPR
, size_exp
,
2569 convert (sizetype
, maxindex
));
2571 tbase
= create_temporary_var (ptype
);
2572 tbase_init
= build_modify_expr (tbase
, NOP_EXPR
,
2573 fold (build (PLUS_EXPR
, ptype
,
2576 DECL_REGISTER (tbase
) = 1;
2577 controller
= build (BIND_EXPR
, void_type_node
, tbase
, NULL_TREE
, NULL_TREE
);
2578 TREE_SIDE_EFFECTS (controller
) = 1;
2582 body
= tree_cons (NULL_TREE
,
2583 build_delete (ptype
, tbase
, integer_two_node
,
2584 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 1),
2587 body
= tree_cons (NULL_TREE
,
2588 build_modify_expr (tbase
, NOP_EXPR
, build (MINUS_EXPR
, ptype
, tbase
, size_exp
)),
2591 body
= tree_cons (NULL_TREE
,
2592 build (EXIT_EXPR
, void_type_node
,
2593 build (EQ_EXPR
, boolean_type_node
, base
, tbase
)),
2596 loop
= build (LOOP_EXPR
, void_type_node
, build_compound_expr (body
));
2598 loop
= tree_cons (NULL_TREE
, tbase_init
,
2599 tree_cons (NULL_TREE
, loop
, NULL_TREE
));
2600 loop
= build_compound_expr (loop
);
2603 /* If the delete flag is one, or anything else with the low bit set,
2604 delete the storage. */
2605 if (auto_delete_vec
== integer_zero_node
)
2606 deallocate_expr
= integer_zero_node
;
2611 /* The below is short by BI_header_size */
2612 virtual_size
= size_binop (MULT_EXPR
, size_exp
,
2613 convert (sizetype
, maxindex
));
2615 if (! TYPE_VEC_NEW_USES_COOKIE (type
))
2622 cookie_size
= get_cookie_size (type
);
2624 = cp_convert (ptype
,
2625 build_binary_op (MINUS_EXPR
,
2626 cp_convert (string_type_node
, base
),
2628 /* True size with header. */
2629 virtual_size
= size_binop (PLUS_EXPR
, virtual_size
, cookie_size
);
2631 deallocate_expr
= build_x_delete (base_tbd
,
2632 2 | use_global_delete
,
2634 deallocate_expr
= fold (build (COND_EXPR
, void_type_node
,
2635 fold (build (BIT_AND_EXPR
,
2639 deallocate_expr
, integer_zero_node
));
2642 if (loop
&& deallocate_expr
!= integer_zero_node
)
2644 body
= tree_cons (NULL_TREE
, loop
,
2645 tree_cons (NULL_TREE
, deallocate_expr
, NULL_TREE
));
2646 body
= build_compound_expr (body
);
2651 /* Outermost wrapper: If pointer is null, punt. */
2652 body
= fold (build (COND_EXPR
, void_type_node
,
2653 fold (build (NE_EXPR
, boolean_type_node
, base
,
2654 integer_zero_node
)),
2655 body
, integer_zero_node
));
2656 body
= build1 (NOP_EXPR
, void_type_node
, body
);
2660 TREE_OPERAND (controller
, 1) = body
;
2664 return cp_convert (void_type_node
, body
);
2668 create_temporary_var (type
)
2673 decl
= build_decl (VAR_DECL
, NULL_TREE
, type
);
2674 TREE_USED (decl
) = 1;
2675 DECL_ARTIFICIAL (decl
) = 1;
2676 DECL_SOURCE_FILE (decl
) = input_filename
;
2677 DECL_SOURCE_LINE (decl
) = lineno
;
2678 DECL_IGNORED_P (decl
) = 1;
2679 DECL_CONTEXT (decl
) = current_function_decl
;
2684 /* Create a new temporary variable of the indicated TYPE, initialized
2687 It is not entered into current_binding_level, because that breaks
2688 things when it comes time to do final cleanups (which take place
2689 "outside" the binding contour of the function). */
2692 get_temp_regvar (type
, init
)
2697 decl
= create_temporary_var (type
);
2698 if (building_stmt_tree ())
2699 add_decl_stmt (decl
);
2700 if (!building_stmt_tree ())
2701 DECL_RTL (decl
) = assign_temp (type
, 2, 0, 1);
2702 finish_expr_stmt (build_modify_expr (decl
, INIT_EXPR
, init
));
2707 /* `build_vec_init' returns tree structure that performs
2708 initialization of a vector of aggregate types.
2710 DECL is passed only for error reporting, and provides line number
2711 and source file name information.
2712 BASE is the space where the vector will be. For a vector of Ts,
2713 the type of BASE is `T*'.
2714 MAXINDEX is the maximum index of the array (one less than the
2715 number of elements).
2716 INIT is the (possibly NULL) initializer.
2718 FROM_ARRAY is 0 if we should init everything with INIT
2719 (i.e., every element initialized from INIT).
2720 FROM_ARRAY is 1 if we should index into INIT in parallel
2721 with initialization of DECL.
2722 FROM_ARRAY is 2 if we should index into INIT in parallel,
2723 but use assignment instead of initialization. */
2726 build_vec_init (decl
, base
, maxindex
, init
, from_array
)
2727 tree decl
, base
, maxindex
, init
;
2731 tree base2
= NULL_TREE
;
2733 tree itype
= NULL_TREE
;
2735 /* The type of an element in the array. */
2737 /* The type of a pointer to an element in the array. */
2742 tree try_block
= NULL_TREE
;
2743 tree try_body
= NULL_TREE
;
2744 int num_initialized_elts
= 0;
2746 maxindex
= cp_convert (ptrdiff_type_node
, maxindex
);
2747 if (maxindex
== error_mark_node
)
2748 return error_mark_node
;
2750 type
= TREE_TYPE (TREE_TYPE (base
));
2751 ptype
= build_pointer_type (type
);
2752 size
= size_in_bytes (type
);
2754 /* The code we are generating looks like:
2758 ptrdiff_t iterator = maxindex;
2760 ... initializations from CONSTRUCTOR ...
2761 if (iterator != -1) {
2763 ... initialize *base ...
2765 } while (--iterator != -1);
2768 ... destroy elements that were constructed ...
2771 We can omit the try and catch blocks if we know that the
2772 initialization will never throw an exception, or if the array
2773 elements do not have destructors. If we have a CONSTRUCTOR to
2774 give us initialization information, we emit code to initialize
2775 each of the elements before the loop in the try block, and then
2776 iterate over fewer elements. We can omit the loop completely if
2777 the elements of the array do not have constructors.
2779 We actually wrap the entire body of the above in a STMT_EXPR, for
2782 When copying from array to another, when the array elements have
2783 only trivial copy constructors, we should use __builtin_memcpy
2784 rather than generating a loop. That way, we could take advantage
2785 of whatever cleverness the back-end has for dealing with copies
2786 of blocks of memory. */
2788 begin_init_stmts (&stmt_expr
, &compound_stmt
);
2789 destroy_temps
= stmts_are_full_exprs_p
;
2790 stmts_are_full_exprs_p
= 0;
2791 rval
= get_temp_regvar (ptype
,
2792 cp_convert (ptype
, default_conversion (base
)));
2793 base
= get_temp_regvar (ptype
, rval
);
2794 iterator
= get_temp_regvar (ptrdiff_type_node
, maxindex
);
2796 /* Protect the entire array initialization so that we can destroy
2797 the partially constructed array if an exception is thrown. */
2798 if (flag_exceptions
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
2800 try_block
= begin_try_block ();
2801 try_body
= begin_compound_stmt (/*has_no_scope=*/1);
2804 if (init
!= NULL_TREE
&& TREE_CODE (init
) == CONSTRUCTOR
2805 && (!decl
|| same_type_p (TREE_TYPE (init
), TREE_TYPE (decl
))))
2807 /* Do non-default initialization resulting from brace-enclosed
2813 for (elts
= CONSTRUCTOR_ELTS (init
); elts
; elts
= TREE_CHAIN (elts
))
2815 tree elt
= TREE_VALUE (elts
);
2816 tree baseref
= build1 (INDIRECT_REF
, type
, base
);
2818 num_initialized_elts
++;
2820 if (IS_AGGR_TYPE (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
2821 finish_expr_stmt (build_aggr_init (baseref
, elt
, 0));
2823 finish_expr_stmt (build_modify_expr (baseref
, NOP_EXPR
,
2826 finish_expr_stmt (build_modify_expr
2829 build (PLUS_EXPR
, build_pointer_type (type
),
2831 finish_expr_stmt (build_modify_expr
2834 build (MINUS_EXPR
, ptrdiff_type_node
,
2835 iterator
, integer_one_node
)));
2838 /* Clear out INIT so that we don't get confused below. */
2841 else if (from_array
)
2843 /* If initializing one array from another, initialize element by
2844 element. We rely upon the below calls the do argument
2846 if (decl
== NULL_TREE
)
2848 sorry ("initialization of array from dissimilar array type");
2849 return error_mark_node
;
2853 base2
= default_conversion (init
);
2854 itype
= TREE_TYPE (base2
);
2855 base2
= get_temp_regvar (itype
, base2
);
2856 itype
= TREE_TYPE (itype
);
2858 else if (TYPE_LANG_SPECIFIC (type
)
2859 && TYPE_NEEDS_CONSTRUCTING (type
)
2860 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
2862 error ("initializer ends prematurely");
2863 return error_mark_node
;
2867 /* Now, default-initialize any remaining elements. We don't need to
2868 do that if a) the type does not need constructing, or b) we've
2869 already initialized all the elements.
2871 We do need to keep going if we're copying an array. */
2874 || (TYPE_NEEDS_CONSTRUCTING (type
)
2875 && ! (TREE_CODE (maxindex
) == INTEGER_CST
2876 && (num_initialized_elts
2877 == (HOST_WIDE_INT
) TREE_INT_CST_LOW (maxindex
) + 1))))
2879 /* If the ITERATOR is equal to -1, then we don't have to loop;
2880 we've already initialized all the elements. */
2886 if_stmt
= begin_if_stmt ();
2887 finish_if_stmt_cond (build (NE_EXPR
, boolean_type_node
,
2888 iterator
, minus_one_node
),
2891 /* Otherwise, loop through the elements. */
2892 do_stmt
= begin_do_stmt ();
2893 do_body
= begin_compound_stmt (/*has_no_scope=*/1);
2895 /* When we're not building a statement-tree, things are a little
2896 complicated. If, when we recursively call build_aggr_init,
2897 an expression containing a TARGET_EXPR is expanded, then it
2898 may get a cleanup. Then, the result of that expression is
2899 passed to finish_expr_stmt, which will call
2900 expand_start_target_temps/expand_end_target_temps. However,
2901 the latter call will not cause the cleanup to run because
2902 that block will still be on the block stack. So, we call
2903 expand_start_target_temps here manually; the corresponding
2904 call to expand_end_target_temps below will cause the cleanup
2906 if (!building_stmt_tree ())
2907 expand_start_target_temps ();
2911 tree to
= build1 (INDIRECT_REF
, type
, base
);
2915 from
= build1 (INDIRECT_REF
, itype
, base2
);
2919 if (from_array
== 2)
2920 elt_init
= build_modify_expr (to
, NOP_EXPR
, from
);
2921 else if (TYPE_NEEDS_CONSTRUCTING (type
))
2922 elt_init
= build_aggr_init (to
, from
, 0);
2924 elt_init
= build_modify_expr (to
, NOP_EXPR
, from
);
2926 my_friendly_abort (57);
2928 else if (TREE_CODE (type
) == ARRAY_TYPE
)
2931 sorry ("cannot initialize multi-dimensional array with initializer");
2932 elt_init
= (build_vec_init
2935 build_pointer_type (TREE_TYPE (type
)),
2937 array_type_nelts (type
), 0, 0));
2940 elt_init
= build_aggr_init (build1 (INDIRECT_REF
, type
, base
),
2943 /* The initialization of each array element is a
2945 if (!building_stmt_tree ())
2947 finish_expr_stmt (elt_init
);
2948 expand_end_target_temps ();
2952 stmts_are_full_exprs_p
= 1;
2953 finish_expr_stmt (elt_init
);
2954 stmts_are_full_exprs_p
= 0;
2957 finish_expr_stmt (build_modify_expr
2960 build (PLUS_EXPR
, build_pointer_type (type
),
2963 finish_expr_stmt (build_modify_expr
2966 build (PLUS_EXPR
, build_pointer_type (type
),
2969 finish_compound_stmt (/*has_no_scope=*/1, do_body
);
2970 finish_do_body (do_stmt
);
2971 finish_do_stmt (build (NE_EXPR
, boolean_type_node
,
2972 build (PREDECREMENT_EXPR
,
2979 finish_then_clause (if_stmt
);
2983 /* Make sure to cleanup any partially constructed elements. */
2984 if (flag_exceptions
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
2988 finish_compound_stmt (/*has_no_scope=*/1, try_body
);
2989 finish_cleanup_try_block (try_block
);
2990 e
= build_vec_delete_1 (rval
,
2991 build_binary_op (MINUS_EXPR
, maxindex
,
2994 /*auto_delete_vec=*/integer_zero_node
,
2995 /*use_global_delete=*/0);
2996 finish_cleanup (e
, try_block
);
2999 /* The value of the array initialization is the address of the
3000 first element in the array. */
3001 finish_expr_stmt (rval
);
3003 stmt_expr
= finish_init_stmts (stmt_expr
, compound_stmt
);
3004 stmts_are_full_exprs_p
= destroy_temps
;
3008 /* Free up storage of type TYPE, at address ADDR.
3010 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3013 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3014 used as the second argument to operator delete. It can include
3015 things like padding and magic size cookies. It has virtual in it,
3016 because if you have a base pointer and you delete through a virtual
3017 destructor, it should be the size of the dynamic object, not the
3018 static object, see Free Store 12.5 ISO C++.
3020 This does not call any destructors. */
3023 build_x_delete (addr
, which_delete
, virtual_size
)
3028 int use_global_delete
= which_delete
& 1;
3029 int use_vec_delete
= !!(which_delete
& 2);
3030 enum tree_code code
= use_vec_delete
? VEC_DELETE_EXPR
: DELETE_EXPR
;
3031 int flags
= LOOKUP_NORMAL
| (use_global_delete
* LOOKUP_GLOBAL
);
3033 return build_op_delete_call (code
, addr
, virtual_size
, flags
, NULL_TREE
);
3036 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3037 ADDR is an expression which yields the store to be destroyed.
3038 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3039 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3040 virtual baseclasses.
3041 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3043 FLAGS is the logical disjunction of zero or more LOOKUP_
3044 flags. See cp-tree.h for more info.
3046 This function does not delete an object's virtual base classes. */
3049 build_delete (type
, addr
, auto_delete
, flags
, use_global_delete
)
3053 int use_global_delete
;
3059 if (addr
== error_mark_node
)
3060 return error_mark_node
;
3062 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3063 set to `error_mark_node' before it gets properly cleaned up. */
3064 if (type
== error_mark_node
)
3065 return error_mark_node
;
3067 type
= TYPE_MAIN_VARIANT (type
);
3069 if (TREE_CODE (type
) == POINTER_TYPE
)
3071 type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
3072 if (type
!= void_type_node
&& !complete_type_or_else (type
, addr
))
3073 return error_mark_node
;
3074 if (TREE_CODE (type
) == ARRAY_TYPE
)
3076 if (! IS_AGGR_TYPE (type
))
3078 /* Call the builtin operator delete. */
3079 return build_builtin_delete_call (addr
);
3081 if (TREE_SIDE_EFFECTS (addr
))
3082 addr
= save_expr (addr
);
3084 /* throw away const and volatile on target type of addr */
3085 addr
= convert_force (build_pointer_type (type
), addr
, 0);
3086 ref
= build_indirect_ref (addr
, NULL_PTR
);
3088 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3091 if (TREE_SIDE_EFFECTS (addr
))
3092 addr
= save_expr (addr
);
3093 if (TYPE_DOMAIN (type
) == NULL_TREE
)
3095 error ("unknown array size in delete");
3096 return error_mark_node
;
3098 return build_vec_delete (addr
, array_type_nelts (type
),
3099 auto_delete
, use_global_delete
);
3103 /* Don't check PROTECT here; leave that decision to the
3104 destructor. If the destructor is accessible, call it,
3105 else report error. */
3106 addr
= build_unary_op (ADDR_EXPR
, addr
, 0);
3107 if (TREE_SIDE_EFFECTS (addr
))
3108 addr
= save_expr (addr
);
3110 if (TREE_CONSTANT (addr
))
3111 addr
= convert_pointer_to (type
, addr
);
3113 addr
= convert_force (build_pointer_type (type
), addr
, 0);
3115 ref
= build_indirect_ref (addr
, NULL_PTR
);
3118 my_friendly_assert (IS_AGGR_TYPE (type
), 220);
3120 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
3122 if (auto_delete
== integer_zero_node
)
3123 return void_zero_node
;
3125 return build_op_delete_call
3126 (DELETE_EXPR
, addr
, c_sizeof_nowarn (type
),
3127 LOOKUP_NORMAL
| (use_global_delete
* LOOKUP_GLOBAL
),
3131 /* Below, we will reverse the order in which these calls are made.
3132 If we have a destructor, then that destructor will take care
3133 of the base classes; otherwise, we must do that here. */
3134 if (TYPE_HAS_DESTRUCTOR (type
))
3136 tree passed_auto_delete
;
3137 tree do_delete
= NULL_TREE
;
3140 if (use_global_delete
)
3142 tree cond
= fold (build (BIT_AND_EXPR
, integer_type_node
,
3143 auto_delete
, integer_one_node
));
3144 tree call
= build_builtin_delete_call (addr
);
3146 cond
= fold (build (COND_EXPR
, void_type_node
, cond
,
3147 call
, void_zero_node
));
3148 if (cond
!= void_zero_node
)
3151 passed_auto_delete
= fold (build (BIT_AND_EXPR
, integer_type_node
,
3152 auto_delete
, integer_two_node
));
3155 passed_auto_delete
= auto_delete
;
3157 expr
= build_method_call
3158 (ref
, dtor_identifier
, build_tree_list (NULL_TREE
, passed_auto_delete
),
3162 expr
= build (COMPOUND_EXPR
, void_type_node
, expr
, do_delete
);
3164 if (flags
& LOOKUP_DESTRUCTOR
)
3165 /* Explicit destructor call; don't check for null pointer. */
3166 ifexp
= integer_one_node
;
3168 /* Handle deleting a null pointer. */
3169 ifexp
= fold (build_binary_op (NE_EXPR
, addr
, integer_zero_node
));
3171 if (ifexp
!= integer_one_node
)
3172 expr
= build (COND_EXPR
, void_type_node
,
3173 ifexp
, expr
, void_zero_node
);
3179 /* We only get here from finish_function for a destructor. */
3180 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (type
));
3181 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3182 tree base_binfo
= n_baseclasses
> 0 ? TREE_VEC_ELT (binfos
, 0) : NULL_TREE
;
3183 tree exprstmt
= NULL_TREE
;
3184 tree parent_auto_delete
= auto_delete
;
3187 /* Set this again before we call anything, as we might get called
3189 TYPE_HAS_DESTRUCTOR (type
) = 1;
3191 /* If we have member delete or vbases, we call delete in
3193 if (auto_delete
== integer_zero_node
)
3195 else if (base_binfo
== NULL_TREE
3196 || TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
)))
3198 cond
= build (COND_EXPR
, void_type_node
,
3199 build (BIT_AND_EXPR
, integer_type_node
, auto_delete
, integer_one_node
),
3200 build_builtin_delete_call (addr
),
3207 exprstmt
= build_tree_list (NULL_TREE
, cond
);
3210 && ! TREE_VIA_VIRTUAL (base_binfo
)
3211 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
)))
3213 tree this_auto_delete
;
3215 if (BINFO_OFFSET_ZEROP (base_binfo
))
3216 this_auto_delete
= parent_auto_delete
;
3218 this_auto_delete
= integer_zero_node
;
3220 expr
= build_scoped_method_call
3221 (ref
, base_binfo
, dtor_identifier
,
3222 build_tree_list (NULL_TREE
, this_auto_delete
));
3223 exprstmt
= tree_cons (NULL_TREE
, expr
, exprstmt
);
3226 /* Take care of the remaining baseclasses. */
3227 for (i
= 1; i
< n_baseclasses
; i
++)
3229 base_binfo
= TREE_VEC_ELT (binfos
, i
);
3230 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
))
3231 || TREE_VIA_VIRTUAL (base_binfo
))
3234 expr
= build_scoped_method_call
3235 (ref
, base_binfo
, dtor_identifier
,
3236 build_tree_list (NULL_TREE
, integer_zero_node
));
3238 exprstmt
= tree_cons (NULL_TREE
, expr
, exprstmt
);
3241 for (member
= TYPE_FIELDS (type
); member
; member
= TREE_CHAIN (member
))
3243 if (TREE_CODE (member
) != FIELD_DECL
)
3245 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member
)))
3247 tree this_member
= build_component_ref (ref
, DECL_NAME (member
), NULL_TREE
, 0);
3248 tree this_type
= TREE_TYPE (member
);
3249 expr
= build_delete (this_type
, this_member
, integer_two_node
, flags
, 0);
3250 exprstmt
= tree_cons (NULL_TREE
, expr
, exprstmt
);
3255 return build_compound_expr (exprstmt
);
3256 /* Virtual base classes make this function do nothing. */
3257 return void_zero_node
;
3261 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3264 build_vbase_delete (type
, decl
)
3267 tree vbases
= CLASSTYPE_VBASECLASSES (type
);
3268 tree result
= NULL_TREE
;
3269 tree addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
3271 my_friendly_assert (addr
!= error_mark_node
, 222);
3275 tree this_addr
= convert_force (build_pointer_type (BINFO_TYPE (vbases
)),
3277 result
= tree_cons (NULL_TREE
,
3278 build_delete (TREE_TYPE (this_addr
), this_addr
,
3280 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0),
3282 vbases
= TREE_CHAIN (vbases
);
3284 return build_compound_expr (nreverse (result
));
3287 /* Build a C++ vector delete expression.
3288 MAXINDEX is the number of elements to be deleted.
3289 ELT_SIZE is the nominal size of each element in the vector.
3290 BASE is the expression that should yield the store to be deleted.
3291 This function expands (or synthesizes) these calls itself.
3292 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3294 This also calls delete for virtual baseclasses of elements of the vector.
3296 Update: MAXINDEX is no longer needed. The size can be extracted from the
3297 start of the vector for pointers, and from the type for arrays. We still
3298 use MAXINDEX for arrays because it happens to already have one of the
3299 values we'd have to extract. (We could use MAXINDEX with pointers to
3300 confirm the size, and trap if the numbers differ; not clear that it'd
3301 be worth bothering.) */
3304 build_vec_delete (base
, maxindex
, auto_delete_vec
, use_global_delete
)
3305 tree base
, maxindex
;
3306 tree auto_delete_vec
;
3307 int use_global_delete
;
3311 if (TREE_CODE (base
) == OFFSET_REF
)
3312 base
= resolve_offset_ref (base
);
3314 type
= TREE_TYPE (base
);
3316 base
= stabilize_reference (base
);
3318 /* Since we can use base many times, save_expr it. */
3319 if (TREE_SIDE_EFFECTS (base
))
3320 base
= save_expr (base
);
3322 if (TREE_CODE (type
) == POINTER_TYPE
)
3324 /* Step back one from start of vector, and read dimension. */
3329 cookie_addr
= build (MINUS_EXPR
,
3330 build_pointer_type (sizetype
),
3332 TYPE_SIZE_UNIT (sizetype
));
3333 maxindex
= build_indirect_ref (cookie_addr
, NULL_PTR
);
3339 cookie_addr
= build (MINUS_EXPR
, build_pointer_type (BI_header_type
),
3340 base
, BI_header_size
);
3341 cookie
= build_indirect_ref (cookie_addr
, NULL_PTR
);
3342 maxindex
= build_component_ref (cookie
, nelts_identifier
,
3346 type
= strip_array_types (TREE_TYPE (type
));
3348 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3350 /* get the total number of things in the array, maxindex is a bad name */
3351 maxindex
= array_type_nelts_total (type
);
3352 type
= strip_array_types (type
);
3353 base
= build_unary_op (ADDR_EXPR
, base
, 1);
3357 if (base
!= error_mark_node
)
3358 error ("type to vector delete is neither pointer or array type");
3359 return error_mark_node
;
3362 return build_vec_delete_1 (base
, maxindex
, type
, auto_delete_vec
,