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
, special_function_kind
, int));
42 static void perform_member_init
PARAMS ((tree
, tree
, int));
43 static void sort_base_init
PARAMS ((tree
, 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
, tree
));
48 static tree initializing_context
PARAMS ((tree
));
49 static void expand_cleanup_for_base
PARAMS ((tree
, tree
));
50 static tree get_temp_regvar
PARAMS ((tree
, tree
));
51 static tree dfs_initialize_vtbl_ptrs
PARAMS ((tree
, void *));
52 static tree build_new_1
PARAMS ((tree
));
53 static tree get_cookie_size
PARAMS ((tree
));
54 static tree build_dtor_call
PARAMS ((tree
, special_function_kind
, int));
55 static tree build_field_list
PARAMS ((tree
, tree
, int *));
56 static tree build_vtbl_address
PARAMS ((tree
));
58 /* Set up local variable for this file. MUST BE CALLED AFTER
59 INIT_DECL_PROCESSING. */
61 static tree BI_header_type
;
63 void init_init_processing ()
67 minus_one_node
= build_int_2 (-1, -1);
69 /* Define the structure that holds header information for
70 arrays allocated via operator new. */
71 BI_header_type
= make_aggr_type (RECORD_TYPE
);
72 fields
[0] = build_decl (FIELD_DECL
, nelts_identifier
, sizetype
);
74 finish_builtin_type (BI_header_type
, "__new_cookie", fields
,
77 ggc_add_tree_root (&BI_header_type
, 1);
80 /* We are about to generate some complex initialization code.
81 Conceptually, it is all a single expression. However, we may want
82 to include conditionals, loops, and other such statement-level
83 constructs. Therefore, we build the initialization code inside a
84 statement-expression. This function starts such an expression.
85 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
86 pass them back to finish_init_stmts when the expression is
90 begin_init_stmts (stmt_expr_p
, compound_stmt_p
)
92 tree
*compound_stmt_p
;
94 if (building_stmt_tree ())
95 *stmt_expr_p
= begin_stmt_expr ();
97 *stmt_expr_p
= begin_global_stmt_expr ();
99 if (building_stmt_tree ())
100 *compound_stmt_p
= begin_compound_stmt (/*has_no_scope=*/1);
102 // *compound_stmt_p = genrtl_begin_compound_stmt (has_no_scope=1);
105 /* Finish out the statement-expression begun by the previous call to
106 begin_init_stmts. Returns the statement-expression itself. */
109 finish_init_stmts (stmt_expr
, compound_stmt
)
114 if (building_stmt_tree ())
115 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt
);
117 if (building_stmt_tree ())
118 stmt_expr
= finish_stmt_expr (stmt_expr
);
120 stmt_expr
= finish_global_stmt_expr (stmt_expr
);
122 /* To avoid spurious warnings about unused values, we set
125 TREE_USED (stmt_expr
) = 1;
132 /* Called from initialize_vtbl_ptrs via dfs_walk. */
135 dfs_initialize_vtbl_ptrs (binfo
, data
)
139 if (!BINFO_PRIMARY_MARKED_P (binfo
)
140 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
142 tree base_ptr
= TREE_VALUE ((tree
) data
);
144 if (TREE_VIA_VIRTUAL (binfo
))
145 base_ptr
= convert_pointer_to_vbase (BINFO_TYPE (binfo
),
149 = build_vbase_path (PLUS_EXPR
,
150 build_pointer_type (BINFO_TYPE (binfo
)),
155 expand_virtual_init (binfo
, base_ptr
);
158 SET_BINFO_MARKED (binfo
);
163 /* Initialize all the vtable pointers in the object pointed to by
167 initialize_vtbl_ptrs (addr
)
173 type
= TREE_TYPE (TREE_TYPE (addr
));
174 list
= build_tree_list (type
, addr
);
176 /* Walk through the hierarchy, initializing the vptr in each base
177 class. We do these in pre-order because under the new ABI we
178 can't find the virtual bases for a class until we've initialized
179 the vtbl for that class. */
180 dfs_walk_real (TYPE_BINFO (type
), dfs_initialize_vtbl_ptrs
,
181 NULL
, dfs_unmarked_real_bases_queue_p
, list
);
182 dfs_walk (TYPE_BINFO (type
), dfs_unmark
,
183 dfs_marked_real_bases_queue_p
, type
);
185 /* If we're not using thunks, we may need to adjust the deltas in
186 the vtable to handle virtual base classes correctly. When we are
187 using thunks, we either use construction vtables (which are
188 preloaded with the right answers) or nothing (in which case
189 vitual function calls sometimes don't work right.) */
190 if (TYPE_USES_VIRTUAL_BASECLASSES (type
) && !flag_vtable_thunks
)
191 fixup_all_virtual_upcast_offsets (addr
);
194 /* Subroutine of emit_base_init. */
197 perform_member_init (member
, init
, explicit)
202 tree type
= TREE_TYPE (member
);
204 decl
= build_component_ref (current_class_ref
, member
, NULL_TREE
, explicit);
206 if (decl
== error_mark_node
)
209 /* Deal with this here, as we will get confused if we try to call the
210 assignment op for an anonymous union. This can happen in a
211 synthesized copy constructor. */
212 if (ANON_AGGR_TYPE_P (type
))
216 init
= build (INIT_EXPR
, type
, decl
, TREE_VALUE (init
));
217 finish_expr_stmt (init
);
220 else if (TYPE_NEEDS_CONSTRUCTING (type
)
221 || (init
&& TYPE_HAS_CONSTRUCTOR (type
)))
223 /* Since `init' is already a TREE_LIST on the member_init_list,
224 only build it into one if we aren't already a list. */
225 if (init
!= NULL_TREE
&& TREE_CODE (init
) != TREE_LIST
)
226 init
= build_tree_list (NULL_TREE
, init
);
229 && TREE_CODE (type
) == ARRAY_TYPE
231 && TREE_CHAIN (init
) == NULL_TREE
232 && TREE_CODE (TREE_TYPE (TREE_VALUE (init
))) == ARRAY_TYPE
)
234 /* Initialization of one array from another. */
236 (build_vec_init (TREE_OPERAND (decl
, 1), decl
,
237 array_type_nelts (type
), TREE_VALUE (init
), 1));
240 finish_expr_stmt (build_aggr_init (decl
, init
, 0));
244 if (init
== NULL_TREE
)
248 /* default-initialization. */
249 if (AGGREGATE_TYPE_P (type
))
251 /* This is a default initialization of an aggregate,
252 but not one of non-POD class type. We cleverly
253 notice that the initialization rules in such a
254 case are the same as for initialization with an
255 empty brace-initialization list. We don't want
256 to call build_modify_expr as that will go looking
257 for constructors and such. */
258 tree e
= build (CONSTRUCTOR
, type
, NULL_TREE
, NULL_TREE
);
259 TREE_SIDE_EFFECTS (e
) = 1;
260 finish_expr_stmt (build (INIT_EXPR
, type
, decl
, e
));
262 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
263 cp_error ("default-initialization of `%#D', which has reference type",
266 init
= integer_zero_node
;
268 /* member traversal: note it leaves init NULL */
269 else if (TREE_CODE (TREE_TYPE (member
)) == REFERENCE_TYPE
)
270 cp_pedwarn ("uninitialized reference member `%D'", member
);
272 else if (TREE_CODE (init
) == TREE_LIST
)
274 /* There was an explicit member initialization. Do some
275 work in that case. */
276 if (TREE_CHAIN (init
))
278 warning ("initializer list treated as compound expression");
279 init
= build_compound_expr (init
);
282 init
= TREE_VALUE (init
);
286 finish_expr_stmt (build_modify_expr (decl
, INIT_EXPR
, init
));
289 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
293 expr
= build_component_ref (current_class_ref
, member
, NULL_TREE
,
295 expr
= build_delete (type
, expr
, sfk_complete_destructor
,
296 LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0);
298 if (expr
!= error_mark_node
)
299 finish_subobject (expr
);
303 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
304 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
307 build_field_list (t
, list
, uses_unions_p
)
314 /* Note whether or not T is a union. */
315 if (TREE_CODE (t
) == UNION_TYPE
)
318 for (fields
= TYPE_FIELDS (t
); fields
; fields
= TREE_CHAIN (fields
))
320 /* Skip CONST_DECLs for enumeration constants and so forth. */
321 if (TREE_CODE (fields
) != FIELD_DECL
)
324 /* Keep track of whether or not any fields are unions. */
325 if (TREE_CODE (TREE_TYPE (fields
)) == UNION_TYPE
)
328 /* For an anonymous struct or union, we must recursively
329 consider the fields of the anonymous type. They can be
330 directly initialized from the constructor. */
331 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
333 /* Add this field itself. Synthesized copy constructors
334 initialize the entire aggregate. */
335 list
= tree_cons (fields
, NULL_TREE
, list
);
336 /* And now add the fields in the anonymous aggregate. */
337 list
= build_field_list (TREE_TYPE (fields
), list
,
340 /* Add this field. */
341 else if (DECL_NAME (fields
))
342 list
= tree_cons (fields
, NULL_TREE
, list
);
348 /* The MEMBER_INIT_LIST is a TREE_LIST. The TREE_PURPOSE of each list
349 gives a FIELD_DECL in T that needs initialization. The TREE_VALUE
350 gives the initializer, or list of initializer arguments. Sort the
351 MEMBER_INIT_LIST, returning a version that contains the same
352 information but in the order that the fields should actually be
353 initialized. Perform error-checking in the process. */
356 sort_member_init (t
, member_init_list
)
358 tree member_init_list
;
365 /* Build up a list of the various fields, in sorted order. */
366 init_list
= nreverse (build_field_list (t
, NULL_TREE
, &uses_unions_p
));
368 /* Go through the explicit initializers, adding them to the
370 last_field
= init_list
;
371 for (init
= member_init_list
; init
; init
= TREE_CHAIN (init
))
374 tree initialized_field
;
376 initialized_field
= TREE_PURPOSE (init
);
377 my_friendly_assert (TREE_CODE (initialized_field
) == FIELD_DECL
,
380 /* If the explicit initializers are in sorted order, then the
381 INITIALIZED_FIELD will be for a field following the
383 for (f
= last_field
; f
; f
= TREE_CHAIN (f
))
384 if (TREE_PURPOSE (f
) == initialized_field
)
387 /* Give a warning, if appropriate. */
388 if (warn_reorder
&& !f
)
390 cp_warning_at ("member initializers for `%#D'",
391 TREE_PURPOSE (last_field
));
392 cp_warning_at (" and `%#D'", initialized_field
);
393 warning (" will be re-ordered to match declaration order");
396 /* Look again, from the beginning of the list. We must find the
397 field on this loop. */
401 while (TREE_PURPOSE (f
) != initialized_field
)
405 /* If there was already an explicit initializer for this field,
408 cp_error ("multiple initializations given for member `%D'",
412 /* Mark the field as explicitly initialized. */
413 TREE_TYPE (f
) = error_mark_node
;
414 /* And insert the initializer. */
415 TREE_VALUE (f
) = TREE_VALUE (init
);
418 /* Remember the location of the last explicitly initialized
425 If a ctor-initializer specifies more than one mem-initializer for
426 multiple members of the same union (including members of
427 anonymous unions), the ctor-initializer is ill-formed. */
430 last_field
= NULL_TREE
;
431 for (init
= init_list
; init
; init
= TREE_CHAIN (init
))
437 /* Skip uninitialized members. */
438 if (!TREE_TYPE (init
))
440 /* See if this field is a member of a union, or a member of a
441 structure contained in a union, etc. */
442 field
= TREE_PURPOSE (init
);
443 for (field_type
= DECL_CONTEXT (field
);
444 !same_type_p (field_type
, t
);
445 field_type
= TYPE_CONTEXT (field_type
))
446 if (TREE_CODE (field_type
) == UNION_TYPE
)
448 /* If this field is not a member of a union, skip it. */
449 if (TREE_CODE (field_type
) != UNION_TYPE
)
452 /* It's only an error if we have two initializers for the same
460 /* See if LAST_FIELD and the field initialized by INIT are
461 members of the same union. If so, there's a problem,
462 unless they're actually members of the same structure
463 which is itself a member of a union. For example, given:
465 union { struct { int i; int j; }; };
467 initializing both `i' and `j' makes sense. */
468 field_type
= DECL_CONTEXT (field
);
472 tree last_field_type
;
474 last_field_type
= DECL_CONTEXT (last_field
);
477 if (same_type_p (last_field_type
, field_type
))
479 if (TREE_CODE (field_type
) == UNION_TYPE
)
480 cp_error ("initializations for multiple members of `%T'",
486 if (same_type_p (last_field_type
, t
))
489 last_field_type
= TYPE_CONTEXT (last_field_type
);
492 /* If we've reached the outermost class, then we're
494 if (same_type_p (field_type
, t
))
497 field_type
= TYPE_CONTEXT (field_type
);
508 /* Like sort_member_init, but used for initializers of base classes.
509 *RBASE_PTR is filled in with the initializers for non-virtual bases;
510 vbase_ptr gets the virtual bases. */
513 sort_base_init (t
, base_init_list
, rbase_ptr
, vbase_ptr
)
516 tree
*rbase_ptr
, *vbase_ptr
;
518 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
519 int n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
525 /* For warn_reorder. */
527 tree last_base
= NULL_TREE
;
529 tree rbases
= NULL_TREE
;
530 tree vbases
= NULL_TREE
;
532 /* First walk through and splice out vbase and invalid initializers.
533 Also replace names with binfos. */
535 last
= tree_cons (NULL_TREE
, NULL_TREE
, base_init_list
);
536 for (x
= TREE_CHAIN (last
); x
; x
= TREE_CHAIN (x
))
538 tree basetype
= TREE_PURPOSE (x
);
539 tree binfo
= NULL_TREE
;
541 if (basetype
== NULL_TREE
)
543 /* Initializer for single base class. Must not
544 use multiple inheritance or this is ambiguous. */
545 switch (n_baseclasses
)
548 cp_error ("`%T' does not have a base class to initialize",
554 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
558 binfo
= TREE_VEC_ELT (binfos
, 0);
560 else if (is_aggr_type (basetype
, 1))
562 binfo
= binfo_or_else (basetype
, t
);
563 if (binfo
== NULL_TREE
)
566 /* Virtual base classes are special cases. Their initializers
567 are recorded with this constructor, and they are used when
568 this constructor is the top-level constructor called. */
569 if (TREE_VIA_VIRTUAL (binfo
))
571 tree v
= binfo_for_vbase (BINFO_TYPE (binfo
), t
);
572 vbases
= tree_cons (v
, TREE_VALUE (x
), vbases
);
577 /* Otherwise, if it is not an immediate base class, complain. */
578 for (i
= n_baseclasses
-1; i
>= 0; i
--)
579 if (BINFO_TYPE (binfo
) == BINFO_TYPE (TREE_VEC_ELT (binfos
, i
)))
583 cp_error ("`%T' is not an immediate base class of `%T'",
584 basetype
, current_class_type
);
590 my_friendly_abort (365);
592 TREE_PURPOSE (x
) = binfo
;
593 TREE_CHAIN (last
) = x
;
596 TREE_CHAIN (last
) = NULL_TREE
;
598 /* Now walk through our regular bases and make sure they're initialized. */
600 for (i
= 0; i
< n_baseclasses
; ++i
)
602 /* The base for which we're currently initializing. */
603 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
604 /* The initializer for BASE_BINFO. */
608 if (TREE_VIA_VIRTUAL (base_binfo
))
611 /* We haven't found the BASE_BINFO yet. */
613 /* Loop through all the explicitly initialized bases, looking
614 for an appropriate initializer. */
615 for (x
= base_init_list
, pos
= 0; x
; x
= TREE_CHAIN (x
), ++pos
)
617 tree binfo
= TREE_PURPOSE (x
);
619 if (binfo
== base_binfo
&& !init
)
625 cp_warning_at ("base initializers for `%#T'", last_base
);
626 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo
));
627 warning (" will be re-ordered to match inheritance order");
630 last_base
= BINFO_TYPE (binfo
);
633 /* Make sure we won't try to work on this init again. */
634 TREE_PURPOSE (x
) = NULL_TREE
;
635 init
= build_tree_list (binfo
, TREE_VALUE (x
));
637 else if (binfo
== base_binfo
)
639 cp_error ("base class `%T' already initialized",
645 /* If we didn't find BASE_BINFO in the list, create a dummy entry
646 so the two lists (RBASES and the list of bases) will be
649 init
= build_tree_list (NULL_TREE
, NULL_TREE
);
650 rbases
= chainon (rbases
, init
);
657 /* Perform whatever initializations have yet to be done on the base
658 class, and non-static data members, of the CURRENT_CLASS_TYPE.
659 These actions are given by the BASE_INIT_LIST and MEM_INIT_LIST,
662 If there is a need for a call to a constructor, we must surround
663 that call with a pushlevel/poplevel pair, since we are technically
664 at the PARM level of scope.
666 Note that emit_base_init does *not* initialize virtual base
667 classes. That is done specially, elsewhere. */
670 emit_base_init (mem_init_list
, base_init_list
)
675 tree rbase_init_list
, vbase_init_list
;
676 tree t
= current_class_type
;
677 tree t_binfo
= TYPE_BINFO (t
);
678 tree binfos
= BINFO_BASETYPES (t_binfo
);
680 int n_baseclasses
= BINFO_N_BASETYPES (t_binfo
);
682 mem_init_list
= sort_member_init (t
, mem_init_list
);
683 sort_base_init (t
, base_init_list
, &rbase_init_list
, &vbase_init_list
);
685 /* First, initialize the virtual base classes, if we are
686 constructing the most-derived object. */
687 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
689 tree first_arg
= TREE_CHAIN (DECL_ARGUMENTS (current_function_decl
));
690 construct_virtual_bases (t
, current_class_ref
, current_class_ptr
,
691 vbase_init_list
, first_arg
);
694 /* Now, perform initialization of non-virtual base classes. */
695 for (i
= 0; i
< n_baseclasses
; i
++)
697 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
698 tree init
= void_list_node
;
700 if (TREE_VIA_VIRTUAL (base_binfo
))
703 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo
) == t_binfo
,
706 if (TREE_PURPOSE (rbase_init_list
))
707 init
= TREE_VALUE (rbase_init_list
);
708 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo
)))
712 && DECL_COPY_CONSTRUCTOR_P (current_function_decl
))
713 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
714 BINFO_TYPE (base_binfo
));
717 if (init
!= void_list_node
)
719 member
= convert_pointer_to_real (base_binfo
, current_class_ptr
);
720 expand_aggr_init_1 (base_binfo
, NULL_TREE
,
721 build_indirect_ref (member
, NULL_PTR
), init
,
725 expand_cleanup_for_base (base_binfo
, NULL_TREE
);
726 rbase_init_list
= TREE_CHAIN (rbase_init_list
);
729 /* Initialize the vtable pointers for the class. */
730 initialize_vtbl_ptrs (current_class_ptr
);
732 while (mem_init_list
)
738 member
= TREE_PURPOSE (mem_init_list
);
740 /* See if we had a user-specified member initialization. */
741 if (TREE_TYPE (mem_init_list
))
743 init
= TREE_VALUE (mem_init_list
);
748 init
= DECL_INITIAL (member
);
751 /* Effective C++ rule 12. */
752 if (warn_ecpp
&& init
== NULL_TREE
753 && !DECL_ARTIFICIAL (member
)
754 && TREE_CODE (TREE_TYPE (member
)) != ARRAY_TYPE
)
755 cp_warning ("`%D' should be initialized in the member initialization list", member
);
758 perform_member_init (member
, init
, from_init_list
);
759 mem_init_list
= TREE_CHAIN (mem_init_list
);
763 /* Returns the address of the vtable (i.e., the value that should be
764 assigned to the vptr) for BINFO. */
767 build_vtbl_address (binfo
)
772 /* Figure out what vtable BINFO's vtable is based on, and mark it as
774 vtbl
= get_vtbl_decl_for_binfo (binfo
);
775 assemble_external (vtbl
);
776 TREE_USED (vtbl
) = 1;
778 /* Now compute the address to use when initializing the vptr. */
779 vtbl
= BINFO_VTABLE (binfo
);
780 if (TREE_CODE (vtbl
) == VAR_DECL
)
782 vtbl
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (vtbl
)), vtbl
);
783 TREE_CONSTANT (vtbl
) = 1;
789 /* This code sets up the virtual function tables appropriate for
790 the pointer DECL. It is a one-ply initialization.
792 BINFO is the exact type that DECL is supposed to be. In
793 multiple inheritance, this might mean "C's A" if C : A, B. */
796 expand_virtual_init (binfo
, decl
)
799 tree type
= BINFO_TYPE (binfo
);
801 tree vtype
, vtype_binfo
;
804 /* Compute the location of the vtable. */
805 vtype
= DECL_CONTEXT (TYPE_VFIELD (type
));
806 vtype_binfo
= get_binfo (vtype
, TREE_TYPE (TREE_TYPE (decl
)), 0);
808 /* Compute the initializer for vptr. */
809 vtbl
= build_vtbl_address (binfo
);
811 /* Under the new ABI, we may get this vptr from a VTT, if this is a
812 subobject constructor or subobject destructor. */
813 vtt_index
= BINFO_VPTR_INDEX (binfo
);
819 /* Compute the value to use, when there's a VTT. */
820 vtt_parm
= DECL_VTT_PARM (current_function_decl
);
821 vtbl2
= build (PLUS_EXPR
,
822 TREE_TYPE (vtt_parm
),
825 vtbl2
= build1 (INDIRECT_REF
, TREE_TYPE (vtbl
), vtbl2
);
827 /* The actual initializer is the VTT value only in the subobject
828 constructor. In maybe_clone_body we'll substitute NULL for
829 the vtt_parm in the case of the non-subobject constructor. */
830 vtbl
= build (COND_EXPR
,
832 DECL_USE_VTT_PARM (current_function_decl
),
837 /* Compute the location of the vtpr. */
838 decl
= convert_pointer_to_real (vtype_binfo
, decl
);
839 vtbl_ptr
= build_vfield_ref (build_indirect_ref (decl
, NULL_PTR
), vtype
);
840 if (vtbl_ptr
== error_mark_node
)
843 /* Assign the vtable to the vptr. */
844 vtbl
= convert_force (TREE_TYPE (vtbl_ptr
), vtbl
, 0);
845 finish_expr_stmt (build_modify_expr (vtbl_ptr
, NOP_EXPR
, vtbl
));
848 /* If an exception is thrown in a constructor, those base classes already
849 constructed must be destroyed. This function creates the cleanup
850 for BINFO, which has just been constructed. If FLAG is non-NULL,
851 it is a DECL which is non-zero when this base needs to be
855 expand_cleanup_for_base (binfo
, flag
)
861 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo
)))
864 /* Call the destructor. */
865 expr
= (build_scoped_method_call
866 (current_class_ref
, binfo
, base_dtor_identifier
, NULL_TREE
));
868 expr
= fold (build (COND_EXPR
, void_type_node
,
869 truthvalue_conversion (flag
),
870 expr
, integer_zero_node
));
872 finish_subobject (expr
);
875 /* Subroutine of `expand_aggr_vbase_init'.
876 BINFO is the binfo of the type that is being initialized.
877 INIT_LIST is the list of initializers for the virtual baseclass. */
880 expand_aggr_vbase_init_1 (binfo
, exp
, addr
, init_list
)
881 tree binfo
, exp
, addr
, init_list
;
883 tree init
= purpose_member (binfo
, init_list
);
884 tree ref
= build_indirect_ref (addr
, NULL_PTR
);
887 init
= TREE_VALUE (init
);
888 /* Call constructors, but don't set up vtables. */
889 expand_aggr_init_1 (binfo
, exp
, ref
, init
, LOOKUP_COMPLAIN
);
892 /* Construct the virtual base-classes of THIS_REF (whose address is
893 THIS_PTR). The object has the indicated TYPE. The construction
894 actually takes place only if FLAG is non-zero. INIT_LIST is list
895 of initializations for constructors to perform. */
898 construct_virtual_bases (type
, this_ref
, this_ptr
, init_list
, flag
)
907 /* If there are no virtual baseclasses, we shouldn't even be here. */
908 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type
), 19990621);
910 /* First set the pointers in our object that tell us where to find
911 our virtual baseclasses. */
912 if (!vbase_offsets_in_vtable_p ())
917 if_stmt
= begin_if_stmt ();
918 finish_if_stmt_cond (flag
, if_stmt
);
919 result
= init_vbase_pointers (type
, this_ptr
);
921 finish_expr_stmt (build_compound_expr (result
));
922 finish_then_clause (if_stmt
);
926 /* Now, run through the baseclasses, initializing each. */
927 for (vbases
= CLASSTYPE_VBASECLASSES (type
); vbases
;
928 vbases
= TREE_CHAIN (vbases
))
935 /* If there are virtual base classes with destructors, we need to
936 emit cleanups to destroy them if an exception is thrown during
937 the construction process. These exception regions (i.e., the
938 period during which the cleanups must occur) begin from the time
939 the construction is complete to the end of the function. If we
940 create a conditional block in which to initialize the
941 base-classes, then the cleanup region for the virtual base begins
942 inside a block, and ends outside of that block. This situation
943 confuses the sjlj exception-handling code. Therefore, we do not
944 create a single conditional block, but one for each
945 initialization. (That way the cleanup regions always begin
946 in the outer block.) We trust the back-end to figure out
947 that the FLAG will not change across initializations, and
948 avoid doing multiple tests. */
949 inner_if_stmt
= begin_if_stmt ();
950 finish_if_stmt_cond (flag
, inner_if_stmt
);
951 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/1);
953 /* Compute the location of the virtual base. If we're
954 constructing virtual bases, then we must be the most derived
955 class. Therefore, we don't have to look up the virtual base;
956 we already know where it is. */
957 vbase
= TREE_VALUE (vbases
);
958 exp
= build (PLUS_EXPR
,
959 TREE_TYPE (this_ptr
),
961 fold (build1 (NOP_EXPR
, TREE_TYPE (this_ptr
),
962 BINFO_OFFSET (vbase
))));
963 exp
= build1 (NOP_EXPR
,
964 build_pointer_type (BINFO_TYPE (vbase
)),
967 expand_aggr_vbase_init_1 (vbase
, this_ref
, exp
, init_list
);
968 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt
);
969 finish_then_clause (inner_if_stmt
);
972 expand_cleanup_for_base (vbase
, flag
);
976 /* Find the context in which this FIELD can be initialized. */
979 initializing_context (field
)
982 tree t
= DECL_CONTEXT (field
);
984 /* Anonymous union members can be initialized in the first enclosing
985 non-anonymous union context. */
986 while (t
&& ANON_AGGR_TYPE_P (t
))
987 t
= TYPE_CONTEXT (t
);
991 /* Function to give error message if member initialization specification
992 is erroneous. FIELD is the member we decided to initialize.
993 TYPE is the type for which the initialization is being performed.
994 FIELD must be a member of TYPE.
996 MEMBER_NAME is the name of the member. */
999 member_init_ok_or_else (field
, type
, member_name
)
1002 const char *member_name
;
1004 if (field
== error_mark_node
)
1006 if (field
== NULL_TREE
|| initializing_context (field
) != type
)
1008 cp_error ("class `%T' does not have any field named `%s'", type
,
1012 if (TREE_STATIC (field
))
1014 cp_error ("field `%#D' is static; only point of initialization is its declaration",
1022 /* If NAME is a viable field name for the aggregate DECL,
1023 and PARMS is a viable parameter list, then expand an _EXPR
1024 which describes this initialization.
1026 Note that we do not need to chase through the class's base classes
1027 to look for NAME, because if it's in that list, it will be handled
1028 by the constructor for that base class.
1030 We do not yet have a fixed-point finder to instantiate types
1031 being fed to overloaded constructors. If there is a unique
1032 constructor, then argument types can be got from that one. */
1035 expand_member_init (exp
, name
, init
)
1036 tree exp
, name
, init
;
1038 tree basetype
= NULL_TREE
, field
;
1041 if (exp
== NULL_TREE
)
1044 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
1046 if (name
&& TYPE_P (name
))
1049 name
= TYPE_IDENTIFIER (name
);
1051 else if (name
&& TREE_CODE (name
) == TYPE_DECL
)
1053 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (name
));
1054 name
= DECL_NAME (name
);
1057 if (name
== NULL_TREE
&& IS_AGGR_TYPE (type
))
1058 switch (CLASSTYPE_N_BASECLASSES (type
))
1061 error ("base class initializer specified, but no base class to initialize");
1064 basetype
= TYPE_BINFO_BASETYPE (type
, 0);
1067 error ("initializer for unnamed base class ambiguous");
1068 cp_error ("(type `%T' uses multiple inheritance)", type
);
1072 my_friendly_assert (init
!= NULL_TREE
, 0);
1074 /* The grammar should not allow fields which have names that are
1075 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
1076 may assume that this is an attempt to initialize a base class
1077 member of the current type. Otherwise, it is an attempt to
1078 initialize a member field. */
1080 if (init
== void_type_node
)
1083 if (name
== NULL_TREE
|| basetype
)
1085 if (name
== NULL_TREE
)
1089 name
= TYPE_IDENTIFIER (basetype
);
1092 error ("no base class to initialize");
1097 else if (basetype
!= type
1098 && ! current_template_parms
1099 && ! vec_binfo_member (basetype
,
1100 TYPE_BINFO_BASETYPES (type
))
1101 && ! binfo_for_vbase (basetype
, type
))
1103 if (IDENTIFIER_CLASS_VALUE (name
))
1105 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
1106 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
1109 cp_error ("type `%T' is not an immediate basetype for `%T'",
1114 init
= build_tree_list (basetype
, init
);
1119 field
= lookup_field (type
, name
, 1, 0);
1121 if (! member_init_ok_or_else (field
, type
, IDENTIFIER_POINTER (name
)))
1124 init
= build_tree_list (field
, init
);
1130 /* This is like `expand_member_init', only it stores one aggregate
1133 INIT comes in two flavors: it is either a value which
1134 is to be stored in EXP, or it is a parameter list
1135 to go to a constructor, which will operate on EXP.
1136 If INIT is not a parameter list for a constructor, then set
1137 LOOKUP_ONLYCONVERTING.
1138 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1139 the initializer, if FLAGS is 0, then it is the (init) form.
1140 If `init' is a CONSTRUCTOR, then we emit a warning message,
1141 explaining that such initializations are invalid.
1143 If INIT resolves to a CALL_EXPR which happens to return
1144 something of the type we are looking for, then we know
1145 that we can safely use that call to perform the
1148 The virtual function table pointer cannot be set up here, because
1149 we do not really know its type.
1151 Virtual baseclass pointers are also set up here.
1153 This never calls operator=().
1155 When initializing, nothing is CONST.
1157 A default copy constructor may have to be used to perform the
1160 A constructor or a conversion operator may have to be used to
1161 perform the initialization, but not both, as it would be ambiguous. */
1164 build_aggr_init (exp
, init
, flags
)
1171 tree type
= TREE_TYPE (exp
);
1172 int was_const
= TREE_READONLY (exp
);
1173 int was_volatile
= TREE_THIS_VOLATILE (exp
);
1175 if (init
== error_mark_node
)
1176 return error_mark_node
;
1178 TREE_READONLY (exp
) = 0;
1179 TREE_THIS_VOLATILE (exp
) = 0;
1181 if (init
&& TREE_CODE (init
) != TREE_LIST
)
1182 flags
|= LOOKUP_ONLYCONVERTING
;
1184 if (TREE_CODE (type
) == ARRAY_TYPE
)
1186 /* Must arrange to initialize each element of EXP
1187 from elements of INIT. */
1188 tree itype
= init
? TREE_TYPE (init
) : NULL_TREE
;
1192 /* Handle bad initializers like:
1196 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1200 int main(int argc, char **argv) {
1201 COMPLEX zees(1.0, 0.0)[10];
1204 cp_error ("bad array initializer");
1205 return error_mark_node
;
1207 if (CP_TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
1209 TREE_TYPE (exp
) = TYPE_MAIN_VARIANT (type
);
1211 TREE_TYPE (init
) = TYPE_MAIN_VARIANT (itype
);
1213 stmt_expr
= build_vec_init (exp
, exp
, array_type_nelts (type
), init
,
1214 init
&& same_type_p (TREE_TYPE (init
),
1216 TREE_READONLY (exp
) = was_const
;
1217 TREE_THIS_VOLATILE (exp
) = was_volatile
;
1218 TREE_TYPE (exp
) = type
;
1220 TREE_TYPE (init
) = itype
;
1224 if (TREE_CODE (exp
) == VAR_DECL
|| TREE_CODE (exp
) == PARM_DECL
)
1225 /* just know that we've seen something for this node */
1226 TREE_USED (exp
) = 1;
1228 TREE_TYPE (exp
) = TYPE_MAIN_VARIANT (type
);
1229 begin_init_stmts (&stmt_expr
, &compound_stmt
);
1230 destroy_temps
= stmts_are_full_exprs_p ();
1231 current_stmt_tree
->stmts_are_full_exprs_p
= 0;
1232 expand_aggr_init_1 (TYPE_BINFO (type
), exp
, exp
,
1233 init
, LOOKUP_NORMAL
|flags
);
1234 stmt_expr
= finish_init_stmts (stmt_expr
, compound_stmt
);
1235 current_stmt_tree
->stmts_are_full_exprs_p
= destroy_temps
;
1236 TREE_TYPE (exp
) = type
;
1237 TREE_READONLY (exp
) = was_const
;
1238 TREE_THIS_VOLATILE (exp
) = was_volatile
;
1244 expand_default_init (binfo
, true_exp
, exp
, init
, flags
)
1250 tree type
= TREE_TYPE (exp
);
1253 /* It fails because there may not be a constructor which takes
1254 its own type as the first (or only parameter), but which does
1255 take other types via a conversion. So, if the thing initializing
1256 the expression is a unit element of type X, first try X(X&),
1257 followed by initialization by X. If neither of these work
1258 out, then look hard. */
1262 if (init
&& TREE_CODE (init
) != TREE_LIST
1263 && (flags
& LOOKUP_ONLYCONVERTING
))
1265 /* Base subobjects should only get direct-initialization. */
1266 if (true_exp
!= exp
)
1269 if (flags
& DIRECT_BIND
)
1270 /* Do nothing. We hit this in two cases: Reference initialization,
1271 where we aren't initializing a real variable, so we don't want
1272 to run a new constructor; and catching an exception, where we
1273 have already built up the constructor call so we could wrap it
1274 in an exception region. */;
1275 else if (TREE_CODE (init
) == CONSTRUCTOR
)
1276 /* A brace-enclosed initializer has whatever type is
1277 required. There's no need to convert it. */
1280 init
= ocp_convert (type
, init
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
1282 if (TREE_CODE (init
) == TRY_CATCH_EXPR
)
1283 /* We need to protect the initialization of a catch parm
1284 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1285 around the TARGET_EXPR for the copy constructor. See
1286 expand_start_catch_block. */
1287 TREE_OPERAND (init
, 0) = build (INIT_EXPR
, TREE_TYPE (exp
), exp
,
1288 TREE_OPERAND (init
, 0));
1290 init
= build (INIT_EXPR
, TREE_TYPE (exp
), exp
, init
);
1291 TREE_SIDE_EFFECTS (init
) = 1;
1292 finish_expr_stmt (init
);
1296 if (init
== NULL_TREE
1297 || (TREE_CODE (init
) == TREE_LIST
&& ! TREE_TYPE (init
)))
1301 init
= TREE_VALUE (parms
);
1304 parms
= build_tree_list (NULL_TREE
, init
);
1306 if (true_exp
== exp
)
1307 ctor_name
= complete_ctor_identifier
;
1309 ctor_name
= base_ctor_identifier
;
1311 rval
= build_method_call (exp
, ctor_name
, parms
, binfo
, flags
);
1312 if (TREE_SIDE_EFFECTS (rval
))
1314 if (building_stmt_tree ())
1315 finish_expr_stmt (rval
);
1317 genrtl_expr_stmt (rval
);
1321 /* This function is responsible for initializing EXP with INIT
1324 BINFO is the binfo of the type for who we are performing the
1325 initialization. For example, if W is a virtual base class of A and B,
1327 If we are initializing B, then W must contain B's W vtable, whereas
1328 were we initializing C, W must contain C's W vtable.
1330 TRUE_EXP is nonzero if it is the true expression being initialized.
1331 In this case, it may be EXP, or may just contain EXP. The reason we
1332 need this is because if EXP is a base element of TRUE_EXP, we
1333 don't necessarily know by looking at EXP where its virtual
1334 baseclass fields should really be pointing. But we do know
1335 from TRUE_EXP. In constructors, we don't know anything about
1336 the value being initialized.
1338 FLAGS is just passes to `build_method_call'. See that function for
1342 expand_aggr_init_1 (binfo
, true_exp
, exp
, init
, flags
)
1348 tree type
= TREE_TYPE (exp
);
1350 my_friendly_assert (init
!= error_mark_node
&& type
!= error_mark_node
, 211);
1352 /* Use a function returning the desired type to initialize EXP for us.
1353 If the function is a constructor, and its first argument is
1354 NULL_TREE, know that it was meant for us--just slide exp on
1355 in and expand the constructor. Constructors now come
1358 if (init
&& TREE_CODE (exp
) == VAR_DECL
1359 && TREE_CODE (init
) == CONSTRUCTOR
1360 && TREE_HAS_CONSTRUCTOR (init
))
1362 /* If store_init_value returns NULL_TREE, the INIT has been
1363 record in the DECL_INITIAL for EXP. That means there's
1364 nothing more we have to do. */
1365 if (!store_init_value (exp
, init
))
1367 if (!building_stmt_tree ())
1368 expand_decl_init (exp
);
1371 finish_expr_stmt (build (INIT_EXPR
, type
, exp
, init
));
1375 /* We know that expand_default_init can handle everything we want
1377 expand_default_init (binfo
, true_exp
, exp
, init
, flags
);
1380 /* Report an error if TYPE is not a user-defined, aggregate type. If
1381 OR_ELSE is nonzero, give an error message. */
1384 is_aggr_type (type
, or_else
)
1388 if (type
== error_mark_node
)
1391 if (! IS_AGGR_TYPE (type
)
1392 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
1393 && TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
)
1396 cp_error ("`%T' is not an aggregate type", type
);
1402 /* Like is_aggr_typedef, but returns typedef if successful. */
1405 get_aggr_from_typedef (name
, or_else
)
1411 if (name
== error_mark_node
)
1414 if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1415 type
= IDENTIFIER_TYPE_VALUE (name
);
1419 cp_error ("`%T' fails to be an aggregate typedef", name
);
1423 if (! IS_AGGR_TYPE (type
)
1424 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
1425 && TREE_CODE (type
) != TEMPLATE_TEMPLATE_PARM
)
1428 cp_error ("type `%T' is of non-aggregate type", type
);
1435 get_type_value (name
)
1438 if (name
== error_mark_node
)
1441 if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1442 return IDENTIFIER_TYPE_VALUE (name
);
1448 /* This code could just as well go in `class.c', but is placed here for
1451 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1452 the appropriate function call. */
1455 build_member_call (type
, name
, parmlist
)
1456 tree type
, name
, parmlist
;
1461 tree basetype_path
, decl
;
1463 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
1464 && TREE_CODE (type
) == NAMESPACE_DECL
)
1466 /* 'name' already refers to the decls from the namespace, since we
1467 hit do_identifier for template_ids. */
1468 method_name
= TREE_OPERAND (name
, 0);
1469 /* FIXME: Since we don't do independent names right yet, the
1470 name might also be a LOOKUP_EXPR. Once we resolve this to a
1471 real decl earlier, this can go. This may happen during
1473 if (TREE_CODE (method_name
) == LOOKUP_EXPR
)
1475 method_name
= lookup_namespace_name
1476 (type
, TREE_OPERAND (method_name
, 0));
1477 TREE_OPERAND (name
, 0) = method_name
;
1479 my_friendly_assert (is_overloaded_fn (method_name
), 980519);
1480 return build_x_function_call (name
, parmlist
, current_class_ref
);
1484 name
= DECL_NAME (name
);
1486 if (type
== std_node
)
1487 return build_x_function_call (do_scoped_id (name
, 0), parmlist
,
1489 if (TREE_CODE (type
) == NAMESPACE_DECL
)
1490 return build_x_function_call (lookup_namespace_name (type
, name
),
1491 parmlist
, current_class_ref
);
1493 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
1495 method_name
= TREE_OPERAND (name
, 0);
1496 if (TREE_CODE (method_name
) == COMPONENT_REF
)
1497 method_name
= TREE_OPERAND (method_name
, 1);
1498 if (is_overloaded_fn (method_name
))
1499 method_name
= DECL_NAME (OVL_CURRENT (method_name
));
1500 TREE_OPERAND (name
, 0) = method_name
;
1505 if (TREE_CODE (method_name
) == BIT_NOT_EXPR
)
1507 method_name
= TREE_OPERAND (method_name
, 0);
1511 /* This shouldn't be here, and build_member_call shouldn't appear in
1513 if (type
&& TREE_CODE (type
) == IDENTIFIER_NODE
1514 && get_aggr_from_typedef (type
, 0) == 0)
1516 tree ns
= lookup_name (type
, 0);
1517 if (ns
&& TREE_CODE (ns
) == NAMESPACE_DECL
)
1519 return build_x_function_call (build_offset_ref (type
, name
), parmlist
, current_class_ref
);
1523 if (type
== NULL_TREE
|| ! is_aggr_type (type
, 1))
1524 return error_mark_node
;
1526 /* An operator we did not like. */
1527 if (name
== NULL_TREE
)
1528 return error_mark_node
;
1532 cp_error ("cannot call destructor `%T::~%T' without object", type
,
1534 return error_mark_node
;
1537 decl
= maybe_dummy_object (type
, &basetype_path
);
1539 /* Convert 'this' to the specified type to disambiguate conversion
1540 to the function's context. Apparently Standard C++ says that we
1541 shouldn't do this. */
1542 if (decl
== current_class_ref
1544 && ACCESSIBLY_UNIQUELY_DERIVED_P (type
, current_class_type
))
1546 tree olddecl
= current_class_ptr
;
1547 tree oldtype
= TREE_TYPE (TREE_TYPE (olddecl
));
1548 if (oldtype
!= type
)
1550 tree newtype
= build_qualified_type (type
, TYPE_QUALS (oldtype
));
1551 decl
= convert_force (build_pointer_type (newtype
), olddecl
, 0);
1552 decl
= build_indirect_ref (decl
, NULL_PTR
);
1556 if (method_name
== constructor_name (type
)
1557 || method_name
== constructor_name_full (type
))
1558 return build_functional_cast (type
, parmlist
);
1559 if (lookup_fnfields (basetype_path
, method_name
, 0))
1560 return build_method_call (decl
,
1561 TREE_CODE (name
) == TEMPLATE_ID_EXPR
1562 ? name
: method_name
,
1563 parmlist
, basetype_path
,
1564 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
1565 if (TREE_CODE (name
) == IDENTIFIER_NODE
1566 && ((t
= lookup_field (TYPE_BINFO (type
), name
, 1, 0))))
1568 if (t
== error_mark_node
)
1569 return error_mark_node
;
1570 if (TREE_CODE (t
) == FIELD_DECL
)
1572 if (is_dummy_object (decl
))
1574 cp_error ("invalid use of non-static field `%D'", t
);
1575 return error_mark_node
;
1577 decl
= build (COMPONENT_REF
, TREE_TYPE (t
), decl
, t
);
1579 else if (TREE_CODE (t
) == VAR_DECL
)
1583 cp_error ("invalid use of member `%D'", t
);
1584 return error_mark_node
;
1586 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl
)))
1587 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, decl
,
1588 parmlist
, NULL_TREE
);
1589 return build_function_call (decl
, parmlist
);
1593 cp_error ("no method `%T::%D'", type
, name
);
1594 return error_mark_node
;
1598 /* Build a reference to a member of an aggregate. This is not a
1599 C++ `&', but really something which can have its address taken,
1600 and then act as a pointer to member, for example TYPE :: FIELD
1601 can have its address taken by saying & TYPE :: FIELD.
1603 @@ Prints out lousy diagnostics for operator <typename>
1606 @@ This function should be rewritten and placed in search.c. */
1609 build_offset_ref (type
, name
)
1612 tree decl
, t
= error_mark_node
;
1614 tree basebinfo
= NULL_TREE
;
1615 tree orig_name
= name
;
1617 /* class templates can come in as TEMPLATE_DECLs here. */
1618 if (TREE_CODE (name
) == TEMPLATE_DECL
)
1621 if (type
== std_node
)
1622 return do_scoped_id (name
, 0);
1624 if (processing_template_decl
|| uses_template_parms (type
))
1625 return build_min_nt (SCOPE_REF
, type
, name
);
1627 /* Handle namespace names fully here. */
1628 if (TREE_CODE (type
) == NAMESPACE_DECL
)
1630 t
= lookup_namespace_name (type
, name
);
1631 if (t
!= error_mark_node
&& ! type_unknown_p (t
))
1634 t
= convert_from_reference (t
);
1639 if (type
== NULL_TREE
|| ! is_aggr_type (type
, 1))
1640 return error_mark_node
;
1642 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
1644 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1645 something like `a.template f<int>' or the like. For the most
1646 part, we treat this just like a.f. We do remember, however,
1647 the template-id that was used. */
1648 name
= TREE_OPERAND (orig_name
, 0);
1651 name
= DECL_NAME (name
);
1654 if (TREE_CODE (name
) == LOOKUP_EXPR
)
1655 /* This can happen during tsubst'ing. */
1656 name
= TREE_OPERAND (name
, 0);
1659 if (TREE_CODE (name
) == COMPONENT_REF
)
1660 name
= TREE_OPERAND (name
, 1);
1661 if (TREE_CODE (name
) == OVERLOAD
)
1662 name
= DECL_NAME (OVL_CURRENT (name
));
1666 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 0);
1669 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1671 if (! check_dtor_name (type
, name
))
1672 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1673 type
, TREE_OPERAND (name
, 0));
1674 name
= dtor_identifier
;
1677 if (!COMPLETE_TYPE_P (complete_type (type
))
1678 && !TYPE_BEING_DEFINED (type
))
1680 cp_error ("incomplete type `%T' does not have member `%D'", type
,
1682 return error_mark_node
;
1685 decl
= maybe_dummy_object (type
, &basebinfo
);
1687 member
= lookup_member (basebinfo
, name
, 1, 0);
1689 if (member
== error_mark_node
)
1690 return error_mark_node
;
1692 /* A lot of this logic is now handled in lookup_member. */
1693 if (member
&& BASELINK_P (member
))
1695 /* Go from the TREE_BASELINK to the member function info. */
1696 tree fnfields
= member
;
1697 t
= TREE_VALUE (fnfields
);
1699 if (TREE_CODE (orig_name
) == TEMPLATE_ID_EXPR
)
1701 /* The FNFIELDS are going to contain functions that aren't
1702 necessarily templates, and templates that don't
1703 necessarily match the explicit template parameters. We
1704 save all the functions, and the explicit parameters, and
1705 then figure out exactly what to instantiate with what
1706 arguments in instantiate_type. */
1708 if (TREE_CODE (t
) != OVERLOAD
)
1709 /* The code in instantiate_type which will process this
1710 expects to encounter OVERLOADs, not raw functions. */
1711 t
= ovl_cons (t
, NULL_TREE
);
1713 t
= build (TEMPLATE_ID_EXPR
, TREE_TYPE (t
), t
,
1714 TREE_OPERAND (orig_name
, 1));
1715 t
= build (OFFSET_REF
, unknown_type_node
, decl
, t
);
1717 PTRMEM_OK_P (t
) = 1;
1722 if (!really_overloaded_fn (t
))
1724 /* Get rid of a potential OVERLOAD around it */
1725 t
= OVL_CURRENT (t
);
1727 /* unique functions are handled easily. */
1728 if (!enforce_access (basebinfo
, t
))
1729 return error_mark_node
;
1731 if (DECL_STATIC_FUNCTION_P (t
))
1733 t
= build (OFFSET_REF
, TREE_TYPE (t
), decl
, t
);
1734 PTRMEM_OK_P (t
) = 1;
1738 TREE_TYPE (fnfields
) = unknown_type_node
;
1740 t
= build (OFFSET_REF
, unknown_type_node
, decl
, fnfields
);
1741 PTRMEM_OK_P (t
) = 1;
1749 cp_error ("`%D' is not a member of type `%T'", name
, type
);
1750 return error_mark_node
;
1753 if (TREE_CODE (t
) == TYPE_DECL
)
1758 /* static class members and class-specific enum
1759 values can be returned without further ado. */
1760 if (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == CONST_DECL
)
1763 return convert_from_reference (t
);
1766 if (TREE_CODE (t
) == FIELD_DECL
&& DECL_C_BIT_FIELD (t
))
1768 cp_error ("illegal pointer to bit field `%D'", t
);
1769 return error_mark_node
;
1772 /* static class functions too. */
1773 if (TREE_CODE (t
) == FUNCTION_DECL
1774 && TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1775 my_friendly_abort (53);
1777 /* In member functions, the form `type::name' is no longer
1778 equivalent to `this->type::name', at least not until
1779 resolve_offset_ref. */
1780 t
= build (OFFSET_REF
, build_offset_type (type
, TREE_TYPE (t
)), decl
, t
);
1781 PTRMEM_OK_P (t
) = 1;
1785 /* If a OFFSET_REF made it through to here, then it did
1786 not have its address taken. */
1789 resolve_offset_ref (exp
)
1792 tree type
= TREE_TYPE (exp
);
1793 tree base
= NULL_TREE
;
1795 tree basetype
, addr
;
1797 if (TREE_CODE (exp
) == OFFSET_REF
)
1799 member
= TREE_OPERAND (exp
, 1);
1800 base
= TREE_OPERAND (exp
, 0);
1804 my_friendly_assert (TREE_CODE (type
) == OFFSET_TYPE
, 214);
1805 if (TYPE_OFFSET_BASETYPE (type
) != current_class_type
)
1807 error ("object missing in use of pointer-to-member construct");
1808 return error_mark_node
;
1811 type
= TREE_TYPE (type
);
1812 base
= current_class_ref
;
1815 if (BASELINK_P (member
))
1816 return build_unary_op (ADDR_EXPR
, exp
, 0);
1818 if (TREE_CODE (TREE_TYPE (member
)) == METHOD_TYPE
)
1820 if (!flag_ms_extensions
)
1821 /* A single non-static member, make sure we don't allow a
1822 pointer-to-member. */
1823 exp
= ovl_cons (member
, NULL_TREE
);
1825 return build_unary_op (ADDR_EXPR
, exp
, 0);
1828 if ((TREE_CODE (member
) == VAR_DECL
1829 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member
))
1830 && ! TYPE_PTRMEM_P (TREE_TYPE (member
)))
1831 || TREE_CODE (TREE_TYPE (member
)) == FUNCTION_TYPE
)
1833 /* These were static members. */
1834 if (mark_addressable (member
) == 0)
1835 return error_mark_node
;
1839 if (TREE_CODE (TREE_TYPE (member
)) == POINTER_TYPE
1840 && TREE_CODE (TREE_TYPE (TREE_TYPE (member
))) == METHOD_TYPE
)
1843 /* Syntax error can cause a member which should
1844 have been seen as static to be grok'd as non-static. */
1845 if (TREE_CODE (member
) == FIELD_DECL
&& current_class_ref
== NULL_TREE
)
1847 cp_error_at ("member `%D' is non-static but referenced as a static member",
1849 error ("at this point in file");
1850 return error_mark_node
;
1853 /* The first case is really just a reference to a member of `this'. */
1854 if (TREE_CODE (member
) == FIELD_DECL
1855 && (base
== current_class_ref
|| is_dummy_object (base
)))
1859 basetype
= DECL_CONTEXT (member
);
1861 /* Try to get to basetype from 'this'; if that doesn't work,
1863 base
= current_class_ref
;
1865 /* First convert to the intermediate base specified, if appropriate. */
1866 if (TREE_CODE (exp
) == OFFSET_REF
&& TREE_CODE (type
) == OFFSET_TYPE
)
1867 base
= build_scoped_ref (base
, TYPE_OFFSET_BASETYPE (type
));
1869 addr
= build_unary_op (ADDR_EXPR
, base
, 0);
1870 addr
= convert_pointer_to (basetype
, addr
);
1872 if (addr
== error_mark_node
)
1873 return error_mark_node
;
1875 expr
= build (COMPONENT_REF
, TREE_TYPE (member
),
1876 build_indirect_ref (addr
, NULL_PTR
), member
);
1877 return convert_from_reference (expr
);
1880 /* Ensure that we have an object. */
1881 if (is_dummy_object (base
))
1882 addr
= error_mark_node
;
1884 /* If this is a reference to a member function, then return the
1885 address of the member function (which may involve going
1886 through the object's vtable), otherwise, return an expression
1887 for the dereferenced pointer-to-member construct. */
1888 addr
= build_unary_op (ADDR_EXPR
, base
, 0);
1890 if (TYPE_PTRMEM_P (TREE_TYPE (member
)))
1892 if (addr
== error_mark_node
)
1894 cp_error ("object missing in `%E'", exp
);
1895 return error_mark_node
;
1898 basetype
= TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member
)));
1899 addr
= convert_pointer_to (basetype
, addr
);
1900 member
= cp_convert (ptrdiff_type_node
, member
);
1903 /* Pointer to data members are offset by one, so that a null
1904 pointer with a real value of 0 is distinguishable from an
1905 offset of the first member of a structure. */
1906 member
= cp_build_binary_op (MINUS_EXPR
, member
,
1907 cp_convert (ptrdiff_type_node
,
1910 return build1 (INDIRECT_REF
, type
,
1911 build (PLUS_EXPR
, build_pointer_type (type
),
1914 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member
)))
1916 return get_member_function_from_ptrfunc (&addr
, member
);
1918 my_friendly_abort (56);
1923 /* If DECL is a `const' declaration, and its value is a known
1924 constant, then return that value. */
1927 decl_constant_value (decl
)
1930 if (TREE_READONLY_DECL_P (decl
)
1931 && ! TREE_THIS_VOLATILE (decl
)
1932 && DECL_INITIAL (decl
)
1933 && DECL_INITIAL (decl
) != error_mark_node
1934 /* This is invalid if initial value is not constant.
1935 If it has either a function call, a memory reference,
1936 or a variable, then re-evaluating it could give different results. */
1937 && TREE_CONSTANT (DECL_INITIAL (decl
))
1938 /* Check for cases where this is sub-optimal, even though valid. */
1939 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1940 return DECL_INITIAL (decl
);
1944 /* Common subroutines of build_new and build_vec_delete. */
1946 /* Call the global __builtin_delete to delete ADDR. */
1949 build_builtin_delete_call (addr
)
1952 mark_used (global_delete_fndecl
);
1953 return build_call (global_delete_fndecl
, build_tree_list (NULL_TREE
, addr
));
1956 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1957 (which needs to go through some sort of groktypename) or it
1958 is the name of the class we are newing. INIT is an initialization value.
1959 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1960 If INIT is void_type_node, it means do *not* call a constructor
1963 For types with constructors, the data returned is initialized
1964 by the appropriate constructor.
1966 Whether the type has a constructor or not, if it has a pointer
1967 to a virtual function table, then that pointer is set up
1970 Unless I am mistaken, a call to new () will return initialized
1971 data regardless of whether the constructor itself is private or
1972 not. NOPE; new fails if the constructor is private (jcm).
1974 Note that build_new does nothing to assure that any special
1975 alignment requirements of the type are met. Rather, it leaves
1976 it up to malloc to do the right thing. Otherwise, folding to
1977 the right alignment cal cause problems if the user tries to later
1978 free the memory returned by `new'.
1980 PLACEMENT is the `placement' list for user-defined operator new (). */
1983 build_new (placement
, decl
, init
, use_global_new
)
1989 tree nelts
= NULL_TREE
, t
;
1992 if (decl
== error_mark_node
)
1993 return error_mark_node
;
1995 if (TREE_CODE (decl
) == TREE_LIST
)
1997 tree absdcl
= TREE_VALUE (decl
);
1998 tree last_absdcl
= NULL_TREE
;
2000 if (current_function_decl
2001 && DECL_CONSTRUCTOR_P (current_function_decl
))
2002 my_friendly_assert (immediate_size_expand
== 0, 19990926);
2004 nelts
= integer_one_node
;
2006 if (absdcl
&& TREE_CODE (absdcl
) == CALL_EXPR
)
2007 my_friendly_abort (215);
2008 while (absdcl
&& TREE_CODE (absdcl
) == INDIRECT_REF
)
2010 last_absdcl
= absdcl
;
2011 absdcl
= TREE_OPERAND (absdcl
, 0);
2014 if (absdcl
&& TREE_CODE (absdcl
) == ARRAY_REF
)
2016 /* probably meant to be a vec new */
2019 while (TREE_OPERAND (absdcl
, 0)
2020 && TREE_CODE (TREE_OPERAND (absdcl
, 0)) == ARRAY_REF
)
2022 last_absdcl
= absdcl
;
2023 absdcl
= TREE_OPERAND (absdcl
, 0);
2027 this_nelts
= TREE_OPERAND (absdcl
, 1);
2028 if (this_nelts
!= error_mark_node
)
2030 if (this_nelts
== NULL_TREE
)
2031 error ("new of array type fails to specify size");
2032 else if (processing_template_decl
)
2035 absdcl
= TREE_OPERAND (absdcl
, 0);
2039 int flags
= pedantic
? WANT_INT
: (WANT_INT
| WANT_ENUM
);
2040 if (build_expr_type_conversion (flags
, this_nelts
, 0)
2042 pedwarn ("size in array new must have integral type");
2044 this_nelts
= save_expr (cp_convert (sizetype
, this_nelts
));
2045 absdcl
= TREE_OPERAND (absdcl
, 0);
2046 if (this_nelts
== integer_zero_node
)
2048 warning ("zero size array reserves no space");
2049 nelts
= integer_zero_node
;
2052 nelts
= cp_build_binary_op (MULT_EXPR
, nelts
, this_nelts
);
2056 nelts
= integer_zero_node
;
2060 TREE_OPERAND (last_absdcl
, 0) = absdcl
;
2062 TREE_VALUE (decl
) = absdcl
;
2064 type
= groktypename (decl
);
2065 if (! type
|| type
== error_mark_node
)
2066 return error_mark_node
;
2068 else if (TREE_CODE (decl
) == IDENTIFIER_NODE
)
2070 if (IDENTIFIER_HAS_TYPE_VALUE (decl
))
2072 /* An aggregate type. */
2073 type
= IDENTIFIER_TYPE_VALUE (decl
);
2074 decl
= TYPE_MAIN_DECL (type
);
2078 /* A builtin type. */
2079 decl
= lookup_name (decl
, 1);
2080 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 215);
2081 type
= TREE_TYPE (decl
);
2084 else if (TREE_CODE (decl
) == TYPE_DECL
)
2086 type
= TREE_TYPE (decl
);
2091 decl
= TYPE_MAIN_DECL (type
);
2094 if (processing_template_decl
)
2097 t
= tree_cons (tree_cons (NULL_TREE
, type
, NULL_TREE
),
2098 build_min_nt (ARRAY_REF
, NULL_TREE
, nelts
),
2103 rval
= build_min_nt (NEW_EXPR
, placement
, t
, init
);
2104 NEW_EXPR_USE_GLOBAL (rval
) = use_global_new
;
2108 /* ``A reference cannot be created by the new operator. A reference
2109 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2110 returned by new.'' ARM 5.3.3 */
2111 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2113 error ("new cannot be applied to a reference type");
2114 type
= TREE_TYPE (type
);
2117 if (TREE_CODE (type
) == FUNCTION_TYPE
)
2119 error ("new cannot be applied to a function type");
2120 return error_mark_node
;
2123 /* When the object being created is an array, the new-expression yields a
2124 pointer to the initial element (if any) of the array. For example,
2125 both new int and new int[10] return an int*. 5.3.4. */
2126 if (TREE_CODE (type
) == ARRAY_TYPE
&& has_array
== 0)
2128 nelts
= array_type_nelts_top (type
);
2130 type
= TREE_TYPE (type
);
2134 t
= build_nt (ARRAY_REF
, type
, nelts
);
2138 rval
= build (NEW_EXPR
, build_pointer_type (type
), placement
, t
, init
);
2139 NEW_EXPR_USE_GLOBAL (rval
) = use_global_new
;
2140 TREE_SIDE_EFFECTS (rval
) = 1;
2141 rval
= build_new_1 (rval
);
2142 if (rval
== error_mark_node
)
2143 return error_mark_node
;
2145 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2146 rval
= build1 (NOP_EXPR
, TREE_TYPE (rval
), rval
);
2147 TREE_NO_UNUSED_WARNING (rval
) = 1;
2152 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2155 build_java_class_ref (type
)
2158 tree name
, class_decl
;
2159 static tree CL_prefix
= NULL_TREE
;
2160 if (CL_prefix
== NULL_TREE
)
2161 CL_prefix
= get_identifier("_CL_");
2162 if (jclass_node
== NULL_TREE
)
2164 jclass_node
= IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2165 if (jclass_node
== NULL_TREE
)
2166 fatal("call to Java constructor, while `jclass' undefined");
2167 jclass_node
= TREE_TYPE (jclass_node
);
2169 name
= build_overload_with_type (CL_prefix
, type
);
2170 class_decl
= IDENTIFIER_GLOBAL_VALUE (name
);
2171 if (class_decl
== NULL_TREE
)
2173 class_decl
= build_decl (VAR_DECL
, name
, TREE_TYPE (jclass_node
));
2174 TREE_STATIC (class_decl
) = 1;
2175 DECL_EXTERNAL (class_decl
) = 1;
2176 TREE_PUBLIC (class_decl
) = 1;
2177 DECL_ARTIFICIAL (class_decl
) = 1;
2178 DECL_IGNORED_P (class_decl
) = 1;
2179 pushdecl_top_level (class_decl
);
2180 make_decl_rtl (class_decl
, NULL_PTR
, 1);
2185 /* Returns teh size of the cookie to use when allocating an array
2186 whose elements have the indicated TYPE. Assumes that it is already
2187 known that a cookie is needed. */
2190 get_cookie_size (type
)
2197 /* Under the new ABI, we need to allocate an additional max
2198 (sizeof (size_t), alignof (true_type)) bytes. */
2202 sizetype_size
= size_in_bytes (sizetype
);
2203 type_align
= size_int (TYPE_ALIGN_UNIT (type
));
2204 if (INT_CST_LT_UNSIGNED (type_align
, sizetype_size
))
2205 cookie_size
= sizetype_size
;
2207 cookie_size
= type_align
;
2211 if (TYPE_ALIGN (type
) > TYPE_ALIGN (BI_header_type
))
2212 return size_int (TYPE_ALIGN_UNIT (type
));
2214 return size_in_bytes (BI_header_type
);
2220 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2221 value is immediately handed to expand_expr. */
2227 tree placement
, init
;
2228 tree type
, true_type
, size
, rval
;
2229 tree nelts
= NULL_TREE
;
2230 tree alloc_expr
, alloc_node
= NULL_TREE
;
2232 enum tree_code code
;
2233 int use_cookie
, nothrow
, check_new
;
2234 /* Nonzero if the user wrote `::new' rather than just `new'. */
2235 int globally_qualified_p
;
2236 /* Nonzero if we're going to call a global operator new, rather than
2237 a class-specific version. */
2239 int use_java_new
= 0;
2240 /* If non-NULL, the number of extra bytes to allocate at the
2241 beginning of the storage allocated for an array-new expression in
2242 order to store the number of elements. */
2243 tree cookie_size
= NULL_TREE
;
2245 placement
= TREE_OPERAND (exp
, 0);
2246 type
= TREE_OPERAND (exp
, 1);
2247 init
= TREE_OPERAND (exp
, 2);
2248 globally_qualified_p
= NEW_EXPR_USE_GLOBAL (exp
);
2250 if (TREE_CODE (type
) == ARRAY_REF
)
2253 nelts
= TREE_OPERAND (type
, 1);
2254 type
= TREE_OPERAND (type
, 0);
2258 code
= has_array
? VEC_NEW_EXPR
: NEW_EXPR
;
2260 if (CP_TYPE_QUALS (type
))
2261 type
= TYPE_MAIN_VARIANT (type
);
2263 /* If our base type is an array, then make sure we know how many elements
2265 while (TREE_CODE (true_type
) == ARRAY_TYPE
)
2267 tree this_nelts
= array_type_nelts_top (true_type
);
2268 nelts
= cp_build_binary_op (MULT_EXPR
, nelts
, this_nelts
);
2269 true_type
= TREE_TYPE (true_type
);
2272 if (!complete_type_or_else (true_type
, exp
))
2273 return error_mark_node
;
2276 size
= fold (cp_build_binary_op (MULT_EXPR
, size_in_bytes (true_type
),
2279 size
= size_in_bytes (type
);
2281 if (TREE_CODE (true_type
) == VOID_TYPE
)
2283 error ("invalid type `void' for new");
2284 return error_mark_node
;
2287 if (abstract_virtuals_error (NULL_TREE
, true_type
))
2288 return error_mark_node
;
2290 /* Figure out whether or not we're going to use the global operator
2292 if (!globally_qualified_p
2293 && IS_AGGR_TYPE (true_type
)
2294 && ((!has_array
&& TYPE_HAS_NEW_OPERATOR (true_type
))
2295 || (has_array
&& TYPE_HAS_ARRAY_NEW_OPERATOR (true_type
))))
2300 /* We only need cookies for arrays containing types for which we
2302 if (!has_array
|| !TYPE_VEC_NEW_USES_COOKIE (true_type
))
2304 /* When using placement new, users may not realize that they need
2305 the extra storage. Under the old ABI, we don't allocate the
2306 cookie whenever they use one placement argument of type `void
2307 *'. Under the new ABI, we require that the operator called be
2308 the global placement operator delete[]. */
2309 else if (placement
&& !TREE_CHAIN (placement
)
2310 && same_type_p (TREE_TYPE (TREE_VALUE (placement
)),
2312 use_cookie
= (!flag_new_abi
|| !use_global_new
);
2313 /* Otherwise, we need the cookie. */
2317 /* Compute the number of extra bytes to allocate, now that we know
2318 whether or not we need the cookie. */
2321 cookie_size
= get_cookie_size (true_type
);
2322 size
= size_binop (PLUS_EXPR
, size
, cookie_size
);
2325 if (has_array
&& init
&& pedantic
)
2326 cp_pedwarn ("initialization in array new");
2328 /* Allocate the object. */
2330 if (! placement
&& TYPE_FOR_JAVA (true_type
))
2332 tree class_addr
, alloc_decl
;
2333 tree class_decl
= build_java_class_ref (true_type
);
2334 tree class_size
= size_in_bytes (true_type
);
2335 static char alloc_name
[] = "_Jv_AllocObject";
2337 alloc_decl
= IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name
));
2338 if (alloc_decl
== NULL_TREE
)
2339 fatal("call to Java constructor, while `%s' undefined", alloc_name
);
2340 class_addr
= build1 (ADDR_EXPR
, jclass_node
, class_decl
);
2341 rval
= build_function_call (alloc_decl
,
2342 tree_cons (NULL_TREE
, class_addr
,
2343 build_tree_list (NULL_TREE
,
2345 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2352 args
= tree_cons (NULL_TREE
, size
, placement
);
2353 fnname
= ansi_opname (code
);
2356 rval
= (build_new_function_call
2357 (lookup_function_nonclass (fnname
, args
),
2360 rval
= build_method_call (build_dummy_object (true_type
),
2361 fnname
, args
, NULL_TREE
,
2363 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2366 if (rval
== error_mark_node
)
2367 return error_mark_node
;
2369 /* unless an allocation function is declared with an empty excep-
2370 tion-specification (_except.spec_), throw(), it indicates failure to
2371 allocate storage by throwing a bad_alloc exception (clause _except_,
2372 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2373 cation function is declared with an empty exception-specification,
2374 throw(), it returns null to indicate failure to allocate storage and a
2375 non-null pointer otherwise.
2377 So check for a null exception spec on the op new we just called. */
2382 /* The CALL_EXPR. */
2383 tree t
= TREE_OPERAND (rval
, 0);
2385 t
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
2386 nothrow
= TYPE_NOTHROW_P (TREE_TYPE (t
));
2388 check_new
= (flag_check_new
|| nothrow
) && ! use_java_new
;
2390 if ((check_new
|| flag_exceptions
) && rval
)
2392 alloc_expr
= get_target_expr (rval
);
2393 alloc_node
= rval
= TREE_OPERAND (alloc_expr
, 0);
2396 alloc_expr
= NULL_TREE
;
2398 /* if rval is NULL_TREE I don't have to allocate it, but are we
2399 totally sure we have some extra bytes in that case for the
2400 cookies? And how does that interact with the code below? (mrs) */
2401 /* Finish up some magic for new'ed arrays */
2402 if (use_cookie
&& rval
!= NULL_TREE
)
2405 rval
= convert (string_type_node
, rval
); /* for ptr arithmetic */
2406 rval
= save_expr (cp_build_binary_op (PLUS_EXPR
, rval
, cookie_size
));
2407 /* Store the number of bytes allocated so that we can know how
2408 many elements to destroy later. */
2411 /* Under the new ABI, we use the last sizeof (size_t) bytes
2412 to store the number of elements. */
2413 cookie
= build_indirect_ref (build (MINUS_EXPR
,
2414 build_pointer_type (sizetype
),
2416 size_in_bytes (sizetype
)),
2418 exp1
= build (MODIFY_EXPR
, void_type_node
, cookie
, nelts
);
2423 = build_indirect_ref (build (MINUS_EXPR
,
2424 build_pointer_type (BI_header_type
),
2425 rval
, cookie_size
), NULL_PTR
);
2426 exp1
= build (MODIFY_EXPR
, void_type_node
,
2427 build_component_ref (cookie
, nelts_identifier
,
2432 /* Build `(cookie = nelts, rval)' and use that as the complete
2434 rval
= cp_convert (build_pointer_type (true_type
), rval
);
2435 rval
= build_compound_expr
2436 (tree_cons (NULL_TREE
, exp1
,
2437 build_tree_list (NULL_TREE
, rval
)));
2440 if (rval
== error_mark_node
)
2441 return error_mark_node
;
2443 /* Don't call any constructors or do any initialization. */
2444 if (init
== void_type_node
)
2447 if (TYPE_NEEDS_CONSTRUCTING (type
) || init
)
2449 if (! TYPE_NEEDS_CONSTRUCTING (type
)
2450 && ! IS_AGGR_TYPE (type
) && ! has_array
)
2452 /* We are processing something like `new int (10)', which
2453 means allocate an int, and initialize it with 10. */
2457 /* At present RVAL is a temporary variable, created to hold
2458 the value from the call to `operator new'. We transform
2459 it to (*RVAL = INIT, RVAL). */
2460 rval
= save_expr (rval
);
2461 deref
= build_indirect_ref (rval
, NULL_PTR
);
2463 /* Even for something like `new const int (10)' we must
2464 allow the expression to be non-const while we do the
2466 deref_type
= TREE_TYPE (deref
);
2467 if (CP_TYPE_CONST_P (deref_type
))
2469 = cp_build_qualified_type (deref_type
,
2470 CP_TYPE_QUALS (deref_type
)
2471 & ~TYPE_QUAL_CONST
);
2472 TREE_READONLY (deref
) = 0;
2474 if (TREE_CHAIN (init
) != NULL_TREE
)
2475 pedwarn ("initializer list being treated as compound expression");
2476 else if (TREE_CODE (init
) == CONSTRUCTOR
)
2478 pedwarn ("initializer list appears where operand should be used");
2479 init
= TREE_OPERAND (init
, 1);
2481 init
= build_compound_expr (init
);
2483 init
= convert_for_initialization (deref
, type
, init
, LOOKUP_NORMAL
,
2484 "new", NULL_TREE
, 0);
2485 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
),
2486 build_modify_expr (deref
, NOP_EXPR
, init
),
2488 TREE_NO_UNUSED_WARNING (rval
) = 1;
2489 TREE_SIDE_EFFECTS (rval
) = 1;
2491 else if (! has_array
)
2494 /* Constructors are never virtual. If it has an initialization, we
2495 need to complain if we aren't allowed to use the ctor that took
2497 int flags
= LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_COMPLAIN
;
2499 rval
= save_expr (rval
);
2502 if (newrval
&& TREE_CODE (TREE_TYPE (newrval
)) == POINTER_TYPE
)
2503 newrval
= build_indirect_ref (newrval
, NULL_PTR
);
2505 newrval
= build_method_call (newrval
,
2506 complete_ctor_identifier
,
2507 init
, TYPE_BINFO (true_type
), flags
);
2509 if (newrval
== NULL_TREE
|| newrval
== error_mark_node
)
2510 return error_mark_node
;
2512 newrval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
), newrval
, rval
);
2514 TREE_HAS_CONSTRUCTOR (rval
) = 1;
2517 rval
= (build_vec_init
2520 cp_build_binary_op (MINUS_EXPR
, nelts
, integer_one_node
),
2524 /* If any part of the object initialization terminates by throwing an
2525 exception and a suitable deallocation function can be found, the
2526 deallocation function is called to free the memory in which the
2527 object was being constructed, after which the exception continues
2528 to propagate in the context of the new-expression. If no
2529 unambiguous matching deallocation function can be found,
2530 propagating the exception does not cause the object's memory to be
2532 if (flag_exceptions
&& alloc_expr
&& ! use_java_new
)
2534 enum tree_code dcode
= has_array
? VEC_DELETE_EXPR
: DELETE_EXPR
;
2535 tree cleanup
, fn
= NULL_TREE
;
2536 int flags
= (LOOKUP_NORMAL
2537 | (globally_qualified_p
* LOOKUP_GLOBAL
));
2539 /* The Standard is unclear here, but the right thing to do
2540 is to use the same method for finding deallocation
2541 functions that we use for finding allocation functions. */
2542 flags
|= LOOKUP_SPECULATIVELY
;
2544 /* We expect alloc_expr to look like a TARGET_EXPR around
2545 a NOP_EXPR around the CALL_EXPR we want. */
2546 fn
= TREE_OPERAND (alloc_expr
, 1);
2547 fn
= TREE_OPERAND (fn
, 0);
2549 cleanup
= build_op_delete_call (dcode
, alloc_node
, size
, flags
, fn
);
2551 /* Ack! First we allocate the memory. Then we set our sentry
2552 variable to true, and expand a cleanup that deletes the memory
2553 if sentry is true. Then we run the constructor and store the
2554 returned pointer in buf. Then we clear sentry and return buf. */
2558 tree end
, sentry
, begin
, buf
, t
= TREE_TYPE (rval
);
2560 begin
= get_target_expr (boolean_true_node
);
2561 sentry
= TREE_OPERAND (begin
, 0);
2563 TREE_OPERAND (begin
, 2)
2564 = build (COND_EXPR
, void_type_node
, sentry
,
2565 cleanup
, void_zero_node
);
2567 rval
= get_target_expr (rval
);
2569 end
= build (MODIFY_EXPR
, TREE_TYPE (sentry
),
2570 sentry
, boolean_false_node
);
2572 buf
= TREE_OPERAND (rval
, 0);
2574 rval
= build (COMPOUND_EXPR
, t
, begin
,
2575 build (COMPOUND_EXPR
, t
, rval
,
2576 build (COMPOUND_EXPR
, t
, end
, buf
)));
2580 else if (CP_TYPE_CONST_P (true_type
))
2581 cp_error ("uninitialized const in `new' of `%#T'", true_type
);
2585 if (alloc_expr
&& rval
== alloc_node
)
2587 rval
= TREE_OPERAND (alloc_expr
, 1);
2588 alloc_expr
= NULL_TREE
;
2591 if (check_new
&& alloc_expr
)
2593 /* Did we modify the storage? */
2594 tree ifexp
= cp_build_binary_op (NE_EXPR
, alloc_node
,
2596 rval
= build_conditional_expr (ifexp
, rval
, alloc_node
);
2600 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
), alloc_expr
, rval
);
2602 if (rval
&& TREE_TYPE (rval
) != build_pointer_type (type
))
2604 /* The type of new int [3][3] is not int *, but int [3] * */
2605 rval
= build_c_cast (build_pointer_type (type
), rval
);
2612 build_vec_delete_1 (base
, maxindex
, type
, auto_delete_vec
, use_global_delete
)
2613 tree base
, maxindex
, type
;
2614 special_function_kind auto_delete_vec
;
2615 int use_global_delete
;
2618 tree ptype
= build_pointer_type (type
= complete_type (type
));
2619 tree size_exp
= size_in_bytes (type
);
2621 /* Temporary variables used by the loop. */
2622 tree tbase
, tbase_init
;
2624 /* This is the body of the loop that implements the deletion of a
2625 single element, and moves temp variables to next elements. */
2628 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2631 /* This is the thing that governs what to do after the loop has run. */
2632 tree deallocate_expr
= 0;
2634 /* This is the BIND_EXPR which holds the outermost iterator of the
2635 loop. It is convenient to set this variable up and test it before
2636 executing any other code in the loop.
2637 This is also the containing expression returned by this function. */
2638 tree controller
= NULL_TREE
;
2640 if (! IS_AGGR_TYPE (type
) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
2642 loop
= integer_zero_node
;
2646 /* The below is short by the cookie size. */
2647 virtual_size
= size_binop (MULT_EXPR
, size_exp
,
2648 convert (sizetype
, maxindex
));
2650 tbase
= create_temporary_var (ptype
);
2651 tbase_init
= build_modify_expr (tbase
, NOP_EXPR
,
2652 fold (build (PLUS_EXPR
, ptype
,
2655 DECL_REGISTER (tbase
) = 1;
2656 controller
= build (BIND_EXPR
, void_type_node
, tbase
, NULL_TREE
, NULL_TREE
);
2657 TREE_SIDE_EFFECTS (controller
) = 1;
2661 body
= tree_cons (NULL_TREE
,
2662 build_delete (ptype
, tbase
, sfk_complete_destructor
,
2663 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 1),
2666 body
= tree_cons (NULL_TREE
,
2667 build_modify_expr (tbase
, NOP_EXPR
, build (MINUS_EXPR
, ptype
, tbase
, size_exp
)),
2670 body
= tree_cons (NULL_TREE
,
2671 build (EXIT_EXPR
, void_type_node
,
2672 build (EQ_EXPR
, boolean_type_node
, base
, tbase
)),
2675 loop
= build (LOOP_EXPR
, void_type_node
, build_compound_expr (body
));
2677 loop
= tree_cons (NULL_TREE
, tbase_init
,
2678 tree_cons (NULL_TREE
, loop
, NULL_TREE
));
2679 loop
= build_compound_expr (loop
);
2682 /* If the delete flag is one, or anything else with the low bit set,
2683 delete the storage. */
2684 deallocate_expr
= integer_zero_node
;
2685 if (auto_delete_vec
!= sfk_base_destructor
)
2689 /* The below is short by the cookie size. */
2690 virtual_size
= size_binop (MULT_EXPR
, size_exp
,
2691 convert (sizetype
, maxindex
));
2693 if (! TYPE_VEC_NEW_USES_COOKIE (type
))
2700 cookie_size
= get_cookie_size (type
);
2702 = cp_convert (ptype
,
2703 cp_build_binary_op (MINUS_EXPR
,
2704 cp_convert (string_type_node
,
2707 /* True size with header. */
2708 virtual_size
= size_binop (PLUS_EXPR
, virtual_size
, cookie_size
);
2711 if (auto_delete_vec
== sfk_deleting_destructor
)
2712 deallocate_expr
= build_x_delete (base_tbd
,
2713 2 | use_global_delete
,
2717 if (loop
&& deallocate_expr
!= integer_zero_node
)
2719 body
= tree_cons (NULL_TREE
, loop
,
2720 tree_cons (NULL_TREE
, deallocate_expr
, NULL_TREE
));
2721 body
= build_compound_expr (body
);
2726 /* Outermost wrapper: If pointer is null, punt. */
2727 body
= fold (build (COND_EXPR
, void_type_node
,
2728 fold (build (NE_EXPR
, boolean_type_node
, base
,
2729 integer_zero_node
)),
2730 body
, integer_zero_node
));
2731 body
= build1 (NOP_EXPR
, void_type_node
, body
);
2735 TREE_OPERAND (controller
, 1) = body
;
2739 return cp_convert (void_type_node
, body
);
2742 /* Create an unnamed variable of the indicated TYPE. */
2745 create_temporary_var (type
)
2750 decl
= build_decl (VAR_DECL
, NULL_TREE
, type
);
2751 TREE_USED (decl
) = 1;
2752 DECL_ARTIFICIAL (decl
) = 1;
2753 DECL_SOURCE_FILE (decl
) = input_filename
;
2754 DECL_SOURCE_LINE (decl
) = lineno
;
2755 DECL_IGNORED_P (decl
) = 1;
2756 DECL_CONTEXT (decl
) = current_function_decl
;
2761 /* Create a new temporary variable of the indicated TYPE, initialized
2764 It is not entered into current_binding_level, because that breaks
2765 things when it comes time to do final cleanups (which take place
2766 "outside" the binding contour of the function). */
2769 get_temp_regvar (type
, init
)
2774 decl
= create_temporary_var (type
);
2775 if (building_stmt_tree ())
2776 add_decl_stmt (decl
);
2777 if (!building_stmt_tree ())
2778 DECL_RTL (decl
) = assign_temp (type
, 2, 0, 1);
2779 finish_expr_stmt (build_modify_expr (decl
, INIT_EXPR
, init
));
2784 /* `build_vec_init' returns tree structure that performs
2785 initialization of a vector of aggregate types.
2787 DECL is passed only for error reporting, and provides line number
2788 and source file name information.
2789 BASE is the space where the vector will be. For a vector of Ts,
2790 the type of BASE is `T*'.
2791 MAXINDEX is the maximum index of the array (one less than the
2792 number of elements).
2793 INIT is the (possibly NULL) initializer.
2795 FROM_ARRAY is 0 if we should init everything with INIT
2796 (i.e., every element initialized from INIT).
2797 FROM_ARRAY is 1 if we should index into INIT in parallel
2798 with initialization of DECL.
2799 FROM_ARRAY is 2 if we should index into INIT in parallel,
2800 but use assignment instead of initialization. */
2803 build_vec_init (decl
, base
, maxindex
, init
, from_array
)
2804 tree decl
, base
, maxindex
, init
;
2808 tree base2
= NULL_TREE
;
2810 tree itype
= NULL_TREE
;
2812 /* The type of an element in the array. */
2814 /* The type of a pointer to an element in the array. */
2819 tree try_block
= NULL_TREE
;
2820 tree try_body
= NULL_TREE
;
2821 int num_initialized_elts
= 0;
2823 maxindex
= cp_convert (ptrdiff_type_node
, maxindex
);
2824 if (maxindex
== error_mark_node
)
2825 return error_mark_node
;
2827 type
= TREE_TYPE (TREE_TYPE (base
));
2828 ptype
= build_pointer_type (type
);
2829 size
= size_in_bytes (type
);
2831 /* The code we are generating looks like:
2835 ptrdiff_t iterator = maxindex;
2837 ... initializations from CONSTRUCTOR ...
2838 if (iterator != -1) {
2840 ... initialize *base ...
2842 } while (--iterator != -1);
2845 ... destroy elements that were constructed ...
2848 We can omit the try and catch blocks if we know that the
2849 initialization will never throw an exception, or if the array
2850 elements do not have destructors. If we have a CONSTRUCTOR to
2851 give us initialization information, we emit code to initialize
2852 each of the elements before the loop in the try block, and then
2853 iterate over fewer elements. We can omit the loop completely if
2854 the elements of the array do not have constructors.
2856 We actually wrap the entire body of the above in a STMT_EXPR, for
2859 When copying from array to another, when the array elements have
2860 only trivial copy constructors, we should use __builtin_memcpy
2861 rather than generating a loop. That way, we could take advantage
2862 of whatever cleverness the back-end has for dealing with copies
2863 of blocks of memory. */
2865 begin_init_stmts (&stmt_expr
, &compound_stmt
);
2866 destroy_temps
= stmts_are_full_exprs_p ();
2867 current_stmt_tree
->stmts_are_full_exprs_p
= 0;
2868 rval
= get_temp_regvar (ptype
,
2869 cp_convert (ptype
, default_conversion (base
)));
2870 base
= get_temp_regvar (ptype
, rval
);
2871 iterator
= get_temp_regvar (ptrdiff_type_node
, maxindex
);
2873 /* Protect the entire array initialization so that we can destroy
2874 the partially constructed array if an exception is thrown. */
2875 if (flag_exceptions
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
2877 try_block
= begin_try_block ();
2878 try_body
= begin_compound_stmt (/*has_no_scope=*/1);
2881 if (init
!= NULL_TREE
&& TREE_CODE (init
) == CONSTRUCTOR
2882 && (!decl
|| same_type_p (TREE_TYPE (init
), TREE_TYPE (decl
))))
2884 /* Do non-default initialization resulting from brace-enclosed
2890 for (elts
= CONSTRUCTOR_ELTS (init
); elts
; elts
= TREE_CHAIN (elts
))
2892 tree elt
= TREE_VALUE (elts
);
2893 tree baseref
= build1 (INDIRECT_REF
, type
, base
);
2895 num_initialized_elts
++;
2897 if (IS_AGGR_TYPE (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
2898 finish_expr_stmt (build_aggr_init (baseref
, elt
, 0));
2900 finish_expr_stmt (build_modify_expr (baseref
, NOP_EXPR
,
2903 finish_expr_stmt (build_modify_expr
2906 build (PLUS_EXPR
, build_pointer_type (type
),
2908 finish_expr_stmt (build_modify_expr
2911 build (MINUS_EXPR
, ptrdiff_type_node
,
2912 iterator
, integer_one_node
)));
2915 /* Clear out INIT so that we don't get confused below. */
2918 else if (from_array
)
2920 /* If initializing one array from another, initialize element by
2921 element. We rely upon the below calls the do argument
2923 if (decl
== NULL_TREE
)
2925 sorry ("initialization of array from dissimilar array type");
2926 return error_mark_node
;
2930 base2
= default_conversion (init
);
2931 itype
= TREE_TYPE (base2
);
2932 base2
= get_temp_regvar (itype
, base2
);
2933 itype
= TREE_TYPE (itype
);
2935 else if (TYPE_LANG_SPECIFIC (type
)
2936 && TYPE_NEEDS_CONSTRUCTING (type
)
2937 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
2939 error ("initializer ends prematurely");
2940 return error_mark_node
;
2944 /* Now, default-initialize any remaining elements. We don't need to
2945 do that if a) the type does not need constructing, or b) we've
2946 already initialized all the elements.
2948 We do need to keep going if we're copying an array. */
2951 || (TYPE_NEEDS_CONSTRUCTING (type
)
2952 && ! (host_integerp (maxindex
, 0)
2953 && (num_initialized_elts
2954 == tree_low_cst (maxindex
, 0) + 1))))
2956 /* If the ITERATOR is equal to -1, then we don't have to loop;
2957 we've already initialized all the elements. */
2963 if_stmt
= begin_if_stmt ();
2964 finish_if_stmt_cond (build (NE_EXPR
, boolean_type_node
,
2965 iterator
, minus_one_node
),
2968 /* Otherwise, loop through the elements. */
2969 do_stmt
= begin_do_stmt ();
2970 do_body
= begin_compound_stmt (/*has_no_scope=*/1);
2972 /* When we're not building a statement-tree, things are a little
2973 complicated. If, when we recursively call build_aggr_init,
2974 an expression containing a TARGET_EXPR is expanded, then it
2975 may get a cleanup. Then, the result of that expression is
2976 passed to finish_expr_stmt, which will call
2977 expand_start_target_temps/expand_end_target_temps. However,
2978 the latter call will not cause the cleanup to run because
2979 that block will still be on the block stack. So, we call
2980 expand_start_target_temps here manually; the corresponding
2981 call to expand_end_target_temps below will cause the cleanup
2983 if (!building_stmt_tree ())
2984 expand_start_target_temps ();
2988 tree to
= build1 (INDIRECT_REF
, type
, base
);
2992 from
= build1 (INDIRECT_REF
, itype
, base2
);
2996 if (from_array
== 2)
2997 elt_init
= build_modify_expr (to
, NOP_EXPR
, from
);
2998 else if (TYPE_NEEDS_CONSTRUCTING (type
))
2999 elt_init
= build_aggr_init (to
, from
, 0);
3001 elt_init
= build_modify_expr (to
, NOP_EXPR
, from
);
3003 my_friendly_abort (57);
3005 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3008 sorry ("cannot initialize multi-dimensional array with initializer");
3009 elt_init
= (build_vec_init
3012 build_pointer_type (TREE_TYPE (type
)),
3014 array_type_nelts (type
), 0, 0));
3017 elt_init
= build_aggr_init (build1 (INDIRECT_REF
, type
, base
),
3020 /* The initialization of each array element is a
3022 if (!building_stmt_tree ())
3024 genrtl_expr_stmt (elt_init
);
3025 expand_end_target_temps ();
3029 current_stmt_tree
->stmts_are_full_exprs_p
= 1;
3030 finish_expr_stmt (elt_init
);
3031 current_stmt_tree
->stmts_are_full_exprs_p
= 0;
3034 finish_expr_stmt (build_modify_expr
3037 build (PLUS_EXPR
, build_pointer_type (type
),
3040 finish_expr_stmt (build_modify_expr
3043 build (PLUS_EXPR
, build_pointer_type (type
),
3046 finish_compound_stmt (/*has_no_scope=*/1, do_body
);
3047 finish_do_body (do_stmt
);
3048 finish_do_stmt (build (NE_EXPR
, boolean_type_node
,
3049 build (PREDECREMENT_EXPR
,
3056 finish_then_clause (if_stmt
);
3060 /* Make sure to cleanup any partially constructed elements. */
3061 if (flag_exceptions
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3065 finish_compound_stmt (/*has_no_scope=*/1, try_body
);
3066 finish_cleanup_try_block (try_block
);
3067 e
= build_vec_delete_1 (rval
,
3068 cp_build_binary_op (MINUS_EXPR
, maxindex
,
3071 sfk_base_destructor
,
3072 /*use_global_delete=*/0);
3073 finish_cleanup (e
, try_block
);
3076 /* The value of the array initialization is the address of the
3077 first element in the array. */
3078 finish_expr_stmt (rval
);
3080 stmt_expr
= finish_init_stmts (stmt_expr
, compound_stmt
);
3081 current_stmt_tree
->stmts_are_full_exprs_p
= destroy_temps
;
3085 /* Free up storage of type TYPE, at address ADDR.
3087 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3090 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3091 used as the second argument to operator delete. It can include
3092 things like padding and magic size cookies. It has virtual in it,
3093 because if you have a base pointer and you delete through a virtual
3094 destructor, it should be the size of the dynamic object, not the
3095 static object, see Free Store 12.5 ISO C++.
3097 This does not call any destructors. */
3100 build_x_delete (addr
, which_delete
, virtual_size
)
3105 int use_global_delete
= which_delete
& 1;
3106 int use_vec_delete
= !!(which_delete
& 2);
3107 enum tree_code code
= use_vec_delete
? VEC_DELETE_EXPR
: DELETE_EXPR
;
3108 int flags
= LOOKUP_NORMAL
| (use_global_delete
* LOOKUP_GLOBAL
);
3110 return build_op_delete_call (code
, addr
, virtual_size
, flags
, NULL_TREE
);
3113 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3117 build_dtor_call (exp
, dtor_kind
, flags
)
3119 special_function_kind dtor_kind
;
3126 case sfk_complete_destructor
:
3127 name
= complete_dtor_identifier
;
3130 case sfk_base_destructor
:
3131 name
= base_dtor_identifier
;
3134 case sfk_deleting_destructor
:
3135 name
= deleting_dtor_identifier
;
3139 my_friendly_abort (20000524);
3141 return build_method_call (exp
, name
, NULL_TREE
, NULL_TREE
, flags
);
3144 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3145 ADDR is an expression which yields the store to be destroyed.
3146 AUTO_DELETE is the name of the destructor to call, i.e., either
3147 sfk_complete_destructor, sfk_base_destructor, or
3148 sfk_deleting_destructor.
3150 FLAGS is the logical disjunction of zero or more LOOKUP_
3151 flags. See cp-tree.h for more info.
3153 This function does not delete an object's virtual base classes. */
3156 build_delete (type
, addr
, auto_delete
, flags
, use_global_delete
)
3158 special_function_kind auto_delete
;
3160 int use_global_delete
;
3166 if (addr
== error_mark_node
)
3167 return error_mark_node
;
3169 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3170 set to `error_mark_node' before it gets properly cleaned up. */
3171 if (type
== error_mark_node
)
3172 return error_mark_node
;
3174 type
= TYPE_MAIN_VARIANT (type
);
3176 if (TREE_CODE (type
) == POINTER_TYPE
)
3178 type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
3179 if (!VOID_TYPE_P (type
) && !complete_type_or_else (type
, addr
))
3180 return error_mark_node
;
3181 if (TREE_CODE (type
) == ARRAY_TYPE
)
3183 if (! IS_AGGR_TYPE (type
))
3185 /* Call the builtin operator delete. */
3186 return build_builtin_delete_call (addr
);
3188 if (TREE_SIDE_EFFECTS (addr
))
3189 addr
= save_expr (addr
);
3191 /* throw away const and volatile on target type of addr */
3192 addr
= convert_force (build_pointer_type (type
), addr
, 0);
3193 ref
= build_indirect_ref (addr
, NULL_PTR
);
3195 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3198 if (TREE_SIDE_EFFECTS (addr
))
3199 addr
= save_expr (addr
);
3200 if (TYPE_DOMAIN (type
) == NULL_TREE
)
3202 error ("unknown array size in delete");
3203 return error_mark_node
;
3205 return build_vec_delete (addr
, array_type_nelts (type
),
3206 auto_delete
, use_global_delete
);
3210 /* Don't check PROTECT here; leave that decision to the
3211 destructor. If the destructor is accessible, call it,
3212 else report error. */
3213 addr
= build_unary_op (ADDR_EXPR
, addr
, 0);
3214 if (TREE_SIDE_EFFECTS (addr
))
3215 addr
= save_expr (addr
);
3217 if (TREE_CONSTANT (addr
))
3218 addr
= convert_pointer_to (type
, addr
);
3220 addr
= convert_force (build_pointer_type (type
), addr
, 0);
3222 ref
= build_indirect_ref (addr
, NULL_PTR
);
3225 my_friendly_assert (IS_AGGR_TYPE (type
), 220);
3227 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
3229 if (auto_delete
== sfk_base_destructor
)
3230 return void_zero_node
;
3232 return build_op_delete_call
3233 (DELETE_EXPR
, addr
, c_sizeof_nowarn (type
),
3234 LOOKUP_NORMAL
| (use_global_delete
* LOOKUP_GLOBAL
),
3238 /* Below, we will reverse the order in which these calls are made.
3239 If we have a destructor, then that destructor will take care
3240 of the base classes; otherwise, we must do that here. */
3241 if (TYPE_HAS_DESTRUCTOR (type
))
3243 tree do_delete
= NULL_TREE
;
3246 if (use_global_delete
&& auto_delete
== sfk_deleting_destructor
)
3248 /* Delete the object. */
3249 do_delete
= build_builtin_delete_call (addr
);
3250 /* Otherwise, treat this like a complete object destructor
3252 auto_delete
= sfk_complete_destructor
;
3255 expr
= build_dtor_call (ref
, auto_delete
, flags
);
3257 expr
= build (COMPOUND_EXPR
, void_type_node
, expr
, do_delete
);
3259 if (flags
& LOOKUP_DESTRUCTOR
)
3260 /* Explicit destructor call; don't check for null pointer. */
3261 ifexp
= integer_one_node
;
3263 /* Handle deleting a null pointer. */
3264 ifexp
= fold (cp_build_binary_op (NE_EXPR
, addr
, integer_zero_node
));
3266 if (ifexp
!= integer_one_node
)
3267 expr
= build (COND_EXPR
, void_type_node
,
3268 ifexp
, expr
, void_zero_node
);
3274 /* We only get here from finish_function for a destructor. */
3275 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (type
));
3276 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (type
);
3277 tree base_binfo
= n_baseclasses
> 0 ? TREE_VEC_ELT (binfos
, 0) : NULL_TREE
;
3278 tree exprstmt
= NULL_TREE
;
3280 /* Set this again before we call anything, as we might get called
3282 TYPE_HAS_DESTRUCTOR (type
) = 1;
3284 /* If we have member delete or vbases, we call delete in
3286 my_friendly_assert (auto_delete
== sfk_base_destructor
, 20000411);
3288 /* Take care of the remaining baseclasses. */
3289 for (i
= 0; i
< n_baseclasses
; i
++)
3291 base_binfo
= TREE_VEC_ELT (binfos
, i
);
3292 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
))
3293 || TREE_VIA_VIRTUAL (base_binfo
))
3296 expr
= build_scoped_method_call (ref
, base_binfo
,
3297 base_dtor_identifier
,
3300 exprstmt
= tree_cons (NULL_TREE
, expr
, exprstmt
);
3303 for (member
= TYPE_FIELDS (type
); member
; member
= TREE_CHAIN (member
))
3305 if (TREE_CODE (member
) != FIELD_DECL
)
3307 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member
)))
3309 tree this_member
= build_component_ref (ref
, DECL_NAME (member
), NULL_TREE
, 0);
3310 tree this_type
= TREE_TYPE (member
);
3311 expr
= build_delete (this_type
, this_member
,
3312 sfk_complete_destructor
, flags
, 0);
3313 exprstmt
= tree_cons (NULL_TREE
, expr
, exprstmt
);
3318 return build_compound_expr (exprstmt
);
3319 /* Virtual base classes make this function do nothing. */
3320 return void_zero_node
;
3324 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3327 build_vbase_delete (type
, decl
)
3330 tree vbases
= CLASSTYPE_VBASECLASSES (type
);
3331 tree result
= NULL_TREE
;
3332 tree addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
3334 my_friendly_assert (addr
!= error_mark_node
, 222);
3339 = convert_force (build_pointer_type (BINFO_TYPE (TREE_VALUE (vbases
))),
3341 result
= tree_cons (NULL_TREE
,
3342 build_delete (TREE_TYPE (this_addr
), this_addr
,
3343 sfk_base_destructor
,
3344 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0),
3346 vbases
= TREE_CHAIN (vbases
);
3348 return build_compound_expr (nreverse (result
));
3351 /* Build a C++ vector delete expression.
3352 MAXINDEX is the number of elements to be deleted.
3353 ELT_SIZE is the nominal size of each element in the vector.
3354 BASE is the expression that should yield the store to be deleted.
3355 This function expands (or synthesizes) these calls itself.
3356 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3358 This also calls delete for virtual baseclasses of elements of the vector.
3360 Update: MAXINDEX is no longer needed. The size can be extracted from the
3361 start of the vector for pointers, and from the type for arrays. We still
3362 use MAXINDEX for arrays because it happens to already have one of the
3363 values we'd have to extract. (We could use MAXINDEX with pointers to
3364 confirm the size, and trap if the numbers differ; not clear that it'd
3365 be worth bothering.) */
3368 build_vec_delete (base
, maxindex
, auto_delete_vec
, use_global_delete
)
3369 tree base
, maxindex
;
3370 special_function_kind auto_delete_vec
;
3371 int use_global_delete
;
3375 if (TREE_CODE (base
) == OFFSET_REF
)
3376 base
= resolve_offset_ref (base
);
3378 type
= TREE_TYPE (base
);
3380 base
= stabilize_reference (base
);
3382 /* Since we can use base many times, save_expr it. */
3383 if (TREE_SIDE_EFFECTS (base
))
3384 base
= save_expr (base
);
3386 if (TREE_CODE (type
) == POINTER_TYPE
)
3388 /* Step back one from start of vector, and read dimension. */
3391 type
= strip_array_types (TREE_TYPE (type
));
3394 cookie_addr
= build (MINUS_EXPR
,
3395 build_pointer_type (sizetype
),
3397 TYPE_SIZE_UNIT (sizetype
));
3398 maxindex
= build_indirect_ref (cookie_addr
, NULL_PTR
);
3404 cookie_addr
= build (MINUS_EXPR
, build_pointer_type (BI_header_type
),
3405 base
, get_cookie_size (type
));
3406 cookie
= build_indirect_ref (cookie_addr
, NULL_PTR
);
3407 maxindex
= build_component_ref (cookie
, nelts_identifier
,
3411 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3413 /* get the total number of things in the array, maxindex is a bad name */
3414 maxindex
= array_type_nelts_total (type
);
3415 type
= strip_array_types (type
);
3416 base
= build_unary_op (ADDR_EXPR
, base
, 1);
3420 if (base
!= error_mark_node
)
3421 error ("type to vector delete is neither pointer or array type");
3422 return error_mark_node
;
3425 return build_vec_delete_1 (base
, maxindex
, type
, auto_delete_vec
,