c++: -Wplacement-new and anon union member [PR100370]
[official-gcc.git] / gcc / cp / init.cc
blobce332c7e3292915bf42fe1ad3624bedd5e1332df
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 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
347 gcc_assert (!processing_template_decl
348 || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
350 if (CLASS_TYPE_P (type) && type_build_ctor_call (type))
352 tree ctor
353 = build_special_member_call (NULL_TREE, complete_ctor_identifier,
354 NULL, type, LOOKUP_NORMAL, complain);
355 if (ctor == error_mark_node || TREE_CONSTANT (ctor))
356 return ctor;
357 tree fn = NULL_TREE;
358 if (TREE_CODE (ctor) == CALL_EXPR)
359 fn = get_callee_fndecl (ctor);
360 ctor = build_aggr_init_expr (type, ctor);
361 if (fn && user_provided_p (fn))
362 return ctor;
363 else if (TYPE_HAS_COMPLEX_DFLT (type))
365 /* This is a class that needs constructing, but doesn't have
366 a user-provided constructor. So we need to zero-initialize
367 the object and then call the implicitly defined ctor.
368 This will be handled in simplify_aggr_init_expr. */
369 AGGR_INIT_ZERO_FIRST (ctor) = 1;
370 return ctor;
374 /* Discard any access checking during subobject initialization;
375 the checks are implied by the call to the ctor which we have
376 verified is OK (cpp0x/defaulted46.C). */
377 push_deferring_access_checks (dk_deferred);
378 tree r = build_value_init_noctor (type, complain);
379 pop_deferring_access_checks ();
380 return r;
383 /* Like build_value_init, but don't call the constructor for TYPE. Used
384 for base initializers. */
386 tree
387 build_value_init_noctor (tree type, tsubst_flags_t complain)
389 if (!COMPLETE_TYPE_P (type))
391 if (complain & tf_error)
392 error ("value-initialization of incomplete type %qT", type);
393 return error_mark_node;
395 /* FIXME the class and array cases should just use digest_init once it is
396 SFINAE-enabled. */
397 if (CLASS_TYPE_P (type))
399 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type)
400 || errorcount != 0);
402 if (TREE_CODE (type) != UNION_TYPE)
404 tree field;
405 vec<constructor_elt, va_gc> *v = NULL;
407 /* Iterate over the fields, building initializations. */
408 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
410 tree ftype, value;
412 if (TREE_CODE (field) != FIELD_DECL)
413 continue;
415 ftype = TREE_TYPE (field);
417 if (ftype == error_mark_node)
418 continue;
420 /* Ignore flexible array members for value initialization. */
421 if (TREE_CODE (ftype) == ARRAY_TYPE
422 && !COMPLETE_TYPE_P (ftype)
423 && !TYPE_DOMAIN (ftype)
424 && COMPLETE_TYPE_P (TREE_TYPE (ftype))
425 && (next_initializable_field (DECL_CHAIN (field))
426 == NULL_TREE))
427 continue;
429 /* Ignore unnamed zero-width bitfields. */
430 if (DECL_UNNAMED_BIT_FIELD (field)
431 && integer_zerop (DECL_SIZE (field)))
432 continue;
434 /* We could skip vfields and fields of types with
435 user-defined constructors, but I think that won't improve
436 performance at all; it should be simpler in general just
437 to zero out the entire object than try to only zero the
438 bits that actually need it. */
440 /* Note that for class types there will be FIELD_DECLs
441 corresponding to base classes as well. Thus, iterating
442 over TYPE_FIELDs will result in correct initialization of
443 all of the subobjects. */
444 value = build_value_init (ftype, complain);
445 value = maybe_constant_init (value);
447 if (value == error_mark_node)
448 return error_mark_node;
450 CONSTRUCTOR_APPEND_ELT(v, field, value);
452 /* We shouldn't have gotten here for anything that would need
453 non-trivial initialization, and gimplify_init_ctor_preeval
454 would need to be fixed to allow it. */
455 gcc_assert (TREE_CODE (value) != TARGET_EXPR
456 && TREE_CODE (value) != AGGR_INIT_EXPR);
459 /* Build a constructor to contain the zero- initializations. */
460 return build_constructor (type, v);
463 else if (TREE_CODE (type) == ARRAY_TYPE)
465 vec<constructor_elt, va_gc> *v = NULL;
467 /* Iterate over the array elements, building initializations. */
468 tree max_index = array_type_nelts (type);
470 /* If we have an error_mark here, we should just return error mark
471 as we don't know the size of the array yet. */
472 if (max_index == error_mark_node)
474 if (complain & tf_error)
475 error ("cannot value-initialize array of unknown bound %qT",
476 type);
477 return error_mark_node;
479 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
481 /* A zero-sized array, which is accepted as an extension, will
482 have an upper bound of -1. */
483 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
485 constructor_elt ce;
487 /* If this is a one element array, we just use a regular init. */
488 if (tree_int_cst_equal (size_zero_node, max_index))
489 ce.index = size_zero_node;
490 else
491 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
493 ce.value = build_value_init (TREE_TYPE (type), complain);
494 ce.value = maybe_constant_init (ce.value);
495 if (ce.value == error_mark_node)
496 return error_mark_node;
498 vec_alloc (v, 1);
499 v->quick_push (ce);
501 /* We shouldn't have gotten here for anything that would need
502 non-trivial initialization, and gimplify_init_ctor_preeval
503 would need to be fixed to allow it. */
504 gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
505 && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
508 /* Build a constructor to contain the initializations. */
509 return build_constructor (type, v);
511 else if (TREE_CODE (type) == FUNCTION_TYPE)
513 if (complain & tf_error)
514 error ("value-initialization of function type %qT", type);
515 return error_mark_node;
517 else if (TYPE_REF_P (type))
519 if (complain & tf_error)
520 error ("value-initialization of reference type %qT", type);
521 return error_mark_node;
524 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
527 /* Initialize current class with INIT, a TREE_LIST of arguments for
528 a target constructor. If TREE_LIST is void_type_node, an empty
529 initializer list was given. Return the target constructor. */
531 static tree
532 perform_target_ctor (tree init)
534 tree decl = current_class_ref;
535 tree type = current_class_type;
537 init = build_aggr_init (decl, init, LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
538 tf_warning_or_error);
539 finish_expr_stmt (init);
540 if (type_build_dtor_call (type))
542 tree expr = build_delete (input_location,
543 type, decl, sfk_complete_destructor,
544 LOOKUP_NORMAL
545 |LOOKUP_NONVIRTUAL
546 |LOOKUP_DESTRUCTOR,
547 0, tf_warning_or_error);
548 if (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
550 tree base = build_delete (input_location,
551 type, decl, sfk_base_destructor,
552 LOOKUP_NORMAL
553 |LOOKUP_NONVIRTUAL
554 |LOOKUP_DESTRUCTOR,
555 0, tf_warning_or_error);
556 expr = build_if_in_charge (expr, base);
558 if (expr != error_mark_node
559 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
560 finish_eh_cleanup (expr);
562 return init;
565 /* Return the non-static data initializer for FIELD_DECL MEMBER. */
567 static GTY((cache)) decl_tree_cache_map *nsdmi_inst;
569 tree
570 get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
572 tree init;
573 tree save_ccp = current_class_ptr;
574 tree save_ccr = current_class_ref;
576 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
578 init = DECL_INITIAL (DECL_TI_TEMPLATE (member));
579 location_t expr_loc
580 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member));
581 if (TREE_CODE (init) == DEFERRED_PARSE)
582 /* Unparsed. */;
583 else if (tree *slot = hash_map_safe_get (nsdmi_inst, member))
584 init = *slot;
585 /* Check recursive instantiation. */
586 else if (DECL_INSTANTIATING_NSDMI_P (member))
588 if (complain & tf_error)
589 error_at (expr_loc, "recursive instantiation of default member "
590 "initializer for %qD", member);
591 init = error_mark_node;
593 else
595 cp_evaluated ev;
597 location_t sloc = input_location;
598 input_location = expr_loc;
600 DECL_INSTANTIATING_NSDMI_P (member) = 1;
602 bool pushed = false;
603 tree ctx = DECL_CONTEXT (member);
605 processing_template_decl_sentinel ptds (/*reset*/false);
606 if (!currently_open_class (ctx))
608 if (!LOCAL_CLASS_P (ctx))
609 push_to_top_level ();
610 else
611 /* push_to_top_level would lose the necessary function context,
612 just reset processing_template_decl. */
613 processing_template_decl = 0;
614 push_nested_class (ctx);
615 push_deferring_access_checks (dk_no_deferred);
616 pushed = true;
619 inject_this_parameter (ctx, TYPE_UNQUALIFIED);
621 start_lambda_scope (member);
623 /* Do deferred instantiation of the NSDMI. */
624 init = (tsubst_copy_and_build
625 (init, DECL_TI_ARGS (member),
626 complain, member, /*function_p=*/false,
627 /*integral_constant_expression_p=*/false));
628 init = digest_nsdmi_init (member, init, complain);
630 finish_lambda_scope ();
632 DECL_INSTANTIATING_NSDMI_P (member) = 0;
634 if (init != error_mark_node)
635 hash_map_safe_put<hm_ggc> (nsdmi_inst, member, init);
637 if (pushed)
639 pop_deferring_access_checks ();
640 pop_nested_class ();
641 if (!LOCAL_CLASS_P (ctx))
642 pop_from_top_level ();
645 input_location = sloc;
648 else
649 init = DECL_INITIAL (member);
651 if (init && TREE_CODE (init) == DEFERRED_PARSE)
653 if (complain & tf_error)
655 error ("default member initializer for %qD required before the end "
656 "of its enclosing class", member);
657 inform (location_of (init), "defined here");
658 DECL_INITIAL (member) = error_mark_node;
660 init = error_mark_node;
663 if (in_ctor)
665 current_class_ptr = save_ccp;
666 current_class_ref = save_ccr;
668 else
670 /* Use a PLACEHOLDER_EXPR when we don't have a 'this' parameter to
671 refer to; constexpr evaluation knows what to do with it. */
672 current_class_ref = build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (member));
673 current_class_ptr = build_address (current_class_ref);
676 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
677 so the aggregate init code below will see a CONSTRUCTOR. */
678 bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init));
679 if (simple_target)
680 init = TARGET_EXPR_INITIAL (init);
681 init = break_out_target_exprs (init, /*loc*/true);
682 if (init && TREE_CODE (init) == TARGET_EXPR)
683 /* In a constructor, this expresses the full initialization, prevent
684 perform_member_init from calling another constructor (58162). */
685 TARGET_EXPR_DIRECT_INIT_P (init) = in_ctor;
686 if (simple_target && TREE_CODE (init) != CONSTRUCTOR)
687 /* Now put it back so C++17 copy elision works. */
688 init = get_target_expr (init);
690 current_class_ptr = save_ccp;
691 current_class_ref = save_ccr;
692 return init;
695 /* Diagnose the flexible array MEMBER if its INITializer is non-null
696 and return true if so. Otherwise return false. */
698 bool
699 maybe_reject_flexarray_init (tree member, tree init)
701 tree type = TREE_TYPE (member);
703 if (!init
704 || TREE_CODE (type) != ARRAY_TYPE
705 || TYPE_DOMAIN (type))
706 return false;
708 /* Point at the flexible array member declaration if it's initialized
709 in-class, and at the ctor if it's initialized in a ctor member
710 initializer list. */
711 location_t loc;
712 if (DECL_INITIAL (member) == init
713 || !current_function_decl
714 || DECL_DEFAULTED_FN (current_function_decl))
715 loc = DECL_SOURCE_LOCATION (member);
716 else
717 loc = DECL_SOURCE_LOCATION (current_function_decl);
719 error_at (loc, "initializer for flexible array member %q#D", member);
720 return true;
723 /* If INIT's value can come from a call to std::initializer_list<T>::begin,
724 return that function. Otherwise, NULL_TREE. */
726 static tree
727 find_list_begin (tree init)
729 STRIP_NOPS (init);
730 while (TREE_CODE (init) == COMPOUND_EXPR)
731 init = TREE_OPERAND (init, 1);
732 STRIP_NOPS (init);
733 if (TREE_CODE (init) == COND_EXPR)
735 tree left = TREE_OPERAND (init, 1);
736 if (!left)
737 left = TREE_OPERAND (init, 0);
738 left = find_list_begin (left);
739 if (left)
740 return left;
741 return find_list_begin (TREE_OPERAND (init, 2));
743 if (TREE_CODE (init) == CALL_EXPR)
744 if (tree fn = get_callee_fndecl (init))
745 if (id_equal (DECL_NAME (fn), "begin")
746 && is_std_init_list (DECL_CONTEXT (fn)))
747 return fn;
748 return NULL_TREE;
751 /* If INIT initializing MEMBER is copying the address of the underlying array
752 of an initializer_list, warn. */
754 static void
755 maybe_warn_list_ctor (tree member, tree init)
757 tree memtype = TREE_TYPE (member);
758 if (!init || !TYPE_PTR_P (memtype)
759 || !is_list_ctor (current_function_decl))
760 return;
762 tree parm = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl);
763 parm = TREE_VALUE (parm);
764 tree initlist = non_reference (parm);
766 /* Do not warn if the parameter is an lvalue reference to non-const. */
767 if (TYPE_REF_P (parm) && !TYPE_REF_IS_RVALUE (parm)
768 && !CP_TYPE_CONST_P (initlist))
769 return;
771 tree targs = CLASSTYPE_TI_ARGS (initlist);
772 tree elttype = TREE_VEC_ELT (targs, 0);
774 if (!same_type_ignoring_top_level_qualifiers_p
775 (TREE_TYPE (memtype), elttype))
776 return;
778 tree begin = find_list_begin (init);
779 if (!begin)
780 return;
782 location_t loc = cp_expr_loc_or_input_loc (init);
783 warning_at (loc, OPT_Winit_list_lifetime,
784 "initializing %qD from %qE does not extend the lifetime "
785 "of the underlying array", member, begin);
788 /* Data structure for find_uninit_fields_r, below. */
790 struct find_uninit_data {
791 /* The set tracking the yet-uninitialized members. */
792 hash_set<tree> *uninitialized;
793 /* The data member we are currently initializing. It can be either
794 a type (initializing a base class/delegating constructors), or
795 a COMPONENT_REF. */
796 tree member;
799 /* walk_tree callback that warns about using uninitialized data in
800 a member-initializer-list. */
802 static tree
803 find_uninit_fields_r (tree *tp, int *walk_subtrees, void *data)
805 find_uninit_data *d = static_cast<find_uninit_data *>(data);
806 hash_set<tree> *uninitialized = d->uninitialized;
807 tree init = *tp;
808 const tree_code code = TREE_CODE (init);
810 /* No need to look into types or unevaluated operands. */
811 if (TYPE_P (init) || unevaluated_p (code))
813 *walk_subtrees = false;
814 return NULL_TREE;
817 switch (code)
819 /* We'd need data flow info to avoid false positives. */
820 case COND_EXPR:
821 case VEC_COND_EXPR:
822 case BIND_EXPR:
823 /* We might see a MODIFY_EXPR in cases like S() : a((b = 42)), c(b) { }
824 where the initializer for 'a' surreptitiously initializes 'b'. Let's
825 not bother with these complicated scenarios in the front end. */
826 case MODIFY_EXPR:
827 /* Don't attempt to handle statement-expressions, either. */
828 case STATEMENT_LIST:
829 uninitialized->empty ();
830 gcc_fallthrough ();
831 /* If we're just taking the address of an object, it doesn't matter
832 whether it's been initialized. */
833 case ADDR_EXPR:
834 *walk_subtrees = false;
835 return NULL_TREE;
836 default:
837 break;
840 /* We'd need data flow info to avoid false positives. */
841 if (truth_value_p (code))
842 goto give_up;
843 /* Attempt to handle a simple a{b}, but no more. */
844 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
846 if (CONSTRUCTOR_NELTS (init) == 1
847 && !BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (init, 0)->value))
848 init = CONSTRUCTOR_ELT (init, 0)->value;
849 else
850 goto give_up;
852 /* Warn about uninitialized 'this'. */
853 else if (code == CALL_EXPR)
855 tree fn = get_callee_fndecl (init);
856 if (fn && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
858 tree op = CALL_EXPR_ARG (init, 0);
859 if (TREE_CODE (op) == ADDR_EXPR)
860 op = TREE_OPERAND (op, 0);
861 temp_override<tree> ovr (d->member, DECL_ARGUMENTS (fn));
862 cp_walk_tree_without_duplicates (&op, find_uninit_fields_r, data);
864 /* Functions (whether static or nonstatic member) may have side effects
865 and initialize other members; it's not the front end's job to try to
866 figure it out. But don't give up for constructors: we still want to
867 warn when initializing base classes:
869 struct D : public B {
870 int x;
871 D() : B(x) {}
874 so carry on to detect that 'x' is used uninitialized. */
875 if (!fn || !DECL_CONSTRUCTOR_P (fn))
876 goto give_up;
879 /* If we find FIELD in the uninitialized set, we warn. */
880 if (code == COMPONENT_REF)
882 tree field = TREE_OPERAND (init, 1);
883 tree type = TYPE_P (d->member) ? d->member : TREE_TYPE (d->member);
885 /* We're initializing a reference member with itself. */
886 if (TYPE_REF_P (type) && cp_tree_equal (d->member, init))
887 warning_at (EXPR_LOCATION (init), OPT_Winit_self,
888 "%qD is initialized with itself", field);
889 else if (cp_tree_equal (TREE_OPERAND (init, 0), current_class_ref)
890 && uninitialized->contains (field))
892 if (TYPE_REF_P (TREE_TYPE (field)))
893 warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
894 "reference %qD is not yet bound to a value when used "
895 "here", field);
896 else if (!INDIRECT_TYPE_P (type) || is_this_parameter (d->member))
897 warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
898 "member %qD is used uninitialized", field);
899 *walk_subtrees = false;
903 return NULL_TREE;
905 give_up:
906 *walk_subtrees = false;
907 uninitialized->empty ();
908 return integer_zero_node;
911 /* Wrapper around find_uninit_fields_r above. */
913 static void
914 find_uninit_fields (tree *t, hash_set<tree> *uninitialized, tree member)
916 if (!uninitialized->is_empty ())
918 find_uninit_data data = { uninitialized, member };
919 cp_walk_tree_without_duplicates (t, find_uninit_fields_r, &data);
923 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
924 arguments. If TREE_LIST is void_type_node, an empty initializer
925 list was given; if NULL_TREE no initializer was given. UNINITIALIZED
926 is the hash set that tracks uninitialized fields. */
928 static void
929 perform_member_init (tree member, tree init, hash_set<tree> &uninitialized)
931 tree decl;
932 tree type = TREE_TYPE (member);
934 /* Use the non-static data member initializer if there was no
935 mem-initializer for this field. */
936 if (init == NULL_TREE)
937 init = get_nsdmi (member, /*ctor*/true, tf_warning_or_error);
939 if (init == error_mark_node)
940 return;
942 /* Effective C++ rule 12 requires that all data members be
943 initialized. */
944 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
945 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
946 "%qD should be initialized in the member initialization list",
947 member);
949 /* Get an lvalue for the data member. */
950 decl = build_class_member_access_expr (current_class_ref, member,
951 /*access_path=*/NULL_TREE,
952 /*preserve_reference=*/true,
953 tf_warning_or_error);
954 if (decl == error_mark_node)
955 return;
957 if ((warn_init_self || warn_uninitialized)
958 && init
959 && TREE_CODE (init) == TREE_LIST
960 && TREE_CHAIN (init) == NULL_TREE)
962 tree val = TREE_VALUE (init);
963 /* Handle references. */
964 if (REFERENCE_REF_P (val))
965 val = TREE_OPERAND (val, 0);
966 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
967 && TREE_OPERAND (val, 0) == current_class_ref)
968 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
969 OPT_Winit_self, "%qD is initialized with itself",
970 member);
971 else
972 find_uninit_fields (&val, &uninitialized, decl);
975 if (array_of_unknown_bound_p (type))
977 maybe_reject_flexarray_init (member, init);
978 return;
981 if (init && TREE_CODE (init) == TREE_LIST)
983 /* A(): a{e} */
984 if (DIRECT_LIST_INIT_P (TREE_VALUE (init)))
985 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
986 tf_warning_or_error);
987 /* We are trying to initialize an array from a ()-list. If we
988 should attempt to do so, conjure up a CONSTRUCTOR. */
989 else if (TREE_CODE (type) == ARRAY_TYPE
990 /* P0960 is a C++20 feature. */
991 && cxx_dialect >= cxx20)
992 init = do_aggregate_paren_init (init, type);
993 else if (!CLASS_TYPE_P (type))
994 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
995 tf_warning_or_error);
996 /* If we're initializing a class from a ()-list, leave the TREE_LIST
997 alone: we might call an appropriate constructor, or (in C++20)
998 do aggregate-initialization. */
1001 /* Assume we are initializing the member. */
1002 bool member_initialized_p = true;
1004 if (init == void_type_node)
1006 /* mem() means value-initialization. */
1007 if (TREE_CODE (type) == ARRAY_TYPE)
1009 init = build_vec_init_expr (type, init, tf_warning_or_error);
1010 init = build2 (INIT_EXPR, type, decl, init);
1011 finish_expr_stmt (init);
1013 else
1015 tree value = build_value_init (type, tf_warning_or_error);
1016 if (value == error_mark_node)
1017 return;
1018 init = build2 (INIT_EXPR, type, decl, value);
1019 finish_expr_stmt (init);
1022 /* Deal with this here, as we will get confused if we try to call the
1023 assignment op for an anonymous union. This can happen in a
1024 synthesized copy constructor. */
1025 else if (ANON_AGGR_TYPE_P (type))
1027 if (init)
1029 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
1030 finish_expr_stmt (init);
1033 else if (init
1034 && (TYPE_REF_P (type)
1035 || (TREE_CODE (init) == CONSTRUCTOR
1036 && (CP_AGGREGATE_TYPE_P (type)
1037 || is_std_init_list (type)))))
1039 /* With references and list-initialization, we need to deal with
1040 extending temporary lifetimes. 12.2p5: "A temporary bound to a
1041 reference member in a constructor’s ctor-initializer (12.6.2)
1042 persists until the constructor exits." */
1043 unsigned i; tree t;
1044 releasing_vec cleanups;
1045 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1047 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1048 && CP_AGGREGATE_TYPE_P (type))
1049 init = reshape_init (type, init, tf_warning_or_error);
1050 init = digest_init (type, init, tf_warning_or_error);
1052 if (init == error_mark_node)
1053 return;
1054 if (is_empty_field (member)
1055 && !TREE_SIDE_EFFECTS (init))
1056 /* Don't add trivial initialization of an empty base/field, as they
1057 might not be ordered the way the back-end expects. */
1058 return;
1059 /* A FIELD_DECL doesn't really have a suitable lifetime, but
1060 make_temporary_var_for_ref_to_temp will treat it as automatic and
1061 set_up_extended_ref_temp wants to use the decl in a warning. */
1062 init = extend_ref_init_temps (member, init, &cleanups);
1063 if (TREE_CODE (type) == ARRAY_TYPE
1064 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
1065 init = build_vec_init_expr (type, init, tf_warning_or_error);
1066 init = build2 (INIT_EXPR, type, decl, init);
1067 finish_expr_stmt (init);
1068 FOR_EACH_VEC_ELT (*cleanups, i, t)
1069 push_cleanup (decl, t, false);
1071 else if (type_build_ctor_call (type)
1072 || (init && CLASS_TYPE_P (strip_array_types (type))))
1074 if (TREE_CODE (type) == ARRAY_TYPE)
1076 if (init == NULL_TREE
1077 || same_type_ignoring_top_level_qualifiers_p (type,
1078 TREE_TYPE (init)))
1080 if (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1082 /* Initialize the array only if it's not a flexible
1083 array member (i.e., if it has an upper bound). */
1084 init = build_vec_init_expr (type, init, tf_warning_or_error);
1085 init = build2 (INIT_EXPR, type, decl, init);
1086 finish_expr_stmt (init);
1089 else
1090 error ("invalid initializer for array member %q#D", member);
1092 else
1094 int flags = LOOKUP_NORMAL;
1095 if (DECL_DEFAULTED_FN (current_function_decl))
1096 flags |= LOOKUP_DEFAULTED;
1097 if (CP_TYPE_CONST_P (type)
1098 && init == NULL_TREE
1099 && default_init_uninitialized_part (type))
1101 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
1102 vtable; still give this diagnostic. */
1103 auto_diagnostic_group d;
1104 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1105 "uninitialized const member in %q#T", type))
1106 inform (DECL_SOURCE_LOCATION (member),
1107 "%q#D should be initialized", member );
1109 finish_expr_stmt (build_aggr_init (decl, init, flags,
1110 tf_warning_or_error));
1113 else
1115 if (init == NULL_TREE)
1117 tree core_type;
1118 /* member traversal: note it leaves init NULL */
1119 if (TYPE_REF_P (type))
1121 auto_diagnostic_group d;
1122 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1123 "uninitialized reference member in %q#T", type))
1124 inform (DECL_SOURCE_LOCATION (member),
1125 "%q#D should be initialized", member);
1127 else if (CP_TYPE_CONST_P (type))
1129 auto_diagnostic_group d;
1130 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1131 "uninitialized const member in %q#T", type))
1132 inform (DECL_SOURCE_LOCATION (member),
1133 "%q#D should be initialized", member );
1136 core_type = strip_array_types (type);
1138 if (CLASS_TYPE_P (core_type)
1139 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
1140 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
1141 diagnose_uninitialized_cst_or_ref_member (core_type,
1142 /*using_new=*/false,
1143 /*complain=*/true);
1145 /* We left the member uninitialized. */
1146 member_initialized_p = false;
1149 maybe_warn_list_ctor (member, init);
1151 if (init)
1152 finish_expr_stmt (cp_build_modify_expr (input_location, decl,
1153 INIT_EXPR, init,
1154 tf_warning_or_error));
1157 if (member_initialized_p && warn_uninitialized)
1158 /* This member is now initialized, remove it from the uninitialized
1159 set. */
1160 uninitialized.remove (member);
1162 if (type_build_dtor_call (type))
1164 tree expr;
1166 expr = build_class_member_access_expr (current_class_ref, member,
1167 /*access_path=*/NULL_TREE,
1168 /*preserve_reference=*/false,
1169 tf_warning_or_error);
1170 expr = build_delete (input_location,
1171 type, expr, sfk_complete_destructor,
1172 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
1173 tf_warning_or_error);
1175 if (expr != error_mark_node
1176 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1177 finish_eh_cleanup (expr);
1181 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
1182 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
1184 static tree
1185 build_field_list (tree t, tree list, int *uses_unions_or_anon_p)
1187 tree fields;
1189 /* Note whether or not T is a union. */
1190 if (TREE_CODE (t) == UNION_TYPE)
1191 *uses_unions_or_anon_p = 1;
1193 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
1195 tree fieldtype;
1197 /* Skip CONST_DECLs for enumeration constants and so forth. */
1198 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
1199 continue;
1201 fieldtype = TREE_TYPE (fields);
1203 /* For an anonymous struct or union, we must recursively
1204 consider the fields of the anonymous type. They can be
1205 directly initialized from the constructor. */
1206 if (ANON_AGGR_TYPE_P (fieldtype))
1208 /* Add this field itself. Synthesized copy constructors
1209 initialize the entire aggregate. */
1210 list = tree_cons (fields, NULL_TREE, list);
1211 /* And now add the fields in the anonymous aggregate. */
1212 list = build_field_list (fieldtype, list, uses_unions_or_anon_p);
1213 *uses_unions_or_anon_p = 1;
1215 /* Add this field. */
1216 else if (DECL_NAME (fields))
1217 list = tree_cons (fields, NULL_TREE, list);
1220 return list;
1223 /* Return the innermost aggregate scope for FIELD, whether that is
1224 the enclosing class or an anonymous aggregate within it. */
1226 static tree
1227 innermost_aggr_scope (tree field)
1229 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1230 return TREE_TYPE (field);
1231 else
1232 return DECL_CONTEXT (field);
1235 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
1236 a FIELD_DECL or BINFO in T that needs initialization. The
1237 TREE_VALUE gives the initializer, or list of initializer arguments.
1239 Return a TREE_LIST containing all of the initializations required
1240 for T, in the order in which they should be performed. The output
1241 list has the same format as the input. */
1243 static tree
1244 sort_mem_initializers (tree t, tree mem_inits)
1246 tree init;
1247 tree base, binfo, base_binfo;
1248 tree sorted_inits;
1249 tree next_subobject;
1250 vec<tree, va_gc> *vbases;
1251 int i;
1252 int uses_unions_or_anon_p = 0;
1254 /* Build up a list of initializations. The TREE_PURPOSE of entry
1255 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
1256 TREE_VALUE will be the constructor arguments, or NULL if no
1257 explicit initialization was provided. */
1258 sorted_inits = NULL_TREE;
1260 /* Process the virtual bases. */
1261 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
1262 vec_safe_iterate (vbases, i, &base); i++)
1263 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
1265 /* Process the direct bases. */
1266 for (binfo = TYPE_BINFO (t), i = 0;
1267 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1268 if (!BINFO_VIRTUAL_P (base_binfo))
1269 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
1271 /* Process the non-static data members. */
1272 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_or_anon_p);
1273 /* Reverse the entire list of initializations, so that they are in
1274 the order that they will actually be performed. */
1275 sorted_inits = nreverse (sorted_inits);
1277 /* If the user presented the initializers in an order different from
1278 that in which they will actually occur, we issue a warning. Keep
1279 track of the next subobject which can be explicitly initialized
1280 without issuing a warning. */
1281 next_subobject = sorted_inits;
1283 /* Go through the explicit initializers, filling in TREE_PURPOSE in
1284 the SORTED_INITS. */
1285 for (init = mem_inits; init; init = TREE_CHAIN (init))
1287 tree subobject;
1288 tree subobject_init;
1290 subobject = TREE_PURPOSE (init);
1292 /* If the explicit initializers are in sorted order, then
1293 SUBOBJECT will be NEXT_SUBOBJECT, or something following
1294 it. */
1295 for (subobject_init = next_subobject;
1296 subobject_init;
1297 subobject_init = TREE_CHAIN (subobject_init))
1298 if (TREE_PURPOSE (subobject_init) == subobject)
1299 break;
1301 /* Issue a warning if the explicit initializer order does not
1302 match that which will actually occur.
1303 ??? Are all these on the correct lines? */
1304 if (warn_reorder && !subobject_init)
1306 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
1307 warning_at (DECL_SOURCE_LOCATION (TREE_PURPOSE (next_subobject)),
1308 OPT_Wreorder, "%qD will be initialized after",
1309 TREE_PURPOSE (next_subobject));
1310 else
1311 warning (OPT_Wreorder, "base %qT will be initialized after",
1312 TREE_PURPOSE (next_subobject));
1313 if (TREE_CODE (subobject) == FIELD_DECL)
1314 warning_at (DECL_SOURCE_LOCATION (subobject),
1315 OPT_Wreorder, " %q#D", subobject);
1316 else
1317 warning (OPT_Wreorder, " base %qT", subobject);
1318 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1319 OPT_Wreorder, " when initialized here");
1322 /* Look again, from the beginning of the list. */
1323 if (!subobject_init)
1325 subobject_init = sorted_inits;
1326 while (TREE_PURPOSE (subobject_init) != subobject)
1327 subobject_init = TREE_CHAIN (subobject_init);
1330 /* It is invalid to initialize the same subobject more than
1331 once. */
1332 if (TREE_VALUE (subobject_init))
1334 if (TREE_CODE (subobject) == FIELD_DECL)
1335 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1336 "multiple initializations given for %qD",
1337 subobject);
1338 else
1339 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1340 "multiple initializations given for base %qT",
1341 subobject);
1344 /* Record the initialization. */
1345 TREE_VALUE (subobject_init) = TREE_VALUE (init);
1346 /* Carry over the dummy TREE_TYPE node containing the source location. */
1347 TREE_TYPE (subobject_init) = TREE_TYPE (init);
1348 next_subobject = subobject_init;
1351 /* [class.base.init]
1353 If a ctor-initializer specifies more than one mem-initializer for
1354 multiple members of the same union (including members of
1355 anonymous unions), the ctor-initializer is ill-formed.
1357 Here we also splice out uninitialized union members. */
1358 if (uses_unions_or_anon_p)
1360 tree *last_p = NULL;
1361 tree *p;
1362 for (p = &sorted_inits; *p; )
1364 tree field;
1365 tree ctx;
1367 init = *p;
1369 field = TREE_PURPOSE (init);
1371 /* Skip base classes. */
1372 if (TREE_CODE (field) != FIELD_DECL)
1373 goto next;
1375 /* If this is an anonymous aggregate with no explicit initializer,
1376 splice it out. */
1377 if (!TREE_VALUE (init) && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1378 goto splice;
1380 /* See if this field is a member of a union, or a member of a
1381 structure contained in a union, etc. */
1382 ctx = innermost_aggr_scope (field);
1384 /* If this field is not a member of a union, skip it. */
1385 if (TREE_CODE (ctx) != UNION_TYPE
1386 && !ANON_AGGR_TYPE_P (ctx))
1387 goto next;
1389 /* If this union member has no explicit initializer and no NSDMI,
1390 splice it out. */
1391 if (TREE_VALUE (init) || DECL_INITIAL (field))
1392 /* OK. */;
1393 else
1394 goto splice;
1396 /* It's only an error if we have two initializers for the same
1397 union type. */
1398 if (!last_p)
1400 last_p = p;
1401 goto next;
1404 /* See if LAST_FIELD and the field initialized by INIT are
1405 members of the same union (or the union itself). If so, there's
1406 a problem, unless they're actually members of the same structure
1407 which is itself a member of a union. For example, given:
1409 union { struct { int i; int j; }; };
1411 initializing both `i' and `j' makes sense. */
1412 ctx = common_enclosing_class
1413 (innermost_aggr_scope (field),
1414 innermost_aggr_scope (TREE_PURPOSE (*last_p)));
1416 if (ctx && (TREE_CODE (ctx) == UNION_TYPE
1417 || ctx == TREE_TYPE (TREE_PURPOSE (*last_p))))
1419 /* A mem-initializer hides an NSDMI. */
1420 if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
1421 *last_p = TREE_CHAIN (*last_p);
1422 else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
1423 goto splice;
1424 else
1426 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1427 "initializations for multiple members of %qT",
1428 ctx);
1429 goto splice;
1433 last_p = p;
1435 next:
1436 p = &TREE_CHAIN (*p);
1437 continue;
1438 splice:
1439 *p = TREE_CHAIN (*p);
1440 continue;
1444 return sorted_inits;
1447 /* Callback for cp_walk_tree to mark all PARM_DECLs in a tree as read. */
1449 static tree
1450 mark_exp_read_r (tree *tp, int *, void *)
1452 tree t = *tp;
1453 if (TREE_CODE (t) == PARM_DECL)
1454 mark_exp_read (t);
1455 return NULL_TREE;
1458 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1459 is a TREE_LIST giving the explicit mem-initializer-list for the
1460 constructor. The TREE_PURPOSE of each entry is a subobject (a
1461 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1462 is a TREE_LIST giving the arguments to the constructor or
1463 void_type_node for an empty list of arguments. */
1465 void
1466 emit_mem_initializers (tree mem_inits)
1468 int flags = LOOKUP_NORMAL;
1470 /* We will already have issued an error message about the fact that
1471 the type is incomplete. */
1472 if (!COMPLETE_TYPE_P (current_class_type))
1473 return;
1475 /* Keep a set holding fields that are not initialized. */
1476 hash_set<tree> uninitialized;
1478 /* Initially that is all of them. */
1479 if (warn_uninitialized)
1480 for (tree f = next_initializable_field (TYPE_FIELDS (current_class_type));
1481 f != NULL_TREE;
1482 f = next_initializable_field (DECL_CHAIN (f)))
1483 if (!DECL_ARTIFICIAL (f)
1484 && !is_really_empty_class (TREE_TYPE (f), /*ignore_vptr*/false))
1485 uninitialized.add (f);
1487 if (mem_inits
1488 && TYPE_P (TREE_PURPOSE (mem_inits))
1489 && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1491 /* Delegating constructor. */
1492 gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
1493 tree ctor = perform_target_ctor (TREE_VALUE (mem_inits));
1494 find_uninit_fields (&ctor, &uninitialized, current_class_type);
1495 return;
1498 if (DECL_DEFAULTED_FN (current_function_decl)
1499 && ! DECL_INHERITED_CTOR (current_function_decl))
1500 flags |= LOOKUP_DEFAULTED;
1502 /* Sort the mem-initializers into the order in which the
1503 initializations should be performed. */
1504 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
1506 in_base_initializer = 1;
1508 /* Initialize base classes. */
1509 for (; (mem_inits
1510 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
1511 mem_inits = TREE_CHAIN (mem_inits))
1513 tree subobject = TREE_PURPOSE (mem_inits);
1514 tree arguments = TREE_VALUE (mem_inits);
1516 /* We already have issued an error message. */
1517 if (arguments == error_mark_node)
1518 continue;
1520 /* Suppress access control when calling the inherited ctor. */
1521 bool inherited_base = (DECL_INHERITED_CTOR (current_function_decl)
1522 && flag_new_inheriting_ctors
1523 && arguments);
1524 if (inherited_base)
1525 push_deferring_access_checks (dk_deferred);
1527 if (arguments == NULL_TREE)
1529 /* If these initializations are taking place in a copy constructor,
1530 the base class should probably be explicitly initialized if there
1531 is a user-defined constructor in the base class (other than the
1532 default constructor, which will be called anyway). */
1533 if (extra_warnings
1534 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1535 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1536 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1537 OPT_Wextra, "base class %q#T should be explicitly "
1538 "initialized in the copy constructor",
1539 BINFO_TYPE (subobject));
1542 /* Initialize the base. */
1543 if (!BINFO_VIRTUAL_P (subobject))
1545 tree base_addr;
1547 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1548 subobject, 1, tf_warning_or_error);
1549 expand_aggr_init_1 (subobject, NULL_TREE,
1550 cp_build_fold_indirect_ref (base_addr),
1551 arguments,
1552 flags,
1553 tf_warning_or_error);
1554 expand_cleanup_for_base (subobject, NULL_TREE);
1555 if (STATEMENT_LIST_TAIL (cur_stmt_list))
1556 find_uninit_fields (&STATEMENT_LIST_TAIL (cur_stmt_list)->stmt,
1557 &uninitialized, BINFO_TYPE (subobject));
1559 else if (!ABSTRACT_CLASS_TYPE_P (current_class_type))
1560 /* C++14 DR1658 Means we do not have to construct vbases of
1561 abstract classes. */
1562 construct_virtual_base (subobject, arguments);
1563 else
1564 /* When not constructing vbases of abstract classes, at least mark
1565 the arguments expressions as read to avoid
1566 -Wunused-but-set-parameter false positives. */
1567 cp_walk_tree (&arguments, mark_exp_read_r, NULL, NULL);
1569 if (inherited_base)
1570 pop_deferring_access_checks ();
1572 in_base_initializer = 0;
1574 /* Initialize the vptrs. */
1575 initialize_vtbl_ptrs (current_class_ptr);
1577 /* Initialize the data members. */
1578 while (mem_inits)
1580 /* If this initializer was explicitly provided, then the dummy TREE_TYPE
1581 node contains the source location. */
1582 iloc_sentinel ils (EXPR_LOCATION (TREE_TYPE (mem_inits)));
1584 perform_member_init (TREE_PURPOSE (mem_inits),
1585 TREE_VALUE (mem_inits),
1586 uninitialized);
1588 mem_inits = TREE_CHAIN (mem_inits);
1592 /* Returns the address of the vtable (i.e., the value that should be
1593 assigned to the vptr) for BINFO. */
1595 tree
1596 build_vtbl_address (tree binfo)
1598 tree binfo_for = binfo;
1599 tree vtbl;
1601 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1602 /* If this is a virtual primary base, then the vtable we want to store
1603 is that for the base this is being used as the primary base of. We
1604 can't simply skip the initialization, because we may be expanding the
1605 inits of a subobject constructor where the virtual base layout
1606 can be different. */
1607 while (BINFO_PRIMARY_P (binfo_for))
1608 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1610 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1611 used. */
1612 vtbl = get_vtbl_decl_for_binfo (binfo_for);
1613 TREE_USED (vtbl) = true;
1615 /* Now compute the address to use when initializing the vptr. */
1616 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1617 if (VAR_P (vtbl))
1618 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1620 return vtbl;
1623 /* This code sets up the virtual function tables appropriate for
1624 the pointer DECL. It is a one-ply initialization.
1626 BINFO is the exact type that DECL is supposed to be. In
1627 multiple inheritance, this might mean "C's A" if C : A, B. */
1629 static void
1630 expand_virtual_init (tree binfo, tree decl)
1632 tree vtbl, vtbl_ptr;
1633 tree vtt_index;
1635 /* Compute the initializer for vptr. */
1636 vtbl = build_vtbl_address (binfo);
1638 /* We may get this vptr from a VTT, if this is a subobject
1639 constructor or subobject destructor. */
1640 vtt_index = BINFO_VPTR_INDEX (binfo);
1641 if (vtt_index)
1643 tree vtbl2;
1644 tree vtt_parm;
1646 /* Compute the value to use, when there's a VTT. */
1647 vtt_parm = current_vtt_parm;
1648 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1649 vtbl2 = cp_build_fold_indirect_ref (vtbl2);
1650 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1652 /* The actual initializer is the VTT value only in the subobject
1653 constructor. In maybe_clone_body we'll substitute NULL for
1654 the vtt_parm in the case of the non-subobject constructor. */
1655 vtbl = build_if_in_charge (vtbl, vtbl2);
1658 /* Compute the location of the vtpr. */
1659 vtbl_ptr = build_vfield_ref (cp_build_fold_indirect_ref (decl),
1660 TREE_TYPE (binfo));
1661 gcc_assert (vtbl_ptr != error_mark_node);
1663 /* Assign the vtable to the vptr. */
1664 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
1665 finish_expr_stmt (cp_build_modify_expr (input_location, vtbl_ptr, NOP_EXPR,
1666 vtbl, tf_warning_or_error));
1669 /* If an exception is thrown in a constructor, those base classes already
1670 constructed must be destroyed. This function creates the cleanup
1671 for BINFO, which has just been constructed. If FLAG is non-NULL,
1672 it is a DECL which is nonzero when this base needs to be
1673 destroyed. */
1675 static void
1676 expand_cleanup_for_base (tree binfo, tree flag)
1678 tree expr;
1680 if (!type_build_dtor_call (BINFO_TYPE (binfo)))
1681 return;
1683 /* Call the destructor. */
1684 expr = build_special_member_call (current_class_ref,
1685 base_dtor_identifier,
1686 NULL,
1687 binfo,
1688 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1689 tf_warning_or_error);
1691 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1692 return;
1694 if (flag)
1695 expr = fold_build3_loc (input_location,
1696 COND_EXPR, void_type_node,
1697 c_common_truthvalue_conversion (input_location, flag),
1698 expr, integer_zero_node);
1700 finish_eh_cleanup (expr);
1703 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1704 constructor. */
1706 static void
1707 construct_virtual_base (tree vbase, tree arguments)
1709 tree inner_if_stmt;
1710 tree exp;
1711 tree flag;
1713 /* If there are virtual base classes with destructors, we need to
1714 emit cleanups to destroy them if an exception is thrown during
1715 the construction process. These exception regions (i.e., the
1716 period during which the cleanups must occur) begin from the time
1717 the construction is complete to the end of the function. If we
1718 create a conditional block in which to initialize the
1719 base-classes, then the cleanup region for the virtual base begins
1720 inside a block, and ends outside of that block. This situation
1721 confuses the sjlj exception-handling code. Therefore, we do not
1722 create a single conditional block, but one for each
1723 initialization. (That way the cleanup regions always begin
1724 in the outer block.) We trust the back end to figure out
1725 that the FLAG will not change across initializations, and
1726 avoid doing multiple tests. */
1727 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1728 inner_if_stmt = begin_if_stmt ();
1729 finish_if_stmt_cond (flag, inner_if_stmt);
1731 /* Compute the location of the virtual base. If we're
1732 constructing virtual bases, then we must be the most derived
1733 class. Therefore, we don't have to look up the virtual base;
1734 we already know where it is. */
1735 exp = convert_to_base_statically (current_class_ref, vbase);
1737 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1738 0, tf_warning_or_error);
1739 finish_then_clause (inner_if_stmt);
1740 finish_if_stmt (inner_if_stmt);
1742 expand_cleanup_for_base (vbase, flag);
1745 /* Find the context in which this FIELD can be initialized. */
1747 static tree
1748 initializing_context (tree field)
1750 tree t = DECL_CONTEXT (field);
1752 /* Anonymous union members can be initialized in the first enclosing
1753 non-anonymous union context. */
1754 while (t && ANON_AGGR_TYPE_P (t))
1755 t = TYPE_CONTEXT (t);
1756 return t;
1759 /* Function to give error message if member initialization specification
1760 is erroneous. FIELD is the member we decided to initialize.
1761 TYPE is the type for which the initialization is being performed.
1762 FIELD must be a member of TYPE.
1764 MEMBER_NAME is the name of the member. */
1766 static int
1767 member_init_ok_or_else (tree field, tree type, tree member_name)
1769 if (field == error_mark_node)
1770 return 0;
1771 if (!field)
1773 error ("class %qT does not have any field named %qD", type,
1774 member_name);
1775 return 0;
1777 if (VAR_P (field))
1779 error ("%q#D is a static data member; it can only be "
1780 "initialized at its definition",
1781 field);
1782 return 0;
1784 if (TREE_CODE (field) != FIELD_DECL)
1786 error ("%q#D is not a non-static data member of %qT",
1787 field, type);
1788 return 0;
1790 if (initializing_context (field) != type)
1792 error ("class %qT does not have any field named %qD", type,
1793 member_name);
1794 return 0;
1797 return 1;
1800 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1801 is a _TYPE node or TYPE_DECL which names a base for that type.
1802 Check the validity of NAME, and return either the base _TYPE, base
1803 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1804 NULL_TREE and issue a diagnostic.
1806 An old style unnamed direct single base construction is permitted,
1807 where NAME is NULL. */
1809 tree
1810 expand_member_init (tree name)
1812 tree basetype;
1813 tree field;
1815 if (!current_class_ref)
1816 return NULL_TREE;
1818 if (!name)
1820 /* This is an obsolete unnamed base class initializer. The
1821 parser will already have warned about its use. */
1822 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1824 case 0:
1825 error ("unnamed initializer for %qT, which has no base classes",
1826 current_class_type);
1827 return NULL_TREE;
1828 case 1:
1829 basetype = BINFO_TYPE
1830 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1831 break;
1832 default:
1833 error ("unnamed initializer for %qT, which uses multiple inheritance",
1834 current_class_type);
1835 return NULL_TREE;
1838 else if (TYPE_P (name))
1840 basetype = TYPE_MAIN_VARIANT (name);
1841 name = TYPE_NAME (name);
1843 else if (TREE_CODE (name) == TYPE_DECL)
1844 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1845 else
1846 basetype = NULL_TREE;
1848 if (basetype)
1850 tree class_binfo;
1851 tree direct_binfo;
1852 tree virtual_binfo;
1853 int i;
1855 if (current_template_parms
1856 || same_type_p (basetype, current_class_type))
1857 return basetype;
1859 class_binfo = TYPE_BINFO (current_class_type);
1860 direct_binfo = NULL_TREE;
1861 virtual_binfo = NULL_TREE;
1863 /* Look for a direct base. */
1864 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1865 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1866 break;
1868 /* Look for a virtual base -- unless the direct base is itself
1869 virtual. */
1870 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1871 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1873 /* [class.base.init]
1875 If a mem-initializer-id is ambiguous because it designates
1876 both a direct non-virtual base class and an inherited virtual
1877 base class, the mem-initializer is ill-formed. */
1878 if (direct_binfo && virtual_binfo)
1880 error ("%qD is both a direct base and an indirect virtual base",
1881 basetype);
1882 return NULL_TREE;
1885 if (!direct_binfo && !virtual_binfo)
1887 if (CLASSTYPE_VBASECLASSES (current_class_type))
1888 error ("type %qT is not a direct or virtual base of %qT",
1889 basetype, current_class_type);
1890 else
1891 error ("type %qT is not a direct base of %qT",
1892 basetype, current_class_type);
1893 return NULL_TREE;
1896 return direct_binfo ? direct_binfo : virtual_binfo;
1898 else
1900 if (identifier_p (name))
1901 field = lookup_field (current_class_type, name, 1, false);
1902 else
1903 field = name;
1905 if (member_init_ok_or_else (field, current_class_type, name))
1906 return field;
1909 return NULL_TREE;
1912 /* This is like `expand_member_init', only it stores one aggregate
1913 value into another.
1915 INIT comes in two flavors: it is either a value which
1916 is to be stored in EXP, or it is a parameter list
1917 to go to a constructor, which will operate on EXP.
1918 If INIT is not a parameter list for a constructor, then set
1919 LOOKUP_ONLYCONVERTING.
1920 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1921 the initializer, if FLAGS is 0, then it is the (init) form.
1922 If `init' is a CONSTRUCTOR, then we emit a warning message,
1923 explaining that such initializations are invalid.
1925 If INIT resolves to a CALL_EXPR which happens to return
1926 something of the type we are looking for, then we know
1927 that we can safely use that call to perform the
1928 initialization.
1930 The virtual function table pointer cannot be set up here, because
1931 we do not really know its type.
1933 This never calls operator=().
1935 When initializing, nothing is CONST.
1937 A default copy constructor may have to be used to perform the
1938 initialization.
1940 A constructor or a conversion operator may have to be used to
1941 perform the initialization, but not both, as it would be ambiguous. */
1943 tree
1944 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1946 tree stmt_expr;
1947 tree compound_stmt;
1948 int destroy_temps;
1949 tree type = TREE_TYPE (exp);
1950 int was_const = TREE_READONLY (exp);
1951 int was_volatile = TREE_THIS_VOLATILE (exp);
1952 int is_global;
1954 if (init == error_mark_node)
1955 return error_mark_node;
1957 location_t init_loc = (init
1958 ? cp_expr_loc_or_input_loc (init)
1959 : location_of (exp));
1961 TREE_READONLY (exp) = 0;
1962 TREE_THIS_VOLATILE (exp) = 0;
1964 if (TREE_CODE (type) == ARRAY_TYPE)
1966 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1967 int from_array = 0;
1969 if (VAR_P (exp) && DECL_DECOMPOSITION_P (exp))
1971 from_array = 1;
1972 init = mark_rvalue_use (init);
1973 if (init
1974 && DECL_P (tree_strip_any_location_wrapper (init))
1975 && !(flags & LOOKUP_ONLYCONVERTING))
1977 /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init
1978 recognizes it as direct-initialization. */
1979 init = build_constructor_single (init_list_type_node,
1980 NULL_TREE, init);
1981 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
1984 else
1986 /* Must arrange to initialize each element of EXP
1987 from elements of INIT. */
1988 if (cv_qualified_p (type))
1989 TREE_TYPE (exp) = cv_unqualified (type);
1990 if (itype && cv_qualified_p (itype))
1991 TREE_TYPE (init) = cv_unqualified (itype);
1992 from_array = (itype && same_type_p (TREE_TYPE (init),
1993 TREE_TYPE (exp)));
1995 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)
1996 && (!from_array
1997 || (TREE_CODE (init) != CONSTRUCTOR
1998 /* Can happen, eg, handling the compound-literals
1999 extension (ext/complit12.C). */
2000 && TREE_CODE (init) != TARGET_EXPR)))
2002 if (complain & tf_error)
2003 error_at (init_loc, "array must be initialized "
2004 "with a brace-enclosed initializer");
2005 return error_mark_node;
2009 stmt_expr = build_vec_init (exp, NULL_TREE, init,
2010 /*explicit_value_init_p=*/false,
2011 from_array,
2012 complain);
2013 TREE_READONLY (exp) = was_const;
2014 TREE_THIS_VOLATILE (exp) = was_volatile;
2015 TREE_TYPE (exp) = type;
2016 /* Restore the type of init unless it was used directly. */
2017 if (init && TREE_CODE (stmt_expr) != INIT_EXPR)
2018 TREE_TYPE (init) = itype;
2019 return stmt_expr;
2022 if (is_copy_initialization (init))
2023 flags |= LOOKUP_ONLYCONVERTING;
2025 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
2026 destroy_temps = stmts_are_full_exprs_p ();
2027 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2028 bool ok = expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
2029 init, LOOKUP_NORMAL|flags, complain);
2030 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
2031 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
2032 TREE_READONLY (exp) = was_const;
2033 TREE_THIS_VOLATILE (exp) = was_volatile;
2034 if (!ok)
2035 return error_mark_node;
2037 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
2038 && TREE_SIDE_EFFECTS (stmt_expr)
2039 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
2040 /* Just know that we've seen something for this node. */
2041 TREE_USED (exp) = 1;
2043 return stmt_expr;
2046 static bool
2047 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
2048 tsubst_flags_t complain)
2050 tree type = TREE_TYPE (exp);
2052 /* It fails because there may not be a constructor which takes
2053 its own type as the first (or only parameter), but which does
2054 take other types via a conversion. So, if the thing initializing
2055 the expression is a unit element of type X, first try X(X&),
2056 followed by initialization by X. If neither of these work
2057 out, then look hard. */
2058 tree rval;
2059 vec<tree, va_gc> *parms;
2061 /* If we have direct-initialization from an initializer list, pull
2062 it out of the TREE_LIST so the code below can see it. */
2063 if (init && TREE_CODE (init) == TREE_LIST
2064 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2066 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
2067 && TREE_CHAIN (init) == NULL_TREE);
2068 init = TREE_VALUE (init);
2069 /* Only call reshape_init if it has not been called earlier
2070 by the callers. */
2071 if (BRACE_ENCLOSED_INITIALIZER_P (init) && CP_AGGREGATE_TYPE_P (type))
2072 init = reshape_init (type, init, complain);
2075 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
2076 && CP_AGGREGATE_TYPE_P (type))
2077 /* A brace-enclosed initializer for an aggregate. In C++0x this can
2078 happen for direct-initialization, too. */
2079 init = digest_init (type, init, complain);
2081 if (init == error_mark_node)
2082 return false;
2084 /* A CONSTRUCTOR of the target's type is a previously digested
2085 initializer, whether that happened just above or in
2086 cp_parser_late_parsing_nsdmi.
2088 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
2089 set represents the whole initialization, so we shouldn't build up
2090 another ctor call. */
2091 if (init
2092 && (TREE_CODE (init) == CONSTRUCTOR
2093 || (TREE_CODE (init) == TARGET_EXPR
2094 && (TARGET_EXPR_DIRECT_INIT_P (init)
2095 || TARGET_EXPR_LIST_INIT_P (init))))
2096 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
2098 /* Early initialization via a TARGET_EXPR only works for
2099 complete objects. */
2100 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
2102 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
2103 TREE_SIDE_EFFECTS (init) = 1;
2104 finish_expr_stmt (init);
2105 return true;
2108 if (init && TREE_CODE (init) != TREE_LIST
2109 && (flags & LOOKUP_ONLYCONVERTING)
2110 && !unsafe_return_slot_p (exp))
2112 /* Base subobjects should only get direct-initialization. */
2113 gcc_assert (true_exp == exp);
2115 if (flags & DIRECT_BIND)
2116 /* Do nothing. We hit this in two cases: Reference initialization,
2117 where we aren't initializing a real variable, so we don't want
2118 to run a new constructor; and catching an exception, where we
2119 have already built up the constructor call so we could wrap it
2120 in an exception region. */;
2121 else
2123 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
2124 flags, complain | tf_no_cleanup);
2125 if (init == error_mark_node)
2126 return false;
2129 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
2130 /* We need to protect the initialization of a catch parm with a
2131 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
2132 around the TARGET_EXPR for the copy constructor. See
2133 initialize_handler_parm. */
2135 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
2136 TREE_OPERAND (init, 0));
2137 TREE_TYPE (init) = void_type_node;
2139 else
2140 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
2141 TREE_SIDE_EFFECTS (init) = 1;
2142 finish_expr_stmt (init);
2143 return true;
2146 if (init == NULL_TREE)
2147 parms = NULL;
2148 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
2150 parms = make_tree_vector ();
2151 for (; init != NULL_TREE; init = TREE_CHAIN (init))
2152 vec_safe_push (parms, TREE_VALUE (init));
2154 else
2155 parms = make_tree_vector_single (init);
2157 if (exp == current_class_ref && current_function_decl
2158 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
2160 /* Delegating constructor. */
2161 tree complete;
2162 tree base;
2163 tree elt; unsigned i;
2165 /* Unshare the arguments for the second call. */
2166 releasing_vec parms2;
2167 FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
2169 elt = break_out_target_exprs (elt);
2170 vec_safe_push (parms2, elt);
2172 complete = build_special_member_call (exp, complete_ctor_identifier,
2173 &parms2, binfo, flags,
2174 complain);
2175 complete = fold_build_cleanup_point_expr (void_type_node, complete);
2177 base = build_special_member_call (exp, base_ctor_identifier,
2178 &parms, binfo, flags,
2179 complain);
2180 base = fold_build_cleanup_point_expr (void_type_node, base);
2181 if (complete == error_mark_node || base == error_mark_node)
2182 return false;
2183 rval = build_if_in_charge (complete, base);
2185 else
2187 tree ctor_name = (true_exp == exp
2188 ? complete_ctor_identifier : base_ctor_identifier);
2190 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
2191 complain);
2192 if (rval == error_mark_node)
2193 return false;
2196 if (parms != NULL)
2197 release_tree_vector (parms);
2199 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
2201 tree fn = get_callee_fndecl (rval);
2202 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
2204 tree e = maybe_constant_init (rval, exp);
2205 if (TREE_CONSTANT (e))
2206 rval = build2 (INIT_EXPR, type, exp, e);
2210 /* FIXME put back convert_to_void? */
2211 if (TREE_SIDE_EFFECTS (rval))
2212 finish_expr_stmt (rval);
2214 return true;
2217 /* This function is responsible for initializing EXP with INIT
2218 (if any). Returns true on success, false on failure.
2220 BINFO is the binfo of the type for who we are performing the
2221 initialization. For example, if W is a virtual base class of A and B,
2222 and C : A, B.
2223 If we are initializing B, then W must contain B's W vtable, whereas
2224 were we initializing C, W must contain C's W vtable.
2226 TRUE_EXP is nonzero if it is the true expression being initialized.
2227 In this case, it may be EXP, or may just contain EXP. The reason we
2228 need this is because if EXP is a base element of TRUE_EXP, we
2229 don't necessarily know by looking at EXP where its virtual
2230 baseclass fields should really be pointing. But we do know
2231 from TRUE_EXP. In constructors, we don't know anything about
2232 the value being initialized.
2234 FLAGS is just passed to `build_new_method_call'. See that function
2235 for its description. */
2237 static bool
2238 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
2239 tsubst_flags_t complain)
2241 tree type = TREE_TYPE (exp);
2243 gcc_assert (init != error_mark_node && type != error_mark_node);
2244 gcc_assert (building_stmt_list_p ());
2246 /* Use a function returning the desired type to initialize EXP for us.
2247 If the function is a constructor, and its first argument is
2248 NULL_TREE, know that it was meant for us--just slide exp on
2249 in and expand the constructor. Constructors now come
2250 as TARGET_EXPRs. */
2252 if (init && VAR_P (exp)
2253 && COMPOUND_LITERAL_P (init))
2255 vec<tree, va_gc> *cleanups = NULL;
2256 /* If store_init_value returns NULL_TREE, the INIT has been
2257 recorded as the DECL_INITIAL for EXP. That means there's
2258 nothing more we have to do. */
2259 init = store_init_value (exp, init, &cleanups, flags);
2260 if (init)
2261 finish_expr_stmt (init);
2262 gcc_assert (!cleanups);
2263 return true;
2266 /* List-initialization from {} becomes value-initialization for non-aggregate
2267 classes with default constructors. Handle this here when we're
2268 initializing a base, so protected access works. */
2269 if (exp != true_exp && init && TREE_CODE (init) == TREE_LIST)
2271 tree elt = TREE_VALUE (init);
2272 if (DIRECT_LIST_INIT_P (elt)
2273 && CONSTRUCTOR_ELTS (elt) == 0
2274 && CLASSTYPE_NON_AGGREGATE (type)
2275 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2276 init = void_type_node;
2279 /* If an explicit -- but empty -- initializer list was present,
2280 that's value-initialization. */
2281 if (init == void_type_node)
2283 /* If the type has data but no user-provided default ctor, we need to zero
2284 out the object. */
2285 if (type_has_non_user_provided_default_constructor (type)
2286 && !is_really_empty_class (type, /*ignore_vptr*/true))
2288 tree field_size = NULL_TREE;
2289 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
2290 /* Don't clobber already initialized virtual bases. */
2291 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
2292 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
2293 field_size);
2294 init = build2 (INIT_EXPR, type, exp, init);
2295 finish_expr_stmt (init);
2298 /* If we don't need to mess with the constructor at all,
2299 then we're done. */
2300 if (! type_build_ctor_call (type))
2301 return true;
2303 /* Otherwise fall through and call the constructor. */
2304 init = NULL_TREE;
2307 /* We know that expand_default_init can handle everything we want
2308 at this point. */
2309 return expand_default_init (binfo, true_exp, exp, init, flags, complain);
2312 /* Report an error if TYPE is not a user-defined, class type. If
2313 OR_ELSE is nonzero, give an error message. */
2316 is_class_type (tree type, int or_else)
2318 if (type == error_mark_node)
2319 return 0;
2321 if (! CLASS_TYPE_P (type))
2323 if (or_else)
2324 error ("%qT is not a class type", type);
2325 return 0;
2327 return 1;
2330 /* Returns true iff the initializer INIT represents copy-initialization
2331 (and therefore we must set LOOKUP_ONLYCONVERTING when processing it). */
2333 bool
2334 is_copy_initialization (tree init)
2336 return (init && init != void_type_node
2337 && TREE_CODE (init) != TREE_LIST
2338 && !(TREE_CODE (init) == TARGET_EXPR
2339 && TARGET_EXPR_DIRECT_INIT_P (init))
2340 && !DIRECT_LIST_INIT_P (init));
2343 /* Build a reference to a member of an aggregate. This is not a C++
2344 `&', but really something which can have its address taken, and
2345 then act as a pointer to member, for example TYPE :: FIELD can have
2346 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
2347 this expression is the operand of "&".
2349 @@ Prints out lousy diagnostics for operator <typename>
2350 @@ fields.
2352 @@ This function should be rewritten and placed in search.cc. */
2354 tree
2355 build_offset_ref (tree type, tree member, bool address_p,
2356 tsubst_flags_t complain)
2358 tree decl;
2359 tree basebinfo = NULL_TREE;
2361 /* class templates can come in as TEMPLATE_DECLs here. */
2362 if (TREE_CODE (member) == TEMPLATE_DECL)
2363 return member;
2365 if (dependent_scope_p (type) || type_dependent_expression_p (member))
2366 return build_qualified_name (NULL_TREE, type, member,
2367 /*template_p=*/false);
2369 gcc_assert (TYPE_P (type));
2370 if (! is_class_type (type, 1))
2371 return error_mark_node;
2373 gcc_assert (DECL_P (member) || BASELINK_P (member));
2374 /* Callers should call mark_used before this point, except for functions. */
2375 gcc_assert (!DECL_P (member) || TREE_USED (member)
2376 || TREE_CODE (member) == FUNCTION_DECL);
2378 type = TYPE_MAIN_VARIANT (type);
2379 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
2381 if (complain & tf_error)
2382 error ("incomplete type %qT does not have member %qD", type, member);
2383 return error_mark_node;
2386 /* Entities other than non-static members need no further
2387 processing. */
2388 if (TREE_CODE (member) == TYPE_DECL)
2389 return member;
2390 if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
2391 return convert_from_reference (member);
2393 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
2395 if (complain & tf_error)
2396 error ("invalid pointer to bit-field %qD", member);
2397 return error_mark_node;
2400 /* Set up BASEBINFO for member lookup. */
2401 decl = maybe_dummy_object (type, &basebinfo);
2403 /* A lot of this logic is now handled in lookup_member. */
2404 if (BASELINK_P (member))
2406 /* Go from the TREE_BASELINK to the member function info. */
2407 tree t = BASELINK_FUNCTIONS (member);
2409 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
2411 /* Get rid of a potential OVERLOAD around it. */
2412 t = OVL_FIRST (t);
2414 /* Unique functions are handled easily. */
2416 /* For non-static member of base class, we need a special rule
2417 for access checking [class.protected]:
2419 If the access is to form a pointer to member, the
2420 nested-name-specifier shall name the derived class
2421 (or any class derived from that class). */
2422 bool ok;
2423 if (address_p && DECL_P (t)
2424 && DECL_NONSTATIC_MEMBER_P (t))
2425 ok = perform_or_defer_access_check (TYPE_BINFO (type), t, t,
2426 complain);
2427 else
2428 ok = perform_or_defer_access_check (basebinfo, t, t,
2429 complain);
2430 if (!ok)
2431 return error_mark_node;
2432 if (DECL_STATIC_FUNCTION_P (t))
2433 return member;
2434 member = t;
2436 else
2437 TREE_TYPE (member) = unknown_type_node;
2439 else if (address_p && TREE_CODE (member) == FIELD_DECL)
2441 /* We need additional test besides the one in
2442 check_accessibility_of_qualified_id in case it is
2443 a pointer to non-static member. */
2444 if (!perform_or_defer_access_check (TYPE_BINFO (type), member, member,
2445 complain))
2446 return error_mark_node;
2449 if (!address_p)
2451 /* If MEMBER is non-static, then the program has fallen afoul of
2452 [expr.prim]:
2454 An id-expression that denotes a non-static data member or
2455 non-static member function of a class can only be used:
2457 -- as part of a class member access (_expr.ref_) in which the
2458 object-expression refers to the member's class or a class
2459 derived from that class, or
2461 -- to form a pointer to member (_expr.unary.op_), or
2463 -- in the body of a non-static member function of that class or
2464 of a class derived from that class (_class.mfct.non-static_), or
2466 -- in a mem-initializer for a constructor for that class or for
2467 a class derived from that class (_class.base.init_). */
2468 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
2470 /* Build a representation of the qualified name suitable
2471 for use as the operand to "&" -- even though the "&" is
2472 not actually present. */
2473 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
2474 /* In Microsoft mode, treat a non-static member function as if
2475 it were a pointer-to-member. */
2476 if (flag_ms_extensions)
2478 PTRMEM_OK_P (member) = 1;
2479 return cp_build_addr_expr (member, complain);
2481 if (complain & tf_error)
2482 error ("invalid use of non-static member function %qD",
2483 TREE_OPERAND (member, 1));
2484 return error_mark_node;
2486 else if (TREE_CODE (member) == FIELD_DECL)
2488 if (complain & tf_error)
2489 error ("invalid use of non-static data member %qD", member);
2490 return error_mark_node;
2492 return member;
2495 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
2496 PTRMEM_OK_P (member) = 1;
2497 return member;
2500 /* If DECL is a scalar enumeration constant or variable with a
2501 constant initializer, return the initializer (or, its initializers,
2502 recursively); otherwise, return DECL. If STRICT_P, the
2503 initializer is only returned if DECL is a
2504 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
2505 return an aggregate constant. If UNSHARE_P, return an unshared
2506 copy of the initializer. */
2508 static tree
2509 constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p,
2510 bool unshare_p)
2512 while (TREE_CODE (decl) == CONST_DECL
2513 || decl_constant_var_p (decl)
2514 || (!strict_p && VAR_P (decl)
2515 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl))))
2517 tree init;
2518 /* If DECL is a static data member in a template
2519 specialization, we must instantiate it here. The
2520 initializer for the static data member is not processed
2521 until needed; we need it now. */
2522 mark_used (decl, tf_none);
2523 init = DECL_INITIAL (decl);
2524 if (init == error_mark_node)
2526 if (TREE_CODE (decl) == CONST_DECL
2527 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2528 /* Treat the error as a constant to avoid cascading errors on
2529 excessively recursive template instantiation (c++/9335). */
2530 return init;
2531 else
2532 return decl;
2534 /* Initializers in templates are generally expanded during
2535 instantiation, so before that for const int i(2)
2536 INIT is a TREE_LIST with the actual initializer as
2537 TREE_VALUE. */
2538 if (processing_template_decl
2539 && init
2540 && TREE_CODE (init) == TREE_LIST
2541 && TREE_CHAIN (init) == NULL_TREE)
2542 init = TREE_VALUE (init);
2543 /* Instantiate a non-dependent initializer for user variables. We
2544 mustn't do this for the temporary for an array compound literal;
2545 trying to instatiate the initializer will keep creating new
2546 temporaries until we crash. Probably it's not useful to do it for
2547 other artificial variables, either. */
2548 if (!DECL_ARTIFICIAL (decl))
2549 init = instantiate_non_dependent_or_null (init);
2550 if (!init
2551 || !TREE_TYPE (init)
2552 || !TREE_CONSTANT (init)
2553 || (!return_aggregate_cst_ok_p
2554 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2555 return an aggregate constant (of which string
2556 literals are a special case), as we do not want
2557 to make inadvertent copies of such entities, and
2558 we must be sure that their addresses are the
2559 same everywhere. */
2560 && (TREE_CODE (init) == CONSTRUCTOR
2561 || TREE_CODE (init) == STRING_CST)))
2562 break;
2563 /* Don't return a CONSTRUCTOR for a variable with partial run-time
2564 initialization, since it doesn't represent the entire value.
2565 Similarly for VECTOR_CSTs created by cp_folding those
2566 CONSTRUCTORs. */
2567 if ((TREE_CODE (init) == CONSTRUCTOR
2568 || TREE_CODE (init) == VECTOR_CST)
2569 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2570 break;
2571 /* If the variable has a dynamic initializer, don't use its
2572 DECL_INITIAL which doesn't reflect the real value. */
2573 if (VAR_P (decl)
2574 && TREE_STATIC (decl)
2575 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
2576 && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2577 break;
2578 decl = init;
2580 return unshare_p ? unshare_expr (decl) : decl;
2583 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant
2584 of integral or enumeration type, or a constexpr variable of scalar type,
2585 then return that value. These are those variables permitted in constant
2586 expressions by [5.19/1]. */
2588 tree
2589 scalar_constant_value (tree decl)
2591 return constant_value_1 (decl, /*strict_p=*/true,
2592 /*return_aggregate_cst_ok_p=*/false,
2593 /*unshare_p=*/true);
2596 /* Like scalar_constant_value, but can also return aggregate initializers.
2597 If UNSHARE_P, return an unshared copy of the initializer. */
2599 tree
2600 decl_really_constant_value (tree decl, bool unshare_p /*= true*/)
2602 return constant_value_1 (decl, /*strict_p=*/true,
2603 /*return_aggregate_cst_ok_p=*/true,
2604 /*unshare_p=*/unshare_p);
2607 /* A more relaxed version of decl_really_constant_value, used by the
2608 common C/C++ code. */
2610 tree
2611 decl_constant_value (tree decl, bool unshare_p)
2613 return constant_value_1 (decl, /*strict_p=*/processing_template_decl,
2614 /*return_aggregate_cst_ok_p=*/true,
2615 /*unshare_p=*/unshare_p);
2618 tree
2619 decl_constant_value (tree decl)
2621 return decl_constant_value (decl, /*unshare_p=*/true);
2624 /* Common subroutines of build_new and build_vec_delete. */
2626 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2627 the type of the object being allocated; otherwise, it's just TYPE.
2628 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2629 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2630 a vector of arguments to be provided as arguments to a placement
2631 new operator. This routine performs no semantic checks; it just
2632 creates and returns a NEW_EXPR. */
2634 static tree
2635 build_raw_new_expr (location_t loc, vec<tree, va_gc> *placement, tree type,
2636 tree nelts, vec<tree, va_gc> *init, int use_global_new)
2638 tree init_list;
2639 tree new_expr;
2641 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2642 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2643 permits us to distinguish the case of a missing initializer "new
2644 int" from an empty initializer "new int()". */
2645 if (init == NULL)
2646 init_list = NULL_TREE;
2647 else if (init->is_empty ())
2648 init_list = void_node;
2649 else
2650 init_list = build_tree_list_vec (init);
2652 new_expr = build4_loc (loc, NEW_EXPR, build_pointer_type (type),
2653 build_tree_list_vec (placement), type, nelts,
2654 init_list);
2655 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2656 TREE_SIDE_EFFECTS (new_expr) = 1;
2658 return new_expr;
2661 /* Diagnose uninitialized const members or reference members of type
2662 TYPE. USING_NEW is used to disambiguate the diagnostic between a
2663 new expression without a new-initializer and a declaration. Returns
2664 the error count. */
2666 static int
2667 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2668 bool using_new, bool complain)
2670 tree field;
2671 int error_count = 0;
2673 if (type_has_user_provided_constructor (type))
2674 return 0;
2676 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2678 tree field_type;
2680 if (TREE_CODE (field) != FIELD_DECL)
2681 continue;
2683 field_type = strip_array_types (TREE_TYPE (field));
2685 if (type_has_user_provided_constructor (field_type))
2686 continue;
2688 if (TYPE_REF_P (field_type))
2690 ++ error_count;
2691 if (complain)
2693 if (DECL_CONTEXT (field) == origin)
2695 if (using_new)
2696 error ("uninitialized reference member in %q#T "
2697 "using %<new%> without new-initializer", origin);
2698 else
2699 error ("uninitialized reference member in %q#T", origin);
2701 else
2703 if (using_new)
2704 error ("uninitialized reference member in base %q#T "
2705 "of %q#T using %<new%> without new-initializer",
2706 DECL_CONTEXT (field), origin);
2707 else
2708 error ("uninitialized reference member in base %q#T "
2709 "of %q#T", DECL_CONTEXT (field), origin);
2711 inform (DECL_SOURCE_LOCATION (field),
2712 "%q#D should be initialized", field);
2716 if (CP_TYPE_CONST_P (field_type))
2718 ++ error_count;
2719 if (complain)
2721 if (DECL_CONTEXT (field) == origin)
2723 if (using_new)
2724 error ("uninitialized const member in %q#T "
2725 "using %<new%> without new-initializer", origin);
2726 else
2727 error ("uninitialized const member in %q#T", origin);
2729 else
2731 if (using_new)
2732 error ("uninitialized const member in base %q#T "
2733 "of %q#T using %<new%> without new-initializer",
2734 DECL_CONTEXT (field), origin);
2735 else
2736 error ("uninitialized const member in base %q#T "
2737 "of %q#T", DECL_CONTEXT (field), origin);
2739 inform (DECL_SOURCE_LOCATION (field),
2740 "%q#D should be initialized", field);
2744 if (CLASS_TYPE_P (field_type))
2745 error_count
2746 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2747 using_new, complain);
2749 return error_count;
2753 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2755 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2758 /* Call __cxa_bad_array_new_length to indicate that the size calculation
2759 overflowed. Pretend it returns sizetype so that it plays nicely in the
2760 COND_EXPR. */
2762 tree
2763 throw_bad_array_new_length (void)
2765 if (!fn)
2767 tree name = get_identifier ("__cxa_throw_bad_array_new_length");
2769 fn = get_global_binding (name);
2770 if (!fn)
2771 fn = push_throw_library_fn
2772 (name, build_function_type_list (sizetype, NULL_TREE));
2775 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2778 /* Attempt to verify that the argument, OPER, of a placement new expression
2779 refers to an object sufficiently large for an object of TYPE or an array
2780 of NELTS of such objects when NELTS is non-null, and issue a warning when
2781 it does not. SIZE specifies the size needed to construct the object or
2782 array and captures the result of NELTS * sizeof (TYPE). (SIZE could be
2783 greater when the array under construction requires a cookie to store
2784 NELTS. GCC's placement new expression stores the cookie when invoking
2785 a user-defined placement new operator function but not the default one.
2786 Placement new expressions with user-defined placement new operator are
2787 not diagnosed since we don't know how they use the buffer (this could
2788 be a future extension). */
2789 static void
2790 warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper)
2792 location_t loc = cp_expr_loc_or_input_loc (oper);
2794 STRIP_NOPS (oper);
2796 /* Using a function argument or a (non-array) variable as an argument
2797 to placement new is not checked since it's unknown what it might
2798 point to. */
2799 if (TREE_CODE (oper) == PARM_DECL
2800 || VAR_P (oper)
2801 || TREE_CODE (oper) == COMPONENT_REF)
2802 return;
2804 /* Evaluate any constant expressions. */
2805 size = fold_non_dependent_expr (size);
2807 access_ref ref;
2808 ref.eval = [](tree x){ return fold_non_dependent_expr (x); };
2809 ref.trail1special = warn_placement_new < 2;
2810 tree objsize = compute_objsize (oper, 1, &ref);
2811 if (!objsize)
2812 return;
2814 /* We can only draw conclusions if ref.deref == -1,
2815 i.e. oper is the address of the object. */
2816 if (ref.deref != -1)
2817 return;
2819 offset_int bytes_avail = wi::to_offset (objsize);
2820 offset_int bytes_need;
2822 if (CONSTANT_CLASS_P (size))
2823 bytes_need = wi::to_offset (size);
2824 else if (nelts && CONSTANT_CLASS_P (nelts))
2825 bytes_need = (wi::to_offset (nelts)
2826 * wi::to_offset (TYPE_SIZE_UNIT (type)));
2827 else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2828 bytes_need = wi::to_offset (TYPE_SIZE_UNIT (type));
2829 else
2831 /* The type is a VLA. */
2832 return;
2835 if (bytes_avail >= bytes_need)
2836 return;
2838 /* True when the size to mention in the warning is exact as opposed
2839 to "at least N". */
2840 const bool exact_size = (ref.offrng[0] == ref.offrng[1]
2841 || ref.sizrng[1] - ref.offrng[0] == 0);
2843 tree opertype = ref.ref ? TREE_TYPE (ref.ref) : TREE_TYPE (oper);
2844 bool warned = false;
2845 if (nelts)
2846 nelts = fold_for_warn (nelts);
2847 if (nelts)
2848 if (CONSTANT_CLASS_P (nelts))
2849 warned = warning_at (loc, OPT_Wplacement_new_,
2850 (exact_size
2851 ? G_("placement new constructing an object "
2852 "of type %<%T [%wu]%> and size %qwu "
2853 "in a region of type %qT and size %qwi")
2854 : G_("placement new constructing an object "
2855 "of type %<%T [%wu]%> and size %qwu "
2856 "in a region of type %qT and size "
2857 "at most %qwu")),
2858 type, tree_to_uhwi (nelts),
2859 bytes_need.to_uhwi (),
2860 opertype, bytes_avail.to_uhwi ());
2861 else
2862 warned = warning_at (loc, OPT_Wplacement_new_,
2863 (exact_size
2864 ? G_("placement new constructing an array "
2865 "of objects of type %qT and size %qwu "
2866 "in a region of type %qT and size %qwi")
2867 : G_("placement new constructing an array "
2868 "of objects of type %qT and size %qwu "
2869 "in a region of type %qT and size "
2870 "at most %qwu")),
2871 type, bytes_need.to_uhwi (), opertype,
2872 bytes_avail.to_uhwi ());
2873 else
2874 warned = warning_at (loc, OPT_Wplacement_new_,
2875 (exact_size
2876 ? G_("placement new constructing an object "
2877 "of type %qT and size %qwu in a region "
2878 "of type %qT and size %qwi")
2879 : G_("placement new constructing an object "
2880 "of type %qT "
2881 "and size %qwu in a region of type %qT "
2882 "and size at most %qwu")),
2883 type, bytes_need.to_uhwi (), opertype,
2884 bytes_avail.to_uhwi ());
2886 if (!warned || !ref.ref)
2887 return;
2889 if (ref.offrng[0] == 0 || !ref.offset_bounded ())
2890 /* Avoid mentioning the offset when its lower bound is zero
2891 or when it's impossibly large. */
2892 inform (DECL_SOURCE_LOCATION (ref.ref),
2893 "%qD declared here", ref.ref);
2894 else if (ref.offrng[0] == ref.offrng[1])
2895 inform (DECL_SOURCE_LOCATION (ref.ref),
2896 "at offset %wi from %qD declared here",
2897 ref.offrng[0].to_shwi (), ref.ref);
2898 else
2899 inform (DECL_SOURCE_LOCATION (ref.ref),
2900 "at offset [%wi, %wi] from %qD declared here",
2901 ref.offrng[0].to_shwi (), ref.offrng[1].to_shwi (), ref.ref);
2904 /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */
2906 bool
2907 type_has_new_extended_alignment (tree t)
2909 return (aligned_new_threshold
2910 && TYPE_ALIGN_UNIT (t) > (unsigned)aligned_new_threshold);
2913 /* Return the alignment we expect malloc to guarantee. This should just be
2914 MALLOC_ABI_ALIGNMENT, but that macro defaults to only BITS_PER_WORD for some
2915 reason, so don't let the threshold be smaller than max_align_t_align. */
2917 unsigned
2918 malloc_alignment ()
2920 return MAX (max_align_t_align(), MALLOC_ABI_ALIGNMENT);
2923 /* Determine whether an allocation function is a namespace-scope
2924 non-replaceable placement new function. See DR 1748. */
2925 static bool
2926 std_placement_new_fn_p (tree alloc_fn)
2928 if (DECL_NAMESPACE_SCOPE_P (alloc_fn))
2930 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
2931 if ((TREE_VALUE (first_arg) == ptr_type_node)
2932 && TREE_CHAIN (first_arg) == void_list_node)
2933 return true;
2935 return false;
2938 /* For element type ELT_TYPE, return the appropriate type of the heap object
2939 containing such element(s). COOKIE_SIZE is the size of cookie in bytes.
2940 Return
2941 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
2942 where N is nothing (flexible array member) if ITYPE2 is NULL, otherwise
2943 the array has ITYPE2 as its TYPE_DOMAIN. */
2945 tree
2946 build_new_constexpr_heap_type (tree elt_type, tree cookie_size, tree itype2)
2948 gcc_assert (tree_fits_uhwi_p (cookie_size));
2949 unsigned HOST_WIDE_INT csz = tree_to_uhwi (cookie_size);
2950 csz /= int_size_in_bytes (sizetype);
2951 tree itype1 = build_index_type (size_int (csz - 1));
2952 tree atype1 = build_cplus_array_type (sizetype, itype1);
2953 tree atype2 = build_cplus_array_type (elt_type, itype2);
2954 tree rtype = cxx_make_type (RECORD_TYPE);
2955 TYPE_NAME (rtype) = heap_identifier;
2956 tree fld1 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype1);
2957 tree fld2 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype2);
2958 DECL_FIELD_CONTEXT (fld1) = rtype;
2959 DECL_FIELD_CONTEXT (fld2) = rtype;
2960 DECL_ARTIFICIAL (fld1) = true;
2961 DECL_ARTIFICIAL (fld2) = true;
2962 TYPE_FIELDS (rtype) = fld1;
2963 DECL_CHAIN (fld1) = fld2;
2964 layout_type (rtype);
2965 return rtype;
2968 /* Help the constexpr code to find the right type for the heap variable
2969 by adding a NOP_EXPR around ALLOC_CALL if needed for cookie_size.
2970 Return ALLOC_CALL or ALLOC_CALL cast to a pointer to
2971 struct { size_t[cookie_size/sizeof(size_t)]; elt_type[]; }. */
2973 static tree
2974 maybe_wrap_new_for_constexpr (tree alloc_call, tree elt_type, tree cookie_size)
2976 if (cxx_dialect < cxx20)
2977 return alloc_call;
2979 if (current_function_decl != NULL_TREE
2980 && !DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2981 return alloc_call;
2983 tree call_expr = extract_call_expr (alloc_call);
2984 if (call_expr == error_mark_node)
2985 return alloc_call;
2987 tree alloc_call_fndecl = cp_get_callee_fndecl_nofold (call_expr);
2988 if (alloc_call_fndecl == NULL_TREE
2989 || !IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_call_fndecl))
2990 || CP_DECL_CONTEXT (alloc_call_fndecl) != global_namespace)
2991 return alloc_call;
2993 tree rtype = build_new_constexpr_heap_type (elt_type, cookie_size,
2994 NULL_TREE);
2995 return build_nop (build_pointer_type (rtype), alloc_call);
2998 /* Generate code for a new-expression, including calling the "operator
2999 new" function, initializing the object, and, if an exception occurs
3000 during construction, cleaning up. The arguments are as for
3001 build_raw_new_expr. This may change PLACEMENT and INIT.
3002 TYPE is the type of the object being constructed, possibly an array
3003 of NELTS elements when NELTS is non-null (in "new T[NELTS]", T may
3004 be an array of the form U[inner], with the whole expression being
3005 "new U[NELTS][inner]"). */
3007 static tree
3008 build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
3009 vec<tree, va_gc> **init, bool globally_qualified_p,
3010 tsubst_flags_t complain)
3012 tree size, rval;
3013 /* True iff this is a call to "operator new[]" instead of just
3014 "operator new". */
3015 bool array_p = false;
3016 /* If ARRAY_P is true, the element type of the array. This is never
3017 an ARRAY_TYPE; for something like "new int[3][4]", the
3018 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
3019 TYPE. */
3020 tree elt_type;
3021 /* The type of the new-expression. (This type is always a pointer
3022 type.) */
3023 tree pointer_type;
3024 tree non_const_pointer_type;
3025 /* The most significant array bound in int[OUTER_NELTS][inner]. */
3026 tree outer_nelts = NULL_TREE;
3027 /* For arrays with a non-constant number of elements, a bounds checks
3028 on the NELTS parameter to avoid integer overflow at runtime. */
3029 tree outer_nelts_check = NULL_TREE;
3030 bool outer_nelts_from_type = false;
3031 /* Number of the "inner" elements in "new T[OUTER_NELTS][inner]". */
3032 offset_int inner_nelts_count = 1;
3033 tree alloc_call, alloc_expr;
3034 /* Size of the inner array elements (those with constant dimensions). */
3035 offset_int inner_size;
3036 /* The address returned by the call to "operator new". This node is
3037 a VAR_DECL and is therefore reusable. */
3038 tree alloc_node;
3039 tree alloc_fn;
3040 tree cookie_expr, init_expr;
3041 int nothrow, check_new;
3042 /* If non-NULL, the number of extra bytes to allocate at the
3043 beginning of the storage allocated for an array-new expression in
3044 order to store the number of elements. */
3045 tree cookie_size = NULL_TREE;
3046 tree placement_first;
3047 tree placement_expr = NULL_TREE;
3048 /* True if the function we are calling is a placement allocation
3049 function. */
3050 bool placement_allocation_fn_p;
3051 /* True if the storage must be initialized, either by a constructor
3052 or due to an explicit new-initializer. */
3053 bool is_initialized;
3054 /* The address of the thing allocated, not including any cookie. In
3055 particular, if an array cookie is in use, DATA_ADDR is the
3056 address of the first array element. This node is a VAR_DECL, and
3057 is therefore reusable. */
3058 tree data_addr;
3059 tree orig_type = type;
3061 if (nelts)
3063 outer_nelts = nelts;
3064 array_p = true;
3066 else if (TREE_CODE (type) == ARRAY_TYPE)
3068 /* Transforms new (T[N]) to new T[N]. The former is a GNU
3069 extension for variable N. (This also covers new T where T is
3070 a VLA typedef.) */
3071 array_p = true;
3072 nelts = array_type_nelts_top (type);
3073 outer_nelts = nelts;
3074 type = TREE_TYPE (type);
3075 outer_nelts_from_type = true;
3078 /* Lots of logic below depends on whether we have a constant number of
3079 elements, so go ahead and fold it now. */
3080 const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain);
3082 /* If our base type is an array, then make sure we know how many elements
3083 it has. */
3084 for (elt_type = type;
3085 TREE_CODE (elt_type) == ARRAY_TYPE;
3086 elt_type = TREE_TYPE (elt_type))
3088 tree inner_nelts = array_type_nelts_top (elt_type);
3089 tree inner_nelts_cst = maybe_constant_value (inner_nelts);
3090 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
3092 wi::overflow_type overflow;
3093 offset_int result = wi::mul (wi::to_offset (inner_nelts_cst),
3094 inner_nelts_count, SIGNED, &overflow);
3095 if (overflow)
3097 if (complain & tf_error)
3098 error ("integer overflow in array size");
3099 nelts = error_mark_node;
3101 inner_nelts_count = result;
3103 else
3105 if (complain & tf_error)
3107 error_at (cp_expr_loc_or_input_loc (inner_nelts),
3108 "array size in new-expression must be constant");
3109 cxx_constant_value(inner_nelts);
3111 nelts = error_mark_node;
3113 if (nelts != error_mark_node)
3114 nelts = cp_build_binary_op (input_location,
3115 MULT_EXPR, nelts,
3116 inner_nelts_cst,
3117 complain);
3120 if (!verify_type_context (input_location, TCTX_ALLOCATION, elt_type,
3121 !(complain & tf_error)))
3122 return error_mark_node;
3124 if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
3126 error ("variably modified type not allowed in new-expression");
3127 return error_mark_node;
3130 if (nelts == error_mark_node)
3131 return error_mark_node;
3133 /* Warn if we performed the (T[N]) to T[N] transformation and N is
3134 variable. */
3135 if (outer_nelts_from_type
3136 && !TREE_CONSTANT (cst_outer_nelts))
3138 if (complain & tf_warning_or_error)
3140 pedwarn (cp_expr_loc_or_input_loc (outer_nelts), OPT_Wvla,
3141 typedef_variant_p (orig_type)
3142 ? G_("non-constant array new length must be specified "
3143 "directly, not by %<typedef%>")
3144 : G_("non-constant array new length must be specified "
3145 "without parentheses around the type-id"));
3147 else
3148 return error_mark_node;
3151 if (VOID_TYPE_P (elt_type))
3153 if (complain & tf_error)
3154 error ("invalid type %<void%> for %<new%>");
3155 return error_mark_node;
3158 if (is_std_init_list (elt_type) && !cp_unevaluated_operand)
3159 warning (OPT_Winit_list_lifetime,
3160 "%<new%> of %<initializer_list%> does not "
3161 "extend the lifetime of the underlying array");
3163 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain))
3164 return error_mark_node;
3166 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
3168 if (*init == NULL && cxx_dialect < cxx11)
3170 bool maybe_uninitialized_error = false;
3171 /* A program that calls for default-initialization [...] of an
3172 entity of reference type is ill-formed. */
3173 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
3174 maybe_uninitialized_error = true;
3176 /* A new-expression that creates an object of type T initializes
3177 that object as follows:
3178 - If the new-initializer is omitted:
3179 -- If T is a (possibly cv-qualified) non-POD class type
3180 (or array thereof), the object is default-initialized (8.5).
3181 [...]
3182 -- Otherwise, the object created has indeterminate
3183 value. If T is a const-qualified type, or a (possibly
3184 cv-qualified) POD class type (or array thereof)
3185 containing (directly or indirectly) a member of
3186 const-qualified type, the program is ill-formed; */
3188 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
3189 maybe_uninitialized_error = true;
3191 if (maybe_uninitialized_error
3192 && diagnose_uninitialized_cst_or_ref_member (elt_type,
3193 /*using_new=*/true,
3194 complain & tf_error))
3195 return error_mark_node;
3198 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
3199 && default_init_uninitialized_part (elt_type))
3201 if (complain & tf_error)
3202 error ("uninitialized const in %<new%> of %q#T", elt_type);
3203 return error_mark_node;
3206 size = size_in_bytes (elt_type);
3207 if (array_p)
3209 /* Maximum available size in bytes. Half of the address space
3210 minus the cookie size. */
3211 offset_int max_size
3212 = wi::set_bit_in_zero <offset_int> (TYPE_PRECISION (sizetype) - 1);
3213 /* Maximum number of outer elements which can be allocated. */
3214 offset_int max_outer_nelts;
3215 tree max_outer_nelts_tree;
3217 gcc_assert (TREE_CODE (size) == INTEGER_CST);
3218 cookie_size = targetm.cxx.get_cookie_size (elt_type);
3219 gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
3220 gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size));
3221 /* Unconditionally subtract the cookie size. This decreases the
3222 maximum object size and is safe even if we choose not to use
3223 a cookie after all. */
3224 max_size -= wi::to_offset (cookie_size);
3225 wi::overflow_type overflow;
3226 inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED,
3227 &overflow);
3228 if (overflow || wi::gtu_p (inner_size, max_size))
3230 if (complain & tf_error)
3232 cst_size_error error;
3233 if (overflow)
3234 error = cst_size_overflow;
3235 else
3237 error = cst_size_too_big;
3238 size = size_binop (MULT_EXPR, size,
3239 wide_int_to_tree (sizetype,
3240 inner_nelts_count));
3241 size = cp_fully_fold (size);
3243 invalid_array_size_error (input_location, error, size,
3244 /*name=*/NULL_TREE);
3246 return error_mark_node;
3249 max_outer_nelts = wi::udiv_trunc (max_size, inner_size);
3250 max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts);
3252 size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts));
3254 if (TREE_CODE (cst_outer_nelts) == INTEGER_CST)
3256 if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts))
3258 /* When the array size is constant, check it at compile time
3259 to make sure it doesn't exceed the implementation-defined
3260 maximum, as required by C++ 14 (in C++ 11 this requirement
3261 isn't explicitly stated but it's enforced anyway -- see
3262 grokdeclarator in cp/decl.cc). */
3263 if (complain & tf_error)
3265 size = cp_fully_fold (size);
3266 invalid_array_size_error (input_location, cst_size_too_big,
3267 size, NULL_TREE);
3269 return error_mark_node;
3272 else
3274 /* When a runtime check is necessary because the array size
3275 isn't constant, keep only the top-most seven bits (starting
3276 with the most significant non-zero bit) of the maximum size
3277 to compare the array size against, to simplify encoding the
3278 constant maximum size in the instruction stream. */
3280 unsigned shift = (max_outer_nelts.get_precision ()) - 7
3281 - wi::clz (max_outer_nelts);
3282 max_outer_nelts = (max_outer_nelts >> shift) << shift;
3284 outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
3285 outer_nelts,
3286 max_outer_nelts_tree);
3290 tree align_arg = NULL_TREE;
3291 if (type_has_new_extended_alignment (elt_type))
3293 unsigned align = TYPE_ALIGN_UNIT (elt_type);
3294 /* Also consider the alignment of the cookie, if any. */
3295 if (TYPE_VEC_NEW_USES_COOKIE (elt_type))
3296 align = MAX (align, TYPE_ALIGN_UNIT (size_type_node));
3297 align_arg = build_int_cst (align_type_node, align);
3300 alloc_fn = NULL_TREE;
3302 /* If PLACEMENT is a single simple pointer type not passed by
3303 reference, prepare to capture it in a temporary variable. Do
3304 this now, since PLACEMENT will change in the calls below. */
3305 placement_first = NULL_TREE;
3306 if (vec_safe_length (*placement) == 1
3307 && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
3308 placement_first = (**placement)[0];
3310 bool member_new_p = false;
3312 /* Allocate the object. */
3313 tree fnname;
3314 tree fns;
3316 fnname = ovl_op_identifier (false, array_p ? VEC_NEW_EXPR : NEW_EXPR);
3318 member_new_p = !globally_qualified_p
3319 && CLASS_TYPE_P (elt_type)
3320 && (array_p
3321 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
3322 : TYPE_HAS_NEW_OPERATOR (elt_type));
3324 bool member_delete_p = (!globally_qualified_p
3325 && CLASS_TYPE_P (elt_type)
3326 && (array_p
3327 ? TYPE_GETS_VEC_DELETE (elt_type)
3328 : TYPE_GETS_REG_DELETE (elt_type)));
3330 if (member_new_p)
3332 /* Use a class-specific operator new. */
3333 /* If a cookie is required, add some extra space. */
3334 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
3335 size = size_binop (PLUS_EXPR, size, cookie_size);
3336 else
3338 cookie_size = NULL_TREE;
3339 /* No size arithmetic necessary, so the size check is
3340 not needed. */
3341 if (outer_nelts_check != NULL && inner_size == 1)
3342 outer_nelts_check = NULL_TREE;
3344 /* Perform the overflow check. */
3345 tree errval = TYPE_MAX_VALUE (sizetype);
3346 if (cxx_dialect >= cxx11 && flag_exceptions)
3347 errval = throw_bad_array_new_length ();
3348 if (outer_nelts_check != NULL_TREE)
3349 size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
3350 size, errval);
3351 /* Create the argument list. */
3352 vec_safe_insert (*placement, 0, size);
3353 /* Do name-lookup to find the appropriate operator. */
3354 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2, complain);
3355 if (fns == NULL_TREE)
3357 if (complain & tf_error)
3358 error ("no suitable %qD found in class %qT", fnname, elt_type);
3359 return error_mark_node;
3361 if (TREE_CODE (fns) == TREE_LIST)
3363 if (complain & tf_error)
3365 error ("request for member %qD is ambiguous", fnname);
3366 print_candidates (fns);
3368 return error_mark_node;
3370 tree dummy = build_dummy_object (elt_type);
3371 alloc_call = NULL_TREE;
3372 if (align_arg)
3374 vec<tree, va_gc> *align_args
3375 = vec_copy_and_insert (*placement, align_arg, 1);
3376 alloc_call
3377 = build_new_method_call (dummy, fns, &align_args,
3378 /*conversion_path=*/NULL_TREE,
3379 LOOKUP_NORMAL, &alloc_fn, tf_none);
3380 /* If no matching function is found and the allocated object type
3381 has new-extended alignment, the alignment argument is removed
3382 from the argument list, and overload resolution is performed
3383 again. */
3384 if (alloc_call == error_mark_node)
3385 alloc_call = NULL_TREE;
3387 if (!alloc_call)
3388 alloc_call = build_new_method_call (dummy, fns, placement,
3389 /*conversion_path=*/NULL_TREE,
3390 LOOKUP_NORMAL,
3391 &alloc_fn, complain);
3393 else
3395 /* Use a global operator new. */
3396 /* See if a cookie might be required. */
3397 if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
3399 cookie_size = NULL_TREE;
3400 /* No size arithmetic necessary, so the size check is
3401 not needed. */
3402 if (outer_nelts_check != NULL && inner_size == 1)
3403 outer_nelts_check = NULL_TREE;
3406 /* If size is zero e.g. due to type having zero size, try to
3407 preserve outer_nelts for constant expression evaluation
3408 purposes. */
3409 if (integer_zerop (size) && outer_nelts)
3410 size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts);
3412 alloc_call = build_operator_new_call (fnname, placement,
3413 &size, &cookie_size,
3414 align_arg, outer_nelts_check,
3415 &alloc_fn, complain);
3418 if (alloc_call == error_mark_node)
3419 return error_mark_node;
3421 gcc_assert (alloc_fn != NULL_TREE);
3423 /* Now, check to see if this function is actually a placement
3424 allocation function. This can happen even when PLACEMENT is NULL
3425 because we might have something like:
3427 struct S { void* operator new (size_t, int i = 0); };
3429 A call to `new S' will get this allocation function, even though
3430 there is no explicit placement argument. If there is more than
3431 one argument, or there are variable arguments, then this is a
3432 placement allocation function. */
3433 placement_allocation_fn_p
3434 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
3435 || varargs_function_p (alloc_fn));
3437 if (complain & tf_warning_or_error
3438 && warn_aligned_new
3439 && !placement_allocation_fn_p
3440 && TYPE_ALIGN (elt_type) > malloc_alignment ()
3441 && (warn_aligned_new > 1
3442 || CP_DECL_CONTEXT (alloc_fn) == global_namespace)
3443 && !aligned_allocation_fn_p (alloc_fn))
3445 auto_diagnostic_group d;
3446 if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended "
3447 "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type)))
3449 inform (input_location, "uses %qD, which does not have an alignment "
3450 "parameter", alloc_fn);
3451 if (!aligned_new_threshold)
3452 inform (input_location, "use %<-faligned-new%> to enable C++17 "
3453 "over-aligned new support");
3457 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
3458 into a temporary variable. */
3459 if (!processing_template_decl
3460 && TREE_CODE (alloc_call) == CALL_EXPR
3461 && call_expr_nargs (alloc_call) == 2
3462 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
3463 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
3465 tree placement = CALL_EXPR_ARG (alloc_call, 1);
3467 if (placement_first != NULL_TREE
3468 && (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))
3469 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))))
3471 placement_expr = get_target_expr (placement_first);
3472 CALL_EXPR_ARG (alloc_call, 1)
3473 = fold_convert (TREE_TYPE (placement), placement_expr);
3476 if (!member_new_p
3477 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1)))))
3479 /* Attempt to make the warning point at the operator new argument. */
3480 if (placement_first)
3481 placement = placement_first;
3483 warn_placement_new_too_small (orig_type, nelts, size, placement);
3487 alloc_expr = alloc_call;
3488 if (cookie_size)
3489 alloc_expr = maybe_wrap_new_for_constexpr (alloc_expr, type,
3490 cookie_size);
3492 /* In the simple case, we can stop now. */
3493 pointer_type = build_pointer_type (type);
3494 if (!cookie_size && !is_initialized && !member_delete_p)
3495 return build_nop (pointer_type, alloc_expr);
3497 /* Store the result of the allocation call in a variable so that we can
3498 use it more than once. */
3499 alloc_expr = get_target_expr (alloc_expr);
3500 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
3502 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
3503 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
3504 alloc_call = TREE_OPERAND (alloc_call, 1);
3506 /* Preevaluate the placement args so that we don't reevaluate them for a
3507 placement delete. */
3508 if (placement_allocation_fn_p)
3510 tree inits;
3511 stabilize_call (alloc_call, &inits);
3512 if (inits)
3513 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
3514 alloc_expr);
3517 /* unless an allocation function is declared with an empty excep-
3518 tion-specification (_except.spec_), throw(), it indicates failure to
3519 allocate storage by throwing a bad_alloc exception (clause _except_,
3520 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
3521 cation function is declared with an empty exception-specification,
3522 throw(), it returns null to indicate failure to allocate storage and a
3523 non-null pointer otherwise.
3525 So check for a null exception spec on the op new we just called. */
3527 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
3528 check_new
3529 = flag_check_new || (nothrow && !std_placement_new_fn_p (alloc_fn));
3531 if (cookie_size)
3533 tree cookie;
3534 tree cookie_ptr;
3535 tree size_ptr_type;
3537 /* Adjust so we're pointing to the start of the object. */
3538 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
3540 /* Store the number of bytes allocated so that we can know how
3541 many elements to destroy later. We use the last sizeof
3542 (size_t) bytes to store the number of elements. */
3543 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
3544 cookie_ptr = fold_build_pointer_plus_loc (input_location,
3545 alloc_node, cookie_ptr);
3546 size_ptr_type = build_pointer_type (sizetype);
3547 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
3548 cookie = cp_build_fold_indirect_ref (cookie_ptr);
3550 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
3552 if (targetm.cxx.cookie_has_size ())
3554 /* Also store the element size. */
3555 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
3556 fold_build1_loc (input_location,
3557 NEGATE_EXPR, sizetype,
3558 size_in_bytes (sizetype)));
3560 cookie = cp_build_fold_indirect_ref (cookie_ptr);
3561 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
3562 size_in_bytes (elt_type));
3563 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
3564 cookie, cookie_expr);
3567 else
3569 cookie_expr = NULL_TREE;
3570 data_addr = alloc_node;
3573 /* Now use a pointer to the type we've actually allocated. */
3575 /* But we want to operate on a non-const version to start with,
3576 since we'll be modifying the elements. */
3577 non_const_pointer_type = build_pointer_type
3578 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
3580 data_addr = fold_convert (non_const_pointer_type, data_addr);
3581 /* Any further uses of alloc_node will want this type, too. */
3582 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
3584 /* Now initialize the allocated object. Note that we preevaluate the
3585 initialization expression, apart from the actual constructor call or
3586 assignment--we do this because we want to delay the allocation as long
3587 as possible in order to minimize the size of the exception region for
3588 placement delete. */
3589 if (is_initialized)
3591 bool explicit_value_init_p = false;
3593 if (*init != NULL && (*init)->is_empty ())
3595 *init = NULL;
3596 explicit_value_init_p = true;
3599 if (processing_template_decl)
3601 /* Avoid an ICE when converting to a base in build_simple_base_path.
3602 We'll throw this all away anyway, and build_new will create
3603 a NEW_EXPR. */
3604 tree t = fold_convert (build_pointer_type (elt_type), data_addr);
3605 /* build_value_init doesn't work in templates, and we don't need
3606 the initializer anyway since we're going to throw it away and
3607 rebuild it at instantiation time, so just build up a single
3608 constructor call to get any appropriate diagnostics. */
3609 init_expr = cp_build_fold_indirect_ref (t);
3610 if (type_build_ctor_call (elt_type))
3611 init_expr = build_special_member_call (init_expr,
3612 complete_ctor_identifier,
3613 init, elt_type,
3614 LOOKUP_NORMAL,
3615 complain);
3617 else if (array_p)
3619 tree vecinit = NULL_TREE;
3620 const size_t len = vec_safe_length (*init);
3621 if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0]))
3623 vecinit = (**init)[0];
3624 if (CONSTRUCTOR_NELTS (vecinit) == 0)
3625 /* List-value-initialization, leave it alone. */;
3626 else
3628 tree arraytype, domain;
3629 if (TREE_CONSTANT (nelts))
3630 domain = compute_array_index_type (NULL_TREE, nelts,
3631 complain);
3632 else
3633 /* We'll check the length at runtime. */
3634 domain = NULL_TREE;
3635 arraytype = build_cplus_array_type (type, domain);
3636 /* If we have new char[4]{"foo"}, we have to reshape
3637 so that the STRING_CST isn't wrapped in { }. */
3638 vecinit = reshape_init (arraytype, vecinit, complain);
3639 /* The middle end doesn't cope with the location wrapper
3640 around a STRING_CST. */
3641 STRIP_ANY_LOCATION_WRAPPER (vecinit);
3642 vecinit = digest_init (arraytype, vecinit, complain);
3645 else if (*init)
3647 if (complain & tf_error)
3648 error ("parenthesized initializer in array new");
3649 return error_mark_node;
3651 init_expr
3652 = build_vec_init (data_addr,
3653 cp_build_binary_op (input_location,
3654 MINUS_EXPR, outer_nelts,
3655 integer_one_node,
3656 complain),
3657 vecinit,
3658 explicit_value_init_p,
3659 /*from_array=*/0,
3660 complain);
3662 else
3664 init_expr = cp_build_fold_indirect_ref (data_addr);
3666 if (type_build_ctor_call (type) && !explicit_value_init_p)
3668 init_expr = build_special_member_call (init_expr,
3669 complete_ctor_identifier,
3670 init, elt_type,
3671 LOOKUP_NORMAL,
3672 complain|tf_no_cleanup);
3674 else if (explicit_value_init_p)
3676 /* Something like `new int()'. NO_CLEANUP is needed so
3677 we don't try and build a (possibly ill-formed)
3678 destructor. */
3679 tree val = build_value_init (type, complain | tf_no_cleanup);
3680 if (val == error_mark_node)
3681 return error_mark_node;
3682 init_expr = build2 (INIT_EXPR, type, init_expr, val);
3684 else
3686 tree ie;
3688 /* We are processing something like `new int (10)', which
3689 means allocate an int, and initialize it with 10.
3691 In C++20, also handle `new A(1, 2)'. */
3692 if (cxx_dialect >= cxx20
3693 && AGGREGATE_TYPE_P (type)
3694 && (*init)->length () > 1)
3696 ie = build_constructor_from_vec (init_list_type_node, *init);
3697 CONSTRUCTOR_IS_DIRECT_INIT (ie) = true;
3698 CONSTRUCTOR_IS_PAREN_INIT (ie) = true;
3699 ie = digest_init (type, ie, complain);
3701 else
3702 ie = build_x_compound_expr_from_vec (*init, "new initializer",
3703 complain);
3704 init_expr = cp_build_modify_expr (input_location, init_expr,
3705 INIT_EXPR, ie, complain);
3707 /* If the initializer uses C++14 aggregate NSDMI that refer to the
3708 object being initialized, replace them now and don't try to
3709 preevaluate. */
3710 bool had_placeholder = false;
3711 if (!processing_template_decl
3712 && TREE_CODE (init_expr) == INIT_EXPR)
3713 TREE_OPERAND (init_expr, 1)
3714 = replace_placeholders (TREE_OPERAND (init_expr, 1),
3715 TREE_OPERAND (init_expr, 0),
3716 &had_placeholder);
3719 if (init_expr == error_mark_node)
3720 return error_mark_node;
3722 else
3723 init_expr = NULL_TREE;
3725 /* If any part of the object initialization terminates by throwing an
3726 exception and a suitable deallocation function can be found, the
3727 deallocation function is called to free the memory in which the
3728 object was being constructed, after which the exception continues
3729 to propagate in the context of the new-expression. If no
3730 unambiguous matching deallocation function can be found,
3731 propagating the exception does not cause the object's memory to be
3732 freed. */
3733 if (flag_exceptions && (init_expr || member_delete_p))
3735 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
3736 tree cleanup;
3738 /* The Standard is unclear here, but the right thing to do
3739 is to use the same method for finding deallocation
3740 functions that we use for finding allocation functions. */
3741 cleanup = (build_op_delete_call
3742 (dcode,
3743 alloc_node,
3744 size,
3745 globally_qualified_p,
3746 placement_allocation_fn_p ? alloc_call : NULL_TREE,
3747 alloc_fn,
3748 complain));
3750 if (cleanup && init_expr && !processing_template_decl)
3751 /* Ack! First we allocate the memory. Then we set our sentry
3752 variable to true, and expand a cleanup that deletes the
3753 memory if sentry is true. Then we run the constructor, and
3754 finally clear the sentry.
3756 We need to do this because we allocate the space first, so
3757 if there are any temporaries with cleanups in the
3758 constructor args, we need this EH region to extend until
3759 end of full-expression to preserve nesting.
3761 We used to try to evaluate the args first to avoid this, but
3762 since C++17 [expr.new] says that "The invocation of the
3763 allocation function is sequenced before the evaluations of
3764 expressions in the new-initializer." */
3766 tree end, sentry, begin;
3768 begin = get_target_expr (boolean_true_node);
3769 CLEANUP_EH_ONLY (begin) = 1;
3771 sentry = TARGET_EXPR_SLOT (begin);
3773 /* CLEANUP is compiler-generated, so no diagnostics. */
3774 suppress_warning (cleanup);
3776 TARGET_EXPR_CLEANUP (begin)
3777 = build3 (COND_EXPR, void_type_node, sentry,
3778 cleanup, void_node);
3780 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
3781 sentry, boolean_false_node);
3783 init_expr
3784 = build2 (COMPOUND_EXPR, void_type_node, begin,
3785 build2 (COMPOUND_EXPR, void_type_node, init_expr,
3786 end));
3787 /* Likewise, this is compiler-generated. */
3788 suppress_warning (init_expr);
3792 /* Now build up the return value in reverse order. */
3794 rval = data_addr;
3796 if (init_expr)
3797 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
3798 if (cookie_expr)
3799 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
3801 if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
3802 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
3803 and return the call (which doesn't need to be adjusted). */
3804 rval = TARGET_EXPR_INITIAL (alloc_expr);
3805 else
3807 if (check_new)
3809 tree ifexp = cp_build_binary_op (input_location,
3810 NE_EXPR, alloc_node,
3811 nullptr_node,
3812 complain);
3813 rval = build_conditional_expr (input_location, ifexp, rval,
3814 alloc_node, complain);
3817 /* Perform the allocation before anything else, so that ALLOC_NODE
3818 has been initialized before we start using it. */
3819 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
3822 /* A new-expression is never an lvalue. */
3823 gcc_assert (!obvalue_p (rval));
3825 return convert (pointer_type, rval);
3828 /* Generate a representation for a C++ "new" expression. *PLACEMENT
3829 is a vector of placement-new arguments (or NULL if none). If NELTS
3830 is NULL, TYPE is the type of the storage to be allocated. If NELTS
3831 is not NULL, then this is an array-new allocation; TYPE is the type
3832 of the elements in the array and NELTS is the number of elements in
3833 the array. *INIT, if non-NULL, is the initializer for the new
3834 object, or an empty vector to indicate an initializer of "()". If
3835 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
3836 rather than just "new". This may change PLACEMENT and INIT. */
3838 tree
3839 build_new (location_t loc, vec<tree, va_gc> **placement, tree type,
3840 tree nelts, vec<tree, va_gc> **init, int use_global_new,
3841 tsubst_flags_t complain)
3843 tree rval;
3844 vec<tree, va_gc> *orig_placement = NULL;
3845 tree orig_nelts = NULL_TREE;
3846 vec<tree, va_gc> *orig_init = NULL;
3848 if (type == error_mark_node)
3849 return error_mark_node;
3851 if (nelts == NULL_TREE
3852 /* Don't do auto deduction where it might affect mangling. */
3853 && (!processing_template_decl || at_function_scope_p ()))
3855 tree auto_node = type_uses_auto (type);
3856 if (auto_node)
3858 tree d_init = NULL_TREE;
3859 const size_t len = vec_safe_length (*init);
3860 /* E.g. new auto(x) must have exactly one element, or
3861 a {} initializer will have one element. */
3862 if (len == 1)
3864 d_init = (**init)[0];
3865 d_init = resolve_nondeduced_context (d_init, complain);
3867 /* For the rest, e.g. new A(1, 2, 3), create a list. */
3868 else if (len > 1)
3870 unsigned int n;
3871 tree t;
3872 tree *pp = &d_init;
3873 FOR_EACH_VEC_ELT (**init, n, t)
3875 t = resolve_nondeduced_context (t, complain);
3876 *pp = build_tree_list (NULL_TREE, t);
3877 pp = &TREE_CHAIN (*pp);
3880 type = do_auto_deduction (type, d_init, auto_node, complain);
3884 if (processing_template_decl)
3886 if (dependent_type_p (type)
3887 || any_type_dependent_arguments_p (*placement)
3888 || (nelts && type_dependent_expression_p (nelts))
3889 || (nelts && *init)
3890 || any_type_dependent_arguments_p (*init))
3891 return build_raw_new_expr (loc, *placement, type, nelts, *init,
3892 use_global_new);
3894 orig_placement = make_tree_vector_copy (*placement);
3895 orig_nelts = nelts;
3896 if (*init)
3898 orig_init = make_tree_vector_copy (*init);
3899 /* Also copy any CONSTRUCTORs in *init, since reshape_init and
3900 digest_init clobber them in place. */
3901 for (unsigned i = 0; i < orig_init->length(); ++i)
3903 tree e = (**init)[i];
3904 if (TREE_CODE (e) == CONSTRUCTOR)
3905 (**init)[i] = copy_node (e);
3909 make_args_non_dependent (*placement);
3910 if (nelts)
3911 nelts = build_non_dependent_expr (nelts);
3912 make_args_non_dependent (*init);
3915 if (nelts)
3917 location_t nelts_loc = cp_expr_loc_or_loc (nelts, loc);
3918 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
3920 if (complain & tf_error)
3921 permerror (nelts_loc,
3922 "size in array new must have integral type");
3923 else
3924 return error_mark_node;
3927 /* Try to determine the constant value only for the purposes
3928 of the diagnostic below but continue to use the original
3929 value and handle const folding later. */
3930 const_tree cst_nelts = fold_non_dependent_expr (nelts, complain);
3932 /* The expression in a noptr-new-declarator is erroneous if it's of
3933 non-class type and its value before converting to std::size_t is
3934 less than zero. ... If the expression is a constant expression,
3935 the program is ill-fomed. */
3936 if (TREE_CODE (cst_nelts) == INTEGER_CST
3937 && !valid_array_size_p (nelts_loc, cst_nelts, NULL_TREE,
3938 complain & tf_error))
3939 return error_mark_node;
3941 nelts = mark_rvalue_use (nelts);
3942 nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
3945 /* ``A reference cannot be created by the new operator. A reference
3946 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3947 returned by new.'' ARM 5.3.3 */
3948 if (TYPE_REF_P (type))
3950 if (complain & tf_error)
3951 error_at (loc, "new cannot be applied to a reference type");
3952 else
3953 return error_mark_node;
3954 type = TREE_TYPE (type);
3957 if (TREE_CODE (type) == FUNCTION_TYPE)
3959 if (complain & tf_error)
3960 error_at (loc, "new cannot be applied to a function type");
3961 return error_mark_node;
3964 /* P1009: Array size deduction in new-expressions. */
3965 const bool array_p = TREE_CODE (type) == ARRAY_TYPE;
3966 if (*init
3967 /* If ARRAY_P, we have to deduce the array bound. For C++20 paren-init,
3968 we have to process the parenthesized-list. But don't do it for (),
3969 which is value-initialization, and INIT should stay empty. */
3970 && (array_p || (cxx_dialect >= cxx20 && nelts && !(*init)->is_empty ())))
3972 /* This means we have 'new T[]()'. */
3973 if ((*init)->is_empty ())
3975 tree ctor = build_constructor (init_list_type_node, NULL);
3976 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
3977 vec_safe_push (*init, ctor);
3979 tree &elt = (**init)[0];
3980 /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */
3981 if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20)
3983 tree ctor = build_constructor_from_vec (init_list_type_node, *init);
3984 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
3985 CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
3986 elt = ctor;
3987 /* We've squashed all the vector elements into the first one;
3988 truncate the rest. */
3989 (*init)->truncate (1);
3991 /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */
3992 if (array_p && !TYPE_DOMAIN (type))
3994 /* We need to reshape before deducing the bounds to handle code like
3996 struct S { int x, y; };
3997 new S[]{1, 2, 3, 4};
3999 which should deduce S[2]. But don't change ELT itself: we want to
4000 pass a list-initializer to build_new_1, even for STRING_CSTs. */
4001 tree e = elt;
4002 if (BRACE_ENCLOSED_INITIALIZER_P (e))
4003 e = reshape_init (type, e, complain);
4004 cp_complete_array_type (&type, e, /*do_default*/false);
4008 /* The type allocated must be complete. If the new-type-id was
4009 "T[N]" then we are just checking that "T" is complete here, but
4010 that is equivalent, since the value of "N" doesn't matter. */
4011 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
4012 return error_mark_node;
4014 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
4015 if (rval == error_mark_node)
4016 return error_mark_node;
4018 if (processing_template_decl)
4020 tree ret = build_raw_new_expr (loc, orig_placement, type, orig_nelts,
4021 orig_init, use_global_new);
4022 release_tree_vector (orig_placement);
4023 release_tree_vector (orig_init);
4024 return ret;
4027 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
4028 rval = build1_loc (loc, NOP_EXPR, TREE_TYPE (rval), rval);
4029 suppress_warning (rval, OPT_Wunused_value);
4031 return rval;
4034 static tree
4035 build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
4036 special_function_kind auto_delete_vec,
4037 int use_global_delete, tsubst_flags_t complain,
4038 bool in_cleanup = false)
4040 tree virtual_size;
4041 tree ptype = build_pointer_type (type = complete_type (type));
4042 tree size_exp;
4044 /* Temporary variables used by the loop. */
4045 tree tbase, tbase_init;
4047 /* This is the body of the loop that implements the deletion of a
4048 single element, and moves temp variables to next elements. */
4049 tree body;
4051 /* This is the LOOP_EXPR that governs the deletion of the elements. */
4052 tree loop = 0;
4054 /* This is the thing that governs what to do after the loop has run. */
4055 tree deallocate_expr = 0;
4057 /* This is the BIND_EXPR which holds the outermost iterator of the
4058 loop. It is convenient to set this variable up and test it before
4059 executing any other code in the loop.
4060 This is also the containing expression returned by this function. */
4061 tree controller = NULL_TREE;
4062 tree tmp;
4064 /* We should only have 1-D arrays here. */
4065 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
4067 if (base == error_mark_node || maxindex == error_mark_node)
4068 return error_mark_node;
4070 if (!verify_type_context (loc, TCTX_DEALLOCATION, type,
4071 !(complain & tf_error)))
4072 return error_mark_node;
4074 if (!COMPLETE_TYPE_P (type))
4076 if (complain & tf_warning)
4078 auto_diagnostic_group d;
4079 if (warning_at (loc, OPT_Wdelete_incomplete,
4080 "possible problem detected in invocation of "
4081 "operator %<delete []%>"))
4083 cxx_incomplete_type_diagnostic (base, type, DK_WARNING);
4084 inform (loc, "neither the destructor nor the "
4085 "class-specific operator %<delete []%> will be called, "
4086 "even if they are declared when the class is defined");
4089 /* This size won't actually be used. */
4090 size_exp = size_one_node;
4091 goto no_destructor;
4094 size_exp = size_in_bytes (type);
4096 if (! MAYBE_CLASS_TYPE_P (type))
4097 goto no_destructor;
4098 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
4100 /* Make sure the destructor is callable. */
4101 if (type_build_dtor_call (type))
4103 tmp = build_delete (loc, ptype, base, sfk_complete_destructor,
4104 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
4105 complain);
4106 if (tmp == error_mark_node)
4107 return error_mark_node;
4109 goto no_destructor;
4112 /* The below is short by the cookie size. */
4113 virtual_size = size_binop (MULT_EXPR, size_exp,
4114 fold_convert (sizetype, maxindex));
4116 tbase = create_temporary_var (ptype);
4117 DECL_INITIAL (tbase)
4118 = fold_build_pointer_plus_loc (loc, fold_convert (ptype, base),
4119 virtual_size);
4120 tbase_init = build_stmt (loc, DECL_EXPR, tbase);
4121 controller = build3 (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
4122 TREE_SIDE_EFFECTS (controller) = 1;
4123 BIND_EXPR_VEC_DTOR (controller) = true;
4125 body = build1 (EXIT_EXPR, void_type_node,
4126 build2 (EQ_EXPR, boolean_type_node, tbase,
4127 fold_convert (ptype, base)));
4128 tmp = fold_build1_loc (loc, NEGATE_EXPR, sizetype, size_exp);
4129 tmp = fold_build_pointer_plus (tbase, tmp);
4130 tmp = cp_build_modify_expr (loc, tbase, NOP_EXPR, tmp, complain);
4131 if (tmp == error_mark_node)
4132 return error_mark_node;
4133 body = build_compound_expr (loc, body, tmp);
4134 tmp = build_delete (loc, ptype, tbase, sfk_complete_destructor,
4135 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
4136 complain);
4137 if (tmp == error_mark_node)
4138 return error_mark_node;
4139 body = build_compound_expr (loc, body, tmp);
4141 loop = build1 (LOOP_EXPR, void_type_node, body);
4143 /* If one destructor throws, keep trying to clean up the rest, unless we're
4144 already in a build_vec_init cleanup. */
4145 if (flag_exceptions && !in_cleanup && !expr_noexcept_p (tmp, tf_none))
4147 loop = build2 (TRY_CATCH_EXPR, void_type_node, loop,
4148 unshare_expr (loop));
4149 /* Tell honor_protect_cleanup_actions to discard this on the
4150 exceptional path. */
4151 TRY_CATCH_IS_CLEANUP (loop) = true;
4154 loop = build_compound_expr (loc, tbase_init, loop);
4156 no_destructor:
4157 /* Delete the storage if appropriate. */
4158 if (auto_delete_vec == sfk_deleting_destructor)
4160 tree base_tbd;
4162 /* The below is short by the cookie size. */
4163 virtual_size = size_binop (MULT_EXPR, size_exp,
4164 fold_convert (sizetype, maxindex));
4166 if (! TYPE_VEC_NEW_USES_COOKIE (type))
4167 /* no header */
4168 base_tbd = base;
4169 else
4171 tree cookie_size;
4173 cookie_size = targetm.cxx.get_cookie_size (type);
4174 base_tbd = cp_build_binary_op (loc,
4175 MINUS_EXPR,
4176 cp_convert (string_type_node,
4177 base, complain),
4178 cookie_size,
4179 complain);
4180 if (base_tbd == error_mark_node)
4181 return error_mark_node;
4182 base_tbd = cp_convert (ptype, base_tbd, complain);
4183 /* True size with header. */
4184 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
4187 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
4188 base_tbd, virtual_size,
4189 use_global_delete & 1,
4190 /*placement=*/NULL_TREE,
4191 /*alloc_fn=*/NULL_TREE,
4192 complain);
4195 body = loop;
4196 if (deallocate_expr == error_mark_node)
4197 return error_mark_node;
4198 else if (!deallocate_expr)
4200 else if (!body)
4201 body = deallocate_expr;
4202 else
4203 /* The delete operator must be called, even if a destructor
4204 throws. */
4205 body = build2 (TRY_FINALLY_EXPR, void_type_node, body, deallocate_expr);
4207 if (!body)
4208 body = integer_zero_node;
4210 /* Outermost wrapper: If pointer is null, punt. */
4211 tree cond = build2_loc (loc, NE_EXPR, boolean_type_node, base,
4212 fold_convert (TREE_TYPE (base), nullptr_node));
4213 /* This is a compiler generated comparison, don't emit
4214 e.g. -Wnonnull-compare warning for it. */
4215 suppress_warning (cond, OPT_Wnonnull_compare);
4216 body = build3_loc (loc, COND_EXPR, void_type_node,
4217 cond, body, integer_zero_node);
4218 COND_EXPR_IS_VEC_DELETE (body) = true;
4219 body = build1 (NOP_EXPR, void_type_node, body);
4221 if (controller)
4223 TREE_OPERAND (controller, 1) = body;
4224 body = controller;
4227 if (TREE_CODE (base) == SAVE_EXPR)
4228 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
4229 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
4231 return convert_to_void (body, ICV_CAST, complain);
4234 /* Create an unnamed variable of the indicated TYPE. */
4236 tree
4237 create_temporary_var (tree type)
4239 tree decl;
4241 decl = build_decl (input_location,
4242 VAR_DECL, NULL_TREE, type);
4243 TREE_USED (decl) = 1;
4244 DECL_ARTIFICIAL (decl) = 1;
4245 DECL_IGNORED_P (decl) = 1;
4246 DECL_CONTEXT (decl) = current_function_decl;
4248 return decl;
4251 /* Create a new temporary variable of the indicated TYPE, initialized
4252 to INIT.
4254 It is not entered into current_binding_level, because that breaks
4255 things when it comes time to do final cleanups (which take place
4256 "outside" the binding contour of the function). */
4258 tree
4259 get_temp_regvar (tree type, tree init)
4261 tree decl;
4263 decl = create_temporary_var (type);
4264 add_decl_expr (decl);
4266 finish_expr_stmt (cp_build_modify_expr (input_location, decl, INIT_EXPR,
4267 init, tf_warning_or_error));
4269 return decl;
4272 /* Subroutine of build_vec_init. Returns true if assigning to an array of
4273 INNER_ELT_TYPE from INIT is trivial. */
4275 static bool
4276 vec_copy_assign_is_trivial (tree inner_elt_type, tree init)
4278 tree fromtype = inner_elt_type;
4279 if (lvalue_p (init))
4280 fromtype = cp_build_reference_type (fromtype, /*rval*/false);
4281 return is_trivially_xible (MODIFY_EXPR, inner_elt_type, fromtype);
4284 /* Subroutine of build_vec_init: Check that the array has at least N
4285 elements. Other parameters are local variables in build_vec_init. */
4287 void
4288 finish_length_check (tree atype, tree iterator, tree obase, unsigned n)
4290 tree nelts = build_int_cst (ptrdiff_type_node, n - 1);
4291 if (TREE_CODE (atype) != ARRAY_TYPE)
4293 if (flag_exceptions)
4295 tree c = fold_build2 (LT_EXPR, boolean_type_node, iterator,
4296 nelts);
4297 c = build3 (COND_EXPR, void_type_node, c,
4298 throw_bad_array_new_length (), void_node);
4299 finish_expr_stmt (c);
4301 /* Don't check an array new when -fno-exceptions. */
4303 else if (sanitize_flags_p (SANITIZE_BOUNDS)
4304 && current_function_decl != NULL_TREE)
4306 /* Make sure the last element of the initializer is in bounds. */
4307 finish_expr_stmt
4308 (ubsan_instrument_bounds
4309 (input_location, obase, &nelts, /*ignore_off_by_one*/false));
4313 /* `build_vec_init' returns tree structure that performs
4314 initialization of a vector of aggregate types.
4316 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
4317 to the first element, of POINTER_TYPE.
4318 MAXINDEX is the maximum index of the array (one less than the
4319 number of elements). It is only used if BASE is a pointer or
4320 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
4322 INIT is the (possibly NULL) initializer.
4324 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
4325 elements in the array are value-initialized.
4327 FROM_ARRAY is 0 if we should init everything with INIT
4328 (i.e., every element initialized from INIT).
4329 FROM_ARRAY is 1 if we should index into INIT in parallel
4330 with initialization of DECL.
4331 FROM_ARRAY is 2 if we should index into INIT in parallel,
4332 but use assignment instead of initialization. */
4334 tree
4335 build_vec_init (tree base, tree maxindex, tree init,
4336 bool explicit_value_init_p,
4337 int from_array,
4338 tsubst_flags_t complain,
4339 vec<tree, va_gc>** flags /* = nullptr */)
4341 tree rval;
4342 tree base2 = NULL_TREE;
4343 tree itype = NULL_TREE;
4344 tree iterator;
4345 /* The type of BASE. */
4346 tree atype = TREE_TYPE (base);
4347 /* The type of an element in the array. */
4348 tree type = TREE_TYPE (atype);
4349 /* The element type reached after removing all outer array
4350 types. */
4351 tree inner_elt_type;
4352 /* The type of a pointer to an element in the array. */
4353 tree ptype;
4354 tree stmt_expr;
4355 tree compound_stmt;
4356 int destroy_temps;
4357 HOST_WIDE_INT num_initialized_elts = 0;
4358 bool is_global;
4359 tree obase = base;
4360 bool xvalue = false;
4361 bool errors = false;
4362 location_t loc = (init ? cp_expr_loc_or_input_loc (init)
4363 : location_of (base));
4365 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
4366 maxindex = array_type_nelts (atype);
4368 if (maxindex == NULL_TREE || maxindex == error_mark_node)
4369 return error_mark_node;
4371 maxindex = maybe_constant_value (maxindex);
4372 if (explicit_value_init_p)
4373 gcc_assert (!init);
4375 inner_elt_type = strip_array_types (type);
4377 /* Look through the TARGET_EXPR around a compound literal. */
4378 if (init && TREE_CODE (init) == TARGET_EXPR
4379 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
4380 && from_array != 2)
4381 init = TARGET_EXPR_INITIAL (init);
4383 if (tree vi = get_vec_init_expr (init))
4384 init = VEC_INIT_EXPR_INIT (vi);
4386 bool direct_init = false;
4387 if (from_array && init && BRACE_ENCLOSED_INITIALIZER_P (init)
4388 && CONSTRUCTOR_NELTS (init) == 1)
4390 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
4391 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
4392 && TREE_CODE (elt) != VEC_INIT_EXPR)
4394 direct_init = DIRECT_LIST_INIT_P (init);
4395 init = elt;
4399 /* If we have a braced-init-list or string constant, make sure that the array
4400 is big enough for all the initializers. */
4401 bool length_check = (init
4402 && (TREE_CODE (init) == STRING_CST
4403 || (TREE_CODE (init) == CONSTRUCTOR
4404 && CONSTRUCTOR_NELTS (init) > 0))
4405 && !TREE_CONSTANT (maxindex));
4407 if (init
4408 && TREE_CODE (atype) == ARRAY_TYPE
4409 && TREE_CONSTANT (maxindex)
4410 && !vla_type_p (type)
4411 && (from_array == 2
4412 ? vec_copy_assign_is_trivial (inner_elt_type, init)
4413 : !TYPE_NEEDS_CONSTRUCTING (type))
4414 && ((TREE_CODE (init) == CONSTRUCTOR
4415 && (BRACE_ENCLOSED_INITIALIZER_P (init)
4416 || (same_type_ignoring_top_level_qualifiers_p
4417 (atype, TREE_TYPE (init))))
4418 /* Don't do this if the CONSTRUCTOR might contain something
4419 that might throw and require us to clean up. */
4420 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
4421 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
4422 || from_array))
4424 /* Do non-default initialization of trivial arrays resulting from
4425 brace-enclosed initializers. In this case, digest_init and
4426 store_constructor will handle the semantics for us. */
4428 if (BRACE_ENCLOSED_INITIALIZER_P (init))
4429 init = digest_init (atype, init, complain);
4430 stmt_expr = build2 (INIT_EXPR, atype, base, init);
4431 return stmt_expr;
4434 maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
4435 maxindex = fold_simple (maxindex);
4437 if (TREE_CODE (atype) == ARRAY_TYPE)
4439 ptype = build_pointer_type (type);
4440 base = decay_conversion (base, complain);
4441 if (base == error_mark_node)
4442 return error_mark_node;
4443 base = cp_convert (ptype, base, complain);
4445 else
4446 ptype = atype;
4448 if (integer_all_onesp (maxindex))
4450 /* Shortcut zero element case to avoid unneeded constructor synthesis. */
4451 if (init && TREE_SIDE_EFFECTS (init))
4452 base = build2 (COMPOUND_EXPR, ptype, init, base);
4453 return base;
4456 /* The code we are generating looks like:
4458 T* t1 = (T*) base;
4459 T* rval = t1;
4460 ptrdiff_t iterator = maxindex;
4461 try {
4462 for (; iterator != -1; --iterator) {
4463 ... initialize *t1 ...
4464 ++t1;
4466 } catch (...) {
4467 ... destroy elements that were constructed ...
4469 rval;
4472 We can omit the try and catch blocks if we know that the
4473 initialization will never throw an exception, or if the array
4474 elements do not have destructors. We can omit the loop completely if
4475 the elements of the array do not have constructors.
4477 We actually wrap the entire body of the above in a STMT_EXPR, for
4478 tidiness.
4480 When copying from array to another, when the array elements have
4481 only trivial copy constructors, we should use __builtin_memcpy
4482 rather than generating a loop. That way, we could take advantage
4483 of whatever cleverness the back end has for dealing with copies
4484 of blocks of memory. */
4486 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
4487 destroy_temps = stmts_are_full_exprs_p ();
4488 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4489 rval = get_temp_regvar (ptype, base);
4490 base = get_temp_regvar (ptype, rval);
4491 tree iterator_targ = get_target_expr (maxindex);
4492 add_stmt (iterator_targ);
4493 iterator = TARGET_EXPR_SLOT (iterator_targ);
4495 /* If initializing one array from another, initialize element by
4496 element. We rely upon the below calls to do the argument
4497 checking. Evaluate the initializer before entering the try block. */
4498 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
4500 if (lvalue_kind (init) & clk_rvalueref)
4501 xvalue = true;
4502 base2 = decay_conversion (init, complain);
4503 if (base2 == error_mark_node)
4504 return error_mark_node;
4505 itype = TREE_TYPE (base2);
4506 base2 = get_temp_regvar (itype, base2);
4507 itype = TREE_TYPE (itype);
4510 /* Protect the entire array initialization so that we can destroy
4511 the partially constructed array if an exception is thrown.
4512 But don't do this if we're assigning. */
4513 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4514 && from_array != 2)
4516 tree e;
4517 tree m = cp_build_binary_op (input_location,
4518 MINUS_EXPR, maxindex, iterator,
4519 complain);
4521 /* Flatten multi-dimensional array since build_vec_delete only
4522 expects one-dimensional array. */
4523 if (TREE_CODE (type) == ARRAY_TYPE)
4524 m = cp_build_binary_op (input_location,
4525 MULT_EXPR, m,
4526 /* Avoid mixing signed and unsigned. */
4527 convert (TREE_TYPE (m),
4528 array_type_nelts_total (type)),
4529 complain);
4531 e = build_vec_delete_1 (input_location, rval, m,
4532 inner_elt_type, sfk_complete_destructor,
4533 /*use_global_delete=*/0, complain,
4534 /*in_cleanup*/true);
4535 if (e == error_mark_node)
4536 errors = true;
4537 TARGET_EXPR_CLEANUP (iterator_targ) = e;
4538 CLEANUP_EH_ONLY (iterator_targ) = true;
4540 /* Since we push this cleanup before doing any initialization, cleanups
4541 for any temporaries in the initialization are naturally within our
4542 cleanup region, so we don't want wrap_temporary_cleanups to do
4543 anything for arrays. But if the array is a subobject, we need to
4544 tell split_nonconstant_init how to turn off this cleanup in favor of
4545 the cleanup for the complete object. */
4546 if (flags)
4547 vec_safe_push (*flags, build_tree_list (iterator, maxindex));
4550 /* Should we try to create a constant initializer? */
4551 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
4552 && TREE_CONSTANT (maxindex)
4553 && (init ? TREE_CODE (init) == CONSTRUCTOR
4554 : (type_has_constexpr_default_constructor
4555 (inner_elt_type)))
4556 && (literal_type_p (inner_elt_type)
4557 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
4558 vec<constructor_elt, va_gc> *const_vec = NULL;
4559 bool saw_non_const = false;
4560 /* If we're initializing a static array, we want to do static
4561 initialization of any elements with constant initializers even if
4562 some are non-constant. */
4563 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
4565 bool empty_list = false;
4566 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
4567 && CONSTRUCTOR_NELTS (init) == 0)
4568 /* Skip over the handling of non-empty init lists. */
4569 empty_list = true;
4571 /* Maybe pull out constant value when from_array? */
4573 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
4575 /* Do non-default initialization of non-trivial arrays resulting from
4576 brace-enclosed initializers. */
4577 unsigned HOST_WIDE_INT idx;
4578 tree field, elt;
4579 /* If the constructor already has the array type, it's been through
4580 digest_init, so we shouldn't try to do anything more. */
4581 bool digested = same_type_p (atype, TREE_TYPE (init));
4582 from_array = 0;
4584 if (length_check)
4585 finish_length_check (atype, iterator, obase, CONSTRUCTOR_NELTS (init));
4587 if (try_const)
4588 vec_alloc (const_vec, CONSTRUCTOR_NELTS (init));
4590 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
4592 tree baseref = build1 (INDIRECT_REF, type, base);
4593 tree one_init;
4595 num_initialized_elts++;
4597 /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can
4598 handle cleanup flags properly. */
4599 gcc_checking_assert (!target_expr_needs_replace (elt));
4601 if (digested)
4602 one_init = build2 (INIT_EXPR, type, baseref, elt);
4603 else if (tree vi = get_vec_init_expr (elt))
4604 one_init = expand_vec_init_expr (baseref, vi, complain, flags);
4605 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
4606 one_init = build_aggr_init (baseref, elt, 0, complain);
4607 else
4608 one_init = cp_build_modify_expr (input_location, baseref,
4609 NOP_EXPR, elt, complain);
4610 if (one_init == error_mark_node)
4611 errors = true;
4612 if (try_const)
4614 if (!field)
4615 field = size_int (idx);
4616 tree e = maybe_constant_init (one_init);
4617 if (reduced_constant_expression_p (e))
4619 CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
4620 if (do_static_init)
4621 one_init = NULL_TREE;
4622 else
4623 one_init = build2 (INIT_EXPR, type, baseref, e);
4625 else
4627 if (do_static_init)
4629 tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
4630 true);
4631 if (value)
4632 CONSTRUCTOR_APPEND_ELT (const_vec, field, value);
4634 saw_non_const = true;
4638 if (one_init)
4639 finish_expr_stmt (one_init);
4641 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, false,
4642 complain);
4643 if (one_init == error_mark_node)
4644 errors = true;
4645 else
4646 finish_expr_stmt (one_init);
4648 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
4649 complain);
4650 if (one_init == error_mark_node)
4651 errors = true;
4652 else
4653 finish_expr_stmt (one_init);
4656 /* Any elements without explicit initializers get T{}. */
4657 empty_list = true;
4659 else if (init && TREE_CODE (init) == STRING_CST)
4661 /* Check that the array is at least as long as the string. */
4662 if (length_check)
4663 finish_length_check (atype, iterator, obase,
4664 TREE_STRING_LENGTH (init));
4665 tree length = build_int_cst (ptrdiff_type_node,
4666 TREE_STRING_LENGTH (init));
4668 /* Copy the string to the first part of the array. */
4669 tree alias_set = build_int_cst (build_pointer_type (type), 0);
4670 tree lhs = build2 (MEM_REF, TREE_TYPE (init), base, alias_set);
4671 tree stmt = build2 (MODIFY_EXPR, void_type_node, lhs, init);
4672 finish_expr_stmt (stmt);
4674 /* Adjust the counter and pointer. */
4675 stmt = cp_build_binary_op (loc, MINUS_EXPR, iterator, length, complain);
4676 stmt = build2 (MODIFY_EXPR, void_type_node, iterator, stmt);
4677 finish_expr_stmt (stmt);
4679 stmt = cp_build_binary_op (loc, PLUS_EXPR, base, length, complain);
4680 stmt = build2 (MODIFY_EXPR, void_type_node, base, stmt);
4681 finish_expr_stmt (stmt);
4683 /* And set the rest of the array to NUL. */
4684 from_array = 0;
4685 explicit_value_init_p = true;
4687 else if (from_array)
4689 if (init)
4690 /* OK, we set base2 above. */;
4691 else if (CLASS_TYPE_P (type)
4692 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4694 if (complain & tf_error)
4695 error ("initializer ends prematurely");
4696 errors = true;
4700 /* Now, default-initialize any remaining elements. We don't need to
4701 do that if a) the type does not need constructing, or b) we've
4702 already initialized all the elements.
4704 We do need to keep going if we're copying an array. */
4706 if (try_const && !init
4707 && (cxx_dialect < cxx20
4708 || !default_init_uninitialized_part (inner_elt_type)))
4709 /* With a constexpr default constructor, which we checked for when
4710 setting try_const above, default-initialization is equivalent to
4711 value-initialization, and build_value_init gives us something more
4712 friendly to maybe_constant_init. Except in C++20 and up a constexpr
4713 constructor need not initialize all the members. */
4714 explicit_value_init_p = true;
4715 if (from_array
4716 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
4717 && ! (tree_fits_shwi_p (maxindex)
4718 && (num_initialized_elts
4719 == tree_to_shwi (maxindex) + 1))))
4721 /* If the ITERATOR is lesser or equal to -1, then we don't have to loop;
4722 we've already initialized all the elements. */
4723 tree for_stmt;
4724 tree elt_init;
4725 tree to;
4727 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
4728 finish_init_stmt (for_stmt);
4729 finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator,
4730 build_int_cst (TREE_TYPE (iterator), -1)),
4731 for_stmt, false, 0);
4732 /* We used to pass this decrement to finish_for_expr; now we add it to
4733 elt_init below so it's part of the same full-expression as the
4734 initialization, and thus happens before any potentially throwing
4735 temporary cleanups. */
4736 tree decr = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
4737 complain);
4740 to = build1 (INDIRECT_REF, type, base);
4742 /* If the initializer is {}, then all elements are initialized from T{}.
4743 But for non-classes, that's the same as value-initialization. */
4744 if (empty_list)
4746 if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type))
4748 init = build_constructor (init_list_type_node, NULL);
4750 else
4752 init = NULL_TREE;
4753 explicit_value_init_p = true;
4757 if (from_array)
4759 tree from;
4761 if (base2)
4763 from = build1 (INDIRECT_REF, itype, base2);
4764 if (xvalue)
4765 from = move (from);
4766 if (direct_init)
4767 from = build_tree_list (NULL_TREE, from);
4769 else
4770 from = NULL_TREE;
4772 if (TREE_CODE (type) == ARRAY_TYPE)
4773 elt_init = build_vec_init (to, NULL_TREE, from, /*val_init*/false,
4774 from_array, complain);
4775 else if (from_array == 2)
4776 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR,
4777 from, complain);
4778 else if (type_build_ctor_call (type))
4779 elt_init = build_aggr_init (to, from, 0, complain);
4780 else if (from)
4781 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, from,
4782 complain);
4783 else
4784 gcc_unreachable ();
4786 else if (TREE_CODE (type) == ARRAY_TYPE)
4788 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
4790 if ((complain & tf_error))
4791 error_at (loc, "array must be initialized "
4792 "with a brace-enclosed initializer");
4793 elt_init = error_mark_node;
4795 else
4796 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
4797 0, init,
4798 explicit_value_init_p,
4799 0, complain);
4801 else if (explicit_value_init_p)
4803 elt_init = build_value_init (type, complain);
4804 if (elt_init != error_mark_node)
4805 elt_init = build2 (INIT_EXPR, type, to, elt_init);
4807 else
4809 gcc_assert (type_build_ctor_call (type) || init);
4810 if (CLASS_TYPE_P (type))
4811 elt_init = build_aggr_init (to, init, 0, complain);
4812 else
4814 if (TREE_CODE (init) == TREE_LIST)
4815 init = build_x_compound_expr_from_list (init, ELK_INIT,
4816 complain);
4817 elt_init = (init == error_mark_node
4818 ? error_mark_node
4819 : build2 (INIT_EXPR, type, to, init));
4823 if (elt_init == error_mark_node)
4824 errors = true;
4826 if (try_const)
4828 /* FIXME refs to earlier elts */
4829 tree e = maybe_constant_init (elt_init);
4830 if (reduced_constant_expression_p (e))
4832 if (initializer_zerop (e))
4833 /* Don't fill the CONSTRUCTOR with zeros. */
4834 e = NULL_TREE;
4835 if (do_static_init)
4836 elt_init = NULL_TREE;
4838 else
4840 saw_non_const = true;
4841 if (do_static_init)
4842 e = build_zero_init (TREE_TYPE (e), NULL_TREE, true);
4843 else
4844 e = NULL_TREE;
4847 if (e)
4849 HOST_WIDE_INT last = tree_to_shwi (maxindex);
4850 if (num_initialized_elts <= last)
4852 tree field = size_int (num_initialized_elts);
4853 if (num_initialized_elts != last)
4854 field = build2 (RANGE_EXPR, sizetype, field,
4855 size_int (last));
4856 CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
4861 /* [class.temporary]: "There are three contexts in which temporaries are
4862 destroyed at a different point than the end of the full-
4863 expression. The first context is when a default constructor is called
4864 to initialize an element of an array with no corresponding
4865 initializer. The second context is when a copy constructor is called
4866 to copy an element of an array while the entire array is copied. In
4867 either case, if the constructor has one or more default arguments, the
4868 destruction of every temporary created in a default argument is
4869 sequenced before the construction of the next array element, if any."
4871 So, for this loop, statements are full-expressions. */
4872 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4873 if (elt_init && !errors)
4874 elt_init = build2 (COMPOUND_EXPR, void_type_node, elt_init, decr);
4875 else
4876 elt_init = decr;
4877 finish_expr_stmt (elt_init);
4878 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4880 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false,
4881 complain));
4882 if (base2)
4883 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, false,
4884 complain));
4886 finish_for_stmt (for_stmt);
4889 /* The value of the array initialization is the array itself, RVAL
4890 is a pointer to the first element. */
4891 finish_stmt_expr_expr (rval, stmt_expr);
4893 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
4895 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
4897 if (errors)
4898 return error_mark_node;
4900 if (try_const)
4902 if (!saw_non_const)
4904 tree const_init = build_constructor (atype, const_vec);
4905 return build2 (INIT_EXPR, atype, obase, const_init);
4907 else if (do_static_init && !vec_safe_is_empty (const_vec))
4908 DECL_INITIAL (obase) = build_constructor (atype, const_vec);
4909 else
4910 vec_free (const_vec);
4913 /* Now make the result have the correct type. */
4914 if (TREE_CODE (atype) == ARRAY_TYPE)
4916 atype = build_reference_type (atype);
4917 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
4918 stmt_expr = convert_from_reference (stmt_expr);
4921 return stmt_expr;
4924 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
4925 build_delete. */
4927 static tree
4928 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
4929 tsubst_flags_t complain)
4931 tree name;
4932 switch (dtor_kind)
4934 case sfk_complete_destructor:
4935 name = complete_dtor_identifier;
4936 break;
4938 case sfk_base_destructor:
4939 name = base_dtor_identifier;
4940 break;
4942 case sfk_deleting_destructor:
4943 name = deleting_dtor_identifier;
4944 break;
4946 default:
4947 gcc_unreachable ();
4950 return build_special_member_call (exp, name,
4951 /*args=*/NULL,
4952 /*binfo=*/TREE_TYPE (exp),
4953 flags,
4954 complain);
4957 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
4958 ADDR is an expression which yields the store to be destroyed.
4959 AUTO_DELETE is the name of the destructor to call, i.e., either
4960 sfk_complete_destructor, sfk_base_destructor, or
4961 sfk_deleting_destructor.
4963 FLAGS is the logical disjunction of zero or more LOOKUP_
4964 flags. See cp-tree.h for more info. */
4966 tree
4967 build_delete (location_t loc, tree otype, tree addr,
4968 special_function_kind auto_delete,
4969 int flags, int use_global_delete, tsubst_flags_t complain)
4971 tree expr;
4973 if (addr == error_mark_node)
4974 return error_mark_node;
4976 tree type = TYPE_MAIN_VARIANT (otype);
4978 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
4979 set to `error_mark_node' before it gets properly cleaned up. */
4980 if (type == error_mark_node)
4981 return error_mark_node;
4983 if (TYPE_PTR_P (type))
4984 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4986 if (TREE_CODE (type) == ARRAY_TYPE)
4988 if (TYPE_DOMAIN (type) == NULL_TREE)
4990 if (complain & tf_error)
4991 error_at (loc, "unknown array size in delete");
4992 return error_mark_node;
4994 return build_vec_delete (loc, addr, array_type_nelts (type),
4995 auto_delete, use_global_delete, complain);
4998 bool deleting = (auto_delete == sfk_deleting_destructor);
4999 gcc_assert (deleting == !(flags & LOOKUP_DESTRUCTOR));
5001 if (TYPE_PTR_P (otype))
5003 addr = mark_rvalue_use (addr);
5005 /* We don't want to warn about delete of void*, only other
5006 incomplete types. Deleting other incomplete types
5007 invokes undefined behavior, but it is not ill-formed, so
5008 compile to something that would even do The Right Thing
5009 (TM) should the type have a trivial dtor and no delete
5010 operator. */
5011 if (!VOID_TYPE_P (type))
5013 complete_type (type);
5014 if (deleting
5015 && !verify_type_context (loc, TCTX_DEALLOCATION, type,
5016 !(complain & tf_error)))
5017 return error_mark_node;
5019 if (!COMPLETE_TYPE_P (type))
5021 if (complain & tf_warning)
5023 auto_diagnostic_group d;
5024 if (warning_at (loc, OPT_Wdelete_incomplete,
5025 "possible problem detected in invocation of "
5026 "%<operator delete%>"))
5028 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
5029 inform (loc,
5030 "neither the destructor nor the class-specific "
5031 "%<operator delete%> will be called, even if "
5032 "they are declared when the class is defined");
5036 else if (deleting && warn_delnonvdtor
5037 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
5038 && TYPE_POLYMORPHIC_P (type))
5040 tree dtor = CLASSTYPE_DESTRUCTOR (type);
5041 if (!dtor || !DECL_VINDEX (dtor))
5043 if (CLASSTYPE_PURE_VIRTUALS (type))
5044 warning_at (loc, OPT_Wdelete_non_virtual_dtor,
5045 "deleting object of abstract class type %qT"
5046 " which has non-virtual destructor"
5047 " will cause undefined behavior", type);
5048 else
5049 warning_at (loc, OPT_Wdelete_non_virtual_dtor,
5050 "deleting object of polymorphic class type %qT"
5051 " which has non-virtual destructor"
5052 " might cause undefined behavior", type);
5057 /* Throw away const and volatile on target type of addr. */
5058 addr = convert_force (build_pointer_type (type), addr, 0, complain);
5060 else
5062 /* Don't check PROTECT here; leave that decision to the
5063 destructor. If the destructor is accessible, call it,
5064 else report error. */
5065 addr = cp_build_addr_expr (addr, complain);
5066 if (addr == error_mark_node)
5067 return error_mark_node;
5069 addr = convert_force (build_pointer_type (type), addr, 0, complain);
5072 if (deleting)
5073 /* We will use ADDR multiple times so we must save it. */
5074 addr = save_expr (addr);
5076 bool virtual_p = false;
5077 if (type_build_dtor_call (type))
5079 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
5080 lazily_declare_fn (sfk_destructor, type);
5081 virtual_p = DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type));
5084 tree head = NULL_TREE;
5085 tree do_delete = NULL_TREE;
5086 bool destroying_delete = false;
5088 if (!deleting)
5090 /* Leave do_delete null. */
5092 /* For `::delete x', we must not use the deleting destructor
5093 since then we would not be sure to get the global `operator
5094 delete'. */
5095 else if (use_global_delete)
5097 head = get_target_expr (build_headof (addr));
5098 /* Delete the object. */
5099 do_delete = build_op_delete_call (DELETE_EXPR,
5100 head,
5101 cxx_sizeof_nowarn (type),
5102 /*global_p=*/true,
5103 /*placement=*/NULL_TREE,
5104 /*alloc_fn=*/NULL_TREE,
5105 complain);
5106 /* Otherwise, treat this like a complete object destructor
5107 call. */
5108 auto_delete = sfk_complete_destructor;
5110 /* If the destructor is non-virtual, there is no deleting
5111 variant. Instead, we must explicitly call the appropriate
5112 `operator delete' here. */
5113 else if (!virtual_p)
5115 /* Build the call. */
5116 do_delete = build_op_delete_call (DELETE_EXPR,
5117 addr,
5118 cxx_sizeof_nowarn (type),
5119 /*global_p=*/false,
5120 /*placement=*/NULL_TREE,
5121 /*alloc_fn=*/NULL_TREE,
5122 complain);
5123 /* Call the complete object destructor. */
5124 auto_delete = sfk_complete_destructor;
5125 if (do_delete != error_mark_node)
5127 tree fn = get_callee_fndecl (do_delete);
5128 destroying_delete = destroying_delete_p (fn);
5131 else if (TYPE_GETS_REG_DELETE (type))
5133 /* Make sure we have access to the member op delete, even though
5134 we'll actually be calling it from the destructor. */
5135 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
5136 /*global_p=*/false,
5137 /*placement=*/NULL_TREE,
5138 /*alloc_fn=*/NULL_TREE,
5139 complain);
5142 if (destroying_delete)
5143 /* The operator delete will call the destructor. */
5144 expr = addr;
5145 else if (type_build_dtor_call (type))
5146 expr = build_dtor_call (cp_build_fold_indirect_ref (addr),
5147 auto_delete, flags, complain);
5148 else
5149 expr = build_trivial_dtor_call (addr);
5150 if (expr == error_mark_node)
5151 return error_mark_node;
5153 if (!deleting)
5155 protected_set_expr_location (expr, loc);
5156 return expr;
5159 if (do_delete == error_mark_node)
5160 return error_mark_node;
5162 if (do_delete && !TREE_SIDE_EFFECTS (expr))
5163 expr = do_delete;
5164 else if (do_delete)
5165 /* The delete operator must be called, regardless of whether
5166 the destructor throws.
5168 [expr.delete]/7 The deallocation function is called
5169 regardless of whether the destructor for the object or some
5170 element of the array throws an exception. */
5171 expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete);
5173 /* We need to calculate this before the dtor changes the vptr. */
5174 if (head)
5175 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
5177 /* Handle deleting a null pointer. */
5178 warning_sentinel s (warn_address);
5179 tree ifexp = cp_build_binary_op (loc, NE_EXPR, addr,
5180 nullptr_node, complain);
5181 ifexp = cp_fully_fold (ifexp);
5183 if (ifexp == error_mark_node)
5184 return error_mark_node;
5185 /* This is a compiler generated comparison, don't emit
5186 e.g. -Wnonnull-compare warning for it. */
5187 else if (TREE_CODE (ifexp) == NE_EXPR)
5188 suppress_warning (ifexp, OPT_Wnonnull_compare);
5190 if (!integer_nonzerop (ifexp))
5191 expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node);
5193 protected_set_expr_location (expr, loc);
5194 return expr;
5197 /* At the beginning of a destructor, push cleanups that will call the
5198 destructors for our base classes and members.
5200 Called from begin_destructor_body. */
5202 void
5203 push_base_cleanups (void)
5205 tree binfo, base_binfo;
5206 int i;
5207 tree member;
5208 tree expr;
5209 vec<tree, va_gc> *vbases;
5211 /* Run destructors for all virtual baseclasses. */
5212 if (!ABSTRACT_CLASS_TYPE_P (current_class_type)
5213 && CLASSTYPE_VBASECLASSES (current_class_type))
5215 tree cond = (condition_conversion
5216 (build2 (BIT_AND_EXPR, integer_type_node,
5217 current_in_charge_parm,
5218 integer_two_node)));
5220 /* The CLASSTYPE_VBASECLASSES vector is in initialization
5221 order, which is also the right order for pushing cleanups. */
5222 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
5223 vec_safe_iterate (vbases, i, &base_binfo); i++)
5225 if (type_build_dtor_call (BINFO_TYPE (base_binfo)))
5227 expr = build_special_member_call (current_class_ref,
5228 base_dtor_identifier,
5229 NULL,
5230 base_binfo,
5231 (LOOKUP_NORMAL
5232 | LOOKUP_NONVIRTUAL),
5233 tf_warning_or_error);
5234 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5236 expr = build3 (COND_EXPR, void_type_node, cond,
5237 expr, void_node);
5238 finish_decl_cleanup (NULL_TREE, expr);
5244 /* Take care of the remaining baseclasses. */
5245 for (binfo = TYPE_BINFO (current_class_type), i = 0;
5246 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5248 if (BINFO_VIRTUAL_P (base_binfo)
5249 || !type_build_dtor_call (BINFO_TYPE (base_binfo)))
5250 continue;
5252 expr = build_special_member_call (current_class_ref,
5253 base_dtor_identifier,
5254 NULL, base_binfo,
5255 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
5256 tf_warning_or_error);
5257 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5258 finish_decl_cleanup (NULL_TREE, expr);
5261 /* Don't automatically destroy union members. */
5262 if (TREE_CODE (current_class_type) == UNION_TYPE)
5263 return;
5265 for (member = TYPE_FIELDS (current_class_type); member;
5266 member = DECL_CHAIN (member))
5268 tree this_type = TREE_TYPE (member);
5269 if (this_type == error_mark_node
5270 || TREE_CODE (member) != FIELD_DECL
5271 || DECL_ARTIFICIAL (member))
5272 continue;
5273 if (ANON_AGGR_TYPE_P (this_type))
5274 continue;
5275 if (type_build_dtor_call (this_type))
5277 tree this_member = (build_class_member_access_expr
5278 (current_class_ref, member,
5279 /*access_path=*/NULL_TREE,
5280 /*preserve_reference=*/false,
5281 tf_warning_or_error));
5282 expr = build_delete (input_location, this_type, this_member,
5283 sfk_complete_destructor,
5284 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
5285 0, tf_warning_or_error);
5286 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
5287 finish_decl_cleanup (NULL_TREE, expr);
5292 /* Build a C++ vector delete expression.
5293 MAXINDEX is the number of elements to be deleted.
5294 ELT_SIZE is the nominal size of each element in the vector.
5295 BASE is the expression that should yield the store to be deleted.
5296 This function expands (or synthesizes) these calls itself.
5297 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
5299 This also calls delete for virtual baseclasses of elements of the vector.
5301 Update: MAXINDEX is no longer needed. The size can be extracted from the
5302 start of the vector for pointers, and from the type for arrays. We still
5303 use MAXINDEX for arrays because it happens to already have one of the
5304 values we'd have to extract. (We could use MAXINDEX with pointers to
5305 confirm the size, and trap if the numbers differ; not clear that it'd
5306 be worth bothering.) */
5308 tree
5309 build_vec_delete (location_t loc, tree base, tree maxindex,
5310 special_function_kind auto_delete_vec,
5311 int use_global_delete, tsubst_flags_t complain)
5313 tree type;
5314 tree rval;
5315 tree base_init = NULL_TREE;
5317 type = TREE_TYPE (base);
5319 if (TYPE_PTR_P (type))
5321 /* Step back one from start of vector, and read dimension. */
5322 tree cookie_addr;
5323 tree size_ptr_type = build_pointer_type (sizetype);
5325 base = mark_rvalue_use (base);
5326 if (TREE_SIDE_EFFECTS (base))
5328 base_init = get_target_expr (base);
5329 base = TARGET_EXPR_SLOT (base_init);
5331 type = strip_array_types (TREE_TYPE (type));
5332 cookie_addr = fold_build1_loc (loc, NEGATE_EXPR,
5333 sizetype, TYPE_SIZE_UNIT (sizetype));
5334 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
5335 cookie_addr);
5336 maxindex = cp_build_fold_indirect_ref (cookie_addr);
5338 else if (TREE_CODE (type) == ARRAY_TYPE)
5340 /* Get the total number of things in the array, maxindex is a
5341 bad name. */
5342 maxindex = array_type_nelts_total (type);
5343 type = strip_array_types (type);
5344 base = decay_conversion (base, complain);
5345 if (base == error_mark_node)
5346 return error_mark_node;
5347 if (TREE_SIDE_EFFECTS (base))
5349 base_init = get_target_expr (base);
5350 base = TARGET_EXPR_SLOT (base_init);
5353 else
5355 if (base != error_mark_node && !(complain & tf_error))
5356 error_at (loc,
5357 "type to vector delete is neither pointer or array type");
5358 return error_mark_node;
5361 rval = build_vec_delete_1 (loc, base, maxindex, type, auto_delete_vec,
5362 use_global_delete, complain);
5363 if (base_init && rval != error_mark_node)
5364 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
5366 protected_set_expr_location (rval, loc);
5367 return rval;
5370 #include "gt-cp-init.h"