[aarch64] Use op_mode instead of vmode in aarch64_vectorize_vec_perm_const.
[official-gcc.git] / gcc / cp / init.cc
bloba4a0a0ac0c2fa07ebc8de3b3660445a5ff5f76c8
1 /* Handle initialization things in -*- C++ -*-
2 Copyright (C) 1987-2022 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)
10 any later version.
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. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "target.h"
27 #include "cp-tree.h"
28 #include "stringpool.h"
29 #include "varasm.h"
30 #include "gimplify.h"
31 #include "c-family/c-ubsan.h"
32 #include "intl.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "asan.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 int member_init_ok_or_else (tree, tree, tree);
45 static void expand_virtual_init (tree, tree);
46 static tree sort_mem_initializers (tree, tree);
47 static tree initializing_context (tree);
48 static void expand_cleanup_for_base (tree, tree);
49 static tree dfs_initialize_vtbl_ptrs (tree, void *);
50 static tree build_field_list (tree, tree, int *);
51 static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
53 static GTY(()) tree fn;
55 /* We are about to generate some complex initialization code.
56 Conceptually, it is all a single expression. However, we may want
57 to include conditionals, loops, and other such statement-level
58 constructs. Therefore, we build the initialization code inside a
59 statement-expression. This function starts such an expression.
60 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
61 pass them back to finish_init_stmts when the expression is
62 complete. */
64 static bool
65 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
67 bool is_global = !building_stmt_list_p ();
69 *stmt_expr_p = begin_stmt_expr ();
70 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
72 return is_global;
75 /* Finish out the statement-expression begun by the previous call to
76 begin_init_stmts. Returns the statement-expression itself. */
78 static tree
79 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
81 finish_compound_stmt (compound_stmt);
83 stmt_expr = finish_stmt_expr (stmt_expr, true);
85 gcc_assert (!building_stmt_list_p () == is_global);
87 return stmt_expr;
90 /* Constructors */
92 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
93 which we want to initialize the vtable pointer for, DATA is
94 TREE_LIST whose TREE_VALUE is the this ptr expression. */
96 static tree
97 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
99 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
100 return dfs_skip_bases;
102 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
104 tree base_ptr = TREE_VALUE ((tree) data);
106 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
107 tf_warning_or_error);
109 expand_virtual_init (binfo, base_ptr);
112 return NULL_TREE;
115 /* Initialize all the vtable pointers in the object pointed to by
116 ADDR. */
118 void
119 initialize_vtbl_ptrs (tree addr)
121 tree list;
122 tree type;
124 type = TREE_TYPE (TREE_TYPE (addr));
125 list = build_tree_list (type, addr);
127 /* Walk through the hierarchy, initializing the vptr in each base
128 class. We do these in pre-order because we can't find the virtual
129 bases for a class until we've initialized the vtbl for that
130 class. */
131 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
134 /* Return an expression for the zero-initialization of an object with
135 type T. This expression will either be a constant (in the case
136 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
137 aggregate), or NULL (in the case that T does not require
138 initialization). In either case, the value can be used as
139 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
140 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
141 is the number of elements in the array. If STATIC_STORAGE_P is
142 TRUE, initializers are only generated for entities for which
143 zero-initialization does not simply mean filling the storage with
144 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
145 subfields with bit positions at or above that bit size shouldn't
146 be added. Note that this only works when the result is assigned
147 to a base COMPONENT_REF; if we only have a pointer to the base subobject,
148 expand_assignment will end up clearing the full size of TYPE. */
150 static tree
151 build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
152 tree field_size)
154 tree init = NULL_TREE;
156 /* [dcl.init]
158 To zero-initialize an object of type T means:
160 -- if T is a scalar type, the storage is set to the value of zero
161 converted to T.
163 -- if T is a non-union class type, the storage for each non-static
164 data member and each base-class subobject is zero-initialized.
166 -- if T is a union type, the storage for its first data member is
167 zero-initialized.
169 -- if T is an array type, the storage for each element is
170 zero-initialized.
172 -- if T is a reference type, no initialization is performed. */
174 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
176 if (type == error_mark_node)
178 else if (static_storage_p && zero_init_p (type))
179 /* In order to save space, we do not explicitly build initializers
180 for items that do not need them. GCC's semantics are that
181 items with static storage duration that are not otherwise
182 initialized are initialized to zero. */
184 else if (TYPE_PTR_OR_PTRMEM_P (type))
185 init = fold (convert (type, nullptr_node));
186 else if (NULLPTR_TYPE_P (type))
187 init = build_int_cst (type, 0);
188 else if (SCALAR_TYPE_P (type))
189 init = build_zero_cst (type);
190 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
192 tree field;
193 vec<constructor_elt, va_gc> *v = NULL;
195 /* Iterate over the fields, building initializations. */
196 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
198 if (TREE_CODE (field) != FIELD_DECL)
199 continue;
201 if (TREE_TYPE (field) == error_mark_node)
202 continue;
204 /* Don't add virtual bases for base classes if they are beyond
205 the size of the current field, that means it is present
206 somewhere else in the object. */
207 if (field_size)
209 tree bitpos = bit_position (field);
210 if (TREE_CODE (bitpos) == INTEGER_CST
211 && !tree_int_cst_lt (bitpos, field_size))
212 continue;
215 /* Note that for class types there will be FIELD_DECLs
216 corresponding to base classes as well. Thus, iterating
217 over TYPE_FIELDs will result in correct initialization of
218 all of the subobjects. */
219 if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
221 tree new_field_size
222 = (DECL_FIELD_IS_BASE (field)
223 && DECL_SIZE (field)
224 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
225 ? DECL_SIZE (field) : NULL_TREE;
226 tree value = build_zero_init_1 (TREE_TYPE (field),
227 /*nelts=*/NULL_TREE,
228 static_storage_p,
229 new_field_size);
230 if (value)
231 CONSTRUCTOR_APPEND_ELT(v, field, value);
234 /* For unions, only the first field is initialized. */
235 if (TREE_CODE (type) == UNION_TYPE)
236 break;
239 /* Build a constructor to contain the initializations. */
240 init = build_constructor (type, v);
242 else if (TREE_CODE (type) == ARRAY_TYPE)
244 tree max_index;
245 vec<constructor_elt, va_gc> *v = NULL;
247 /* Iterate over the array elements, building initializations. */
248 if (nelts)
249 max_index = fold_build2_loc (input_location, MINUS_EXPR,
250 TREE_TYPE (nelts), nelts,
251 build_one_cst (TREE_TYPE (nelts)));
252 /* Treat flexible array members like [0] arrays. */
253 else if (TYPE_DOMAIN (type) == NULL_TREE)
254 return NULL_TREE;
255 else
256 max_index = array_type_nelts (type);
258 /* If we have an error_mark here, we should just return error mark
259 as we don't know the size of the array yet. */
260 if (max_index == error_mark_node)
261 return error_mark_node;
262 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
264 /* A zero-sized array, which is accepted as an extension, will
265 have an upper bound of -1. */
266 if (!integer_minus_onep (max_index))
268 constructor_elt ce;
270 /* If this is a one element array, we just use a regular init. */
271 if (integer_zerop (max_index))
272 ce.index = size_zero_node;
273 else
274 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
275 max_index);
277 ce.value = build_zero_init_1 (TREE_TYPE (type), /*nelts=*/NULL_TREE,
278 static_storage_p, NULL_TREE);
279 if (ce.value)
281 vec_alloc (v, 1);
282 v->quick_push (ce);
286 /* Build a constructor to contain the initializations. */
287 init = build_constructor (type, v);
289 else if (VECTOR_TYPE_P (type))
290 init = build_zero_cst (type);
291 else
292 gcc_assert (TYPE_REF_P (type));
294 /* In all cases, the initializer is a constant. */
295 if (init)
296 TREE_CONSTANT (init) = 1;
298 return init;
301 /* Return an expression for the zero-initialization of an object with
302 type T. This expression will either be a constant (in the case
303 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
304 aggregate), or NULL (in the case that T does not require
305 initialization). In either case, the value can be used as
306 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
307 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
308 is the number of elements in the array. If STATIC_STORAGE_P is
309 TRUE, initializers are only generated for entities for which
310 zero-initialization does not simply mean filling the storage with
311 zero bytes. */
313 tree
314 build_zero_init (tree type, tree nelts, bool static_storage_p)
316 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
319 /* Return a suitable initializer for value-initializing an object of type
320 TYPE, as described in [dcl.init]. */
322 tree
323 build_value_init (tree type, tsubst_flags_t complain)
325 /* [dcl.init]
327 To value-initialize an object of type T means:
329 - if T is a class type (clause 9) with either no default constructor
330 (12.1) or a default constructor that is user-provided or deleted,
331 then the object is default-initialized;
333 - if T is a (possibly cv-qualified) class type without a user-provided
334 or deleted default constructor, then the object is zero-initialized
335 and the semantic constraints for default-initialization are checked,
336 and if T has a non-trivial default constructor, the object is
337 default-initialized;
339 - if T is an array type, then each element is value-initialized;
341 - otherwise, the object is zero-initialized.
343 A program that calls for default-initialization or
344 value-initialization of an entity of reference type is ill-formed. */
346 if (CLASS_TYPE_P (type) && type_build_ctor_call (type))
348 tree ctor
349 = build_special_member_call (NULL_TREE, complete_ctor_identifier,
350 NULL, type, LOOKUP_NORMAL, complain);
351 if (ctor == error_mark_node || TREE_CONSTANT (ctor))
352 return ctor;
353 if (processing_template_decl)
354 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
355 return build_min (CAST_EXPR, type, NULL_TREE);
356 tree fn = NULL_TREE;
357 if (TREE_CODE (ctor) == CALL_EXPR)
358 fn = get_callee_fndecl (ctor);
359 ctor = build_aggr_init_expr (type, ctor);
360 if (fn && user_provided_p (fn))
361 return ctor;
362 else if (TYPE_HAS_COMPLEX_DFLT (type))
364 /* This is a class that needs constructing, but doesn't have
365 a user-provided constructor. So we need to zero-initialize
366 the object and then call the implicitly defined ctor.
367 This will be handled in simplify_aggr_init_expr. */
368 AGGR_INIT_ZERO_FIRST (ctor) = 1;
369 return ctor;
373 /* Discard any access checking during subobject initialization;
374 the checks are implied by the call to the ctor which we have
375 verified is OK (cpp0x/defaulted46.C). */
376 push_deferring_access_checks (dk_deferred);
377 tree r = build_value_init_noctor (type, complain);
378 pop_deferring_access_checks ();
379 return r;
382 /* Like build_value_init, but don't call the constructor for TYPE. Used
383 for base initializers. */
385 tree
386 build_value_init_noctor (tree type, tsubst_flags_t complain)
388 if (!COMPLETE_TYPE_P (type))
390 if (complain & tf_error)
391 error ("value-initialization of incomplete type %qT", type);
392 return error_mark_node;
394 /* FIXME the class and array cases should just use digest_init once it is
395 SFINAE-enabled. */
396 if (CLASS_TYPE_P (type))
398 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type)
399 || errorcount != 0);
401 if (TREE_CODE (type) != UNION_TYPE)
403 tree field;
404 vec<constructor_elt, va_gc> *v = NULL;
406 /* Iterate over the fields, building initializations. */
407 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
409 tree ftype, value;
411 if (TREE_CODE (field) != FIELD_DECL)
412 continue;
414 ftype = TREE_TYPE (field);
416 if (ftype == error_mark_node)
417 continue;
419 /* Ignore flexible array members for value initialization. */
420 if (TREE_CODE (ftype) == ARRAY_TYPE
421 && !COMPLETE_TYPE_P (ftype)
422 && !TYPE_DOMAIN (ftype)
423 && COMPLETE_TYPE_P (TREE_TYPE (ftype))
424 && (next_aggregate_field (DECL_CHAIN (field))
425 == NULL_TREE))
426 continue;
428 /* Ignore unnamed zero-width bitfields. */
429 if (DECL_UNNAMED_BIT_FIELD (field)
430 && integer_zerop (DECL_SIZE (field)))
431 continue;
433 /* We could skip vfields and fields of types with
434 user-defined constructors, but I think that won't improve
435 performance at all; it should be simpler in general just
436 to zero out the entire object than try to only zero the
437 bits that actually need it. */
439 /* Note that for class types there will be FIELD_DECLs
440 corresponding to base classes as well. Thus, iterating
441 over TYPE_FIELDs will result in correct initialization of
442 all of the subobjects. */
443 value = build_value_init (ftype, complain);
444 value = maybe_constant_init (value);
446 if (value == error_mark_node)
447 return error_mark_node;
449 CONSTRUCTOR_APPEND_ELT(v, field, value);
451 /* We shouldn't have gotten here for anything that would need
452 non-trivial initialization, and gimplify_init_ctor_preeval
453 would need to be fixed to allow it. */
454 gcc_assert (TREE_CODE (value) != TARGET_EXPR
455 && TREE_CODE (value) != AGGR_INIT_EXPR);
458 /* Build a constructor to contain the zero- initializations. */
459 return build_constructor (type, v);
462 else if (TREE_CODE (type) == ARRAY_TYPE)
464 vec<constructor_elt, va_gc> *v = NULL;
466 /* Iterate over the array elements, building initializations. */
467 tree max_index = array_type_nelts (type);
469 /* If we have an error_mark here, we should just return error mark
470 as we don't know the size of the array yet. */
471 if (max_index == error_mark_node)
473 if (complain & tf_error)
474 error ("cannot value-initialize array of unknown bound %qT",
475 type);
476 return error_mark_node;
478 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
480 /* A zero-sized array, which is accepted as an extension, will
481 have an upper bound of -1. */
482 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
484 constructor_elt ce;
486 /* If this is a one element array, we just use a regular init. */
487 if (tree_int_cst_equal (size_zero_node, max_index))
488 ce.index = size_zero_node;
489 else
490 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
492 ce.value = build_value_init (TREE_TYPE (type), complain);
493 ce.value = maybe_constant_init (ce.value);
494 if (ce.value == error_mark_node)
495 return error_mark_node;
497 vec_alloc (v, 1);
498 v->quick_push (ce);
500 /* We shouldn't have gotten here for anything that would need
501 non-trivial initialization, and gimplify_init_ctor_preeval
502 would need to be fixed to allow it. */
503 gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
504 && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
507 /* Build a constructor to contain the initializations. */
508 return build_constructor (type, v);
510 else if (TREE_CODE (type) == FUNCTION_TYPE)
512 if (complain & tf_error)
513 error ("value-initialization of function type %qT", type);
514 return error_mark_node;
516 else if (TYPE_REF_P (type))
518 if (complain & tf_error)
519 error ("value-initialization of reference type %qT", type);
520 return error_mark_node;
523 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
526 /* Initialize current class with INIT, a TREE_LIST of arguments for
527 a target constructor. If TREE_LIST is void_type_node, an empty
528 initializer list was given. Return the target constructor. */
530 static tree
531 perform_target_ctor (tree init)
533 tree decl = current_class_ref;
534 tree type = current_class_type;
536 init = build_aggr_init (decl, init, LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
537 tf_warning_or_error);
538 finish_expr_stmt (init);
539 if (type_build_dtor_call (type))
541 tree expr = build_delete (input_location,
542 type, decl, sfk_complete_destructor,
543 LOOKUP_NORMAL
544 |LOOKUP_NONVIRTUAL
545 |LOOKUP_DESTRUCTOR,
546 0, tf_warning_or_error);
547 if (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
549 tree base = build_delete (input_location,
550 type, decl, sfk_base_destructor,
551 LOOKUP_NORMAL
552 |LOOKUP_NONVIRTUAL
553 |LOOKUP_DESTRUCTOR,
554 0, tf_warning_or_error);
555 expr = build_if_in_charge (expr, base);
557 if (expr != error_mark_node
558 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
559 finish_eh_cleanup (expr);
561 return init;
564 /* Return the non-static data initializer for FIELD_DECL MEMBER. */
566 static GTY((cache)) decl_tree_cache_map *nsdmi_inst;
568 tree
569 get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
571 tree init;
572 tree save_ccp = current_class_ptr;
573 tree save_ccr = current_class_ref;
575 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
577 init = DECL_INITIAL (DECL_TI_TEMPLATE (member));
578 location_t expr_loc
579 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member));
580 if (TREE_CODE (init) == DEFERRED_PARSE)
581 /* Unparsed. */;
582 else if (tree *slot = hash_map_safe_get (nsdmi_inst, member))
583 init = *slot;
584 /* Check recursive instantiation. */
585 else if (DECL_INSTANTIATING_NSDMI_P (member))
587 if (complain & tf_error)
588 error_at (expr_loc, "recursive instantiation of default member "
589 "initializer for %qD", member);
590 init = error_mark_node;
592 else
594 cp_evaluated ev;
596 location_t sloc = input_location;
597 input_location = expr_loc;
599 DECL_INSTANTIATING_NSDMI_P (member) = 1;
601 bool pushed = false;
602 tree ctx = DECL_CONTEXT (member);
604 processing_template_decl_sentinel ptds (/*reset*/false);
605 if (!currently_open_class (ctx))
607 if (!LOCAL_CLASS_P (ctx))
608 push_to_top_level ();
609 else
610 /* push_to_top_level would lose the necessary function context,
611 just reset processing_template_decl. */
612 processing_template_decl = 0;
613 push_nested_class (ctx);
614 push_deferring_access_checks (dk_no_deferred);
615 pushed = true;
618 inject_this_parameter (ctx, TYPE_UNQUALIFIED);
620 start_lambda_scope (member);
622 /* Do deferred instantiation of the NSDMI. */
623 init = (tsubst_copy_and_build
624 (init, DECL_TI_ARGS (member),
625 complain, member, /*function_p=*/false,
626 /*integral_constant_expression_p=*/false));
627 init = digest_nsdmi_init (member, init, complain);
629 finish_lambda_scope ();
631 DECL_INSTANTIATING_NSDMI_P (member) = 0;
633 if (init != error_mark_node)
634 hash_map_safe_put<hm_ggc> (nsdmi_inst, member, init);
636 if (pushed)
638 pop_deferring_access_checks ();
639 pop_nested_class ();
640 if (!LOCAL_CLASS_P (ctx))
641 pop_from_top_level ();
644 input_location = sloc;
647 else
648 init = DECL_INITIAL (member);
650 if (init && TREE_CODE (init) == DEFERRED_PARSE)
652 if (complain & tf_error)
654 error ("default member initializer for %qD required before the end "
655 "of its enclosing class", member);
656 inform (location_of (init), "defined here");
657 DECL_INITIAL (member) = error_mark_node;
659 init = error_mark_node;
662 if (in_ctor)
664 current_class_ptr = save_ccp;
665 current_class_ref = save_ccr;
667 else
669 /* Use a PLACEHOLDER_EXPR when we don't have a 'this' parameter to
670 refer to; constexpr evaluation knows what to do with it. */
671 current_class_ref = build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (member));
672 current_class_ptr = build_address (current_class_ref);
675 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
676 so the aggregate init code below will see a CONSTRUCTOR. */
677 bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init));
678 if (simple_target)
679 init = TARGET_EXPR_INITIAL (init);
680 init = break_out_target_exprs (init, /*loc*/true);
681 if (init && TREE_CODE (init) == TARGET_EXPR)
682 /* In a constructor, this expresses the full initialization, prevent
683 perform_member_init from calling another constructor (58162). */
684 TARGET_EXPR_DIRECT_INIT_P (init) = in_ctor;
685 if (simple_target && TREE_CODE (init) != CONSTRUCTOR)
686 /* Now put it back so C++17 copy elision works. */
687 init = get_target_expr (init);
689 current_class_ptr = save_ccp;
690 current_class_ref = save_ccr;
691 return init;
694 /* Diagnose the flexible array MEMBER if its INITializer is non-null
695 and return true if so. Otherwise return false. */
697 bool
698 maybe_reject_flexarray_init (tree member, tree init)
700 tree type = TREE_TYPE (member);
702 if (!init
703 || TREE_CODE (type) != ARRAY_TYPE
704 || TYPE_DOMAIN (type))
705 return false;
707 /* Point at the flexible array member declaration if it's initialized
708 in-class, and at the ctor if it's initialized in a ctor member
709 initializer list. */
710 location_t loc;
711 if (DECL_INITIAL (member) == init
712 || !current_function_decl
713 || DECL_DEFAULTED_FN (current_function_decl))
714 loc = DECL_SOURCE_LOCATION (member);
715 else
716 loc = DECL_SOURCE_LOCATION (current_function_decl);
718 error_at (loc, "initializer for flexible array member %q#D", member);
719 return true;
722 /* If INIT's value can come from a call to std::initializer_list<T>::begin,
723 return that function. Otherwise, NULL_TREE. */
725 static tree
726 find_list_begin (tree init)
728 STRIP_NOPS (init);
729 while (TREE_CODE (init) == COMPOUND_EXPR)
730 init = TREE_OPERAND (init, 1);
731 STRIP_NOPS (init);
732 if (TREE_CODE (init) == COND_EXPR)
734 tree left = TREE_OPERAND (init, 1);
735 if (!left)
736 left = TREE_OPERAND (init, 0);
737 left = find_list_begin (left);
738 if (left)
739 return left;
740 return find_list_begin (TREE_OPERAND (init, 2));
742 if (TREE_CODE (init) == CALL_EXPR)
743 if (tree fn = get_callee_fndecl (init))
744 if (id_equal (DECL_NAME (fn), "begin")
745 && is_std_init_list (DECL_CONTEXT (fn)))
746 return fn;
747 return NULL_TREE;
750 /* If INIT initializing MEMBER is copying the address of the underlying array
751 of an initializer_list, warn. */
753 static void
754 maybe_warn_list_ctor (tree member, tree init)
756 tree memtype = TREE_TYPE (member);
757 if (!init || !TYPE_PTR_P (memtype)
758 || !is_list_ctor (current_function_decl))
759 return;
761 tree parm = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl);
762 parm = TREE_VALUE (parm);
763 tree initlist = non_reference (parm);
765 /* Do not warn if the parameter is an lvalue reference to non-const. */
766 if (TYPE_REF_P (parm) && !TYPE_REF_IS_RVALUE (parm)
767 && !CP_TYPE_CONST_P (initlist))
768 return;
770 tree targs = CLASSTYPE_TI_ARGS (initlist);
771 tree elttype = TREE_VEC_ELT (targs, 0);
773 if (!same_type_ignoring_top_level_qualifiers_p
774 (TREE_TYPE (memtype), elttype))
775 return;
777 tree begin = find_list_begin (init);
778 if (!begin)
779 return;
781 location_t loc = cp_expr_loc_or_input_loc (init);
782 warning_at (loc, OPT_Winit_list_lifetime,
783 "initializing %qD from %qE does not extend the lifetime "
784 "of the underlying array", member, begin);
787 /* Data structure for find_uninit_fields_r, below. */
789 struct find_uninit_data {
790 /* The set tracking the yet-uninitialized members. */
791 hash_set<tree> *uninitialized;
792 /* The data member we are currently initializing. It can be either
793 a type (initializing a base class/delegating constructors), or
794 a COMPONENT_REF. */
795 tree member;
798 /* walk_tree callback that warns about using uninitialized data in
799 a member-initializer-list. */
801 static tree
802 find_uninit_fields_r (tree *tp, int *walk_subtrees, void *data)
804 find_uninit_data *d = static_cast<find_uninit_data *>(data);
805 hash_set<tree> *uninitialized = d->uninitialized;
806 tree init = *tp;
807 const tree_code code = TREE_CODE (init);
809 /* No need to look into types or unevaluated operands. */
810 if (TYPE_P (init) || unevaluated_p (code))
812 *walk_subtrees = false;
813 return NULL_TREE;
816 switch (code)
818 /* We'd need data flow info to avoid false positives. */
819 case COND_EXPR:
820 case VEC_COND_EXPR:
821 case BIND_EXPR:
822 /* We might see a MODIFY_EXPR in cases like S() : a((b = 42)), c(b) { }
823 where the initializer for 'a' surreptitiously initializes 'b'. Let's
824 not bother with these complicated scenarios in the front end. */
825 case MODIFY_EXPR:
826 /* Don't attempt to handle statement-expressions, either. */
827 case STATEMENT_LIST:
828 uninitialized->empty ();
829 gcc_fallthrough ();
830 /* If we're just taking the address of an object, it doesn't matter
831 whether it's been initialized. */
832 case ADDR_EXPR:
833 *walk_subtrees = false;
834 return NULL_TREE;
835 default:
836 break;
839 /* We'd need data flow info to avoid false positives. */
840 if (truth_value_p (code))
841 goto give_up;
842 /* Attempt to handle a simple a{b}, but no more. */
843 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
845 if (CONSTRUCTOR_NELTS (init) == 1
846 && !BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (init, 0)->value))
847 init = CONSTRUCTOR_ELT (init, 0)->value;
848 else
849 goto give_up;
851 /* Warn about uninitialized 'this'. */
852 else if (code == CALL_EXPR)
854 tree fn = get_callee_fndecl (init);
855 if (fn && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
857 tree op = CALL_EXPR_ARG (init, 0);
858 if (TREE_CODE (op) == ADDR_EXPR)
859 op = TREE_OPERAND (op, 0);
860 temp_override<tree> ovr (d->member, DECL_ARGUMENTS (fn));
861 cp_walk_tree_without_duplicates (&op, find_uninit_fields_r, data);
863 /* Functions (whether static or nonstatic member) may have side effects
864 and initialize other members; it's not the front end's job to try to
865 figure it out. But don't give up for constructors: we still want to
866 warn when initializing base classes:
868 struct D : public B {
869 int x;
870 D() : B(x) {}
873 so carry on to detect that 'x' is used uninitialized. */
874 if (!fn || !DECL_CONSTRUCTOR_P (fn))
875 goto give_up;
878 /* If we find FIELD in the uninitialized set, we warn. */
879 if (code == COMPONENT_REF)
881 tree field = TREE_OPERAND (init, 1);
882 tree type = TYPE_P (d->member) ? d->member : TREE_TYPE (d->member);
884 /* We're initializing a reference member with itself. */
885 if (TYPE_REF_P (type) && cp_tree_equal (d->member, init))
886 warning_at (EXPR_LOCATION (init), OPT_Winit_self,
887 "%qD is initialized with itself", field);
888 else if (cp_tree_equal (TREE_OPERAND (init, 0), current_class_ref)
889 && uninitialized->contains (field))
891 if (TYPE_REF_P (TREE_TYPE (field)))
892 warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
893 "reference %qD is not yet bound to a value when used "
894 "here", field);
895 else if (!INDIRECT_TYPE_P (type) || is_this_parameter (d->member))
896 warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
897 "member %qD is used uninitialized", field);
898 *walk_subtrees = false;
902 return NULL_TREE;
904 give_up:
905 *walk_subtrees = false;
906 uninitialized->empty ();
907 return integer_zero_node;
910 /* Wrapper around find_uninit_fields_r above. */
912 static void
913 find_uninit_fields (tree *t, hash_set<tree> *uninitialized, tree member)
915 if (!uninitialized->is_empty ())
917 find_uninit_data data = { uninitialized, member };
918 cp_walk_tree_without_duplicates (t, find_uninit_fields_r, &data);
922 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
923 arguments. If TREE_LIST is void_type_node, an empty initializer
924 list was given; if NULL_TREE no initializer was given. UNINITIALIZED
925 is the hash set that tracks uninitialized fields. */
927 static void
928 perform_member_init (tree member, tree init, hash_set<tree> &uninitialized)
930 tree decl;
931 tree type = TREE_TYPE (member);
933 /* Use the non-static data member initializer if there was no
934 mem-initializer for this field. */
935 if (init == NULL_TREE)
936 init = get_nsdmi (member, /*ctor*/true, tf_warning_or_error);
938 if (init == error_mark_node)
939 return;
941 /* Effective C++ rule 12 requires that all data members be
942 initialized. */
943 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
944 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
945 "%qD should be initialized in the member initialization list",
946 member);
948 /* Get an lvalue for the data member. */
949 decl = build_class_member_access_expr (current_class_ref, member,
950 /*access_path=*/NULL_TREE,
951 /*preserve_reference=*/true,
952 tf_warning_or_error);
953 if (decl == error_mark_node)
954 return;
956 if ((warn_init_self || warn_uninitialized)
957 && init
958 && TREE_CODE (init) == TREE_LIST
959 && TREE_CHAIN (init) == NULL_TREE)
961 tree val = TREE_VALUE (init);
962 /* Handle references. */
963 if (REFERENCE_REF_P (val))
964 val = TREE_OPERAND (val, 0);
965 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
966 && TREE_OPERAND (val, 0) == current_class_ref)
967 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
968 OPT_Winit_self, "%qD is initialized with itself",
969 member);
970 else
971 find_uninit_fields (&val, &uninitialized, decl);
974 if (array_of_unknown_bound_p (type))
976 maybe_reject_flexarray_init (member, init);
977 return;
980 if (init && TREE_CODE (init) == TREE_LIST)
982 /* A(): a{e} */
983 if (DIRECT_LIST_INIT_P (TREE_VALUE (init)))
984 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
985 tf_warning_or_error);
986 /* We are trying to initialize an array from a ()-list. If we
987 should attempt to do so, conjure up a CONSTRUCTOR. */
988 else if (TREE_CODE (type) == ARRAY_TYPE
989 /* P0960 is a C++20 feature. */
990 && cxx_dialect >= cxx20)
991 init = do_aggregate_paren_init (init, type);
992 else if (!CLASS_TYPE_P (type))
993 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
994 tf_warning_or_error);
995 /* If we're initializing a class from a ()-list, leave the TREE_LIST
996 alone: we might call an appropriate constructor, or (in C++20)
997 do aggregate-initialization. */
1000 /* Assume we are initializing the member. */
1001 bool member_initialized_p = true;
1003 if (init == void_type_node)
1005 /* mem() means value-initialization. */
1006 if (TREE_CODE (type) == ARRAY_TYPE)
1008 init = build_vec_init_expr (type, init, tf_warning_or_error);
1009 init = build2 (INIT_EXPR, type, decl, init);
1010 finish_expr_stmt (init);
1012 else
1014 tree value = build_value_init (type, tf_warning_or_error);
1015 if (value == error_mark_node)
1016 return;
1017 init = build2 (INIT_EXPR, type, decl, value);
1018 finish_expr_stmt (init);
1021 /* Deal with this here, as we will get confused if we try to call the
1022 assignment op for an anonymous union. This can happen in a
1023 synthesized copy constructor. */
1024 else if (ANON_AGGR_TYPE_P (type))
1026 if (init)
1028 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
1029 finish_expr_stmt (init);
1032 else if (init
1033 && (TYPE_REF_P (type)
1034 || (TREE_CODE (init) == CONSTRUCTOR
1035 && (CP_AGGREGATE_TYPE_P (type)
1036 || is_std_init_list (type)))))
1038 /* With references and list-initialization, we need to deal with
1039 extending temporary lifetimes. 12.2p5: "A temporary bound to a
1040 reference member in a constructor’s ctor-initializer (12.6.2)
1041 persists until the constructor exits." */
1042 unsigned i; tree t;
1043 releasing_vec cleanups;
1044 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1046 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1047 && CP_AGGREGATE_TYPE_P (type))
1048 init = reshape_init (type, init, tf_warning_or_error);
1049 init = digest_init (type, init, tf_warning_or_error);
1051 if (init == error_mark_node)
1052 return;
1053 if (is_empty_field (member)
1054 && !TREE_SIDE_EFFECTS (init))
1055 /* Don't add trivial initialization of an empty base/field, as they
1056 might not be ordered the way the back-end expects. */
1057 return;
1058 /* A FIELD_DECL doesn't really have a suitable lifetime, but
1059 make_temporary_var_for_ref_to_temp will treat it as automatic and
1060 set_up_extended_ref_temp wants to use the decl in a warning. */
1061 init = extend_ref_init_temps (member, init, &cleanups);
1062 if (TREE_CODE (type) == ARRAY_TYPE
1063 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
1064 init = build_vec_init_expr (type, init, tf_warning_or_error);
1065 init = build2 (INIT_EXPR, type, decl, init);
1066 finish_expr_stmt (init);
1067 FOR_EACH_VEC_ELT (*cleanups, i, t)
1068 push_cleanup (NULL_TREE, t, false);
1070 else if (type_build_ctor_call (type)
1071 || (init && CLASS_TYPE_P (strip_array_types (type))))
1073 if (TREE_CODE (type) == ARRAY_TYPE)
1075 if (init == NULL_TREE
1076 || same_type_ignoring_top_level_qualifiers_p (type,
1077 TREE_TYPE (init)))
1079 if (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1081 /* Initialize the array only if it's not a flexible
1082 array member (i.e., if it has an upper bound). */
1083 init = build_vec_init_expr (type, init, tf_warning_or_error);
1084 init = build2 (INIT_EXPR, type, decl, init);
1085 finish_expr_stmt (init);
1088 else
1089 error ("invalid initializer for array member %q#D", member);
1091 else
1093 int flags = LOOKUP_NORMAL;
1094 if (DECL_DEFAULTED_FN (current_function_decl))
1095 flags |= LOOKUP_DEFAULTED;
1096 if (CP_TYPE_CONST_P (type)
1097 && init == NULL_TREE
1098 && default_init_uninitialized_part (type))
1100 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
1101 vtable; still give this diagnostic. */
1102 auto_diagnostic_group d;
1103 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1104 "uninitialized const member in %q#T", type))
1105 inform (DECL_SOURCE_LOCATION (member),
1106 "%q#D should be initialized", member );
1108 finish_expr_stmt (build_aggr_init (decl, init, flags,
1109 tf_warning_or_error));
1112 else
1114 if (init == NULL_TREE)
1116 tree core_type;
1117 /* member traversal: note it leaves init NULL */
1118 if (TYPE_REF_P (type))
1120 auto_diagnostic_group d;
1121 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1122 "uninitialized reference member in %q#T", type))
1123 inform (DECL_SOURCE_LOCATION (member),
1124 "%q#D should be initialized", member);
1126 else if (CP_TYPE_CONST_P (type))
1128 auto_diagnostic_group d;
1129 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1130 "uninitialized const member in %q#T", type))
1131 inform (DECL_SOURCE_LOCATION (member),
1132 "%q#D should be initialized", member );
1135 core_type = strip_array_types (type);
1137 if (CLASS_TYPE_P (core_type)
1138 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
1139 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
1140 diagnose_uninitialized_cst_or_ref_member (core_type,
1141 /*using_new=*/false,
1142 /*complain=*/true);
1144 /* We left the member uninitialized. */
1145 member_initialized_p = false;
1148 maybe_warn_list_ctor (member, init);
1150 if (init)
1151 finish_expr_stmt (cp_build_modify_expr (input_location, decl,
1152 INIT_EXPR, init,
1153 tf_warning_or_error));
1156 if (member_initialized_p && warn_uninitialized)
1157 /* This member is now initialized, remove it from the uninitialized
1158 set. */
1159 uninitialized.remove (member);
1161 if (type_build_dtor_call (type))
1163 tree expr;
1165 expr = build_class_member_access_expr (current_class_ref, member,
1166 /*access_path=*/NULL_TREE,
1167 /*preserve_reference=*/false,
1168 tf_warning_or_error);
1169 expr = build_delete (input_location,
1170 type, expr, sfk_complete_destructor,
1171 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
1172 tf_warning_or_error);
1174 if (expr != error_mark_node
1175 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1176 finish_eh_cleanup (expr);
1180 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
1181 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
1183 static tree
1184 build_field_list (tree t, tree list, int *uses_unions_or_anon_p)
1186 tree fields;
1188 /* Note whether or not T is a union. */
1189 if (TREE_CODE (t) == UNION_TYPE)
1190 *uses_unions_or_anon_p = 1;
1192 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
1194 tree fieldtype;
1196 /* Skip CONST_DECLs for enumeration constants and so forth. */
1197 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
1198 continue;
1200 fieldtype = TREE_TYPE (fields);
1202 /* For an anonymous struct or union, we must recursively
1203 consider the fields of the anonymous type. They can be
1204 directly initialized from the constructor. */
1205 if (ANON_AGGR_TYPE_P (fieldtype))
1207 /* Add this field itself. Synthesized copy constructors
1208 initialize the entire aggregate. */
1209 list = tree_cons (fields, NULL_TREE, list);
1210 /* And now add the fields in the anonymous aggregate. */
1211 list = build_field_list (fieldtype, list, uses_unions_or_anon_p);
1212 *uses_unions_or_anon_p = 1;
1214 /* Add this field. */
1215 else if (DECL_NAME (fields))
1216 list = tree_cons (fields, NULL_TREE, list);
1219 return list;
1222 /* Return the innermost aggregate scope for FIELD, whether that is
1223 the enclosing class or an anonymous aggregate within it. */
1225 static tree
1226 innermost_aggr_scope (tree field)
1228 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1229 return TREE_TYPE (field);
1230 else
1231 return DECL_CONTEXT (field);
1234 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
1235 a FIELD_DECL or BINFO in T that needs initialization. The
1236 TREE_VALUE gives the initializer, or list of initializer arguments.
1238 Return a TREE_LIST containing all of the initializations required
1239 for T, in the order in which they should be performed. The output
1240 list has the same format as the input. */
1242 static tree
1243 sort_mem_initializers (tree t, tree mem_inits)
1245 tree init;
1246 tree base, binfo, base_binfo;
1247 tree sorted_inits;
1248 tree next_subobject;
1249 vec<tree, va_gc> *vbases;
1250 int i;
1251 int uses_unions_or_anon_p = 0;
1253 /* Build up a list of initializations. The TREE_PURPOSE of entry
1254 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
1255 TREE_VALUE will be the constructor arguments, or NULL if no
1256 explicit initialization was provided. */
1257 sorted_inits = NULL_TREE;
1259 /* Process the virtual bases. */
1260 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
1261 vec_safe_iterate (vbases, i, &base); i++)
1262 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
1264 /* Process the direct bases. */
1265 for (binfo = TYPE_BINFO (t), i = 0;
1266 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1267 if (!BINFO_VIRTUAL_P (base_binfo))
1268 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
1270 /* Process the non-static data members. */
1271 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_or_anon_p);
1272 /* Reverse the entire list of initializations, so that they are in
1273 the order that they will actually be performed. */
1274 sorted_inits = nreverse (sorted_inits);
1276 /* If the user presented the initializers in an order different from
1277 that in which they will actually occur, we issue a warning. Keep
1278 track of the next subobject which can be explicitly initialized
1279 without issuing a warning. */
1280 next_subobject = sorted_inits;
1282 /* Go through the explicit initializers, filling in TREE_PURPOSE in
1283 the SORTED_INITS. */
1284 for (init = mem_inits; init; init = TREE_CHAIN (init))
1286 tree subobject;
1287 tree subobject_init;
1289 subobject = TREE_PURPOSE (init);
1291 /* If the explicit initializers are in sorted order, then
1292 SUBOBJECT will be NEXT_SUBOBJECT, or something following
1293 it. */
1294 for (subobject_init = next_subobject;
1295 subobject_init;
1296 subobject_init = TREE_CHAIN (subobject_init))
1297 if (TREE_PURPOSE (subobject_init) == subobject)
1298 break;
1300 /* Issue a warning if the explicit initializer order does not
1301 match that which will actually occur.
1302 ??? Are all these on the correct lines? */
1303 if (warn_reorder && !subobject_init)
1305 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
1306 warning_at (DECL_SOURCE_LOCATION (TREE_PURPOSE (next_subobject)),
1307 OPT_Wreorder, "%qD will be initialized after",
1308 TREE_PURPOSE (next_subobject));
1309 else
1310 warning (OPT_Wreorder, "base %qT will be initialized after",
1311 TREE_PURPOSE (next_subobject));
1312 if (TREE_CODE (subobject) == FIELD_DECL)
1313 warning_at (DECL_SOURCE_LOCATION (subobject),
1314 OPT_Wreorder, " %q#D", subobject);
1315 else
1316 warning (OPT_Wreorder, " base %qT", subobject);
1317 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1318 OPT_Wreorder, " when initialized here");
1321 /* Look again, from the beginning of the list. */
1322 if (!subobject_init)
1324 subobject_init = sorted_inits;
1325 while (TREE_PURPOSE (subobject_init) != subobject)
1326 subobject_init = TREE_CHAIN (subobject_init);
1329 /* It is invalid to initialize the same subobject more than
1330 once. */
1331 if (TREE_VALUE (subobject_init))
1333 if (TREE_CODE (subobject) == FIELD_DECL)
1334 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1335 "multiple initializations given for %qD",
1336 subobject);
1337 else
1338 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1339 "multiple initializations given for base %qT",
1340 subobject);
1343 /* Record the initialization. */
1344 TREE_VALUE (subobject_init) = TREE_VALUE (init);
1345 /* Carry over the dummy TREE_TYPE node containing the source location. */
1346 TREE_TYPE (subobject_init) = TREE_TYPE (init);
1347 next_subobject = subobject_init;
1350 /* [class.base.init]
1352 If a ctor-initializer specifies more than one mem-initializer for
1353 multiple members of the same union (including members of
1354 anonymous unions), the ctor-initializer is ill-formed.
1356 Here we also splice out uninitialized union members. */
1357 if (uses_unions_or_anon_p)
1359 tree *last_p = NULL;
1360 tree *p;
1361 for (p = &sorted_inits; *p; )
1363 tree field;
1364 tree ctx;
1366 init = *p;
1368 field = TREE_PURPOSE (init);
1370 /* Skip base classes. */
1371 if (TREE_CODE (field) != FIELD_DECL)
1372 goto next;
1374 /* If this is an anonymous aggregate with no explicit initializer,
1375 splice it out. */
1376 if (!TREE_VALUE (init) && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1377 goto splice;
1379 /* See if this field is a member of a union, or a member of a
1380 structure contained in a union, etc. */
1381 ctx = innermost_aggr_scope (field);
1383 /* If this field is not a member of a union, skip it. */
1384 if (TREE_CODE (ctx) != UNION_TYPE
1385 && !ANON_AGGR_TYPE_P (ctx))
1386 goto next;
1388 /* If this union member has no explicit initializer and no NSDMI,
1389 splice it out. */
1390 if (TREE_VALUE (init) || DECL_INITIAL (field))
1391 /* OK. */;
1392 else
1393 goto splice;
1395 /* It's only an error if we have two initializers for the same
1396 union type. */
1397 if (!last_p)
1399 last_p = p;
1400 goto next;
1403 /* See if LAST_FIELD and the field initialized by INIT are
1404 members of the same union (or the union itself). If so, there's
1405 a problem, unless they're actually members of the same structure
1406 which is itself a member of a union. For example, given:
1408 union { struct { int i; int j; }; };
1410 initializing both `i' and `j' makes sense. */
1411 ctx = common_enclosing_class
1412 (innermost_aggr_scope (field),
1413 innermost_aggr_scope (TREE_PURPOSE (*last_p)));
1415 if (ctx && (TREE_CODE (ctx) == UNION_TYPE
1416 || ctx == TREE_TYPE (TREE_PURPOSE (*last_p))))
1418 /* A mem-initializer hides an NSDMI. */
1419 if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
1420 *last_p = TREE_CHAIN (*last_p);
1421 else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
1422 goto splice;
1423 else
1425 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1426 "initializations for multiple members of %qT",
1427 ctx);
1428 goto splice;
1432 last_p = p;
1434 next:
1435 p = &TREE_CHAIN (*p);
1436 continue;
1437 splice:
1438 *p = TREE_CHAIN (*p);
1439 continue;
1443 return sorted_inits;
1446 /* Callback for cp_walk_tree to mark all PARM_DECLs in a tree as read. */
1448 static tree
1449 mark_exp_read_r (tree *tp, int *, void *)
1451 tree t = *tp;
1452 if (TREE_CODE (t) == PARM_DECL)
1453 mark_exp_read (t);
1454 return NULL_TREE;
1457 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1458 is a TREE_LIST giving the explicit mem-initializer-list for the
1459 constructor. The TREE_PURPOSE of each entry is a subobject (a
1460 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1461 is a TREE_LIST giving the arguments to the constructor or
1462 void_type_node for an empty list of arguments. */
1464 void
1465 emit_mem_initializers (tree mem_inits)
1467 int flags = LOOKUP_NORMAL;
1469 /* We will already have issued an error message about the fact that
1470 the type is incomplete. */
1471 if (!COMPLETE_TYPE_P (current_class_type))
1472 return;
1474 /* Keep a set holding fields that are not initialized. */
1475 hash_set<tree> uninitialized;
1477 /* Initially that is all of them. */
1478 if (warn_uninitialized)
1479 for (tree f = next_aggregate_field (TYPE_FIELDS (current_class_type));
1480 f != NULL_TREE;
1481 f = next_aggregate_field (DECL_CHAIN (f)))
1482 if (!DECL_ARTIFICIAL (f)
1483 && !is_really_empty_class (TREE_TYPE (f), /*ignore_vptr*/false))
1484 uninitialized.add (f);
1486 if (mem_inits
1487 && TYPE_P (TREE_PURPOSE (mem_inits))
1488 && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1490 /* Delegating constructor. */
1491 gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
1492 tree ctor = perform_target_ctor (TREE_VALUE (mem_inits));
1493 find_uninit_fields (&ctor, &uninitialized, current_class_type);
1494 return;
1497 if (DECL_DEFAULTED_FN (current_function_decl)
1498 && ! DECL_INHERITED_CTOR (current_function_decl))
1499 flags |= LOOKUP_DEFAULTED;
1501 /* Sort the mem-initializers into the order in which the
1502 initializations should be performed. */
1503 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
1505 in_base_initializer = 1;
1507 /* Initialize base classes. */
1508 for (; (mem_inits
1509 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
1510 mem_inits = TREE_CHAIN (mem_inits))
1512 tree subobject = TREE_PURPOSE (mem_inits);
1513 tree arguments = TREE_VALUE (mem_inits);
1515 /* We already have issued an error message. */
1516 if (arguments == error_mark_node)
1517 continue;
1519 /* Suppress access control when calling the inherited ctor. */
1520 bool inherited_base = (DECL_INHERITED_CTOR (current_function_decl)
1521 && flag_new_inheriting_ctors
1522 && arguments);
1523 if (inherited_base)
1524 push_deferring_access_checks (dk_deferred);
1526 if (arguments == NULL_TREE)
1528 /* If these initializations are taking place in a copy constructor,
1529 the base class should probably be explicitly initialized if there
1530 is a user-defined constructor in the base class (other than the
1531 default constructor, which will be called anyway). */
1532 if (extra_warnings
1533 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1534 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1535 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1536 OPT_Wextra, "base class %q#T should be explicitly "
1537 "initialized in the copy constructor",
1538 BINFO_TYPE (subobject));
1541 /* Initialize the base. */
1542 if (!BINFO_VIRTUAL_P (subobject))
1544 tree base_addr;
1546 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1547 subobject, 1, tf_warning_or_error);
1548 expand_aggr_init_1 (subobject, NULL_TREE,
1549 cp_build_fold_indirect_ref (base_addr),
1550 arguments,
1551 flags,
1552 tf_warning_or_error);
1553 expand_cleanup_for_base (subobject, NULL_TREE);
1554 if (STATEMENT_LIST_TAIL (cur_stmt_list))
1555 find_uninit_fields (&STATEMENT_LIST_TAIL (cur_stmt_list)->stmt,
1556 &uninitialized, BINFO_TYPE (subobject));
1558 else if (!ABSTRACT_CLASS_TYPE_P (current_class_type))
1559 /* C++14 DR1658 Means we do not have to construct vbases of
1560 abstract classes. */
1561 construct_virtual_base (subobject, arguments);
1562 else
1563 /* When not constructing vbases of abstract classes, at least mark
1564 the arguments expressions as read to avoid
1565 -Wunused-but-set-parameter false positives. */
1566 cp_walk_tree (&arguments, mark_exp_read_r, NULL, NULL);
1568 if (inherited_base)
1569 pop_deferring_access_checks ();
1571 in_base_initializer = 0;
1573 /* Initialize the vptrs. */
1574 initialize_vtbl_ptrs (current_class_ptr);
1576 /* Initialize the data members. */
1577 while (mem_inits)
1579 /* If this initializer was explicitly provided, then the dummy TREE_TYPE
1580 node contains the source location. */
1581 iloc_sentinel ils (EXPR_LOCATION (TREE_TYPE (mem_inits)));
1583 perform_member_init (TREE_PURPOSE (mem_inits),
1584 TREE_VALUE (mem_inits),
1585 uninitialized);
1587 mem_inits = TREE_CHAIN (mem_inits);
1591 /* Returns the address of the vtable (i.e., the value that should be
1592 assigned to the vptr) for BINFO. */
1594 tree
1595 build_vtbl_address (tree binfo)
1597 tree binfo_for = binfo;
1598 tree vtbl;
1600 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1601 /* If this is a virtual primary base, then the vtable we want to store
1602 is that for the base this is being used as the primary base of. We
1603 can't simply skip the initialization, because we may be expanding the
1604 inits of a subobject constructor where the virtual base layout
1605 can be different. */
1606 while (BINFO_PRIMARY_P (binfo_for))
1607 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1609 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1610 used. */
1611 vtbl = get_vtbl_decl_for_binfo (binfo_for);
1612 TREE_USED (vtbl) = true;
1614 /* Now compute the address to use when initializing the vptr. */
1615 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1616 if (VAR_P (vtbl))
1617 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1619 return vtbl;
1622 /* This code sets up the virtual function tables appropriate for
1623 the pointer DECL. It is a one-ply initialization.
1625 BINFO is the exact type that DECL is supposed to be. In
1626 multiple inheritance, this might mean "C's A" if C : A, B. */
1628 static void
1629 expand_virtual_init (tree binfo, tree decl)
1631 tree vtbl, vtbl_ptr;
1632 tree vtt_index;
1634 /* Compute the initializer for vptr. */
1635 vtbl = build_vtbl_address (binfo);
1637 /* We may get this vptr from a VTT, if this is a subobject
1638 constructor or subobject destructor. */
1639 vtt_index = BINFO_VPTR_INDEX (binfo);
1640 if (vtt_index)
1642 tree vtbl2;
1643 tree vtt_parm;
1645 /* Compute the value to use, when there's a VTT. */
1646 vtt_parm = current_vtt_parm;
1647 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1648 vtbl2 = cp_build_fold_indirect_ref (vtbl2);
1649 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1651 /* The actual initializer is the VTT value only in the subobject
1652 constructor. In maybe_clone_body we'll substitute NULL for
1653 the vtt_parm in the case of the non-subobject constructor. */
1654 vtbl = build_if_in_charge (vtbl, vtbl2);
1657 /* Compute the location of the vtpr. */
1658 vtbl_ptr = build_vfield_ref (cp_build_fold_indirect_ref (decl),
1659 TREE_TYPE (binfo));
1660 gcc_assert (vtbl_ptr != error_mark_node);
1662 /* Assign the vtable to the vptr. */
1663 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
1664 finish_expr_stmt (cp_build_modify_expr (input_location, vtbl_ptr, NOP_EXPR,
1665 vtbl, tf_warning_or_error));
1668 /* If an exception is thrown in a constructor, those base classes already
1669 constructed must be destroyed. This function creates the cleanup
1670 for BINFO, which has just been constructed. If FLAG is non-NULL,
1671 it is a DECL which is nonzero when this base needs to be
1672 destroyed. */
1674 static void
1675 expand_cleanup_for_base (tree binfo, tree flag)
1677 tree expr;
1679 if (!type_build_dtor_call (BINFO_TYPE (binfo)))
1680 return;
1682 /* Call the destructor. */
1683 expr = build_special_member_call (current_class_ref,
1684 base_dtor_identifier,
1685 NULL,
1686 binfo,
1687 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1688 tf_warning_or_error);
1690 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1691 return;
1693 if (flag)
1694 expr = fold_build3_loc (input_location,
1695 COND_EXPR, void_type_node,
1696 c_common_truthvalue_conversion (input_location, flag),
1697 expr, integer_zero_node);
1699 finish_eh_cleanup (expr);
1702 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1703 constructor. */
1705 static void
1706 construct_virtual_base (tree vbase, tree arguments)
1708 tree inner_if_stmt;
1709 tree exp;
1710 tree flag;
1712 /* If there are virtual base classes with destructors, we need to
1713 emit cleanups to destroy them if an exception is thrown during
1714 the construction process. These exception regions (i.e., the
1715 period during which the cleanups must occur) begin from the time
1716 the construction is complete to the end of the function. If we
1717 create a conditional block in which to initialize the
1718 base-classes, then the cleanup region for the virtual base begins
1719 inside a block, and ends outside of that block. This situation
1720 confuses the sjlj exception-handling code. Therefore, we do not
1721 create a single conditional block, but one for each
1722 initialization. (That way the cleanup regions always begin
1723 in the outer block.) We trust the back end to figure out
1724 that the FLAG will not change across initializations, and
1725 avoid doing multiple tests. */
1726 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1727 inner_if_stmt = begin_if_stmt ();
1728 finish_if_stmt_cond (flag, inner_if_stmt);
1730 /* Compute the location of the virtual base. If we're
1731 constructing virtual bases, then we must be the most derived
1732 class. Therefore, we don't have to look up the virtual base;
1733 we already know where it is. */
1734 exp = convert_to_base_statically (current_class_ref, vbase);
1736 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1737 0, tf_warning_or_error);
1738 finish_then_clause (inner_if_stmt);
1739 finish_if_stmt (inner_if_stmt);
1741 expand_cleanup_for_base (vbase, flag);
1744 /* Find the context in which this FIELD can be initialized. */
1746 static tree
1747 initializing_context (tree field)
1749 tree t = DECL_CONTEXT (field);
1751 /* Anonymous union members can be initialized in the first enclosing
1752 non-anonymous union context. */
1753 while (t && ANON_AGGR_TYPE_P (t))
1754 t = TYPE_CONTEXT (t);
1755 return t;
1758 /* Function to give error message if member initialization specification
1759 is erroneous. FIELD is the member we decided to initialize.
1760 TYPE is the type for which the initialization is being performed.
1761 FIELD must be a member of TYPE.
1763 MEMBER_NAME is the name of the member. */
1765 static int
1766 member_init_ok_or_else (tree field, tree type, tree member_name)
1768 if (field == error_mark_node)
1769 return 0;
1770 if (!field)
1772 error ("class %qT does not have any field named %qD", type,
1773 member_name);
1774 return 0;
1776 if (VAR_P (field))
1778 error ("%q#D is a static data member; it can only be "
1779 "initialized at its definition",
1780 field);
1781 return 0;
1783 if (TREE_CODE (field) != FIELD_DECL)
1785 error ("%q#D is not a non-static data member of %qT",
1786 field, type);
1787 return 0;
1789 if (initializing_context (field) != type)
1791 error ("class %qT does not have any field named %qD", type,
1792 member_name);
1793 return 0;
1796 return 1;
1799 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1800 is a _TYPE node or TYPE_DECL which names a base for that type.
1801 Check the validity of NAME, and return either the base _TYPE, base
1802 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1803 NULL_TREE and issue a diagnostic.
1805 An old style unnamed direct single base construction is permitted,
1806 where NAME is NULL. */
1808 tree
1809 expand_member_init (tree name)
1811 tree basetype;
1812 tree field;
1814 if (!current_class_ref)
1815 return NULL_TREE;
1817 if (!name)
1819 /* This is an obsolete unnamed base class initializer. The
1820 parser will already have warned about its use. */
1821 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1823 case 0:
1824 error ("unnamed initializer for %qT, which has no base classes",
1825 current_class_type);
1826 return NULL_TREE;
1827 case 1:
1828 basetype = BINFO_TYPE
1829 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1830 break;
1831 default:
1832 error ("unnamed initializer for %qT, which uses multiple inheritance",
1833 current_class_type);
1834 return NULL_TREE;
1837 else if (TYPE_P (name))
1839 basetype = TYPE_MAIN_VARIANT (name);
1840 name = TYPE_NAME (name);
1842 else if (TREE_CODE (name) == TYPE_DECL)
1843 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1844 else
1845 basetype = NULL_TREE;
1847 if (basetype)
1849 tree class_binfo;
1850 tree direct_binfo;
1851 tree virtual_binfo;
1852 int i;
1854 if (current_template_parms
1855 || same_type_p (basetype, current_class_type))
1856 return basetype;
1858 class_binfo = TYPE_BINFO (current_class_type);
1859 direct_binfo = NULL_TREE;
1860 virtual_binfo = NULL_TREE;
1862 /* Look for a direct base. */
1863 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1864 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1865 break;
1867 /* Look for a virtual base -- unless the direct base is itself
1868 virtual. */
1869 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1870 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1872 /* [class.base.init]
1874 If a mem-initializer-id is ambiguous because it designates
1875 both a direct non-virtual base class and an inherited virtual
1876 base class, the mem-initializer is ill-formed. */
1877 if (direct_binfo && virtual_binfo)
1879 error ("%qD is both a direct base and an indirect virtual base",
1880 basetype);
1881 return NULL_TREE;
1884 if (!direct_binfo && !virtual_binfo)
1886 if (CLASSTYPE_VBASECLASSES (current_class_type))
1887 error ("type %qT is not a direct or virtual base of %qT",
1888 basetype, current_class_type);
1889 else
1890 error ("type %qT is not a direct base of %qT",
1891 basetype, current_class_type);
1892 return NULL_TREE;
1895 return direct_binfo ? direct_binfo : virtual_binfo;
1897 else
1899 if (identifier_p (name))
1900 field = lookup_field (current_class_type, name, 1, false);
1901 else
1902 field = name;
1904 if (member_init_ok_or_else (field, current_class_type, name))
1905 return field;
1908 return NULL_TREE;
1911 /* This is like `expand_member_init', only it stores one aggregate
1912 value into another.
1914 INIT comes in two flavors: it is either a value which
1915 is to be stored in EXP, or it is a parameter list
1916 to go to a constructor, which will operate on EXP.
1917 If INIT is not a parameter list for a constructor, then set
1918 LOOKUP_ONLYCONVERTING.
1919 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1920 the initializer, if FLAGS is 0, then it is the (init) form.
1921 If `init' is a CONSTRUCTOR, then we emit a warning message,
1922 explaining that such initializations are invalid.
1924 If INIT resolves to a CALL_EXPR which happens to return
1925 something of the type we are looking for, then we know
1926 that we can safely use that call to perform the
1927 initialization.
1929 The virtual function table pointer cannot be set up here, because
1930 we do not really know its type.
1932 This never calls operator=().
1934 When initializing, nothing is CONST.
1936 A default copy constructor may have to be used to perform the
1937 initialization.
1939 A constructor or a conversion operator may have to be used to
1940 perform the initialization, but not both, as it would be ambiguous. */
1942 tree
1943 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1945 tree stmt_expr;
1946 tree compound_stmt;
1947 int destroy_temps;
1948 tree type = TREE_TYPE (exp);
1949 int was_const = TREE_READONLY (exp);
1950 int was_volatile = TREE_THIS_VOLATILE (exp);
1951 int is_global;
1953 if (init == error_mark_node)
1954 return error_mark_node;
1956 location_t init_loc = (init
1957 ? cp_expr_loc_or_input_loc (init)
1958 : location_of (exp));
1960 TREE_READONLY (exp) = 0;
1961 TREE_THIS_VOLATILE (exp) = 0;
1963 if (TREE_CODE (type) == ARRAY_TYPE)
1965 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1966 int from_array = 0;
1968 if (VAR_P (exp) && DECL_DECOMPOSITION_P (exp))
1970 from_array = 1;
1971 init = mark_rvalue_use (init);
1972 if (init
1973 && DECL_P (tree_strip_any_location_wrapper (init))
1974 && !(flags & LOOKUP_ONLYCONVERTING))
1976 /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init
1977 recognizes it as direct-initialization. */
1978 init = build_constructor_single (init_list_type_node,
1979 NULL_TREE, init);
1980 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
1983 else
1985 /* Must arrange to initialize each element of EXP
1986 from elements of INIT. */
1987 if (cv_qualified_p (type))
1988 TREE_TYPE (exp) = cv_unqualified (type);
1989 if (itype && cv_qualified_p (itype))
1990 TREE_TYPE (init) = cv_unqualified (itype);
1991 from_array = (itype && same_type_p (TREE_TYPE (init),
1992 TREE_TYPE (exp)));
1994 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)
1995 && (!from_array
1996 || (TREE_CODE (init) != CONSTRUCTOR
1997 /* Can happen, eg, handling the compound-literals
1998 extension (ext/complit12.C). */
1999 && TREE_CODE (init) != TARGET_EXPR)))
2001 if (complain & tf_error)
2002 error_at (init_loc, "array must be initialized "
2003 "with a brace-enclosed initializer");
2004 return error_mark_node;
2008 stmt_expr = build_vec_init (exp, NULL_TREE, init,
2009 /*explicit_value_init_p=*/false,
2010 from_array,
2011 complain);
2012 TREE_READONLY (exp) = was_const;
2013 TREE_THIS_VOLATILE (exp) = was_volatile;
2014 TREE_TYPE (exp) = type;
2015 /* Restore the type of init unless it was used directly. */
2016 if (init && TREE_CODE (stmt_expr) != INIT_EXPR)
2017 TREE_TYPE (init) = itype;
2018 return stmt_expr;
2021 if (is_copy_initialization (init))
2022 flags |= LOOKUP_ONLYCONVERTING;
2024 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
2025 destroy_temps = stmts_are_full_exprs_p ();
2026 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2027 bool ok = expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
2028 init, LOOKUP_NORMAL|flags, complain);
2029 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
2030 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
2031 TREE_READONLY (exp) = was_const;
2032 TREE_THIS_VOLATILE (exp) = was_volatile;
2033 if (!ok)
2034 return error_mark_node;
2036 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
2037 && TREE_SIDE_EFFECTS (stmt_expr)
2038 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
2039 /* Just know that we've seen something for this node. */
2040 TREE_USED (exp) = 1;
2042 return stmt_expr;
2045 static bool
2046 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
2047 tsubst_flags_t complain)
2049 tree type = TREE_TYPE (exp);
2051 /* It fails because there may not be a constructor which takes
2052 its own type as the first (or only parameter), but which does
2053 take other types via a conversion. So, if the thing initializing
2054 the expression is a unit element of type X, first try X(X&),
2055 followed by initialization by X. If neither of these work
2056 out, then look hard. */
2057 tree rval;
2058 vec<tree, va_gc> *parms;
2060 /* If we have direct-initialization from an initializer list, pull
2061 it out of the TREE_LIST so the code below can see it. */
2062 if (init && TREE_CODE (init) == TREE_LIST
2063 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2065 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
2066 && TREE_CHAIN (init) == NULL_TREE);
2067 init = TREE_VALUE (init);
2068 /* Only call reshape_init if it has not been called earlier
2069 by the callers. */
2070 if (BRACE_ENCLOSED_INITIALIZER_P (init) && CP_AGGREGATE_TYPE_P (type))
2071 init = reshape_init (type, init, complain);
2074 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
2075 && CP_AGGREGATE_TYPE_P (type))
2076 /* A brace-enclosed initializer for an aggregate. In C++0x this can
2077 happen for direct-initialization, too. */
2078 init = digest_init (type, init, complain);
2080 if (init == error_mark_node)
2081 return false;
2083 /* A CONSTRUCTOR of the target's type is a previously digested
2084 initializer, whether that happened just above or in
2085 cp_parser_late_parsing_nsdmi.
2087 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
2088 set represents the whole initialization, so we shouldn't build up
2089 another ctor call. */
2090 if (init
2091 && (TREE_CODE (init) == CONSTRUCTOR
2092 || (TREE_CODE (init) == TARGET_EXPR
2093 && (TARGET_EXPR_DIRECT_INIT_P (init)
2094 || TARGET_EXPR_LIST_INIT_P (init))))
2095 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
2097 /* Early initialization via a TARGET_EXPR only works for
2098 complete objects. */
2099 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
2101 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
2102 TREE_SIDE_EFFECTS (init) = 1;
2103 finish_expr_stmt (init);
2104 return true;
2107 if (init && TREE_CODE (init) != TREE_LIST
2108 && (flags & LOOKUP_ONLYCONVERTING)
2109 && !unsafe_return_slot_p (exp))
2111 /* Base subobjects should only get direct-initialization. */
2112 gcc_assert (true_exp == exp);
2114 if (flags & DIRECT_BIND)
2115 /* Do nothing. We hit this in two cases: Reference initialization,
2116 where we aren't initializing a real variable, so we don't want
2117 to run a new constructor; and catching an exception, where we
2118 have already built up the constructor call so we could wrap it
2119 in an exception region. */;
2120 else
2122 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
2123 flags, complain | tf_no_cleanup);
2124 if (init == error_mark_node)
2125 return false;
2128 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
2129 /* We need to protect the initialization of a catch parm with a
2130 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
2131 around the TARGET_EXPR for the copy constructor. See
2132 initialize_handler_parm. */
2134 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
2135 TREE_OPERAND (init, 0));
2136 TREE_TYPE (init) = void_type_node;
2138 else
2139 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
2140 TREE_SIDE_EFFECTS (init) = 1;
2141 finish_expr_stmt (init);
2142 return true;
2145 if (init == NULL_TREE)
2146 parms = NULL;
2147 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
2149 parms = make_tree_vector ();
2150 for (; init != NULL_TREE; init = TREE_CHAIN (init))
2151 vec_safe_push (parms, TREE_VALUE (init));
2153 else
2154 parms = make_tree_vector_single (init);
2156 if (exp == current_class_ref && current_function_decl
2157 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
2159 /* Delegating constructor. */
2160 tree complete;
2161 tree base;
2162 tree elt; unsigned i;
2164 /* Unshare the arguments for the second call. */
2165 releasing_vec parms2;
2166 FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
2168 elt = break_out_target_exprs (elt);
2169 vec_safe_push (parms2, elt);
2171 complete = build_special_member_call (exp, complete_ctor_identifier,
2172 &parms2, binfo, flags,
2173 complain);
2174 complete = fold_build_cleanup_point_expr (void_type_node, complete);
2176 base = build_special_member_call (exp, base_ctor_identifier,
2177 &parms, binfo, flags,
2178 complain);
2179 base = fold_build_cleanup_point_expr (void_type_node, base);
2180 if (complete == error_mark_node || base == error_mark_node)
2181 return false;
2182 rval = build_if_in_charge (complete, base);
2184 else
2186 tree ctor_name = (true_exp == exp
2187 ? complete_ctor_identifier : base_ctor_identifier);
2189 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
2190 complain);
2191 if (rval == error_mark_node)
2192 return false;
2195 if (parms != NULL)
2196 release_tree_vector (parms);
2198 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
2200 tree fn = get_callee_fndecl (rval);
2201 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
2203 tree e = maybe_constant_init (rval, exp);
2204 if (TREE_CONSTANT (e))
2205 rval = build2 (INIT_EXPR, type, exp, e);
2209 /* FIXME put back convert_to_void? */
2210 if (TREE_SIDE_EFFECTS (rval))
2211 finish_expr_stmt (rval);
2213 return true;
2216 /* This function is responsible for initializing EXP with INIT
2217 (if any). Returns true on success, false on failure.
2219 BINFO is the binfo of the type for who we are performing the
2220 initialization. For example, if W is a virtual base class of A and B,
2221 and C : A, B.
2222 If we are initializing B, then W must contain B's W vtable, whereas
2223 were we initializing C, W must contain C's W vtable.
2225 TRUE_EXP is nonzero if it is the true expression being initialized.
2226 In this case, it may be EXP, or may just contain EXP. The reason we
2227 need this is because if EXP is a base element of TRUE_EXP, we
2228 don't necessarily know by looking at EXP where its virtual
2229 baseclass fields should really be pointing. But we do know
2230 from TRUE_EXP. In constructors, we don't know anything about
2231 the value being initialized.
2233 FLAGS is just passed to `build_new_method_call'. See that function
2234 for its description. */
2236 static bool
2237 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
2238 tsubst_flags_t complain)
2240 tree type = TREE_TYPE (exp);
2242 gcc_assert (init != error_mark_node && type != error_mark_node);
2243 gcc_assert (building_stmt_list_p ());
2245 /* Use a function returning the desired type to initialize EXP for us.
2246 If the function is a constructor, and its first argument is
2247 NULL_TREE, know that it was meant for us--just slide exp on
2248 in and expand the constructor. Constructors now come
2249 as TARGET_EXPRs. */
2251 if (init && VAR_P (exp)
2252 && COMPOUND_LITERAL_P (init))
2254 vec<tree, va_gc> *cleanups = NULL;
2255 /* If store_init_value returns NULL_TREE, the INIT has been
2256 recorded as the DECL_INITIAL for EXP. That means there's
2257 nothing more we have to do. */
2258 init = store_init_value (exp, init, &cleanups, flags);
2259 if (init)
2260 finish_expr_stmt (init);
2261 gcc_assert (!cleanups);
2262 return true;
2265 /* List-initialization from {} becomes value-initialization for non-aggregate
2266 classes with default constructors. Handle this here when we're
2267 initializing a base, so protected access works. */
2268 if (exp != true_exp && init && TREE_CODE (init) == TREE_LIST)
2270 tree elt = TREE_VALUE (init);
2271 if (DIRECT_LIST_INIT_P (elt)
2272 && CONSTRUCTOR_ELTS (elt) == 0
2273 && CLASSTYPE_NON_AGGREGATE (type)
2274 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2275 init = void_type_node;
2278 /* If an explicit -- but empty -- initializer list was present,
2279 that's value-initialization. */
2280 if (init == void_type_node)
2282 /* If the type has data but no user-provided default ctor, we need to zero
2283 out the object. */
2284 if (type_has_non_user_provided_default_constructor (type)
2285 && !is_really_empty_class (type, /*ignore_vptr*/true))
2287 tree field_size = NULL_TREE;
2288 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
2289 /* Don't clobber already initialized virtual bases. */
2290 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
2291 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
2292 field_size);
2293 init = build2 (INIT_EXPR, type, exp, init);
2294 finish_expr_stmt (init);
2297 /* If we don't need to mess with the constructor at all,
2298 then we're done. */
2299 if (! type_build_ctor_call (type))
2300 return true;
2302 /* Otherwise fall through and call the constructor. */
2303 init = NULL_TREE;
2306 /* We know that expand_default_init can handle everything we want
2307 at this point. */
2308 return expand_default_init (binfo, true_exp, exp, init, flags, complain);
2311 /* Report an error if TYPE is not a user-defined, class type. If
2312 OR_ELSE is nonzero, give an error message. */
2315 is_class_type (tree type, int or_else)
2317 if (type == error_mark_node)
2318 return 0;
2320 if (! CLASS_TYPE_P (type))
2322 if (or_else)
2323 error ("%qT is not a class type", type);
2324 return 0;
2326 return 1;
2329 /* Returns true iff the initializer INIT represents copy-initialization
2330 (and therefore we must set LOOKUP_ONLYCONVERTING when processing it). */
2332 bool
2333 is_copy_initialization (tree init)
2335 return (init && init != void_type_node
2336 && TREE_CODE (init) != TREE_LIST
2337 && !(TREE_CODE (init) == TARGET_EXPR
2338 && TARGET_EXPR_DIRECT_INIT_P (init))
2339 && !DIRECT_LIST_INIT_P (init));
2342 /* Build a reference to a member of an aggregate. This is not a C++
2343 `&', but really something which can have its address taken, and
2344 then act as a pointer to member, for example TYPE :: FIELD can have
2345 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
2346 this expression is the operand of "&".
2348 @@ Prints out lousy diagnostics for operator <typename>
2349 @@ fields.
2351 @@ This function should be rewritten and placed in search.cc. */
2353 tree
2354 build_offset_ref (tree type, tree member, bool address_p,
2355 tsubst_flags_t complain)
2357 tree decl;
2358 tree basebinfo = NULL_TREE;
2360 /* class templates can come in as TEMPLATE_DECLs here. */
2361 if (TREE_CODE (member) == TEMPLATE_DECL)
2362 return member;
2364 if (dependent_scope_p (type) || type_dependent_expression_p (member))
2365 return build_qualified_name (NULL_TREE, type, member,
2366 /*template_p=*/false);
2368 gcc_assert (TYPE_P (type));
2369 if (! is_class_type (type, 1))
2370 return error_mark_node;
2372 gcc_assert (DECL_P (member) || BASELINK_P (member));
2373 /* Callers should call mark_used before this point, except for functions. */
2374 gcc_assert (!DECL_P (member) || TREE_USED (member)
2375 || TREE_CODE (member) == FUNCTION_DECL);
2377 type = TYPE_MAIN_VARIANT (type);
2378 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
2380 if (complain & tf_error)
2381 error ("incomplete type %qT does not have member %qD", type, member);
2382 return error_mark_node;
2385 /* Entities other than non-static members need no further
2386 processing. */
2387 if (TREE_CODE (member) == TYPE_DECL)
2388 return member;
2389 if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
2390 return convert_from_reference (member);
2392 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
2394 if (complain & tf_error)
2395 error ("invalid pointer to bit-field %qD", member);
2396 return error_mark_node;
2399 /* Set up BASEBINFO for member lookup. */
2400 decl = maybe_dummy_object (type, &basebinfo);
2402 /* A lot of this logic is now handled in lookup_member. */
2403 if (BASELINK_P (member))
2405 /* Go from the TREE_BASELINK to the member function info. */
2406 tree t = BASELINK_FUNCTIONS (member);
2408 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
2410 /* Get rid of a potential OVERLOAD around it. */
2411 t = OVL_FIRST (t);
2413 /* Unique functions are handled easily. */
2415 /* For non-static member of base class, we need a special rule
2416 for access checking [class.protected]:
2418 If the access is to form a pointer to member, the
2419 nested-name-specifier shall name the derived class
2420 (or any class derived from that class). */
2421 bool ok;
2422 if (address_p && DECL_P (t)
2423 && DECL_NONSTATIC_MEMBER_P (t))
2424 ok = perform_or_defer_access_check (TYPE_BINFO (type), t, t,
2425 complain);
2426 else
2427 ok = perform_or_defer_access_check (basebinfo, t, t,
2428 complain);
2429 if (!ok)
2430 return error_mark_node;
2431 if (DECL_STATIC_FUNCTION_P (t))
2432 return member;
2433 member = t;
2435 else
2436 TREE_TYPE (member) = unknown_type_node;
2438 else if (address_p && TREE_CODE (member) == FIELD_DECL)
2440 /* We need additional test besides the one in
2441 check_accessibility_of_qualified_id in case it is
2442 a pointer to non-static member. */
2443 if (!perform_or_defer_access_check (TYPE_BINFO (type), member, member,
2444 complain))
2445 return error_mark_node;
2448 if (!address_p)
2450 /* If MEMBER is non-static, then the program has fallen afoul of
2451 [expr.prim]:
2453 An id-expression that denotes a non-static data member or
2454 non-static member function of a class can only be used:
2456 -- as part of a class member access (_expr.ref_) in which the
2457 object-expression refers to the member's class or a class
2458 derived from that class, or
2460 -- to form a pointer to member (_expr.unary.op_), or
2462 -- in the body of a non-static member function of that class or
2463 of a class derived from that class (_class.mfct.non-static_), or
2465 -- in a mem-initializer for a constructor for that class or for
2466 a class derived from that class (_class.base.init_). */
2467 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
2469 /* Build a representation of the qualified name suitable
2470 for use as the operand to "&" -- even though the "&" is
2471 not actually present. */
2472 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
2473 /* In Microsoft mode, treat a non-static member function as if
2474 it were a pointer-to-member. */
2475 if (flag_ms_extensions)
2477 PTRMEM_OK_P (member) = 1;
2478 return cp_build_addr_expr (member, complain);
2480 if (complain & tf_error)
2481 error ("invalid use of non-static member function %qD",
2482 TREE_OPERAND (member, 1));
2483 return error_mark_node;
2485 else if (TREE_CODE (member) == FIELD_DECL)
2487 if (complain & tf_error)
2488 error ("invalid use of non-static data member %qD", member);
2489 return error_mark_node;
2491 return member;
2494 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
2495 PTRMEM_OK_P (member) = 1;
2496 return member;
2499 /* If DECL is a scalar enumeration constant or variable with a
2500 constant initializer, return the initializer (or, its initializers,
2501 recursively); otherwise, return DECL. If STRICT_P, the
2502 initializer is only returned if DECL is a
2503 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
2504 return an aggregate constant. If UNSHARE_P, return an unshared
2505 copy of the initializer. */
2507 static tree
2508 constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p,
2509 bool unshare_p)
2511 while (TREE_CODE (decl) == CONST_DECL
2512 || decl_constant_var_p (decl)
2513 || (!strict_p && VAR_P (decl)
2514 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl))))
2516 tree init;
2517 /* If DECL is a static data member in a template
2518 specialization, we must instantiate it here. The
2519 initializer for the static data member is not processed
2520 until needed; we need it now. */
2521 mark_used (decl, tf_none);
2522 init = DECL_INITIAL (decl);
2523 if (init == error_mark_node)
2525 if (TREE_CODE (decl) == CONST_DECL
2526 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2527 /* Treat the error as a constant to avoid cascading errors on
2528 excessively recursive template instantiation (c++/9335). */
2529 return init;
2530 else
2531 return decl;
2533 /* Initializers in templates are generally expanded during
2534 instantiation, so before that for const int i(2)
2535 INIT is a TREE_LIST with the actual initializer as
2536 TREE_VALUE. */
2537 if (processing_template_decl
2538 && init
2539 && TREE_CODE (init) == TREE_LIST
2540 && TREE_CHAIN (init) == NULL_TREE)
2541 init = TREE_VALUE (init);
2542 /* Instantiate a non-dependent initializer for user variables. We
2543 mustn't do this for the temporary for an array compound literal;
2544 trying to instatiate the initializer will keep creating new
2545 temporaries until we crash. Probably it's not useful to do it for
2546 other artificial variables, either. */
2547 if (!DECL_ARTIFICIAL (decl))
2548 init = instantiate_non_dependent_or_null (init);
2549 if (!init
2550 || !TREE_TYPE (init)
2551 || !TREE_CONSTANT (init)
2552 || (!return_aggregate_cst_ok_p
2553 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2554 return an aggregate constant (of which string
2555 literals are a special case), as we do not want
2556 to make inadvertent copies of such entities, and
2557 we must be sure that their addresses are the
2558 same everywhere. */
2559 && (TREE_CODE (init) == CONSTRUCTOR
2560 || TREE_CODE (init) == STRING_CST)))
2561 break;
2562 /* Don't return a CONSTRUCTOR for a variable with partial run-time
2563 initialization, since it doesn't represent the entire value.
2564 Similarly for VECTOR_CSTs created by cp_folding those
2565 CONSTRUCTORs. */
2566 if ((TREE_CODE (init) == CONSTRUCTOR
2567 || TREE_CODE (init) == VECTOR_CST)
2568 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2569 break;
2570 /* If the variable has a dynamic initializer, don't use its
2571 DECL_INITIAL which doesn't reflect the real value. */
2572 if (VAR_P (decl)
2573 && TREE_STATIC (decl)
2574 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
2575 && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2576 break;
2577 decl = init;
2579 return unshare_p ? unshare_expr (decl) : decl;
2582 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant
2583 of integral or enumeration type, or a constexpr variable of scalar type,
2584 then return that value. These are those variables permitted in constant
2585 expressions by [5.19/1]. */
2587 tree
2588 scalar_constant_value (tree decl)
2590 return constant_value_1 (decl, /*strict_p=*/true,
2591 /*return_aggregate_cst_ok_p=*/false,
2592 /*unshare_p=*/true);
2595 /* Like scalar_constant_value, but can also return aggregate initializers.
2596 If UNSHARE_P, return an unshared copy of the initializer. */
2598 tree
2599 decl_really_constant_value (tree decl, bool unshare_p /*= true*/)
2601 return constant_value_1 (decl, /*strict_p=*/true,
2602 /*return_aggregate_cst_ok_p=*/true,
2603 /*unshare_p=*/unshare_p);
2606 /* A more relaxed version of decl_really_constant_value, used by the
2607 common C/C++ code. */
2609 tree
2610 decl_constant_value (tree decl, bool unshare_p)
2612 return constant_value_1 (decl, /*strict_p=*/processing_template_decl,
2613 /*return_aggregate_cst_ok_p=*/true,
2614 /*unshare_p=*/unshare_p);
2617 tree
2618 decl_constant_value (tree decl)
2620 return decl_constant_value (decl, /*unshare_p=*/true);
2623 /* Common subroutines of build_new and build_vec_delete. */
2625 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2626 the type of the object being allocated; otherwise, it's just TYPE.
2627 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2628 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2629 a vector of arguments to be provided as arguments to a placement
2630 new operator. This routine performs no semantic checks; it just
2631 creates and returns a NEW_EXPR. */
2633 static tree
2634 build_raw_new_expr (location_t loc, vec<tree, va_gc> *placement, tree type,
2635 tree nelts, vec<tree, va_gc> *init, int use_global_new)
2637 tree init_list;
2638 tree new_expr;
2640 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2641 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2642 permits us to distinguish the case of a missing initializer "new
2643 int" from an empty initializer "new int()". */
2644 if (init == NULL)
2645 init_list = NULL_TREE;
2646 else if (init->is_empty ())
2647 init_list = void_node;
2648 else
2649 init_list = build_tree_list_vec (init);
2651 new_expr = build4_loc (loc, NEW_EXPR, build_pointer_type (type),
2652 build_tree_list_vec (placement), type, nelts,
2653 init_list);
2654 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2655 TREE_SIDE_EFFECTS (new_expr) = 1;
2657 return new_expr;
2660 /* Diagnose uninitialized const members or reference members of type
2661 TYPE. USING_NEW is used to disambiguate the diagnostic between a
2662 new expression without a new-initializer and a declaration. Returns
2663 the error count. */
2665 static int
2666 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2667 bool using_new, bool complain)
2669 tree field;
2670 int error_count = 0;
2672 if (type_has_user_provided_constructor (type))
2673 return 0;
2675 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2677 tree field_type;
2679 if (TREE_CODE (field) != FIELD_DECL)
2680 continue;
2682 field_type = strip_array_types (TREE_TYPE (field));
2684 if (type_has_user_provided_constructor (field_type))
2685 continue;
2687 if (TYPE_REF_P (field_type))
2689 ++ error_count;
2690 if (complain)
2692 if (DECL_CONTEXT (field) == origin)
2694 if (using_new)
2695 error ("uninitialized reference member in %q#T "
2696 "using %<new%> without new-initializer", origin);
2697 else
2698 error ("uninitialized reference member in %q#T", origin);
2700 else
2702 if (using_new)
2703 error ("uninitialized reference member in base %q#T "
2704 "of %q#T using %<new%> without new-initializer",
2705 DECL_CONTEXT (field), origin);
2706 else
2707 error ("uninitialized reference member in base %q#T "
2708 "of %q#T", DECL_CONTEXT (field), origin);
2710 inform (DECL_SOURCE_LOCATION (field),
2711 "%q#D should be initialized", field);
2715 if (CP_TYPE_CONST_P (field_type))
2717 ++ error_count;
2718 if (complain)
2720 if (DECL_CONTEXT (field) == origin)
2722 if (using_new)
2723 error ("uninitialized const member in %q#T "
2724 "using %<new%> without new-initializer", origin);
2725 else
2726 error ("uninitialized const member in %q#T", origin);
2728 else
2730 if (using_new)
2731 error ("uninitialized const member in base %q#T "
2732 "of %q#T using %<new%> without new-initializer",
2733 DECL_CONTEXT (field), origin);
2734 else
2735 error ("uninitialized const member in base %q#T "
2736 "of %q#T", DECL_CONTEXT (field), origin);
2738 inform (DECL_SOURCE_LOCATION (field),
2739 "%q#D should be initialized", field);
2743 if (CLASS_TYPE_P (field_type))
2744 error_count
2745 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2746 using_new, complain);
2748 return error_count;
2752 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2754 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2757 /* Call __cxa_bad_array_new_length to indicate that the size calculation
2758 overflowed. Pretend it returns sizetype so that it plays nicely in the
2759 COND_EXPR. */
2761 tree
2762 throw_bad_array_new_length (void)
2764 if (!fn)
2766 tree name = get_identifier ("__cxa_throw_bad_array_new_length");
2768 fn = get_global_binding (name);
2769 if (!fn)
2770 fn = push_throw_library_fn
2771 (name, build_function_type_list (sizetype, NULL_TREE));
2774 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2777 /* Attempt to verify that the argument, OPER, of a placement new expression
2778 refers to an object sufficiently large for an object of TYPE or an array
2779 of NELTS of such objects when NELTS is non-null, and issue a warning when
2780 it does not. SIZE specifies the size needed to construct the object or
2781 array and captures the result of NELTS * sizeof (TYPE). (SIZE could be
2782 greater when the array under construction requires a cookie to store
2783 NELTS. GCC's placement new expression stores the cookie when invoking
2784 a user-defined placement new operator function but not the default one.
2785 Placement new expressions with user-defined placement new operator are
2786 not diagnosed since we don't know how they use the buffer (this could
2787 be a future extension). */
2788 static void
2789 warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper)
2791 location_t loc = cp_expr_loc_or_input_loc (oper);
2793 STRIP_NOPS (oper);
2795 /* Using a function argument or a (non-array) variable as an argument
2796 to placement new is not checked since it's unknown what it might
2797 point to. */
2798 if (TREE_CODE (oper) == PARM_DECL
2799 || VAR_P (oper)
2800 || TREE_CODE (oper) == COMPONENT_REF)
2801 return;
2803 /* Evaluate any constant expressions. */
2804 size = fold_non_dependent_expr (size);
2806 access_ref ref;
2807 ref.eval = [](tree x){ return fold_non_dependent_expr (x); };
2808 ref.trail1special = warn_placement_new < 2;
2809 tree objsize = compute_objsize (oper, 1, &ref);
2810 if (!objsize)
2811 return;
2813 /* We can only draw conclusions if ref.deref == -1,
2814 i.e. oper is the address of the object. */
2815 if (ref.deref != -1)
2816 return;
2818 offset_int bytes_avail = wi::to_offset (objsize);
2819 offset_int bytes_need;
2821 if (CONSTANT_CLASS_P (size))
2822 bytes_need = wi::to_offset (size);
2823 else if (nelts && CONSTANT_CLASS_P (nelts))
2824 bytes_need = (wi::to_offset (nelts)
2825 * wi::to_offset (TYPE_SIZE_UNIT (type)));
2826 else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2827 bytes_need = wi::to_offset (TYPE_SIZE_UNIT (type));
2828 else
2830 /* The type is a VLA. */
2831 return;
2834 if (bytes_avail >= bytes_need)
2835 return;
2837 /* True when the size to mention in the warning is exact as opposed
2838 to "at least N". */
2839 const bool exact_size = (ref.offrng[0] == ref.offrng[1]
2840 || ref.sizrng[1] - ref.offrng[0] == 0);
2842 tree opertype = ref.ref ? TREE_TYPE (ref.ref) : TREE_TYPE (oper);
2843 bool warned = false;
2844 if (nelts)
2845 nelts = fold_for_warn (nelts);
2846 if (nelts)
2847 if (CONSTANT_CLASS_P (nelts))
2848 warned = warning_at (loc, OPT_Wplacement_new_,
2849 (exact_size
2850 ? G_("placement new constructing an object "
2851 "of type %<%T [%wu]%> and size %qwu "
2852 "in a region of type %qT and size %qwi")
2853 : G_("placement new constructing an object "
2854 "of type %<%T [%wu]%> and size %qwu "
2855 "in a region of type %qT and size "
2856 "at most %qwu")),
2857 type, tree_to_uhwi (nelts),
2858 bytes_need.to_uhwi (),
2859 opertype, bytes_avail.to_uhwi ());
2860 else
2861 warned = warning_at (loc, OPT_Wplacement_new_,
2862 (exact_size
2863 ? G_("placement new constructing an array "
2864 "of objects of type %qT and size %qwu "
2865 "in a region of type %qT and size %qwi")
2866 : G_("placement new constructing an array "
2867 "of objects of type %qT and size %qwu "
2868 "in a region of type %qT and size "
2869 "at most %qwu")),
2870 type, bytes_need.to_uhwi (), opertype,
2871 bytes_avail.to_uhwi ());
2872 else
2873 warned = warning_at (loc, OPT_Wplacement_new_,
2874 (exact_size
2875 ? G_("placement new constructing an object "
2876 "of type %qT and size %qwu in a region "
2877 "of type %qT and size %qwi")
2878 : G_("placement new constructing an object "
2879 "of type %qT "
2880 "and size %qwu in a region of type %qT "
2881 "and size at most %qwu")),
2882 type, bytes_need.to_uhwi (), opertype,
2883 bytes_avail.to_uhwi ());
2885 if (!warned || !ref.ref)
2886 return;
2888 if (ref.offrng[0] == 0 || !ref.offset_bounded ())
2889 /* Avoid mentioning the offset when its lower bound is zero
2890 or when it's impossibly large. */
2891 inform (DECL_SOURCE_LOCATION (ref.ref),
2892 "%qD declared here", ref.ref);
2893 else if (ref.offrng[0] == ref.offrng[1])
2894 inform (DECL_SOURCE_LOCATION (ref.ref),
2895 "at offset %wi from %qD declared here",
2896 ref.offrng[0].to_shwi (), ref.ref);
2897 else
2898 inform (DECL_SOURCE_LOCATION (ref.ref),
2899 "at offset [%wi, %wi] from %qD declared here",
2900 ref.offrng[0].to_shwi (), ref.offrng[1].to_shwi (), ref.ref);
2903 /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */
2905 bool
2906 type_has_new_extended_alignment (tree t)
2908 return (aligned_new_threshold
2909 && TYPE_ALIGN_UNIT (t) > (unsigned)aligned_new_threshold);
2912 /* Return the alignment we expect malloc to guarantee. This should just be
2913 MALLOC_ABI_ALIGNMENT, but that macro defaults to only BITS_PER_WORD for some
2914 reason, so don't let the threshold be smaller than max_align_t_align. */
2916 unsigned
2917 malloc_alignment ()
2919 return MAX (max_align_t_align(), MALLOC_ABI_ALIGNMENT);
2922 /* Determine whether an allocation function is a namespace-scope
2923 non-replaceable placement new function. See DR 1748. */
2924 static bool
2925 std_placement_new_fn_p (tree alloc_fn)
2927 if (DECL_NAMESPACE_SCOPE_P (alloc_fn))
2929 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
2930 if ((TREE_VALUE (first_arg) == ptr_type_node)
2931 && TREE_CHAIN (first_arg) == void_list_node)
2932 return true;
2934 return false;
2937 /* For element type ELT_TYPE, return the appropriate type of the heap object
2938 containing such element(s). COOKIE_SIZE is the size of cookie in bytes.
2939 Return
2940 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
2941 where N is nothing (flexible array member) if ITYPE2 is NULL, otherwise
2942 the array has ITYPE2 as its TYPE_DOMAIN. */
2944 tree
2945 build_new_constexpr_heap_type (tree elt_type, tree cookie_size, tree itype2)
2947 gcc_assert (tree_fits_uhwi_p (cookie_size));
2948 unsigned HOST_WIDE_INT csz = tree_to_uhwi (cookie_size);
2949 csz /= int_size_in_bytes (sizetype);
2950 tree itype1 = build_index_type (size_int (csz - 1));
2951 tree atype1 = build_cplus_array_type (sizetype, itype1);
2952 tree atype2 = build_cplus_array_type (elt_type, itype2);
2953 tree rtype = cxx_make_type (RECORD_TYPE);
2954 TYPE_NAME (rtype) = heap_identifier;
2955 tree fld1 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype1);
2956 tree fld2 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype2);
2957 DECL_FIELD_CONTEXT (fld1) = rtype;
2958 DECL_FIELD_CONTEXT (fld2) = rtype;
2959 DECL_ARTIFICIAL (fld1) = true;
2960 DECL_ARTIFICIAL (fld2) = true;
2961 TYPE_FIELDS (rtype) = fld1;
2962 DECL_CHAIN (fld1) = fld2;
2963 layout_type (rtype);
2964 return rtype;
2967 /* Help the constexpr code to find the right type for the heap variable
2968 by adding a NOP_EXPR around ALLOC_CALL if needed for cookie_size.
2969 Return ALLOC_CALL or ALLOC_CALL cast to a pointer to
2970 struct { size_t[cookie_size/sizeof(size_t)]; elt_type[]; }. */
2972 static tree
2973 maybe_wrap_new_for_constexpr (tree alloc_call, tree elt_type, tree cookie_size)
2975 if (cxx_dialect < cxx20)
2976 return alloc_call;
2978 if (current_function_decl != NULL_TREE
2979 && !DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2980 return alloc_call;
2982 tree call_expr = extract_call_expr (alloc_call);
2983 if (call_expr == error_mark_node)
2984 return alloc_call;
2986 tree alloc_call_fndecl = cp_get_callee_fndecl_nofold (call_expr);
2987 if (alloc_call_fndecl == NULL_TREE
2988 || !IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_call_fndecl))
2989 || CP_DECL_CONTEXT (alloc_call_fndecl) != global_namespace)
2990 return alloc_call;
2992 tree rtype = build_new_constexpr_heap_type (elt_type, cookie_size,
2993 NULL_TREE);
2994 return build_nop (build_pointer_type (rtype), alloc_call);
2997 /* Generate code for a new-expression, including calling the "operator
2998 new" function, initializing the object, and, if an exception occurs
2999 during construction, cleaning up. The arguments are as for
3000 build_raw_new_expr. This may change PLACEMENT and INIT.
3001 TYPE is the type of the object being constructed, possibly an array
3002 of NELTS elements when NELTS is non-null (in "new T[NELTS]", T may
3003 be an array of the form U[inner], with the whole expression being
3004 "new U[NELTS][inner]"). */
3006 static tree
3007 build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
3008 vec<tree, va_gc> **init, bool globally_qualified_p,
3009 tsubst_flags_t complain)
3011 tree size, rval;
3012 /* True iff this is a call to "operator new[]" instead of just
3013 "operator new". */
3014 bool array_p = false;
3015 /* If ARRAY_P is true, the element type of the array. This is never
3016 an ARRAY_TYPE; for something like "new int[3][4]", the
3017 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
3018 TYPE. */
3019 tree elt_type;
3020 /* The type of the new-expression. (This type is always a pointer
3021 type.) */
3022 tree pointer_type;
3023 tree non_const_pointer_type;
3024 /* The most significant array bound in int[OUTER_NELTS][inner]. */
3025 tree outer_nelts = NULL_TREE;
3026 /* For arrays with a non-constant number of elements, a bounds checks
3027 on the NELTS parameter to avoid integer overflow at runtime. */
3028 tree outer_nelts_check = NULL_TREE;
3029 bool outer_nelts_from_type = false;
3030 /* Number of the "inner" elements in "new T[OUTER_NELTS][inner]". */
3031 offset_int inner_nelts_count = 1;
3032 tree alloc_call, alloc_expr;
3033 /* Size of the inner array elements (those with constant dimensions). */
3034 offset_int inner_size;
3035 /* The address returned by the call to "operator new". This node is
3036 a VAR_DECL and is therefore reusable. */
3037 tree alloc_node;
3038 tree alloc_fn;
3039 tree cookie_expr, init_expr;
3040 int nothrow, check_new;
3041 /* If non-NULL, the number of extra bytes to allocate at the
3042 beginning of the storage allocated for an array-new expression in
3043 order to store the number of elements. */
3044 tree cookie_size = NULL_TREE;
3045 tree placement_first;
3046 tree placement_expr = NULL_TREE;
3047 /* True if the function we are calling is a placement allocation
3048 function. */
3049 bool placement_allocation_fn_p;
3050 /* True if the storage must be initialized, either by a constructor
3051 or due to an explicit new-initializer. */
3052 bool is_initialized;
3053 /* The address of the thing allocated, not including any cookie. In
3054 particular, if an array cookie is in use, DATA_ADDR is the
3055 address of the first array element. This node is a VAR_DECL, and
3056 is therefore reusable. */
3057 tree data_addr;
3058 tree orig_type = type;
3060 if (nelts)
3062 outer_nelts = nelts;
3063 array_p = true;
3065 else if (TREE_CODE (type) == ARRAY_TYPE)
3067 /* Transforms new (T[N]) to new T[N]. The former is a GNU
3068 extension for variable N. (This also covers new T where T is
3069 a VLA typedef.) */
3070 array_p = true;
3071 nelts = array_type_nelts_top (type);
3072 outer_nelts = nelts;
3073 type = TREE_TYPE (type);
3074 outer_nelts_from_type = true;
3077 /* Lots of logic below depends on whether we have a constant number of
3078 elements, so go ahead and fold it now. */
3079 const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain);
3081 /* If our base type is an array, then make sure we know how many elements
3082 it has. */
3083 for (elt_type = type;
3084 TREE_CODE (elt_type) == ARRAY_TYPE;
3085 elt_type = TREE_TYPE (elt_type))
3087 tree inner_nelts = array_type_nelts_top (elt_type);
3088 tree inner_nelts_cst = maybe_constant_value (inner_nelts);
3089 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
3091 wi::overflow_type overflow;
3092 offset_int result = wi::mul (wi::to_offset (inner_nelts_cst),
3093 inner_nelts_count, SIGNED, &overflow);
3094 if (overflow)
3096 if (complain & tf_error)
3097 error ("integer overflow in array size");
3098 nelts = error_mark_node;
3100 inner_nelts_count = result;
3102 else
3104 if (complain & tf_error)
3106 error_at (cp_expr_loc_or_input_loc (inner_nelts),
3107 "array size in new-expression must be constant");
3108 cxx_constant_value(inner_nelts);
3110 nelts = error_mark_node;
3112 if (nelts != error_mark_node)
3113 nelts = cp_build_binary_op (input_location,
3114 MULT_EXPR, nelts,
3115 inner_nelts_cst,
3116 complain);
3119 if (!verify_type_context (input_location, TCTX_ALLOCATION, elt_type,
3120 !(complain & tf_error)))
3121 return error_mark_node;
3123 if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
3125 error ("variably modified type not allowed in new-expression");
3126 return error_mark_node;
3129 if (nelts == error_mark_node)
3130 return error_mark_node;
3132 /* Warn if we performed the (T[N]) to T[N] transformation and N is
3133 variable. */
3134 if (outer_nelts_from_type
3135 && !TREE_CONSTANT (cst_outer_nelts))
3137 if (complain & tf_warning_or_error)
3139 pedwarn (cp_expr_loc_or_input_loc (outer_nelts), OPT_Wvla,
3140 typedef_variant_p (orig_type)
3141 ? G_("non-constant array new length must be specified "
3142 "directly, not by %<typedef%>")
3143 : G_("non-constant array new length must be specified "
3144 "without parentheses around the type-id"));
3146 else
3147 return error_mark_node;
3150 if (VOID_TYPE_P (elt_type))
3152 if (complain & tf_error)
3153 error ("invalid type %<void%> for %<new%>");
3154 return error_mark_node;
3157 if (is_std_init_list (elt_type) && !cp_unevaluated_operand)
3158 warning (OPT_Winit_list_lifetime,
3159 "%<new%> of %<initializer_list%> does not "
3160 "extend the lifetime of the underlying array");
3162 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain))
3163 return error_mark_node;
3165 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
3167 if (*init == NULL && cxx_dialect < cxx11)
3169 bool maybe_uninitialized_error = false;
3170 /* A program that calls for default-initialization [...] of an
3171 entity of reference type is ill-formed. */
3172 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
3173 maybe_uninitialized_error = true;
3175 /* A new-expression that creates an object of type T initializes
3176 that object as follows:
3177 - If the new-initializer is omitted:
3178 -- If T is a (possibly cv-qualified) non-POD class type
3179 (or array thereof), the object is default-initialized (8.5).
3180 [...]
3181 -- Otherwise, the object created has indeterminate
3182 value. If T is a const-qualified type, or a (possibly
3183 cv-qualified) POD class type (or array thereof)
3184 containing (directly or indirectly) a member of
3185 const-qualified type, the program is ill-formed; */
3187 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
3188 maybe_uninitialized_error = true;
3190 if (maybe_uninitialized_error
3191 && diagnose_uninitialized_cst_or_ref_member (elt_type,
3192 /*using_new=*/true,
3193 complain & tf_error))
3194 return error_mark_node;
3197 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
3198 && default_init_uninitialized_part (elt_type))
3200 if (complain & tf_error)
3201 error ("uninitialized const in %<new%> of %q#T", elt_type);
3202 return error_mark_node;
3205 size = size_in_bytes (elt_type);
3206 if (array_p)
3208 /* Maximum available size in bytes. Half of the address space
3209 minus the cookie size. */
3210 offset_int max_size
3211 = wi::set_bit_in_zero <offset_int> (TYPE_PRECISION (sizetype) - 1);
3212 /* Maximum number of outer elements which can be allocated. */
3213 offset_int max_outer_nelts;
3214 tree max_outer_nelts_tree;
3216 gcc_assert (TREE_CODE (size) == INTEGER_CST);
3217 cookie_size = targetm.cxx.get_cookie_size (elt_type);
3218 gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
3219 gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size));
3220 /* Unconditionally subtract the cookie size. This decreases the
3221 maximum object size and is safe even if we choose not to use
3222 a cookie after all. */
3223 max_size -= wi::to_offset (cookie_size);
3224 wi::overflow_type overflow;
3225 inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED,
3226 &overflow);
3227 if (overflow || wi::gtu_p (inner_size, max_size))
3229 if (complain & tf_error)
3231 cst_size_error error;
3232 if (overflow)
3233 error = cst_size_overflow;
3234 else
3236 error = cst_size_too_big;
3237 size = size_binop (MULT_EXPR, size,
3238 wide_int_to_tree (sizetype,
3239 inner_nelts_count));
3240 size = cp_fully_fold (size);
3242 invalid_array_size_error (input_location, error, size,
3243 /*name=*/NULL_TREE);
3245 return error_mark_node;
3248 max_outer_nelts = wi::udiv_trunc (max_size, inner_size);
3249 max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts);
3251 size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts));
3253 if (TREE_CODE (cst_outer_nelts) == INTEGER_CST)
3255 if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts))
3257 /* When the array size is constant, check it at compile time
3258 to make sure it doesn't exceed the implementation-defined
3259 maximum, as required by C++ 14 (in C++ 11 this requirement
3260 isn't explicitly stated but it's enforced anyway -- see
3261 grokdeclarator in cp/decl.cc). */
3262 if (complain & tf_error)
3264 size = cp_fully_fold (size);
3265 invalid_array_size_error (input_location, cst_size_too_big,
3266 size, NULL_TREE);
3268 return error_mark_node;
3271 else
3273 /* When a runtime check is necessary because the array size
3274 isn't constant, keep only the top-most seven bits (starting
3275 with the most significant non-zero bit) of the maximum size
3276 to compare the array size against, to simplify encoding the
3277 constant maximum size in the instruction stream. */
3279 unsigned shift = (max_outer_nelts.get_precision ()) - 7
3280 - wi::clz (max_outer_nelts);
3281 max_outer_nelts = (max_outer_nelts >> shift) << shift;
3283 outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
3284 outer_nelts,
3285 max_outer_nelts_tree);
3289 tree align_arg = NULL_TREE;
3290 if (type_has_new_extended_alignment (elt_type))
3292 unsigned align = TYPE_ALIGN_UNIT (elt_type);
3293 /* Also consider the alignment of the cookie, if any. */
3294 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
3295 align = MAX (align, TYPE_ALIGN_UNIT (size_type_node));
3296 align_arg = build_int_cst (align_type_node, align);
3299 alloc_fn = NULL_TREE;
3301 /* If PLACEMENT is a single simple pointer type not passed by
3302 reference, prepare to capture it in a temporary variable. Do
3303 this now, since PLACEMENT will change in the calls below. */
3304 placement_first = NULL_TREE;
3305 if (vec_safe_length (*placement) == 1
3306 && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
3307 placement_first = (**placement)[0];
3309 bool member_new_p = false;
3311 /* Allocate the object. */
3312 tree fnname;
3313 tree fns;
3315 fnname = ovl_op_identifier (false, array_p ? VEC_NEW_EXPR : NEW_EXPR);
3317 member_new_p = !globally_qualified_p
3318 && CLASS_TYPE_P (elt_type)
3319 && (array_p
3320 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
3321 : TYPE_HAS_NEW_OPERATOR (elt_type));
3323 bool member_delete_p = (!globally_qualified_p
3324 && CLASS_TYPE_P (elt_type)
3325 && (array_p
3326 ? TYPE_GETS_VEC_DELETE (elt_type)
3327 : TYPE_GETS_REG_DELETE (elt_type)));
3329 if (member_new_p)
3331 /* Use a class-specific operator new. */
3332 /* If a cookie is required, add some extra space. */
3333 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
3334 size = size_binop (PLUS_EXPR, size, cookie_size);
3335 else
3337 cookie_size = NULL_TREE;
3338 /* No size arithmetic necessary, so the size check is
3339 not needed. */
3340 if (outer_nelts_check != NULL && inner_size == 1)
3341 outer_nelts_check = NULL_TREE;
3343 /* Perform the overflow check. */
3344 tree errval = TYPE_MAX_VALUE (sizetype);
3345 if (cxx_dialect >= cxx11 && flag_exceptions)
3346 errval = throw_bad_array_new_length ();
3347 if (outer_nelts_check != NULL_TREE)
3348 size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
3349 size, errval);
3350 /* Create the argument list. */
3351 vec_safe_insert (*placement, 0, size);
3352 /* Do name-lookup to find the appropriate operator. */
3353 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2, complain);
3354 if (fns == NULL_TREE)
3356 if (complain & tf_error)
3357 error ("no suitable %qD found in class %qT", fnname, elt_type);
3358 return error_mark_node;
3360 if (TREE_CODE (fns) == TREE_LIST)
3362 if (complain & tf_error)
3364 error ("request for member %qD is ambiguous", fnname);
3365 print_candidates (fns);
3367 return error_mark_node;
3369 tree dummy = build_dummy_object (elt_type);
3370 alloc_call = NULL_TREE;
3371 if (align_arg)
3373 vec<tree, va_gc> *align_args
3374 = vec_copy_and_insert (*placement, align_arg, 1);
3375 alloc_call
3376 = build_new_method_call (dummy, fns, &align_args,
3377 /*conversion_path=*/NULL_TREE,
3378 LOOKUP_NORMAL, &alloc_fn, tf_none);
3379 /* If no matching function is found and the allocated object type
3380 has new-extended alignment, the alignment argument is removed
3381 from the argument list, and overload resolution is performed
3382 again. */
3383 if (alloc_call == error_mark_node)
3384 alloc_call = NULL_TREE;
3386 if (!alloc_call)
3387 alloc_call = build_new_method_call (dummy, fns, placement,
3388 /*conversion_path=*/NULL_TREE,
3389 LOOKUP_NORMAL,
3390 &alloc_fn, complain);
3392 else
3394 /* Use a global operator new. */
3395 /* See if a cookie might be required. */
3396 if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
3398 cookie_size = NULL_TREE;
3399 /* No size arithmetic necessary, so the size check is
3400 not needed. */
3401 if (outer_nelts_check != NULL && inner_size == 1)
3402 outer_nelts_check = NULL_TREE;
3405 /* If size is zero e.g. due to type having zero size, try to
3406 preserve outer_nelts for constant expression evaluation
3407 purposes. */
3408 if (integer_zerop (size) && outer_nelts)
3409 size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts);
3411 alloc_call = build_operator_new_call (fnname, placement,
3412 &size, &cookie_size,
3413 align_arg, outer_nelts_check,
3414 &alloc_fn, complain);
3417 if (alloc_call == error_mark_node)
3418 return error_mark_node;
3420 gcc_assert (alloc_fn != NULL_TREE);
3422 /* Now, check to see if this function is actually a placement
3423 allocation function. This can happen even when PLACEMENT is NULL
3424 because we might have something like:
3426 struct S { void* operator new (size_t, int i = 0); };
3428 A call to `new S' will get this allocation function, even though
3429 there is no explicit placement argument. If there is more than
3430 one argument, or there are variable arguments, then this is a
3431 placement allocation function. */
3432 placement_allocation_fn_p
3433 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
3434 || varargs_function_p (alloc_fn));
3436 if (complain & tf_warning_or_error
3437 && warn_aligned_new
3438 && !placement_allocation_fn_p
3439 && TYPE_ALIGN (elt_type) > malloc_alignment ()
3440 && (warn_aligned_new > 1
3441 || CP_DECL_CONTEXT (alloc_fn) == global_namespace)
3442 && !aligned_allocation_fn_p (alloc_fn))
3444 auto_diagnostic_group d;
3445 if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended "
3446 "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type)))
3448 inform (input_location, "uses %qD, which does not have an alignment "
3449 "parameter", alloc_fn);
3450 if (!aligned_new_threshold)
3451 inform (input_location, "use %<-faligned-new%> to enable C++17 "
3452 "over-aligned new support");
3456 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
3457 into a temporary variable. */
3458 if (!processing_template_decl
3459 && TREE_CODE (alloc_call) == CALL_EXPR
3460 && call_expr_nargs (alloc_call) == 2
3461 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
3462 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
3464 tree placement = CALL_EXPR_ARG (alloc_call, 1);
3466 if (placement_first != NULL_TREE
3467 && (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))
3468 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))))
3470 placement_expr = get_target_expr (placement_first);
3471 CALL_EXPR_ARG (alloc_call, 1)
3472 = fold_convert (TREE_TYPE (placement), placement_expr);
3475 if (!member_new_p
3476 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1)))))
3478 /* Attempt to make the warning point at the operator new argument. */
3479 if (placement_first)
3480 placement = placement_first;
3482 warn_placement_new_too_small (orig_type, nelts, size, placement);
3486 alloc_expr = alloc_call;
3487 if (cookie_size)
3488 alloc_expr = maybe_wrap_new_for_constexpr (alloc_expr, type,
3489 cookie_size);
3491 /* In the simple case, we can stop now. */
3492 pointer_type = build_pointer_type (type);
3493 if (!cookie_size && !is_initialized && !member_delete_p)
3494 return build_nop (pointer_type, alloc_expr);
3496 /* Store the result of the allocation call in a variable so that we can
3497 use it more than once. */
3498 alloc_expr = get_target_expr (alloc_expr);
3499 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
3501 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
3502 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
3503 alloc_call = TREE_OPERAND (alloc_call, 1);
3505 /* Preevaluate the placement args so that we don't reevaluate them for a
3506 placement delete. */
3507 if (placement_allocation_fn_p)
3509 tree inits;
3510 stabilize_call (alloc_call, &inits);
3511 if (inits)
3512 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
3513 alloc_expr);
3516 /* unless an allocation function is declared with an empty excep-
3517 tion-specification (_except.spec_), throw(), it indicates failure to
3518 allocate storage by throwing a bad_alloc exception (clause _except_,
3519 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
3520 cation function is declared with an empty exception-specification,
3521 throw(), it returns null to indicate failure to allocate storage and a
3522 non-null pointer otherwise.
3524 So check for a null exception spec on the op new we just called. */
3526 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
3527 check_new
3528 = flag_check_new || (nothrow && !std_placement_new_fn_p (alloc_fn));
3530 if (cookie_size)
3532 tree cookie;
3533 tree cookie_ptr;
3534 tree size_ptr_type;
3536 /* Adjust so we're pointing to the start of the object. */
3537 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
3539 /* Store the number of bytes allocated so that we can know how
3540 many elements to destroy later. We use the last sizeof
3541 (size_t) bytes to store the number of elements. */
3542 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
3543 cookie_ptr = fold_build_pointer_plus_loc (input_location,
3544 alloc_node, cookie_ptr);
3545 size_ptr_type = build_pointer_type (sizetype);
3546 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
3547 cookie = cp_build_fold_indirect_ref (cookie_ptr);
3549 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
3551 if (targetm.cxx.cookie_has_size ())
3553 /* Also store the element size. */
3554 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
3555 fold_build1_loc (input_location,
3556 NEGATE_EXPR, sizetype,
3557 size_in_bytes (sizetype)));
3559 cookie = cp_build_fold_indirect_ref (cookie_ptr);
3560 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
3561 size_in_bytes (elt_type));
3562 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
3563 cookie, cookie_expr);
3566 else
3568 cookie_expr = NULL_TREE;
3569 data_addr = alloc_node;
3572 /* Now use a pointer to the type we've actually allocated. */
3574 /* But we want to operate on a non-const version to start with,
3575 since we'll be modifying the elements. */
3576 non_const_pointer_type = build_pointer_type
3577 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
3579 data_addr = fold_convert (non_const_pointer_type, data_addr);
3580 /* Any further uses of alloc_node will want this type, too. */
3581 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
3583 /* Now initialize the allocated object. Note that we preevaluate the
3584 initialization expression, apart from the actual constructor call or
3585 assignment--we do this because we want to delay the allocation as long
3586 as possible in order to minimize the size of the exception region for
3587 placement delete. */
3588 if (is_initialized)
3590 bool explicit_value_init_p = false;
3592 if (*init != NULL && (*init)->is_empty ())
3594 *init = NULL;
3595 explicit_value_init_p = true;
3598 if (processing_template_decl)
3600 /* Avoid an ICE when converting to a base in build_simple_base_path.
3601 We'll throw this all away anyway, and build_new will create
3602 a NEW_EXPR. */
3603 tree t = fold_convert (build_pointer_type (elt_type), data_addr);
3604 /* build_value_init doesn't work in templates, and we don't need
3605 the initializer anyway since we're going to throw it away and
3606 rebuild it at instantiation time, so just build up a single
3607 constructor call to get any appropriate diagnostics. */
3608 init_expr = cp_build_fold_indirect_ref (t);
3609 if (type_build_ctor_call (elt_type))
3610 init_expr = build_special_member_call (init_expr,
3611 complete_ctor_identifier,
3612 init, elt_type,
3613 LOOKUP_NORMAL,
3614 complain);
3616 else if (array_p)
3618 tree vecinit = NULL_TREE;
3619 const size_t len = vec_safe_length (*init);
3620 if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0]))
3622 vecinit = (**init)[0];
3623 if (CONSTRUCTOR_NELTS (vecinit) == 0)
3624 /* List-value-initialization, leave it alone. */;
3625 else
3627 tree arraytype, domain;
3628 if (TREE_CONSTANT (nelts))
3629 domain = compute_array_index_type (NULL_TREE, nelts,
3630 complain);
3631 else
3632 /* We'll check the length at runtime. */
3633 domain = NULL_TREE;
3634 arraytype = build_cplus_array_type (type, domain);
3635 /* If we have new char[4]{"foo"}, we have to reshape
3636 so that the STRING_CST isn't wrapped in { }. */
3637 vecinit = reshape_init (arraytype, vecinit, complain);
3638 /* The middle end doesn't cope with the location wrapper
3639 around a STRING_CST. */
3640 STRIP_ANY_LOCATION_WRAPPER (vecinit);
3641 vecinit = digest_init (arraytype, vecinit, complain);
3644 else if (*init)
3646 if (complain & tf_error)
3647 error ("parenthesized initializer in array new");
3648 return error_mark_node;
3650 init_expr
3651 = build_vec_init (data_addr,
3652 cp_build_binary_op (input_location,
3653 MINUS_EXPR, outer_nelts,
3654 integer_one_node,
3655 complain),
3656 vecinit,
3657 explicit_value_init_p,
3658 /*from_array=*/0,
3659 complain);
3661 else
3663 init_expr = cp_build_fold_indirect_ref (data_addr);
3665 if (type_build_ctor_call (type) && !explicit_value_init_p)
3667 init_expr = build_special_member_call (init_expr,
3668 complete_ctor_identifier,
3669 init, elt_type,
3670 LOOKUP_NORMAL,
3671 complain|tf_no_cleanup);
3673 else if (explicit_value_init_p)
3675 /* Something like `new int()'. NO_CLEANUP is needed so
3676 we don't try and build a (possibly ill-formed)
3677 destructor. */
3678 tree val = build_value_init (type, complain | tf_no_cleanup);
3679 if (val == error_mark_node)
3680 return error_mark_node;
3681 init_expr = build2 (INIT_EXPR, type, init_expr, val);
3683 else
3685 tree ie;
3687 /* We are processing something like `new int (10)', which
3688 means allocate an int, and initialize it with 10.
3690 In C++20, also handle `new A(1, 2)'. */
3691 if (cxx_dialect >= cxx20
3692 && AGGREGATE_TYPE_P (type)
3693 && (*init)->length () > 1)
3695 ie = build_constructor_from_vec (init_list_type_node, *init);
3696 CONSTRUCTOR_IS_DIRECT_INIT (ie) = true;
3697 CONSTRUCTOR_IS_PAREN_INIT (ie) = true;
3698 ie = digest_init (type, ie, complain);
3700 else
3701 ie = build_x_compound_expr_from_vec (*init, "new initializer",
3702 complain);
3703 init_expr = cp_build_modify_expr (input_location, init_expr,
3704 INIT_EXPR, ie, complain);
3706 /* If the initializer uses C++14 aggregate NSDMI that refer to the
3707 object being initialized, replace them now and don't try to
3708 preevaluate. */
3709 bool had_placeholder = false;
3710 if (!processing_template_decl
3711 && TREE_CODE (init_expr) == INIT_EXPR)
3712 TREE_OPERAND (init_expr, 1)
3713 = replace_placeholders (TREE_OPERAND (init_expr, 1),
3714 TREE_OPERAND (init_expr, 0),
3715 &had_placeholder);
3718 if (init_expr == error_mark_node)
3719 return error_mark_node;
3721 else
3722 init_expr = NULL_TREE;
3724 /* If any part of the object initialization terminates by throwing an
3725 exception and a suitable deallocation function can be found, the
3726 deallocation function is called to free the memory in which the
3727 object was being constructed, after which the exception continues
3728 to propagate in the context of the new-expression. If no
3729 unambiguous matching deallocation function can be found,
3730 propagating the exception does not cause the object's memory to be
3731 freed. */
3732 if (flag_exceptions && (init_expr || member_delete_p))
3734 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
3735 tree cleanup;
3737 /* The Standard is unclear here, but the right thing to do
3738 is to use the same method for finding deallocation
3739 functions that we use for finding allocation functions. */
3740 cleanup = (build_op_delete_call
3741 (dcode,
3742 alloc_node,
3743 size,
3744 globally_qualified_p,
3745 placement_allocation_fn_p ? alloc_call : NULL_TREE,
3746 alloc_fn,
3747 complain));
3749 if (cleanup && init_expr && !processing_template_decl)
3750 /* Ack! First we allocate the memory. Then we set our sentry
3751 variable to true, and expand a cleanup that deletes the
3752 memory if sentry is true. Then we run the constructor, and
3753 finally clear the sentry.
3755 We need to do this because we allocate the space first, so
3756 if there are any temporaries with cleanups in the
3757 constructor args, we need this EH region to extend until
3758 end of full-expression to preserve nesting.
3760 We used to try to evaluate the args first to avoid this, but
3761 since C++17 [expr.new] says that "The invocation of the
3762 allocation function is sequenced before the evaluations of
3763 expressions in the new-initializer." */
3765 tree end, sentry, begin;
3767 begin = get_target_expr (boolean_true_node);
3768 CLEANUP_EH_ONLY (begin) = 1;
3770 sentry = TARGET_EXPR_SLOT (begin);
3772 /* CLEANUP is compiler-generated, so no diagnostics. */
3773 suppress_warning (cleanup);
3775 TARGET_EXPR_CLEANUP (begin)
3776 = build3 (COND_EXPR, void_type_node, sentry,
3777 cleanup, void_node);
3779 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
3780 sentry, boolean_false_node);
3782 init_expr
3783 = build2 (COMPOUND_EXPR, void_type_node, begin,
3784 build2 (COMPOUND_EXPR, void_type_node, init_expr,
3785 end));
3786 /* Likewise, this is compiler-generated. */
3787 suppress_warning (init_expr);
3791 /* Now build up the return value in reverse order. */
3793 rval = data_addr;
3795 if (init_expr)
3796 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
3797 if (cookie_expr)
3798 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
3800 if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
3801 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
3802 and return the call (which doesn't need to be adjusted). */
3803 rval = TARGET_EXPR_INITIAL (alloc_expr);
3804 else
3806 if (check_new)
3808 tree ifexp = cp_build_binary_op (input_location,
3809 NE_EXPR, alloc_node,
3810 nullptr_node,
3811 complain);
3812 rval = build_conditional_expr (input_location, ifexp, rval,
3813 alloc_node, complain);
3816 /* Perform the allocation before anything else, so that ALLOC_NODE
3817 has been initialized before we start using it. */
3818 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
3821 /* A new-expression is never an lvalue. */
3822 gcc_assert (!obvalue_p (rval));
3824 return convert (pointer_type, rval);
3827 /* Generate a representation for a C++ "new" expression. *PLACEMENT
3828 is a vector of placement-new arguments (or NULL if none). If NELTS
3829 is NULL, TYPE is the type of the storage to be allocated. If NELTS
3830 is not NULL, then this is an array-new allocation; TYPE is the type
3831 of the elements in the array and NELTS is the number of elements in
3832 the array. *INIT, if non-NULL, is the initializer for the new
3833 object, or an empty vector to indicate an initializer of "()". If
3834 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
3835 rather than just "new". This may change PLACEMENT and INIT. */
3837 tree
3838 build_new (location_t loc, vec<tree, va_gc> **placement, tree type,
3839 tree nelts, vec<tree, va_gc> **init, int use_global_new,
3840 tsubst_flags_t complain)
3842 tree rval;
3843 vec<tree, va_gc> *orig_placement = NULL;
3844 tree orig_nelts = NULL_TREE;
3845 vec<tree, va_gc> *orig_init = NULL;
3847 if (type == error_mark_node)
3848 return error_mark_node;
3850 if (nelts == NULL_TREE
3851 /* Don't do auto deduction where it might affect mangling. */
3852 && (!processing_template_decl || at_function_scope_p ()))
3854 tree auto_node = type_uses_auto (type);
3855 if (auto_node)
3857 tree d_init = NULL_TREE;
3858 const size_t len = vec_safe_length (*init);
3859 /* E.g. new auto(x) must have exactly one element, or
3860 a {} initializer will have one element. */
3861 if (len == 1)
3863 d_init = (**init)[0];
3864 d_init = resolve_nondeduced_context (d_init, complain);
3866 /* For the rest, e.g. new A(1, 2, 3), create a list. */
3867 else if (len > 1)
3869 unsigned int n;
3870 tree t;
3871 tree *pp = &d_init;
3872 FOR_EACH_VEC_ELT (**init, n, t)
3874 t = resolve_nondeduced_context (t, complain);
3875 *pp = build_tree_list (NULL_TREE, t);
3876 pp = &TREE_CHAIN (*pp);
3879 type = do_auto_deduction (type, d_init, auto_node, complain);
3883 if (processing_template_decl)
3885 if (dependent_type_p (type)
3886 || any_type_dependent_arguments_p (*placement)
3887 || (nelts && type_dependent_expression_p (nelts))
3888 || (nelts && *init)
3889 || any_type_dependent_arguments_p (*init))
3890 return build_raw_new_expr (loc, *placement, type, nelts, *init,
3891 use_global_new);
3893 orig_placement = make_tree_vector_copy (*placement);
3894 orig_nelts = nelts;
3895 if (*init)
3897 orig_init = make_tree_vector_copy (*init);
3898 /* Also copy any CONSTRUCTORs in *init, since reshape_init and
3899 digest_init clobber them in place. */
3900 for (unsigned i = 0; i < orig_init->length(); ++i)
3902 tree e = (**init)[i];
3903 if (TREE_CODE (e) == CONSTRUCTOR)
3904 (**init)[i] = copy_node (e);
3908 make_args_non_dependent (*placement);
3909 if (nelts)
3910 nelts = build_non_dependent_expr (nelts);
3911 make_args_non_dependent (*init);
3914 if (nelts)
3916 location_t nelts_loc = cp_expr_loc_or_loc (nelts, loc);
3917 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
3919 if (complain & tf_error)
3920 permerror (nelts_loc,
3921 "size in array new must have integral type");
3922 else
3923 return error_mark_node;
3926 /* Try to determine the constant value only for the purposes
3927 of the diagnostic below but continue to use the original
3928 value and handle const folding later. */
3929 const_tree cst_nelts = fold_non_dependent_expr (nelts, complain);
3931 /* The expression in a noptr-new-declarator is erroneous if it's of
3932 non-class type and its value before converting to std::size_t is
3933 less than zero. ... If the expression is a constant expression,
3934 the program is ill-fomed. */
3935 if (TREE_CODE (cst_nelts) == INTEGER_CST
3936 && !valid_array_size_p (nelts_loc, cst_nelts, NULL_TREE,
3937 complain & tf_error))
3938 return error_mark_node;
3940 nelts = mark_rvalue_use (nelts);
3941 nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
3944 /* ``A reference cannot be created by the new operator. A reference
3945 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3946 returned by new.'' ARM 5.3.3 */
3947 if (TYPE_REF_P (type))
3949 if (complain & tf_error)
3950 error_at (loc, "new cannot be applied to a reference type");
3951 else
3952 return error_mark_node;
3953 type = TREE_TYPE (type);
3956 if (TREE_CODE (type) == FUNCTION_TYPE)
3958 if (complain & tf_error)
3959 error_at (loc, "new cannot be applied to a function type");
3960 return error_mark_node;
3963 /* P1009: Array size deduction in new-expressions. */
3964 const bool array_p = TREE_CODE (type) == ARRAY_TYPE;
3965 if (*init
3966 /* If ARRAY_P, we have to deduce the array bound. For C++20 paren-init,
3967 we have to process the parenthesized-list. But don't do it for (),
3968 which is value-initialization, and INIT should stay empty. */
3969 && (array_p || (cxx_dialect >= cxx20 && nelts && !(*init)->is_empty ())))
3971 /* This means we have 'new T[]()'. */
3972 if ((*init)->is_empty ())
3974 tree ctor = build_constructor (init_list_type_node, NULL);
3975 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
3976 vec_safe_push (*init, ctor);
3978 tree &elt = (**init)[0];
3979 /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */
3980 if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20)
3982 tree ctor = build_constructor_from_vec (init_list_type_node, *init);
3983 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
3984 CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
3985 elt = ctor;
3986 /* We've squashed all the vector elements into the first one;
3987 truncate the rest. */
3988 (*init)->truncate (1);
3990 /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */
3991 if (array_p && !TYPE_DOMAIN (type))
3993 /* We need to reshape before deducing the bounds to handle code like
3995 struct S { int x, y; };
3996 new S[]{1, 2, 3, 4};
3998 which should deduce S[2]. But don't change ELT itself: we want to
3999 pass a list-initializer to build_new_1, even for STRING_CSTs. */
4000 tree e = elt;
4001 if (BRACE_ENCLOSED_INITIALIZER_P (e))
4002 e = reshape_init (type, e, complain);
4003 cp_complete_array_type (&type, e, /*do_default*/false);
4007 /* The type allocated must be complete. If the new-type-id was
4008 "T[N]" then we are just checking that "T" is complete here, but
4009 that is equivalent, since the value of "N" doesn't matter. */
4010 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
4011 return error_mark_node;
4013 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
4014 if (rval == error_mark_node)
4015 return error_mark_node;
4017 if (processing_template_decl)
4019 tree ret = build_raw_new_expr (loc, orig_placement, type, orig_nelts,
4020 orig_init, use_global_new);
4021 release_tree_vector (orig_placement);
4022 release_tree_vector (orig_init);
4023 return ret;
4026 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
4027 rval = build1_loc (loc, NOP_EXPR, TREE_TYPE (rval), rval);
4028 suppress_warning (rval, OPT_Wunused_value);
4030 return rval;
4033 static tree
4034 build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
4035 special_function_kind auto_delete_vec,
4036 int use_global_delete, tsubst_flags_t complain,
4037 bool in_cleanup = false)
4039 tree virtual_size;
4040 tree ptype = build_pointer_type (type = complete_type (type));
4041 tree size_exp;
4043 /* Temporary variables used by the loop. */
4044 tree tbase, tbase_init;
4046 /* This is the body of the loop that implements the deletion of a
4047 single element, and moves temp variables to next elements. */
4048 tree body;
4050 /* This is the LOOP_EXPR that governs the deletion of the elements. */
4051 tree loop = 0;
4053 /* This is the thing that governs what to do after the loop has run. */
4054 tree deallocate_expr = 0;
4056 /* This is the BIND_EXPR which holds the outermost iterator of the
4057 loop. It is convenient to set this variable up and test it before
4058 executing any other code in the loop.
4059 This is also the containing expression returned by this function. */
4060 tree controller = NULL_TREE;
4061 tree tmp;
4063 /* We should only have 1-D arrays here. */
4064 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
4066 if (base == error_mark_node || maxindex == error_mark_node)
4067 return error_mark_node;
4069 if (!verify_type_context (loc, TCTX_DEALLOCATION, type,
4070 !(complain & tf_error)))
4071 return error_mark_node;
4073 if (!COMPLETE_TYPE_P (type))
4075 if (complain & tf_warning)
4077 auto_diagnostic_group d;
4078 if (warning_at (loc, OPT_Wdelete_incomplete,
4079 "possible problem detected in invocation of "
4080 "operator %<delete []%>"))
4082 cxx_incomplete_type_diagnostic (base, type, DK_WARNING);
4083 inform (loc, "neither the destructor nor the "
4084 "class-specific operator %<delete []%> will be called, "
4085 "even if they are declared when the class is defined");
4088 /* This size won't actually be used. */
4089 size_exp = size_one_node;
4090 goto no_destructor;
4093 size_exp = size_in_bytes (type);
4095 if (! MAYBE_CLASS_TYPE_P (type))
4096 goto no_destructor;
4097 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
4099 /* Make sure the destructor is callable. */
4100 if (type_build_dtor_call (type))
4102 tmp = build_delete (loc, ptype, base, sfk_complete_destructor,
4103 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
4104 complain);
4105 if (tmp == error_mark_node)
4106 return error_mark_node;
4108 goto no_destructor;
4111 /* The below is short by the cookie size. */
4112 virtual_size = size_binop (MULT_EXPR, size_exp,
4113 fold_convert (sizetype, maxindex));
4115 tbase = create_temporary_var (ptype);
4116 DECL_INITIAL (tbase)
4117 = fold_build_pointer_plus_loc (loc, fold_convert (ptype, base),
4118 virtual_size);
4119 tbase_init = build_stmt (loc, DECL_EXPR, tbase);
4120 controller = build3 (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
4121 TREE_SIDE_EFFECTS (controller) = 1;
4122 BIND_EXPR_VEC_DTOR (controller) = true;
4124 body = build1 (EXIT_EXPR, void_type_node,
4125 build2 (EQ_EXPR, boolean_type_node, tbase,
4126 fold_convert (ptype, base)));
4127 tmp = fold_build1_loc (loc, NEGATE_EXPR, sizetype, size_exp);
4128 tmp = fold_build_pointer_plus (tbase, tmp);
4129 tmp = cp_build_modify_expr (loc, tbase, NOP_EXPR, tmp, complain);
4130 if (tmp == error_mark_node)
4131 return error_mark_node;
4132 body = build_compound_expr (loc, body, tmp);
4133 tmp = build_delete (loc, ptype, tbase, sfk_complete_destructor,
4134 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
4135 complain);
4136 if (tmp == error_mark_node)
4137 return error_mark_node;
4138 body = build_compound_expr (loc, body, tmp);
4140 loop = build1 (LOOP_EXPR, void_type_node, body);
4142 /* If one destructor throws, keep trying to clean up the rest, unless we're
4143 already in a build_vec_init cleanup. */
4144 if (flag_exceptions && !in_cleanup && !expr_noexcept_p (tmp, tf_none))
4146 loop = build2 (TRY_CATCH_EXPR, void_type_node, loop,
4147 unshare_expr (loop));
4148 /* Tell honor_protect_cleanup_actions to discard this on the
4149 exceptional path. */
4150 TRY_CATCH_IS_CLEANUP (loop) = true;
4153 loop = build_compound_expr (loc, tbase_init, loop);
4155 no_destructor:
4156 /* Delete the storage if appropriate. */
4157 if (auto_delete_vec == sfk_deleting_destructor)
4159 tree base_tbd;
4161 /* The below is short by the cookie size. */
4162 virtual_size = size_binop (MULT_EXPR, size_exp,
4163 fold_convert (sizetype, maxindex));
4165 if (! TYPE_VEC_NEW_USES_COOKIE (type))
4166 /* no header */
4167 base_tbd = base;
4168 else
4170 tree cookie_size;
4172 cookie_size = targetm.cxx.get_cookie_size (type);
4173 base_tbd = cp_build_binary_op (loc,
4174 MINUS_EXPR,
4175 cp_convert (string_type_node,
4176 base, complain),
4177 cookie_size,
4178 complain);
4179 if (base_tbd == error_mark_node)
4180 return error_mark_node;
4181 base_tbd = cp_convert (ptype, base_tbd, complain);
4182 /* True size with header. */
4183 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
4186 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
4187 base_tbd, virtual_size,
4188 use_global_delete & 1,
4189 /*placement=*/NULL_TREE,
4190 /*alloc_fn=*/NULL_TREE,
4191 complain);
4194 body = loop;
4195 if (deallocate_expr == error_mark_node)
4196 return error_mark_node;
4197 else if (!deallocate_expr)
4199 else if (!body)
4200 body = deallocate_expr;
4201 else
4202 /* The delete operator must be called, even if a destructor
4203 throws. */
4204 body = build2 (TRY_FINALLY_EXPR, void_type_node, body, deallocate_expr);
4206 if (!body)
4207 body = integer_zero_node;
4209 /* Outermost wrapper: If pointer is null, punt. */
4210 tree cond = build2_loc (loc, NE_EXPR, boolean_type_node, base,
4211 fold_convert (TREE_TYPE (base), nullptr_node));
4212 /* This is a compiler generated comparison, don't emit
4213 e.g. -Wnonnull-compare warning for it. */
4214 suppress_warning (cond, OPT_Wnonnull_compare);
4215 body = build3_loc (loc, COND_EXPR, void_type_node,
4216 cond, body, integer_zero_node);
4217 COND_EXPR_IS_VEC_DELETE (body) = true;
4218 body = build1 (NOP_EXPR, void_type_node, body);
4220 if (controller)
4222 TREE_OPERAND (controller, 1) = body;
4223 body = controller;
4226 if (TREE_CODE (base) == SAVE_EXPR)
4227 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
4228 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
4230 return convert_to_void (body, ICV_CAST, complain);
4233 /* Create an unnamed variable of the indicated TYPE. */
4235 tree
4236 create_temporary_var (tree type)
4238 tree decl;
4240 decl = build_decl (input_location,
4241 VAR_DECL, NULL_TREE, type);
4242 TREE_USED (decl) = 1;
4243 DECL_ARTIFICIAL (decl) = 1;
4244 DECL_IGNORED_P (decl) = 1;
4245 DECL_CONTEXT (decl) = current_function_decl;
4247 return decl;
4250 /* Create a new temporary variable of the indicated TYPE, initialized
4251 to INIT.
4253 It is not entered into current_binding_level, because that breaks
4254 things when it comes time to do final cleanups (which take place
4255 "outside" the binding contour of the function). */
4257 tree
4258 get_temp_regvar (tree type, tree init)
4260 tree decl;
4262 decl = create_temporary_var (type);
4263 add_decl_expr (decl);
4265 finish_expr_stmt (cp_build_modify_expr (input_location, decl, INIT_EXPR,
4266 init, tf_warning_or_error));
4268 return decl;
4271 /* Subroutine of build_vec_init. Returns true if assigning to an array of
4272 INNER_ELT_TYPE from INIT is trivial. */
4274 static bool
4275 vec_copy_assign_is_trivial (tree inner_elt_type, tree init)
4277 tree fromtype = inner_elt_type;
4278 if (lvalue_p (init))
4279 fromtype = cp_build_reference_type (fromtype, /*rval*/false);
4280 return is_trivially_xible (MODIFY_EXPR, inner_elt_type, fromtype);
4283 /* Subroutine of build_vec_init: Check that the array has at least N
4284 elements. Other parameters are local variables in build_vec_init. */
4286 void
4287 finish_length_check (tree atype, tree iterator, tree obase, unsigned n)
4289 tree nelts = build_int_cst (ptrdiff_type_node, n - 1);
4290 if (TREE_CODE (atype) != ARRAY_TYPE)
4292 if (flag_exceptions)
4294 tree c = fold_build2 (LT_EXPR, boolean_type_node, iterator,
4295 nelts);
4296 c = build3 (COND_EXPR, void_type_node, c,
4297 throw_bad_array_new_length (), void_node);
4298 finish_expr_stmt (c);
4300 /* Don't check an array new when -fno-exceptions. */
4302 else if (sanitize_flags_p (SANITIZE_BOUNDS)
4303 && current_function_decl != NULL_TREE)
4305 /* Make sure the last element of the initializer is in bounds. */
4306 finish_expr_stmt
4307 (ubsan_instrument_bounds
4308 (input_location, obase, &nelts, /*ignore_off_by_one*/false));
4312 /* `build_vec_init' returns tree structure that performs
4313 initialization of a vector of aggregate types.
4315 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
4316 to the first element, of POINTER_TYPE.
4317 MAXINDEX is the maximum index of the array (one less than the
4318 number of elements). It is only used if BASE is a pointer or
4319 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
4321 INIT is the (possibly NULL) initializer.
4323 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
4324 elements in the array are value-initialized.
4326 FROM_ARRAY is 0 if we should init everything with INIT
4327 (i.e., every element initialized from INIT).
4328 FROM_ARRAY is 1 if we should index into INIT in parallel
4329 with initialization of DECL.
4330 FROM_ARRAY is 2 if we should index into INIT in parallel,
4331 but use assignment instead of initialization. */
4333 tree
4334 build_vec_init (tree base, tree maxindex, tree init,
4335 bool explicit_value_init_p,
4336 int from_array,
4337 tsubst_flags_t complain,
4338 vec<tree, va_gc>** flags /* = nullptr */)
4340 tree rval;
4341 tree base2 = NULL_TREE;
4342 tree itype = NULL_TREE;
4343 tree iterator;
4344 /* The type of BASE. */
4345 tree atype = TREE_TYPE (base);
4346 /* The type of an element in the array. */
4347 tree type = TREE_TYPE (atype);
4348 /* The element type reached after removing all outer array
4349 types. */
4350 tree inner_elt_type;
4351 /* The type of a pointer to an element in the array. */
4352 tree ptype;
4353 tree stmt_expr;
4354 tree compound_stmt;
4355 int destroy_temps;
4356 HOST_WIDE_INT num_initialized_elts = 0;
4357 bool is_global;
4358 tree obase = base;
4359 bool xvalue = false;
4360 bool errors = false;
4361 location_t loc = (init ? cp_expr_loc_or_input_loc (init)
4362 : location_of (base));
4364 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
4365 maxindex = array_type_nelts (atype);
4367 if (maxindex == NULL_TREE || maxindex == error_mark_node)
4368 return error_mark_node;
4370 maxindex = maybe_constant_value (maxindex);
4371 if (explicit_value_init_p)
4372 gcc_assert (!init);
4374 inner_elt_type = strip_array_types (type);
4376 /* Look through the TARGET_EXPR around a compound literal. */
4377 if (init && TREE_CODE (init) == TARGET_EXPR
4378 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
4379 && from_array != 2)
4380 init = TARGET_EXPR_INITIAL (init);
4382 if (tree vi = get_vec_init_expr (init))
4383 init = VEC_INIT_EXPR_INIT (vi);
4385 bool direct_init = false;
4386 if (from_array && init && BRACE_ENCLOSED_INITIALIZER_P (init)
4387 && CONSTRUCTOR_NELTS (init) == 1)
4389 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
4390 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
4391 && TREE_CODE (elt) != VEC_INIT_EXPR)
4393 direct_init = DIRECT_LIST_INIT_P (init);
4394 init = elt;
4398 /* If we have a braced-init-list or string constant, make sure that the array
4399 is big enough for all the initializers. */
4400 bool length_check = (init
4401 && (TREE_CODE (init) == STRING_CST
4402 || (TREE_CODE (init) == CONSTRUCTOR
4403 && CONSTRUCTOR_NELTS (init) > 0))
4404 && !TREE_CONSTANT (maxindex));
4406 if (init
4407 && TREE_CODE (atype) == ARRAY_TYPE
4408 && TREE_CONSTANT (maxindex)
4409 && !vla_type_p (type)
4410 && (from_array == 2
4411 ? vec_copy_assign_is_trivial (inner_elt_type, init)
4412 : !TYPE_NEEDS_CONSTRUCTING (type))
4413 && ((TREE_CODE (init) == CONSTRUCTOR
4414 && (BRACE_ENCLOSED_INITIALIZER_P (init)
4415 || (same_type_ignoring_top_level_qualifiers_p
4416 (atype, TREE_TYPE (init))))
4417 /* Don't do this if the CONSTRUCTOR might contain something
4418 that might throw and require us to clean up. */
4419 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
4420 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
4421 || from_array))
4423 /* Do non-default initialization of trivial arrays resulting from
4424 brace-enclosed initializers. In this case, digest_init and
4425 store_constructor will handle the semantics for us. */
4427 if (BRACE_ENCLOSED_INITIALIZER_P (init))
4428 init = digest_init (atype, init, complain);
4429 stmt_expr = build2 (INIT_EXPR, atype, base, init);
4430 return stmt_expr;
4433 maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
4434 maxindex = fold_simple (maxindex);
4436 if (TREE_CODE (atype) == ARRAY_TYPE)
4438 ptype = build_pointer_type (type);
4439 base = decay_conversion (base, complain);
4440 if (base == error_mark_node)
4441 return error_mark_node;
4442 base = cp_convert (ptype, base, complain);
4444 else
4445 ptype = atype;
4447 if (integer_all_onesp (maxindex))
4449 /* Shortcut zero element case to avoid unneeded constructor synthesis. */
4450 if (init && TREE_SIDE_EFFECTS (init))
4451 base = build2 (COMPOUND_EXPR, ptype, init, base);
4452 return base;
4455 /* The code we are generating looks like:
4457 T* t1 = (T*) base;
4458 T* rval = t1;
4459 ptrdiff_t iterator = maxindex;
4460 try {
4461 for (; iterator != -1; --iterator) {
4462 ... initialize *t1 ...
4463 ++t1;
4465 } catch (...) {
4466 ... destroy elements that were constructed ...
4468 rval;
4471 We can omit the try and catch blocks if we know that the
4472 initialization will never throw an exception, or if the array
4473 elements do not have destructors. We can omit the loop completely if
4474 the elements of the array do not have constructors.
4476 We actually wrap the entire body of the above in a STMT_EXPR, for
4477 tidiness.
4479 When copying from array to another, when the array elements have
4480 only trivial copy constructors, we should use __builtin_memcpy
4481 rather than generating a loop. That way, we could take advantage
4482 of whatever cleverness the back end has for dealing with copies
4483 of blocks of memory. */
4485 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
4486 destroy_temps = stmts_are_full_exprs_p ();
4487 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4488 rval = get_temp_regvar (ptype, base);
4489 base = get_temp_regvar (ptype, rval);
4490 tree iterator_targ = get_target_expr (maxindex);
4491 add_stmt (iterator_targ);
4492 iterator = TARGET_EXPR_SLOT (iterator_targ);
4494 /* If initializing one array from another, initialize element by
4495 element. We rely upon the below calls to do the argument
4496 checking. Evaluate the initializer before entering the try block. */
4497 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
4499 if (lvalue_kind (init) & clk_rvalueref)
4500 xvalue = true;
4501 base2 = decay_conversion (init, complain);
4502 if (base2 == error_mark_node)
4503 return error_mark_node;
4504 itype = TREE_TYPE (base2);
4505 base2 = get_temp_regvar (itype, base2);
4506 itype = TREE_TYPE (itype);
4509 /* Protect the entire array initialization so that we can destroy
4510 the partially constructed array if an exception is thrown.
4511 But don't do this if we're assigning. */
4512 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4513 && from_array != 2)
4515 tree e;
4516 tree m = cp_build_binary_op (input_location,
4517 MINUS_EXPR, maxindex, iterator,
4518 complain);
4520 /* Flatten multi-dimensional array since build_vec_delete only
4521 expects one-dimensional array. */
4522 if (TREE_CODE (type) == ARRAY_TYPE)
4523 m = cp_build_binary_op (input_location,
4524 MULT_EXPR, m,
4525 /* Avoid mixing signed and unsigned. */
4526 convert (TREE_TYPE (m),
4527 array_type_nelts_total (type)),
4528 complain);
4530 e = build_vec_delete_1 (input_location, rval, m,
4531 inner_elt_type, sfk_complete_destructor,
4532 /*use_global_delete=*/0, complain,
4533 /*in_cleanup*/true);
4534 if (e == error_mark_node)
4535 errors = true;
4536 TARGET_EXPR_CLEANUP (iterator_targ) = e;
4537 CLEANUP_EH_ONLY (iterator_targ) = true;
4539 /* Since we push this cleanup before doing any initialization, cleanups
4540 for any temporaries in the initialization are naturally within our
4541 cleanup region, so we don't want wrap_temporary_cleanups to do
4542 anything for arrays. But if the array is a subobject, we need to
4543 tell split_nonconstant_init how to turn off this cleanup in favor of
4544 the cleanup for the complete object. */
4545 if (flags)
4546 vec_safe_push (*flags, build_tree_list (iterator, maxindex));
4549 /* Should we try to create a constant initializer? */
4550 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
4551 && TREE_CONSTANT (maxindex)
4552 && (init ? TREE_CODE (init) == CONSTRUCTOR
4553 : (type_has_constexpr_default_constructor
4554 (inner_elt_type)))
4555 && (literal_type_p (inner_elt_type)
4556 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
4557 vec<constructor_elt, va_gc> *const_vec = NULL;
4558 bool saw_non_const = false;
4559 /* If we're initializing a static array, we want to do static
4560 initialization of any elements with constant initializers even if
4561 some are non-constant. */
4562 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
4564 bool empty_list = false;
4565 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
4566 && CONSTRUCTOR_NELTS (init) == 0)
4567 /* Skip over the handling of non-empty init lists. */
4568 empty_list = true;
4570 /* Maybe pull out constant value when from_array? */
4572 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
4574 /* Do non-default initialization of non-trivial arrays resulting from
4575 brace-enclosed initializers. */
4576 unsigned HOST_WIDE_INT idx;
4577 tree field, elt;
4578 /* If the constructor already has the array type, it's been through
4579 digest_init, so we shouldn't try to do anything more. */
4580 bool digested = same_type_p (atype, TREE_TYPE (init));
4581 from_array = 0;
4583 if (length_check)
4584 finish_length_check (atype, iterator, obase, CONSTRUCTOR_NELTS (init));
4586 if (try_const)
4587 vec_alloc (const_vec, CONSTRUCTOR_NELTS (init));
4589 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
4591 tree baseref = build1 (INDIRECT_REF, type, base);
4592 tree one_init;
4594 num_initialized_elts++;
4596 /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can
4597 handle cleanup flags properly. */
4598 gcc_checking_assert (!target_expr_needs_replace (elt));
4600 if (digested)
4601 one_init = build2 (INIT_EXPR, type, baseref, elt);
4602 else if (tree vi = get_vec_init_expr (elt))
4603 one_init = expand_vec_init_expr (baseref, vi, complain, flags);
4604 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
4605 one_init = build_aggr_init (baseref, elt, 0, complain);
4606 else
4607 one_init = cp_build_modify_expr (input_location, baseref,
4608 NOP_EXPR, elt, complain);
4609 if (one_init == error_mark_node)
4610 errors = true;
4611 if (try_const)
4613 if (!field)
4614 field = size_int (idx);
4615 tree e = maybe_constant_init (one_init);
4616 if (reduced_constant_expression_p (e))
4618 CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
4619 if (do_static_init)
4620 one_init = NULL_TREE;
4621 else
4622 one_init = build2 (INIT_EXPR, type, baseref, e);
4624 else
4626 if (do_static_init)
4628 tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
4629 true);
4630 if (value)
4631 CONSTRUCTOR_APPEND_ELT (const_vec, field, value);
4633 saw_non_const = true;
4637 if (one_init)
4638 finish_expr_stmt (one_init);
4640 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, false,
4641 complain);
4642 if (one_init == error_mark_node)
4643 errors = true;
4644 else
4645 finish_expr_stmt (one_init);
4647 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
4648 complain);
4649 if (one_init == error_mark_node)
4650 errors = true;
4651 else
4652 finish_expr_stmt (one_init);
4655 /* Any elements without explicit initializers get T{}. */
4656 empty_list = true;
4658 else if (init && TREE_CODE (init) == STRING_CST)
4660 /* Check that the array is at least as long as the string. */
4661 if (length_check)
4662 finish_length_check (atype, iterator, obase,
4663 TREE_STRING_LENGTH (init));
4664 tree length = build_int_cst (ptrdiff_type_node,
4665 TREE_STRING_LENGTH (init));
4667 /* Copy the string to the first part of the array. */
4668 tree alias_set = build_int_cst (build_pointer_type (type), 0);
4669 tree lhs = build2 (MEM_REF, TREE_TYPE (init), base, alias_set);
4670 tree stmt = build2 (MODIFY_EXPR, void_type_node, lhs, init);
4671 finish_expr_stmt (stmt);
4673 /* Adjust the counter and pointer. */
4674 stmt = cp_build_binary_op (loc, MINUS_EXPR, iterator, length, complain);
4675 stmt = build2 (MODIFY_EXPR, void_type_node, iterator, stmt);
4676 finish_expr_stmt (stmt);
4678 stmt = cp_build_binary_op (loc, PLUS_EXPR, base, length, complain);
4679 stmt = build2 (MODIFY_EXPR, void_type_node, base, stmt);
4680 finish_expr_stmt (stmt);
4682 /* And set the rest of the array to NUL. */
4683 from_array = 0;
4684 explicit_value_init_p = true;
4686 else if (from_array)
4688 if (init)
4689 /* OK, we set base2 above. */;
4690 else if (CLASS_TYPE_P (type)
4691 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4693 if (complain & tf_error)
4694 error ("initializer ends prematurely");
4695 errors = true;
4699 /* Now, default-initialize any remaining elements. We don't need to
4700 do that if a) the type does not need constructing, or b) we've
4701 already initialized all the elements.
4703 We do need to keep going if we're copying an array. */
4705 if (try_const && !init
4706 && (cxx_dialect < cxx20
4707 || !default_init_uninitialized_part (inner_elt_type)))
4708 /* With a constexpr default constructor, which we checked for when
4709 setting try_const above, default-initialization is equivalent to
4710 value-initialization, and build_value_init gives us something more
4711 friendly to maybe_constant_init. Except in C++20 and up a constexpr
4712 constructor need not initialize all the members. */
4713 explicit_value_init_p = true;
4714 if (from_array
4715 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
4716 && ! (tree_fits_shwi_p (maxindex)
4717 && (num_initialized_elts
4718 == tree_to_shwi (maxindex) + 1))))
4720 /* If the ITERATOR is lesser or equal to -1, then we don't have to loop;
4721 we've already initialized all the elements. */
4722 tree for_stmt;
4723 tree elt_init;
4724 tree to;
4726 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
4727 finish_init_stmt (for_stmt);
4728 finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator,
4729 build_int_cst (TREE_TYPE (iterator), -1)),
4730 for_stmt, false, 0);
4731 /* We used to pass this decrement to finish_for_expr; now we add it to
4732 elt_init below so it's part of the same full-expression as the
4733 initialization, and thus happens before any potentially throwing
4734 temporary cleanups. */
4735 tree decr = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
4736 complain);
4739 to = build1 (INDIRECT_REF, type, base);
4741 /* If the initializer is {}, then all elements are initialized from T{}.
4742 But for non-classes, that's the same as value-initialization. */
4743 if (empty_list)
4745 if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type))
4747 init = build_constructor (init_list_type_node, NULL);
4749 else
4751 init = NULL_TREE;
4752 explicit_value_init_p = true;
4756 if (from_array)
4758 tree from;
4760 if (base2)
4762 from = build1 (INDIRECT_REF, itype, base2);
4763 if (xvalue)
4764 from = move (from);
4765 if (direct_init)
4766 from = build_tree_list (NULL_TREE, from);
4768 else
4769 from = NULL_TREE;
4771 if (TREE_CODE (type) == ARRAY_TYPE)
4772 elt_init = build_vec_init (to, NULL_TREE, from, /*val_init*/false,
4773 from_array, complain);
4774 else if (from_array == 2)
4775 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR,
4776 from, complain);
4777 else if (type_build_ctor_call (type))
4778 elt_init = build_aggr_init (to, from, 0, complain);
4779 else if (from)
4780 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, from,
4781 complain);
4782 else
4783 gcc_unreachable ();
4785 else if (TREE_CODE (type) == ARRAY_TYPE)
4787 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
4789 if ((complain & tf_error))
4790 error_at (loc, "array must be initialized "
4791 "with a brace-enclosed initializer");
4792 elt_init = error_mark_node;
4794 else
4795 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
4796 0, init,
4797 explicit_value_init_p,
4798 0, complain);
4800 else if (explicit_value_init_p)
4802 elt_init = build_value_init (type, complain);
4803 if (elt_init != error_mark_node)
4804 elt_init = build2 (INIT_EXPR, type, to, elt_init);
4806 else
4808 gcc_assert (type_build_ctor_call (type) || init);
4809 if (CLASS_TYPE_P (type))
4810 elt_init = build_aggr_init (to, init, 0, complain);
4811 else
4813 if (TREE_CODE (init) == TREE_LIST)
4814 init = build_x_compound_expr_from_list (init, ELK_INIT,
4815 complain);
4816 elt_init = (init == error_mark_node
4817 ? error_mark_node
4818 : build2 (INIT_EXPR, type, to, init));
4822 if (elt_init == error_mark_node)
4823 errors = true;
4825 if (try_const)
4827 /* FIXME refs to earlier elts */
4828 tree e = maybe_constant_init (elt_init);
4829 if (reduced_constant_expression_p (e))
4831 if (initializer_zerop (e))
4832 /* Don't fill the CONSTRUCTOR with zeros. */
4833 e = NULL_TREE;
4834 if (do_static_init)
4835 elt_init = NULL_TREE;
4837 else
4839 saw_non_const = true;
4840 if (do_static_init)
4841 e = build_zero_init (TREE_TYPE (e), NULL_TREE, true);
4842 else
4843 e = NULL_TREE;
4846 if (e)
4848 HOST_WIDE_INT last = tree_to_shwi (maxindex);
4849 if (num_initialized_elts <= last)
4851 tree field = size_int (num_initialized_elts);
4852 if (num_initialized_elts != last)
4853 field = build2 (RANGE_EXPR, sizetype, field,
4854 size_int (last));
4855 CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
4860 /* [class.temporary]: "There are three contexts in which temporaries are
4861 destroyed at a different point than the end of the full-
4862 expression. The first context is when a default constructor is called
4863 to initialize an element of an array with no corresponding
4864 initializer. The second context is when a copy constructor is called
4865 to copy an element of an array while the entire array is copied. In
4866 either case, if the constructor has one or more default arguments, the
4867 destruction of every temporary created in a default argument is
4868 sequenced before the construction of the next array element, if any."
4870 So, for this loop, statements are full-expressions. */
4871 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4872 if (elt_init && !errors)
4873 elt_init = build2 (COMPOUND_EXPR, void_type_node, elt_init, decr);
4874 else
4875 elt_init = decr;
4876 finish_expr_stmt (elt_init);
4877 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4879 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false,
4880 complain));
4881 if (base2)
4882 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, false,
4883 complain));
4885 finish_for_stmt (for_stmt);
4888 /* The value of the array initialization is the array itself, RVAL
4889 is a pointer to the first element. */
4890 finish_stmt_expr_expr (rval, stmt_expr);
4892 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
4894 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
4896 if (errors)
4897 return error_mark_node;
4899 if (try_const)
4901 if (!saw_non_const)
4903 tree const_init = build_constructor (atype, const_vec);
4904 return build2 (INIT_EXPR, atype, obase, const_init);
4906 else if (do_static_init && !vec_safe_is_empty (const_vec))
4907 DECL_INITIAL (obase) = build_constructor (atype, const_vec);
4908 else
4909 vec_free (const_vec);
4912 /* Now make the result have the correct type. */
4913 if (TREE_CODE (atype) == ARRAY_TYPE)
4915 atype = build_reference_type (atype);
4916 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
4917 stmt_expr = convert_from_reference (stmt_expr);
4920 return stmt_expr;
4923 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
4924 build_delete. */
4926 static tree
4927 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
4928 tsubst_flags_t complain)
4930 tree name;
4931 switch (dtor_kind)
4933 case sfk_complete_destructor:
4934 name = complete_dtor_identifier;
4935 break;
4937 case sfk_base_destructor:
4938 name = base_dtor_identifier;
4939 break;
4941 case sfk_deleting_destructor:
4942 name = deleting_dtor_identifier;
4943 break;
4945 default:
4946 gcc_unreachable ();
4949 return build_special_member_call (exp, name,
4950 /*args=*/NULL,
4951 /*binfo=*/TREE_TYPE (exp),
4952 flags,
4953 complain);
4956 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
4957 ADDR is an expression which yields the store to be destroyed.
4958 AUTO_DELETE is the name of the destructor to call, i.e., either
4959 sfk_complete_destructor, sfk_base_destructor, or
4960 sfk_deleting_destructor.
4962 FLAGS is the logical disjunction of zero or more LOOKUP_
4963 flags. See cp-tree.h for more info. */
4965 tree
4966 build_delete (location_t loc, tree otype, tree addr,
4967 special_function_kind auto_delete,
4968 int flags, int use_global_delete, tsubst_flags_t complain)
4970 tree expr;
4972 if (addr == error_mark_node)
4973 return error_mark_node;
4975 tree type = TYPE_MAIN_VARIANT (otype);
4977 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
4978 set to `error_mark_node' before it gets properly cleaned up. */
4979 if (type == error_mark_node)
4980 return error_mark_node;
4982 if (TYPE_PTR_P (type))
4983 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4985 if (TREE_CODE (type) == ARRAY_TYPE)
4987 if (TYPE_DOMAIN (type) == NULL_TREE)
4989 if (complain & tf_error)
4990 error_at (loc, "unknown array size in delete");
4991 return error_mark_node;
4993 return build_vec_delete (loc, addr, array_type_nelts (type),
4994 auto_delete, use_global_delete, complain);
4997 bool deleting = (auto_delete == sfk_deleting_destructor);
4998 gcc_assert (deleting == !(flags & LOOKUP_DESTRUCTOR));
5000 if (TYPE_PTR_P (otype))
5002 addr = mark_rvalue_use (addr);
5004 /* We don't want to warn about delete of void*, only other
5005 incomplete types. Deleting other incomplete types
5006 invokes undefined behavior, but it is not ill-formed, so
5007 compile to something that would even do The Right Thing
5008 (TM) should the type have a trivial dtor and no delete
5009 operator. */
5010 if (!VOID_TYPE_P (type))
5012 complete_type (type);
5013 if (deleting
5014 && !verify_type_context (loc, TCTX_DEALLOCATION, type,
5015 !(complain & tf_error)))
5016 return error_mark_node;
5018 if (!COMPLETE_TYPE_P (type))
5020 if (complain & tf_warning)
5022 auto_diagnostic_group d;
5023 if (warning_at (loc, OPT_Wdelete_incomplete,
5024 "possible problem detected in invocation of "
5025 "%<operator delete%>"))
5027 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
5028 inform (loc,
5029 "neither the destructor nor the class-specific "
5030 "%<operator delete%> will be called, even if "
5031 "they are declared when the class is defined");
5035 else if (deleting && warn_delnonvdtor
5036 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
5037 && TYPE_POLYMORPHIC_P (type))
5039 tree dtor = CLASSTYPE_DESTRUCTOR (type);
5040 if (!dtor || !DECL_VINDEX (dtor))
5042 if (CLASSTYPE_PURE_VIRTUALS (type))
5043 warning_at (loc, OPT_Wdelete_non_virtual_dtor,
5044 "deleting object of abstract class type %qT"
5045 " which has non-virtual destructor"
5046 " will cause undefined behavior", type);
5047 else
5048 warning_at (loc, OPT_Wdelete_non_virtual_dtor,
5049 "deleting object of polymorphic class type %qT"
5050 " which has non-virtual destructor"
5051 " might cause undefined behavior", type);
5056 /* Throw away const and volatile on target type of addr. */
5057 addr = convert_force (build_pointer_type (type), addr, 0, complain);
5059 else
5061 /* Don't check PROTECT here; leave that decision to the
5062 destructor. If the destructor is accessible, call it,
5063 else report error. */
5064 addr = cp_build_addr_expr (addr, complain);
5065 if (addr == error_mark_node)
5066 return error_mark_node;
5068 addr = convert_force (build_pointer_type (type), addr, 0, complain);
5071 if (deleting)
5072 /* We will use ADDR multiple times so we must save it. */
5073 addr = save_expr (addr);
5075 bool virtual_p = false;
5076 if (type_build_dtor_call (type))
5078 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
5079 lazily_declare_fn (sfk_destructor, type);
5080 virtual_p = DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type));
5083 tree head = NULL_TREE;
5084 tree do_delete = NULL_TREE;
5085 bool destroying_delete = false;
5087 if (!deleting)
5089 /* Leave do_delete null. */
5091 /* For `::delete x', we must not use the deleting destructor
5092 since then we would not be sure to get the global `operator
5093 delete'. */
5094 else if (use_global_delete)
5096 head = get_target_expr (build_headof (addr));
5097 /* Delete the object. */
5098 do_delete = build_op_delete_call (DELETE_EXPR,
5099 head,
5100 cxx_sizeof_nowarn (type),
5101 /*global_p=*/true,
5102 /*placement=*/NULL_TREE,
5103 /*alloc_fn=*/NULL_TREE,
5104 complain);
5105 /* Otherwise, treat this like a complete object destructor
5106 call. */
5107 auto_delete = sfk_complete_destructor;
5109 /* If the destructor is non-virtual, there is no deleting
5110 variant. Instead, we must explicitly call the appropriate
5111 `operator delete' here. */
5112 else if (!virtual_p)
5114 /* Build the call. */
5115 do_delete = build_op_delete_call (DELETE_EXPR,
5116 addr,
5117 cxx_sizeof_nowarn (type),
5118 /*global_p=*/false,
5119 /*placement=*/NULL_TREE,
5120 /*alloc_fn=*/NULL_TREE,
5121 complain);
5122 /* Call the complete object destructor. */
5123 auto_delete = sfk_complete_destructor;
5124 if (do_delete != error_mark_node)
5126 tree fn = get_callee_fndecl (do_delete);
5127 destroying_delete = destroying_delete_p (fn);
5130 else if (TYPE_GETS_REG_DELETE (type))
5132 /* Make sure we have access to the member op delete, even though
5133 we'll actually be calling it from the destructor. */
5134 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
5135 /*global_p=*/false,
5136 /*placement=*/NULL_TREE,
5137 /*alloc_fn=*/NULL_TREE,
5138 complain);
5141 if (destroying_delete)
5142 /* The operator delete will call the destructor. */
5143 expr = addr;
5144 else if (type_build_dtor_call (type))
5145 expr = build_dtor_call (cp_build_fold_indirect_ref (addr),
5146 auto_delete, flags, complain);
5147 else
5148 expr = build_trivial_dtor_call (addr);
5149 if (expr == error_mark_node)
5150 return error_mark_node;
5152 if (!deleting)
5154 protected_set_expr_location (expr, loc);
5155 return expr;
5158 if (do_delete == error_mark_node)
5159 return error_mark_node;
5161 if (do_delete && !TREE_SIDE_EFFECTS (expr))
5162 expr = do_delete;
5163 else if (do_delete)
5164 /* The delete operator must be called, regardless of whether
5165 the destructor throws.
5167 [expr.delete]/7 The deallocation function is called
5168 regardless of whether the destructor for the object or some
5169 element of the array throws an exception. */
5170 expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete);
5172 /* We need to calculate this before the dtor changes the vptr. */
5173 if (head)
5174 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
5176 /* Handle deleting a null pointer. */
5177 warning_sentinel s (warn_address);
5178 tree ifexp = cp_build_binary_op (loc, NE_EXPR, addr,
5179 nullptr_node, complain);
5180 ifexp = cp_fully_fold (ifexp);
5182 if (ifexp == error_mark_node)
5183 return error_mark_node;
5184 /* This is a compiler generated comparison, don't emit
5185 e.g. -Wnonnull-compare warning for it. */
5186 else if (TREE_CODE (ifexp) == NE_EXPR)
5187 suppress_warning (ifexp, OPT_Wnonnull_compare);
5189 if (!integer_nonzerop (ifexp))
5190 expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node);
5192 protected_set_expr_location (expr, loc);
5193 return expr;
5196 /* At the beginning of a destructor, push cleanups that will call the
5197 destructors for our base classes and members.
5199 Called from begin_destructor_body. */
5201 void
5202 push_base_cleanups (void)
5204 tree binfo, base_binfo;
5205 int i;
5206 tree member;
5207 tree expr;
5208 vec<tree, va_gc> *vbases;
5210 /* Run destructors for all virtual baseclasses. */
5211 if (!ABSTRACT_CLASS_TYPE_P (current_class_type)
5212 && CLASSTYPE_VBASECLASSES (current_class_type))
5214 tree cond = (condition_conversion
5215 (build2 (BIT_AND_EXPR, integer_type_node,
5216 current_in_charge_parm,
5217 integer_two_node)));
5219 /* The CLASSTYPE_VBASECLASSES vector is in initialization
5220 order, which is also the right order for pushing cleanups. */
5221 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
5222 vec_safe_iterate (vbases, i, &base_binfo); i++)
5224 if (type_build_dtor_call (BINFO_TYPE (base_binfo)))
5226 expr = build_special_member_call (current_class_ref,
5227 base_dtor_identifier,
5228 NULL,
5229 base_binfo,
5230 (LOOKUP_NORMAL
5231 | LOOKUP_NONVIRTUAL),
5232 tf_warning_or_error);
5233 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5235 expr = build3 (COND_EXPR, void_type_node, cond,
5236 expr, void_node);
5237 finish_decl_cleanup (NULL_TREE, expr);
5243 /* Take care of the remaining baseclasses. */
5244 for (binfo = TYPE_BINFO (current_class_type), i = 0;
5245 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5247 if (BINFO_VIRTUAL_P (base_binfo)
5248 || !type_build_dtor_call (BINFO_TYPE (base_binfo)))
5249 continue;
5251 expr = build_special_member_call (current_class_ref,
5252 base_dtor_identifier,
5253 NULL, base_binfo,
5254 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
5255 tf_warning_or_error);
5256 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5257 finish_decl_cleanup (NULL_TREE, expr);
5260 /* Don't automatically destroy union members. */
5261 if (TREE_CODE (current_class_type) == UNION_TYPE)
5262 return;
5264 for (member = TYPE_FIELDS (current_class_type); member;
5265 member = DECL_CHAIN (member))
5267 tree this_type = TREE_TYPE (member);
5268 if (this_type == error_mark_node
5269 || TREE_CODE (member) != FIELD_DECL
5270 || DECL_ARTIFICIAL (member))
5271 continue;
5272 if (ANON_AGGR_TYPE_P (this_type))
5273 continue;
5274 if (type_build_dtor_call (this_type))
5276 tree this_member = (build_class_member_access_expr
5277 (current_class_ref, member,
5278 /*access_path=*/NULL_TREE,
5279 /*preserve_reference=*/false,
5280 tf_warning_or_error));
5281 expr = build_delete (input_location, this_type, this_member,
5282 sfk_complete_destructor,
5283 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
5284 0, tf_warning_or_error);
5285 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
5286 finish_decl_cleanup (NULL_TREE, expr);
5291 /* Build a C++ vector delete expression.
5292 MAXINDEX is the number of elements to be deleted.
5293 ELT_SIZE is the nominal size of each element in the vector.
5294 BASE is the expression that should yield the store to be deleted.
5295 This function expands (or synthesizes) these calls itself.
5296 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
5298 This also calls delete for virtual baseclasses of elements of the vector.
5300 Update: MAXINDEX is no longer needed. The size can be extracted from the
5301 start of the vector for pointers, and from the type for arrays. We still
5302 use MAXINDEX for arrays because it happens to already have one of the
5303 values we'd have to extract. (We could use MAXINDEX with pointers to
5304 confirm the size, and trap if the numbers differ; not clear that it'd
5305 be worth bothering.) */
5307 tree
5308 build_vec_delete (location_t loc, tree base, tree maxindex,
5309 special_function_kind auto_delete_vec,
5310 int use_global_delete, tsubst_flags_t complain)
5312 tree type;
5313 tree rval;
5314 tree base_init = NULL_TREE;
5316 type = TREE_TYPE (base);
5318 if (TYPE_PTR_P (type))
5320 /* Step back one from start of vector, and read dimension. */
5321 tree cookie_addr;
5322 tree size_ptr_type = build_pointer_type (sizetype);
5324 base = mark_rvalue_use (base);
5325 if (TREE_SIDE_EFFECTS (base))
5327 base_init = get_target_expr (base);
5328 base = TARGET_EXPR_SLOT (base_init);
5330 type = strip_array_types (TREE_TYPE (type));
5331 cookie_addr = fold_build1_loc (loc, NEGATE_EXPR,
5332 sizetype, TYPE_SIZE_UNIT (sizetype));
5333 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
5334 cookie_addr);
5335 maxindex = cp_build_fold_indirect_ref (cookie_addr);
5337 else if (TREE_CODE (type) == ARRAY_TYPE)
5339 /* Get the total number of things in the array, maxindex is a
5340 bad name. */
5341 maxindex = array_type_nelts_total (type);
5342 type = strip_array_types (type);
5343 base = decay_conversion (base, complain);
5344 if (base == error_mark_node)
5345 return error_mark_node;
5346 if (TREE_SIDE_EFFECTS (base))
5348 base_init = get_target_expr (base);
5349 base = TARGET_EXPR_SLOT (base_init);
5352 else
5354 if (base != error_mark_node && !(complain & tf_error))
5355 error_at (loc,
5356 "type to vector delete is neither pointer or array type");
5357 return error_mark_node;
5360 rval = build_vec_delete_1 (loc, base, maxindex, type, auto_delete_vec,
5361 use_global_delete, complain);
5362 if (base_init && rval != error_mark_node)
5363 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
5365 protected_set_expr_location (rval, loc);
5366 return rval;
5369 #include "gt-cp-init.h"