1 /* Handle initialization things in C++.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* High-level class interface. */
25 #include "coretypes.h"
28 #include "stringpool.h"
31 #include "c-family/c-ubsan.h"
33 #include "stringpool.h"
36 #include "stor-layout.h"
37 #include "pointer-query.h"
39 static bool begin_init_stmts (tree
*, tree
*);
40 static tree
finish_init_stmts (bool, tree
, tree
);
41 static void construct_virtual_base (tree
, tree
);
42 static bool expand_aggr_init_1 (tree
, tree
, tree
, tree
, int, tsubst_flags_t
);
43 static bool expand_default_init (tree
, tree
, tree
, tree
, int, tsubst_flags_t
);
44 static void perform_member_init (tree
, tree
);
45 static int member_init_ok_or_else (tree
, tree
, tree
);
46 static void expand_virtual_init (tree
, tree
);
47 static tree
sort_mem_initializers (tree
, tree
);
48 static tree
initializing_context (tree
);
49 static void expand_cleanup_for_base (tree
, tree
);
50 static tree
dfs_initialize_vtbl_ptrs (tree
, void *);
51 static tree
build_field_list (tree
, tree
, int *);
52 static int diagnose_uninitialized_cst_or_ref_member_1 (tree
, tree
, bool, bool);
54 static GTY(()) tree fn
;
56 /* We are about to generate some complex initialization code.
57 Conceptually, it is all a single expression. However, we may want
58 to include conditionals, loops, and other such statement-level
59 constructs. Therefore, we build the initialization code inside a
60 statement-expression. This function starts such an expression.
61 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
62 pass them back to finish_init_stmts when the expression is
66 begin_init_stmts (tree
*stmt_expr_p
, tree
*compound_stmt_p
)
68 bool is_global
= !building_stmt_list_p ();
70 *stmt_expr_p
= begin_stmt_expr ();
71 *compound_stmt_p
= begin_compound_stmt (BCS_NO_SCOPE
);
76 /* Finish out the statement-expression begun by the previous call to
77 begin_init_stmts. Returns the statement-expression itself. */
80 finish_init_stmts (bool is_global
, tree stmt_expr
, tree compound_stmt
)
82 finish_compound_stmt (compound_stmt
);
84 stmt_expr
= finish_stmt_expr (stmt_expr
, true);
86 gcc_assert (!building_stmt_list_p () == is_global
);
93 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
94 which we want to initialize the vtable pointer for, DATA is
95 TREE_LIST whose TREE_VALUE is the this ptr expression. */
98 dfs_initialize_vtbl_ptrs (tree binfo
, void *data
)
100 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
101 return dfs_skip_bases
;
103 if (!BINFO_PRIMARY_P (binfo
) || BINFO_VIRTUAL_P (binfo
))
105 tree base_ptr
= TREE_VALUE ((tree
) data
);
107 base_ptr
= build_base_path (PLUS_EXPR
, base_ptr
, binfo
, /*nonnull=*/1,
108 tf_warning_or_error
);
110 expand_virtual_init (binfo
, base_ptr
);
116 /* Initialize all the vtable pointers in the object pointed to by
120 initialize_vtbl_ptrs (tree addr
)
125 type
= TREE_TYPE (TREE_TYPE (addr
));
126 list
= build_tree_list (type
, addr
);
128 /* Walk through the hierarchy, initializing the vptr in each base
129 class. We do these in pre-order because we can't find the virtual
130 bases for a class until we've initialized the vtbl for that
132 dfs_walk_once (TYPE_BINFO (type
), dfs_initialize_vtbl_ptrs
, NULL
, list
);
135 /* Return an expression for the zero-initialization of an object with
136 type T. This expression will either be a constant (in the case
137 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
138 aggregate), or NULL (in the case that T does not require
139 initialization). In either case, the value can be used as
140 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
141 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
142 is the number of elements in the array. If STATIC_STORAGE_P is
143 TRUE, initializers are only generated for entities for which
144 zero-initialization does not simply mean filling the storage with
145 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
146 subfields with bit positions at or above that bit size shouldn't
147 be added. Note that this only works when the result is assigned
148 to a base COMPONENT_REF; if we only have a pointer to the base subobject,
149 expand_assignment will end up clearing the full size of TYPE. */
152 build_zero_init_1 (tree type
, tree nelts
, bool static_storage_p
,
155 tree init
= NULL_TREE
;
159 To zero-initialize an object of type T means:
161 -- if T is a scalar type, the storage is set to the value of zero
164 -- if T is a non-union class type, the storage for each non-static
165 data member and each base-class subobject is zero-initialized.
167 -- if T is a union type, the storage for its first data member is
170 -- if T is an array type, the storage for each element is
173 -- if T is a reference type, no initialization is performed. */
175 gcc_assert (nelts
== NULL_TREE
|| TREE_CODE (nelts
) == INTEGER_CST
);
177 if (type
== error_mark_node
)
179 else if (static_storage_p
&& zero_init_p (type
))
180 /* In order to save space, we do not explicitly build initializers
181 for items that do not need them. GCC's semantics are that
182 items with static storage duration that are not otherwise
183 initialized are initialized to zero. */
185 else if (TYPE_PTR_OR_PTRMEM_P (type
))
186 init
= fold (convert (type
, nullptr_node
));
187 else if (NULLPTR_TYPE_P (type
))
188 init
= build_int_cst (type
, 0);
189 else if (SCALAR_TYPE_P (type
))
190 init
= build_zero_cst (type
);
191 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type
)))
194 vec
<constructor_elt
, va_gc
> *v
= NULL
;
196 /* Iterate over the fields, building initializations. */
197 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
199 if (TREE_CODE (field
) != FIELD_DECL
)
202 if (TREE_TYPE (field
) == error_mark_node
)
205 /* Don't add virtual bases for base classes if they are beyond
206 the size of the current field, that means it is present
207 somewhere else in the object. */
210 tree bitpos
= bit_position (field
);
211 if (TREE_CODE (bitpos
) == INTEGER_CST
212 && !tree_int_cst_lt (bitpos
, field_size
))
216 /* Note that for class types there will be FIELD_DECLs
217 corresponding to base classes as well. Thus, iterating
218 over TYPE_FIELDs will result in correct initialization of
219 all of the subobjects. */
220 if (!static_storage_p
|| !zero_init_p (TREE_TYPE (field
)))
223 = (DECL_FIELD_IS_BASE (field
)
225 && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
226 ? DECL_SIZE (field
) : NULL_TREE
;
227 tree value
= build_zero_init_1 (TREE_TYPE (field
),
232 CONSTRUCTOR_APPEND_ELT(v
, field
, value
);
235 /* For unions, only the first field is initialized. */
236 if (TREE_CODE (type
) == UNION_TYPE
)
240 /* Build a constructor to contain the initializations. */
241 init
= build_constructor (type
, v
);
243 else if (TREE_CODE (type
) == ARRAY_TYPE
)
246 vec
<constructor_elt
, va_gc
> *v
= NULL
;
248 /* Iterate over the array elements, building initializations. */
250 max_index
= fold_build2_loc (input_location
, MINUS_EXPR
,
251 TREE_TYPE (nelts
), nelts
,
252 build_one_cst (TREE_TYPE (nelts
)));
253 /* Treat flexible array members like [0] arrays. */
254 else if (TYPE_DOMAIN (type
) == NULL_TREE
)
257 max_index
= array_type_nelts (type
);
259 /* If we have an error_mark here, we should just return error mark
260 as we don't know the size of the array yet. */
261 if (max_index
== error_mark_node
)
262 return error_mark_node
;
263 gcc_assert (TREE_CODE (max_index
) == INTEGER_CST
);
265 /* A zero-sized array, which is accepted as an extension, will
266 have an upper bound of -1. */
267 if (!integer_minus_onep (max_index
))
271 /* If this is a one element array, we just use a regular init. */
272 if (integer_zerop (max_index
))
273 ce
.index
= size_zero_node
;
275 ce
.index
= build2 (RANGE_EXPR
, sizetype
, size_zero_node
,
278 ce
.value
= build_zero_init_1 (TREE_TYPE (type
), /*nelts=*/NULL_TREE
,
279 static_storage_p
, NULL_TREE
);
287 /* Build a constructor to contain the initializations. */
288 init
= build_constructor (type
, v
);
290 else if (VECTOR_TYPE_P (type
))
291 init
= build_zero_cst (type
);
293 gcc_assert (TYPE_REF_P (type
));
295 /* In all cases, the initializer is a constant. */
297 TREE_CONSTANT (init
) = 1;
302 /* Return an expression for the zero-initialization of an object with
303 type T. This expression will either be a constant (in the case
304 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
305 aggregate), or NULL (in the case that T does not require
306 initialization). In either case, the value can be used as
307 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
308 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
309 is the number of elements in the array. If STATIC_STORAGE_P is
310 TRUE, initializers are only generated for entities for which
311 zero-initialization does not simply mean filling the storage with
315 build_zero_init (tree type
, tree nelts
, bool static_storage_p
)
317 return build_zero_init_1 (type
, nelts
, static_storage_p
, NULL_TREE
);
320 /* Return a suitable initializer for value-initializing an object of type
321 TYPE, as described in [dcl.init]. */
324 build_value_init (tree type
, tsubst_flags_t complain
)
328 To value-initialize an object of type T means:
330 - if T is a class type (clause 9) with either no default constructor
331 (12.1) or a default constructor that is user-provided or deleted,
332 then the object is default-initialized;
334 - if T is a (possibly cv-qualified) class type without a user-provided
335 or deleted default constructor, then the object is zero-initialized
336 and the semantic constraints for default-initialization are checked,
337 and if T has a non-trivial default constructor, the object is
340 - if T is an array type, then each element is value-initialized;
342 - otherwise, the object is zero-initialized.
344 A program that calls for default-initialization or
345 value-initialization of an entity of reference type is ill-formed. */
347 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
348 gcc_assert (!processing_template_decl
349 || (SCALAR_TYPE_P (type
) || TREE_CODE (type
) == ARRAY_TYPE
));
351 if (CLASS_TYPE_P (type
) && type_build_ctor_call (type
))
354 = build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
355 NULL
, type
, LOOKUP_NORMAL
, complain
);
356 if (ctor
== error_mark_node
|| TREE_CONSTANT (ctor
))
359 if (TREE_CODE (ctor
) == CALL_EXPR
)
360 fn
= get_callee_fndecl (ctor
);
361 ctor
= build_aggr_init_expr (type
, ctor
);
362 if (fn
&& user_provided_p (fn
))
364 else if (TYPE_HAS_COMPLEX_DFLT (type
))
366 /* This is a class that needs constructing, but doesn't have
367 a user-provided constructor. So we need to zero-initialize
368 the object and then call the implicitly defined ctor.
369 This will be handled in simplify_aggr_init_expr. */
370 AGGR_INIT_ZERO_FIRST (ctor
) = 1;
375 /* Discard any access checking during subobject initialization;
376 the checks are implied by the call to the ctor which we have
377 verified is OK (cpp0x/defaulted46.C). */
378 push_deferring_access_checks (dk_deferred
);
379 tree r
= build_value_init_noctor (type
, complain
);
380 pop_deferring_access_checks ();
384 /* Like build_value_init, but don't call the constructor for TYPE. Used
385 for base initializers. */
388 build_value_init_noctor (tree type
, tsubst_flags_t complain
)
390 if (!COMPLETE_TYPE_P (type
))
392 if (complain
& tf_error
)
393 error ("value-initialization of incomplete type %qT", type
);
394 return error_mark_node
;
396 /* FIXME the class and array cases should just use digest_init once it is
398 if (CLASS_TYPE_P (type
))
400 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type
)
403 if (TREE_CODE (type
) != UNION_TYPE
)
406 vec
<constructor_elt
, va_gc
> *v
= NULL
;
408 /* Iterate over the fields, building initializations. */
409 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
413 if (TREE_CODE (field
) != FIELD_DECL
)
416 ftype
= TREE_TYPE (field
);
418 if (ftype
== error_mark_node
)
421 /* Ignore flexible array members for value initialization. */
422 if (TREE_CODE (ftype
) == ARRAY_TYPE
423 && !COMPLETE_TYPE_P (ftype
)
424 && !TYPE_DOMAIN (ftype
)
425 && COMPLETE_TYPE_P (TREE_TYPE (ftype
))
426 && (next_initializable_field (DECL_CHAIN (field
))
430 /* Ignore unnamed zero-width bitfields. */
431 if (DECL_UNNAMED_BIT_FIELD (field
)
432 && integer_zerop (DECL_SIZE (field
)))
435 /* We could skip vfields and fields of types with
436 user-defined constructors, but I think that won't improve
437 performance at all; it should be simpler in general just
438 to zero out the entire object than try to only zero the
439 bits that actually need it. */
441 /* Note that for class types there will be FIELD_DECLs
442 corresponding to base classes as well. Thus, iterating
443 over TYPE_FIELDs will result in correct initialization of
444 all of the subobjects. */
445 value
= build_value_init (ftype
, complain
);
446 value
= maybe_constant_init (value
);
448 if (value
== error_mark_node
)
449 return error_mark_node
;
451 CONSTRUCTOR_APPEND_ELT(v
, field
, value
);
453 /* We shouldn't have gotten here for anything that would need
454 non-trivial initialization, and gimplify_init_ctor_preeval
455 would need to be fixed to allow it. */
456 gcc_assert (TREE_CODE (value
) != TARGET_EXPR
457 && TREE_CODE (value
) != AGGR_INIT_EXPR
);
460 /* Build a constructor to contain the zero- initializations. */
461 return build_constructor (type
, v
);
464 else if (TREE_CODE (type
) == ARRAY_TYPE
)
466 vec
<constructor_elt
, va_gc
> *v
= NULL
;
468 /* Iterate over the array elements, building initializations. */
469 tree max_index
= array_type_nelts (type
);
471 /* If we have an error_mark here, we should just return error mark
472 as we don't know the size of the array yet. */
473 if (max_index
== error_mark_node
)
475 if (complain
& tf_error
)
476 error ("cannot value-initialize array of unknown bound %qT",
478 return error_mark_node
;
480 gcc_assert (TREE_CODE (max_index
) == INTEGER_CST
);
482 /* A zero-sized array, which is accepted as an extension, will
483 have an upper bound of -1. */
484 if (!tree_int_cst_equal (max_index
, integer_minus_one_node
))
488 /* If this is a one element array, we just use a regular init. */
489 if (tree_int_cst_equal (size_zero_node
, max_index
))
490 ce
.index
= size_zero_node
;
492 ce
.index
= build2 (RANGE_EXPR
, sizetype
, size_zero_node
, max_index
);
494 ce
.value
= build_value_init (TREE_TYPE (type
), complain
);
495 ce
.value
= maybe_constant_init (ce
.value
);
496 if (ce
.value
== error_mark_node
)
497 return error_mark_node
;
502 /* We shouldn't have gotten here for anything that would need
503 non-trivial initialization, and gimplify_init_ctor_preeval
504 would need to be fixed to allow it. */
505 gcc_assert (TREE_CODE (ce
.value
) != TARGET_EXPR
506 && TREE_CODE (ce
.value
) != AGGR_INIT_EXPR
);
509 /* Build a constructor to contain the initializations. */
510 return build_constructor (type
, v
);
512 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
514 if (complain
& tf_error
)
515 error ("value-initialization of function type %qT", type
);
516 return error_mark_node
;
518 else if (TYPE_REF_P (type
))
520 if (complain
& tf_error
)
521 error ("value-initialization of reference type %qT", type
);
522 return error_mark_node
;
525 return build_zero_init (type
, NULL_TREE
, /*static_storage_p=*/false);
528 /* Initialize current class with INIT, a TREE_LIST of
529 arguments for a target constructor. If TREE_LIST is void_type_node,
530 an empty initializer list was given. */
533 perform_target_ctor (tree init
)
535 tree decl
= current_class_ref
;
536 tree type
= current_class_type
;
538 finish_expr_stmt (build_aggr_init (decl
, init
,
539 LOOKUP_NORMAL
|LOOKUP_DELEGATING_CONS
,
540 tf_warning_or_error
));
541 if (type_build_dtor_call (type
))
543 tree expr
= build_delete (input_location
,
544 type
, decl
, sfk_complete_destructor
,
548 0, tf_warning_or_error
);
549 if (expr
!= error_mark_node
550 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
551 finish_eh_cleanup (expr
);
555 /* Return the non-static data initializer for FIELD_DECL MEMBER. */
557 static GTY((cache
)) decl_tree_cache_map
*nsdmi_inst
;
560 get_nsdmi (tree member
, bool in_ctor
, tsubst_flags_t complain
)
563 tree save_ccp
= current_class_ptr
;
564 tree save_ccr
= current_class_ref
;
566 if (DECL_LANG_SPECIFIC (member
) && DECL_TEMPLATE_INFO (member
))
568 init
= DECL_INITIAL (DECL_TI_TEMPLATE (member
));
570 = cp_expr_loc_or_loc (init
, DECL_SOURCE_LOCATION (member
));
571 if (TREE_CODE (init
) == DEFERRED_PARSE
)
573 else if (tree
*slot
= hash_map_safe_get (nsdmi_inst
, member
))
575 /* Check recursive instantiation. */
576 else if (DECL_INSTANTIATING_NSDMI_P (member
))
578 if (complain
& tf_error
)
579 error_at (expr_loc
, "recursive instantiation of default member "
580 "initializer for %qD", member
);
581 init
= error_mark_node
;
587 location_t sloc
= input_location
;
588 input_location
= expr_loc
;
590 DECL_INSTANTIATING_NSDMI_P (member
) = 1;
593 tree ctx
= DECL_CONTEXT (member
);
595 processing_template_decl_sentinel
ptds (/*reset*/false);
596 if (!currently_open_class (ctx
))
598 if (!LOCAL_CLASS_P (ctx
))
599 push_to_top_level ();
601 /* push_to_top_level would lose the necessary function context,
602 just reset processing_template_decl. */
603 processing_template_decl
= 0;
604 push_nested_class (ctx
);
605 push_deferring_access_checks (dk_no_deferred
);
609 inject_this_parameter (ctx
, TYPE_UNQUALIFIED
);
611 start_lambda_scope (member
);
613 /* Do deferred instantiation of the NSDMI. */
614 init
= (tsubst_copy_and_build
615 (init
, DECL_TI_ARGS (member
),
616 complain
, member
, /*function_p=*/false,
617 /*integral_constant_expression_p=*/false));
618 init
= digest_nsdmi_init (member
, init
, complain
);
620 finish_lambda_scope ();
622 DECL_INSTANTIATING_NSDMI_P (member
) = 0;
624 if (init
!= error_mark_node
)
625 hash_map_safe_put
<hm_ggc
> (nsdmi_inst
, member
, init
);
629 pop_deferring_access_checks ();
631 if (!LOCAL_CLASS_P (ctx
))
632 pop_from_top_level ();
635 input_location
= sloc
;
639 init
= DECL_INITIAL (member
);
641 if (init
&& TREE_CODE (init
) == DEFERRED_PARSE
)
643 if (complain
& tf_error
)
645 error ("default member initializer for %qD required before the end "
646 "of its enclosing class", member
);
647 inform (location_of (init
), "defined here");
648 DECL_INITIAL (member
) = error_mark_node
;
650 init
= error_mark_node
;
655 current_class_ptr
= save_ccp
;
656 current_class_ref
= save_ccr
;
660 /* Use a PLACEHOLDER_EXPR when we don't have a 'this' parameter to
661 refer to; constexpr evaluation knows what to do with it. */
662 current_class_ref
= build0 (PLACEHOLDER_EXPR
, DECL_CONTEXT (member
));
663 current_class_ptr
= build_address (current_class_ref
);
666 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
667 so the aggregate init code below will see a CONSTRUCTOR. */
668 bool simple_target
= (init
&& SIMPLE_TARGET_EXPR_P (init
));
670 init
= TARGET_EXPR_INITIAL (init
);
671 init
= break_out_target_exprs (init
, /*loc*/true);
672 if (in_ctor
&& init
&& TREE_CODE (init
) == TARGET_EXPR
)
673 /* This expresses the full initialization, prevent perform_member_init from
674 calling another constructor (58162). */
675 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
676 if (simple_target
&& TREE_CODE (init
) != CONSTRUCTOR
)
677 /* Now put it back so C++17 copy elision works. */
678 init
= get_target_expr (init
);
680 current_class_ptr
= save_ccp
;
681 current_class_ref
= save_ccr
;
685 /* Diagnose the flexible array MEMBER if its INITializer is non-null
686 and return true if so. Otherwise return false. */
689 maybe_reject_flexarray_init (tree member
, tree init
)
691 tree type
= TREE_TYPE (member
);
694 || TREE_CODE (type
) != ARRAY_TYPE
695 || TYPE_DOMAIN (type
))
698 /* Point at the flexible array member declaration if it's initialized
699 in-class, and at the ctor if it's initialized in a ctor member
702 if (DECL_INITIAL (member
) == init
703 || !current_function_decl
704 || DECL_DEFAULTED_FN (current_function_decl
))
705 loc
= DECL_SOURCE_LOCATION (member
);
707 loc
= DECL_SOURCE_LOCATION (current_function_decl
);
709 error_at (loc
, "initializer for flexible array member %q#D", member
);
713 /* If INIT's value can come from a call to std::initializer_list<T>::begin,
714 return that function. Otherwise, NULL_TREE. */
717 find_list_begin (tree init
)
720 while (TREE_CODE (init
) == COMPOUND_EXPR
)
721 init
= TREE_OPERAND (init
, 1);
723 if (TREE_CODE (init
) == COND_EXPR
)
725 tree left
= TREE_OPERAND (init
, 1);
727 left
= TREE_OPERAND (init
, 0);
728 left
= find_list_begin (left
);
731 return find_list_begin (TREE_OPERAND (init
, 2));
733 if (TREE_CODE (init
) == CALL_EXPR
)
734 if (tree fn
= get_callee_fndecl (init
))
735 if (id_equal (DECL_NAME (fn
), "begin")
736 && is_std_init_list (DECL_CONTEXT (fn
)))
741 /* If INIT initializing MEMBER is copying the address of the underlying array
742 of an initializer_list, warn. */
745 maybe_warn_list_ctor (tree member
, tree init
)
747 tree memtype
= TREE_TYPE (member
);
748 if (!init
|| !TYPE_PTR_P (memtype
)
749 || !is_list_ctor (current_function_decl
))
752 tree parm
= FUNCTION_FIRST_USER_PARMTYPE (current_function_decl
);
753 parm
= TREE_VALUE (parm
);
754 tree initlist
= non_reference (parm
);
756 /* Do not warn if the parameter is an lvalue reference to non-const. */
757 if (TYPE_REF_P (parm
) && !TYPE_REF_IS_RVALUE (parm
)
758 && !CP_TYPE_CONST_P (initlist
))
761 tree targs
= CLASSTYPE_TI_ARGS (initlist
);
762 tree elttype
= TREE_VEC_ELT (targs
, 0);
764 if (!same_type_ignoring_top_level_qualifiers_p
765 (TREE_TYPE (memtype
), elttype
))
768 tree begin
= find_list_begin (init
);
772 location_t loc
= cp_expr_loc_or_input_loc (init
);
773 warning_at (loc
, OPT_Winit_list_lifetime
,
774 "initializing %qD from %qE does not extend the lifetime "
775 "of the underlying array", member
, begin
);
778 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
779 arguments. If TREE_LIST is void_type_node, an empty initializer
780 list was given; if NULL_TREE no initializer was given. */
783 perform_member_init (tree member
, tree init
)
786 tree type
= TREE_TYPE (member
);
788 /* Use the non-static data member initializer if there was no
789 mem-initializer for this field. */
790 if (init
== NULL_TREE
)
791 init
= get_nsdmi (member
, /*ctor*/true, tf_warning_or_error
);
793 if (init
== error_mark_node
)
796 /* Effective C++ rule 12 requires that all data members be
798 if (warn_ecpp
&& init
== NULL_TREE
&& TREE_CODE (type
) != ARRAY_TYPE
)
799 warning_at (DECL_SOURCE_LOCATION (current_function_decl
), OPT_Weffc__
,
800 "%qD should be initialized in the member initialization list",
803 /* Get an lvalue for the data member. */
804 decl
= build_class_member_access_expr (current_class_ref
, member
,
805 /*access_path=*/NULL_TREE
,
806 /*preserve_reference=*/true,
807 tf_warning_or_error
);
808 if (decl
== error_mark_node
)
811 if (warn_init_self
&& init
&& TREE_CODE (init
) == TREE_LIST
812 && TREE_CHAIN (init
) == NULL_TREE
)
814 tree val
= TREE_VALUE (init
);
815 /* Handle references. */
816 if (REFERENCE_REF_P (val
))
817 val
= TREE_OPERAND (val
, 0);
818 if (TREE_CODE (val
) == COMPONENT_REF
&& TREE_OPERAND (val
, 1) == member
819 && TREE_OPERAND (val
, 0) == current_class_ref
)
820 warning_at (DECL_SOURCE_LOCATION (current_function_decl
),
821 OPT_Winit_self
, "%qD is initialized with itself",
825 if (array_of_unknown_bound_p (type
))
827 maybe_reject_flexarray_init (member
, init
);
831 if (init
&& TREE_CODE (init
) == TREE_LIST
)
834 if (DIRECT_LIST_INIT_P (TREE_VALUE (init
)))
835 init
= build_x_compound_expr_from_list (init
, ELK_MEM_INIT
,
836 tf_warning_or_error
);
837 /* We are trying to initialize an array from a ()-list. If we
838 should attempt to do so, conjure up a CONSTRUCTOR. */
839 else if (TREE_CODE (type
) == ARRAY_TYPE
840 /* P0960 is a C++20 feature. */
841 && cxx_dialect
>= cxx20
)
842 init
= do_aggregate_paren_init (init
, type
);
843 else if (!CLASS_TYPE_P (type
))
844 init
= build_x_compound_expr_from_list (init
, ELK_MEM_INIT
,
845 tf_warning_or_error
);
846 /* If we're initializing a class from a ()-list, leave the TREE_LIST
847 alone: we might call an appropriate constructor, or (in C++20)
848 do aggregate-initialization. */
851 if (init
== void_type_node
)
853 /* mem() means value-initialization. */
854 if (TREE_CODE (type
) == ARRAY_TYPE
)
856 init
= build_vec_init_expr (type
, init
, tf_warning_or_error
);
857 init
= build2 (INIT_EXPR
, type
, decl
, init
);
858 finish_expr_stmt (init
);
862 tree value
= build_value_init (type
, tf_warning_or_error
);
863 if (value
== error_mark_node
)
865 init
= build2 (INIT_EXPR
, type
, decl
, value
);
866 finish_expr_stmt (init
);
869 /* Deal with this here, as we will get confused if we try to call the
870 assignment op for an anonymous union. This can happen in a
871 synthesized copy constructor. */
872 else if (ANON_AGGR_TYPE_P (type
))
876 init
= build2 (INIT_EXPR
, type
, decl
, TREE_VALUE (init
));
877 finish_expr_stmt (init
);
881 && (TYPE_REF_P (type
)
882 || (TREE_CODE (init
) == CONSTRUCTOR
883 && (CP_AGGREGATE_TYPE_P (type
)
884 || is_std_init_list (type
)))))
886 /* With references and list-initialization, we need to deal with
887 extending temporary lifetimes. 12.2p5: "A temporary bound to a
888 reference member in a constructor’s ctor-initializer (12.6.2)
889 persists until the constructor exits." */
891 releasing_vec cleanups
;
892 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
), type
))
894 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
895 && CP_AGGREGATE_TYPE_P (type
))
896 init
= reshape_init (type
, init
, tf_warning_or_error
);
897 init
= digest_init (type
, init
, tf_warning_or_error
);
899 if (init
== error_mark_node
)
901 if (is_empty_field (member
)
902 && !TREE_SIDE_EFFECTS (init
))
903 /* Don't add trivial initialization of an empty base/field, as they
904 might not be ordered the way the back-end expects. */
906 /* A FIELD_DECL doesn't really have a suitable lifetime, but
907 make_temporary_var_for_ref_to_temp will treat it as automatic and
908 set_up_extended_ref_temp wants to use the decl in a warning. */
909 init
= extend_ref_init_temps (member
, init
, &cleanups
);
910 if (TREE_CODE (type
) == ARRAY_TYPE
911 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type
)))
912 init
= build_vec_init_expr (type
, init
, tf_warning_or_error
);
913 init
= build2 (INIT_EXPR
, type
, decl
, init
);
914 finish_expr_stmt (init
);
915 FOR_EACH_VEC_ELT (*cleanups
, i
, t
)
916 push_cleanup (decl
, t
, false);
918 else if (type_build_ctor_call (type
)
919 || (init
&& CLASS_TYPE_P (strip_array_types (type
))))
921 if (TREE_CODE (type
) == ARRAY_TYPE
)
923 if (init
== NULL_TREE
924 || same_type_ignoring_top_level_qualifiers_p (type
,
927 if (TYPE_DOMAIN (type
) && TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
929 /* Initialize the array only if it's not a flexible
930 array member (i.e., if it has an upper bound). */
931 init
= build_vec_init_expr (type
, init
, tf_warning_or_error
);
932 init
= build2 (INIT_EXPR
, type
, decl
, init
);
933 finish_expr_stmt (init
);
937 error ("invalid initializer for array member %q#D", member
);
941 int flags
= LOOKUP_NORMAL
;
942 if (DECL_DEFAULTED_FN (current_function_decl
))
943 flags
|= LOOKUP_DEFAULTED
;
944 if (CP_TYPE_CONST_P (type
)
946 && default_init_uninitialized_part (type
))
948 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
949 vtable; still give this diagnostic. */
950 auto_diagnostic_group d
;
951 if (permerror (DECL_SOURCE_LOCATION (current_function_decl
),
952 "uninitialized const member in %q#T", type
))
953 inform (DECL_SOURCE_LOCATION (member
),
954 "%q#D should be initialized", member
);
956 finish_expr_stmt (build_aggr_init (decl
, init
, flags
,
957 tf_warning_or_error
));
962 if (init
== NULL_TREE
)
965 /* member traversal: note it leaves init NULL */
966 if (TYPE_REF_P (type
))
968 auto_diagnostic_group d
;
969 if (permerror (DECL_SOURCE_LOCATION (current_function_decl
),
970 "uninitialized reference member in %q#T", type
))
971 inform (DECL_SOURCE_LOCATION (member
),
972 "%q#D should be initialized", member
);
974 else if (CP_TYPE_CONST_P (type
))
976 auto_diagnostic_group d
;
977 if (permerror (DECL_SOURCE_LOCATION (current_function_decl
),
978 "uninitialized const member in %q#T", type
))
979 inform (DECL_SOURCE_LOCATION (member
),
980 "%q#D should be initialized", member
);
983 core_type
= strip_array_types (type
);
985 if (CLASS_TYPE_P (core_type
)
986 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type
)
987 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type
)))
988 diagnose_uninitialized_cst_or_ref_member (core_type
,
993 maybe_warn_list_ctor (member
, init
);
996 finish_expr_stmt (cp_build_modify_expr (input_location
, decl
,
998 tf_warning_or_error
));
1001 if (type_build_dtor_call (type
))
1005 expr
= build_class_member_access_expr (current_class_ref
, member
,
1006 /*access_path=*/NULL_TREE
,
1007 /*preserve_reference=*/false,
1008 tf_warning_or_error
);
1009 expr
= build_delete (input_location
,
1010 type
, expr
, sfk_complete_destructor
,
1011 LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0,
1012 tf_warning_or_error
);
1014 if (expr
!= error_mark_node
1015 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1016 finish_eh_cleanup (expr
);
1020 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
1021 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
1024 build_field_list (tree t
, tree list
, int *uses_unions_or_anon_p
)
1028 /* Note whether or not T is a union. */
1029 if (TREE_CODE (t
) == UNION_TYPE
)
1030 *uses_unions_or_anon_p
= 1;
1032 for (fields
= TYPE_FIELDS (t
); fields
; fields
= DECL_CHAIN (fields
))
1036 /* Skip CONST_DECLs for enumeration constants and so forth. */
1037 if (TREE_CODE (fields
) != FIELD_DECL
|| DECL_ARTIFICIAL (fields
))
1040 fieldtype
= TREE_TYPE (fields
);
1042 /* For an anonymous struct or union, we must recursively
1043 consider the fields of the anonymous type. They can be
1044 directly initialized from the constructor. */
1045 if (ANON_AGGR_TYPE_P (fieldtype
))
1047 /* Add this field itself. Synthesized copy constructors
1048 initialize the entire aggregate. */
1049 list
= tree_cons (fields
, NULL_TREE
, list
);
1050 /* And now add the fields in the anonymous aggregate. */
1051 list
= build_field_list (fieldtype
, list
, uses_unions_or_anon_p
);
1052 *uses_unions_or_anon_p
= 1;
1054 /* Add this field. */
1055 else if (DECL_NAME (fields
))
1056 list
= tree_cons (fields
, NULL_TREE
, list
);
1062 /* Return the innermost aggregate scope for FIELD, whether that is
1063 the enclosing class or an anonymous aggregate within it. */
1066 innermost_aggr_scope (tree field
)
1068 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1069 return TREE_TYPE (field
);
1071 return DECL_CONTEXT (field
);
1074 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
1075 a FIELD_DECL or BINFO in T that needs initialization. The
1076 TREE_VALUE gives the initializer, or list of initializer arguments.
1078 Return a TREE_LIST containing all of the initializations required
1079 for T, in the order in which they should be performed. The output
1080 list has the same format as the input. */
1083 sort_mem_initializers (tree t
, tree mem_inits
)
1086 tree base
, binfo
, base_binfo
;
1088 tree next_subobject
;
1089 vec
<tree
, va_gc
> *vbases
;
1091 int uses_unions_or_anon_p
= 0;
1093 /* Build up a list of initializations. The TREE_PURPOSE of entry
1094 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
1095 TREE_VALUE will be the constructor arguments, or NULL if no
1096 explicit initialization was provided. */
1097 sorted_inits
= NULL_TREE
;
1099 /* Process the virtual bases. */
1100 for (vbases
= CLASSTYPE_VBASECLASSES (t
), i
= 0;
1101 vec_safe_iterate (vbases
, i
, &base
); i
++)
1102 sorted_inits
= tree_cons (base
, NULL_TREE
, sorted_inits
);
1104 /* Process the direct bases. */
1105 for (binfo
= TYPE_BINFO (t
), i
= 0;
1106 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
1107 if (!BINFO_VIRTUAL_P (base_binfo
))
1108 sorted_inits
= tree_cons (base_binfo
, NULL_TREE
, sorted_inits
);
1110 /* Process the non-static data members. */
1111 sorted_inits
= build_field_list (t
, sorted_inits
, &uses_unions_or_anon_p
);
1112 /* Reverse the entire list of initializations, so that they are in
1113 the order that they will actually be performed. */
1114 sorted_inits
= nreverse (sorted_inits
);
1116 /* If the user presented the initializers in an order different from
1117 that in which they will actually occur, we issue a warning. Keep
1118 track of the next subobject which can be explicitly initialized
1119 without issuing a warning. */
1120 next_subobject
= sorted_inits
;
1122 /* Go through the explicit initializers, filling in TREE_PURPOSE in
1123 the SORTED_INITS. */
1124 for (init
= mem_inits
; init
; init
= TREE_CHAIN (init
))
1127 tree subobject_init
;
1129 subobject
= TREE_PURPOSE (init
);
1131 /* If the explicit initializers are in sorted order, then
1132 SUBOBJECT will be NEXT_SUBOBJECT, or something following
1134 for (subobject_init
= next_subobject
;
1136 subobject_init
= TREE_CHAIN (subobject_init
))
1137 if (TREE_PURPOSE (subobject_init
) == subobject
)
1140 /* Issue a warning if the explicit initializer order does not
1141 match that which will actually occur.
1142 ??? Are all these on the correct lines? */
1143 if (warn_reorder
&& !subobject_init
)
1145 if (TREE_CODE (TREE_PURPOSE (next_subobject
)) == FIELD_DECL
)
1146 warning_at (DECL_SOURCE_LOCATION (TREE_PURPOSE (next_subobject
)),
1147 OPT_Wreorder
, "%qD will be initialized after",
1148 TREE_PURPOSE (next_subobject
));
1150 warning (OPT_Wreorder
, "base %qT will be initialized after",
1151 TREE_PURPOSE (next_subobject
));
1152 if (TREE_CODE (subobject
) == FIELD_DECL
)
1153 warning_at (DECL_SOURCE_LOCATION (subobject
),
1154 OPT_Wreorder
, " %q#D", subobject
);
1156 warning (OPT_Wreorder
, " base %qT", subobject
);
1157 warning_at (DECL_SOURCE_LOCATION (current_function_decl
),
1158 OPT_Wreorder
, " when initialized here");
1161 /* Look again, from the beginning of the list. */
1162 if (!subobject_init
)
1164 subobject_init
= sorted_inits
;
1165 while (TREE_PURPOSE (subobject_init
) != subobject
)
1166 subobject_init
= TREE_CHAIN (subobject_init
);
1169 /* It is invalid to initialize the same subobject more than
1171 if (TREE_VALUE (subobject_init
))
1173 if (TREE_CODE (subobject
) == FIELD_DECL
)
1174 error_at (DECL_SOURCE_LOCATION (current_function_decl
),
1175 "multiple initializations given for %qD",
1178 error_at (DECL_SOURCE_LOCATION (current_function_decl
),
1179 "multiple initializations given for base %qT",
1183 /* Record the initialization. */
1184 TREE_VALUE (subobject_init
) = TREE_VALUE (init
);
1185 /* Carry over the dummy TREE_TYPE node containing the source location. */
1186 TREE_TYPE (subobject_init
) = TREE_TYPE (init
);
1187 next_subobject
= subobject_init
;
1190 /* [class.base.init]
1192 If a ctor-initializer specifies more than one mem-initializer for
1193 multiple members of the same union (including members of
1194 anonymous unions), the ctor-initializer is ill-formed.
1196 Here we also splice out uninitialized union members. */
1197 if (uses_unions_or_anon_p
)
1199 tree
*last_p
= NULL
;
1201 for (p
= &sorted_inits
; *p
; )
1208 field
= TREE_PURPOSE (init
);
1210 /* Skip base classes. */
1211 if (TREE_CODE (field
) != FIELD_DECL
)
1214 /* If this is an anonymous aggregate with no explicit initializer,
1216 if (!TREE_VALUE (init
) && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1219 /* See if this field is a member of a union, or a member of a
1220 structure contained in a union, etc. */
1221 ctx
= innermost_aggr_scope (field
);
1223 /* If this field is not a member of a union, skip it. */
1224 if (TREE_CODE (ctx
) != UNION_TYPE
1225 && !ANON_AGGR_TYPE_P (ctx
))
1228 /* If this union member has no explicit initializer and no NSDMI,
1230 if (TREE_VALUE (init
) || DECL_INITIAL (field
))
1235 /* It's only an error if we have two initializers for the same
1243 /* See if LAST_FIELD and the field initialized by INIT are
1244 members of the same union (or the union itself). If so, there's
1245 a problem, unless they're actually members of the same structure
1246 which is itself a member of a union. For example, given:
1248 union { struct { int i; int j; }; };
1250 initializing both `i' and `j' makes sense. */
1251 ctx
= common_enclosing_class
1252 (innermost_aggr_scope (field
),
1253 innermost_aggr_scope (TREE_PURPOSE (*last_p
)));
1255 if (ctx
&& (TREE_CODE (ctx
) == UNION_TYPE
1256 || ctx
== TREE_TYPE (TREE_PURPOSE (*last_p
))))
1258 /* A mem-initializer hides an NSDMI. */
1259 if (TREE_VALUE (init
) && !TREE_VALUE (*last_p
))
1260 *last_p
= TREE_CHAIN (*last_p
);
1261 else if (TREE_VALUE (*last_p
) && !TREE_VALUE (init
))
1265 error_at (DECL_SOURCE_LOCATION (current_function_decl
),
1266 "initializations for multiple members of %qT",
1275 p
= &TREE_CHAIN (*p
);
1278 *p
= TREE_CHAIN (*p
);
1283 return sorted_inits
;
1286 /* Callback for cp_walk_tree to mark all PARM_DECLs in a tree as read. */
1289 mark_exp_read_r (tree
*tp
, int *, void *)
1292 if (TREE_CODE (t
) == PARM_DECL
)
1297 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1298 is a TREE_LIST giving the explicit mem-initializer-list for the
1299 constructor. The TREE_PURPOSE of each entry is a subobject (a
1300 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1301 is a TREE_LIST giving the arguments to the constructor or
1302 void_type_node for an empty list of arguments. */
1305 emit_mem_initializers (tree mem_inits
)
1307 int flags
= LOOKUP_NORMAL
;
1309 /* We will already have issued an error message about the fact that
1310 the type is incomplete. */
1311 if (!COMPLETE_TYPE_P (current_class_type
))
1315 && TYPE_P (TREE_PURPOSE (mem_inits
))
1316 && same_type_p (TREE_PURPOSE (mem_inits
), current_class_type
))
1318 /* Delegating constructor. */
1319 gcc_assert (TREE_CHAIN (mem_inits
) == NULL_TREE
);
1320 perform_target_ctor (TREE_VALUE (mem_inits
));
1324 if (DECL_DEFAULTED_FN (current_function_decl
)
1325 && ! DECL_INHERITED_CTOR (current_function_decl
))
1326 flags
|= LOOKUP_DEFAULTED
;
1328 /* Sort the mem-initializers into the order in which the
1329 initializations should be performed. */
1330 mem_inits
= sort_mem_initializers (current_class_type
, mem_inits
);
1332 in_base_initializer
= 1;
1334 /* Initialize base classes. */
1336 && TREE_CODE (TREE_PURPOSE (mem_inits
)) != FIELD_DECL
);
1337 mem_inits
= TREE_CHAIN (mem_inits
))
1339 tree subobject
= TREE_PURPOSE (mem_inits
);
1340 tree arguments
= TREE_VALUE (mem_inits
);
1342 /* We already have issued an error message. */
1343 if (arguments
== error_mark_node
)
1346 /* Suppress access control when calling the inherited ctor. */
1347 bool inherited_base
= (DECL_INHERITED_CTOR (current_function_decl
)
1348 && flag_new_inheriting_ctors
1351 push_deferring_access_checks (dk_deferred
);
1353 if (arguments
== NULL_TREE
)
1355 /* If these initializations are taking place in a copy constructor,
1356 the base class should probably be explicitly initialized if there
1357 is a user-defined constructor in the base class (other than the
1358 default constructor, which will be called anyway). */
1360 && DECL_COPY_CONSTRUCTOR_P (current_function_decl
)
1361 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject
)))
1362 warning_at (DECL_SOURCE_LOCATION (current_function_decl
),
1363 OPT_Wextra
, "base class %q#T should be explicitly "
1364 "initialized in the copy constructor",
1365 BINFO_TYPE (subobject
));
1368 /* Initialize the base. */
1369 if (!BINFO_VIRTUAL_P (subobject
))
1373 base_addr
= build_base_path (PLUS_EXPR
, current_class_ptr
,
1374 subobject
, 1, tf_warning_or_error
);
1375 expand_aggr_init_1 (subobject
, NULL_TREE
,
1376 cp_build_fold_indirect_ref (base_addr
),
1379 tf_warning_or_error
);
1380 expand_cleanup_for_base (subobject
, NULL_TREE
);
1382 else if (!ABSTRACT_CLASS_TYPE_P (current_class_type
))
1383 /* C++14 DR1658 Means we do not have to construct vbases of
1384 abstract classes. */
1385 construct_virtual_base (subobject
, arguments
);
1387 /* When not constructing vbases of abstract classes, at least mark
1388 the arguments expressions as read to avoid
1389 -Wunused-but-set-parameter false positives. */
1390 cp_walk_tree (&arguments
, mark_exp_read_r
, NULL
, NULL
);
1393 pop_deferring_access_checks ();
1395 in_base_initializer
= 0;
1397 /* Initialize the vptrs. */
1398 initialize_vtbl_ptrs (current_class_ptr
);
1400 /* Initialize the data members. */
1403 /* If this initializer was explicitly provided, then the dummy TREE_TYPE
1404 node contains the source location. */
1405 iloc_sentinel
ils (EXPR_LOCATION (TREE_TYPE (mem_inits
)));
1407 perform_member_init (TREE_PURPOSE (mem_inits
),
1408 TREE_VALUE (mem_inits
));
1409 mem_inits
= TREE_CHAIN (mem_inits
);
1413 /* Returns the address of the vtable (i.e., the value that should be
1414 assigned to the vptr) for BINFO. */
1417 build_vtbl_address (tree binfo
)
1419 tree binfo_for
= binfo
;
1422 if (BINFO_VPTR_INDEX (binfo
) && BINFO_VIRTUAL_P (binfo
))
1423 /* If this is a virtual primary base, then the vtable we want to store
1424 is that for the base this is being used as the primary base of. We
1425 can't simply skip the initialization, because we may be expanding the
1426 inits of a subobject constructor where the virtual base layout
1427 can be different. */
1428 while (BINFO_PRIMARY_P (binfo_for
))
1429 binfo_for
= BINFO_INHERITANCE_CHAIN (binfo_for
);
1431 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1433 vtbl
= get_vtbl_decl_for_binfo (binfo_for
);
1434 TREE_USED (vtbl
) = true;
1436 /* Now compute the address to use when initializing the vptr. */
1437 vtbl
= unshare_expr (BINFO_VTABLE (binfo_for
));
1439 vtbl
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (vtbl
)), vtbl
);
1444 /* This code sets up the virtual function tables appropriate for
1445 the pointer DECL. It is a one-ply initialization.
1447 BINFO is the exact type that DECL is supposed to be. In
1448 multiple inheritance, this might mean "C's A" if C : A, B. */
1451 expand_virtual_init (tree binfo
, tree decl
)
1453 tree vtbl
, vtbl_ptr
;
1456 /* Compute the initializer for vptr. */
1457 vtbl
= build_vtbl_address (binfo
);
1459 /* We may get this vptr from a VTT, if this is a subobject
1460 constructor or subobject destructor. */
1461 vtt_index
= BINFO_VPTR_INDEX (binfo
);
1467 /* Compute the value to use, when there's a VTT. */
1468 vtt_parm
= current_vtt_parm
;
1469 vtbl2
= fold_build_pointer_plus (vtt_parm
, vtt_index
);
1470 vtbl2
= cp_build_fold_indirect_ref (vtbl2
);
1471 vtbl2
= convert (TREE_TYPE (vtbl
), vtbl2
);
1473 /* The actual initializer is the VTT value only in the subobject
1474 constructor. In maybe_clone_body we'll substitute NULL for
1475 the vtt_parm in the case of the non-subobject constructor. */
1476 vtbl
= build_if_in_charge (vtbl
, vtbl2
);
1479 /* Compute the location of the vtpr. */
1480 vtbl_ptr
= build_vfield_ref (cp_build_fold_indirect_ref (decl
),
1482 gcc_assert (vtbl_ptr
!= error_mark_node
);
1484 /* Assign the vtable to the vptr. */
1485 vtbl
= convert_force (TREE_TYPE (vtbl_ptr
), vtbl
, 0, tf_warning_or_error
);
1486 finish_expr_stmt (cp_build_modify_expr (input_location
, vtbl_ptr
, NOP_EXPR
,
1487 vtbl
, tf_warning_or_error
));
1490 /* If an exception is thrown in a constructor, those base classes already
1491 constructed must be destroyed. This function creates the cleanup
1492 for BINFO, which has just been constructed. If FLAG is non-NULL,
1493 it is a DECL which is nonzero when this base needs to be
1497 expand_cleanup_for_base (tree binfo
, tree flag
)
1501 if (!type_build_dtor_call (BINFO_TYPE (binfo
)))
1504 /* Call the destructor. */
1505 expr
= build_special_member_call (current_class_ref
,
1506 base_dtor_identifier
,
1509 LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
,
1510 tf_warning_or_error
);
1512 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo
)))
1516 expr
= fold_build3_loc (input_location
,
1517 COND_EXPR
, void_type_node
,
1518 c_common_truthvalue_conversion (input_location
, flag
),
1519 expr
, integer_zero_node
);
1521 finish_eh_cleanup (expr
);
1524 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1528 construct_virtual_base (tree vbase
, tree arguments
)
1534 /* If there are virtual base classes with destructors, we need to
1535 emit cleanups to destroy them if an exception is thrown during
1536 the construction process. These exception regions (i.e., the
1537 period during which the cleanups must occur) begin from the time
1538 the construction is complete to the end of the function. If we
1539 create a conditional block in which to initialize the
1540 base-classes, then the cleanup region for the virtual base begins
1541 inside a block, and ends outside of that block. This situation
1542 confuses the sjlj exception-handling code. Therefore, we do not
1543 create a single conditional block, but one for each
1544 initialization. (That way the cleanup regions always begin
1545 in the outer block.) We trust the back end to figure out
1546 that the FLAG will not change across initializations, and
1547 avoid doing multiple tests. */
1548 flag
= DECL_CHAIN (DECL_ARGUMENTS (current_function_decl
));
1549 inner_if_stmt
= begin_if_stmt ();
1550 finish_if_stmt_cond (flag
, inner_if_stmt
);
1552 /* Compute the location of the virtual base. If we're
1553 constructing virtual bases, then we must be the most derived
1554 class. Therefore, we don't have to look up the virtual base;
1555 we already know where it is. */
1556 exp
= convert_to_base_statically (current_class_ref
, vbase
);
1558 expand_aggr_init_1 (vbase
, current_class_ref
, exp
, arguments
,
1559 0, tf_warning_or_error
);
1560 finish_then_clause (inner_if_stmt
);
1561 finish_if_stmt (inner_if_stmt
);
1563 expand_cleanup_for_base (vbase
, flag
);
1566 /* Find the context in which this FIELD can be initialized. */
1569 initializing_context (tree field
)
1571 tree t
= DECL_CONTEXT (field
);
1573 /* Anonymous union members can be initialized in the first enclosing
1574 non-anonymous union context. */
1575 while (t
&& ANON_AGGR_TYPE_P (t
))
1576 t
= TYPE_CONTEXT (t
);
1580 /* Function to give error message if member initialization specification
1581 is erroneous. FIELD is the member we decided to initialize.
1582 TYPE is the type for which the initialization is being performed.
1583 FIELD must be a member of TYPE.
1585 MEMBER_NAME is the name of the member. */
1588 member_init_ok_or_else (tree field
, tree type
, tree member_name
)
1590 if (field
== error_mark_node
)
1594 error ("class %qT does not have any field named %qD", type
,
1600 error ("%q#D is a static data member; it can only be "
1601 "initialized at its definition",
1605 if (TREE_CODE (field
) != FIELD_DECL
)
1607 error ("%q#D is not a non-static data member of %qT",
1611 if (initializing_context (field
) != type
)
1613 error ("class %qT does not have any field named %qD", type
,
1621 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1622 is a _TYPE node or TYPE_DECL which names a base for that type.
1623 Check the validity of NAME, and return either the base _TYPE, base
1624 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1625 NULL_TREE and issue a diagnostic.
1627 An old style unnamed direct single base construction is permitted,
1628 where NAME is NULL. */
1631 expand_member_init (tree name
)
1636 if (!current_class_ref
)
1641 /* This is an obsolete unnamed base class initializer. The
1642 parser will already have warned about its use. */
1643 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type
)))
1646 error ("unnamed initializer for %qT, which has no base classes",
1647 current_class_type
);
1650 basetype
= BINFO_TYPE
1651 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type
), 0));
1654 error ("unnamed initializer for %qT, which uses multiple inheritance",
1655 current_class_type
);
1659 else if (TYPE_P (name
))
1661 basetype
= TYPE_MAIN_VARIANT (name
);
1662 name
= TYPE_NAME (name
);
1664 else if (TREE_CODE (name
) == TYPE_DECL
)
1665 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (name
));
1667 basetype
= NULL_TREE
;
1676 if (current_template_parms
1677 || same_type_p (basetype
, current_class_type
))
1680 class_binfo
= TYPE_BINFO (current_class_type
);
1681 direct_binfo
= NULL_TREE
;
1682 virtual_binfo
= NULL_TREE
;
1684 /* Look for a direct base. */
1685 for (i
= 0; BINFO_BASE_ITERATE (class_binfo
, i
, direct_binfo
); ++i
)
1686 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo
), basetype
))
1689 /* Look for a virtual base -- unless the direct base is itself
1691 if (!direct_binfo
|| !BINFO_VIRTUAL_P (direct_binfo
))
1692 virtual_binfo
= binfo_for_vbase (basetype
, current_class_type
);
1694 /* [class.base.init]
1696 If a mem-initializer-id is ambiguous because it designates
1697 both a direct non-virtual base class and an inherited virtual
1698 base class, the mem-initializer is ill-formed. */
1699 if (direct_binfo
&& virtual_binfo
)
1701 error ("%qD is both a direct base and an indirect virtual base",
1706 if (!direct_binfo
&& !virtual_binfo
)
1708 if (CLASSTYPE_VBASECLASSES (current_class_type
))
1709 error ("type %qT is not a direct or virtual base of %qT",
1710 basetype
, current_class_type
);
1712 error ("type %qT is not a direct base of %qT",
1713 basetype
, current_class_type
);
1717 return direct_binfo
? direct_binfo
: virtual_binfo
;
1721 if (identifier_p (name
))
1722 field
= lookup_field (current_class_type
, name
, 1, false);
1726 if (member_init_ok_or_else (field
, current_class_type
, name
))
1733 /* This is like `expand_member_init', only it stores one aggregate
1736 INIT comes in two flavors: it is either a value which
1737 is to be stored in EXP, or it is a parameter list
1738 to go to a constructor, which will operate on EXP.
1739 If INIT is not a parameter list for a constructor, then set
1740 LOOKUP_ONLYCONVERTING.
1741 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1742 the initializer, if FLAGS is 0, then it is the (init) form.
1743 If `init' is a CONSTRUCTOR, then we emit a warning message,
1744 explaining that such initializations are invalid.
1746 If INIT resolves to a CALL_EXPR which happens to return
1747 something of the type we are looking for, then we know
1748 that we can safely use that call to perform the
1751 The virtual function table pointer cannot be set up here, because
1752 we do not really know its type.
1754 This never calls operator=().
1756 When initializing, nothing is CONST.
1758 A default copy constructor may have to be used to perform the
1761 A constructor or a conversion operator may have to be used to
1762 perform the initialization, but not both, as it would be ambiguous. */
1765 build_aggr_init (tree exp
, tree init
, int flags
, tsubst_flags_t complain
)
1770 tree type
= TREE_TYPE (exp
);
1771 int was_const
= TREE_READONLY (exp
);
1772 int was_volatile
= TREE_THIS_VOLATILE (exp
);
1775 if (init
== error_mark_node
)
1776 return error_mark_node
;
1778 location_t init_loc
= (init
1779 ? cp_expr_loc_or_input_loc (init
)
1780 : location_of (exp
));
1782 TREE_READONLY (exp
) = 0;
1783 TREE_THIS_VOLATILE (exp
) = 0;
1785 if (TREE_CODE (type
) == ARRAY_TYPE
)
1787 tree itype
= init
? TREE_TYPE (init
) : NULL_TREE
;
1790 if (VAR_P (exp
) && DECL_DECOMPOSITION_P (exp
))
1793 init
= mark_rvalue_use (init
);
1795 && DECL_P (tree_strip_any_location_wrapper (init
))
1796 && !(flags
& LOOKUP_ONLYCONVERTING
))
1798 /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init
1799 recognizes it as direct-initialization. */
1800 init
= build_constructor_single (init_list_type_node
,
1802 CONSTRUCTOR_IS_DIRECT_INIT (init
) = true;
1807 /* Must arrange to initialize each element of EXP
1808 from elements of INIT. */
1809 if (cv_qualified_p (type
))
1810 TREE_TYPE (exp
) = cv_unqualified (type
);
1811 if (itype
&& cv_qualified_p (itype
))
1812 TREE_TYPE (init
) = cv_unqualified (itype
);
1813 from_array
= (itype
&& same_type_p (TREE_TYPE (init
),
1816 if (init
&& !BRACE_ENCLOSED_INITIALIZER_P (init
)
1818 || (TREE_CODE (init
) != CONSTRUCTOR
1819 /* Can happen, eg, handling the compound-literals
1820 extension (ext/complit12.C). */
1821 && TREE_CODE (init
) != TARGET_EXPR
)))
1823 if (complain
& tf_error
)
1824 error_at (init_loc
, "array must be initialized "
1825 "with a brace-enclosed initializer");
1826 return error_mark_node
;
1830 stmt_expr
= build_vec_init (exp
, NULL_TREE
, init
,
1831 /*explicit_value_init_p=*/false,
1834 TREE_READONLY (exp
) = was_const
;
1835 TREE_THIS_VOLATILE (exp
) = was_volatile
;
1836 TREE_TYPE (exp
) = type
;
1837 /* Restore the type of init unless it was used directly. */
1838 if (init
&& TREE_CODE (stmt_expr
) != INIT_EXPR
)
1839 TREE_TYPE (init
) = itype
;
1843 if (init
&& init
!= void_type_node
1844 && TREE_CODE (init
) != TREE_LIST
1845 && !(TREE_CODE (init
) == TARGET_EXPR
1846 && TARGET_EXPR_DIRECT_INIT_P (init
))
1847 && !DIRECT_LIST_INIT_P (init
))
1848 flags
|= LOOKUP_ONLYCONVERTING
;
1850 is_global
= begin_init_stmts (&stmt_expr
, &compound_stmt
);
1851 destroy_temps
= stmts_are_full_exprs_p ();
1852 current_stmt_tree ()->stmts_are_full_exprs_p
= 0;
1853 bool ok
= expand_aggr_init_1 (TYPE_BINFO (type
), exp
, exp
,
1854 init
, LOOKUP_NORMAL
|flags
, complain
);
1855 stmt_expr
= finish_init_stmts (is_global
, stmt_expr
, compound_stmt
);
1856 current_stmt_tree ()->stmts_are_full_exprs_p
= destroy_temps
;
1857 TREE_READONLY (exp
) = was_const
;
1858 TREE_THIS_VOLATILE (exp
) = was_volatile
;
1860 return error_mark_node
;
1862 if ((VAR_P (exp
) || TREE_CODE (exp
) == PARM_DECL
)
1863 && TREE_SIDE_EFFECTS (stmt_expr
)
1864 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type
)))
1865 /* Just know that we've seen something for this node. */
1866 TREE_USED (exp
) = 1;
1872 expand_default_init (tree binfo
, tree true_exp
, tree exp
, tree init
, int flags
,
1873 tsubst_flags_t complain
)
1875 tree type
= TREE_TYPE (exp
);
1877 /* It fails because there may not be a constructor which takes
1878 its own type as the first (or only parameter), but which does
1879 take other types via a conversion. So, if the thing initializing
1880 the expression is a unit element of type X, first try X(X&),
1881 followed by initialization by X. If neither of these work
1882 out, then look hard. */
1884 vec
<tree
, va_gc
> *parms
;
1886 /* If we have direct-initialization from an initializer list, pull
1887 it out of the TREE_LIST so the code below can see it. */
1888 if (init
&& TREE_CODE (init
) == TREE_LIST
1889 && DIRECT_LIST_INIT_P (TREE_VALUE (init
)))
1891 gcc_checking_assert ((flags
& LOOKUP_ONLYCONVERTING
) == 0
1892 && TREE_CHAIN (init
) == NULL_TREE
);
1893 init
= TREE_VALUE (init
);
1894 /* Only call reshape_init if it has not been called earlier
1896 if (BRACE_ENCLOSED_INITIALIZER_P (init
) && CP_AGGREGATE_TYPE_P (type
))
1897 init
= reshape_init (type
, init
, complain
);
1900 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
)
1901 && CP_AGGREGATE_TYPE_P (type
))
1902 /* A brace-enclosed initializer for an aggregate. In C++0x this can
1903 happen for direct-initialization, too. */
1904 init
= digest_init (type
, init
, complain
);
1906 if (init
== error_mark_node
)
1909 /* A CONSTRUCTOR of the target's type is a previously digested
1910 initializer, whether that happened just above or in
1911 cp_parser_late_parsing_nsdmi.
1913 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
1914 set represents the whole initialization, so we shouldn't build up
1915 another ctor call. */
1917 && (TREE_CODE (init
) == CONSTRUCTOR
1918 || (TREE_CODE (init
) == TARGET_EXPR
1919 && (TARGET_EXPR_DIRECT_INIT_P (init
)
1920 || TARGET_EXPR_LIST_INIT_P (init
))))
1921 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
), type
))
1923 /* Early initialization via a TARGET_EXPR only works for
1924 complete objects. */
1925 gcc_assert (TREE_CODE (init
) == CONSTRUCTOR
|| true_exp
== exp
);
1927 init
= build2 (INIT_EXPR
, TREE_TYPE (exp
), exp
, init
);
1928 TREE_SIDE_EFFECTS (init
) = 1;
1929 finish_expr_stmt (init
);
1933 if (init
&& TREE_CODE (init
) != TREE_LIST
1934 && (flags
& LOOKUP_ONLYCONVERTING
)
1935 && !unsafe_return_slot_p (exp
))
1937 /* Base subobjects should only get direct-initialization. */
1938 gcc_assert (true_exp
== exp
);
1940 if (flags
& DIRECT_BIND
)
1941 /* Do nothing. We hit this in two cases: Reference initialization,
1942 where we aren't initializing a real variable, so we don't want
1943 to run a new constructor; and catching an exception, where we
1944 have already built up the constructor call so we could wrap it
1945 in an exception region. */;
1948 init
= ocp_convert (type
, init
, CONV_IMPLICIT
|CONV_FORCE_TEMP
,
1949 flags
, complain
| tf_no_cleanup
);
1950 if (init
== error_mark_node
)
1954 if (TREE_CODE (init
) == MUST_NOT_THROW_EXPR
)
1955 /* We need to protect the initialization of a catch parm with a
1956 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1957 around the TARGET_EXPR for the copy constructor. See
1958 initialize_handler_parm. */
1960 TREE_OPERAND (init
, 0) = build2 (INIT_EXPR
, TREE_TYPE (exp
), exp
,
1961 TREE_OPERAND (init
, 0));
1962 TREE_TYPE (init
) = void_type_node
;
1965 init
= build2 (INIT_EXPR
, TREE_TYPE (exp
), exp
, init
);
1966 TREE_SIDE_EFFECTS (init
) = 1;
1967 finish_expr_stmt (init
);
1971 if (init
== NULL_TREE
)
1973 else if (TREE_CODE (init
) == TREE_LIST
&& !TREE_TYPE (init
))
1975 parms
= make_tree_vector ();
1976 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1977 vec_safe_push (parms
, TREE_VALUE (init
));
1980 parms
= make_tree_vector_single (init
);
1982 if (exp
== current_class_ref
&& current_function_decl
1983 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl
))
1985 /* Delegating constructor. */
1988 tree elt
; unsigned i
;
1990 /* Unshare the arguments for the second call. */
1991 releasing_vec parms2
;
1992 FOR_EACH_VEC_SAFE_ELT (parms
, i
, elt
)
1994 elt
= break_out_target_exprs (elt
);
1995 vec_safe_push (parms2
, elt
);
1997 complete
= build_special_member_call (exp
, complete_ctor_identifier
,
1998 &parms2
, binfo
, flags
,
2000 complete
= fold_build_cleanup_point_expr (void_type_node
, complete
);
2002 base
= build_special_member_call (exp
, base_ctor_identifier
,
2003 &parms
, binfo
, flags
,
2005 base
= fold_build_cleanup_point_expr (void_type_node
, base
);
2006 if (complete
== error_mark_node
|| base
== error_mark_node
)
2008 rval
= build_if_in_charge (complete
, base
);
2012 tree ctor_name
= (true_exp
== exp
2013 ? complete_ctor_identifier
: base_ctor_identifier
);
2015 rval
= build_special_member_call (exp
, ctor_name
, &parms
, binfo
, flags
,
2017 if (rval
== error_mark_node
)
2022 release_tree_vector (parms
);
2024 if (exp
== true_exp
&& TREE_CODE (rval
) == CALL_EXPR
)
2026 tree fn
= get_callee_fndecl (rval
);
2027 if (fn
&& DECL_DECLARED_CONSTEXPR_P (fn
))
2029 tree e
= maybe_constant_init (rval
, exp
);
2030 if (TREE_CONSTANT (e
))
2031 rval
= build2 (INIT_EXPR
, type
, exp
, e
);
2035 /* FIXME put back convert_to_void? */
2036 if (TREE_SIDE_EFFECTS (rval
))
2037 finish_expr_stmt (rval
);
2042 /* This function is responsible for initializing EXP with INIT
2043 (if any). Returns true on success, false on failure.
2045 BINFO is the binfo of the type for who we are performing the
2046 initialization. For example, if W is a virtual base class of A and B,
2048 If we are initializing B, then W must contain B's W vtable, whereas
2049 were we initializing C, W must contain C's W vtable.
2051 TRUE_EXP is nonzero if it is the true expression being initialized.
2052 In this case, it may be EXP, or may just contain EXP. The reason we
2053 need this is because if EXP is a base element of TRUE_EXP, we
2054 don't necessarily know by looking at EXP where its virtual
2055 baseclass fields should really be pointing. But we do know
2056 from TRUE_EXP. In constructors, we don't know anything about
2057 the value being initialized.
2059 FLAGS is just passed to `build_new_method_call'. See that function
2060 for its description. */
2063 expand_aggr_init_1 (tree binfo
, tree true_exp
, tree exp
, tree init
, int flags
,
2064 tsubst_flags_t complain
)
2066 tree type
= TREE_TYPE (exp
);
2068 gcc_assert (init
!= error_mark_node
&& type
!= error_mark_node
);
2069 gcc_assert (building_stmt_list_p ());
2071 /* Use a function returning the desired type to initialize EXP for us.
2072 If the function is a constructor, and its first argument is
2073 NULL_TREE, know that it was meant for us--just slide exp on
2074 in and expand the constructor. Constructors now come
2077 if (init
&& VAR_P (exp
)
2078 && COMPOUND_LITERAL_P (init
))
2080 vec
<tree
, va_gc
> *cleanups
= NULL
;
2081 /* If store_init_value returns NULL_TREE, the INIT has been
2082 recorded as the DECL_INITIAL for EXP. That means there's
2083 nothing more we have to do. */
2084 init
= store_init_value (exp
, init
, &cleanups
, flags
);
2086 finish_expr_stmt (init
);
2087 gcc_assert (!cleanups
);
2091 /* List-initialization from {} becomes value-initialization for non-aggregate
2092 classes with default constructors. Handle this here when we're
2093 initializing a base, so protected access works. */
2094 if (exp
!= true_exp
&& init
&& TREE_CODE (init
) == TREE_LIST
)
2096 tree elt
= TREE_VALUE (init
);
2097 if (DIRECT_LIST_INIT_P (elt
)
2098 && CONSTRUCTOR_ELTS (elt
) == 0
2099 && CLASSTYPE_NON_AGGREGATE (type
)
2100 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
2101 init
= void_type_node
;
2104 /* If an explicit -- but empty -- initializer list was present,
2105 that's value-initialization. */
2106 if (init
== void_type_node
)
2108 /* If the type has data but no user-provided default ctor, we need to zero
2110 if (type_has_non_user_provided_default_constructor (type
)
2111 && !is_really_empty_class (type
, /*ignore_vptr*/true))
2113 tree field_size
= NULL_TREE
;
2114 if (exp
!= true_exp
&& CLASSTYPE_AS_BASE (type
) != type
)
2115 /* Don't clobber already initialized virtual bases. */
2116 field_size
= TYPE_SIZE (CLASSTYPE_AS_BASE (type
));
2117 init
= build_zero_init_1 (type
, NULL_TREE
, /*static_storage_p=*/false,
2119 init
= build2 (INIT_EXPR
, type
, exp
, init
);
2120 finish_expr_stmt (init
);
2123 /* If we don't need to mess with the constructor at all,
2125 if (! type_build_ctor_call (type
))
2128 /* Otherwise fall through and call the constructor. */
2132 /* We know that expand_default_init can handle everything we want
2134 return expand_default_init (binfo
, true_exp
, exp
, init
, flags
, complain
);
2137 /* Report an error if TYPE is not a user-defined, class type. If
2138 OR_ELSE is nonzero, give an error message. */
2141 is_class_type (tree type
, int or_else
)
2143 if (type
== error_mark_node
)
2146 if (! CLASS_TYPE_P (type
))
2149 error ("%qT is not a class type", type
);
2155 /* Build a reference to a member of an aggregate. This is not a C++
2156 `&', but really something which can have its address taken, and
2157 then act as a pointer to member, for example TYPE :: FIELD can have
2158 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
2159 this expression is the operand of "&".
2161 @@ Prints out lousy diagnostics for operator <typename>
2164 @@ This function should be rewritten and placed in search.c. */
2167 build_offset_ref (tree type
, tree member
, bool address_p
,
2168 tsubst_flags_t complain
)
2171 tree basebinfo
= NULL_TREE
;
2173 /* class templates can come in as TEMPLATE_DECLs here. */
2174 if (TREE_CODE (member
) == TEMPLATE_DECL
)
2177 if (dependent_scope_p (type
) || type_dependent_expression_p (member
))
2178 return build_qualified_name (NULL_TREE
, type
, member
,
2179 /*template_p=*/false);
2181 gcc_assert (TYPE_P (type
));
2182 if (! is_class_type (type
, 1))
2183 return error_mark_node
;
2185 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2186 /* Callers should call mark_used before this point. */
2187 gcc_assert (!DECL_P (member
) || TREE_USED (member
));
2189 type
= TYPE_MAIN_VARIANT (type
);
2190 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type
)))
2192 if (complain
& tf_error
)
2193 error ("incomplete type %qT does not have member %qD", type
, member
);
2194 return error_mark_node
;
2197 /* Entities other than non-static members need no further
2199 if (TREE_CODE (member
) == TYPE_DECL
)
2201 if (VAR_P (member
) || TREE_CODE (member
) == CONST_DECL
)
2202 return convert_from_reference (member
);
2204 if (TREE_CODE (member
) == FIELD_DECL
&& DECL_C_BIT_FIELD (member
))
2206 if (complain
& tf_error
)
2207 error ("invalid pointer to bit-field %qD", member
);
2208 return error_mark_node
;
2211 /* Set up BASEBINFO for member lookup. */
2212 decl
= maybe_dummy_object (type
, &basebinfo
);
2214 /* A lot of this logic is now handled in lookup_member. */
2215 if (BASELINK_P (member
))
2217 /* Go from the TREE_BASELINK to the member function info. */
2218 tree t
= BASELINK_FUNCTIONS (member
);
2220 if (TREE_CODE (t
) != TEMPLATE_ID_EXPR
&& !really_overloaded_fn (t
))
2222 /* Get rid of a potential OVERLOAD around it. */
2225 /* Unique functions are handled easily. */
2227 /* For non-static member of base class, we need a special rule
2228 for access checking [class.protected]:
2230 If the access is to form a pointer to member, the
2231 nested-name-specifier shall name the derived class
2232 (or any class derived from that class). */
2234 if (address_p
&& DECL_P (t
)
2235 && DECL_NONSTATIC_MEMBER_P (t
))
2236 ok
= perform_or_defer_access_check (TYPE_BINFO (type
), t
, t
,
2239 ok
= perform_or_defer_access_check (basebinfo
, t
, t
,
2242 return error_mark_node
;
2243 if (DECL_STATIC_FUNCTION_P (t
))
2248 TREE_TYPE (member
) = unknown_type_node
;
2250 else if (address_p
&& TREE_CODE (member
) == FIELD_DECL
)
2252 /* We need additional test besides the one in
2253 check_accessibility_of_qualified_id in case it is
2254 a pointer to non-static member. */
2255 if (!perform_or_defer_access_check (TYPE_BINFO (type
), member
, member
,
2257 return error_mark_node
;
2262 /* If MEMBER is non-static, then the program has fallen afoul of
2265 An id-expression that denotes a non-static data member or
2266 non-static member function of a class can only be used:
2268 -- as part of a class member access (_expr.ref_) in which the
2269 object-expression refers to the member's class or a class
2270 derived from that class, or
2272 -- to form a pointer to member (_expr.unary.op_), or
2274 -- in the body of a non-static member function of that class or
2275 of a class derived from that class (_class.mfct.non-static_), or
2277 -- in a mem-initializer for a constructor for that class or for
2278 a class derived from that class (_class.base.init_). */
2279 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member
))
2281 /* Build a representation of the qualified name suitable
2282 for use as the operand to "&" -- even though the "&" is
2283 not actually present. */
2284 member
= build2 (OFFSET_REF
, TREE_TYPE (member
), decl
, member
);
2285 /* In Microsoft mode, treat a non-static member function as if
2286 it were a pointer-to-member. */
2287 if (flag_ms_extensions
)
2289 PTRMEM_OK_P (member
) = 1;
2290 return cp_build_addr_expr (member
, complain
);
2292 if (complain
& tf_error
)
2293 error ("invalid use of non-static member function %qD",
2294 TREE_OPERAND (member
, 1));
2295 return error_mark_node
;
2297 else if (TREE_CODE (member
) == FIELD_DECL
)
2299 if (complain
& tf_error
)
2300 error ("invalid use of non-static data member %qD", member
);
2301 return error_mark_node
;
2306 member
= build2 (OFFSET_REF
, TREE_TYPE (member
), decl
, member
);
2307 PTRMEM_OK_P (member
) = 1;
2311 /* If DECL is a scalar enumeration constant or variable with a
2312 constant initializer, return the initializer (or, its initializers,
2313 recursively); otherwise, return DECL. If STRICT_P, the
2314 initializer is only returned if DECL is a
2315 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
2316 return an aggregate constant. If UNSHARE_P, return an unshared
2317 copy of the initializer. */
2320 constant_value_1 (tree decl
, bool strict_p
, bool return_aggregate_cst_ok_p
,
2323 while (TREE_CODE (decl
) == CONST_DECL
2324 || decl_constant_var_p (decl
)
2325 || (!strict_p
&& VAR_P (decl
)
2326 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl
))))
2329 /* If DECL is a static data member in a template
2330 specialization, we must instantiate it here. The
2331 initializer for the static data member is not processed
2332 until needed; we need it now. */
2333 mark_used (decl
, tf_none
);
2334 init
= DECL_INITIAL (decl
);
2335 if (init
== error_mark_node
)
2337 if (TREE_CODE (decl
) == CONST_DECL
2338 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
2339 /* Treat the error as a constant to avoid cascading errors on
2340 excessively recursive template instantiation (c++/9335). */
2345 /* Initializers in templates are generally expanded during
2346 instantiation, so before that for const int i(2)
2347 INIT is a TREE_LIST with the actual initializer as
2349 if (processing_template_decl
2351 && TREE_CODE (init
) == TREE_LIST
2352 && TREE_CHAIN (init
) == NULL_TREE
)
2353 init
= TREE_VALUE (init
);
2354 /* Instantiate a non-dependent initializer for user variables. We
2355 mustn't do this for the temporary for an array compound literal;
2356 trying to instatiate the initializer will keep creating new
2357 temporaries until we crash. Probably it's not useful to do it for
2358 other artificial variables, either. */
2359 if (!DECL_ARTIFICIAL (decl
))
2360 init
= instantiate_non_dependent_or_null (init
);
2362 || !TREE_TYPE (init
)
2363 || !TREE_CONSTANT (init
)
2364 || (!return_aggregate_cst_ok_p
2365 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2366 return an aggregate constant (of which string
2367 literals are a special case), as we do not want
2368 to make inadvertent copies of such entities, and
2369 we must be sure that their addresses are the
2371 && (TREE_CODE (init
) == CONSTRUCTOR
2372 || TREE_CODE (init
) == STRING_CST
)))
2374 /* Don't return a CONSTRUCTOR for a variable with partial run-time
2375 initialization, since it doesn't represent the entire value.
2376 Similarly for VECTOR_CSTs created by cp_folding those
2378 if ((TREE_CODE (init
) == CONSTRUCTOR
2379 || TREE_CODE (init
) == VECTOR_CST
)
2380 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
2382 /* If the variable has a dynamic initializer, don't use its
2383 DECL_INITIAL which doesn't reflect the real value. */
2385 && TREE_STATIC (decl
)
2386 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
)
2387 && DECL_NONTRIVIALLY_INITIALIZED_P (decl
))
2391 return unshare_p
? unshare_expr (decl
) : decl
;
2394 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant
2395 of integral or enumeration type, or a constexpr variable of scalar type,
2396 then return that value. These are those variables permitted in constant
2397 expressions by [5.19/1]. */
2400 scalar_constant_value (tree decl
)
2402 return constant_value_1 (decl
, /*strict_p=*/true,
2403 /*return_aggregate_cst_ok_p=*/false,
2404 /*unshare_p=*/true);
2407 /* Like scalar_constant_value, but can also return aggregate initializers.
2408 If UNSHARE_P, return an unshared copy of the initializer. */
2411 decl_really_constant_value (tree decl
, bool unshare_p
/*= true*/)
2413 return constant_value_1 (decl
, /*strict_p=*/true,
2414 /*return_aggregate_cst_ok_p=*/true,
2415 /*unshare_p=*/unshare_p
);
2418 /* A more relaxed version of decl_really_constant_value, used by the
2419 common C/C++ code. */
2422 decl_constant_value (tree decl
, bool unshare_p
)
2424 return constant_value_1 (decl
, /*strict_p=*/processing_template_decl
,
2425 /*return_aggregate_cst_ok_p=*/true,
2426 /*unshare_p=*/unshare_p
);
2430 decl_constant_value (tree decl
)
2432 return decl_constant_value (decl
, /*unshare_p=*/true);
2435 /* Common subroutines of build_new and build_vec_delete. */
2437 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2438 the type of the object being allocated; otherwise, it's just TYPE.
2439 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2440 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2441 a vector of arguments to be provided as arguments to a placement
2442 new operator. This routine performs no semantic checks; it just
2443 creates and returns a NEW_EXPR. */
2446 build_raw_new_expr (location_t loc
, vec
<tree
, va_gc
> *placement
, tree type
,
2447 tree nelts
, vec
<tree
, va_gc
> *init
, int use_global_new
)
2452 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2453 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2454 permits us to distinguish the case of a missing initializer "new
2455 int" from an empty initializer "new int()". */
2457 init_list
= NULL_TREE
;
2458 else if (init
->is_empty ())
2459 init_list
= void_node
;
2461 init_list
= build_tree_list_vec (init
);
2463 new_expr
= build4_loc (loc
, NEW_EXPR
, build_pointer_type (type
),
2464 build_tree_list_vec (placement
), type
, nelts
,
2466 NEW_EXPR_USE_GLOBAL (new_expr
) = use_global_new
;
2467 TREE_SIDE_EFFECTS (new_expr
) = 1;
2472 /* Diagnose uninitialized const members or reference members of type
2473 TYPE. USING_NEW is used to disambiguate the diagnostic between a
2474 new expression without a new-initializer and a declaration. Returns
2478 diagnose_uninitialized_cst_or_ref_member_1 (tree type
, tree origin
,
2479 bool using_new
, bool complain
)
2482 int error_count
= 0;
2484 if (type_has_user_provided_constructor (type
))
2487 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2491 if (TREE_CODE (field
) != FIELD_DECL
)
2494 field_type
= strip_array_types (TREE_TYPE (field
));
2496 if (type_has_user_provided_constructor (field_type
))
2499 if (TYPE_REF_P (field_type
))
2504 if (DECL_CONTEXT (field
) == origin
)
2507 error ("uninitialized reference member in %q#T "
2508 "using %<new%> without new-initializer", origin
);
2510 error ("uninitialized reference member in %q#T", origin
);
2515 error ("uninitialized reference member in base %q#T "
2516 "of %q#T using %<new%> without new-initializer",
2517 DECL_CONTEXT (field
), origin
);
2519 error ("uninitialized reference member in base %q#T "
2520 "of %q#T", DECL_CONTEXT (field
), origin
);
2522 inform (DECL_SOURCE_LOCATION (field
),
2523 "%q#D should be initialized", field
);
2527 if (CP_TYPE_CONST_P (field_type
))
2532 if (DECL_CONTEXT (field
) == origin
)
2535 error ("uninitialized const member in %q#T "
2536 "using %<new%> without new-initializer", origin
);
2538 error ("uninitialized const member in %q#T", origin
);
2543 error ("uninitialized const member in base %q#T "
2544 "of %q#T using %<new%> without new-initializer",
2545 DECL_CONTEXT (field
), origin
);
2547 error ("uninitialized const member in base %q#T "
2548 "of %q#T", DECL_CONTEXT (field
), origin
);
2550 inform (DECL_SOURCE_LOCATION (field
),
2551 "%q#D should be initialized", field
);
2555 if (CLASS_TYPE_P (field_type
))
2557 += diagnose_uninitialized_cst_or_ref_member_1 (field_type
, origin
,
2558 using_new
, complain
);
2564 diagnose_uninitialized_cst_or_ref_member (tree type
, bool using_new
, bool complain
)
2566 return diagnose_uninitialized_cst_or_ref_member_1 (type
, type
, using_new
, complain
);
2569 /* Call __cxa_bad_array_new_length to indicate that the size calculation
2570 overflowed. Pretend it returns sizetype so that it plays nicely in the
2574 throw_bad_array_new_length (void)
2578 tree name
= get_identifier ("__cxa_throw_bad_array_new_length");
2580 fn
= get_global_binding (name
);
2582 fn
= push_throw_library_fn
2583 (name
, build_function_type_list (sizetype
, NULL_TREE
));
2586 return build_cxx_call (fn
, 0, NULL
, tf_warning_or_error
);
2589 /* Attempt to verify that the argument, OPER, of a placement new expression
2590 refers to an object sufficiently large for an object of TYPE or an array
2591 of NELTS of such objects when NELTS is non-null, and issue a warning when
2592 it does not. SIZE specifies the size needed to construct the object or
2593 array and captures the result of NELTS * sizeof (TYPE). (SIZE could be
2594 greater when the array under construction requires a cookie to store
2595 NELTS. GCC's placement new expression stores the cookie when invoking
2596 a user-defined placement new operator function but not the default one.
2597 Placement new expressions with user-defined placement new operator are
2598 not diagnosed since we don't know how they use the buffer (this could
2599 be a future extension). */
2601 warn_placement_new_too_small (tree type
, tree nelts
, tree size
, tree oper
)
2603 location_t loc
= cp_expr_loc_or_input_loc (oper
);
2607 /* Using a function argument or a (non-array) variable as an argument
2608 to placement new is not checked since it's unknown what it might
2610 if (TREE_CODE (oper
) == PARM_DECL
2612 || TREE_CODE (oper
) == COMPONENT_REF
)
2615 /* Evaluate any constant expressions. */
2616 size
= fold_non_dependent_expr (size
);
2619 ref
.eval
= [](tree x
){ return fold_non_dependent_expr (x
); };
2620 ref
.trail1special
= warn_placement_new
< 2;
2621 tree objsize
= compute_objsize (oper
, 1, &ref
);
2625 offset_int bytes_avail
= wi::to_offset (objsize
);
2626 offset_int bytes_need
;
2628 if (CONSTANT_CLASS_P (size
))
2629 bytes_need
= wi::to_offset (size
);
2630 else if (nelts
&& CONSTANT_CLASS_P (nelts
))
2631 bytes_need
= (wi::to_offset (nelts
)
2632 * wi::to_offset (TYPE_SIZE_UNIT (type
)));
2633 else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
)))
2634 bytes_need
= wi::to_offset (TYPE_SIZE_UNIT (type
));
2637 /* The type is a VLA. */
2641 if (bytes_avail
>= bytes_need
)
2644 /* True when the size to mention in the warning is exact as opposed
2646 const bool exact_size
= (ref
.offrng
[0] == ref
.offrng
[1]
2647 || ref
.sizrng
[1] - ref
.offrng
[0] == 0);
2649 tree opertype
= ref
.ref
? TREE_TYPE (ref
.ref
) : TREE_TYPE (oper
);
2650 bool warned
= false;
2652 nelts
= fold_for_warn (nelts
);
2654 if (CONSTANT_CLASS_P (nelts
))
2655 warned
= warning_at (loc
, OPT_Wplacement_new_
,
2657 ? G_("placement new constructing an object "
2658 "of type %<%T [%wu]%> and size %qwu "
2659 "in a region of type %qT and size %qwi")
2660 : G_("placement new constructing an object "
2661 "of type %<%T [%wu]%> and size %qwu "
2662 "in a region of type %qT and size "
2664 type
, tree_to_uhwi (nelts
),
2665 bytes_need
.to_uhwi (),
2666 opertype
, bytes_avail
.to_uhwi ());
2668 warned
= warning_at (loc
, OPT_Wplacement_new_
,
2670 ? G_("placement new constructing an array "
2671 "of objects of type %qT and size %qwu "
2672 "in a region of type %qT and size %qwi")
2673 : G_("placement new constructing an array "
2674 "of objects of type %qT and size %qwu "
2675 "in a region of type %qT and size "
2677 type
, bytes_need
.to_uhwi (), opertype
,
2678 bytes_avail
.to_uhwi ());
2680 warned
= warning_at (loc
, OPT_Wplacement_new_
,
2682 ? G_("placement new constructing an object "
2683 "of type %qT and size %qwu in a region "
2684 "of type %qT and size %qwi")
2685 : G_("placement new constructing an object "
2687 "and size %qwu in a region of type %qT "
2688 "and size at most %qwu")),
2689 type
, bytes_need
.to_uhwi (), opertype
,
2690 bytes_avail
.to_uhwi ());
2692 if (!warned
|| !ref
.ref
)
2695 if (ref
.offrng
[0] == 0 || !ref
.offset_bounded ())
2696 /* Avoid mentioning the offset when its lower bound is zero
2697 or when it's impossibly large. */
2698 inform (DECL_SOURCE_LOCATION (ref
.ref
),
2699 "%qD declared here", ref
.ref
);
2700 else if (ref
.offrng
[0] == ref
.offrng
[1])
2701 inform (DECL_SOURCE_LOCATION (ref
.ref
),
2702 "at offset %wi from %qD declared here",
2703 ref
.offrng
[0].to_shwi (), ref
.ref
);
2705 inform (DECL_SOURCE_LOCATION (ref
.ref
),
2706 "at offset [%wi, %wi] from %qD declared here",
2707 ref
.offrng
[0].to_shwi (), ref
.offrng
[1].to_shwi (), ref
.ref
);
2710 /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */
2713 type_has_new_extended_alignment (tree t
)
2715 return (aligned_new_threshold
2716 && TYPE_ALIGN_UNIT (t
) > (unsigned)aligned_new_threshold
);
2719 /* Return the alignment we expect malloc to guarantee. This should just be
2720 MALLOC_ABI_ALIGNMENT, but that macro defaults to only BITS_PER_WORD for some
2721 reason, so don't let the threshold be smaller than max_align_t_align. */
2726 return MAX (max_align_t_align(), MALLOC_ABI_ALIGNMENT
);
2729 /* Determine whether an allocation function is a namespace-scope
2730 non-replaceable placement new function. See DR 1748. */
2732 std_placement_new_fn_p (tree alloc_fn
)
2734 if (DECL_NAMESPACE_SCOPE_P (alloc_fn
))
2736 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn
)));
2737 if ((TREE_VALUE (first_arg
) == ptr_type_node
)
2738 && TREE_CHAIN (first_arg
) == void_list_node
)
2744 /* For element type ELT_TYPE, return the appropriate type of the heap object
2745 containing such element(s). COOKIE_SIZE is NULL or the size of cookie
2746 in bytes. FULL_SIZE is NULL if it is unknown how big the heap allocation
2747 will be, otherwise size of the heap object. If COOKIE_SIZE is NULL,
2748 return array type ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
2749 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
2750 where N is nothing (flexible array member) if FULL_SIZE is NULL, otherwise
2751 it is computed such that the size of the struct fits into FULL_SIZE. */
2754 build_new_constexpr_heap_type (tree elt_type
, tree cookie_size
, tree full_size
)
2756 gcc_assert (cookie_size
== NULL_TREE
|| tree_fits_uhwi_p (cookie_size
));
2757 gcc_assert (full_size
== NULL_TREE
|| tree_fits_uhwi_p (full_size
));
2758 unsigned HOST_WIDE_INT csz
= cookie_size
? tree_to_uhwi (cookie_size
) : 0;
2759 tree itype2
= NULL_TREE
;
2762 unsigned HOST_WIDE_INT fsz
= tree_to_uhwi (full_size
);
2763 gcc_assert (fsz
>= csz
);
2765 fsz
/= int_size_in_bytes (elt_type
);
2766 itype2
= build_index_type (size_int (fsz
- 1));
2768 return build_cplus_array_type (elt_type
, itype2
);
2771 gcc_assert (cookie_size
);
2772 csz
/= int_size_in_bytes (sizetype
);
2773 tree itype1
= build_index_type (size_int (csz
- 1));
2774 tree atype1
= build_cplus_array_type (sizetype
, itype1
);
2775 tree atype2
= build_cplus_array_type (elt_type
, itype2
);
2776 tree rtype
= cxx_make_type (RECORD_TYPE
);
2777 TYPE_NAME (rtype
) = heap_identifier
;
2778 tree fld1
= build_decl (UNKNOWN_LOCATION
, FIELD_DECL
, NULL_TREE
, atype1
);
2779 tree fld2
= build_decl (UNKNOWN_LOCATION
, FIELD_DECL
, NULL_TREE
, atype2
);
2780 DECL_FIELD_CONTEXT (fld1
) = rtype
;
2781 DECL_FIELD_CONTEXT (fld2
) = rtype
;
2782 DECL_ARTIFICIAL (fld1
) = true;
2783 DECL_ARTIFICIAL (fld2
) = true;
2784 TYPE_FIELDS (rtype
) = fld1
;
2785 DECL_CHAIN (fld1
) = fld2
;
2786 layout_type (rtype
);
2790 /* Help the constexpr code to find the right type for the heap variable
2791 by adding a NOP_EXPR around ALLOC_CALL if needed for cookie_size.
2792 Return ALLOC_CALL or ALLOC_CALL cast to a pointer to
2793 struct { size_t[cookie_size/sizeof(size_t)]; elt_type[]; }. */
2796 maybe_wrap_new_for_constexpr (tree alloc_call
, tree elt_type
, tree cookie_size
)
2798 if (cxx_dialect
< cxx20
)
2801 if (current_function_decl
!= NULL_TREE
2802 && !DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
2805 tree call_expr
= extract_call_expr (alloc_call
);
2806 if (call_expr
== error_mark_node
)
2809 tree alloc_call_fndecl
= cp_get_callee_fndecl_nofold (call_expr
);
2810 if (alloc_call_fndecl
== NULL_TREE
2811 || !IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_call_fndecl
))
2812 || CP_DECL_CONTEXT (alloc_call_fndecl
) != global_namespace
)
2815 tree rtype
= build_new_constexpr_heap_type (elt_type
, cookie_size
,
2817 return build_nop (build_pointer_type (rtype
), alloc_call
);
2820 /* Generate code for a new-expression, including calling the "operator
2821 new" function, initializing the object, and, if an exception occurs
2822 during construction, cleaning up. The arguments are as for
2823 build_raw_new_expr. This may change PLACEMENT and INIT.
2824 TYPE is the type of the object being constructed, possibly an array
2825 of NELTS elements when NELTS is non-null (in "new T[NELTS]", T may
2826 be an array of the form U[inner], with the whole expression being
2827 "new U[NELTS][inner]"). */
2830 build_new_1 (vec
<tree
, va_gc
> **placement
, tree type
, tree nelts
,
2831 vec
<tree
, va_gc
> **init
, bool globally_qualified_p
,
2832 tsubst_flags_t complain
)
2835 /* True iff this is a call to "operator new[]" instead of just
2837 bool array_p
= false;
2838 /* If ARRAY_P is true, the element type of the array. This is never
2839 an ARRAY_TYPE; for something like "new int[3][4]", the
2840 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
2843 /* The type of the new-expression. (This type is always a pointer
2846 tree non_const_pointer_type
;
2847 /* The most significant array bound in int[OUTER_NELTS][inner]. */
2848 tree outer_nelts
= NULL_TREE
;
2849 /* For arrays with a non-constant number of elements, a bounds checks
2850 on the NELTS parameter to avoid integer overflow at runtime. */
2851 tree outer_nelts_check
= NULL_TREE
;
2852 bool outer_nelts_from_type
= false;
2853 /* Number of the "inner" elements in "new T[OUTER_NELTS][inner]". */
2854 offset_int inner_nelts_count
= 1;
2855 tree alloc_call
, alloc_expr
;
2856 /* Size of the inner array elements (those with constant dimensions). */
2857 offset_int inner_size
;
2858 /* The address returned by the call to "operator new". This node is
2859 a VAR_DECL and is therefore reusable. */
2862 tree cookie_expr
, init_expr
;
2863 int nothrow
, check_new
;
2864 /* If non-NULL, the number of extra bytes to allocate at the
2865 beginning of the storage allocated for an array-new expression in
2866 order to store the number of elements. */
2867 tree cookie_size
= NULL_TREE
;
2868 tree placement_first
;
2869 tree placement_expr
= NULL_TREE
;
2870 /* True if the function we are calling is a placement allocation
2872 bool placement_allocation_fn_p
;
2873 /* True if the storage must be initialized, either by a constructor
2874 or due to an explicit new-initializer. */
2875 bool is_initialized
;
2876 /* The address of the thing allocated, not including any cookie. In
2877 particular, if an array cookie is in use, DATA_ADDR is the
2878 address of the first array element. This node is a VAR_DECL, and
2879 is therefore reusable. */
2881 tree init_preeval_expr
= NULL_TREE
;
2882 tree orig_type
= type
;
2886 outer_nelts
= nelts
;
2889 else if (TREE_CODE (type
) == ARRAY_TYPE
)
2891 /* Transforms new (T[N]) to new T[N]. The former is a GNU
2892 extension for variable N. (This also covers new T where T is
2895 nelts
= array_type_nelts_top (type
);
2896 outer_nelts
= nelts
;
2897 type
= TREE_TYPE (type
);
2898 outer_nelts_from_type
= true;
2901 /* Lots of logic below depends on whether we have a constant number of
2902 elements, so go ahead and fold it now. */
2903 const_tree cst_outer_nelts
= fold_non_dependent_expr (outer_nelts
, complain
);
2905 /* If our base type is an array, then make sure we know how many elements
2907 for (elt_type
= type
;
2908 TREE_CODE (elt_type
) == ARRAY_TYPE
;
2909 elt_type
= TREE_TYPE (elt_type
))
2911 tree inner_nelts
= array_type_nelts_top (elt_type
);
2912 tree inner_nelts_cst
= maybe_constant_value (inner_nelts
);
2913 if (TREE_CODE (inner_nelts_cst
) == INTEGER_CST
)
2915 wi::overflow_type overflow
;
2916 offset_int result
= wi::mul (wi::to_offset (inner_nelts_cst
),
2917 inner_nelts_count
, SIGNED
, &overflow
);
2920 if (complain
& tf_error
)
2921 error ("integer overflow in array size");
2922 nelts
= error_mark_node
;
2924 inner_nelts_count
= result
;
2928 if (complain
& tf_error
)
2930 error_at (cp_expr_loc_or_input_loc (inner_nelts
),
2931 "array size in new-expression must be constant");
2932 cxx_constant_value(inner_nelts
);
2934 nelts
= error_mark_node
;
2936 if (nelts
!= error_mark_node
)
2937 nelts
= cp_build_binary_op (input_location
,
2943 if (!verify_type_context (input_location
, TCTX_ALLOCATION
, elt_type
,
2944 !(complain
& tf_error
)))
2945 return error_mark_node
;
2947 if (variably_modified_type_p (elt_type
, NULL_TREE
) && (complain
& tf_error
))
2949 error ("variably modified type not allowed in new-expression");
2950 return error_mark_node
;
2953 if (nelts
== error_mark_node
)
2954 return error_mark_node
;
2956 /* Warn if we performed the (T[N]) to T[N] transformation and N is
2958 if (outer_nelts_from_type
2959 && !TREE_CONSTANT (cst_outer_nelts
))
2961 if (complain
& tf_warning_or_error
)
2963 pedwarn (cp_expr_loc_or_input_loc (outer_nelts
), OPT_Wvla
,
2964 typedef_variant_p (orig_type
)
2965 ? G_("non-constant array new length must be specified "
2966 "directly, not by %<typedef%>")
2967 : G_("non-constant array new length must be specified "
2968 "without parentheses around the type-id"));
2971 return error_mark_node
;
2974 if (VOID_TYPE_P (elt_type
))
2976 if (complain
& tf_error
)
2977 error ("invalid type %<void%> for %<new%>");
2978 return error_mark_node
;
2981 if (is_std_init_list (elt_type
) && !cp_unevaluated_operand
)
2982 warning (OPT_Winit_list_lifetime
,
2983 "%<new%> of %<initializer_list%> does not "
2984 "extend the lifetime of the underlying array");
2986 if (abstract_virtuals_error_sfinae (ACU_NEW
, elt_type
, complain
))
2987 return error_mark_node
;
2989 is_initialized
= (type_build_ctor_call (elt_type
) || *init
!= NULL
);
2991 if (*init
== NULL
&& cxx_dialect
< cxx11
)
2993 bool maybe_uninitialized_error
= false;
2994 /* A program that calls for default-initialization [...] of an
2995 entity of reference type is ill-formed. */
2996 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type
))
2997 maybe_uninitialized_error
= true;
2999 /* A new-expression that creates an object of type T initializes
3000 that object as follows:
3001 - If the new-initializer is omitted:
3002 -- If T is a (possibly cv-qualified) non-POD class type
3003 (or array thereof), the object is default-initialized (8.5).
3005 -- Otherwise, the object created has indeterminate
3006 value. If T is a const-qualified type, or a (possibly
3007 cv-qualified) POD class type (or array thereof)
3008 containing (directly or indirectly) a member of
3009 const-qualified type, the program is ill-formed; */
3011 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type
))
3012 maybe_uninitialized_error
= true;
3014 if (maybe_uninitialized_error
3015 && diagnose_uninitialized_cst_or_ref_member (elt_type
,
3017 complain
& tf_error
))
3018 return error_mark_node
;
3021 if (CP_TYPE_CONST_P (elt_type
) && *init
== NULL
3022 && default_init_uninitialized_part (elt_type
))
3024 if (complain
& tf_error
)
3025 error ("uninitialized const in %<new%> of %q#T", elt_type
);
3026 return error_mark_node
;
3029 size
= size_in_bytes (elt_type
);
3032 /* Maximum available size in bytes. Half of the address space
3033 minus the cookie size. */
3035 = wi::set_bit_in_zero
<offset_int
> (TYPE_PRECISION (sizetype
) - 1);
3036 /* Maximum number of outer elements which can be allocated. */
3037 offset_int max_outer_nelts
;
3038 tree max_outer_nelts_tree
;
3040 gcc_assert (TREE_CODE (size
) == INTEGER_CST
);
3041 cookie_size
= targetm
.cxx
.get_cookie_size (elt_type
);
3042 gcc_assert (TREE_CODE (cookie_size
) == INTEGER_CST
);
3043 gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size
), max_size
));
3044 /* Unconditionally subtract the cookie size. This decreases the
3045 maximum object size and is safe even if we choose not to use
3046 a cookie after all. */
3047 max_size
-= wi::to_offset (cookie_size
);
3048 wi::overflow_type overflow
;
3049 inner_size
= wi::mul (wi::to_offset (size
), inner_nelts_count
, SIGNED
,
3051 if (overflow
|| wi::gtu_p (inner_size
, max_size
))
3053 if (complain
& tf_error
)
3055 cst_size_error error
;
3057 error
= cst_size_overflow
;
3060 error
= cst_size_too_big
;
3061 size
= size_binop (MULT_EXPR
, size
,
3062 wide_int_to_tree (sizetype
,
3063 inner_nelts_count
));
3064 size
= cp_fully_fold (size
);
3066 invalid_array_size_error (input_location
, error
, size
,
3067 /*name=*/NULL_TREE
);
3069 return error_mark_node
;
3072 max_outer_nelts
= wi::udiv_trunc (max_size
, inner_size
);
3073 max_outer_nelts_tree
= wide_int_to_tree (sizetype
, max_outer_nelts
);
3075 size
= size_binop (MULT_EXPR
, size
, fold_convert (sizetype
, nelts
));
3077 if (TREE_CODE (cst_outer_nelts
) == INTEGER_CST
)
3079 if (tree_int_cst_lt (max_outer_nelts_tree
, cst_outer_nelts
))
3081 /* When the array size is constant, check it at compile time
3082 to make sure it doesn't exceed the implementation-defined
3083 maximum, as required by C++ 14 (in C++ 11 this requirement
3084 isn't explicitly stated but it's enforced anyway -- see
3085 grokdeclarator in cp/decl.c). */
3086 if (complain
& tf_error
)
3088 size
= cp_fully_fold (size
);
3089 invalid_array_size_error (input_location
, cst_size_too_big
,
3092 return error_mark_node
;
3097 /* When a runtime check is necessary because the array size
3098 isn't constant, keep only the top-most seven bits (starting
3099 with the most significant non-zero bit) of the maximum size
3100 to compare the array size against, to simplify encoding the
3101 constant maximum size in the instruction stream. */
3103 unsigned shift
= (max_outer_nelts
.get_precision ()) - 7
3104 - wi::clz (max_outer_nelts
);
3105 max_outer_nelts
= (max_outer_nelts
>> shift
) << shift
;
3107 outer_nelts_check
= fold_build2 (LE_EXPR
, boolean_type_node
,
3109 max_outer_nelts_tree
);
3113 tree align_arg
= NULL_TREE
;
3114 if (type_has_new_extended_alignment (elt_type
))
3115 align_arg
= build_int_cst (align_type_node
, TYPE_ALIGN_UNIT (elt_type
));
3117 alloc_fn
= NULL_TREE
;
3119 /* If PLACEMENT is a single simple pointer type not passed by
3120 reference, prepare to capture it in a temporary variable. Do
3121 this now, since PLACEMENT will change in the calls below. */
3122 placement_first
= NULL_TREE
;
3123 if (vec_safe_length (*placement
) == 1
3124 && (TYPE_PTR_P (TREE_TYPE ((**placement
)[0]))))
3125 placement_first
= (**placement
)[0];
3127 bool member_new_p
= false;
3129 /* Allocate the object. */
3133 fnname
= ovl_op_identifier (false, array_p
? VEC_NEW_EXPR
: NEW_EXPR
);
3135 member_new_p
= !globally_qualified_p
3136 && CLASS_TYPE_P (elt_type
)
3138 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type
)
3139 : TYPE_HAS_NEW_OPERATOR (elt_type
));
3143 /* Use a class-specific operator new. */
3144 /* If a cookie is required, add some extra space. */
3145 if (array_p
&& TYPE_VEC_NEW_USES_COOKIE (elt_type
))
3146 size
= size_binop (PLUS_EXPR
, size
, cookie_size
);
3149 cookie_size
= NULL_TREE
;
3150 /* No size arithmetic necessary, so the size check is
3152 if (outer_nelts_check
!= NULL
&& inner_size
== 1)
3153 outer_nelts_check
= NULL_TREE
;
3155 /* Perform the overflow check. */
3156 tree errval
= TYPE_MAX_VALUE (sizetype
);
3157 if (cxx_dialect
>= cxx11
&& flag_exceptions
)
3158 errval
= throw_bad_array_new_length ();
3159 if (outer_nelts_check
!= NULL_TREE
)
3160 size
= fold_build3 (COND_EXPR
, sizetype
, outer_nelts_check
,
3162 /* Create the argument list. */
3163 vec_safe_insert (*placement
, 0, size
);
3164 /* Do name-lookup to find the appropriate operator. */
3165 fns
= lookup_fnfields (elt_type
, fnname
, /*protect=*/2, complain
);
3166 if (fns
== NULL_TREE
)
3168 if (complain
& tf_error
)
3169 error ("no suitable %qD found in class %qT", fnname
, elt_type
);
3170 return error_mark_node
;
3172 if (TREE_CODE (fns
) == TREE_LIST
)
3174 if (complain
& tf_error
)
3176 error ("request for member %qD is ambiguous", fnname
);
3177 print_candidates (fns
);
3179 return error_mark_node
;
3181 tree dummy
= build_dummy_object (elt_type
);
3182 alloc_call
= NULL_TREE
;
3185 vec
<tree
, va_gc
> *align_args
3186 = vec_copy_and_insert (*placement
, align_arg
, 1);
3188 = build_new_method_call (dummy
, fns
, &align_args
,
3189 /*conversion_path=*/NULL_TREE
,
3190 LOOKUP_NORMAL
, &alloc_fn
, tf_none
);
3191 /* If no matching function is found and the allocated object type
3192 has new-extended alignment, the alignment argument is removed
3193 from the argument list, and overload resolution is performed
3195 if (alloc_call
== error_mark_node
)
3196 alloc_call
= NULL_TREE
;
3199 alloc_call
= build_new_method_call (dummy
, fns
, placement
,
3200 /*conversion_path=*/NULL_TREE
,
3202 &alloc_fn
, complain
);
3206 /* Use a global operator new. */
3207 /* See if a cookie might be required. */
3208 if (!(array_p
&& TYPE_VEC_NEW_USES_COOKIE (elt_type
)))
3210 cookie_size
= NULL_TREE
;
3211 /* No size arithmetic necessary, so the size check is
3213 if (outer_nelts_check
!= NULL
&& inner_size
== 1)
3214 outer_nelts_check
= NULL_TREE
;
3217 alloc_call
= build_operator_new_call (fnname
, placement
,
3218 &size
, &cookie_size
,
3219 align_arg
, outer_nelts_check
,
3220 &alloc_fn
, complain
);
3223 if (alloc_call
== error_mark_node
)
3224 return error_mark_node
;
3226 gcc_assert (alloc_fn
!= NULL_TREE
);
3228 /* Now, check to see if this function is actually a placement
3229 allocation function. This can happen even when PLACEMENT is NULL
3230 because we might have something like:
3232 struct S { void* operator new (size_t, int i = 0); };
3234 A call to `new S' will get this allocation function, even though
3235 there is no explicit placement argument. If there is more than
3236 one argument, or there are variable arguments, then this is a
3237 placement allocation function. */
3238 placement_allocation_fn_p
3239 = (type_num_arguments (TREE_TYPE (alloc_fn
)) > 1
3240 || varargs_function_p (alloc_fn
));
3242 if (warn_aligned_new
3243 && !placement_allocation_fn_p
3244 && TYPE_ALIGN (elt_type
) > malloc_alignment ()
3245 && (warn_aligned_new
> 1
3246 || CP_DECL_CONTEXT (alloc_fn
) == global_namespace
)
3247 && !aligned_allocation_fn_p (alloc_fn
))
3249 auto_diagnostic_group d
;
3250 if (warning (OPT_Waligned_new_
, "%<new%> of type %qT with extended "
3251 "alignment %d", elt_type
, TYPE_ALIGN_UNIT (elt_type
)))
3253 inform (input_location
, "uses %qD, which does not have an alignment "
3254 "parameter", alloc_fn
);
3255 if (!aligned_new_threshold
)
3256 inform (input_location
, "use %<-faligned-new%> to enable C++17 "
3257 "over-aligned new support");
3261 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
3262 into a temporary variable. */
3263 if (!processing_template_decl
3264 && TREE_CODE (alloc_call
) == CALL_EXPR
3265 && call_expr_nargs (alloc_call
) == 2
3266 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call
, 0))) == INTEGER_TYPE
3267 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call
, 1))))
3269 tree placement
= CALL_EXPR_ARG (alloc_call
, 1);
3271 if (placement_first
!= NULL_TREE
3272 && (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement
)))
3273 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement
)))))
3275 placement_expr
= get_target_expr (placement_first
);
3276 CALL_EXPR_ARG (alloc_call
, 1)
3277 = fold_convert (TREE_TYPE (placement
), placement_expr
);
3281 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (alloc_call
, 1)))))
3283 /* Attempt to make the warning point at the operator new argument. */
3284 if (placement_first
)
3285 placement
= placement_first
;
3287 warn_placement_new_too_small (orig_type
, nelts
, size
, placement
);
3292 alloc_call
= maybe_wrap_new_for_constexpr (alloc_call
, elt_type
,
3295 /* In the simple case, we can stop now. */
3296 pointer_type
= build_pointer_type (type
);
3297 if (!cookie_size
&& !is_initialized
)
3298 return build_nop (pointer_type
, alloc_call
);
3300 /* Store the result of the allocation call in a variable so that we can
3301 use it more than once. */
3302 alloc_expr
= get_target_expr (alloc_call
);
3303 alloc_node
= TARGET_EXPR_SLOT (alloc_expr
);
3305 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
3306 while (TREE_CODE (alloc_call
) == COMPOUND_EXPR
)
3307 alloc_call
= TREE_OPERAND (alloc_call
, 1);
3309 /* Preevaluate the placement args so that we don't reevaluate them for a
3310 placement delete. */
3311 if (placement_allocation_fn_p
)
3314 stabilize_call (alloc_call
, &inits
);
3316 alloc_expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (alloc_expr
), inits
,
3320 /* unless an allocation function is declared with an empty excep-
3321 tion-specification (_except.spec_), throw(), it indicates failure to
3322 allocate storage by throwing a bad_alloc exception (clause _except_,
3323 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
3324 cation function is declared with an empty exception-specification,
3325 throw(), it returns null to indicate failure to allocate storage and a
3326 non-null pointer otherwise.
3328 So check for a null exception spec on the op new we just called. */
3330 nothrow
= TYPE_NOTHROW_P (TREE_TYPE (alloc_fn
));
3332 = flag_check_new
|| (nothrow
&& !std_placement_new_fn_p (alloc_fn
));
3340 /* Adjust so we're pointing to the start of the object. */
3341 data_addr
= fold_build_pointer_plus (alloc_node
, cookie_size
);
3343 /* Store the number of bytes allocated so that we can know how
3344 many elements to destroy later. We use the last sizeof
3345 (size_t) bytes to store the number of elements. */
3346 cookie_ptr
= size_binop (MINUS_EXPR
, cookie_size
, size_in_bytes (sizetype
));
3347 cookie_ptr
= fold_build_pointer_plus_loc (input_location
,
3348 alloc_node
, cookie_ptr
);
3349 size_ptr_type
= build_pointer_type (sizetype
);
3350 cookie_ptr
= fold_convert (size_ptr_type
, cookie_ptr
);
3351 cookie
= cp_build_fold_indirect_ref (cookie_ptr
);
3353 cookie_expr
= build2 (MODIFY_EXPR
, sizetype
, cookie
, nelts
);
3355 if (targetm
.cxx
.cookie_has_size ())
3357 /* Also store the element size. */
3358 cookie_ptr
= fold_build_pointer_plus (cookie_ptr
,
3359 fold_build1_loc (input_location
,
3360 NEGATE_EXPR
, sizetype
,
3361 size_in_bytes (sizetype
)));
3363 cookie
= cp_build_fold_indirect_ref (cookie_ptr
);
3364 cookie
= build2 (MODIFY_EXPR
, sizetype
, cookie
,
3365 size_in_bytes (elt_type
));
3366 cookie_expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cookie_expr
),
3367 cookie
, cookie_expr
);
3372 cookie_expr
= NULL_TREE
;
3373 data_addr
= alloc_node
;
3376 /* Now use a pointer to the type we've actually allocated. */
3378 /* But we want to operate on a non-const version to start with,
3379 since we'll be modifying the elements. */
3380 non_const_pointer_type
= build_pointer_type
3381 (cp_build_qualified_type (type
, cp_type_quals (type
) & ~TYPE_QUAL_CONST
));
3383 data_addr
= fold_convert (non_const_pointer_type
, data_addr
);
3384 /* Any further uses of alloc_node will want this type, too. */
3385 alloc_node
= fold_convert (non_const_pointer_type
, alloc_node
);
3387 /* Now initialize the allocated object. Note that we preevaluate the
3388 initialization expression, apart from the actual constructor call or
3389 assignment--we do this because we want to delay the allocation as long
3390 as possible in order to minimize the size of the exception region for
3391 placement delete. */
3395 bool explicit_value_init_p
= false;
3397 if (*init
!= NULL
&& (*init
)->is_empty ())
3400 explicit_value_init_p
= true;
3403 if (processing_template_decl
)
3405 /* Avoid an ICE when converting to a base in build_simple_base_path.
3406 We'll throw this all away anyway, and build_new will create
3408 tree t
= fold_convert (build_pointer_type (elt_type
), data_addr
);
3409 /* build_value_init doesn't work in templates, and we don't need
3410 the initializer anyway since we're going to throw it away and
3411 rebuild it at instantiation time, so just build up a single
3412 constructor call to get any appropriate diagnostics. */
3413 init_expr
= cp_build_fold_indirect_ref (t
);
3414 if (type_build_ctor_call (elt_type
))
3415 init_expr
= build_special_member_call (init_expr
,
3416 complete_ctor_identifier
,
3420 stable
= stabilize_init (init_expr
, &init_preeval_expr
);
3424 tree vecinit
= NULL_TREE
;
3425 const size_t len
= vec_safe_length (*init
);
3426 if (len
== 1 && DIRECT_LIST_INIT_P ((**init
)[0]))
3428 vecinit
= (**init
)[0];
3429 if (CONSTRUCTOR_NELTS (vecinit
) == 0)
3430 /* List-value-initialization, leave it alone. */;
3433 tree arraytype
, domain
;
3434 if (TREE_CONSTANT (nelts
))
3435 domain
= compute_array_index_type (NULL_TREE
, nelts
,
3438 /* We'll check the length at runtime. */
3440 arraytype
= build_cplus_array_type (type
, domain
);
3441 /* If we have new char[4]{"foo"}, we have to reshape
3442 so that the STRING_CST isn't wrapped in { }. */
3443 vecinit
= reshape_init (arraytype
, vecinit
, complain
);
3444 /* The middle end doesn't cope with the location wrapper
3445 around a STRING_CST. */
3446 STRIP_ANY_LOCATION_WRAPPER (vecinit
);
3447 vecinit
= digest_init (arraytype
, vecinit
, complain
);
3452 if (complain
& tf_error
)
3453 error ("parenthesized initializer in array new");
3454 return error_mark_node
;
3457 = build_vec_init (data_addr
,
3458 cp_build_binary_op (input_location
,
3459 MINUS_EXPR
, outer_nelts
,
3463 explicit_value_init_p
,
3467 /* An array initialization is stable because the initialization
3468 of each element is a full-expression, so the temporaries don't
3474 init_expr
= cp_build_fold_indirect_ref (data_addr
);
3476 if (type_build_ctor_call (type
) && !explicit_value_init_p
)
3478 init_expr
= build_special_member_call (init_expr
,
3479 complete_ctor_identifier
,
3482 complain
|tf_no_cleanup
);
3484 else if (explicit_value_init_p
)
3486 /* Something like `new int()'. NO_CLEANUP is needed so
3487 we don't try and build a (possibly ill-formed)
3489 tree val
= build_value_init (type
, complain
| tf_no_cleanup
);
3490 if (val
== error_mark_node
)
3491 return error_mark_node
;
3492 init_expr
= build2 (INIT_EXPR
, type
, init_expr
, val
);
3498 /* We are processing something like `new int (10)', which
3499 means allocate an int, and initialize it with 10.
3501 In C++20, also handle `new A(1, 2)'. */
3502 if (cxx_dialect
>= cxx20
3503 && AGGREGATE_TYPE_P (type
)
3504 && (*init
)->length () > 1)
3506 ie
= build_constructor_from_vec (init_list_type_node
, *init
);
3507 CONSTRUCTOR_IS_DIRECT_INIT (ie
) = true;
3508 CONSTRUCTOR_IS_PAREN_INIT (ie
) = true;
3509 ie
= digest_init (type
, ie
, complain
);
3512 ie
= build_x_compound_expr_from_vec (*init
, "new initializer",
3514 init_expr
= cp_build_modify_expr (input_location
, init_expr
,
3515 INIT_EXPR
, ie
, complain
);
3517 /* If the initializer uses C++14 aggregate NSDMI that refer to the
3518 object being initialized, replace them now and don't try to
3520 bool had_placeholder
= false;
3521 if (!processing_template_decl
3522 && TREE_CODE (init_expr
) == INIT_EXPR
)
3523 TREE_OPERAND (init_expr
, 1)
3524 = replace_placeholders (TREE_OPERAND (init_expr
, 1),
3525 TREE_OPERAND (init_expr
, 0),
3527 stable
= (!had_placeholder
3528 && stabilize_init (init_expr
, &init_preeval_expr
));
3531 if (init_expr
== error_mark_node
)
3532 return error_mark_node
;
3534 /* If any part of the object initialization terminates by throwing an
3535 exception and a suitable deallocation function can be found, the
3536 deallocation function is called to free the memory in which the
3537 object was being constructed, after which the exception continues
3538 to propagate in the context of the new-expression. If no
3539 unambiguous matching deallocation function can be found,
3540 propagating the exception does not cause the object's memory to be
3542 if (flag_exceptions
)
3544 enum tree_code dcode
= array_p
? VEC_DELETE_EXPR
: DELETE_EXPR
;
3547 /* The Standard is unclear here, but the right thing to do
3548 is to use the same method for finding deallocation
3549 functions that we use for finding allocation functions. */
3550 cleanup
= (build_op_delete_call
3554 globally_qualified_p
,
3555 placement_allocation_fn_p
? alloc_call
: NULL_TREE
,
3562 /* This is much simpler if we were able to preevaluate all of
3563 the arguments to the constructor call. */
3565 /* CLEANUP is compiler-generated, so no diagnostics. */
3566 suppress_warning (cleanup
);
3567 init_expr
= build2 (TRY_CATCH_EXPR
, void_type_node
,
3568 init_expr
, cleanup
);
3569 /* Likewise, this try-catch is compiler-generated. */
3570 suppress_warning (init_expr
);
3573 /* Ack! First we allocate the memory. Then we set our sentry
3574 variable to true, and expand a cleanup that deletes the
3575 memory if sentry is true. Then we run the constructor, and
3576 finally clear the sentry.
3578 We need to do this because we allocate the space first, so
3579 if there are any temporaries with cleanups in the
3580 constructor args and we weren't able to preevaluate them, we
3581 need this EH region to extend until end of full-expression
3582 to preserve nesting. */
3584 tree end
, sentry
, begin
;
3586 begin
= get_target_expr (boolean_true_node
);
3587 CLEANUP_EH_ONLY (begin
) = 1;
3589 sentry
= TARGET_EXPR_SLOT (begin
);
3591 /* CLEANUP is compiler-generated, so no diagnostics. */
3592 suppress_warning (cleanup
);
3594 TARGET_EXPR_CLEANUP (begin
)
3595 = build3 (COND_EXPR
, void_type_node
, sentry
,
3596 cleanup
, void_node
);
3598 end
= build2 (MODIFY_EXPR
, TREE_TYPE (sentry
),
3599 sentry
, boolean_false_node
);
3602 = build2 (COMPOUND_EXPR
, void_type_node
, begin
,
3603 build2 (COMPOUND_EXPR
, void_type_node
, init_expr
,
3605 /* Likewise, this is compiler-generated. */
3606 suppress_warning (init_expr
);
3611 init_expr
= NULL_TREE
;
3613 /* Now build up the return value in reverse order. */
3618 rval
= build2 (COMPOUND_EXPR
, TREE_TYPE (rval
), init_expr
, rval
);
3620 rval
= build2 (COMPOUND_EXPR
, TREE_TYPE (rval
), cookie_expr
, rval
);
3622 if (rval
== data_addr
)
3623 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
3624 and return the call (which doesn't need to be adjusted). */
3625 rval
= TARGET_EXPR_INITIAL (alloc_expr
);
3630 tree ifexp
= cp_build_binary_op (input_location
,
3631 NE_EXPR
, alloc_node
,
3634 rval
= build_conditional_expr (input_location
, ifexp
, rval
,
3635 alloc_node
, complain
);
3638 /* Perform the allocation before anything else, so that ALLOC_NODE
3639 has been initialized before we start using it. */
3640 rval
= build2 (COMPOUND_EXPR
, TREE_TYPE (rval
), alloc_expr
, rval
);
3643 if (init_preeval_expr
)
3644 rval
= build2 (COMPOUND_EXPR
, TREE_TYPE (rval
), init_preeval_expr
, rval
);
3646 /* A new-expression is never an lvalue. */
3647 gcc_assert (!obvalue_p (rval
));
3649 return convert (pointer_type
, rval
);
3652 /* Generate a representation for a C++ "new" expression. *PLACEMENT
3653 is a vector of placement-new arguments (or NULL if none). If NELTS
3654 is NULL, TYPE is the type of the storage to be allocated. If NELTS
3655 is not NULL, then this is an array-new allocation; TYPE is the type
3656 of the elements in the array and NELTS is the number of elements in
3657 the array. *INIT, if non-NULL, is the initializer for the new
3658 object, or an empty vector to indicate an initializer of "()". If
3659 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
3660 rather than just "new". This may change PLACEMENT and INIT. */
3663 build_new (location_t loc
, vec
<tree
, va_gc
> **placement
, tree type
,
3664 tree nelts
, vec
<tree
, va_gc
> **init
, int use_global_new
,
3665 tsubst_flags_t complain
)
3668 vec
<tree
, va_gc
> *orig_placement
= NULL
;
3669 tree orig_nelts
= NULL_TREE
;
3670 vec
<tree
, va_gc
> *orig_init
= NULL
;
3672 if (type
== error_mark_node
)
3673 return error_mark_node
;
3675 if (nelts
== NULL_TREE
3676 /* Don't do auto deduction where it might affect mangling. */
3677 && (!processing_template_decl
|| at_function_scope_p ()))
3679 tree auto_node
= type_uses_auto (type
);
3682 tree d_init
= NULL_TREE
;
3683 const size_t len
= vec_safe_length (*init
);
3684 /* E.g. new auto(x) must have exactly one element, or
3685 a {} initializer will have one element. */
3688 d_init
= (**init
)[0];
3689 d_init
= resolve_nondeduced_context (d_init
, complain
);
3691 /* For the rest, e.g. new A(1, 2, 3), create a list. */
3697 FOR_EACH_VEC_ELT (**init
, n
, t
)
3699 t
= resolve_nondeduced_context (t
, complain
);
3700 *pp
= build_tree_list (NULL_TREE
, t
);
3701 pp
= &TREE_CHAIN (*pp
);
3704 type
= do_auto_deduction (type
, d_init
, auto_node
, complain
);
3708 if (processing_template_decl
)
3710 if (dependent_type_p (type
)
3711 || any_type_dependent_arguments_p (*placement
)
3712 || (nelts
&& type_dependent_expression_p (nelts
))
3714 || any_type_dependent_arguments_p (*init
))
3715 return build_raw_new_expr (loc
, *placement
, type
, nelts
, *init
,
3718 orig_placement
= make_tree_vector_copy (*placement
);
3722 orig_init
= make_tree_vector_copy (*init
);
3723 /* Also copy any CONSTRUCTORs in *init, since reshape_init and
3724 digest_init clobber them in place. */
3725 for (unsigned i
= 0; i
< orig_init
->length(); ++i
)
3727 tree e
= (**init
)[i
];
3728 if (TREE_CODE (e
) == CONSTRUCTOR
)
3729 (**init
)[i
] = copy_node (e
);
3733 make_args_non_dependent (*placement
);
3735 nelts
= build_non_dependent_expr (nelts
);
3736 make_args_non_dependent (*init
);
3741 location_t nelts_loc
= cp_expr_loc_or_loc (nelts
, loc
);
3742 if (!build_expr_type_conversion (WANT_INT
| WANT_ENUM
, nelts
, false))
3744 if (complain
& tf_error
)
3745 permerror (nelts_loc
,
3746 "size in array new must have integral type");
3748 return error_mark_node
;
3751 /* Try to determine the constant value only for the purposes
3752 of the diagnostic below but continue to use the original
3753 value and handle const folding later. */
3754 const_tree cst_nelts
= fold_non_dependent_expr (nelts
, complain
);
3756 /* The expression in a noptr-new-declarator is erroneous if it's of
3757 non-class type and its value before converting to std::size_t is
3758 less than zero. ... If the expression is a constant expression,
3759 the program is ill-fomed. */
3760 if (TREE_CODE (cst_nelts
) == INTEGER_CST
3761 && !valid_array_size_p (nelts_loc
, cst_nelts
, NULL_TREE
,
3762 complain
& tf_error
))
3763 return error_mark_node
;
3765 nelts
= mark_rvalue_use (nelts
);
3766 nelts
= cp_save_expr (cp_convert (sizetype
, nelts
, complain
));
3769 /* ``A reference cannot be created by the new operator. A reference
3770 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3771 returned by new.'' ARM 5.3.3 */
3772 if (TYPE_REF_P (type
))
3774 if (complain
& tf_error
)
3775 error_at (loc
, "new cannot be applied to a reference type");
3777 return error_mark_node
;
3778 type
= TREE_TYPE (type
);
3781 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3783 if (complain
& tf_error
)
3784 error_at (loc
, "new cannot be applied to a function type");
3785 return error_mark_node
;
3788 /* P1009: Array size deduction in new-expressions. */
3789 const bool array_p
= TREE_CODE (type
) == ARRAY_TYPE
;
3791 /* If ARRAY_P, we have to deduce the array bound. For C++20 paren-init,
3792 we have to process the parenthesized-list. But don't do it for (),
3793 which is value-initialization, and INIT should stay empty. */
3794 && (array_p
|| (cxx_dialect
>= cxx20
&& nelts
&& !(*init
)->is_empty ())))
3796 /* This means we have 'new T[]()'. */
3797 if ((*init
)->is_empty ())
3799 tree ctor
= build_constructor (init_list_type_node
, NULL
);
3800 CONSTRUCTOR_IS_DIRECT_INIT (ctor
) = true;
3801 vec_safe_push (*init
, ctor
);
3803 tree
&elt
= (**init
)[0];
3804 /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */
3805 if (!DIRECT_LIST_INIT_P (elt
) && cxx_dialect
>= cxx20
)
3807 tree ctor
= build_constructor_from_vec (init_list_type_node
, *init
);
3808 CONSTRUCTOR_IS_DIRECT_INIT (ctor
) = true;
3809 CONSTRUCTOR_IS_PAREN_INIT (ctor
) = true;
3811 /* We've squashed all the vector elements into the first one;
3812 truncate the rest. */
3813 (*init
)->truncate (1);
3815 /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */
3816 if (array_p
&& !TYPE_DOMAIN (type
))
3818 /* We need to reshape before deducing the bounds to handle code like
3820 struct S { int x, y; };
3821 new S[]{1, 2, 3, 4};
3823 which should deduce S[2]. But don't change ELT itself: we want to
3824 pass a list-initializer to build_new_1, even for STRING_CSTs. */
3826 if (BRACE_ENCLOSED_INITIALIZER_P (e
))
3827 e
= reshape_init (type
, e
, complain
);
3828 cp_complete_array_type (&type
, e
, /*do_default*/false);
3832 /* The type allocated must be complete. If the new-type-id was
3833 "T[N]" then we are just checking that "T" is complete here, but
3834 that is equivalent, since the value of "N" doesn't matter. */
3835 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
3836 return error_mark_node
;
3838 rval
= build_new_1 (placement
, type
, nelts
, init
, use_global_new
, complain
);
3839 if (rval
== error_mark_node
)
3840 return error_mark_node
;
3842 if (processing_template_decl
)
3844 tree ret
= build_raw_new_expr (loc
, orig_placement
, type
, orig_nelts
,
3845 orig_init
, use_global_new
);
3846 release_tree_vector (orig_placement
);
3847 release_tree_vector (orig_init
);
3851 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
3852 rval
= build1_loc (loc
, NOP_EXPR
, TREE_TYPE (rval
), rval
);
3853 suppress_warning (rval
, OPT_Wunused_value
);
3859 build_vec_delete_1 (location_t loc
, tree base
, tree maxindex
, tree type
,
3860 special_function_kind auto_delete_vec
,
3861 int use_global_delete
, tsubst_flags_t complain
)
3864 tree ptype
= build_pointer_type (type
= complete_type (type
));
3867 /* Temporary variables used by the loop. */
3868 tree tbase
, tbase_init
;
3870 /* This is the body of the loop that implements the deletion of a
3871 single element, and moves temp variables to next elements. */
3874 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3877 /* This is the thing that governs what to do after the loop has run. */
3878 tree deallocate_expr
= 0;
3880 /* This is the BIND_EXPR which holds the outermost iterator of the
3881 loop. It is convenient to set this variable up and test it before
3882 executing any other code in the loop.
3883 This is also the containing expression returned by this function. */
3884 tree controller
= NULL_TREE
;
3887 /* We should only have 1-D arrays here. */
3888 gcc_assert (TREE_CODE (type
) != ARRAY_TYPE
);
3890 if (base
== error_mark_node
|| maxindex
== error_mark_node
)
3891 return error_mark_node
;
3893 if (!verify_type_context (loc
, TCTX_DEALLOCATION
, type
,
3894 !(complain
& tf_error
)))
3895 return error_mark_node
;
3897 if (!COMPLETE_TYPE_P (type
))
3899 if (complain
& tf_warning
)
3901 auto_diagnostic_group d
;
3902 if (warning_at (loc
, OPT_Wdelete_incomplete
,
3903 "possible problem detected in invocation of "
3904 "operator %<delete []%>"))
3906 cxx_incomplete_type_diagnostic (base
, type
, DK_WARNING
);
3907 inform (loc
, "neither the destructor nor the "
3908 "class-specific operator %<delete []%> will be called, "
3909 "even if they are declared when the class is defined");
3912 /* This size won't actually be used. */
3913 size_exp
= size_one_node
;
3917 size_exp
= size_in_bytes (type
);
3919 if (! MAYBE_CLASS_TYPE_P (type
))
3921 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type
))
3923 /* Make sure the destructor is callable. */
3924 if (type_build_dtor_call (type
))
3926 tmp
= build_delete (loc
, ptype
, base
, sfk_complete_destructor
,
3927 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 1,
3929 if (tmp
== error_mark_node
)
3930 return error_mark_node
;
3935 /* The below is short by the cookie size. */
3936 virtual_size
= size_binop (MULT_EXPR
, size_exp
,
3937 fold_convert (sizetype
, maxindex
));
3939 tbase
= create_temporary_var (ptype
);
3940 DECL_INITIAL (tbase
)
3941 = fold_build_pointer_plus_loc (loc
, fold_convert (ptype
, base
),
3943 tbase_init
= build_stmt (loc
, DECL_EXPR
, tbase
);
3944 controller
= build3 (BIND_EXPR
, void_type_node
, tbase
, NULL_TREE
, NULL_TREE
);
3945 TREE_SIDE_EFFECTS (controller
) = 1;
3947 body
= build1 (EXIT_EXPR
, void_type_node
,
3948 build2 (EQ_EXPR
, boolean_type_node
, tbase
,
3949 fold_convert (ptype
, base
)));
3950 tmp
= fold_build1_loc (loc
, NEGATE_EXPR
, sizetype
, size_exp
);
3951 tmp
= fold_build_pointer_plus (tbase
, tmp
);
3952 tmp
= cp_build_modify_expr (loc
, tbase
, NOP_EXPR
, tmp
, complain
);
3953 if (tmp
== error_mark_node
)
3954 return error_mark_node
;
3955 body
= build_compound_expr (loc
, body
, tmp
);
3956 tmp
= build_delete (loc
, ptype
, tbase
, sfk_complete_destructor
,
3957 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 1,
3959 if (tmp
== error_mark_node
)
3960 return error_mark_node
;
3961 body
= build_compound_expr (loc
, body
, tmp
);
3963 loop
= build1 (LOOP_EXPR
, void_type_node
, body
);
3964 loop
= build_compound_expr (loc
, tbase_init
, loop
);
3967 /* Delete the storage if appropriate. */
3968 if (auto_delete_vec
== sfk_deleting_destructor
)
3972 /* The below is short by the cookie size. */
3973 virtual_size
= size_binop (MULT_EXPR
, size_exp
,
3974 fold_convert (sizetype
, maxindex
));
3976 if (! TYPE_VEC_NEW_USES_COOKIE (type
))
3983 cookie_size
= targetm
.cxx
.get_cookie_size (type
);
3984 base_tbd
= cp_build_binary_op (loc
,
3986 cp_convert (string_type_node
,
3990 if (base_tbd
== error_mark_node
)
3991 return error_mark_node
;
3992 base_tbd
= cp_convert (ptype
, base_tbd
, complain
);
3993 /* True size with header. */
3994 virtual_size
= size_binop (PLUS_EXPR
, virtual_size
, cookie_size
);
3997 deallocate_expr
= build_op_delete_call (VEC_DELETE_EXPR
,
3998 base_tbd
, virtual_size
,
3999 use_global_delete
& 1,
4000 /*placement=*/NULL_TREE
,
4001 /*alloc_fn=*/NULL_TREE
,
4006 if (deallocate_expr
== error_mark_node
)
4007 return error_mark_node
;
4008 else if (!deallocate_expr
)
4011 body
= deallocate_expr
;
4013 /* The delete operator must be called, even if a destructor
4015 body
= build2 (TRY_FINALLY_EXPR
, void_type_node
, body
, deallocate_expr
);
4018 body
= integer_zero_node
;
4020 /* Outermost wrapper: If pointer is null, punt. */
4021 tree cond
= build2_loc (loc
, NE_EXPR
, boolean_type_node
, base
,
4022 fold_convert (TREE_TYPE (base
), nullptr_node
));
4023 /* This is a compiler generated comparison, don't emit
4024 e.g. -Wnonnull-compare warning for it. */
4025 suppress_warning (cond
, OPT_Wnonnull_compare
);
4026 body
= build3_loc (loc
, COND_EXPR
, void_type_node
,
4027 cond
, body
, integer_zero_node
);
4028 COND_EXPR_IS_VEC_DELETE (body
) = true;
4029 body
= build1 (NOP_EXPR
, void_type_node
, body
);
4033 TREE_OPERAND (controller
, 1) = body
;
4037 if (TREE_CODE (base
) == SAVE_EXPR
)
4038 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
4039 body
= build2 (COMPOUND_EXPR
, void_type_node
, base
, body
);
4041 return convert_to_void (body
, ICV_CAST
, complain
);
4044 /* Create an unnamed variable of the indicated TYPE. */
4047 create_temporary_var (tree type
)
4051 decl
= build_decl (input_location
,
4052 VAR_DECL
, NULL_TREE
, type
);
4053 TREE_USED (decl
) = 1;
4054 DECL_ARTIFICIAL (decl
) = 1;
4055 DECL_IGNORED_P (decl
) = 1;
4056 DECL_CONTEXT (decl
) = current_function_decl
;
4061 /* Create a new temporary variable of the indicated TYPE, initialized
4064 It is not entered into current_binding_level, because that breaks
4065 things when it comes time to do final cleanups (which take place
4066 "outside" the binding contour of the function). */
4069 get_temp_regvar (tree type
, tree init
)
4073 decl
= create_temporary_var (type
);
4074 add_decl_expr (decl
);
4076 finish_expr_stmt (cp_build_modify_expr (input_location
, decl
, INIT_EXPR
,
4077 init
, tf_warning_or_error
));
4082 /* Subroutine of build_vec_init. Returns true if assigning to an array of
4083 INNER_ELT_TYPE from INIT is trivial. */
4086 vec_copy_assign_is_trivial (tree inner_elt_type
, tree init
)
4088 tree fromtype
= inner_elt_type
;
4089 if (lvalue_p (init
))
4090 fromtype
= cp_build_reference_type (fromtype
, /*rval*/false);
4091 return is_trivially_xible (MODIFY_EXPR
, inner_elt_type
, fromtype
);
4094 /* Subroutine of build_vec_init: Check that the array has at least N
4095 elements. Other parameters are local variables in build_vec_init. */
4098 finish_length_check (tree atype
, tree iterator
, tree obase
, unsigned n
)
4100 tree nelts
= build_int_cst (ptrdiff_type_node
, n
- 1);
4101 if (TREE_CODE (atype
) != ARRAY_TYPE
)
4103 if (flag_exceptions
)
4105 tree c
= fold_build2 (LT_EXPR
, boolean_type_node
, iterator
,
4107 c
= build3 (COND_EXPR
, void_type_node
, c
,
4108 throw_bad_array_new_length (), void_node
);
4109 finish_expr_stmt (c
);
4111 /* Don't check an array new when -fno-exceptions. */
4113 else if (sanitize_flags_p (SANITIZE_BOUNDS
)
4114 && current_function_decl
!= NULL_TREE
)
4116 /* Make sure the last element of the initializer is in bounds. */
4118 (ubsan_instrument_bounds
4119 (input_location
, obase
, &nelts
, /*ignore_off_by_one*/false));
4123 /* `build_vec_init' returns tree structure that performs
4124 initialization of a vector of aggregate types.
4126 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
4127 to the first element, of POINTER_TYPE.
4128 MAXINDEX is the maximum index of the array (one less than the
4129 number of elements). It is only used if BASE is a pointer or
4130 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
4132 INIT is the (possibly NULL) initializer.
4134 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
4135 elements in the array are value-initialized.
4137 FROM_ARRAY is 0 if we should init everything with INIT
4138 (i.e., every element initialized from INIT).
4139 FROM_ARRAY is 1 if we should index into INIT in parallel
4140 with initialization of DECL.
4141 FROM_ARRAY is 2 if we should index into INIT in parallel,
4142 but use assignment instead of initialization. */
4145 build_vec_init (tree base
, tree maxindex
, tree init
,
4146 bool explicit_value_init_p
,
4147 int from_array
, tsubst_flags_t complain
)
4150 tree base2
= NULL_TREE
;
4151 tree itype
= NULL_TREE
;
4153 /* The type of BASE. */
4154 tree atype
= TREE_TYPE (base
);
4155 /* The type of an element in the array. */
4156 tree type
= TREE_TYPE (atype
);
4157 /* The element type reached after removing all outer array
4159 tree inner_elt_type
;
4160 /* The type of a pointer to an element in the array. */
4165 tree try_block
= NULL_TREE
;
4166 HOST_WIDE_INT num_initialized_elts
= 0;
4169 bool xvalue
= false;
4170 bool errors
= false;
4171 location_t loc
= (init
? cp_expr_loc_or_input_loc (init
)
4172 : location_of (base
));
4174 if (TREE_CODE (atype
) == ARRAY_TYPE
&& TYPE_DOMAIN (atype
))
4175 maxindex
= array_type_nelts (atype
);
4177 if (maxindex
== NULL_TREE
|| maxindex
== error_mark_node
)
4178 return error_mark_node
;
4180 maxindex
= maybe_constant_value (maxindex
);
4181 if (explicit_value_init_p
)
4184 inner_elt_type
= strip_array_types (type
);
4186 /* Look through the TARGET_EXPR around a compound literal. */
4187 if (init
&& TREE_CODE (init
) == TARGET_EXPR
4188 && TREE_CODE (TARGET_EXPR_INITIAL (init
)) == CONSTRUCTOR
4190 init
= TARGET_EXPR_INITIAL (init
);
4192 bool direct_init
= false;
4193 if (from_array
&& init
&& BRACE_ENCLOSED_INITIALIZER_P (init
)
4194 && CONSTRUCTOR_NELTS (init
) == 1)
4196 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
4197 if (TREE_CODE (TREE_TYPE (elt
)) == ARRAY_TYPE
)
4199 direct_init
= DIRECT_LIST_INIT_P (init
);
4204 /* If we have a braced-init-list or string constant, make sure that the array
4205 is big enough for all the initializers. */
4206 bool length_check
= (init
4207 && (TREE_CODE (init
) == STRING_CST
4208 || (TREE_CODE (init
) == CONSTRUCTOR
4209 && CONSTRUCTOR_NELTS (init
) > 0))
4210 && !TREE_CONSTANT (maxindex
));
4213 && TREE_CODE (atype
) == ARRAY_TYPE
4214 && TREE_CONSTANT (maxindex
)
4216 ? vec_copy_assign_is_trivial (inner_elt_type
, init
)
4217 : !TYPE_NEEDS_CONSTRUCTING (type
))
4218 && ((TREE_CODE (init
) == CONSTRUCTOR
4219 && (BRACE_ENCLOSED_INITIALIZER_P (init
)
4220 || (same_type_ignoring_top_level_qualifiers_p
4221 (atype
, TREE_TYPE (init
))))
4222 /* Don't do this if the CONSTRUCTOR might contain something
4223 that might throw and require us to clean up. */
4224 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
))
4225 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type
)))
4228 /* Do non-default initialization of trivial arrays resulting from
4229 brace-enclosed initializers. In this case, digest_init and
4230 store_constructor will handle the semantics for us. */
4232 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
4233 init
= digest_init (atype
, init
, complain
);
4234 stmt_expr
= build2 (INIT_EXPR
, atype
, base
, init
);
4238 maxindex
= cp_convert (ptrdiff_type_node
, maxindex
, complain
);
4239 maxindex
= fold_simple (maxindex
);
4241 if (TREE_CODE (atype
) == ARRAY_TYPE
)
4243 ptype
= build_pointer_type (type
);
4244 base
= decay_conversion (base
, complain
);
4245 if (base
== error_mark_node
)
4246 return error_mark_node
;
4247 base
= cp_convert (ptype
, base
, complain
);
4252 if (integer_all_onesp (maxindex
))
4254 /* Shortcut zero element case to avoid unneeded constructor synthesis. */
4255 if (init
&& TREE_SIDE_EFFECTS (init
))
4256 base
= build2 (COMPOUND_EXPR
, ptype
, init
, base
);
4260 /* The code we are generating looks like:
4264 ptrdiff_t iterator = maxindex;
4266 for (; iterator != -1; --iterator) {
4267 ... initialize *t1 ...
4271 ... destroy elements that were constructed ...
4276 We can omit the try and catch blocks if we know that the
4277 initialization will never throw an exception, or if the array
4278 elements do not have destructors. We can omit the loop completely if
4279 the elements of the array do not have constructors.
4281 We actually wrap the entire body of the above in a STMT_EXPR, for
4284 When copying from array to another, when the array elements have
4285 only trivial copy constructors, we should use __builtin_memcpy
4286 rather than generating a loop. That way, we could take advantage
4287 of whatever cleverness the back end has for dealing with copies
4288 of blocks of memory. */
4290 is_global
= begin_init_stmts (&stmt_expr
, &compound_stmt
);
4291 destroy_temps
= stmts_are_full_exprs_p ();
4292 current_stmt_tree ()->stmts_are_full_exprs_p
= 0;
4293 rval
= get_temp_regvar (ptype
, base
);
4294 base
= get_temp_regvar (ptype
, rval
);
4295 iterator
= get_temp_regvar (ptrdiff_type_node
, maxindex
);
4297 /* If initializing one array from another, initialize element by
4298 element. We rely upon the below calls to do the argument
4299 checking. Evaluate the initializer before entering the try block. */
4300 if (from_array
&& init
&& TREE_CODE (init
) != CONSTRUCTOR
)
4302 if (lvalue_kind (init
) & clk_rvalueref
)
4304 base2
= decay_conversion (init
, complain
);
4305 if (base2
== error_mark_node
)
4306 return error_mark_node
;
4307 itype
= TREE_TYPE (base2
);
4308 base2
= get_temp_regvar (itype
, base2
);
4309 itype
= TREE_TYPE (itype
);
4312 /* Protect the entire array initialization so that we can destroy
4313 the partially constructed array if an exception is thrown.
4314 But don't do this if we're assigning. */
4315 if (flag_exceptions
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4318 try_block
= begin_try_block ();
4321 /* Should we try to create a constant initializer? */
4322 bool try_const
= (TREE_CODE (atype
) == ARRAY_TYPE
4323 && TREE_CONSTANT (maxindex
)
4324 && (init
? TREE_CODE (init
) == CONSTRUCTOR
4325 : (type_has_constexpr_default_constructor
4327 && (literal_type_p (inner_elt_type
)
4328 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type
)));
4329 vec
<constructor_elt
, va_gc
> *const_vec
= NULL
;
4330 bool saw_non_const
= false;
4331 /* If we're initializing a static array, we want to do static
4332 initialization of any elements with constant initializers even if
4333 some are non-constant. */
4334 bool do_static_init
= (DECL_P (obase
) && TREE_STATIC (obase
));
4336 bool empty_list
= false;
4337 if (init
&& BRACE_ENCLOSED_INITIALIZER_P (init
)
4338 && CONSTRUCTOR_NELTS (init
) == 0)
4339 /* Skip over the handling of non-empty init lists. */
4342 /* Maybe pull out constant value when from_array? */
4344 else if (init
!= NULL_TREE
&& TREE_CODE (init
) == CONSTRUCTOR
)
4346 /* Do non-default initialization of non-trivial arrays resulting from
4347 brace-enclosed initializers. */
4348 unsigned HOST_WIDE_INT idx
;
4350 /* If the constructor already has the array type, it's been through
4351 digest_init, so we shouldn't try to do anything more. */
4352 bool digested
= same_type_p (atype
, TREE_TYPE (init
));
4356 finish_length_check (atype
, iterator
, obase
, CONSTRUCTOR_NELTS (init
));
4359 vec_alloc (const_vec
, CONSTRUCTOR_NELTS (init
));
4361 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
, field
, elt
)
4363 tree baseref
= build1 (INDIRECT_REF
, type
, base
);
4366 num_initialized_elts
++;
4368 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
4370 one_init
= build2 (INIT_EXPR
, type
, baseref
, elt
);
4371 else if (MAYBE_CLASS_TYPE_P (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
4372 one_init
= build_aggr_init (baseref
, elt
, 0, complain
);
4374 one_init
= cp_build_modify_expr (input_location
, baseref
,
4375 NOP_EXPR
, elt
, complain
);
4376 if (one_init
== error_mark_node
)
4381 field
= size_int (idx
);
4382 tree e
= maybe_constant_init (one_init
);
4383 if (reduced_constant_expression_p (e
))
4385 CONSTRUCTOR_APPEND_ELT (const_vec
, field
, e
);
4387 one_init
= NULL_TREE
;
4389 one_init
= build2 (INIT_EXPR
, type
, baseref
, e
);
4395 tree value
= build_zero_init (TREE_TYPE (e
), NULL_TREE
,
4398 CONSTRUCTOR_APPEND_ELT (const_vec
, field
, value
);
4400 saw_non_const
= true;
4405 finish_expr_stmt (one_init
);
4406 current_stmt_tree ()->stmts_are_full_exprs_p
= 0;
4408 one_init
= cp_build_unary_op (PREINCREMENT_EXPR
, base
, false,
4410 if (one_init
== error_mark_node
)
4413 finish_expr_stmt (one_init
);
4415 one_init
= cp_build_unary_op (PREDECREMENT_EXPR
, iterator
, false,
4417 if (one_init
== error_mark_node
)
4420 finish_expr_stmt (one_init
);
4423 /* Any elements without explicit initializers get T{}. */
4426 else if (init
&& TREE_CODE (init
) == STRING_CST
)
4428 /* Check that the array is at least as long as the string. */
4430 finish_length_check (atype
, iterator
, obase
,
4431 TREE_STRING_LENGTH (init
));
4432 tree length
= build_int_cst (ptrdiff_type_node
,
4433 TREE_STRING_LENGTH (init
));
4435 /* Copy the string to the first part of the array. */
4436 tree alias_set
= build_int_cst (build_pointer_type (type
), 0);
4437 tree lhs
= build2 (MEM_REF
, TREE_TYPE (init
), base
, alias_set
);
4438 tree stmt
= build2 (MODIFY_EXPR
, void_type_node
, lhs
, init
);
4439 finish_expr_stmt (stmt
);
4441 /* Adjust the counter and pointer. */
4442 stmt
= cp_build_binary_op (loc
, MINUS_EXPR
, iterator
, length
, complain
);
4443 stmt
= build2 (MODIFY_EXPR
, void_type_node
, iterator
, stmt
);
4444 finish_expr_stmt (stmt
);
4446 stmt
= cp_build_binary_op (loc
, PLUS_EXPR
, base
, length
, complain
);
4447 stmt
= build2 (MODIFY_EXPR
, void_type_node
, base
, stmt
);
4448 finish_expr_stmt (stmt
);
4450 /* And set the rest of the array to NUL. */
4452 explicit_value_init_p
= true;
4454 else if (from_array
)
4457 /* OK, we set base2 above. */;
4458 else if (CLASS_TYPE_P (type
)
4459 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
4461 if (complain
& tf_error
)
4462 error ("initializer ends prematurely");
4467 /* Now, default-initialize any remaining elements. We don't need to
4468 do that if a) the type does not need constructing, or b) we've
4469 already initialized all the elements.
4471 We do need to keep going if we're copying an array. */
4473 if (try_const
&& !init
)
4474 /* With a constexpr default constructor, which we checked for when
4475 setting try_const above, default-initialization is equivalent to
4476 value-initialization, and build_value_init gives us something more
4477 friendly to maybe_constant_init. */
4478 explicit_value_init_p
= true;
4480 || ((type_build_ctor_call (type
) || init
|| explicit_value_init_p
)
4481 && ! (tree_fits_shwi_p (maxindex
)
4482 && (num_initialized_elts
4483 == tree_to_shwi (maxindex
) + 1))))
4485 /* If the ITERATOR is lesser or equal to -1, then we don't have to loop;
4486 we've already initialized all the elements. */
4491 for_stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
4492 finish_init_stmt (for_stmt
);
4493 finish_for_cond (build2 (GT_EXPR
, boolean_type_node
, iterator
,
4494 build_int_cst (TREE_TYPE (iterator
), -1)),
4495 for_stmt
, false, 0);
4496 elt_init
= cp_build_unary_op (PREDECREMENT_EXPR
, iterator
, false,
4498 if (elt_init
== error_mark_node
)
4500 finish_for_expr (elt_init
, for_stmt
);
4502 to
= build1 (INDIRECT_REF
, type
, base
);
4504 /* If the initializer is {}, then all elements are initialized from T{}.
4505 But for non-classes, that's the same as value-initialization. */
4508 if (cxx_dialect
>= cxx11
&& AGGREGATE_TYPE_P (type
))
4510 init
= build_constructor (init_list_type_node
, NULL
);
4515 explicit_value_init_p
= true;
4525 from
= build1 (INDIRECT_REF
, itype
, base2
);
4529 from
= build_tree_list (NULL_TREE
, from
);
4534 if (TREE_CODE (type
) == ARRAY_TYPE
)
4535 elt_init
= build_vec_init (to
, NULL_TREE
, from
, /*val_init*/false,
4536 from_array
, complain
);
4537 else if (from_array
== 2)
4538 elt_init
= cp_build_modify_expr (input_location
, to
, NOP_EXPR
,
4540 else if (type_build_ctor_call (type
))
4541 elt_init
= build_aggr_init (to
, from
, 0, complain
);
4543 elt_init
= cp_build_modify_expr (input_location
, to
, NOP_EXPR
, from
,
4548 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4550 if (init
&& !BRACE_ENCLOSED_INITIALIZER_P (init
))
4552 if ((complain
& tf_error
))
4553 error_at (loc
, "array must be initialized "
4554 "with a brace-enclosed initializer");
4555 elt_init
= error_mark_node
;
4558 elt_init
= build_vec_init (build1 (INDIRECT_REF
, type
, base
),
4560 explicit_value_init_p
,
4563 else if (explicit_value_init_p
)
4565 elt_init
= build_value_init (type
, complain
);
4566 if (elt_init
!= error_mark_node
)
4567 elt_init
= build2 (INIT_EXPR
, type
, to
, elt_init
);
4571 gcc_assert (type_build_ctor_call (type
) || init
);
4572 if (CLASS_TYPE_P (type
))
4573 elt_init
= build_aggr_init (to
, init
, 0, complain
);
4576 if (TREE_CODE (init
) == TREE_LIST
)
4577 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
4579 elt_init
= (init
== error_mark_node
4581 : build2 (INIT_EXPR
, type
, to
, init
));
4585 if (elt_init
== error_mark_node
)
4590 /* FIXME refs to earlier elts */
4591 tree e
= maybe_constant_init (elt_init
);
4592 if (reduced_constant_expression_p (e
))
4594 if (initializer_zerop (e
))
4595 /* Don't fill the CONSTRUCTOR with zeros. */
4598 elt_init
= NULL_TREE
;
4602 saw_non_const
= true;
4604 e
= build_zero_init (TREE_TYPE (e
), NULL_TREE
, true);
4611 HOST_WIDE_INT last
= tree_to_shwi (maxindex
);
4612 if (num_initialized_elts
<= last
)
4614 tree field
= size_int (num_initialized_elts
);
4615 if (num_initialized_elts
!= last
)
4616 field
= build2 (RANGE_EXPR
, sizetype
, field
,
4618 CONSTRUCTOR_APPEND_ELT (const_vec
, field
, e
);
4623 current_stmt_tree ()->stmts_are_full_exprs_p
= 1;
4624 if (elt_init
&& !errors
)
4625 finish_expr_stmt (elt_init
);
4626 current_stmt_tree ()->stmts_are_full_exprs_p
= 0;
4628 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR
, base
, false,
4631 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR
, base2
, false,
4634 finish_for_stmt (for_stmt
);
4637 /* Make sure to cleanup any partially constructed elements. */
4638 if (flag_exceptions
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4642 tree m
= cp_build_binary_op (input_location
,
4643 MINUS_EXPR
, maxindex
, iterator
,
4646 /* Flatten multi-dimensional array since build_vec_delete only
4647 expects one-dimensional array. */
4648 if (TREE_CODE (type
) == ARRAY_TYPE
)
4649 m
= cp_build_binary_op (input_location
,
4651 /* Avoid mixing signed and unsigned. */
4652 convert (TREE_TYPE (m
),
4653 array_type_nelts_total (type
)),
4656 finish_cleanup_try_block (try_block
);
4657 e
= build_vec_delete_1 (input_location
, rval
, m
,
4658 inner_elt_type
, sfk_complete_destructor
,
4659 /*use_global_delete=*/0, complain
);
4660 if (e
== error_mark_node
)
4662 finish_cleanup (e
, try_block
);
4665 /* The value of the array initialization is the array itself, RVAL
4666 is a pointer to the first element. */
4667 finish_stmt_expr_expr (rval
, stmt_expr
);
4669 stmt_expr
= finish_init_stmts (is_global
, stmt_expr
, compound_stmt
);
4671 current_stmt_tree ()->stmts_are_full_exprs_p
= destroy_temps
;
4674 return error_mark_node
;
4680 tree const_init
= build_constructor (atype
, const_vec
);
4681 return build2 (INIT_EXPR
, atype
, obase
, const_init
);
4683 else if (do_static_init
&& !vec_safe_is_empty (const_vec
))
4684 DECL_INITIAL (obase
) = build_constructor (atype
, const_vec
);
4686 vec_free (const_vec
);
4689 /* Now make the result have the correct type. */
4690 if (TREE_CODE (atype
) == ARRAY_TYPE
)
4692 atype
= build_pointer_type (atype
);
4693 stmt_expr
= build1 (NOP_EXPR
, atype
, stmt_expr
);
4694 stmt_expr
= cp_build_fold_indirect_ref (stmt_expr
);
4695 suppress_warning (stmt_expr
/* What warning? */);
4701 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
4705 build_dtor_call (tree exp
, special_function_kind dtor_kind
, int flags
,
4706 tsubst_flags_t complain
)
4711 case sfk_complete_destructor
:
4712 name
= complete_dtor_identifier
;
4715 case sfk_base_destructor
:
4716 name
= base_dtor_identifier
;
4719 case sfk_deleting_destructor
:
4720 name
= deleting_dtor_identifier
;
4727 return build_special_member_call (exp
, name
,
4729 /*binfo=*/TREE_TYPE (exp
),
4734 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
4735 ADDR is an expression which yields the store to be destroyed.
4736 AUTO_DELETE is the name of the destructor to call, i.e., either
4737 sfk_complete_destructor, sfk_base_destructor, or
4738 sfk_deleting_destructor.
4740 FLAGS is the logical disjunction of zero or more LOOKUP_
4741 flags. See cp-tree.h for more info. */
4744 build_delete (location_t loc
, tree otype
, tree addr
,
4745 special_function_kind auto_delete
,
4746 int flags
, int use_global_delete
, tsubst_flags_t complain
)
4750 if (addr
== error_mark_node
)
4751 return error_mark_node
;
4753 tree type
= TYPE_MAIN_VARIANT (otype
);
4755 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
4756 set to `error_mark_node' before it gets properly cleaned up. */
4757 if (type
== error_mark_node
)
4758 return error_mark_node
;
4760 if (TYPE_PTR_P (type
))
4761 type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
4763 if (TREE_CODE (type
) == ARRAY_TYPE
)
4765 if (TYPE_DOMAIN (type
) == NULL_TREE
)
4767 if (complain
& tf_error
)
4768 error_at (loc
, "unknown array size in delete");
4769 return error_mark_node
;
4771 return build_vec_delete (loc
, addr
, array_type_nelts (type
),
4772 auto_delete
, use_global_delete
, complain
);
4775 bool deleting
= (auto_delete
== sfk_deleting_destructor
);
4776 gcc_assert (deleting
== !(flags
& LOOKUP_DESTRUCTOR
));
4778 if (TYPE_PTR_P (otype
))
4780 addr
= mark_rvalue_use (addr
);
4782 /* We don't want to warn about delete of void*, only other
4783 incomplete types. Deleting other incomplete types
4784 invokes undefined behavior, but it is not ill-formed, so
4785 compile to something that would even do The Right Thing
4786 (TM) should the type have a trivial dtor and no delete
4788 if (!VOID_TYPE_P (type
))
4790 complete_type (type
);
4792 && !verify_type_context (loc
, TCTX_DEALLOCATION
, type
,
4793 !(complain
& tf_error
)))
4794 return error_mark_node
;
4796 if (!COMPLETE_TYPE_P (type
))
4798 if (complain
& tf_warning
)
4800 auto_diagnostic_group d
;
4801 if (warning_at (loc
, OPT_Wdelete_incomplete
,
4802 "possible problem detected in invocation of "
4803 "%<operator delete%>"))
4805 cxx_incomplete_type_diagnostic (addr
, type
, DK_WARNING
);
4807 "neither the destructor nor the class-specific "
4808 "%<operator delete%> will be called, even if "
4809 "they are declared when the class is defined");
4813 else if (deleting
&& warn_delnonvdtor
4814 && MAYBE_CLASS_TYPE_P (type
) && !CLASSTYPE_FINAL (type
)
4815 && TYPE_POLYMORPHIC_P (type
))
4817 tree dtor
= CLASSTYPE_DESTRUCTOR (type
);
4818 if (!dtor
|| !DECL_VINDEX (dtor
))
4820 if (CLASSTYPE_PURE_VIRTUALS (type
))
4821 warning_at (loc
, OPT_Wdelete_non_virtual_dtor
,
4822 "deleting object of abstract class type %qT"
4823 " which has non-virtual destructor"
4824 " will cause undefined behavior", type
);
4826 warning_at (loc
, OPT_Wdelete_non_virtual_dtor
,
4827 "deleting object of polymorphic class type %qT"
4828 " which has non-virtual destructor"
4829 " might cause undefined behavior", type
);
4834 /* Throw away const and volatile on target type of addr. */
4835 addr
= convert_force (build_pointer_type (type
), addr
, 0, complain
);
4839 /* Don't check PROTECT here; leave that decision to the
4840 destructor. If the destructor is accessible, call it,
4841 else report error. */
4842 addr
= cp_build_addr_expr (addr
, complain
);
4843 if (addr
== error_mark_node
)
4844 return error_mark_node
;
4846 addr
= convert_force (build_pointer_type (type
), addr
, 0, complain
);
4850 /* We will use ADDR multiple times so we must save it. */
4851 addr
= save_expr (addr
);
4853 bool virtual_p
= false;
4854 if (type_build_dtor_call (type
))
4856 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
4857 lazily_declare_fn (sfk_destructor
, type
);
4858 virtual_p
= DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type
));
4861 tree head
= NULL_TREE
;
4862 tree do_delete
= NULL_TREE
;
4863 bool destroying_delete
= false;
4867 /* Leave do_delete null. */
4869 /* For `::delete x', we must not use the deleting destructor
4870 since then we would not be sure to get the global `operator
4872 else if (use_global_delete
)
4874 head
= get_target_expr (build_headof (addr
));
4875 /* Delete the object. */
4876 do_delete
= build_op_delete_call (DELETE_EXPR
,
4878 cxx_sizeof_nowarn (type
),
4880 /*placement=*/NULL_TREE
,
4881 /*alloc_fn=*/NULL_TREE
,
4883 /* Otherwise, treat this like a complete object destructor
4885 auto_delete
= sfk_complete_destructor
;
4887 /* If the destructor is non-virtual, there is no deleting
4888 variant. Instead, we must explicitly call the appropriate
4889 `operator delete' here. */
4890 else if (!virtual_p
)
4892 /* Build the call. */
4893 do_delete
= build_op_delete_call (DELETE_EXPR
,
4895 cxx_sizeof_nowarn (type
),
4897 /*placement=*/NULL_TREE
,
4898 /*alloc_fn=*/NULL_TREE
,
4900 /* Call the complete object destructor. */
4901 auto_delete
= sfk_complete_destructor
;
4902 if (do_delete
!= error_mark_node
)
4904 tree fn
= get_callee_fndecl (do_delete
);
4905 destroying_delete
= destroying_delete_p (fn
);
4908 else if (TYPE_GETS_REG_DELETE (type
))
4910 /* Make sure we have access to the member op delete, even though
4911 we'll actually be calling it from the destructor. */
4912 build_op_delete_call (DELETE_EXPR
, addr
, cxx_sizeof_nowarn (type
),
4914 /*placement=*/NULL_TREE
,
4915 /*alloc_fn=*/NULL_TREE
,
4919 if (destroying_delete
)
4920 /* The operator delete will call the destructor. */
4922 else if (type_build_dtor_call (type
))
4923 expr
= build_dtor_call (cp_build_fold_indirect_ref (addr
),
4924 auto_delete
, flags
, complain
);
4926 expr
= build_trivial_dtor_call (addr
);
4927 if (expr
== error_mark_node
)
4928 return error_mark_node
;
4932 protected_set_expr_location (expr
, loc
);
4936 if (do_delete
== error_mark_node
)
4937 return error_mark_node
;
4939 if (do_delete
&& !TREE_SIDE_EFFECTS (expr
))
4942 /* The delete operator must be called, regardless of whether
4943 the destructor throws.
4945 [expr.delete]/7 The deallocation function is called
4946 regardless of whether the destructor for the object or some
4947 element of the array throws an exception. */
4948 expr
= build2 (TRY_FINALLY_EXPR
, void_type_node
, expr
, do_delete
);
4950 /* We need to calculate this before the dtor changes the vptr. */
4952 expr
= build2 (COMPOUND_EXPR
, void_type_node
, head
, expr
);
4954 /* Handle deleting a null pointer. */
4955 warning_sentinel
s (warn_address
);
4956 tree ifexp
= cp_build_binary_op (loc
, NE_EXPR
, addr
,
4957 nullptr_node
, complain
);
4958 ifexp
= cp_fully_fold (ifexp
);
4960 if (ifexp
== error_mark_node
)
4961 return error_mark_node
;
4962 /* This is a compiler generated comparison, don't emit
4963 e.g. -Wnonnull-compare warning for it. */
4964 else if (TREE_CODE (ifexp
) == NE_EXPR
)
4965 suppress_warning (ifexp
, OPT_Wnonnull_compare
);
4967 if (!integer_nonzerop (ifexp
))
4968 expr
= build3 (COND_EXPR
, void_type_node
, ifexp
, expr
, void_node
);
4970 protected_set_expr_location (expr
, loc
);
4974 /* At the beginning of a destructor, push cleanups that will call the
4975 destructors for our base classes and members.
4977 Called from begin_destructor_body. */
4980 push_base_cleanups (void)
4982 tree binfo
, base_binfo
;
4986 vec
<tree
, va_gc
> *vbases
;
4988 /* Run destructors for all virtual baseclasses. */
4989 if (!ABSTRACT_CLASS_TYPE_P (current_class_type
)
4990 && CLASSTYPE_VBASECLASSES (current_class_type
))
4992 tree cond
= (condition_conversion
4993 (build2 (BIT_AND_EXPR
, integer_type_node
,
4994 current_in_charge_parm
,
4995 integer_two_node
)));
4997 /* The CLASSTYPE_VBASECLASSES vector is in initialization
4998 order, which is also the right order for pushing cleanups. */
4999 for (vbases
= CLASSTYPE_VBASECLASSES (current_class_type
), i
= 0;
5000 vec_safe_iterate (vbases
, i
, &base_binfo
); i
++)
5002 if (type_build_dtor_call (BINFO_TYPE (base_binfo
)))
5004 expr
= build_special_member_call (current_class_ref
,
5005 base_dtor_identifier
,
5009 | LOOKUP_NONVIRTUAL
),
5010 tf_warning_or_error
);
5011 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
)))
5013 expr
= build3 (COND_EXPR
, void_type_node
, cond
,
5015 finish_decl_cleanup (NULL_TREE
, expr
);
5021 /* Take care of the remaining baseclasses. */
5022 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
5023 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
5025 if (BINFO_VIRTUAL_P (base_binfo
)
5026 || !type_build_dtor_call (BINFO_TYPE (base_binfo
)))
5029 expr
= build_special_member_call (current_class_ref
,
5030 base_dtor_identifier
,
5032 LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
,
5033 tf_warning_or_error
);
5034 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
)))
5035 finish_decl_cleanup (NULL_TREE
, expr
);
5038 /* Don't automatically destroy union members. */
5039 if (TREE_CODE (current_class_type
) == UNION_TYPE
)
5042 for (member
= TYPE_FIELDS (current_class_type
); member
;
5043 member
= DECL_CHAIN (member
))
5045 tree this_type
= TREE_TYPE (member
);
5046 if (this_type
== error_mark_node
5047 || TREE_CODE (member
) != FIELD_DECL
5048 || DECL_ARTIFICIAL (member
))
5050 if (ANON_AGGR_TYPE_P (this_type
))
5052 if (type_build_dtor_call (this_type
))
5054 tree this_member
= (build_class_member_access_expr
5055 (current_class_ref
, member
,
5056 /*access_path=*/NULL_TREE
,
5057 /*preserve_reference=*/false,
5058 tf_warning_or_error
));
5059 expr
= build_delete (input_location
, this_type
, this_member
,
5060 sfk_complete_destructor
,
5061 LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
|LOOKUP_NORMAL
,
5062 0, tf_warning_or_error
);
5063 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type
))
5064 finish_decl_cleanup (NULL_TREE
, expr
);
5069 /* Build a C++ vector delete expression.
5070 MAXINDEX is the number of elements to be deleted.
5071 ELT_SIZE is the nominal size of each element in the vector.
5072 BASE is the expression that should yield the store to be deleted.
5073 This function expands (or synthesizes) these calls itself.
5074 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
5076 This also calls delete for virtual baseclasses of elements of the vector.
5078 Update: MAXINDEX is no longer needed. The size can be extracted from the
5079 start of the vector for pointers, and from the type for arrays. We still
5080 use MAXINDEX for arrays because it happens to already have one of the
5081 values we'd have to extract. (We could use MAXINDEX with pointers to
5082 confirm the size, and trap if the numbers differ; not clear that it'd
5083 be worth bothering.) */
5086 build_vec_delete (location_t loc
, tree base
, tree maxindex
,
5087 special_function_kind auto_delete_vec
,
5088 int use_global_delete
, tsubst_flags_t complain
)
5092 tree base_init
= NULL_TREE
;
5094 type
= TREE_TYPE (base
);
5096 if (TYPE_PTR_P (type
))
5098 /* Step back one from start of vector, and read dimension. */
5100 tree size_ptr_type
= build_pointer_type (sizetype
);
5102 base
= mark_rvalue_use (base
);
5103 if (TREE_SIDE_EFFECTS (base
))
5105 base_init
= get_target_expr (base
);
5106 base
= TARGET_EXPR_SLOT (base_init
);
5108 type
= strip_array_types (TREE_TYPE (type
));
5109 cookie_addr
= fold_build1_loc (loc
, NEGATE_EXPR
,
5110 sizetype
, TYPE_SIZE_UNIT (sizetype
));
5111 cookie_addr
= fold_build_pointer_plus (fold_convert (size_ptr_type
, base
),
5113 maxindex
= cp_build_fold_indirect_ref (cookie_addr
);
5115 else if (TREE_CODE (type
) == ARRAY_TYPE
)
5117 /* Get the total number of things in the array, maxindex is a
5119 maxindex
= array_type_nelts_total (type
);
5120 type
= strip_array_types (type
);
5121 base
= decay_conversion (base
, complain
);
5122 if (base
== error_mark_node
)
5123 return error_mark_node
;
5124 if (TREE_SIDE_EFFECTS (base
))
5126 base_init
= get_target_expr (base
);
5127 base
= TARGET_EXPR_SLOT (base_init
);
5132 if (base
!= error_mark_node
&& !(complain
& tf_error
))
5134 "type to vector delete is neither pointer or array type");
5135 return error_mark_node
;
5138 rval
= build_vec_delete_1 (loc
, base
, maxindex
, type
, auto_delete_vec
,
5139 use_global_delete
, complain
);
5140 if (base_init
&& rval
!= error_mark_node
)
5141 rval
= build2 (COMPOUND_EXPR
, TREE_TYPE (rval
), base_init
, rval
);
5143 protected_set_expr_location (rval
, loc
);
5147 #include "gt-cp-init.h"