]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/init.cc
vect: Don't retry if the previous analysis fails
[thirdparty/gcc.git] / gcc / cp / init.cc
CommitLineData
71c5b8ba 1/* Handle initialization things in -*- C++ -*-
83ffe9cd 2 Copyright (C) 1987-2023 Free Software Foundation, Inc.
8d08fdba
MS
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
f5adbb8d 5This file is part of GCC.
8d08fdba 6
f5adbb8d 7GCC is free software; you can redistribute it and/or modify
8d08fdba 8it under the terms of the GNU General Public License as published by
e77f031d 9the Free Software Foundation; either version 3, or (at your option)
8d08fdba
MS
10any later version.
11
f5adbb8d 12GCC is distributed in the hope that it will be useful,
8d08fdba
MS
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e77f031d
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
8d08fdba 20
e92cc029 21/* High-level class interface. */
8d08fdba
MS
22
23#include "config.h"
8d052bc7 24#include "system.h"
4977bab6 25#include "coretypes.h"
2adfab87 26#include "target.h"
2adfab87 27#include "cp-tree.h"
d8a2d370
DN
28#include "stringpool.h"
29#include "varasm.h"
45b0be94 30#include "gimplify.h"
46621807 31#include "c-family/c-ubsan.h"
24f12823 32#include "intl.h"
314e6352
ML
33#include "stringpool.h"
34#include "attribs.h"
45b2222a 35#include "asan.h"
8e007055 36#include "stor-layout.h"
2a837de2 37#include "pointer-query.h"
8d08fdba 38
2a3398e1
NS
39static bool begin_init_stmts (tree *, tree *);
40static tree finish_init_stmts (bool, tree, tree);
2282d28d 41static void construct_virtual_base (tree, tree);
b9119edc
PP
42static bool expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
43static bool expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
362efdc1
NN
44static int member_init_ok_or_else (tree, tree, tree);
45static void expand_virtual_init (tree, tree);
2282d28d 46static tree sort_mem_initializers (tree, tree);
362efdc1
NN
47static tree initializing_context (tree);
48static void expand_cleanup_for_base (tree, tree);
362efdc1 49static tree dfs_initialize_vtbl_ptrs (tree, void *);
362efdc1 50static tree build_field_list (tree, tree, int *);
40bb78ad 51static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
8d08fdba 52
ff502317
BE
53static GTY(()) tree fn;
54
3dbc07b6
MM
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. */
63
2a3398e1 64static bool
362efdc1 65begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
3dbc07b6 66{
38e01f9e 67 bool is_global = !building_stmt_list_p ();
c8094d83 68
2a3398e1 69 *stmt_expr_p = begin_stmt_expr ();
325c3691 70 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
2a3398e1
NS
71
72 return is_global;
3dbc07b6
MM
73}
74
75/* Finish out the statement-expression begun by the previous call to
76 begin_init_stmts. Returns the statement-expression itself. */
77
2a3398e1
NS
78static tree
79finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
c8094d83 80{
7a3397c7 81 finish_compound_stmt (compound_stmt);
c8094d83 82
303b7406 83 stmt_expr = finish_stmt_expr (stmt_expr, true);
3dbc07b6 84
38e01f9e 85 gcc_assert (!building_stmt_list_p () == is_global);
c8094d83 86
3dbc07b6
MM
87 return stmt_expr;
88}
89
90/* Constructors */
91
338d90b8
NS
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. */
7177d104 95
d569399b 96static tree
362efdc1 97dfs_initialize_vtbl_ptrs (tree binfo, void *data)
d569399b 98{
5d5a519f
NS
99 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
100 return dfs_skip_bases;
c8094d83 101
5d5a519f 102 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
d569399b
MM
103 {
104 tree base_ptr = TREE_VALUE ((tree) data);
7177d104 105
a271590a
PC
106 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
107 tf_warning_or_error);
d569399b
MM
108
109 expand_virtual_init (binfo, base_ptr);
110 }
7177d104 111
d569399b
MM
112 return NULL_TREE;
113}
114
cf2e003b
MM
115/* Initialize all the vtable pointers in the object pointed to by
116 ADDR. */
e92cc029 117
8d08fdba 118void
362efdc1 119initialize_vtbl_ptrs (tree addr)
8d08fdba 120{
cf2e003b
MM
121 tree list;
122 tree type;
123
124 type = TREE_TYPE (TREE_TYPE (addr));
125 list = build_tree_list (type, addr);
d569399b 126
bbd15aac 127 /* Walk through the hierarchy, initializing the vptr in each base
1f5a253a 128 class. We do these in pre-order because we can't find the virtual
3461fba7
NS
129 bases for a class until we've initialized the vtbl for that
130 class. */
5d5a519f 131 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
8d08fdba 132}
d569399b 133
17bbb839
MM
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
b43d1bde
PC
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
1cb8292f 143 zero-initialization does not simply mean filling the storage with
e33eba75
JJ
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
a8c1d899
JM
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. */
94e6e4c4 149
e33eba75
JJ
150static tree
151build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
152 tree field_size)
94e6e4c4 153{
17bbb839
MM
154 tree init = NULL_TREE;
155
156 /* [dcl.init]
157
0fcedd9c 158 To zero-initialize an object of type T means:
17bbb839
MM
159
160 -- if T is a scalar type, the storage is set to the value of zero
0cbd7506 161 converted to T.
17bbb839 162
d20b7173 163 -- if T is a non-union class type, the storage for each non-static
0cbd7506 164 data member and each base-class subobject is zero-initialized.
17bbb839
MM
165
166 -- if T is a union type, the storage for its first data member is
0cbd7506 167 zero-initialized.
17bbb839
MM
168
169 -- if T is an array type, the storage for each element is
0cbd7506 170 zero-initialized.
17bbb839
MM
171
172 -- if T is a reference type, no initialization is performed. */
94e6e4c4 173
50bc768d 174 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
7a1d37e9 175
17bbb839
MM
176 if (type == error_mark_node)
177 ;
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. */
183 ;
b2b1ea34 184 else if (TYPE_PTR_OR_PTRMEM_P (type))
cda0a029 185 init = fold (convert (type, nullptr_node));
b2b1ea34
JJ
186 else if (NULLPTR_TYPE_P (type))
187 init = build_int_cst (type, 0);
b8063b29 188 else if (SCALAR_TYPE_P (type))
550880a3 189 init = build_zero_cst (type);
5621a5d7 190 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
17bbb839
MM
191 {
192 tree field;
9771b263 193 vec<constructor_elt, va_gc> *v = NULL;
17bbb839 194
17bbb839 195 /* Iterate over the fields, building initializations. */
910ad8de 196 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
17bbb839
MM
197 {
198 if (TREE_CODE (field) != FIELD_DECL)
199 continue;
200
7614d42c
JJ
201 if (TREE_TYPE (field) == error_mark_node)
202 continue;
203
e33eba75
JJ
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)
208 {
209 tree bitpos = bit_position (field);
210 if (TREE_CODE (bitpos) == INTEGER_CST
211 && !tree_int_cst_lt (bitpos, field_size))
212 continue;
213 }
214
17bbb839
MM
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. */
32a11c08 219 if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
4038c495 220 {
e33eba75
JJ
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);
b43d1bde
PC
230 if (value)
231 CONSTRUCTOR_APPEND_ELT(v, field, value);
4038c495 232 }
17bbb839
MM
233
234 /* For unions, only the first field is initialized. */
235 if (TREE_CODE (type) == UNION_TYPE)
236 break;
237 }
4038c495 238
0fcedd9c
JM
239 /* Build a constructor to contain the initializations. */
240 init = build_constructor (type, v);
17bbb839
MM
241 }
242 else if (TREE_CODE (type) == ARRAY_TYPE)
94e6e4c4 243 {
17bbb839 244 tree max_index;
9771b263 245 vec<constructor_elt, va_gc> *v = NULL;
17bbb839 246
17bbb839 247 /* Iterate over the array elements, building initializations. */
6b6c8106 248 if (nelts)
2dcdd15d
JJ
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)
7768cadb 254 return NULL_TREE;
6b6c8106
SB
255 else
256 max_index = array_type_nelts (type);
9bdb04a2
AP
257
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;
50bc768d 262 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
7a1d37e9 263
a8e6c82a
MM
264 /* A zero-sized array, which is accepted as an extension, will
265 have an upper bound of -1. */
2dcdd15d 266 if (!integer_minus_onep (max_index))
94763647 267 {
f32682ca 268 constructor_elt ce;
4038c495 269
b01f0d13 270 /* If this is a one element array, we just use a regular init. */
2dcdd15d 271 if (integer_zerop (max_index))
f32682ca 272 ce.index = size_zero_node;
b01f0d13 273 else
f32682ca 274 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
2dcdd15d 275 max_index);
c8094d83 276
2dcdd15d
JJ
277 ce.value = build_zero_init_1 (TREE_TYPE (type), /*nelts=*/NULL_TREE,
278 static_storage_p, NULL_TREE);
f11c7048
JJ
279 if (ce.value)
280 {
281 vec_alloc (v, 1);
282 v->quick_push (ce);
283 }
94763647 284 }
c8094d83 285
4038c495
GB
286 /* Build a constructor to contain the initializations. */
287 init = build_constructor (type, v);
94e6e4c4 288 }
b55b02ea 289 else if (VECTOR_TYPE_P (type))
e8160c9a 290 init = build_zero_cst (type);
94e6e4c4 291 else
a9ed1829 292 gcc_assert (TYPE_REF_P (type));
94e6e4c4 293
17bbb839
MM
294 /* In all cases, the initializer is a constant. */
295 if (init)
51eed280 296 TREE_CONSTANT (init) = 1;
94e6e4c4
AO
297
298 return init;
299}
300
e33eba75
JJ
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. */
312
313tree
314build_zero_init (tree type, tree nelts, bool static_storage_p)
315{
316 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
317}
318
0fcedd9c 319/* Return a suitable initializer for value-initializing an object of type
8f540f06 320 TYPE, as described in [dcl.init]. */
0fcedd9c 321
8f540f06 322tree
309714d4 323build_value_init (tree type, tsubst_flags_t complain)
0fcedd9c
JM
324{
325 /* [dcl.init]
326
327 To value-initialize an object of type T means:
328
eca7fc57
JM
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,
026c3cfd 331 then the object is default-initialized;
0fcedd9c 332
eca7fc57
JM
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;
0fcedd9c
JM
338
339 - if T is an array type, then each element is value-initialized;
340
341 - otherwise, the object is zero-initialized.
342
343 A program that calls for default-initialization or
eca7fc57 344 value-initialization of an entity of reference type is ill-formed. */
0fcedd9c 345
f968ef9b 346 if (CLASS_TYPE_P (type) && type_build_ctor_call (type))
0fcedd9c 347 {
f968ef9b
JJ
348 tree ctor
349 = build_special_member_call (NULL_TREE, complete_ctor_identifier,
350 NULL, type, LOOKUP_NORMAL, complain);
351 if (ctor == error_mark_node || TREE_CONSTANT (ctor))
a710f1f8 352 return ctor;
ce46d604
JM
353 if (processing_template_decl)
354 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
355 return build_min (CAST_EXPR, type, NULL_TREE);
a710f1f8
JM
356 tree fn = NULL_TREE;
357 if (TREE_CODE (ctor) == CALL_EXPR)
358 fn = get_callee_fndecl (ctor);
359 ctor = build_aggr_init_expr (type, ctor);
360 if (fn && user_provided_p (fn))
eca7fc57 361 return ctor;
7b37a0c5 362 else if (TYPE_HAS_COMPLEX_DFLT (type))
8f540f06
JM
363 {
364 /* This is a class that needs constructing, but doesn't have
365 a user-provided constructor. So we need to zero-initialize
366 the object and then call the implicitly defined ctor.
450a927a 367 This will be handled in simplify_aggr_init_expr. */
eca7fc57 368 AGGR_INIT_ZERO_FIRST (ctor) = 1;
8f540f06
JM
369 return ctor;
370 }
fd97a96a 371 }
eca7fc57
JM
372
373 /* Discard any access checking during subobject initialization;
374 the checks are implied by the call to the ctor which we have
375 verified is OK (cpp0x/defaulted46.C). */
376 push_deferring_access_checks (dk_deferred);
377 tree r = build_value_init_noctor (type, complain);
378 pop_deferring_access_checks ();
379 return r;
fd97a96a
JM
380}
381
382/* Like build_value_init, but don't call the constructor for TYPE. Used
383 for base initializers. */
384
385tree
309714d4 386build_value_init_noctor (tree type, tsubst_flags_t complain)
fd97a96a 387{
46a76d4b
JM
388 if (!COMPLETE_TYPE_P (type))
389 {
390 if (complain & tf_error)
391 error ("value-initialization of incomplete type %qT", type);
392 return error_mark_node;
393 }
4ddd8a74
JM
394 /* FIXME the class and array cases should just use digest_init once it is
395 SFINAE-enabled. */
fd97a96a
JM
396 if (CLASS_TYPE_P (type))
397 {
12185846
PC
398 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type)
399 || errorcount != 0);
fd97a96a
JM
400
401 if (TREE_CODE (type) != UNION_TYPE)
0fcedd9c 402 {
8f540f06 403 tree field;
9771b263 404 vec<constructor_elt, va_gc> *v = NULL;
0fcedd9c
JM
405
406 /* Iterate over the fields, building initializations. */
910ad8de 407 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
0fcedd9c
JM
408 {
409 tree ftype, value;
410
411 if (TREE_CODE (field) != FIELD_DECL)
412 continue;
413
414 ftype = TREE_TYPE (field);
415
a95aef3c
PC
416 if (ftype == error_mark_node)
417 continue;
418
71b6cb2b
JJ
419 /* Ignore flexible array members for value initialization. */
420 if (TREE_CODE (ftype) == ARRAY_TYPE
421 && !COMPLETE_TYPE_P (ftype)
422 && !TYPE_DOMAIN (ftype)
423 && COMPLETE_TYPE_P (TREE_TYPE (ftype))
0c7bce0a 424 && (next_aggregate_field (DECL_CHAIN (field))
71b6cb2b
JJ
425 == NULL_TREE))
426 continue;
427
1ab84eda
JJ
428 /* Ignore unnamed zero-width bitfields. */
429 if (DECL_UNNAMED_BIT_FIELD (field)
430 && integer_zerop (DECL_SIZE (field)))
431 continue;
432
0fcedd9c
JM
433 /* We could skip vfields and fields of types with
434 user-defined constructors, but I think that won't improve
435 performance at all; it should be simpler in general just
436 to zero out the entire object than try to only zero the
437 bits that actually need it. */
438
439 /* Note that for class types there will be FIELD_DECLs
440 corresponding to base classes as well. Thus, iterating
441 over TYPE_FIELDs will result in correct initialization of
442 all of the subobjects. */
309714d4 443 value = build_value_init (ftype, complain);
c0014b07 444 value = maybe_constant_init (value);
0fcedd9c 445
351ccf20
JM
446 if (value == error_mark_node)
447 return error_mark_node;
448
c0014b07
JM
449 CONSTRUCTOR_APPEND_ELT(v, field, value);
450
451 /* We shouldn't have gotten here for anything that would need
452 non-trivial initialization, and gimplify_init_ctor_preeval
453 would need to be fixed to allow it. */
454 gcc_assert (TREE_CODE (value) != TARGET_EXPR
455 && TREE_CODE (value) != AGGR_INIT_EXPR);
0fcedd9c
JM
456 }
457
458 /* Build a constructor to contain the zero- initializations. */
8f540f06 459 return build_constructor (type, v);
0fcedd9c
JM
460 }
461 }
462 else if (TREE_CODE (type) == ARRAY_TYPE)
463 {
9771b263 464 vec<constructor_elt, va_gc> *v = NULL;
0fcedd9c
JM
465
466 /* Iterate over the array elements, building initializations. */
467 tree max_index = array_type_nelts (type);
468
469 /* If we have an error_mark here, we should just return error mark
470 as we don't know the size of the array yet. */
471 if (max_index == error_mark_node)
462aa169 472 {
e2a009c7
JM
473 if (complain & tf_error)
474 error ("cannot value-initialize array of unknown bound %qT",
475 type);
462aa169
JM
476 return error_mark_node;
477 }
0fcedd9c
JM
478 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
479
480 /* A zero-sized array, which is accepted as an extension, will
481 have an upper bound of -1. */
482 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
483 {
f32682ca 484 constructor_elt ce;
0fcedd9c 485
0fcedd9c
JM
486 /* If this is a one element array, we just use a regular init. */
487 if (tree_int_cst_equal (size_zero_node, max_index))
f32682ca 488 ce.index = size_zero_node;
0fcedd9c 489 else
f32682ca 490 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
0fcedd9c 491
f32682ca 492 ce.value = build_value_init (TREE_TYPE (type), complain);
c0014b07
JM
493 ce.value = maybe_constant_init (ce.value);
494 if (ce.value == error_mark_node)
495 return error_mark_node;
9dbd4406 496
c0014b07
JM
497 vec_alloc (v, 1);
498 v->quick_push (ce);
351ccf20 499
c0014b07
JM
500 /* We shouldn't have gotten here for anything that would need
501 non-trivial initialization, and gimplify_init_ctor_preeval
502 would need to be fixed to allow it. */
503 gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
504 && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
0fcedd9c
JM
505 }
506
507 /* Build a constructor to contain the initializations. */
508 return build_constructor (type, v);
509 }
2b8497cd
JM
510 else if (TREE_CODE (type) == FUNCTION_TYPE)
511 {
512 if (complain & tf_error)
513 error ("value-initialization of function type %qT", type);
514 return error_mark_node;
515 }
9f613f06 516 else if (TYPE_REF_P (type))
351ccf20
JM
517 {
518 if (complain & tf_error)
519 error ("value-initialization of reference type %qT", type);
520 return error_mark_node;
521 }
0fcedd9c
JM
522
523 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
524}
525
0790c8aa
MP
526/* Initialize current class with INIT, a TREE_LIST of arguments for
527 a target constructor. If TREE_LIST is void_type_node, an empty
528 initializer list was given. Return the target constructor. */
238e471c 529
0790c8aa 530static tree
238e471c
VV
531perform_target_ctor (tree init)
532{
533 tree decl = current_class_ref;
534 tree type = current_class_type;
535
0790c8aa
MP
536 init = build_aggr_init (decl, init, LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
537 tf_warning_or_error);
538 finish_expr_stmt (init);
eca7fc57 539 if (type_build_dtor_call (type))
238e471c 540 {
04e4997a
PC
541 tree expr = build_delete (input_location,
542 type, decl, sfk_complete_destructor,
238e471c
VV
543 LOOKUP_NORMAL
544 |LOOKUP_NONVIRTUAL
545 |LOOKUP_DESTRUCTOR,
546 0, tf_warning_or_error);
32d8ff73
JM
547 if (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
548 {
549 tree base = build_delete (input_location,
550 type, decl, sfk_base_destructor,
551 LOOKUP_NORMAL
552 |LOOKUP_NONVIRTUAL
553 |LOOKUP_DESTRUCTOR,
554 0, tf_warning_or_error);
555 expr = build_if_in_charge (expr, base);
556 }
eca7fc57
JM
557 if (expr != error_mark_node
558 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
238e471c
VV
559 finish_eh_cleanup (expr);
560 }
0790c8aa 561 return init;
238e471c
VV
562}
563
78b3bf0e
JM
564/* Instantiate the default member initializer of MEMBER, if needed.
565 Only get_nsdmi should use the return value of this function. */
b15ea309
JM
566
567tree
78b3bf0e 568maybe_instantiate_nsdmi_init (tree member, tsubst_flags_t complain)
b15ea309 569{
78b3bf0e 570 tree init = DECL_INITIAL (member);
9b40ca25
JM
571
572 /* tsubst_decl uses void_node to indicate an uninstantiated DMI. */
573 if (init == void_node)
f4cd9c51 574 {
04eca83e 575 init = DECL_INITIAL (DECL_TI_TEMPLATE (member));
12659e10 576 location_t expr_loc
6bdfada4 577 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member));
7b49e3da 578 if (TREE_CODE (init) == DEFERRED_PARSE)
9fb82e65 579 /* Unparsed. */;
04eca83e 580 /* Check recursive instantiation. */
9fb82e65 581 else if (DECL_INSTANTIATING_NSDMI_P (member))
04eca83e 582 {
9fb82e65 583 if (complain & tf_error)
12659e10
JM
584 error_at (expr_loc, "recursive instantiation of default member "
585 "initializer for %qD", member);
04eca83e
NS
586 init = error_mark_node;
587 }
588 else
589 {
16e723e6 590 cp_evaluated ev;
12659e10
JM
591
592 location_t sloc = input_location;
593 input_location = expr_loc;
594
04eca83e 595 DECL_INSTANTIATING_NSDMI_P (member) = 1;
9fb82e65 596
298434c9 597 bool pushed = false;
58df5350 598 tree ctx = type_context_for_name_lookup (member);
6173f713 599
07c52d1e 600 bool push_to_top = maybe_push_to_top_level (member);
6173f713 601 if (!currently_open_class (ctx))
298434c9 602 {
82d5dece 603 push_nested_class (ctx);
de03b82f 604 push_deferring_access_checks (dk_no_deferred);
298434c9
JM
605 pushed = true;
606 }
607
82d5dece 608 inject_this_parameter (ctx, TYPE_UNQUALIFIED);
12659e10 609
f44a8dd5
JM
610 start_lambda_scope (member);
611
04eca83e 612 /* Do deferred instantiation of the NSDMI. */
5925f0ec
PP
613 init = tsubst_copy_and_build (init, DECL_TI_ARGS (member),
614 complain, member);
9fb82e65 615 init = digest_nsdmi_init (member, init, complain);
f44a8dd5
JM
616
617 finish_lambda_scope ();
618
04eca83e 619 DECL_INSTANTIATING_NSDMI_P (member) = 0;
12659e10
JM
620
621 if (init != error_mark_node)
9b40ca25 622 DECL_INITIAL (member) = init;
12659e10 623
298434c9
JM
624 if (pushed)
625 {
de03b82f 626 pop_deferring_access_checks ();
298434c9 627 pop_nested_class ();
298434c9 628 }
07c52d1e 629 maybe_pop_from_top_level (push_to_top);
298434c9 630
12659e10 631 input_location = sloc;
04eca83e 632 }
f4cd9c51 633 }
78b3bf0e
JM
634
635 return init;
636}
637
638/* Return the non-static data initializer for FIELD_DECL MEMBER. */
639
640tree
641get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
642{
643 tree save_ccp = current_class_ptr;
644 tree save_ccr = current_class_ref;
645
646 tree init = maybe_instantiate_nsdmi_init (member, complain);
9fb82e65 647
7b49e3da 648 if (init && TREE_CODE (init) == DEFERRED_PARSE)
b15ea309 649 {
9fb82e65 650 if (complain & tf_error)
b15ea309 651 {
9fb82e65
JM
652 error ("default member initializer for %qD required before the end "
653 "of its enclosing class", member);
654 inform (location_of (init), "defined here");
b15ea309 655 DECL_INITIAL (member) = error_mark_node;
b15ea309 656 }
9fb82e65 657 init = error_mark_node;
b15ea309 658 }
9fb82e65 659
12659e10
JM
660 if (in_ctor)
661 {
662 current_class_ptr = save_ccp;
663 current_class_ref = save_ccr;
664 }
665 else
666 {
667 /* Use a PLACEHOLDER_EXPR when we don't have a 'this' parameter to
668 refer to; constexpr evaluation knows what to do with it. */
669 current_class_ref = build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (member));
670 current_class_ptr = build_address (current_class_ref);
671 }
672
cf59c898
PP
673 /* Clear processing_template_decl for sake of break_out_target_exprs;
674 INIT is always non-templated. */
675 processing_template_decl_sentinel ptds;
676
9fb82e65
JM
677 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
678 so the aggregate init code below will see a CONSTRUCTOR. */
679 bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init));
680 if (simple_target)
681 init = TARGET_EXPR_INITIAL (init);
45d14461 682 init = break_out_target_exprs (init, /*loc*/true);
f8c1f29a
MP
683 if (init && TREE_CODE (init) == TARGET_EXPR)
684 /* In a constructor, this expresses the full initialization, prevent
685 perform_member_init from calling another constructor (58162). */
686 TARGET_EXPR_DIRECT_INIT_P (init) = in_ctor;
9fb82e65
JM
687 if (simple_target && TREE_CODE (init) != CONSTRUCTOR)
688 /* Now put it back so C++17 copy elision works. */
689 init = get_target_expr (init);
690
6ffbf87c
JM
691 set_target_expr_eliding (init);
692
b15ea309
JM
693 current_class_ptr = save_ccp;
694 current_class_ref = save_ccr;
695 return init;
696}
697
945c17d8
MS
698/* Diagnose the flexible array MEMBER if its INITializer is non-null
699 and return true if so. Otherwise return false. */
700
a232a1cb 701bool
945c17d8
MS
702maybe_reject_flexarray_init (tree member, tree init)
703{
704 tree type = TREE_TYPE (member);
705
706 if (!init
707 || TREE_CODE (type) != ARRAY_TYPE
708 || TYPE_DOMAIN (type))
709 return false;
710
711 /* Point at the flexible array member declaration if it's initialized
712 in-class, and at the ctor if it's initialized in a ctor member
713 initializer list. */
714 location_t loc;
715 if (DECL_INITIAL (member) == init
a232a1cb 716 || !current_function_decl
945c17d8
MS
717 || DECL_DEFAULTED_FN (current_function_decl))
718 loc = DECL_SOURCE_LOCATION (member);
719 else
720 loc = DECL_SOURCE_LOCATION (current_function_decl);
721
722 error_at (loc, "initializer for flexible array member %q#D", member);
723 return true;
724}
725
04eb9c55
JM
726/* If INIT's value can come from a call to std::initializer_list<T>::begin,
727 return that function. Otherwise, NULL_TREE. */
728
729static tree
730find_list_begin (tree init)
731{
732 STRIP_NOPS (init);
733 while (TREE_CODE (init) == COMPOUND_EXPR)
734 init = TREE_OPERAND (init, 1);
735 STRIP_NOPS (init);
736 if (TREE_CODE (init) == COND_EXPR)
737 {
738 tree left = TREE_OPERAND (init, 1);
739 if (!left)
740 left = TREE_OPERAND (init, 0);
741 left = find_list_begin (left);
742 if (left)
743 return left;
744 return find_list_begin (TREE_OPERAND (init, 2));
745 }
746 if (TREE_CODE (init) == CALL_EXPR)
747 if (tree fn = get_callee_fndecl (init))
748 if (id_equal (DECL_NAME (fn), "begin")
749 && is_std_init_list (DECL_CONTEXT (fn)))
750 return fn;
751 return NULL_TREE;
752}
753
754/* If INIT initializing MEMBER is copying the address of the underlying array
755 of an initializer_list, warn. */
756
757static void
758maybe_warn_list_ctor (tree member, tree init)
759{
760 tree memtype = TREE_TYPE (member);
761 if (!init || !TYPE_PTR_P (memtype)
762 || !is_list_ctor (current_function_decl))
763 return;
764
9b239d05
JW
765 tree parm = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl);
766 parm = TREE_VALUE (parm);
767 tree initlist = non_reference (parm);
768
769 /* Do not warn if the parameter is an lvalue reference to non-const. */
770 if (TYPE_REF_P (parm) && !TYPE_REF_IS_RVALUE (parm)
771 && !CP_TYPE_CONST_P (initlist))
772 return;
773
04eb9c55
JM
774 tree targs = CLASSTYPE_TI_ARGS (initlist);
775 tree elttype = TREE_VEC_ELT (targs, 0);
776
777 if (!same_type_ignoring_top_level_qualifiers_p
778 (TREE_TYPE (memtype), elttype))
779 return;
780
781 tree begin = find_list_begin (init);
782 if (!begin)
783 return;
784
f9d0ca40 785 location_t loc = cp_expr_loc_or_input_loc (init);
04eb9c55
JM
786 warning_at (loc, OPT_Winit_list_lifetime,
787 "initializing %qD from %qE does not extend the lifetime "
788 "of the underlying array", member, begin);
789}
790
0790c8aa
MP
791/* Data structure for find_uninit_fields_r, below. */
792
793struct find_uninit_data {
794 /* The set tracking the yet-uninitialized members. */
795 hash_set<tree> *uninitialized;
796 /* The data member we are currently initializing. It can be either
797 a type (initializing a base class/delegating constructors), or
798 a COMPONENT_REF. */
799 tree member;
800};
801
802/* walk_tree callback that warns about using uninitialized data in
803 a member-initializer-list. */
804
805static tree
806find_uninit_fields_r (tree *tp, int *walk_subtrees, void *data)
807{
808 find_uninit_data *d = static_cast<find_uninit_data *>(data);
809 hash_set<tree> *uninitialized = d->uninitialized;
810 tree init = *tp;
811 const tree_code code = TREE_CODE (init);
812
813 /* No need to look into types or unevaluated operands. */
814 if (TYPE_P (init) || unevaluated_p (code))
815 {
816 *walk_subtrees = false;
817 return NULL_TREE;
818 }
819
820 switch (code)
821 {
822 /* We'd need data flow info to avoid false positives. */
823 case COND_EXPR:
824 case VEC_COND_EXPR:
825 case BIND_EXPR:
826 /* We might see a MODIFY_EXPR in cases like S() : a((b = 42)), c(b) { }
827 where the initializer for 'a' surreptitiously initializes 'b'. Let's
828 not bother with these complicated scenarios in the front end. */
829 case MODIFY_EXPR:
830 /* Don't attempt to handle statement-expressions, either. */
831 case STATEMENT_LIST:
832 uninitialized->empty ();
833 gcc_fallthrough ();
834 /* If we're just taking the address of an object, it doesn't matter
835 whether it's been initialized. */
836 case ADDR_EXPR:
837 *walk_subtrees = false;
838 return NULL_TREE;
839 default:
840 break;
841 }
842
843 /* We'd need data flow info to avoid false positives. */
844 if (truth_value_p (code))
845 goto give_up;
846 /* Attempt to handle a simple a{b}, but no more. */
847 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
848 {
849 if (CONSTRUCTOR_NELTS (init) == 1
850 && !BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (init, 0)->value))
851 init = CONSTRUCTOR_ELT (init, 0)->value;
852 else
853 goto give_up;
854 }
855 /* Warn about uninitialized 'this'. */
856 else if (code == CALL_EXPR)
857 {
858 tree fn = get_callee_fndecl (init);
859 if (fn && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
860 {
861 tree op = CALL_EXPR_ARG (init, 0);
862 if (TREE_CODE (op) == ADDR_EXPR)
863 op = TREE_OPERAND (op, 0);
864 temp_override<tree> ovr (d->member, DECL_ARGUMENTS (fn));
865 cp_walk_tree_without_duplicates (&op, find_uninit_fields_r, data);
866 }
867 /* Functions (whether static or nonstatic member) may have side effects
868 and initialize other members; it's not the front end's job to try to
869 figure it out. But don't give up for constructors: we still want to
870 warn when initializing base classes:
871
872 struct D : public B {
873 int x;
874 D() : B(x) {}
875 };
876
877 so carry on to detect that 'x' is used uninitialized. */
878 if (!fn || !DECL_CONSTRUCTOR_P (fn))
879 goto give_up;
880 }
881
882 /* If we find FIELD in the uninitialized set, we warn. */
883 if (code == COMPONENT_REF)
884 {
885 tree field = TREE_OPERAND (init, 1);
886 tree type = TYPE_P (d->member) ? d->member : TREE_TYPE (d->member);
887
888 /* We're initializing a reference member with itself. */
889 if (TYPE_REF_P (type) && cp_tree_equal (d->member, init))
890 warning_at (EXPR_LOCATION (init), OPT_Winit_self,
891 "%qD is initialized with itself", field);
892 else if (cp_tree_equal (TREE_OPERAND (init, 0), current_class_ref)
893 && uninitialized->contains (field))
894 {
895 if (TYPE_REF_P (TREE_TYPE (field)))
896 warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
897 "reference %qD is not yet bound to a value when used "
898 "here", field);
899 else if (!INDIRECT_TYPE_P (type) || is_this_parameter (d->member))
900 warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
901 "member %qD is used uninitialized", field);
902 *walk_subtrees = false;
903 }
904 }
905
906 return NULL_TREE;
907
908give_up:
909 *walk_subtrees = false;
910 uninitialized->empty ();
911 return integer_zero_node;
912}
913
914/* Wrapper around find_uninit_fields_r above. */
915
916static void
917find_uninit_fields (tree *t, hash_set<tree> *uninitialized, tree member)
918{
919 if (!uninitialized->is_empty ())
920 {
921 find_uninit_data data = { uninitialized, member };
922 cp_walk_tree_without_duplicates (t, find_uninit_fields_r, &data);
923 }
924}
925
2282d28d
MM
926/* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
927 arguments. If TREE_LIST is void_type_node, an empty initializer
0790c8aa
MP
928 list was given; if NULL_TREE no initializer was given. UNINITIALIZED
929 is the hash set that tracks uninitialized fields. */
e92cc029 930
8d08fdba 931static void
0790c8aa 932perform_member_init (tree member, tree init, hash_set<tree> &uninitialized)
8d08fdba
MS
933{
934 tree decl;
935 tree type = TREE_TYPE (member);
2282d28d 936
0e5f8a59
JM
937 /* Use the non-static data member initializer if there was no
938 mem-initializer for this field. */
939 if (init == NULL_TREE)
9fb82e65 940 init = get_nsdmi (member, /*ctor*/true, tf_warning_or_error);
0e5f8a59 941
a9eba00e
PC
942 if (init == error_mark_node)
943 return;
944
2282d28d
MM
945 /* Effective C++ rule 12 requires that all data members be
946 initialized. */
9dbd4406 947 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
c5d75364
MLI
948 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
949 "%qD should be initialized in the member initialization list",
950 member);
2282d28d 951
2282d28d 952 /* Get an lvalue for the data member. */
50ad9642
MM
953 decl = build_class_member_access_expr (current_class_ref, member,
954 /*access_path=*/NULL_TREE,
5ade1ed2
DG
955 /*preserve_reference=*/true,
956 tf_warning_or_error);
2fbfe9b8
MS
957 if (decl == error_mark_node)
958 return;
959
0790c8aa
MP
960 if ((warn_init_self || warn_uninitialized)
961 && init
962 && TREE_CODE (init) == TREE_LIST
c11e39b0
JW
963 && TREE_CHAIN (init) == NULL_TREE)
964 {
965 tree val = TREE_VALUE (init);
0aa359c1
PC
966 /* Handle references. */
967 if (REFERENCE_REF_P (val))
968 val = TREE_OPERAND (val, 0);
c11e39b0
JW
969 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
970 && TREE_OPERAND (val, 0) == current_class_ref)
971 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
0ccb505d 972 OPT_Winit_self, "%qD is initialized with itself",
c11e39b0 973 member);
0790c8aa
MP
974 else
975 find_uninit_fields (&val, &uninitialized, decl);
c11e39b0
JW
976 }
977
59dbb04d
JM
978 if (array_of_unknown_bound_p (type))
979 {
980 maybe_reject_flexarray_init (member, init);
981 return;
982 }
a1c9c9ff 983
753b4679
MP
984 if (init && TREE_CODE (init) == TREE_LIST)
985 {
986 /* A(): a{e} */
987 if (DIRECT_LIST_INIT_P (TREE_VALUE (init)))
988 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
989 tf_warning_or_error);
990 /* We are trying to initialize an array from a ()-list. If we
991 should attempt to do so, conjure up a CONSTRUCTOR. */
992 else if (TREE_CODE (type) == ARRAY_TYPE
993 /* P0960 is a C++20 feature. */
994 && cxx_dialect >= cxx20)
995 init = do_aggregate_paren_init (init, type);
996 else if (!CLASS_TYPE_P (type))
997 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
998 tf_warning_or_error);
999 /* If we're initializing a class from a ()-list, leave the TREE_LIST
1000 alone: we might call an appropriate constructor, or (in C++20)
1001 do aggregate-initialization. */
1002 }
a1c9c9ff 1003
0790c8aa
MP
1004 /* Assume we are initializing the member. */
1005 bool member_initialized_p = true;
1006
9dbd4406
JM
1007 if (init == void_type_node)
1008 {
1009 /* mem() means value-initialization. */
1010 if (TREE_CODE (type) == ARRAY_TYPE)
0f737a30 1011 {
9c69dcea 1012 init = build_vec_init_expr (type, init, tf_warning_or_error);
6ffbf87c 1013 init = cp_build_init_expr (decl, init);
0f737a30
JJ
1014 finish_expr_stmt (init);
1015 }
9dbd4406
JM
1016 else
1017 {
48e5d119
PC
1018 tree value = build_value_init (type, tf_warning_or_error);
1019 if (value == error_mark_node)
1020 return;
6ffbf87c 1021 init = cp_build_init_expr (decl, value);
351ccf20 1022 finish_expr_stmt (init);
9dbd4406 1023 }
9dbd4406 1024 }
6bdb8141
JM
1025 /* Deal with this here, as we will get confused if we try to call the
1026 assignment op for an anonymous union. This can happen in a
1027 synthesized copy constructor. */
9dbd4406 1028 else if (ANON_AGGR_TYPE_P (type))
6bdb8141 1029 {
ff9f1a5d
MM
1030 if (init)
1031 {
6ffbf87c 1032 init = cp_build_init_expr (decl, TREE_VALUE (init));
ff9f1a5d
MM
1033 finish_expr_stmt (init);
1034 }
6bdb8141 1035 }
e2df21bf 1036 else if (init
9f613f06 1037 && (TYPE_REF_P (type)
a1c9c9ff 1038 || (TREE_CODE (init) == CONSTRUCTOR
e2df21bf
JM
1039 && (CP_AGGREGATE_TYPE_P (type)
1040 || is_std_init_list (type)))))
1041 {
1042 /* With references and list-initialization, we need to deal with
1043 extending temporary lifetimes. 12.2p5: "A temporary bound to a
1044 reference member in a constructor’s ctor-initializer (12.6.2)
1045 persists until the constructor exits." */
1046 unsigned i; tree t;
cd9cf97b 1047 releasing_vec cleanups;
a1c9c9ff 1048 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
4794d4b5
JM
1049 {
1050 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1051 && CP_AGGREGATE_TYPE_P (type))
1052 init = reshape_init (type, init, tf_warning_or_error);
1053 init = digest_init (type, init, tf_warning_or_error);
1054 }
e2df21bf
JM
1055 if (init == error_mark_node)
1056 return;
a4dfd0f0 1057 if (is_empty_field (member)
6876b269
JM
1058 && !TREE_SIDE_EFFECTS (init))
1059 /* Don't add trivial initialization of an empty base/field, as they
1060 might not be ordered the way the back-end expects. */
1061 return;
f3fae478
JM
1062 /* A FIELD_DECL doesn't really have a suitable lifetime, but
1063 make_temporary_var_for_ref_to_temp will treat it as automatic and
1064 set_up_extended_ref_temp wants to use the decl in a warning. */
2c6f7927 1065 init = extend_ref_init_temps (member, init, &cleanups);
e2df21bf
JM
1066 if (TREE_CODE (type) == ARRAY_TYPE
1067 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
1068 init = build_vec_init_expr (type, init, tf_warning_or_error);
6ffbf87c 1069 init = cp_build_init_expr (decl, init);
e2df21bf 1070 finish_expr_stmt (init);
9771b263 1071 FOR_EACH_VEC_ELT (*cleanups, i, t)
ad8161e6 1072 push_cleanup (NULL_TREE, t, false);
e2df21bf 1073 }
a0348261
JM
1074 else if (type_build_ctor_call (type)
1075 || (init && CLASS_TYPE_P (strip_array_types (type))))
8d08fdba 1076 {
534ecb17 1077 if (TREE_CODE (type) == ARRAY_TYPE)
8d08fdba 1078 {
534ecb17
JM
1079 if (init == NULL_TREE
1080 || same_type_ignoring_top_level_qualifiers_p (type,
1081 TREE_TYPE (init)))
1082 {
7e9a3ad3
MS
1083 if (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1084 {
1085 /* Initialize the array only if it's not a flexible
1086 array member (i.e., if it has an upper bound). */
1087 init = build_vec_init_expr (type, init, tf_warning_or_error);
6ffbf87c 1088 init = cp_build_init_expr (decl, init);
7e9a3ad3
MS
1089 finish_expr_stmt (init);
1090 }
534ecb17
JM
1091 }
1092 else
1093 error ("invalid initializer for array member %q#D", member);
8d08fdba
MS
1094 }
1095 else
b87d79e6 1096 {
b8bf6ad9
JM
1097 int flags = LOOKUP_NORMAL;
1098 if (DECL_DEFAULTED_FN (current_function_decl))
1099 flags |= LOOKUP_DEFAULTED;
b87d79e6
JM
1100 if (CP_TYPE_CONST_P (type)
1101 && init == NULL_TREE
6132bdd7 1102 && default_init_uninitialized_part (type))
0df9da03
FC
1103 {
1104 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
1105 vtable; still give this diagnostic. */
097f82ec 1106 auto_diagnostic_group d;
0df9da03
FC
1107 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1108 "uninitialized const member in %q#T", type))
1109 inform (DECL_SOURCE_LOCATION (member),
1110 "%q#D should be initialized", member );
1111 }
b8bf6ad9 1112 finish_expr_stmt (build_aggr_init (decl, init, flags,
b87d79e6
JM
1113 tf_warning_or_error));
1114 }
8d08fdba
MS
1115 }
1116 else
1117 {
1118 if (init == NULL_TREE)
1119 {
31d1acec 1120 tree core_type;
8d08fdba 1121 /* member traversal: note it leaves init NULL */
9f613f06 1122 if (TYPE_REF_P (type))
0df9da03 1123 {
097f82ec 1124 auto_diagnostic_group d;
0df9da03
FC
1125 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1126 "uninitialized reference member in %q#T", type))
1127 inform (DECL_SOURCE_LOCATION (member),
1128 "%q#D should be initialized", member);
1129 }
58ec3cc5 1130 else if (CP_TYPE_CONST_P (type))
0df9da03 1131 {
097f82ec 1132 auto_diagnostic_group d;
0df9da03
FC
1133 if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1134 "uninitialized const member in %q#T", type))
1135 inform (DECL_SOURCE_LOCATION (member),
1136 "%q#D should be initialized", member );
1137 }
31d1acec 1138
69f36ba6
JM
1139 core_type = strip_array_types (type);
1140
012e6a1e
JM
1141 if (CLASS_TYPE_P (core_type)
1142 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
1143 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
1144 diagnose_uninitialized_cst_or_ref_member (core_type,
40bb78ad
FC
1145 /*using_new=*/false,
1146 /*complain=*/true);
0790c8aa
MP
1147
1148 /* We left the member uninitialized. */
1149 member_initialized_p = false;
8d08fdba 1150 }
8d08fdba 1151
04eb9c55
JM
1152 maybe_warn_list_ctor (member, init);
1153
a1c9c9ff 1154 if (init)
4f2e1536
MP
1155 finish_expr_stmt (cp_build_modify_expr (input_location, decl,
1156 INIT_EXPR, init,
5ade1ed2 1157 tf_warning_or_error));
8d08fdba 1158 }
eb66be0e 1159
0790c8aa
MP
1160 if (member_initialized_p && warn_uninitialized)
1161 /* This member is now initialized, remove it from the uninitialized
1162 set. */
1163 uninitialized.remove (member);
1164
eca7fc57 1165 if (type_build_dtor_call (type))
b7484fbe 1166 {
de22184b
MS
1167 tree expr;
1168
50ad9642
MM
1169 expr = build_class_member_access_expr (current_class_ref, member,
1170 /*access_path=*/NULL_TREE,
5ade1ed2
DG
1171 /*preserve_reference=*/false,
1172 tf_warning_or_error);
04e4997a
PC
1173 expr = build_delete (input_location,
1174 type, expr, sfk_complete_destructor,
574cfaa4
JM
1175 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
1176 tf_warning_or_error);
b7484fbe 1177
eca7fc57
JM
1178 if (expr != error_mark_node
1179 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
659e5a7a 1180 finish_eh_cleanup (expr);
b7484fbe 1181 }
8d08fdba
MS
1182}
1183
ff9f1a5d
MM
1184/* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
1185 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
1186
c8094d83 1187static tree
a81072c7 1188build_field_list (tree t, tree list, int *uses_unions_or_anon_p)
ff9f1a5d
MM
1189{
1190 tree fields;
1191
1192 /* Note whether or not T is a union. */
1193 if (TREE_CODE (t) == UNION_TYPE)
a81072c7 1194 *uses_unions_or_anon_p = 1;
ff9f1a5d 1195
910ad8de 1196 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
ff9f1a5d 1197 {
535335bf
JM
1198 tree fieldtype;
1199
ff9f1a5d 1200 /* Skip CONST_DECLs for enumeration constants and so forth. */
17bbb839 1201 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
ff9f1a5d 1202 continue;
c8094d83 1203
535335bf 1204 fieldtype = TREE_TYPE (fields);
ff9f1a5d
MM
1205
1206 /* For an anonymous struct or union, we must recursively
1207 consider the fields of the anonymous type. They can be
1208 directly initialized from the constructor. */
535335bf 1209 if (ANON_AGGR_TYPE_P (fieldtype))
ff9f1a5d
MM
1210 {
1211 /* Add this field itself. Synthesized copy constructors
1212 initialize the entire aggregate. */
1213 list = tree_cons (fields, NULL_TREE, list);
1214 /* And now add the fields in the anonymous aggregate. */
a81072c7
JM
1215 list = build_field_list (fieldtype, list, uses_unions_or_anon_p);
1216 *uses_unions_or_anon_p = 1;
ff9f1a5d
MM
1217 }
1218 /* Add this field. */
1219 else if (DECL_NAME (fields))
1220 list = tree_cons (fields, NULL_TREE, list);
1221 }
1222
1223 return list;
1224}
1225
a81072c7
JM
1226/* Return the innermost aggregate scope for FIELD, whether that is
1227 the enclosing class or an anonymous aggregate within it. */
1228
1229static tree
1230innermost_aggr_scope (tree field)
1231{
1232 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1233 return TREE_TYPE (field);
1234 else
1235 return DECL_CONTEXT (field);
1236}
1237
2282d28d
MM
1238/* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
1239 a FIELD_DECL or BINFO in T that needs initialization. The
1240 TREE_VALUE gives the initializer, or list of initializer arguments.
1241
1242 Return a TREE_LIST containing all of the initializations required
1243 for T, in the order in which they should be performed. The output
1244 list has the same format as the input. */
e92cc029 1245
8d08fdba 1246static tree
2282d28d 1247sort_mem_initializers (tree t, tree mem_inits)
8d08fdba 1248{
ff9f1a5d 1249 tree init;
fa743e8c 1250 tree base, binfo, base_binfo;
2282d28d
MM
1251 tree sorted_inits;
1252 tree next_subobject;
9771b263 1253 vec<tree, va_gc> *vbases;
2282d28d 1254 int i;
a81072c7 1255 int uses_unions_or_anon_p = 0;
ff9f1a5d 1256
2282d28d
MM
1257 /* Build up a list of initializations. The TREE_PURPOSE of entry
1258 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
1259 TREE_VALUE will be the constructor arguments, or NULL if no
1260 explicit initialization was provided. */
1261 sorted_inits = NULL_TREE;
c8094d83 1262
2282d28d 1263 /* Process the virtual bases. */
9ba5ff0f 1264 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
9771b263 1265 vec_safe_iterate (vbases, i, &base); i++)
58c42dc2 1266 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
c8094d83 1267
2282d28d 1268 /* Process the direct bases. */
fa743e8c
NS
1269 for (binfo = TYPE_BINFO (t), i = 0;
1270 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1271 if (!BINFO_VIRTUAL_P (base_binfo))
1272 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
1273
2282d28d 1274 /* Process the non-static data members. */
a81072c7 1275 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_or_anon_p);
2282d28d
MM
1276 /* Reverse the entire list of initializations, so that they are in
1277 the order that they will actually be performed. */
1278 sorted_inits = nreverse (sorted_inits);
1279
1280 /* If the user presented the initializers in an order different from
1281 that in which they will actually occur, we issue a warning. Keep
1282 track of the next subobject which can be explicitly initialized
1283 without issuing a warning. */
1284 next_subobject = sorted_inits;
1285
1286 /* Go through the explicit initializers, filling in TREE_PURPOSE in
1287 the SORTED_INITS. */
1288 for (init = mem_inits; init; init = TREE_CHAIN (init))
1289 {
1290 tree subobject;
1291 tree subobject_init;
1292
1293 subobject = TREE_PURPOSE (init);
1294
1295 /* If the explicit initializers are in sorted order, then
c8094d83 1296 SUBOBJECT will be NEXT_SUBOBJECT, or something following
2282d28d 1297 it. */
c8094d83
MS
1298 for (subobject_init = next_subobject;
1299 subobject_init;
2282d28d
MM
1300 subobject_init = TREE_CHAIN (subobject_init))
1301 if (TREE_PURPOSE (subobject_init) == subobject)
ff9f1a5d
MM
1302 break;
1303
2282d28d 1304 /* Issue a warning if the explicit initializer order does not
2cfe82fe 1305 match that which will actually occur.
0cbd7506 1306 ??? Are all these on the correct lines? */
2282d28d 1307 if (warn_reorder && !subobject_init)
ff9f1a5d 1308 {
2282d28d 1309 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
af718670
PC
1310 warning_at (DECL_SOURCE_LOCATION (TREE_PURPOSE (next_subobject)),
1311 OPT_Wreorder, "%qD will be initialized after",
1312 TREE_PURPOSE (next_subobject));
2282d28d 1313 else
b323323f 1314 warning (OPT_Wreorder, "base %qT will be initialized after",
2282d28d
MM
1315 TREE_PURPOSE (next_subobject));
1316 if (TREE_CODE (subobject) == FIELD_DECL)
af718670
PC
1317 warning_at (DECL_SOURCE_LOCATION (subobject),
1318 OPT_Wreorder, " %q#D", subobject);
2282d28d 1319 else
b323323f 1320 warning (OPT_Wreorder, " base %qT", subobject);
c5d75364
MLI
1321 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1322 OPT_Wreorder, " when initialized here");
ff9f1a5d 1323 }
b7484fbe 1324
2282d28d
MM
1325 /* Look again, from the beginning of the list. */
1326 if (!subobject_init)
ff9f1a5d 1327 {
2282d28d
MM
1328 subobject_init = sorted_inits;
1329 while (TREE_PURPOSE (subobject_init) != subobject)
1330 subobject_init = TREE_CHAIN (subobject_init);
ff9f1a5d 1331 }
c8094d83 1332
2282d28d
MM
1333 /* It is invalid to initialize the same subobject more than
1334 once. */
1335 if (TREE_VALUE (subobject_init))
ff9f1a5d 1336 {
2282d28d 1337 if (TREE_CODE (subobject) == FIELD_DECL)
c5d75364
MLI
1338 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1339 "multiple initializations given for %qD",
1340 subobject);
2282d28d 1341 else
c5d75364
MLI
1342 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1343 "multiple initializations given for base %qT",
1344 subobject);
ff9f1a5d
MM
1345 }
1346
2282d28d
MM
1347 /* Record the initialization. */
1348 TREE_VALUE (subobject_init) = TREE_VALUE (init);
843710c0
PP
1349 /* Carry over the dummy TREE_TYPE node containing the source location. */
1350 TREE_TYPE (subobject_init) = TREE_TYPE (init);
2282d28d 1351 next_subobject = subobject_init;
ff9f1a5d
MM
1352 }
1353
1354 /* [class.base.init]
b7484fbe 1355
ff9f1a5d
MM
1356 If a ctor-initializer specifies more than one mem-initializer for
1357 multiple members of the same union (including members of
57ece258
JM
1358 anonymous unions), the ctor-initializer is ill-formed.
1359
1360 Here we also splice out uninitialized union members. */
a81072c7 1361 if (uses_unions_or_anon_p)
ff9f1a5d 1362 {
3a6a88c8 1363 tree *last_p = NULL;
57ece258
JM
1364 tree *p;
1365 for (p = &sorted_inits; *p; )
8d08fdba 1366 {
ff9f1a5d 1367 tree field;
535335bf 1368 tree ctx;
ff9f1a5d 1369
57ece258
JM
1370 init = *p;
1371
1372 field = TREE_PURPOSE (init);
1373
1374 /* Skip base classes. */
1375 if (TREE_CODE (field) != FIELD_DECL)
1376 goto next;
1377
a81072c7 1378 /* If this is an anonymous aggregate with no explicit initializer,
57ece258 1379 splice it out. */
a81072c7 1380 if (!TREE_VALUE (init) && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
57ece258
JM
1381 goto splice;
1382
ff9f1a5d
MM
1383 /* See if this field is a member of a union, or a member of a
1384 structure contained in a union, etc. */
a81072c7
JM
1385 ctx = innermost_aggr_scope (field);
1386
ff9f1a5d 1387 /* If this field is not a member of a union, skip it. */
a81072c7
JM
1388 if (TREE_CODE (ctx) != UNION_TYPE
1389 && !ANON_AGGR_TYPE_P (ctx))
57ece258
JM
1390 goto next;
1391
3a6a88c8
JM
1392 /* If this union member has no explicit initializer and no NSDMI,
1393 splice it out. */
1394 if (TREE_VALUE (init) || DECL_INITIAL (field))
1395 /* OK. */;
1396 else
57ece258 1397 goto splice;
8d08fdba 1398
ff9f1a5d
MM
1399 /* It's only an error if we have two initializers for the same
1400 union type. */
3a6a88c8 1401 if (!last_p)
6bdb8141 1402 {
3a6a88c8 1403 last_p = p;
57ece258 1404 goto next;
6bdb8141 1405 }
8d08fdba 1406
ff9f1a5d 1407 /* See if LAST_FIELD and the field initialized by INIT are
a81072c7
JM
1408 members of the same union (or the union itself). If so, there's
1409 a problem, unless they're actually members of the same structure
ff9f1a5d 1410 which is itself a member of a union. For example, given:
8d08fdba 1411
ff9f1a5d
MM
1412 union { struct { int i; int j; }; };
1413
1414 initializing both `i' and `j' makes sense. */
a81072c7
JM
1415 ctx = common_enclosing_class
1416 (innermost_aggr_scope (field),
1417 innermost_aggr_scope (TREE_PURPOSE (*last_p)));
3a6a88c8 1418
a81072c7
JM
1419 if (ctx && (TREE_CODE (ctx) == UNION_TYPE
1420 || ctx == TREE_TYPE (TREE_PURPOSE (*last_p))))
8d08fdba 1421 {
3a6a88c8
JM
1422 /* A mem-initializer hides an NSDMI. */
1423 if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
1424 *last_p = TREE_CHAIN (*last_p);
1425 else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
1426 goto splice;
1427 else
2ef7251f
MP
1428 {
1429 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1430 "initializations for multiple members of %qT",
1431 ctx);
1432 goto splice;
1433 }
8d08fdba 1434 }
ff9f1a5d 1435
3a6a88c8 1436 last_p = p;
57ece258
JM
1437
1438 next:
1439 p = &TREE_CHAIN (*p);
1440 continue;
1441 splice:
1442 *p = TREE_CHAIN (*p);
b7484fbe
MS
1443 }
1444 }
8d08fdba 1445
2282d28d 1446 return sorted_inits;
b7484fbe
MS
1447}
1448
50bea0c5
JJ
1449/* Callback for cp_walk_tree to mark all PARM_DECLs in a tree as read. */
1450
1451static tree
1452mark_exp_read_r (tree *tp, int *, void *)
1453{
1454 tree t = *tp;
1455 if (TREE_CODE (t) == PARM_DECL)
1456 mark_exp_read (t);
1457 return NULL_TREE;
1458}
1459
2282d28d
MM
1460/* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1461 is a TREE_LIST giving the explicit mem-initializer-list for the
1462 constructor. The TREE_PURPOSE of each entry is a subobject (a
1463 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1464 is a TREE_LIST giving the arguments to the constructor or
1465 void_type_node for an empty list of arguments. */
a9aedbc2 1466
3dbc07b6 1467void
2282d28d 1468emit_mem_initializers (tree mem_inits)
8d08fdba 1469{
b8bf6ad9
JM
1470 int flags = LOOKUP_NORMAL;
1471
72e4661a
PC
1472 /* We will already have issued an error message about the fact that
1473 the type is incomplete. */
1474 if (!COMPLETE_TYPE_P (current_class_type))
1475 return;
c8094d83 1476
0790c8aa
MP
1477 /* Keep a set holding fields that are not initialized. */
1478 hash_set<tree> uninitialized;
1479
1480 /* Initially that is all of them. */
1481 if (warn_uninitialized)
0c7bce0a 1482 for (tree f = next_aggregate_field (TYPE_FIELDS (current_class_type));
0790c8aa 1483 f != NULL_TREE;
0c7bce0a 1484 f = next_aggregate_field (DECL_CHAIN (f)))
4b1d3d8d
MP
1485 if (!DECL_ARTIFICIAL (f)
1486 && !is_really_empty_class (TREE_TYPE (f), /*ignore_vptr*/false))
0790c8aa
MP
1487 uninitialized.add (f);
1488
238e471c
VV
1489 if (mem_inits
1490 && TYPE_P (TREE_PURPOSE (mem_inits))
1491 && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1492 {
1493 /* Delegating constructor. */
1494 gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
0790c8aa
MP
1495 tree ctor = perform_target_ctor (TREE_VALUE (mem_inits));
1496 find_uninit_fields (&ctor, &uninitialized, current_class_type);
238e471c
VV
1497 return;
1498 }
1499
85b5d65a 1500 if (DECL_DEFAULTED_FN (current_function_decl)
31f7f784 1501 && ! DECL_INHERITED_CTOR (current_function_decl))
b8bf6ad9
JM
1502 flags |= LOOKUP_DEFAULTED;
1503
2282d28d
MM
1504 /* Sort the mem-initializers into the order in which the
1505 initializations should be performed. */
1506 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
8d08fdba 1507
1f5a253a 1508 in_base_initializer = 1;
c8094d83 1509
2282d28d 1510 /* Initialize base classes. */
3b616f08
PC
1511 for (; (mem_inits
1512 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
1513 mem_inits = TREE_CHAIN (mem_inits))
8d08fdba 1514 {
2282d28d
MM
1515 tree subobject = TREE_PURPOSE (mem_inits);
1516 tree arguments = TREE_VALUE (mem_inits);
1517
3b616f08
PC
1518 /* We already have issued an error message. */
1519 if (arguments == error_mark_node)
1520 continue;
1521
31f7f784
JM
1522 /* Suppress access control when calling the inherited ctor. */
1523 bool inherited_base = (DECL_INHERITED_CTOR (current_function_decl)
1524 && flag_new_inheriting_ctors
1525 && arguments);
1526 if (inherited_base)
1527 push_deferring_access_checks (dk_deferred);
1528
91ea6df3
GDR
1529 if (arguments == NULL_TREE)
1530 {
1531 /* If these initializations are taking place in a copy constructor,
1532 the base class should probably be explicitly initialized if there
1533 is a user-defined constructor in the base class (other than the
1534 default constructor, which will be called anyway). */
1535 if (extra_warnings
1536 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1537 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1538 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1539 OPT_Wextra, "base class %q#T should be explicitly "
1540 "initialized in the copy constructor",
1541 BINFO_TYPE (subobject));
91ea6df3 1542 }
2282d28d 1543
2282d28d 1544 /* Initialize the base. */
45e2bf2e 1545 if (!BINFO_VIRTUAL_P (subobject))
b7484fbe 1546 {
2282d28d 1547 tree base_addr;
c8094d83 1548
2282d28d 1549 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
a271590a 1550 subobject, 1, tf_warning_or_error);
2282d28d 1551 expand_aggr_init_1 (subobject, NULL_TREE,
04757a2a 1552 cp_build_fold_indirect_ref (base_addr),
2282d28d 1553 arguments,
b8bf6ad9 1554 flags,
5ade1ed2 1555 tf_warning_or_error);
2282d28d 1556 expand_cleanup_for_base (subobject, NULL_TREE);
0790c8aa
MP
1557 if (STATEMENT_LIST_TAIL (cur_stmt_list))
1558 find_uninit_fields (&STATEMENT_LIST_TAIL (cur_stmt_list)->stmt,
1559 &uninitialized, BINFO_TYPE (subobject));
8d08fdba 1560 }
45e2bf2e
NS
1561 else if (!ABSTRACT_CLASS_TYPE_P (current_class_type))
1562 /* C++14 DR1658 Means we do not have to construct vbases of
1563 abstract classes. */
1564 construct_virtual_base (subobject, arguments);
e3e9e8ca
JJ
1565 else
1566 /* When not constructing vbases of abstract classes, at least mark
1567 the arguments expressions as read to avoid
1568 -Wunused-but-set-parameter false positives. */
50bea0c5 1569 cp_walk_tree (&arguments, mark_exp_read_r, NULL, NULL);
31f7f784
JM
1570
1571 if (inherited_base)
1572 pop_deferring_access_checks ();
8d08fdba 1573 }
1f5a253a 1574 in_base_initializer = 0;
8d08fdba 1575
2282d28d 1576 /* Initialize the vptrs. */
cf2e003b 1577 initialize_vtbl_ptrs (current_class_ptr);
c8094d83 1578
2282d28d
MM
1579 /* Initialize the data members. */
1580 while (mem_inits)
8d08fdba 1581 {
843710c0
PP
1582 /* If this initializer was explicitly provided, then the dummy TREE_TYPE
1583 node contains the source location. */
1584 iloc_sentinel ils (EXPR_LOCATION (TREE_TYPE (mem_inits)));
1585
2282d28d 1586 perform_member_init (TREE_PURPOSE (mem_inits),
0790c8aa
MP
1587 TREE_VALUE (mem_inits),
1588 uninitialized);
1589
2282d28d 1590 mem_inits = TREE_CHAIN (mem_inits);
b7484fbe 1591 }
8d08fdba
MS
1592}
1593
3ec6bad3
MM
1594/* Returns the address of the vtable (i.e., the value that should be
1595 assigned to the vptr) for BINFO. */
1596
2077db1b 1597tree
362efdc1 1598build_vtbl_address (tree binfo)
3ec6bad3 1599{
9965d119 1600 tree binfo_for = binfo;
3ec6bad3
MM
1601 tree vtbl;
1602
fc6633e0 1603 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
9965d119
NS
1604 /* If this is a virtual primary base, then the vtable we want to store
1605 is that for the base this is being used as the primary base of. We
1606 can't simply skip the initialization, because we may be expanding the
1607 inits of a subobject constructor where the virtual base layout
1608 can be different. */
fc6633e0
NS
1609 while (BINFO_PRIMARY_P (binfo_for))
1610 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
9965d119 1611
3ec6bad3
MM
1612 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1613 used. */
9965d119 1614 vtbl = get_vtbl_decl_for_binfo (binfo_for);
c3439626 1615 TREE_USED (vtbl) = true;
3ec6bad3
MM
1616
1617 /* Now compute the address to use when initializing the vptr. */
6de9cd9a 1618 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
5a6ccc94 1619 if (VAR_P (vtbl))
6de9cd9a 1620 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
3ec6bad3
MM
1621
1622 return vtbl;
1623}
1624
8d08fdba
MS
1625/* This code sets up the virtual function tables appropriate for
1626 the pointer DECL. It is a one-ply initialization.
1627
1628 BINFO is the exact type that DECL is supposed to be. In
1629 multiple inheritance, this might mean "C's A" if C : A, B. */
e92cc029 1630
8926095f 1631static void
362efdc1 1632expand_virtual_init (tree binfo, tree decl)
8d08fdba 1633{
8d08fdba 1634 tree vtbl, vtbl_ptr;
3ec6bad3 1635 tree vtt_index;
8d08fdba 1636
3ec6bad3
MM
1637 /* Compute the initializer for vptr. */
1638 vtbl = build_vtbl_address (binfo);
1639
3461fba7
NS
1640 /* We may get this vptr from a VTT, if this is a subobject
1641 constructor or subobject destructor. */
3ec6bad3
MM
1642 vtt_index = BINFO_VPTR_INDEX (binfo);
1643 if (vtt_index)
1644 {
1645 tree vtbl2;
1646 tree vtt_parm;
1647
1648 /* Compute the value to use, when there's a VTT. */
e0fff4b3 1649 vtt_parm = current_vtt_parm;
5d49b6a7 1650 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
04757a2a 1651 vtbl2 = cp_build_fold_indirect_ref (vtbl2);
6de9cd9a 1652 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
3ec6bad3
MM
1653
1654 /* The actual initializer is the VTT value only in the subobject
1655 constructor. In maybe_clone_body we'll substitute NULL for
1656 the vtt_parm in the case of the non-subobject constructor. */
eb0dbdc7 1657 vtbl = build_if_in_charge (vtbl, vtbl2);
3ec6bad3 1658 }
70ae3201
MM
1659
1660 /* Compute the location of the vtpr. */
04757a2a 1661 vtbl_ptr = build_vfield_ref (cp_build_fold_indirect_ref (decl),
338d90b8 1662 TREE_TYPE (binfo));
50bc768d 1663 gcc_assert (vtbl_ptr != error_mark_node);
8d08fdba 1664
70ae3201 1665 /* Assign the vtable to the vptr. */
4b978f96 1666 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
4f2e1536
MP
1667 finish_expr_stmt (cp_build_modify_expr (input_location, vtbl_ptr, NOP_EXPR,
1668 vtbl, tf_warning_or_error));
8d08fdba
MS
1669}
1670
f33e32a8
MM
1671/* If an exception is thrown in a constructor, those base classes already
1672 constructed must be destroyed. This function creates the cleanup
0b8a1e58 1673 for BINFO, which has just been constructed. If FLAG is non-NULL,
838dfd8a 1674 it is a DECL which is nonzero when this base needs to be
0b8a1e58 1675 destroyed. */
f33e32a8
MM
1676
1677static void
362efdc1 1678expand_cleanup_for_base (tree binfo, tree flag)
f33e32a8
MM
1679{
1680 tree expr;
1681
eca7fc57 1682 if (!type_build_dtor_call (BINFO_TYPE (binfo)))
f33e32a8
MM
1683 return;
1684
0b8a1e58 1685 /* Call the destructor. */
c8094d83 1686 expr = build_special_member_call (current_class_ref,
4ba126e4 1687 base_dtor_identifier,
c166b898 1688 NULL,
4ba126e4 1689 binfo,
5ade1ed2
DG
1690 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1691 tf_warning_or_error);
eca7fc57
JM
1692
1693 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1694 return;
1695
0b8a1e58 1696 if (flag)
db3927fb
AH
1697 expr = fold_build3_loc (input_location,
1698 COND_EXPR, void_type_node,
ba47d38d 1699 c_common_truthvalue_conversion (input_location, flag),
7866705a 1700 expr, integer_zero_node);
0b8a1e58 1701
659e5a7a 1702 finish_eh_cleanup (expr);
f33e32a8
MM
1703}
1704
2282d28d
MM
1705/* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1706 constructor. */
e92cc029 1707
8d08fdba 1708static void
2282d28d 1709construct_virtual_base (tree vbase, tree arguments)
8d08fdba 1710{
2282d28d 1711 tree inner_if_stmt;
2282d28d 1712 tree exp;
c8094d83 1713 tree flag;
2282d28d
MM
1714
1715 /* If there are virtual base classes with destructors, we need to
1716 emit cleanups to destroy them if an exception is thrown during
1717 the construction process. These exception regions (i.e., the
1718 period during which the cleanups must occur) begin from the time
1719 the construction is complete to the end of the function. If we
1720 create a conditional block in which to initialize the
1721 base-classes, then the cleanup region for the virtual base begins
1722 inside a block, and ends outside of that block. This situation
1723 confuses the sjlj exception-handling code. Therefore, we do not
1724 create a single conditional block, but one for each
1725 initialization. (That way the cleanup regions always begin
3b426391 1726 in the outer block.) We trust the back end to figure out
2282d28d
MM
1727 that the FLAG will not change across initializations, and
1728 avoid doing multiple tests. */
910ad8de 1729 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
2282d28d
MM
1730 inner_if_stmt = begin_if_stmt ();
1731 finish_if_stmt_cond (flag, inner_if_stmt);
2282d28d
MM
1732
1733 /* Compute the location of the virtual base. If we're
1734 constructing virtual bases, then we must be the most derived
1735 class. Therefore, we don't have to look up the virtual base;
1736 we already know where it is. */
22ed7e5f
MM
1737 exp = convert_to_base_statically (current_class_ref, vbase);
1738
c8094d83 1739 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
4b978f96 1740 0, tf_warning_or_error);
2282d28d 1741 finish_then_clause (inner_if_stmt);
325c3691 1742 finish_if_stmt (inner_if_stmt);
2282d28d
MM
1743
1744 expand_cleanup_for_base (vbase, flag);
8d08fdba
MS
1745}
1746
2ee887f2 1747/* Find the context in which this FIELD can be initialized. */
e92cc029 1748
2ee887f2 1749static tree
362efdc1 1750initializing_context (tree field)
2ee887f2
MS
1751{
1752 tree t = DECL_CONTEXT (field);
1753
1754 /* Anonymous union members can be initialized in the first enclosing
1755 non-anonymous union context. */
6bdb8141 1756 while (t && ANON_AGGR_TYPE_P (t))
2ee887f2
MS
1757 t = TYPE_CONTEXT (t);
1758 return t;
1759}
1760
8d08fdba
MS
1761/* Function to give error message if member initialization specification
1762 is erroneous. FIELD is the member we decided to initialize.
1763 TYPE is the type for which the initialization is being performed.
72b7eeff 1764 FIELD must be a member of TYPE.
c8094d83 1765
8d08fdba
MS
1766 MEMBER_NAME is the name of the member. */
1767
1768static int
362efdc1 1769member_init_ok_or_else (tree field, tree type, tree member_name)
8d08fdba
MS
1770{
1771 if (field == error_mark_node)
1772 return 0;
a723baf1 1773 if (!field)
8d08fdba 1774 {
15a7ee29 1775 error ("class %qT does not have any field named %qD", type,
a723baf1 1776 member_name);
8d08fdba
MS
1777 return 0;
1778 }
5a6ccc94 1779 if (VAR_P (field))
b7484fbe 1780 {
15a7ee29 1781 error ("%q#D is a static data member; it can only be "
a723baf1
MM
1782 "initialized at its definition",
1783 field);
1784 return 0;
1785 }
1786 if (TREE_CODE (field) != FIELD_DECL)
1787 {
15a7ee29 1788 error ("%q#D is not a non-static data member of %qT",
a723baf1
MM
1789 field, type);
1790 return 0;
1791 }
1792 if (initializing_context (field) != type)
1793 {
15a7ee29 1794 error ("class %qT does not have any field named %qD", type,
a723baf1 1795 member_name);
b7484fbe
MS
1796 return 0;
1797 }
1798
8d08fdba
MS
1799 return 1;
1800}
1801
2282d28d
MM
1802/* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1803 is a _TYPE node or TYPE_DECL which names a base for that type.
1f5a253a
NS
1804 Check the validity of NAME, and return either the base _TYPE, base
1805 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
2282d28d 1806 NULL_TREE and issue a diagnostic.
8d08fdba 1807
36a68fe7
NS
1808 An old style unnamed direct single base construction is permitted,
1809 where NAME is NULL. */
8d08fdba 1810
fd74ca0b 1811tree
1f5a253a 1812expand_member_init (tree name)
8d08fdba 1813{
2282d28d
MM
1814 tree basetype;
1815 tree field;
8d08fdba 1816
2282d28d 1817 if (!current_class_ref)
fd74ca0b 1818 return NULL_TREE;
8d08fdba 1819
36a68fe7 1820 if (!name)
90418208 1821 {
36a68fe7
NS
1822 /* This is an obsolete unnamed base class initializer. The
1823 parser will already have warned about its use. */
604a3205 1824 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
36a68fe7
NS
1825 {
1826 case 0:
15a7ee29 1827 error ("unnamed initializer for %qT, which has no base classes",
2282d28d 1828 current_class_type);
36a68fe7
NS
1829 return NULL_TREE;
1830 case 1:
604a3205
NS
1831 basetype = BINFO_TYPE
1832 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
36a68fe7
NS
1833 break;
1834 default:
15a7ee29 1835 error ("unnamed initializer for %qT, which uses multiple inheritance",
2282d28d 1836 current_class_type);
36a68fe7
NS
1837 return NULL_TREE;
1838 }
90418208 1839 }
36a68fe7 1840 else if (TYPE_P (name))
be99da77 1841 {
a82d6da5 1842 basetype = TYPE_MAIN_VARIANT (name);
36a68fe7 1843 name = TYPE_NAME (name);
be99da77 1844 }
36a68fe7
NS
1845 else if (TREE_CODE (name) == TYPE_DECL)
1846 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
2282d28d
MM
1847 else
1848 basetype = NULL_TREE;
8d08fdba 1849
36a68fe7 1850 if (basetype)
41efda8f 1851 {
d9148cf4
MM
1852 tree class_binfo;
1853 tree direct_binfo;
1854 tree virtual_binfo;
1855 int i;
2282d28d 1856
5cd5a78c
JM
1857 if (current_template_parms
1858 || same_type_p (basetype, current_class_type))
238e471c 1859 return basetype;
2282d28d 1860
d9148cf4
MM
1861 class_binfo = TYPE_BINFO (current_class_type);
1862 direct_binfo = NULL_TREE;
1863 virtual_binfo = NULL_TREE;
1864
1865 /* Look for a direct base. */
fa743e8c 1866 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
539ed333 1867 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
fa743e8c
NS
1868 break;
1869
d9148cf4
MM
1870 /* Look for a virtual base -- unless the direct base is itself
1871 virtual. */
809e3e7f 1872 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
58c42dc2 1873 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
d9148cf4
MM
1874
1875 /* [class.base.init]
c8094d83 1876
0cbd7506 1877 If a mem-initializer-id is ambiguous because it designates
d9148cf4
MM
1878 both a direct non-virtual base class and an inherited virtual
1879 base class, the mem-initializer is ill-formed. */
1880 if (direct_binfo && virtual_binfo)
1881 {
15a7ee29 1882 error ("%qD is both a direct base and an indirect virtual base",
d9148cf4
MM
1883 basetype);
1884 return NULL_TREE;
1885 }
1886
1887 if (!direct_binfo && !virtual_binfo)
8d08fdba 1888 {
5775a06a 1889 if (CLASSTYPE_VBASECLASSES (current_class_type))
c3115fd2
MM
1890 error ("type %qT is not a direct or virtual base of %qT",
1891 basetype, current_class_type);
41efda8f 1892 else
c3115fd2
MM
1893 error ("type %qT is not a direct base of %qT",
1894 basetype, current_class_type);
fd74ca0b 1895 return NULL_TREE;
41efda8f 1896 }
d9148cf4
MM
1897
1898 return direct_binfo ? direct_binfo : virtual_binfo;
41efda8f
MM
1899 }
1900 else
1901 {
9dc6f476 1902 if (identifier_p (name))
86ac0575 1903 field = lookup_field (current_class_type, name, 1, false);
2282d28d
MM
1904 else
1905 field = name;
8d08fdba 1906
2282d28d 1907 if (member_init_ok_or_else (field, current_class_type, name))
1f5a253a 1908 return field;
41efda8f 1909 }
fd74ca0b 1910
2282d28d 1911 return NULL_TREE;
8d08fdba
MS
1912}
1913
1914/* This is like `expand_member_init', only it stores one aggregate
1915 value into another.
1916
1917 INIT comes in two flavors: it is either a value which
1918 is to be stored in EXP, or it is a parameter list
1919 to go to a constructor, which will operate on EXP.
f30432d7
MS
1920 If INIT is not a parameter list for a constructor, then set
1921 LOOKUP_ONLYCONVERTING.
6060a796
MS
1922 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1923 the initializer, if FLAGS is 0, then it is the (init) form.
8d08fdba 1924 If `init' is a CONSTRUCTOR, then we emit a warning message,
59be0cdd 1925 explaining that such initializations are invalid.
8d08fdba 1926
8d08fdba
MS
1927 If INIT resolves to a CALL_EXPR which happens to return
1928 something of the type we are looking for, then we know
1929 that we can safely use that call to perform the
1930 initialization.
1931
1932 The virtual function table pointer cannot be set up here, because
1933 we do not really know its type.
1934
8d08fdba
MS
1935 This never calls operator=().
1936
1937 When initializing, nothing is CONST.
1938
1939 A default copy constructor may have to be used to perform the
1940 initialization.
1941
1942 A constructor or a conversion operator may have to be used to
e92cc029 1943 perform the initialization, but not both, as it would be ambiguous. */
8d08fdba 1944
f1dedc31 1945tree
5ade1ed2 1946build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
8d08fdba 1947{
f1dedc31
MM
1948 tree stmt_expr;
1949 tree compound_stmt;
1950 int destroy_temps;
8d08fdba
MS
1951 tree type = TREE_TYPE (exp);
1952 int was_const = TREE_READONLY (exp);
f30432d7 1953 int was_volatile = TREE_THIS_VOLATILE (exp);
2a3398e1 1954 int is_global;
8d08fdba
MS
1955
1956 if (init == error_mark_node)
f1dedc31 1957 return error_mark_node;
8d08fdba 1958
d1a73b0b 1959 location_t init_loc = (init
f9d0ca40 1960 ? cp_expr_loc_or_input_loc (init)
d1a73b0b
JM
1961 : location_of (exp));
1962
8d08fdba 1963 TREE_READONLY (exp) = 0;
f30432d7
MS
1964 TREE_THIS_VOLATILE (exp) = 0;
1965
8d08fdba
MS
1966 if (TREE_CODE (type) == ARRAY_TYPE)
1967 {
70f40fea
JJ
1968 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1969 int from_array = 0;
671cb993 1970
70f40fea 1971 if (VAR_P (exp) && DECL_DECOMPOSITION_P (exp))
0655c6d5
JM
1972 {
1973 from_array = 1;
69ce0c8c 1974 init = mark_rvalue_use (init);
dfd7fdca
DM
1975 if (init
1976 && DECL_P (tree_strip_any_location_wrapper (init))
0655c6d5
JM
1977 && !(flags & LOOKUP_ONLYCONVERTING))
1978 {
1979 /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init
1980 recognizes it as direct-initialization. */
1981 init = build_constructor_single (init_list_type_node,
1982 NULL_TREE, init);
1983 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
1984 }
1985 }
70f40fea 1986 else
8d08fdba 1987 {
70f40fea
JJ
1988 /* Must arrange to initialize each element of EXP
1989 from elements of INIT. */
1990 if (cv_qualified_p (type))
1991 TREE_TYPE (exp) = cv_unqualified (type);
1992 if (itype && cv_qualified_p (itype))
1993 TREE_TYPE (init) = cv_unqualified (itype);
1994 from_array = (itype && same_type_p (TREE_TYPE (init),
1995 TREE_TYPE (exp)));
d1a73b0b 1996
e278212e
PC
1997 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)
1998 && (!from_array
1999 || (TREE_CODE (init) != CONSTRUCTOR
2000 /* Can happen, eg, handling the compound-literals
2001 extension (ext/complit12.C). */
2002 && TREE_CODE (init) != TARGET_EXPR)))
d1a73b0b
JM
2003 {
2004 if (complain & tf_error)
e278212e
PC
2005 error_at (init_loc, "array must be initialized "
2006 "with a brace-enclosed initializer");
2007 return error_mark_node;
d1a73b0b 2008 }
8d08fdba 2009 }
70f40fea 2010
a48cccea 2011 stmt_expr = build_vec_init (exp, NULL_TREE, init,
844ae01d 2012 /*explicit_value_init_p=*/false,
70f40fea 2013 from_array,
5ade1ed2 2014 complain);
8d08fdba 2015 TREE_READONLY (exp) = was_const;
f30432d7 2016 TREE_THIS_VOLATILE (exp) = was_volatile;
8d08fdba 2017 TREE_TYPE (exp) = type;
a47c2f62
JM
2018 /* Restore the type of init unless it was used directly. */
2019 if (init && TREE_CODE (stmt_expr) != INIT_EXPR)
f376e137 2020 TREE_TYPE (init) = itype;
f1dedc31 2021 return stmt_expr;
8d08fdba
MS
2022 }
2023
e32869a1 2024 if (is_copy_initialization (init))
0655c6d5
JM
2025 flags |= LOOKUP_ONLYCONVERTING;
2026
2a3398e1 2027 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
f2c5f623 2028 destroy_temps = stmts_are_full_exprs_p ();
ae499cce 2029 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
b9119edc
PP
2030 bool ok = expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
2031 init, LOOKUP_NORMAL|flags, complain);
2a3398e1 2032 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
ae499cce 2033 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
8d08fdba 2034 TREE_READONLY (exp) = was_const;
f30432d7 2035 TREE_THIS_VOLATILE (exp) = was_volatile;
b9119edc
PP
2036 if (!ok)
2037 return error_mark_node;
f1dedc31 2038
b46b715d
JM
2039 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
2040 && TREE_SIDE_EFFECTS (stmt_expr)
2041 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
2042 /* Just know that we've seen something for this node. */
2043 TREE_USED (exp) = 1;
2044
f1dedc31 2045 return stmt_expr;
8d08fdba
MS
2046}
2047
b9119edc 2048static bool
5ade1ed2
DG
2049expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
2050 tsubst_flags_t complain)
8d08fdba 2051{
fc378698
MS
2052 tree type = TREE_TYPE (exp);
2053
8d08fdba
MS
2054 /* It fails because there may not be a constructor which takes
2055 its own type as the first (or only parameter), but which does
2056 take other types via a conversion. So, if the thing initializing
2057 the expression is a unit element of type X, first try X(X&),
2058 followed by initialization by X. If neither of these work
2059 out, then look hard. */
2060 tree rval;
9771b263 2061 vec<tree, va_gc> *parms;
8d08fdba 2062
2061820e
JM
2063 /* If we have direct-initialization from an initializer list, pull
2064 it out of the TREE_LIST so the code below can see it. */
2065 if (init && TREE_CODE (init) == TREE_LIST
014397c2 2066 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2061820e
JM
2067 {
2068 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
2069 && TREE_CHAIN (init) == NULL_TREE);
2070 init = TREE_VALUE (init);
f0516ca4
JJ
2071 /* Only call reshape_init if it has not been called earlier
2072 by the callers. */
2073 if (BRACE_ENCLOSED_INITIALIZER_P (init) && CP_AGGREGATE_TYPE_P (type))
2074 init = reshape_init (type, init, complain);
2061820e
JM
2075 }
2076
4c9b3895
JM
2077 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
2078 && CP_AGGREGATE_TYPE_P (type))
e08cc018
JM
2079 /* A brace-enclosed initializer for an aggregate. In C++0x this can
2080 happen for direct-initialization, too. */
f0516ca4 2081 init = digest_init (type, init, complain);
e08cc018 2082
b9119edc
PP
2083 if (init == error_mark_node)
2084 return false;
2085
e08cc018
JM
2086 /* A CONSTRUCTOR of the target's type is a previously digested
2087 initializer, whether that happened just above or in
2088 cp_parser_late_parsing_nsdmi.
2089
36cbfdb0
JM
2090 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
2091 set represents the whole initialization, so we shouldn't build up
2092 another ctor call. */
e08cc018
JM
2093 if (init
2094 && (TREE_CODE (init) == CONSTRUCTOR
36cbfdb0
JM
2095 || (TREE_CODE (init) == TARGET_EXPR
2096 && (TARGET_EXPR_DIRECT_INIT_P (init)
2097 || TARGET_EXPR_LIST_INIT_P (init))))
e08cc018 2098 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
4c9b3895 2099 {
e08cc018
JM
2100 /* Early initialization via a TARGET_EXPR only works for
2101 complete objects. */
2102 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
2103
6ffbf87c 2104 init = cp_build_init_expr (exp, init);
4c9b3895
JM
2105 TREE_SIDE_EFFECTS (init) = 1;
2106 finish_expr_stmt (init);
b9119edc 2107 return true;
4c9b3895
JM
2108 }
2109
277294d7 2110 if (init && TREE_CODE (init) != TREE_LIST
32005478
JM
2111 && (flags & LOOKUP_ONLYCONVERTING)
2112 && !unsafe_return_slot_p (exp))
faf5394a
MS
2113 {
2114 /* Base subobjects should only get direct-initialization. */
8dc2b103 2115 gcc_assert (true_exp == exp);
faf5394a 2116
c37dc68e
JM
2117 if (flags & DIRECT_BIND)
2118 /* Do nothing. We hit this in two cases: Reference initialization,
2119 where we aren't initializing a real variable, so we don't want
2120 to run a new constructor; and catching an exception, where we
2121 have already built up the constructor call so we could wrap it
2122 in an exception region. */;
2123 else
b9119edc
PP
2124 {
2125 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
2126 flags, complain | tf_no_cleanup);
2127 if (init == error_mark_node)
2128 return false;
2129 }
faf5394a 2130
f8ba88b6
JM
2131 /* We need to protect the initialization of a catch parm with a
2132 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
2133 around the TARGET_EXPR for the copy constructor. See
2134 initialize_handler_parm. */
2135 tree *p = &init;
2136 while (TREE_CODE (*p) == MUST_NOT_THROW_EXPR
2137 || TREE_CODE (*p) == CLEANUP_POINT_EXPR)
4e8dca1c 2138 {
f8ba88b6
JM
2139 /* Avoid voidify_wrapper_expr making a temporary. */
2140 TREE_TYPE (*p) = void_type_node;
2141 p = &TREE_OPERAND (*p, 0);
4e8dca1c 2142 }
6ffbf87c 2143 *p = cp_build_init_expr (exp, *p);
f1dedc31 2144 finish_expr_stmt (init);
b9119edc 2145 return true;
faf5394a
MS
2146 }
2147
c166b898
ILT
2148 if (init == NULL_TREE)
2149 parms = NULL;
2150 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
8d08fdba 2151 {
c166b898
ILT
2152 parms = make_tree_vector ();
2153 for (; init != NULL_TREE; init = TREE_CHAIN (init))
9771b263 2154 vec_safe_push (parms, TREE_VALUE (init));
8d08fdba 2155 }
8d08fdba 2156 else
c166b898 2157 parms = make_tree_vector_single (init);
8d08fdba 2158
238e471c
VV
2159 if (exp == current_class_ref && current_function_decl
2160 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
2161 {
2162 /* Delegating constructor. */
2163 tree complete;
2164 tree base;
c4ce224c
JM
2165 tree elt; unsigned i;
2166
2167 /* Unshare the arguments for the second call. */
cd9cf97b 2168 releasing_vec parms2;
9771b263 2169 FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
c4ce224c
JM
2170 {
2171 elt = break_out_target_exprs (elt);
9771b263 2172 vec_safe_push (parms2, elt);
c4ce224c 2173 }
238e471c 2174 complete = build_special_member_call (exp, complete_ctor_identifier,
c4ce224c
JM
2175 &parms2, binfo, flags,
2176 complain);
2177 complete = fold_build_cleanup_point_expr (void_type_node, complete);
c4ce224c 2178
238e471c
VV
2179 base = build_special_member_call (exp, base_ctor_identifier,
2180 &parms, binfo, flags,
2181 complain);
c4ce224c 2182 base = fold_build_cleanup_point_expr (void_type_node, base);
b9119edc
PP
2183 if (complete == error_mark_node || base == error_mark_node)
2184 return false;
eb0dbdc7 2185 rval = build_if_in_charge (complete, base);
238e471c
VV
2186 }
2187 else
2188 {
d6ef53f2
NS
2189 tree ctor_name = (true_exp == exp
2190 ? complete_ctor_identifier : base_ctor_identifier);
2191
238e471c
VV
2192 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
2193 complain);
b9119edc
PP
2194 if (rval == error_mark_node)
2195 return false;
c0cdf62c 2196 }
c166b898
ILT
2197
2198 if (parms != NULL)
2199 release_tree_vector (parms);
2200
fa2200cb
JM
2201 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
2202 {
2203 tree fn = get_callee_fndecl (rval);
a76c13bf 2204 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
fa2200cb 2205 {
3e605b20 2206 tree e = maybe_constant_init (rval, exp);
fa2200cb 2207 if (TREE_CONSTANT (e))
6ffbf87c 2208 rval = cp_build_init_expr (exp, e);
fa2200cb
JM
2209 }
2210 }
2211
2212 /* FIXME put back convert_to_void? */
25eb19ff 2213 if (TREE_SIDE_EFFECTS (rval))
fa2200cb 2214 finish_expr_stmt (rval);
b9119edc
PP
2215
2216 return true;
8d08fdba
MS
2217}
2218
2219/* This function is responsible for initializing EXP with INIT
b9119edc 2220 (if any). Returns true on success, false on failure.
8d08fdba
MS
2221
2222 BINFO is the binfo of the type for who we are performing the
2223 initialization. For example, if W is a virtual base class of A and B,
2224 and C : A, B.
2225 If we are initializing B, then W must contain B's W vtable, whereas
2226 were we initializing C, W must contain C's W vtable.
2227
2228 TRUE_EXP is nonzero if it is the true expression being initialized.
2229 In this case, it may be EXP, or may just contain EXP. The reason we
2230 need this is because if EXP is a base element of TRUE_EXP, we
2231 don't necessarily know by looking at EXP where its virtual
2232 baseclass fields should really be pointing. But we do know
2233 from TRUE_EXP. In constructors, we don't know anything about
2234 the value being initialized.
2235
9f880ef9
MM
2236 FLAGS is just passed to `build_new_method_call'. See that function
2237 for its description. */
8d08fdba 2238
b9119edc 2239static bool
5ade1ed2
DG
2240expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
2241 tsubst_flags_t complain)
8d08fdba
MS
2242{
2243 tree type = TREE_TYPE (exp);
8d08fdba 2244
50bc768d 2245 gcc_assert (init != error_mark_node && type != error_mark_node);
38e01f9e 2246 gcc_assert (building_stmt_list_p ());
8d08fdba
MS
2247
2248 /* Use a function returning the desired type to initialize EXP for us.
2249 If the function is a constructor, and its first argument is
2250 NULL_TREE, know that it was meant for us--just slide exp on
2251 in and expand the constructor. Constructors now come
2252 as TARGET_EXPRs. */
faf5394a 2253
5a6ccc94 2254 if (init && VAR_P (exp)
3b2db49f 2255 && COMPOUND_LITERAL_P (init))
faf5394a 2256 {
9771b263 2257 vec<tree, va_gc> *cleanups = NULL;
f1dedc31 2258 /* If store_init_value returns NULL_TREE, the INIT has been
3b2db49f 2259 recorded as the DECL_INITIAL for EXP. That means there's
f1dedc31 2260 nothing more we have to do. */
b25dd954 2261 init = store_init_value (exp, init, &cleanups, flags);
25ebb82a
RH
2262 if (init)
2263 finish_expr_stmt (init);
b25dd954 2264 gcc_assert (!cleanups);
b9119edc 2265 return true;
faf5394a
MS
2266 }
2267
f388b7be 2268 /* List-initialization from {} becomes value-initialization for non-aggregate
49b5925f
JM
2269 classes with default constructors. Handle this here when we're
2270 initializing a base, so protected access works. */
2271 if (exp != true_exp && init && TREE_CODE (init) == TREE_LIST)
f388b7be
JM
2272 {
2273 tree elt = TREE_VALUE (init);
2274 if (DIRECT_LIST_INIT_P (elt)
2275 && CONSTRUCTOR_ELTS (elt) == 0
2276 && CLASSTYPE_NON_AGGREGATE (type)
2277 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2278 init = void_type_node;
2279 }
2280
fd97a96a
JM
2281 /* If an explicit -- but empty -- initializer list was present,
2282 that's value-initialization. */
2283 if (init == void_type_node)
2284 {
ac0bc21b 2285 /* If the type has data but no user-provided default ctor, we need to zero
c2b3ec18 2286 out the object. */
ac0bc21b 2287 if (type_has_non_user_provided_default_constructor (type)
dbcd32f8 2288 && !is_really_empty_class (type, /*ignore_vptr*/true))
fd97a96a 2289 {
1fb0b801
JM
2290 tree field_size = NULL_TREE;
2291 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
2292 /* Don't clobber already initialized virtual bases. */
2293 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
2294 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
2295 field_size);
6ffbf87c 2296 init = cp_build_init_expr (exp, init);
fd97a96a 2297 finish_expr_stmt (init);
fd97a96a 2298 }
1fb0b801 2299
fd97a96a 2300 /* If we don't need to mess with the constructor at all,
1fb0b801
JM
2301 then we're done. */
2302 if (! type_build_ctor_call (type))
b9119edc 2303 return true;
1fb0b801
JM
2304
2305 /* Otherwise fall through and call the constructor. */
fd97a96a
JM
2306 init = NULL_TREE;
2307 }
2308
9e9ff709
MS
2309 /* We know that expand_default_init can handle everything we want
2310 at this point. */
b9119edc 2311 return expand_default_init (binfo, true_exp, exp, init, flags, complain);
8d08fdba
MS
2312}
2313
9e1e64ec 2314/* Report an error if TYPE is not a user-defined, class type. If
be99da77 2315 OR_ELSE is nonzero, give an error message. */
e92cc029 2316
be99da77 2317int
9e1e64ec 2318is_class_type (tree type, int or_else)
be99da77
MS
2319{
2320 if (type == error_mark_node)
2321 return 0;
2322
9e1e64ec 2323 if (! CLASS_TYPE_P (type))
be99da77
MS
2324 {
2325 if (or_else)
9e1e64ec 2326 error ("%qT is not a class type", type);
be99da77
MS
2327 return 0;
2328 }
2329 return 1;
2330}
2331
e32869a1
PP
2332/* Returns true iff the initializer INIT represents copy-initialization
2333 (and therefore we must set LOOKUP_ONLYCONVERTING when processing it). */
2334
2335bool
2336is_copy_initialization (tree init)
2337{
2338 return (init && init != void_type_node
2339 && TREE_CODE (init) != TREE_LIST
2340 && !(TREE_CODE (init) == TARGET_EXPR
2341 && TARGET_EXPR_DIRECT_INIT_P (init))
2342 && !DIRECT_LIST_INIT_P (init));
2343}
2344
a5ac359a
MM
2345/* Build a reference to a member of an aggregate. This is not a C++
2346 `&', but really something which can have its address taken, and
2347 then act as a pointer to member, for example TYPE :: FIELD can have
2348 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
2349 this expression is the operand of "&".
8d08fdba
MS
2350
2351 @@ Prints out lousy diagnostics for operator <typename>
2352 @@ fields.
2353
e53b6e56 2354 @@ This function should be rewritten and placed in search.cc. */
e92cc029 2355
8d08fdba 2356tree
a378996b
PC
2357build_offset_ref (tree type, tree member, bool address_p,
2358 tsubst_flags_t complain)
8d08fdba 2359{
8d245821 2360 tree decl;
fc378698 2361 tree basebinfo = NULL_TREE;
8d08fdba 2362
5f311aec 2363 /* class templates can come in as TEMPLATE_DECLs here. */
d4f0f205
MM
2364 if (TREE_CODE (member) == TEMPLATE_DECL)
2365 return member;
93cdc044 2366
627bc938
JM
2367 if (dependent_scope_p (type) || type_dependent_expression_p (member))
2368 return build_qualified_name (NULL_TREE, type, member,
2d660b7f 2369 /*template_p=*/false);
5566b478 2370
d4f0f205 2371 gcc_assert (TYPE_P (type));
9e1e64ec 2372 if (! is_class_type (type, 1))
c833d2be
NS
2373 return error_mark_node;
2374
d4f0f205 2375 gcc_assert (DECL_P (member) || BASELINK_P (member));
c352ef0e
JM
2376 /* Callers should call mark_used before this point, except for functions. */
2377 gcc_assert (!DECL_P (member) || TREE_USED (member)
2378 || TREE_CODE (member) == FUNCTION_DECL);
be99da77 2379
627bc938 2380 type = TYPE_MAIN_VARIANT (type);
01628e54 2381 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
8d08fdba 2382 {
a378996b
PC
2383 if (complain & tf_error)
2384 error ("incomplete type %qT does not have member %qD", type, member);
a5ac359a
MM
2385 return error_mark_node;
2386 }
2387
d4f0f205 2388 /* Entities other than non-static members need no further
3db45ab5 2389 processing. */
a5ac359a 2390 if (TREE_CODE (member) == TYPE_DECL)
d4f0f205 2391 return member;
5a6ccc94 2392 if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
d4f0f205 2393 return convert_from_reference (member);
a5ac359a
MM
2394
2395 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
2396 {
a378996b
PC
2397 if (complain & tf_error)
2398 error ("invalid pointer to bit-field %qD", member);
a5ac359a
MM
2399 return error_mark_node;
2400 }
2401
d4f0f205
MM
2402 /* Set up BASEBINFO for member lookup. */
2403 decl = maybe_dummy_object (type, &basebinfo);
2404
aa52c1ff 2405 /* A lot of this logic is now handled in lookup_member. */
a5ac359a 2406 if (BASELINK_P (member))
8d08fdba 2407 {
8d08fdba 2408 /* Go from the TREE_BASELINK to the member function info. */
7304fcb4 2409 tree t = BASELINK_FUNCTIONS (member);
8d08fdba 2410
50ad9642 2411 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
8d08fdba 2412 {
f4f206f4 2413 /* Get rid of a potential OVERLOAD around it. */
848bf88d 2414 t = OVL_FIRST (t);
2c73f9f5 2415
b54f5338
KL
2416 /* Unique functions are handled easily. */
2417
2418 /* For non-static member of base class, we need a special rule
2419 for access checking [class.protected]:
2420
2421 If the access is to form a pointer to member, the
2422 nested-name-specifier shall name the derived class
2423 (or any class derived from that class). */
080384d6 2424 bool ok;
b54f5338
KL
2425 if (address_p && DECL_P (t)
2426 && DECL_NONSTATIC_MEMBER_P (t))
080384d6
JM
2427 ok = perform_or_defer_access_check (TYPE_BINFO (type), t, t,
2428 complain);
b54f5338 2429 else
080384d6
JM
2430 ok = perform_or_defer_access_check (basebinfo, t, t,
2431 complain);
2432 if (!ok)
2433 return error_mark_node;
848b92e1 2434 if (DECL_STATIC_FUNCTION_P (t))
a1b3484a 2435 return member;
a5ac359a
MM
2436 member = t;
2437 }
2438 else
7304fcb4 2439 TREE_TYPE (member) = unknown_type_node;
8d08fdba 2440 }
b54f5338 2441 else if (address_p && TREE_CODE (member) == FIELD_DECL)
080384d6
JM
2442 {
2443 /* We need additional test besides the one in
2444 check_accessibility_of_qualified_id in case it is
2445 a pointer to non-static member. */
2446 if (!perform_or_defer_access_check (TYPE_BINFO (type), member, member,
2447 complain))
2448 return error_mark_node;
2449 }
8d08fdba 2450
a5ac359a 2451 if (!address_p)
8d08fdba 2452 {
a5ac359a
MM
2453 /* If MEMBER is non-static, then the program has fallen afoul of
2454 [expr.prim]:
8d08fdba 2455
d20b7173
MP
2456 An id-expression that denotes a non-static data member or
2457 non-static member function of a class can only be used:
8d08fdba 2458
a5ac359a
MM
2459 -- as part of a class member access (_expr.ref_) in which the
2460 object-expression refers to the member's class or a class
2461 derived from that class, or
b7484fbe 2462
a5ac359a
MM
2463 -- to form a pointer to member (_expr.unary.op_), or
2464
d20b7173
MP
2465 -- in the body of a non-static member function of that class or
2466 of a class derived from that class (_class.mfct.non-static_), or
a5ac359a
MM
2467
2468 -- in a mem-initializer for a constructor for that class or for
2469 a class derived from that class (_class.base.init_). */
2470 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
2471 {
39a13be5 2472 /* Build a representation of the qualified name suitable
e9525111
MM
2473 for use as the operand to "&" -- even though the "&" is
2474 not actually present. */
f293ce4b 2475 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
a5ac359a
MM
2476 /* In Microsoft mode, treat a non-static member function as if
2477 it were a pointer-to-member. */
2478 if (flag_ms_extensions)
2479 {
a5ac359a 2480 PTRMEM_OK_P (member) = 1;
a378996b 2481 return cp_build_addr_expr (member, complain);
a5ac359a 2482 }
a378996b
PC
2483 if (complain & tf_error)
2484 error ("invalid use of non-static member function %qD",
2485 TREE_OPERAND (member, 1));
07471dfb 2486 return error_mark_node;
a5ac359a
MM
2487 }
2488 else if (TREE_CODE (member) == FIELD_DECL)
2489 {
a378996b
PC
2490 if (complain & tf_error)
2491 error ("invalid use of non-static data member %qD", member);
a5ac359a
MM
2492 return error_mark_node;
2493 }
2494 return member;
2495 }
8d08fdba 2496
f293ce4b 2497 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
8d245821
MM
2498 PTRMEM_OK_P (member) = 1;
2499 return member;
8d08fdba
MS
2500}
2501
393e756d
MM
2502/* If DECL is a scalar enumeration constant or variable with a
2503 constant initializer, return the initializer (or, its initializers,
69eb4fde
JM
2504 recursively); otherwise, return DECL. If STRICT_P, the
2505 initializer is only returned if DECL is a
90454da1 2506 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
8c00059c
PP
2507 return an aggregate constant. If UNSHARE_P, return an unshared
2508 copy of the initializer. */
8d08fdba 2509
393e756d 2510static tree
8c00059c
PP
2511constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p,
2512 bool unshare_p)
8d08fdba 2513{
f513e31f 2514 while (TREE_CODE (decl) == CONST_DECL
fd338b13
JM
2515 || decl_constant_var_p (decl)
2516 || (!strict_p && VAR_P (decl)
2517 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl))))
b794e321
MM
2518 {
2519 tree init;
fa2200cb
JM
2520 /* If DECL is a static data member in a template
2521 specialization, we must instantiate it here. The
2522 initializer for the static data member is not processed
2523 until needed; we need it now. */
fd5c817a 2524 mark_used (decl, tf_none);
fa2200cb 2525 init = DECL_INITIAL (decl);
d174af6c 2526 if (init == error_mark_node)
88274c4d 2527 {
cdd669f9
JJ
2528 if (TREE_CODE (decl) == CONST_DECL
2529 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
88274c4d
JM
2530 /* Treat the error as a constant to avoid cascading errors on
2531 excessively recursive template instantiation (c++/9335). */
2532 return init;
2533 else
2534 return decl;
2535 }
73ce7fcb
JJ
2536 /* Initializers in templates are generally expanded during
2537 instantiation, so before that for const int i(2)
2538 INIT is a TREE_LIST with the actual initializer as
2539 TREE_VALUE. */
2540 if (processing_template_decl
2541 && init
2542 && TREE_CODE (init) == TREE_LIST
2543 && TREE_CHAIN (init) == NULL_TREE)
2544 init = TREE_VALUE (init);
d1dfa20d
JM
2545 /* Instantiate a non-dependent initializer for user variables. We
2546 mustn't do this for the temporary for an array compound literal;
2547 trying to instatiate the initializer will keep creating new
2548 temporaries until we crash. Probably it's not useful to do it for
2549 other artificial variables, either. */
2550 if (!DECL_ARTIFICIAL (decl))
2551 init = instantiate_non_dependent_or_null (init);
d174af6c 2552 if (!init
b794e321 2553 || !TREE_TYPE (init)
8152661b 2554 || !TREE_CONSTANT (init)
69eb4fde 2555 || (!return_aggregate_cst_ok_p
90454da1
PC
2556 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2557 return an aggregate constant (of which string
2558 literals are a special case), as we do not want
2559 to make inadvertent copies of such entities, and
2560 we must be sure that their addresses are the
2561 same everywhere. */
8152661b
JM
2562 && (TREE_CODE (init) == CONSTRUCTOR
2563 || TREE_CODE (init) == STRING_CST)))
b794e321 2564 break;
62f9ab0d 2565 /* Don't return a CONSTRUCTOR for a variable with partial run-time
5a5da480
JJ
2566 initialization, since it doesn't represent the entire value.
2567 Similarly for VECTOR_CSTs created by cp_folding those
2568 CONSTRUCTORs. */
2569 if ((TREE_CODE (init) == CONSTRUCTOR
2570 || TREE_CODE (init) == VECTOR_CST)
62f9ab0d
JM
2571 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2572 break;
6443c7c0
MP
2573 /* If the variable has a dynamic initializer, don't use its
2574 DECL_INITIAL which doesn't reflect the real value. */
2575 if (VAR_P (decl)
2576 && TREE_STATIC (decl)
2577 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
2578 && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2579 break;
8c00059c 2580 decl = init;
b794e321 2581 }
8c00059c 2582 return unshare_p ? unshare_expr (decl) : decl;
8a784e4a 2583}
a1652802 2584
69eb4fde
JM
2585/* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant
2586 of integral or enumeration type, or a constexpr variable of scalar type,
2587 then return that value. These are those variables permitted in constant
2588 expressions by [5.19/1]. */
a1652802 2589
8a784e4a 2590tree
69eb4fde 2591scalar_constant_value (tree decl)
8a784e4a 2592{
69eb4fde 2593 return constant_value_1 (decl, /*strict_p=*/true,
8c00059c
PP
2594 /*return_aggregate_cst_ok_p=*/false,
2595 /*unshare_p=*/true);
393e756d 2596}
c8094d83 2597
8c00059c
PP
2598/* Like scalar_constant_value, but can also return aggregate initializers.
2599 If UNSHARE_P, return an unshared copy of the initializer. */
393e756d
MM
2600
2601tree
8c00059c 2602decl_really_constant_value (tree decl, bool unshare_p /*= true*/)
393e756d 2603{
69eb4fde 2604 return constant_value_1 (decl, /*strict_p=*/true,
8c00059c
PP
2605 /*return_aggregate_cst_ok_p=*/true,
2606 /*unshare_p=*/unshare_p);
90454da1
PC
2607}
2608
8c00059c 2609/* A more relaxed version of decl_really_constant_value, used by the
69eb4fde 2610 common C/C++ code. */
90454da1
PC
2611
2612tree
8c00059c 2613decl_constant_value (tree decl, bool unshare_p)
90454da1 2614{
69eb4fde 2615 return constant_value_1 (decl, /*strict_p=*/processing_template_decl,
8c00059c
PP
2616 /*return_aggregate_cst_ok_p=*/true,
2617 /*unshare_p=*/unshare_p);
2618}
2619
2620tree
2621decl_constant_value (tree decl)
2622{
2623 return decl_constant_value (decl, /*unshare_p=*/true);
8d08fdba
MS
2624}
2625\f
8d08fdba 2626/* Common subroutines of build_new and build_vec_delete. */
8d08fdba 2627\f
63c9a190
MM
2628/* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2629 the type of the object being allocated; otherwise, it's just TYPE.
2630 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2631 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
c166b898
ILT
2632 a vector of arguments to be provided as arguments to a placement
2633 new operator. This routine performs no semantic checks; it just
2634 creates and returns a NEW_EXPR. */
a0d5fba7 2635
63c9a190 2636static tree
87d3f828
PC
2637build_raw_new_expr (location_t loc, vec<tree, va_gc> *placement, tree type,
2638 tree nelts, vec<tree, va_gc> *init, int use_global_new)
743f140d 2639{
c166b898 2640 tree init_list;
63c9a190 2641 tree new_expr;
3db45ab5 2642
c166b898
ILT
2643 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2644 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2645 permits us to distinguish the case of a missing initializer "new
2646 int" from an empty initializer "new int()". */
2647 if (init == NULL)
2648 init_list = NULL_TREE;
9771b263 2649 else if (init->is_empty ())
632f2871 2650 init_list = void_node;
c166b898 2651 else
335a120f 2652 init_list = build_tree_list_vec (init);
c166b898 2653
87d3f828
PC
2654 new_expr = build4_loc (loc, NEW_EXPR, build_pointer_type (type),
2655 build_tree_list_vec (placement), type, nelts,
2656 init_list);
63c9a190
MM
2657 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2658 TREE_SIDE_EFFECTS (new_expr) = 1;
2659
2660 return new_expr;
743f140d
PB
2661}
2662
9d809e8f
FC
2663/* Diagnose uninitialized const members or reference members of type
2664 TYPE. USING_NEW is used to disambiguate the diagnostic between a
40bb78ad
FC
2665 new expression without a new-initializer and a declaration. Returns
2666 the error count. */
9d809e8f 2667
40bb78ad 2668static int
9d809e8f 2669diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
40bb78ad 2670 bool using_new, bool complain)
9d809e8f
FC
2671{
2672 tree field;
40bb78ad 2673 int error_count = 0;
9d809e8f 2674
10ab8f62 2675 if (type_has_user_provided_constructor (type))
40bb78ad 2676 return 0;
10ab8f62 2677
910ad8de 2678 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9d809e8f
FC
2679 {
2680 tree field_type;
2681
2682 if (TREE_CODE (field) != FIELD_DECL)
2683 continue;
2684
2685 field_type = strip_array_types (TREE_TYPE (field));
2686
1c682d06
FC
2687 if (type_has_user_provided_constructor (field_type))
2688 continue;
2689
9f613f06 2690 if (TYPE_REF_P (field_type))
9d809e8f 2691 {
40bb78ad
FC
2692 ++ error_count;
2693 if (complain)
2694 {
0e02d8e3
PC
2695 if (DECL_CONTEXT (field) == origin)
2696 {
2697 if (using_new)
2698 error ("uninitialized reference member in %q#T "
2699 "using %<new%> without new-initializer", origin);
2700 else
2701 error ("uninitialized reference member in %q#T", origin);
2702 }
40bb78ad 2703 else
0e02d8e3
PC
2704 {
2705 if (using_new)
2706 error ("uninitialized reference member in base %q#T "
2707 "of %q#T using %<new%> without new-initializer",
2708 DECL_CONTEXT (field), origin);
2709 else
2710 error ("uninitialized reference member in base %q#T "
2711 "of %q#T", DECL_CONTEXT (field), origin);
2712 }
40bb78ad 2713 inform (DECL_SOURCE_LOCATION (field),
816551fe 2714 "%q#D should be initialized", field);
40bb78ad 2715 }
9d809e8f
FC
2716 }
2717
2718 if (CP_TYPE_CONST_P (field_type))
2719 {
40bb78ad
FC
2720 ++ error_count;
2721 if (complain)
2722 {
0e02d8e3
PC
2723 if (DECL_CONTEXT (field) == origin)
2724 {
2725 if (using_new)
2726 error ("uninitialized const member in %q#T "
2727 "using %<new%> without new-initializer", origin);
2728 else
2729 error ("uninitialized const member in %q#T", origin);
2730 }
40bb78ad 2731 else
0e02d8e3
PC
2732 {
2733 if (using_new)
2734 error ("uninitialized const member in base %q#T "
2735 "of %q#T using %<new%> without new-initializer",
2736 DECL_CONTEXT (field), origin);
2737 else
2738 error ("uninitialized const member in base %q#T "
2739 "of %q#T", DECL_CONTEXT (field), origin);
2740 }
40bb78ad 2741 inform (DECL_SOURCE_LOCATION (field),
816551fe 2742 "%q#D should be initialized", field);
40bb78ad 2743 }
9d809e8f
FC
2744 }
2745
2746 if (CLASS_TYPE_P (field_type))
40bb78ad
FC
2747 error_count
2748 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2749 using_new, complain);
9d809e8f 2750 }
40bb78ad 2751 return error_count;
9d809e8f
FC
2752}
2753
40bb78ad
FC
2754int
2755diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
9d809e8f 2756{
40bb78ad 2757 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
9d809e8f
FC
2758}
2759
7d5e76c8
JM
2760/* Call __cxa_bad_array_new_length to indicate that the size calculation
2761 overflowed. Pretend it returns sizetype so that it plays nicely in the
2762 COND_EXPR. */
2763
2764tree
2765throw_bad_array_new_length (void)
2766{
4b4b2e58
NS
2767 if (!fn)
2768 {
2769 tree name = get_identifier ("__cxa_throw_bad_array_new_length");
2770
87e3d7cf 2771 fn = get_global_binding (name);
4b4b2e58
NS
2772 if (!fn)
2773 fn = push_throw_library_fn
2774 (name, build_function_type_list (sizetype, NULL_TREE));
2775 }
7d5e76c8
JM
2776
2777 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2778}
2779
e2f5cc96
MS
2780/* Attempt to verify that the argument, OPER, of a placement new expression
2781 refers to an object sufficiently large for an object of TYPE or an array
2782 of NELTS of such objects when NELTS is non-null, and issue a warning when
2783 it does not. SIZE specifies the size needed to construct the object or
2784 array and captures the result of NELTS * sizeof (TYPE). (SIZE could be
2785 greater when the array under construction requires a cookie to store
2786 NELTS. GCC's placement new expression stores the cookie when invoking
2787 a user-defined placement new operator function but not the default one.
2788 Placement new expressions with user-defined placement new operator are
2789 not diagnosed since we don't know how they use the buffer (this could
2790 be a future extension). */
2791static void
2792warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper)
2793{
f9d0ca40 2794 location_t loc = cp_expr_loc_or_input_loc (oper);
e2f5cc96 2795
e2f5cc96
MS
2796 STRIP_NOPS (oper);
2797
2798 /* Using a function argument or a (non-array) variable as an argument
2799 to placement new is not checked since it's unknown what it might
2800 point to. */
2801 if (TREE_CODE (oper) == PARM_DECL
86287716 2802 || VAR_P (oper)
e2f5cc96
MS
2803 || TREE_CODE (oper) == COMPONENT_REF)
2804 return;
2805
2806 /* Evaluate any constant expressions. */
2807 size = fold_non_dependent_expr (size);
2808
de05c19d
MS
2809 access_ref ref;
2810 ref.eval = [](tree x){ return fold_non_dependent_expr (x); };
2811 ref.trail1special = warn_placement_new < 2;
2812 tree objsize = compute_objsize (oper, 1, &ref);
2813 if (!objsize)
2814 return;
e2f5cc96 2815
790b02af
JM
2816 /* We can only draw conclusions if ref.deref == -1,
2817 i.e. oper is the address of the object. */
2818 if (ref.deref != -1)
2819 return;
2820
de05c19d
MS
2821 offset_int bytes_avail = wi::to_offset (objsize);
2822 offset_int bytes_need;
e2f5cc96 2823
de05c19d
MS
2824 if (CONSTANT_CLASS_P (size))
2825 bytes_need = wi::to_offset (size);
2826 else if (nelts && CONSTANT_CLASS_P (nelts))
2827 bytes_need = (wi::to_offset (nelts)
2828 * wi::to_offset (TYPE_SIZE_UNIT (type)));
2829 else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2830 bytes_need = wi::to_offset (TYPE_SIZE_UNIT (type));
2831 else
46cb9332 2832 {
de05c19d
MS
2833 /* The type is a VLA. */
2834 return;
46cb9332 2835 }
e2f5cc96 2836
de05c19d
MS
2837 if (bytes_avail >= bytes_need)
2838 return;
e2f5cc96 2839
de05c19d
MS
2840 /* True when the size to mention in the warning is exact as opposed
2841 to "at least N". */
2842 const bool exact_size = (ref.offrng[0] == ref.offrng[1]
2843 || ref.sizrng[1] - ref.offrng[0] == 0);
2844
2845 tree opertype = ref.ref ? TREE_TYPE (ref.ref) : TREE_TYPE (oper);
2846 bool warned = false;
2847 if (nelts)
2848 nelts = fold_for_warn (nelts);
2849 if (nelts)
2850 if (CONSTANT_CLASS_P (nelts))
2851 warned = warning_at (loc, OPT_Wplacement_new_,
2852 (exact_size
2853 ? G_("placement new constructing an object "
2854 "of type %<%T [%wu]%> and size %qwu "
2855 "in a region of type %qT and size %qwi")
2856 : G_("placement new constructing an object "
2857 "of type %<%T [%wu]%> and size %qwu "
2858 "in a region of type %qT and size "
2859 "at most %qwu")),
2860 type, tree_to_uhwi (nelts),
2861 bytes_need.to_uhwi (),
2862 opertype, bytes_avail.to_uhwi ());
2863 else
2864 warned = warning_at (loc, OPT_Wplacement_new_,
2865 (exact_size
2866 ? G_("placement new constructing an array "
2867 "of objects of type %qT and size %qwu "
2868 "in a region of type %qT and size %qwi")
2869 : G_("placement new constructing an array "
2870 "of objects of type %qT and size %qwu "
2871 "in a region of type %qT and size "
2872 "at most %qwu")),
2873 type, bytes_need.to_uhwi (), opertype,
2874 bytes_avail.to_uhwi ());
2875 else
2876 warned = warning_at (loc, OPT_Wplacement_new_,
2877 (exact_size
2878 ? G_("placement new constructing an object "
2879 "of type %qT and size %qwu in a region "
2880 "of type %qT and size %qwi")
2881 : G_("placement new constructing an object "
2882 "of type %qT "
2883 "and size %qwu in a region of type %qT "
2884 "and size at most %qwu")),
2885 type, bytes_need.to_uhwi (), opertype,
2886 bytes_avail.to_uhwi ());
2887
2888 if (!warned || !ref.ref)
2889 return;
e2f5cc96 2890
de05c19d
MS
2891 if (ref.offrng[0] == 0 || !ref.offset_bounded ())
2892 /* Avoid mentioning the offset when its lower bound is zero
2893 or when it's impossibly large. */
2894 inform (DECL_SOURCE_LOCATION (ref.ref),
2895 "%qD declared here", ref.ref);
2896 else if (ref.offrng[0] == ref.offrng[1])
2897 inform (DECL_SOURCE_LOCATION (ref.ref),
2898 "at offset %wi from %qD declared here",
2899 ref.offrng[0].to_shwi (), ref.ref);
2900 else
2901 inform (DECL_SOURCE_LOCATION (ref.ref),
2902 "at offset [%wi, %wi] from %qD declared here",
2903 ref.offrng[0].to_shwi (), ref.offrng[1].to_shwi (), ref.ref);
e2f5cc96
MS
2904}
2905
af63ba4b
JM
2906/* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */
2907
2908bool
2909type_has_new_extended_alignment (tree t)
2910{
2e1c20b1
JJ
2911 return (aligned_new_threshold
2912 && TYPE_ALIGN_UNIT (t) > (unsigned)aligned_new_threshold);
af63ba4b
JM
2913}
2914
2ec69f56
JM
2915/* Return the alignment we expect malloc to guarantee. This should just be
2916 MALLOC_ABI_ALIGNMENT, but that macro defaults to only BITS_PER_WORD for some
2917 reason, so don't let the threshold be smaller than max_align_t_align. */
2918
2919unsigned
2920malloc_alignment ()
2921{
2922 return MAX (max_align_t_align(), MALLOC_ABI_ALIGNMENT);
2923}
2924
ece3b7e6 2925/* Determine whether an allocation function is a namespace-scope
cf9847d2 2926 non-replaceable placement new function. See DR 1748. */
8bee092e
JJ
2927static bool
2928std_placement_new_fn_p (tree alloc_fn)
ece3b7e6 2929{
8f2b097e 2930 if (DECL_NAMESPACE_SCOPE_P (alloc_fn))
ece3b7e6
VV
2931 {
2932 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
2933 if ((TREE_VALUE (first_arg) == ptr_type_node)
2934 && TREE_CHAIN (first_arg) == void_list_node)
2935 return true;
2936 }
2937 return false;
2938}
2939
8e007055 2940/* For element type ELT_TYPE, return the appropriate type of the heap object
0a0c2c3f
JJ
2941 containing such element(s). COOKIE_SIZE is the size of cookie in bytes.
2942 Return
8e007055 2943 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
0a0c2c3f
JJ
2944 where N is nothing (flexible array member) if ITYPE2 is NULL, otherwise
2945 the array has ITYPE2 as its TYPE_DOMAIN. */
8e007055
JJ
2946
2947tree
0a0c2c3f 2948build_new_constexpr_heap_type (tree elt_type, tree cookie_size, tree itype2)
8e007055 2949{
0a0c2c3f
JJ
2950 gcc_assert (tree_fits_uhwi_p (cookie_size));
2951 unsigned HOST_WIDE_INT csz = tree_to_uhwi (cookie_size);
8e007055
JJ
2952 csz /= int_size_in_bytes (sizetype);
2953 tree itype1 = build_index_type (size_int (csz - 1));
2954 tree atype1 = build_cplus_array_type (sizetype, itype1);
2955 tree atype2 = build_cplus_array_type (elt_type, itype2);
2956 tree rtype = cxx_make_type (RECORD_TYPE);
2957 TYPE_NAME (rtype) = heap_identifier;
2958 tree fld1 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype1);
2959 tree fld2 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype2);
2960 DECL_FIELD_CONTEXT (fld1) = rtype;
2961 DECL_FIELD_CONTEXT (fld2) = rtype;
2962 DECL_ARTIFICIAL (fld1) = true;
2963 DECL_ARTIFICIAL (fld2) = true;
2964 TYPE_FIELDS (rtype) = fld1;
2965 DECL_CHAIN (fld1) = fld2;
2966 layout_type (rtype);
2967 return rtype;
2968}
2969
2970/* Help the constexpr code to find the right type for the heap variable
2971 by adding a NOP_EXPR around ALLOC_CALL if needed for cookie_size.
2972 Return ALLOC_CALL or ALLOC_CALL cast to a pointer to
2973 struct { size_t[cookie_size/sizeof(size_t)]; elt_type[]; }. */
2974
2975static tree
2976maybe_wrap_new_for_constexpr (tree alloc_call, tree elt_type, tree cookie_size)
2977{
b04445d4 2978 if (cxx_dialect < cxx20)
8e007055
JJ
2979 return alloc_call;
2980
2981 if (current_function_decl != NULL_TREE
2982 && !DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2983 return alloc_call;
2984
2985 tree call_expr = extract_call_expr (alloc_call);
2986 if (call_expr == error_mark_node)
2987 return alloc_call;
2988
2989 tree alloc_call_fndecl = cp_get_callee_fndecl_nofold (call_expr);
2990 if (alloc_call_fndecl == NULL_TREE
2991 || !IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_call_fndecl))
2992 || CP_DECL_CONTEXT (alloc_call_fndecl) != global_namespace)
2993 return alloc_call;
2994
2995 tree rtype = build_new_constexpr_heap_type (elt_type, cookie_size,
2996 NULL_TREE);
2997 return build_nop (build_pointer_type (rtype), alloc_call);
2998}
2999
63c9a190
MM
3000/* Generate code for a new-expression, including calling the "operator
3001 new" function, initializing the object, and, if an exception occurs
3002 during construction, cleaning up. The arguments are as for
685c8340
MS
3003 build_raw_new_expr. This may change PLACEMENT and INIT.
3004 TYPE is the type of the object being constructed, possibly an array
3005 of NELTS elements when NELTS is non-null (in "new T[NELTS]", T may
3006 be an array of the form U[inner], with the whole expression being
3007 "new U[NELTS][inner]"). */
a0d5fba7 3008
834c6dff 3009static tree
9771b263
DN
3010build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
3011 vec<tree, va_gc> **init, bool globally_qualified_p,
c166b898 3012 tsubst_flags_t complain)
a0d5fba7 3013{
d746e87d
MM
3014 tree size, rval;
3015 /* True iff this is a call to "operator new[]" instead of just
c8094d83 3016 "operator new". */
d746e87d 3017 bool array_p = false;
9207099b
JM
3018 /* If ARRAY_P is true, the element type of the array. This is never
3019 an ARRAY_TYPE; for something like "new int[3][4]", the
d746e87d 3020 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
9207099b 3021 TYPE. */
d746e87d 3022 tree elt_type;
f4f4610e
MM
3023 /* The type of the new-expression. (This type is always a pointer
3024 type.) */
3025 tree pointer_type;
25357d1e 3026 tree non_const_pointer_type;
685c8340 3027 /* The most significant array bound in int[OUTER_NELTS][inner]. */
a48cccea 3028 tree outer_nelts = NULL_TREE;
685c8340
MS
3029 /* For arrays with a non-constant number of elements, a bounds checks
3030 on the NELTS parameter to avoid integer overflow at runtime. */
4a84253c 3031 tree outer_nelts_check = NULL_TREE;
4ebc46e9 3032 bool outer_nelts_from_type = false;
685c8340 3033 /* Number of the "inner" elements in "new T[OUTER_NELTS][inner]". */
807e902e 3034 offset_int inner_nelts_count = 1;
f4f4610e 3035 tree alloc_call, alloc_expr;
685c8340 3036 /* Size of the inner array elements (those with constant dimensions). */
807e902e 3037 offset_int inner_size;
f4f4610e
MM
3038 /* The address returned by the call to "operator new". This node is
3039 a VAR_DECL and is therefore reusable. */
3040 tree alloc_node;
46ff5047 3041 tree alloc_fn;
8b5e2ce4 3042 tree cookie_expr, init_expr;
089d6ea7 3043 int nothrow, check_new;
834c6dff
MM
3044 /* If non-NULL, the number of extra bytes to allocate at the
3045 beginning of the storage allocated for an array-new expression in
3046 order to store the number of elements. */
3047 tree cookie_size = NULL_TREE;
c166b898 3048 tree placement_first;
a9de800a 3049 tree placement_expr = NULL_TREE;
3f41ffd8
MM
3050 /* True if the function we are calling is a placement allocation
3051 function. */
3052 bool placement_allocation_fn_p;
f4f4610e 3053 /* True if the storage must be initialized, either by a constructor
34cd5ae7 3054 or due to an explicit new-initializer. */
f4f4610e
MM
3055 bool is_initialized;
3056 /* The address of the thing allocated, not including any cookie. In
3057 particular, if an array cookie is in use, DATA_ADDR is the
3058 address of the first array element. This node is a VAR_DECL, and
3059 is therefore reusable. */
3060 tree data_addr;
b75bf8b1 3061 tree orig_type = type;
a0d5fba7 3062
058b15c1 3063 if (nelts)
a0d5fba7 3064 {
058b15c1 3065 outer_nelts = nelts;
d746e87d 3066 array_p = true;
a0d5fba7 3067 }
9207099b 3068 else if (TREE_CODE (type) == ARRAY_TYPE)
d746e87d 3069 {
4ebc46e9
FW
3070 /* Transforms new (T[N]) to new T[N]. The former is a GNU
3071 extension for variable N. (This also covers new T where T is
3072 a VLA typedef.) */
9207099b
JM
3073 array_p = true;
3074 nelts = array_type_nelts_top (type);
3075 outer_nelts = nelts;
3076 type = TREE_TYPE (type);
4ebc46e9 3077 outer_nelts_from_type = true;
d746e87d 3078 }
834c6dff 3079
ea219e6e 3080 /* Lots of logic below depends on whether we have a constant number of
cda0a029 3081 elements, so go ahead and fold it now. */
e56f6629 3082 const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain);
cda0a029 3083
8d08fdba
MS
3084 /* If our base type is an array, then make sure we know how many elements
3085 it has. */
d746e87d
MM
3086 for (elt_type = type;
3087 TREE_CODE (elt_type) == ARRAY_TYPE;
3088 elt_type = TREE_TYPE (elt_type))
4ebc46e9
FW
3089 {
3090 tree inner_nelts = array_type_nelts_top (elt_type);
3091 tree inner_nelts_cst = maybe_constant_value (inner_nelts);
e1f10dd9 3092 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
4a84253c 3093 {
4a669ac3 3094 wi::overflow_type overflow;
807e902e
KZ
3095 offset_int result = wi::mul (wi::to_offset (inner_nelts_cst),
3096 inner_nelts_count, SIGNED, &overflow);
9be0ac8c 3097 if (overflow)
4a84253c
FW
3098 {
3099 if (complain & tf_error)
3100 error ("integer overflow in array size");
3101 nelts = error_mark_node;
3102 }
3103 inner_nelts_count = result;
3104 }
3105 else
4ebc46e9
FW
3106 {
3107 if (complain & tf_error)
3108 {
f9d0ca40 3109 error_at (cp_expr_loc_or_input_loc (inner_nelts),
b75bf8b1 3110 "array size in new-expression must be constant");
4ebc46e9
FW
3111 cxx_constant_value(inner_nelts);
3112 }
3113 nelts = error_mark_node;
3114 }
3115 if (nelts != error_mark_node)
3116 nelts = cp_build_binary_op (input_location,
3117 MULT_EXPR, nelts,
3118 inner_nelts_cst,
3119 complain);
3120 }
3121
02a32ab4
RS
3122 if (!verify_type_context (input_location, TCTX_ALLOCATION, elt_type,
3123 !(complain & tf_error)))
3124 return error_mark_node;
3125
4ebc46e9
FW
3126 if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
3127 {
b75bf8b1 3128 error ("variably modified type not allowed in new-expression");
4ebc46e9
FW
3129 return error_mark_node;
3130 }
3131
3132 if (nelts == error_mark_node)
3133 return error_mark_node;
3134
3135 /* Warn if we performed the (T[N]) to T[N] transformation and N is
3136 variable. */
3137 if (outer_nelts_from_type
ea219e6e 3138 && !TREE_CONSTANT (cst_outer_nelts))
4ebc46e9
FW
3139 {
3140 if (complain & tf_warning_or_error)
b75bf8b1 3141 {
f9d0ca40 3142 pedwarn (cp_expr_loc_or_input_loc (outer_nelts), OPT_Wvla,
24f12823 3143 typedef_variant_p (orig_type)
324ff1a0 3144 ? G_("non-constant array new length must be specified "
a9c697b8 3145 "directly, not by %<typedef%>")
24f12823
VR
3146 : G_("non-constant array new length must be specified "
3147 "without parentheses around the type-id"));
b75bf8b1 3148 }
4ebc46e9
FW
3149 else
3150 return error_mark_node;
3151 }
5566b478 3152
50e10fa8 3153 if (VOID_TYPE_P (elt_type))
e1cd6e56 3154 {
5ade1ed2 3155 if (complain & tf_error)
a9c697b8 3156 error ("invalid type %<void%> for %<new%>");
e1cd6e56
MS
3157 return error_mark_node;
3158 }
3159
f620e64a 3160 if (is_std_init_list (elt_type) && !cp_unevaluated_operand)
04eb9c55 3161 warning (OPT_Winit_list_lifetime,
a9c697b8 3162 "%<new%> of %<initializer_list%> does not "
04eb9c55
JM
3163 "extend the lifetime of the underlying array");
3164
c17fa0f2 3165 if (abstract_virtuals_error (ACU_NEW, elt_type, complain))
a7a64a77 3166 return error_mark_node;
8926095f 3167
95552437 3168 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
b87d79e6 3169
eca7fc57 3170 if (*init == NULL && cxx_dialect < cxx11)
9d809e8f 3171 {
40bb78ad 3172 bool maybe_uninitialized_error = false;
9d809e8f
FC
3173 /* A program that calls for default-initialization [...] of an
3174 entity of reference type is ill-formed. */
3175 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
40bb78ad 3176 maybe_uninitialized_error = true;
9d809e8f
FC
3177
3178 /* A new-expression that creates an object of type T initializes
3179 that object as follows:
3180 - If the new-initializer is omitted:
3181 -- If T is a (possibly cv-qualified) non-POD class type
3182 (or array thereof), the object is default-initialized (8.5).
3183 [...]
3184 -- Otherwise, the object created has indeterminate
3185 value. If T is a const-qualified type, or a (possibly
3186 cv-qualified) POD class type (or array thereof)
3187 containing (directly or indirectly) a member of
3188 const-qualified type, the program is ill-formed; */
3189
3190 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
40bb78ad 3191 maybe_uninitialized_error = true;
9d809e8f 3192
40bb78ad
FC
3193 if (maybe_uninitialized_error
3194 && diagnose_uninitialized_cst_or_ref_member (elt_type,
3195 /*using_new=*/true,
3196 complain & tf_error))
3197 return error_mark_node;
9d809e8f
FC
3198 }
3199
c166b898 3200 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
6132bdd7 3201 && default_init_uninitialized_part (elt_type))
f4f4610e 3202 {
5ade1ed2
DG
3203 if (complain & tf_error)
3204 error ("uninitialized const in %<new%> of %q#T", elt_type);
f4f4610e
MM
3205 return error_mark_node;
3206 }
3207
d746e87d
MM
3208 size = size_in_bytes (elt_type);
3209 if (array_p)
4a84253c
FW
3210 {
3211 /* Maximum available size in bytes. Half of the address space
3212 minus the cookie size. */
807e902e
KZ
3213 offset_int max_size
3214 = wi::set_bit_in_zero <offset_int> (TYPE_PRECISION (sizetype) - 1);
4a84253c 3215 /* Maximum number of outer elements which can be allocated. */
807e902e 3216 offset_int max_outer_nelts;
4a84253c
FW
3217 tree max_outer_nelts_tree;
3218
3219 gcc_assert (TREE_CODE (size) == INTEGER_CST);
3220 cookie_size = targetm.cxx.get_cookie_size (elt_type);
3221 gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
807e902e 3222 gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size));
c65cb8d1 3223 /* Unconditionally subtract the cookie size. This decreases the
4a84253c
FW
3224 maximum object size and is safe even if we choose not to use
3225 a cookie after all. */
807e902e 3226 max_size -= wi::to_offset (cookie_size);
4a669ac3 3227 wi::overflow_type overflow;
807e902e
KZ
3228 inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED,
3229 &overflow);
3230 if (overflow || wi::gtu_p (inner_size, max_size))
4a84253c
FW
3231 {
3232 if (complain & tf_error)
28a8cef1
MS
3233 {
3234 cst_size_error error;
3235 if (overflow)
3236 error = cst_size_overflow;
3237 else
3238 {
3239 error = cst_size_too_big;
3240 size = size_binop (MULT_EXPR, size,
3241 wide_int_to_tree (sizetype,
3242 inner_nelts_count));
3243 size = cp_fully_fold (size);
3244 }
3245 invalid_array_size_error (input_location, error, size,
3246 /*name=*/NULL_TREE);
3247 }
4a84253c
FW
3248 return error_mark_node;
3249 }
807e902e
KZ
3250
3251 max_outer_nelts = wi::udiv_trunc (max_size, inner_size);
807e902e 3252 max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts);
4a84253c 3253
cda0a029 3254 size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts));
685c8340 3255
ea219e6e 3256 if (TREE_CODE (cst_outer_nelts) == INTEGER_CST)
685c8340 3257 {
ea219e6e 3258 if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts))
685c8340
MS
3259 {
3260 /* When the array size is constant, check it at compile time
3261 to make sure it doesn't exceed the implementation-defined
3262 maximum, as required by C++ 14 (in C++ 11 this requirement
3263 isn't explicitly stated but it's enforced anyway -- see
e53b6e56 3264 grokdeclarator in cp/decl.cc). */
685c8340 3265 if (complain & tf_error)
28a8cef1
MS
3266 {
3267 size = cp_fully_fold (size);
3268 invalid_array_size_error (input_location, cst_size_too_big,
3269 size, NULL_TREE);
3270 }
685c8340
MS
3271 return error_mark_node;
3272 }
3273 }
3274 else
3275 {
3276 /* When a runtime check is necessary because the array size
3277 isn't constant, keep only the top-most seven bits (starting
3278 with the most significant non-zero bit) of the maximum size
3279 to compare the array size against, to simplify encoding the
3280 constant maximum size in the instruction stream. */
3281
3282 unsigned shift = (max_outer_nelts.get_precision ()) - 7
3283 - wi::clz (max_outer_nelts);
8de73453 3284 max_outer_nelts = (max_outer_nelts >> shift) << shift;
685c8340
MS
3285
3286 outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
3287 outer_nelts,
3288 max_outer_nelts_tree);
3289 }
4a84253c 3290 }
a28e3c7f 3291
af63ba4b
JM
3292 tree align_arg = NULL_TREE;
3293 if (type_has_new_extended_alignment (elt_type))
19b87a06
JM
3294 {
3295 unsigned align = TYPE_ALIGN_UNIT (elt_type);
3296 /* Also consider the alignment of the cookie, if any. */
164c6a1c 3297 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
19b87a06
JM
3298 align = MAX (align, TYPE_ALIGN_UNIT (size_type_node));
3299 align_arg = build_int_cst (align_type_node, align);
3300 }
af63ba4b 3301
63c9a190
MM
3302 alloc_fn = NULL_TREE;
3303
c166b898
ILT
3304 /* If PLACEMENT is a single simple pointer type not passed by
3305 reference, prepare to capture it in a temporary variable. Do
3306 this now, since PLACEMENT will change in the calls below. */
c166b898 3307 placement_first = NULL_TREE;
9771b263 3308 if (vec_safe_length (*placement) == 1
50e10fa8 3309 && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
9771b263 3310 placement_first = (**placement)[0];
c166b898 3311
e2f5cc96
MS
3312 bool member_new_p = false;
3313
e92cc029 3314 /* Allocate the object. */
bfecd57c
JJ
3315 tree fnname;
3316 tree fns;
6de9cd9a 3317
88a819be 3318 fnname = ovl_op_identifier (false, array_p ? VEC_NEW_EXPR : NEW_EXPR);
71b71b96 3319
bfecd57c
JJ
3320 member_new_p = !globally_qualified_p
3321 && CLASS_TYPE_P (elt_type)
3322 && (array_p
3323 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
3324 : TYPE_HAS_NEW_OPERATOR (elt_type));
a3d536f1 3325
6cd51207
JM
3326 bool member_delete_p = (!globally_qualified_p
3327 && CLASS_TYPE_P (elt_type)
3328 && (array_p
3329 ? TYPE_GETS_VEC_DELETE (elt_type)
3330 : TYPE_GETS_REG_DELETE (elt_type)));
3331
bfecd57c
JJ
3332 if (member_new_p)
3333 {
3334 /* Use a class-specific operator new. */
3335 /* If a cookie is required, add some extra space. */
3336 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
3337 size = size_binop (PLUS_EXPR, size, cookie_size);
3338 else
b1e5b86c 3339 {
bfecd57c
JJ
3340 cookie_size = NULL_TREE;
3341 /* No size arithmetic necessary, so the size check is
3342 not needed. */
3343 if (outer_nelts_check != NULL && inner_size == 1)
3344 outer_nelts_check = NULL_TREE;
3345 }
3346 /* Perform the overflow check. */
3347 tree errval = TYPE_MAX_VALUE (sizetype);
3348 if (cxx_dialect >= cxx11 && flag_exceptions)
3349 errval = throw_bad_array_new_length ();
3350 if (outer_nelts_check != NULL_TREE)
3351 size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
3352 size, errval);
3353 /* Create the argument list. */
3354 vec_safe_insert (*placement, 0, size);
3355 /* Do name-lookup to find the appropriate operator. */
098cf31a 3356 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2, complain);
bfecd57c 3357 if (fns == NULL_TREE)
b1e5b86c 3358 {
bfecd57c
JJ
3359 if (complain & tf_error)
3360 error ("no suitable %qD found in class %qT", fnname, elt_type);
6961a592
GB
3361 return error_mark_node;
3362 }
bfecd57c 3363 if (TREE_CODE (fns) == TREE_LIST)
dd125026
JJ
3364 {
3365 if (complain & tf_error)
bfecd57c
JJ
3366 {
3367 error ("request for member %qD is ambiguous", fnname);
3368 print_candidates (fns);
3369 }
dd125026
JJ
3370 return error_mark_node;
3371 }
bfecd57c
JJ
3372 tree dummy = build_dummy_object (elt_type);
3373 alloc_call = NULL_TREE;
3374 if (align_arg)
3375 {
3376 vec<tree, va_gc> *align_args
3377 = vec_copy_and_insert (*placement, align_arg, 1);
3378 alloc_call
3379 = build_new_method_call (dummy, fns, &align_args,
3380 /*conversion_path=*/NULL_TREE,
3381 LOOKUP_NORMAL, &alloc_fn, tf_none);
3382 /* If no matching function is found and the allocated object type
3383 has new-extended alignment, the alignment argument is removed
3384 from the argument list, and overload resolution is performed
3385 again. */
3386 if (alloc_call == error_mark_node)
3387 alloc_call = NULL_TREE;
3388 }
3389 if (!alloc_call)
3390 alloc_call = build_new_method_call (dummy, fns, placement,
3391 /*conversion_path=*/NULL_TREE,
3392 LOOKUP_NORMAL,
3393 &alloc_fn, complain);
360f866c 3394 }
8d08fdba
MS
3395 else
3396 {
bfecd57c
JJ
3397 /* Use a global operator new. */
3398 /* See if a cookie might be required. */
3399 if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
089d6ea7 3400 {
bfecd57c
JJ
3401 cookie_size = NULL_TREE;
3402 /* No size arithmetic necessary, so the size check is
3403 not needed. */
3404 if (outer_nelts_check != NULL && inner_size == 1)
3405 outer_nelts_check = NULL_TREE;
089d6ea7 3406 }
125e6594 3407
0a0c2c3f
JJ
3408 /* If size is zero e.g. due to type having zero size, try to
3409 preserve outer_nelts for constant expression evaluation
3410 purposes. */
3411 if (integer_zerop (size) && outer_nelts)
3412 size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts);
3413
bfecd57c
JJ
3414 alloc_call = build_operator_new_call (fnname, placement,
3415 &size, &cookie_size,
3416 align_arg, outer_nelts_check,
3417 &alloc_fn, complain);
8d08fdba
MS
3418 }
3419
96790071 3420 if (alloc_call == error_mark_node)
2bb5d995
JM
3421 return error_mark_node;
3422
63c9a190
MM
3423 gcc_assert (alloc_fn != NULL_TREE);
3424
2ec69f56
JM
3425 /* Now, check to see if this function is actually a placement
3426 allocation function. This can happen even when PLACEMENT is NULL
3427 because we might have something like:
3428
3429 struct S { void* operator new (size_t, int i = 0); };
3430
3431 A call to `new S' will get this allocation function, even though
3432 there is no explicit placement argument. If there is more than
3433 one argument, or there are variable arguments, then this is a
3434 placement allocation function. */
3435 placement_allocation_fn_p
3436 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
3437 || varargs_function_p (alloc_fn));
3438
f2da9e26
NS
3439 if (complain & tf_warning_or_error
3440 && warn_aligned_new
2ec69f56
JM
3441 && !placement_allocation_fn_p
3442 && TYPE_ALIGN (elt_type) > malloc_alignment ()
af63ba4b
JM
3443 && (warn_aligned_new > 1
3444 || CP_DECL_CONTEXT (alloc_fn) == global_namespace)
3445 && !aligned_allocation_fn_p (alloc_fn))
3446 {
097f82ec 3447 auto_diagnostic_group d;
34d57a10
JW
3448 if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended "
3449 "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type)))
3450 {
3451 inform (input_location, "uses %qD, which does not have an alignment "
3452 "parameter", alloc_fn);
3453 if (!aligned_new_threshold)
3454 inform (input_location, "use %<-faligned-new%> to enable C++17 "
3455 "over-aligned new support");
3456 }
af63ba4b
JM
3457 }
3458
c166b898
ILT
3459 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
3460 into a temporary variable. */
a9de800a 3461 if (!processing_template_decl
a9de800a
JJ
3462 && TREE_CODE (alloc_call) == CALL_EXPR
3463 && call_expr_nargs (alloc_call) == 2
3464 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
50e10fa8 3465 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
a9de800a 3466 {
e2f5cc96 3467 tree placement = CALL_EXPR_ARG (alloc_call, 1);
a9de800a 3468
e2f5cc96
MS
3469 if (placement_first != NULL_TREE
3470 && (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))
3471 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))))
a9de800a 3472 {
c166b898 3473 placement_expr = get_target_expr (placement_first);
a9de800a 3474 CALL_EXPR_ARG (alloc_call, 1)
cda0a029 3475 = fold_convert (TREE_TYPE (placement), placement_expr);
e2f5cc96
MS
3476 }
3477
3478 if (!member_new_p
3479 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1)))))
3480 {
3481 /* Attempt to make the warning point at the operator new argument. */
3482 if (placement_first)
3483 placement = placement_first;
3484
3485 warn_placement_new_too_small (orig_type, nelts, size, placement);
a9de800a
JJ
3486 }
3487 }
3488
19b87a06 3489 alloc_expr = alloc_call;
8e007055 3490 if (cookie_size)
19b87a06 3491 alloc_expr = maybe_wrap_new_for_constexpr (alloc_expr, type,
8e007055
JJ
3492 cookie_size);
3493
a6111661
JM
3494 /* In the simple case, we can stop now. */
3495 pointer_type = build_pointer_type (type);
6cd51207 3496 if (!cookie_size && !is_initialized && !member_delete_p)
19b87a06 3497 return build_nop (pointer_type, alloc_expr);
a6111661 3498
10ee5386
JM
3499 /* Store the result of the allocation call in a variable so that we can
3500 use it more than once. */
19b87a06 3501 alloc_expr = get_target_expr (alloc_expr);
a6111661
JM
3502 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
3503
3504 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
c8094d83 3505 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
a6111661 3506 alloc_call = TREE_OPERAND (alloc_call, 1);
089d6ea7 3507
a6111661
JM
3508 /* Preevaluate the placement args so that we don't reevaluate them for a
3509 placement delete. */
3510 if (placement_allocation_fn_p)
3511 {
6de9cd9a
DN
3512 tree inits;
3513 stabilize_call (alloc_call, &inits);
a6111661 3514 if (inits)
f293ce4b
RS
3515 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
3516 alloc_expr);
a6111661
JM
3517 }
3518
047f64a3
JM
3519 /* unless an allocation function is declared with an empty excep-
3520 tion-specification (_except.spec_), throw(), it indicates failure to
3521 allocate storage by throwing a bad_alloc exception (clause _except_,
3522 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
3523 cation function is declared with an empty exception-specification,
3524 throw(), it returns null to indicate failure to allocate storage and a
3525 non-null pointer otherwise.
3526
3527 So check for a null exception spec on the op new we just called. */
3528
46ff5047 3529 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
8bee092e
JJ
3530 check_new
3531 = flag_check_new || (nothrow && !std_placement_new_fn_p (alloc_fn));
047f64a3 3532
089d6ea7 3533 if (cookie_size)
8d08fdba 3534 {
96790071 3535 tree cookie;
46e995e0 3536 tree cookie_ptr;
b5119fa1 3537 tree size_ptr_type;
f4f4610e
MM
3538
3539 /* Adjust so we're pointing to the start of the object. */
5d49b6a7 3540 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
96790071 3541
834c6dff 3542 /* Store the number of bytes allocated so that we can know how
3461fba7
NS
3543 many elements to destroy later. We use the last sizeof
3544 (size_t) bytes to store the number of elements. */
10ee5386 3545 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
5d49b6a7
RG
3546 cookie_ptr = fold_build_pointer_plus_loc (input_location,
3547 alloc_node, cookie_ptr);
b5119fa1 3548 size_ptr_type = build_pointer_type (sizetype);
10ee5386 3549 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
04757a2a 3550 cookie = cp_build_fold_indirect_ref (cookie_ptr);
1f84ec23 3551
f293ce4b 3552 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
46e995e0
PB
3553
3554 if (targetm.cxx.cookie_has_size ())
3555 {
3556 /* Also store the element size. */
5d49b6a7 3557 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
db3927fb 3558 fold_build1_loc (input_location,
5d49b6a7
RG
3559 NEGATE_EXPR, sizetype,
3560 size_in_bytes (sizetype)));
b2ec1738 3561
04757a2a 3562 cookie = cp_build_fold_indirect_ref (cookie_ptr);
f293ce4b 3563 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
10ee5386 3564 size_in_bytes (elt_type));
f293ce4b
RS
3565 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
3566 cookie, cookie_expr);
46e995e0 3567 }
8d08fdba 3568 }
96790071 3569 else
8b5e2ce4
JM
3570 {
3571 cookie_expr = NULL_TREE;
3572 data_addr = alloc_node;
3573 }
8d08fdba 3574
10ee5386 3575 /* Now use a pointer to the type we've actually allocated. */
25357d1e
JM
3576
3577 /* But we want to operate on a non-const version to start with,
3578 since we'll be modifying the elements. */
3579 non_const_pointer_type = build_pointer_type
a3360e77 3580 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
25357d1e
JM
3581
3582 data_addr = fold_convert (non_const_pointer_type, data_addr);
9207099b 3583 /* Any further uses of alloc_node will want this type, too. */
25357d1e 3584 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
10ee5386 3585
6de9cd9a
DN
3586 /* Now initialize the allocated object. Note that we preevaluate the
3587 initialization expression, apart from the actual constructor call or
3588 assignment--we do this because we want to delay the allocation as long
3589 as possible in order to minimize the size of the exception region for
3590 placement delete. */
f4f4610e 3591 if (is_initialized)
8d08fdba 3592 {
844ae01d 3593 bool explicit_value_init_p = false;
6de9cd9a 3594
9771b263 3595 if (*init != NULL && (*init)->is_empty ())
6de9cd9a 3596 {
c166b898 3597 *init = NULL;
844ae01d
JM
3598 explicit_value_init_p = true;
3599 }
b84f4651 3600
38e10026 3601 if (processing_template_decl)
a67e7daa 3602 {
38e10026
MP
3603 /* Avoid an ICE when converting to a base in build_simple_base_path.
3604 We'll throw this all away anyway, and build_new will create
3605 a NEW_EXPR. */
3606 tree t = fold_convert (build_pointer_type (elt_type), data_addr);
a67e7daa
JM
3607 /* build_value_init doesn't work in templates, and we don't need
3608 the initializer anyway since we're going to throw it away and
3609 rebuild it at instantiation time, so just build up a single
3610 constructor call to get any appropriate diagnostics. */
38e10026 3611 init_expr = cp_build_fold_indirect_ref (t);
95552437 3612 if (type_build_ctor_call (elt_type))
a67e7daa
JM
3613 init_expr = build_special_member_call (init_expr,
3614 complete_ctor_identifier,
3615 init, elt_type,
3616 LOOKUP_NORMAL,
3617 complain);
a67e7daa
JM
3618 }
3619 else if (array_p)
844ae01d 3620 {
25357d1e 3621 tree vecinit = NULL_TREE;
73a2b8dd
MP
3622 const size_t len = vec_safe_length (*init);
3623 if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0]))
25357d1e 3624 {
9771b263 3625 vecinit = (**init)[0];
1f65a8c8
JM
3626 if (CONSTRUCTOR_NELTS (vecinit) == 0)
3627 /* List-value-initialization, leave it alone. */;
25357d1e
JM
3628 else
3629 {
1f65a8c8
JM
3630 tree arraytype, domain;
3631 if (TREE_CONSTANT (nelts))
3632 domain = compute_array_index_type (NULL_TREE, nelts,
3633 complain);
3634 else
7d5e76c8
JM
3635 /* We'll check the length at runtime. */
3636 domain = NULL_TREE;
1f65a8c8 3637 arraytype = build_cplus_array_type (type, domain);
b1c59b31
MP
3638 /* If we have new char[4]{"foo"}, we have to reshape
3639 so that the STRING_CST isn't wrapped in { }. */
3640 vecinit = reshape_init (arraytype, vecinit, complain);
3641 /* The middle end doesn't cope with the location wrapper
3642 around a STRING_CST. */
3643 STRIP_ANY_LOCATION_WRAPPER (vecinit);
1f65a8c8 3644 vecinit = digest_init (arraytype, vecinit, complain);
25357d1e 3645 }
25357d1e
JM
3646 }
3647 else if (*init)
5ade1ed2
DG
3648 {
3649 if (complain & tf_error)
3ec16e36
AO
3650 error ("parenthesized initializer in array new");
3651 return error_mark_node;
5ade1ed2 3652 }
6de9cd9a 3653 init_expr
b73a4704
JM
3654 = build_vec_init (data_addr,
3655 cp_build_binary_op (input_location,
3656 MINUS_EXPR, outer_nelts,
3657 integer_one_node,
3658 complain),
3659 vecinit,
3660 explicit_value_init_p,
3661 /*from_array=*/0,
3662 complain);
6de9cd9a 3663 }
f30efcb7 3664 else
8d08fdba 3665 {
04757a2a 3666 init_expr = cp_build_fold_indirect_ref (data_addr);
9207099b 3667
95552437 3668 if (type_build_ctor_call (type) && !explicit_value_init_p)
b84f4651
MM
3669 {
3670 init_expr = build_special_member_call (init_expr,
3671 complete_ctor_identifier,
3672 init, elt_type,
5ade1ed2 3673 LOOKUP_NORMAL,
0e5def81 3674 complain|tf_no_cleanup);
844ae01d
JM
3675 }
3676 else if (explicit_value_init_p)
3677 {
1d7bc790
NS
3678 /* Something like `new int()'. NO_CLEANUP is needed so
3679 we don't try and build a (possibly ill-formed)
3680 destructor. */
3681 tree val = build_value_init (type, complain | tf_no_cleanup);
a67e7daa
JM
3682 if (val == error_mark_node)
3683 return error_mark_node;
6ffbf87c 3684 init_expr = cp_build_init_expr (init_expr, val);
b84f4651 3685 }
8dc2b103 3686 else
b84f4651 3687 {
c166b898
ILT
3688 tree ie;
3689
b84f4651 3690 /* We are processing something like `new int (10)', which
c20f7e99 3691 means allocate an int, and initialize it with 10.
3db45ab5 3692
c20f7e99 3693 In C++20, also handle `new A(1, 2)'. */
b04445d4 3694 if (cxx_dialect >= cxx20
c20f7e99
MP
3695 && AGGREGATE_TYPE_P (type)
3696 && (*init)->length () > 1)
3697 {
73a2b8dd 3698 ie = build_constructor_from_vec (init_list_type_node, *init);
c20f7e99
MP
3699 CONSTRUCTOR_IS_DIRECT_INIT (ie) = true;
3700 CONSTRUCTOR_IS_PAREN_INIT (ie) = true;
3701 ie = digest_init (type, ie, complain);
3702 }
3703 else
3704 ie = build_x_compound_expr_from_vec (*init, "new initializer",
3705 complain);
4f2e1536
MP
3706 init_expr = cp_build_modify_expr (input_location, init_expr,
3707 INIT_EXPR, ie, complain);
b84f4651 3708 }
817a77e4
JM
3709 /* If the initializer uses C++14 aggregate NSDMI that refer to the
3710 object being initialized, replace them now and don't try to
3711 preevaluate. */
3712 bool had_placeholder = false;
2166aeb3 3713 if (!processing_template_decl
817a77e4
JM
3714 && TREE_CODE (init_expr) == INIT_EXPR)
3715 TREE_OPERAND (init_expr, 1)
3716 = replace_placeholders (TREE_OPERAND (init_expr, 1),
3717 TREE_OPERAND (init_expr, 0),
3718 &had_placeholder);
96790071
JM
3719 }
3720
3721 if (init_expr == error_mark_node)
3722 return error_mark_node;
6cd51207
JM
3723 }
3724 else
3725 init_expr = NULL_TREE;
1f109f0f 3726
6cd51207
JM
3727 /* If any part of the object initialization terminates by throwing an
3728 exception and a suitable deallocation function can be found, the
3729 deallocation function is called to free the memory in which the
3730 object was being constructed, after which the exception continues
3731 to propagate in the context of the new-expression. If no
3732 unambiguous matching deallocation function can be found,
3733 propagating the exception does not cause the object's memory to be
3734 freed. */
3735 if (flag_exceptions && (init_expr || member_delete_p))
3736 {
3737 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
3738 tree cleanup;
3739
3740 /* The Standard is unclear here, but the right thing to do
3741 is to use the same method for finding deallocation
3742 functions that we use for finding allocation functions. */
3743 cleanup = (build_op_delete_call
3744 (dcode,
3745 alloc_node,
3746 size,
3747 globally_qualified_p,
3748 placement_allocation_fn_p ? alloc_call : NULL_TREE,
3749 alloc_fn,
3750 complain));
3751
3752 if (cleanup && init_expr && !processing_template_decl)
3753 /* Ack! First we allocate the memory. Then we set our sentry
3754 variable to true, and expand a cleanup that deletes the
3755 memory if sentry is true. Then we run the constructor, and
3756 finally clear the sentry.
3757
3758 We need to do this because we allocate the space first, so
3759 if there are any temporaries with cleanups in the
3760 constructor args, we need this EH region to extend until
3761 end of full-expression to preserve nesting.
3762
3763 We used to try to evaluate the args first to avoid this, but
3764 since C++17 [expr.new] says that "The invocation of the
3765 allocation function is sequenced before the evaluations of
3766 expressions in the new-initializer." */
1f109f0f 3767 {
6cd51207 3768 tree end, sentry, begin;
2face519 3769
6cd51207
JM
3770 begin = get_target_expr (boolean_true_node);
3771 CLEANUP_EH_ONLY (begin) = 1;
2face519 3772
6cd51207 3773 sentry = TARGET_EXPR_SLOT (begin);
659e5a7a 3774
6cd51207
JM
3775 /* CLEANUP is compiler-generated, so no diagnostics. */
3776 suppress_warning (cleanup);
d665b6e5 3777
6cd51207
JM
3778 TARGET_EXPR_CLEANUP (begin)
3779 = build3 (COND_EXPR, void_type_node, sentry,
3780 cleanup, void_node);
2face519 3781
6cd51207
JM
3782 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
3783 sentry, boolean_false_node);
2face519 3784
6cd51207
JM
3785 init_expr
3786 = build2 (COMPOUND_EXPR, void_type_node, begin,
3787 build2 (COMPOUND_EXPR, void_type_node, init_expr,
3788 end));
3789 /* Likewise, this is compiler-generated. */
3790 suppress_warning (init_expr);
1f109f0f 3791 }
f4f4610e 3792 }
8b5e2ce4
JM
3793
3794 /* Now build up the return value in reverse order. */
96790071 3795
8b5e2ce4 3796 rval = data_addr;
2face519 3797
8b5e2ce4 3798 if (init_expr)
f293ce4b 3799 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
8b5e2ce4 3800 if (cookie_expr)
f293ce4b 3801 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
8b5e2ce4 3802
e3585e6a
MP
3803 suppress_warning (rval, OPT_Wunused_value);
3804
847a8301 3805 if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
8b5e2ce4
JM
3806 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
3807 and return the call (which doesn't need to be adjusted). */
3808 rval = TARGET_EXPR_INITIAL (alloc_expr);
3809 else
d18c083e 3810 {
8b5e2ce4
JM
3811 if (check_new)
3812 {
ba47d38d
AH
3813 tree ifexp = cp_build_binary_op (input_location,
3814 NE_EXPR, alloc_node,
6d96d7ff 3815 nullptr_node,
5ade1ed2 3816 complain);
4cbc4bd7
PC
3817 rval = build_conditional_expr (input_location, ifexp, rval,
3818 alloc_node, complain);
8b5e2ce4 3819 }
d18c083e 3820
8b5e2ce4
JM
3821 /* Perform the allocation before anything else, so that ALLOC_NODE
3822 has been initialized before we start using it. */
f293ce4b 3823 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
8b5e2ce4 3824 }
51c184be 3825
d04a575f 3826 /* A new-expression is never an lvalue. */
bb19d4af 3827 gcc_assert (!obvalue_p (rval));
058dcc25 3828
25357d1e 3829 return convert (pointer_type, rval);
8d08fdba 3830}
63c9a190 3831
c166b898
ILT
3832/* Generate a representation for a C++ "new" expression. *PLACEMENT
3833 is a vector of placement-new arguments (or NULL if none). If NELTS
3834 is NULL, TYPE is the type of the storage to be allocated. If NELTS
3835 is not NULL, then this is an array-new allocation; TYPE is the type
3836 of the elements in the array and NELTS is the number of elements in
3837 the array. *INIT, if non-NULL, is the initializer for the new
3838 object, or an empty vector to indicate an initializer of "()". If
3839 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
3840 rather than just "new". This may change PLACEMENT and INIT. */
63c9a190
MM
3841
3842tree
87d3f828
PC
3843build_new (location_t loc, vec<tree, va_gc> **placement, tree type,
3844 tree nelts, vec<tree, va_gc> **init, int use_global_new,
3845 tsubst_flags_t complain)
63c9a190
MM
3846{
3847 tree rval;
9771b263 3848 vec<tree, va_gc> *orig_placement = NULL;
c166b898 3849 tree orig_nelts = NULL_TREE;
9771b263 3850 vec<tree, va_gc> *orig_init = NULL;
63c9a190 3851
c166b898 3852 if (type == error_mark_node)
63c9a190
MM
3853 return error_mark_node;
3854
9a642cca 3855 if (nelts == NULL_TREE
c19267cb
JM
3856 /* Don't do auto deduction where it might affect mangling. */
3857 && (!processing_template_decl || at_function_scope_p ()))
86a09a9e
JM
3858 {
3859 tree auto_node = type_uses_auto (type);
2e5748d2
JM
3860 if (auto_node)
3861 {
9a642cca 3862 tree d_init = NULL_TREE;
009bb506
MP
3863 const size_t len = vec_safe_length (*init);
3864 /* E.g. new auto(x) must have exactly one element, or
3865 a {} initializer will have one element. */
3866 if (len == 1)
9a642cca
JM
3867 {
3868 d_init = (**init)[0];
3869 d_init = resolve_nondeduced_context (d_init, complain);
3870 }
009bb506
MP
3871 /* For the rest, e.g. new A(1, 2, 3), create a list. */
3872 else if (len > 1)
3873 {
3874 unsigned int n;
3875 tree t;
3876 tree *pp = &d_init;
3877 FOR_EACH_VEC_ELT (**init, n, t)
3878 {
3879 t = resolve_nondeduced_context (t, complain);
3880 *pp = build_tree_list (NULL_TREE, t);
3881 pp = &TREE_CHAIN (*pp);
3882 }
3883 }
87ca4015 3884 type = do_auto_deduction (type, d_init, auto_node, complain);
2e5748d2 3885 }
86a09a9e
JM
3886 }
3887
63c9a190
MM
3888 if (processing_template_decl)
3889 {
3890 if (dependent_type_p (type)
c166b898 3891 || any_type_dependent_arguments_p (*placement)
63c9a190 3892 || (nelts && type_dependent_expression_p (nelts))
879b0a1d 3893 || (nelts && *init)
c166b898 3894 || any_type_dependent_arguments_p (*init))
87d3f828 3895 return build_raw_new_expr (loc, *placement, type, nelts, *init,
63c9a190 3896 use_global_new);
c166b898
ILT
3897
3898 orig_placement = make_tree_vector_copy (*placement);
3899 orig_nelts = nelts;
a4bbf910 3900 if (*init)
817a77e4
JM
3901 {
3902 orig_init = make_tree_vector_copy (*init);
3903 /* Also copy any CONSTRUCTORs in *init, since reshape_init and
3904 digest_init clobber them in place. */
3905 for (unsigned i = 0; i < orig_init->length(); ++i)
3906 {
3907 tree e = (**init)[i];
3908 if (TREE_CODE (e) == CONSTRUCTOR)
3909 (**init)[i] = copy_node (e);
3910 }
3911 }
c166b898
ILT
3912
3913 make_args_non_dependent (*placement);
63c9a190
MM
3914 if (nelts)
3915 nelts = build_non_dependent_expr (nelts);
c166b898 3916 make_args_non_dependent (*init);
63c9a190
MM
3917 }
3918
3919 if (nelts)
3920 {
87d3f828 3921 location_t nelts_loc = cp_expr_loc_or_loc (nelts, loc);
63c9a190 3922 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
5ade1ed2
DG
3923 {
3924 if (complain & tf_error)
87d3f828 3925 permerror (nelts_loc,
f5fc32e4 3926 "size in array new must have integral type");
5ade1ed2
DG
3927 else
3928 return error_mark_node;
3929 }
685c8340
MS
3930
3931 /* Try to determine the constant value only for the purposes
3932 of the diagnostic below but continue to use the original
3933 value and handle const folding later. */
e56f6629 3934 const_tree cst_nelts = fold_non_dependent_expr (nelts, complain);
685c8340
MS
3935
3936 /* The expression in a noptr-new-declarator is erroneous if it's of
3937 non-class type and its value before converting to std::size_t is
3938 less than zero. ... If the expression is a constant expression,
3939 the program is ill-fomed. */
ea219e6e 3940 if (TREE_CODE (cst_nelts) == INTEGER_CST
87d3f828 3941 && !valid_array_size_p (nelts_loc, cst_nelts, NULL_TREE,
28a8cef1
MS
3942 complain & tf_error))
3943 return error_mark_node;
685c8340 3944
03a904b5 3945 nelts = mark_rvalue_use (nelts);
4b978f96 3946 nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
63c9a190
MM
3947 }
3948
3949 /* ``A reference cannot be created by the new operator. A reference
3950 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3951 returned by new.'' ARM 5.3.3 */
9f613f06 3952 if (TYPE_REF_P (type))
63c9a190 3953 {
5ade1ed2 3954 if (complain & tf_error)
87d3f828 3955 error_at (loc, "new cannot be applied to a reference type");
5ade1ed2
DG
3956 else
3957 return error_mark_node;
63c9a190
MM
3958 type = TREE_TYPE (type);
3959 }
3960
3961 if (TREE_CODE (type) == FUNCTION_TYPE)
3962 {
5ade1ed2 3963 if (complain & tf_error)
87d3f828 3964 error_at (loc, "new cannot be applied to a function type");
63c9a190
MM
3965 return error_mark_node;
3966 }
3967
73a2b8dd 3968 /* P1009: Array size deduction in new-expressions. */
81de459e 3969 const bool array_p = TREE_CODE (type) == ARRAY_TYPE;
ae48b74c
MP
3970 if (*init
3971 /* If ARRAY_P, we have to deduce the array bound. For C++20 paren-init,
3972 we have to process the parenthesized-list. But don't do it for (),
3973 which is value-initialization, and INIT should stay empty. */
3974 && (array_p || (cxx_dialect >= cxx20 && nelts && !(*init)->is_empty ())))
73a2b8dd
MP
3975 {
3976 /* This means we have 'new T[]()'. */
3977 if ((*init)->is_empty ())
3978 {
3979 tree ctor = build_constructor (init_list_type_node, NULL);
3980 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
3981 vec_safe_push (*init, ctor);
3982 }
3983 tree &elt = (**init)[0];
3984 /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */
3985 if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20)
3986 {
81de459e
MP
3987 tree ctor = build_constructor_from_vec (init_list_type_node, *init);
3988 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
3989 CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
3990 elt = ctor;
3991 /* We've squashed all the vector elements into the first one;
3992 truncate the rest. */
3993 (*init)->truncate (1);
73a2b8dd
MP
3994 }
3995 /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */
81de459e
MP
3996 if (array_p && !TYPE_DOMAIN (type))
3997 {
3998 /* We need to reshape before deducing the bounds to handle code like
3999
4000 struct S { int x, y; };
4001 new S[]{1, 2, 3, 4};
4002
4003 which should deduce S[2]. But don't change ELT itself: we want to
4004 pass a list-initializer to build_new_1, even for STRING_CSTs. */
4005 tree e = elt;
4006 if (BRACE_ENCLOSED_INITIALIZER_P (e))
4007 e = reshape_init (type, e, complain);
4008 cp_complete_array_type (&type, e, /*do_default*/false);
4009 }
73a2b8dd
MP
4010 }
4011
57ccb546
MM
4012 /* The type allocated must be complete. If the new-type-id was
4013 "T[N]" then we are just checking that "T" is complete here, but
4014 that is equivalent, since the value of "N" doesn't matter. */
309714d4 4015 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
39fb9d72
DB
4016 return error_mark_node;
4017
5ade1ed2 4018 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
63c9a190
MM
4019 if (rval == error_mark_node)
4020 return error_mark_node;
4021
4022 if (processing_template_decl)
c166b898 4023 {
87d3f828 4024 tree ret = build_raw_new_expr (loc, orig_placement, type, orig_nelts,
c166b898
ILT
4025 orig_init, use_global_new);
4026 release_tree_vector (orig_placement);
4027 release_tree_vector (orig_init);
4028 return ret;
4029 }
63c9a190
MM
4030
4031 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
87d3f828 4032 rval = build1_loc (loc, NOP_EXPR, TREE_TYPE (rval), rval);
65870e75 4033 suppress_warning (rval, OPT_Wunused_value);
63c9a190
MM
4034
4035 return rval;
4036}
8d08fdba 4037\f
f30432d7 4038static tree
04e4997a 4039build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
574cfaa4 4040 special_function_kind auto_delete_vec,
2fbc4548
JM
4041 int use_global_delete, tsubst_flags_t complain,
4042 bool in_cleanup = false)
f30432d7
MS
4043{
4044 tree virtual_size;
e92cc029 4045 tree ptype = build_pointer_type (type = complete_type (type));
c9b0866a 4046 tree size_exp;
f30432d7
MS
4047
4048 /* Temporary variables used by the loop. */
4049 tree tbase, tbase_init;
4050
4051 /* This is the body of the loop that implements the deletion of a
4052 single element, and moves temp variables to next elements. */
4053 tree body;
4054
4055 /* This is the LOOP_EXPR that governs the deletion of the elements. */
c7b62f14 4056 tree loop = 0;
f30432d7
MS
4057
4058 /* This is the thing that governs what to do after the loop has run. */
4059 tree deallocate_expr = 0;
4060
4061 /* This is the BIND_EXPR which holds the outermost iterator of the
4062 loop. It is convenient to set this variable up and test it before
4063 executing any other code in the loop.
4064 This is also the containing expression returned by this function. */
4065 tree controller = NULL_TREE;
5be014d5 4066 tree tmp;
f30432d7 4067
b2153b98 4068 /* We should only have 1-D arrays here. */
8dc2b103 4069 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
b2153b98 4070
574cfaa4
JM
4071 if (base == error_mark_node || maxindex == error_mark_node)
4072 return error_mark_node;
4073
04e4997a 4074 if (!verify_type_context (loc, TCTX_DEALLOCATION, type,
02a32ab4
RS
4075 !(complain & tf_error)))
4076 return error_mark_node;
4077
c9b0866a
PC
4078 if (!COMPLETE_TYPE_P (type))
4079 {
097f82ec
DM
4080 if (complain & tf_warning)
4081 {
4082 auto_diagnostic_group d;
04e4997a
PC
4083 if (warning_at (loc, OPT_Wdelete_incomplete,
4084 "possible problem detected in invocation of "
4085 "operator %<delete []%>"))
097f82ec
DM
4086 {
4087 cxx_incomplete_type_diagnostic (base, type, DK_WARNING);
04e4997a
PC
4088 inform (loc, "neither the destructor nor the "
4089 "class-specific operator %<delete []%> will be called, "
4090 "even if they are declared when the class is defined");
097f82ec
DM
4091 }
4092 }
20b06add
JM
4093 /* This size won't actually be used. */
4094 size_exp = size_one_node;
4095 goto no_destructor;
c9b0866a
PC
4096 }
4097
4098 size_exp = size_in_bytes (type);
4099
eca7fc57 4100 if (! MAYBE_CLASS_TYPE_P (type))
c7b62f14 4101 goto no_destructor;
eca7fc57
JM
4102 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
4103 {
4104 /* Make sure the destructor is callable. */
4105 if (type_build_dtor_call (type))
4106 {
04e4997a 4107 tmp = build_delete (loc, ptype, base, sfk_complete_destructor,
eca7fc57
JM
4108 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
4109 complain);
4110 if (tmp == error_mark_node)
4111 return error_mark_node;
4112 }
4113 goto no_destructor;
4114 }
f30432d7 4115
708cae97 4116 /* The below is short by the cookie size. */
fed3cef0 4117 virtual_size = size_binop (MULT_EXPR, size_exp,
cda0a029 4118 fold_convert (sizetype, maxindex));
f30432d7 4119
46e8c075 4120 tbase = create_temporary_var (ptype);
8e007055 4121 DECL_INITIAL (tbase)
04e4997a 4122 = fold_build_pointer_plus_loc (loc, fold_convert (ptype, base),
8e007055 4123 virtual_size);
04e4997a 4124 tbase_init = build_stmt (loc, DECL_EXPR, tbase);
8e007055 4125 controller = build3 (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
f30432d7 4126 TREE_SIDE_EFFECTS (controller) = 1;
4c6afbbd 4127 BIND_EXPR_VEC_DTOR (controller) = true;
f30432d7 4128
f293ce4b 4129 body = build1 (EXIT_EXPR, void_type_node,
5cd88d68
RS
4130 build2 (EQ_EXPR, boolean_type_node, tbase,
4131 fold_convert (ptype, base)));
04e4997a 4132 tmp = fold_build1_loc (loc, NEGATE_EXPR, sizetype, size_exp);
5d49b6a7 4133 tmp = fold_build_pointer_plus (tbase, tmp);
04e4997a 4134 tmp = cp_build_modify_expr (loc, tbase, NOP_EXPR, tmp, complain);
574cfaa4
JM
4135 if (tmp == error_mark_node)
4136 return error_mark_node;
04e4997a
PC
4137 body = build_compound_expr (loc, body, tmp);
4138 tmp = build_delete (loc, ptype, tbase, sfk_complete_destructor,
574cfaa4
JM
4139 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
4140 complain);
4141 if (tmp == error_mark_node)
4142 return error_mark_node;
04e4997a 4143 body = build_compound_expr (loc, body, tmp);
f30432d7 4144
f293ce4b 4145 loop = build1 (LOOP_EXPR, void_type_node, body);
2fbc4548
JM
4146
4147 /* If one destructor throws, keep trying to clean up the rest, unless we're
4148 already in a build_vec_init cleanup. */
4149 if (flag_exceptions && !in_cleanup && !expr_noexcept_p (tmp, tf_none))
4150 {
4151 loop = build2 (TRY_CATCH_EXPR, void_type_node, loop,
4152 unshare_expr (loop));
4153 /* Tell honor_protect_cleanup_actions to discard this on the
4154 exceptional path. */
4155 TRY_CATCH_IS_CLEANUP (loop) = true;
4156 }
4157
04e4997a 4158 loop = build_compound_expr (loc, tbase_init, loop);
f30432d7
MS
4159
4160 no_destructor:
e79a6b40
JM
4161 /* Delete the storage if appropriate. */
4162 if (auto_delete_vec == sfk_deleting_destructor)
f30432d7
MS
4163 {
4164 tree base_tbd;
4165
708cae97 4166 /* The below is short by the cookie size. */
fed3cef0 4167 virtual_size = size_binop (MULT_EXPR, size_exp,
cda0a029 4168 fold_convert (sizetype, maxindex));
f30432d7
MS
4169
4170 if (! TYPE_VEC_NEW_USES_COOKIE (type))
4171 /* no header */
4172 base_tbd = base;
4173 else
4174 {
834c6dff
MM
4175 tree cookie_size;
4176
46e995e0 4177 cookie_size = targetm.cxx.get_cookie_size (type);
04e4997a 4178 base_tbd = cp_build_binary_op (loc,
574cfaa4
JM
4179 MINUS_EXPR,
4180 cp_convert (string_type_node,
4b978f96 4181 base, complain),
574cfaa4
JM
4182 cookie_size,
4183 complain);
4184 if (base_tbd == error_mark_node)
4185 return error_mark_node;
4b978f96 4186 base_tbd = cp_convert (ptype, base_tbd, complain);
e92cc029 4187 /* True size with header. */
834c6dff 4188 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
f30432d7 4189 }
86f45d2c 4190
e79a6b40
JM
4191 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
4192 base_tbd, virtual_size,
4193 use_global_delete & 1,
4194 /*placement=*/NULL_TREE,
4b978f96
PC
4195 /*alloc_fn=*/NULL_TREE,
4196 complain);
f30432d7
MS
4197 }
4198
c7b62f14 4199 body = loop;
4924293a
PP
4200 if (deallocate_expr == error_mark_node)
4201 return error_mark_node;
4202 else if (!deallocate_expr)
c7b62f14
NS
4203 ;
4204 else if (!body)
4205 body = deallocate_expr;
f30432d7 4206 else
4e8e460b 4207 /* The delete operator must be called, even if a destructor
ee392fc2
NS
4208 throws. */
4209 body = build2 (TRY_FINALLY_EXPR, void_type_node, body, deallocate_expr);
c8094d83 4210
c7b62f14
NS
4211 if (!body)
4212 body = integer_zero_node;
c8094d83 4213
f30432d7 4214 /* Outermost wrapper: If pointer is null, punt. */
04e4997a 4215 tree cond = build2_loc (loc, NE_EXPR, boolean_type_node, base,
03ca8fb3 4216 fold_convert (TREE_TYPE (base), nullptr_node));
03a616ac
JJ
4217 /* This is a compiler generated comparison, don't emit
4218 e.g. -Wnonnull-compare warning for it. */
65870e75 4219 suppress_warning (cond, OPT_Wnonnull_compare);
04e4997a 4220 body = build3_loc (loc, COND_EXPR, void_type_node,
03ca8fb3 4221 cond, body, integer_zero_node);
d5bcd6d4 4222 COND_EXPR_IS_VEC_DELETE (body) = true;
f30432d7
MS
4223 body = build1 (NOP_EXPR, void_type_node, body);
4224
4225 if (controller)
4226 {
4227 TREE_OPERAND (controller, 1) = body;
4e8dca1c 4228 body = controller;
f30432d7 4229 }
4e8dca1c
JM
4230
4231 if (TREE_CODE (base) == SAVE_EXPR)
4232 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
f293ce4b 4233 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
4e8dca1c 4234
574cfaa4 4235 return convert_to_void (body, ICV_CAST, complain);
f30432d7
MS
4236}
4237
c8094d83 4238/* Create an unnamed variable of the indicated TYPE. */
c395453c 4239
f1dedc31 4240tree
362efdc1 4241create_temporary_var (tree type)
8a72a046 4242{
f1dedc31 4243 tree decl;
c8094d83 4244
c2255bc4
AH
4245 decl = build_decl (input_location,
4246 VAR_DECL, NULL_TREE, type);
f1dedc31
MM
4247 TREE_USED (decl) = 1;
4248 DECL_ARTIFICIAL (decl) = 1;
f1dedc31 4249 DECL_IGNORED_P (decl) = 1;
b35d4555 4250 DECL_CONTEXT (decl) = current_function_decl;
f1dedc31 4251
f1dedc31 4252 return decl;
8a72a046
MM
4253}
4254
f1dedc31
MM
4255/* Create a new temporary variable of the indicated TYPE, initialized
4256 to INIT.
8a72a046 4257
f1dedc31
MM
4258 It is not entered into current_binding_level, because that breaks
4259 things when it comes time to do final cleanups (which take place
4260 "outside" the binding contour of the function). */
4261
fe5b5c36 4262tree
362efdc1 4263get_temp_regvar (tree type, tree init)
f30432d7 4264{
f1dedc31 4265 tree decl;
8a72a046 4266
f1dedc31 4267 decl = create_temporary_var (type);
350fae66 4268 add_decl_expr (decl);
c8094d83 4269
4f2e1536
MP
4270 finish_expr_stmt (cp_build_modify_expr (input_location, decl, INIT_EXPR,
4271 init, tf_warning_or_error));
8a72a046 4272
f1dedc31 4273 return decl;
f30432d7
MS
4274}
4275
024f2d89
JM
4276/* Subroutine of build_vec_init. Returns true if assigning to an array of
4277 INNER_ELT_TYPE from INIT is trivial. */
4278
4279static bool
4280vec_copy_assign_is_trivial (tree inner_elt_type, tree init)
4281{
4282 tree fromtype = inner_elt_type;
72b3e203 4283 if (lvalue_p (init))
024f2d89
JM
4284 fromtype = cp_build_reference_type (fromtype, /*rval*/false);
4285 return is_trivially_xible (MODIFY_EXPR, inner_elt_type, fromtype);
4286}
4287
5a68fae7
JM
4288/* Subroutine of build_vec_init: Check that the array has at least N
4289 elements. Other parameters are local variables in build_vec_init. */
4290
4291void
4292finish_length_check (tree atype, tree iterator, tree obase, unsigned n)
4293{
4294 tree nelts = build_int_cst (ptrdiff_type_node, n - 1);
4295 if (TREE_CODE (atype) != ARRAY_TYPE)
4296 {
4297 if (flag_exceptions)
4298 {
4299 tree c = fold_build2 (LT_EXPR, boolean_type_node, iterator,
4300 nelts);
4301 c = build3 (COND_EXPR, void_type_node, c,
4302 throw_bad_array_new_length (), void_node);
4303 finish_expr_stmt (c);
4304 }
4305 /* Don't check an array new when -fno-exceptions. */
4306 }
f34ebeb2
ML
4307 else if (sanitize_flags_p (SANITIZE_BOUNDS)
4308 && current_function_decl != NULL_TREE)
5a68fae7
JM
4309 {
4310 /* Make sure the last element of the initializer is in bounds. */
4311 finish_expr_stmt
4312 (ubsan_instrument_bounds
4313 (input_location, obase, &nelts, /*ignore_off_by_one*/false));
4314 }
4315}
4316
1e184761
JM
4317/* walk_tree callback to collect temporaries in an expression. */
4318
4319tree
4320find_temps_r (tree *tp, int *walk_subtrees, void *data)
4321{
4322 vec<tree*> &temps = *static_cast<auto_vec<tree*> *>(data);
4323 tree t = *tp;
4324 if (TREE_CODE (t) == TARGET_EXPR
4325 && !TARGET_EXPR_ELIDING_P (t))
4326 temps.safe_push (tp);
4327 else if (TYPE_P (t))
4328 *walk_subtrees = 0;
4329
4330 return NULL_TREE;
4331}
4332
e0324e26
JM
4333/* walk_tree callback to collect temporaries in an expression that
4334 are allocator arguments to standard library classes. */
4335
4336static tree
4337find_allocator_temps_r (tree *tp, int *walk_subtrees, void *data)
4338{
4339 vec<tree*> &temps = *static_cast<auto_vec<tree*> *>(data);
4340 tree t = *tp;
4341 if (TYPE_P (t))
4342 {
4343 *walk_subtrees = 0;
4344 return NULL_TREE;
4345 }
4346
4347 /* If this is a call to a constructor for a std:: class, look for
4348 a reference-to-allocator argument. */
4349 tree fn = cp_get_callee_fndecl_nofold (t);
4350 if (fn && DECL_CONSTRUCTOR_P (fn)
4351 && decl_in_std_namespace_p (TYPE_NAME (DECL_CONTEXT (fn))))
4352 {
4353 int nargs = call_expr_nargs (t);
4354 for (int i = 1; i < nargs; ++i)
4355 {
4356 tree arg = get_nth_callarg (t, i);
4357 tree atype = TREE_TYPE (arg);
4358 if (TREE_CODE (atype) == REFERENCE_TYPE
4359 && is_std_allocator (TREE_TYPE (atype)))
4360 {
4361 STRIP_NOPS (arg);
4362 if (TREE_CODE (arg) == ADDR_EXPR)
4363 {
4364 tree *ap = &TREE_OPERAND (arg, 0);
4365 if (TREE_CODE (*ap) == TARGET_EXPR)
4366 temps.safe_push (ap);
4367 }
4368 }
4369 }
4370 }
4371
4372 return NULL_TREE;
4373}
4374
1e184761 4375/* If INIT initializes a standard library class, and involves a temporary
e0324e26
JM
4376 std::allocator<T>, use ALLOC_OBJ for all such temporaries.
4377
4378 Note that this can clobber the input to build_vec_init; no unsharing is
4379 done. To make this safe we use the TARGET_EXPR in all places rather than
4380 pulling out the TARGET_EXPR_SLOT.
1e184761
JM
4381
4382 Used by build_vec_init when initializing an array of e.g. strings to reuse
4383 the same temporary allocator for all of the strings. We can do this because
4384 std::allocator has no data and the standard library doesn't care about the
4385 address of allocator objects.
4386
4387 ??? Add an attribute to allow users to assert the same property for other
4388 classes, i.e. one object of the type is interchangeable with any other? */
4389
e0324e26
JM
4390static void
4391combine_allocator_temps (tree &init, tree &alloc_obj)
1e184761 4392{
1e184761 4393 auto_vec<tree*> temps;
e0324e26 4394 cp_walk_tree_without_duplicates (&init, find_allocator_temps_r, &temps);
1e184761 4395 for (tree *p : temps)
e0324e26
JM
4396 {
4397 if (!alloc_obj)
4398 alloc_obj = *p;
4399 else
4400 *p = alloc_obj;
4401 }
1e184761
JM
4402}
4403
f1dedc31
MM
4404/* `build_vec_init' returns tree structure that performs
4405 initialization of a vector of aggregate types.
8d08fdba 4406
9207099b
JM
4407 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
4408 to the first element, of POINTER_TYPE.
a48cccea 4409 MAXINDEX is the maximum index of the array (one less than the
9207099b 4410 number of elements). It is only used if BASE is a pointer or
a48cccea 4411 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
b84f4651 4412
8d08fdba
MS
4413 INIT is the (possibly NULL) initializer.
4414
844ae01d
JM
4415 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
4416 elements in the array are value-initialized.
b84f4651 4417
8d08fdba
MS
4418 FROM_ARRAY is 0 if we should init everything with INIT
4419 (i.e., every element initialized from INIT).
4420 FROM_ARRAY is 1 if we should index into INIT in parallel
4421 with initialization of DECL.
4422 FROM_ARRAY is 2 if we should index into INIT in parallel,
4423 but use assignment instead of initialization. */
4424
4425tree
3db45ab5 4426build_vec_init (tree base, tree maxindex, tree init,
844ae01d 4427 bool explicit_value_init_p,
beaee0a8
JM
4428 int from_array,
4429 tsubst_flags_t complain,
57ab3714 4430 vec<tree, va_gc>** cleanup_flags /* = nullptr */)
8d08fdba
MS
4431{
4432 tree rval;
8a72a046 4433 tree base2 = NULL_TREE;
e833cb11 4434 tree itype = NULL_TREE;
8a72a046 4435 tree iterator;
9207099b 4436 /* The type of BASE. */
f30efcb7 4437 tree atype = TREE_TYPE (base);
f1dedc31 4438 /* The type of an element in the array. */
f30efcb7 4439 tree type = TREE_TYPE (atype);
c8094d83 4440 /* The element type reached after removing all outer array
b5af3133
MM
4441 types. */
4442 tree inner_elt_type;
f1dedc31
MM
4443 /* The type of a pointer to an element in the array. */
4444 tree ptype;
4445 tree stmt_expr;
4446 tree compound_stmt;
4447 int destroy_temps;
faa9232d 4448 HOST_WIDE_INT num_initialized_elts = 0;
2a3398e1 4449 bool is_global;
fa2200cb 4450 tree obase = base;
f91352dc 4451 bool xvalue = false;
574cfaa4 4452 bool errors = false;
f9d0ca40 4453 location_t loc = (init ? cp_expr_loc_or_input_loc (init)
5a68fae7 4454 : location_of (base));
c8094d83 4455
9207099b 4456 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
a48cccea
JM
4457 maxindex = array_type_nelts (atype);
4458
ddffee68 4459 if (maxindex == NULL_TREE || maxindex == error_mark_node)
8d08fdba
MS
4460 return error_mark_node;
4461
cda0a029 4462 maxindex = maybe_constant_value (maxindex);
844ae01d 4463 if (explicit_value_init_p)
b84f4651
MM
4464 gcc_assert (!init);
4465
9207099b 4466 inner_elt_type = strip_array_types (type);
567ef749
JM
4467
4468 /* Look through the TARGET_EXPR around a compound literal. */
4469 if (init && TREE_CODE (init) == TARGET_EXPR
5a4d8044 4470 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
77ebee2b 4471 && from_array != 2)
567ef749
JM
4472 init = TARGET_EXPR_INITIAL (init);
4473
119cea98
JM
4474 if (tree vi = get_vec_init_expr (init))
4475 init = VEC_INIT_EXPR_INIT (vi);
e948436e 4476
0655c6d5
JM
4477 bool direct_init = false;
4478 if (from_array && init && BRACE_ENCLOSED_INITIALIZER_P (init)
4479 && CONSTRUCTOR_NELTS (init) == 1)
4480 {
4481 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
e948436e
JM
4482 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
4483 && TREE_CODE (elt) != VEC_INIT_EXPR)
0655c6d5
JM
4484 {
4485 direct_init = DIRECT_LIST_INIT_P (init);
4486 init = elt;
4487 }
4488 }
4489
dd9c8f64
JM
4490 /* from_array doesn't apply to initialization from CONSTRUCTOR. */
4491 if (init && TREE_CODE (init) == CONSTRUCTOR)
4492 from_array = 0;
4493
5a68fae7 4494 /* If we have a braced-init-list or string constant, make sure that the array
7d5e76c8 4495 is big enough for all the initializers. */
5a68fae7
JM
4496 bool length_check = (init
4497 && (TREE_CODE (init) == STRING_CST
4498 || (TREE_CODE (init) == CONSTRUCTOR
4499 && CONSTRUCTOR_NELTS (init) > 0))
46621807 4500 && !TREE_CONSTANT (maxindex));
7d5e76c8 4501
c8a3d889 4502 if (init
25357d1e 4503 && TREE_CODE (atype) == ARRAY_TYPE
06b76c7f 4504 && TREE_CONSTANT (maxindex)
c65bd532 4505 && !vla_type_p (type)
c8a3d889 4506 && (from_array == 2
024f2d89 4507 ? vec_copy_assign_is_trivial (inner_elt_type, init)
c8a3d889 4508 : !TYPE_NEEDS_CONSTRUCTING (type))
f30efcb7 4509 && ((TREE_CODE (init) == CONSTRUCTOR
d1a73b0b
JM
4510 && (BRACE_ENCLOSED_INITIALIZER_P (init)
4511 || (same_type_ignoring_top_level_qualifiers_p
4512 (atype, TREE_TYPE (init))))
f30efcb7
JM
4513 /* Don't do this if the CONSTRUCTOR might contain something
4514 that might throw and require us to clean up. */
9771b263 4515 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
b5af3133 4516 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
f30efcb7
JM
4517 || from_array))
4518 {
c32097d8 4519 /* Do non-default initialization of trivial arrays resulting from
f30efcb7
JM
4520 brace-enclosed initializers. In this case, digest_init and
4521 store_constructor will handle the semantics for us. */
4522
a47c2f62
JM
4523 if (BRACE_ENCLOSED_INITIALIZER_P (init))
4524 init = digest_init (atype, init, complain);
6ffbf87c 4525 stmt_expr = cp_build_init_expr (base, init);
f30efcb7
JM
4526 return stmt_expr;
4527 }
4528
4b978f96 4529 maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
cda0a029
JM
4530 maxindex = fold_simple (maxindex);
4531
9207099b
JM
4532 if (TREE_CODE (atype) == ARRAY_TYPE)
4533 {
4534 ptype = build_pointer_type (type);
89fcabaf
PC
4535 base = decay_conversion (base, complain);
4536 if (base == error_mark_node)
4537 return error_mark_node;
4b978f96 4538 base = cp_convert (ptype, base, complain);
9207099b
JM
4539 }
4540 else
4541 ptype = atype;
8d08fdba 4542
08e1ff9d
JM
4543 if (integer_all_onesp (maxindex))
4544 {
4545 /* Shortcut zero element case to avoid unneeded constructor synthesis. */
4546 if (init && TREE_SIDE_EFFECTS (init))
ff4deb4b 4547 base = build2 (COMPOUND_EXPR, ptype, init, base);
08e1ff9d
JM
4548 return base;
4549 }
4550
f1dedc31 4551 /* The code we are generating looks like:
303b7406 4552 ({
f1dedc31 4553 T* t1 = (T*) base;
f30efcb7 4554 T* rval = t1;
f1dedc31
MM
4555 ptrdiff_t iterator = maxindex;
4556 try {
4977bab6 4557 for (; iterator != -1; --iterator) {
f30efcb7
JM
4558 ... initialize *t1 ...
4559 ++t1;
4977bab6 4560 }
f1dedc31 4561 } catch (...) {
0cbd7506 4562 ... destroy elements that were constructed ...
f1dedc31 4563 }
303b7406
NS
4564 rval;
4565 })
c8094d83 4566
f1dedc31
MM
4567 We can omit the try and catch blocks if we know that the
4568 initialization will never throw an exception, or if the array
f30efcb7 4569 elements do not have destructors. We can omit the loop completely if
c8094d83 4570 the elements of the array do not have constructors.
f1dedc31
MM
4571
4572 We actually wrap the entire body of the above in a STMT_EXPR, for
c8094d83 4573 tidiness.
f1dedc31
MM
4574
4575 When copying from array to another, when the array elements have
4576 only trivial copy constructors, we should use __builtin_memcpy
4577 rather than generating a loop. That way, we could take advantage
3b426391 4578 of whatever cleverness the back end has for dealing with copies
f1dedc31
MM
4579 of blocks of memory. */
4580
2a3398e1 4581 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
f2c5f623 4582 destroy_temps = stmts_are_full_exprs_p ();
ae499cce 4583 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
f30efcb7 4584 rval = get_temp_regvar (ptype, base);
f1dedc31 4585 base = get_temp_regvar (ptype, rval);
beaee0a8
JM
4586 tree iterator_targ = get_target_expr (maxindex);
4587 add_stmt (iterator_targ);
4588 iterator = TARGET_EXPR_SLOT (iterator_targ);
8d08fdba 4589
5a4d8044
JM
4590 /* If initializing one array from another, initialize element by
4591 element. We rely upon the below calls to do the argument
4592 checking. Evaluate the initializer before entering the try block. */
dd9c8f64 4593 if (from_array)
5a4d8044 4594 {
f91352dc
JM
4595 if (lvalue_kind (init) & clk_rvalueref)
4596 xvalue = true;
77ebee2b 4597 base2 = decay_conversion (init, complain);
89fcabaf
PC
4598 if (base2 == error_mark_node)
4599 return error_mark_node;
5a4d8044
JM
4600 itype = TREE_TYPE (base2);
4601 base2 = get_temp_regvar (itype, base2);
4602 itype = TREE_TYPE (itype);
4603 }
4604
8a72a046 4605 /* Protect the entire array initialization so that we can destroy
f30efcb7
JM
4606 the partially constructed array if an exception is thrown.
4607 But don't do this if we're assigning. */
4608 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4609 && from_array != 2)
ed5511d9 4610 {
beaee0a8
JM
4611 tree e;
4612 tree m = cp_build_binary_op (input_location,
4613 MINUS_EXPR, maxindex, iterator,
4614 complain);
4615
4616 /* Flatten multi-dimensional array since build_vec_delete only
4617 expects one-dimensional array. */
4618 if (TREE_CODE (type) == ARRAY_TYPE)
4619 m = cp_build_binary_op (input_location,
4620 MULT_EXPR, m,
4621 /* Avoid mixing signed and unsigned. */
4622 convert (TREE_TYPE (m),
4623 array_type_nelts_total (type)),
4624 complain);
4625
4626 e = build_vec_delete_1 (input_location, rval, m,
4627 inner_elt_type, sfk_complete_destructor,
2fbc4548
JM
4628 /*use_global_delete=*/0, complain,
4629 /*in_cleanup*/true);
beaee0a8
JM
4630 if (e == error_mark_node)
4631 errors = true;
4632 TARGET_EXPR_CLEANUP (iterator_targ) = e;
4633 CLEANUP_EH_ONLY (iterator_targ) = true;
4634
4635 /* Since we push this cleanup before doing any initialization, cleanups
4636 for any temporaries in the initialization are naturally within our
4637 cleanup region, so we don't want wrap_temporary_cleanups to do
4638 anything for arrays. But if the array is a subobject, we need to
4639 tell split_nonconstant_init how to turn off this cleanup in favor of
4640 the cleanup for the complete object. */
57ab3714
JM
4641 if (cleanup_flags)
4642 vec_safe_push (*cleanup_flags, build_tree_list (iterator, maxindex));
ed5511d9 4643 }
8a72a046 4644
c0014b07
JM
4645 /* Should we try to create a constant initializer? */
4646 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
4647 && TREE_CONSTANT (maxindex)
a02f26f6
JM
4648 && (init ? TREE_CODE (init) == CONSTRUCTOR
4649 : (type_has_constexpr_default_constructor
4650 (inner_elt_type)))
c0014b07
JM
4651 && (literal_type_p (inner_elt_type)
4652 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
4653 vec<constructor_elt, va_gc> *const_vec = NULL;
4654 bool saw_non_const = false;
4655 /* If we're initializing a static array, we want to do static
4656 initialization of any elements with constant initializers even if
4657 some are non-constant. */
4658 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
4659
3bc63227 4660 bool empty_list = false;
1f65a8c8
JM
4661 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
4662 && CONSTRUCTOR_NELTS (init) == 0)
3bc63227
JM
4663 /* Skip over the handling of non-empty init lists. */
4664 empty_list = true;
1f65a8c8 4665
fa2200cb
JM
4666 /* Maybe pull out constant value when from_array? */
4667
1f65a8c8 4668 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
8d08fdba 4669 {
c32097d8 4670 /* Do non-default initialization of non-trivial arrays resulting from
f30efcb7 4671 brace-enclosed initializers. */
4038c495 4672 unsigned HOST_WIDE_INT idx;
fa2200cb 4673 tree field, elt;
b25dd954
JM
4674 /* If the constructor already has the array type, it's been through
4675 digest_init, so we shouldn't try to do anything more. */
4676 bool digested = same_type_p (atype, TREE_TYPE (init));
094fe153
JM
4677 from_array = 0;
4678
7d5e76c8 4679 if (length_check)
5a68fae7 4680 finish_length_check (atype, iterator, obase, CONSTRUCTOR_NELTS (init));
7d5e76c8 4681
fa2200cb 4682 if (try_const)
c0014b07 4683 vec_alloc (const_vec, CONSTRUCTOR_NELTS (init));
fa2200cb 4684
1e184761
JM
4685 tree alloc_obj = NULL_TREE;
4686
fa2200cb 4687 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
8d08fdba 4688 {
f1dedc31 4689 tree baseref = build1 (INDIRECT_REF, type, base);
fa2200cb 4690 tree one_init;
8d08fdba 4691
8a72a046 4692 num_initialized_elts++;
8d08fdba 4693
119cea98
JM
4694 /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can
4695 handle cleanup flags properly. */
4696 gcc_checking_assert (!target_expr_needs_replace (elt));
4697
b25dd954 4698 if (digested)
6ffbf87c 4699 one_init = cp_build_init_expr (baseref, elt);
119cea98 4700 else if (tree vi = get_vec_init_expr (elt))
57ab3714
JM
4701 one_init = expand_vec_init_expr (baseref, vi, complain,
4702 cleanup_flags);
b25dd954 4703 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
fa2200cb 4704 one_init = build_aggr_init (baseref, elt, 0, complain);
8a72a046 4705 else
4f2e1536
MP
4706 one_init = cp_build_modify_expr (input_location, baseref,
4707 NOP_EXPR, elt, complain);
574cfaa4
JM
4708 if (one_init == error_mark_node)
4709 errors = true;
fa2200cb
JM
4710 if (try_const)
4711 {
f84aded8
MP
4712 if (!field)
4713 field = size_int (idx);
c0014b07 4714 tree e = maybe_constant_init (one_init);
fa2200cb
JM
4715 if (reduced_constant_expression_p (e))
4716 {
c0014b07 4717 CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
fa2200cb
JM
4718 if (do_static_init)
4719 one_init = NULL_TREE;
4720 else
6ffbf87c 4721 one_init = cp_build_init_expr (baseref, e);
fa2200cb
JM
4722 }
4723 else
4724 {
4725 if (do_static_init)
f11c7048
JJ
4726 {
4727 tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
4728 true);
4729 if (value)
c0014b07 4730 CONSTRUCTOR_APPEND_ELT (const_vec, field, value);
f11c7048 4731 }
fa2200cb
JM
4732 saw_non_const = true;
4733 }
4734 }
4735
4736 if (one_init)
1e184761
JM
4737 {
4738 /* Only create one std::allocator temporary. */
e0324e26 4739 combine_allocator_temps (one_init, alloc_obj);
1e184761
JM
4740 finish_expr_stmt (one_init);
4741 }
8a72a046 4742
e51fbec3
MP
4743 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, false,
4744 complain);
574cfaa4
JM
4745 if (one_init == error_mark_node)
4746 errors = true;
4747 else
4748 finish_expr_stmt (one_init);
4749
e51fbec3 4750 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
574cfaa4
JM
4751 complain);
4752 if (one_init == error_mark_node)
4753 errors = true;
4754 else
4755 finish_expr_stmt (one_init);
8d08fdba 4756 }
8d08fdba 4757
3bc63227
JM
4758 /* Any elements without explicit initializers get T{}. */
4759 empty_list = true;
8d08fdba 4760 }
5a68fae7
JM
4761 else if (init && TREE_CODE (init) == STRING_CST)
4762 {
4763 /* Check that the array is at least as long as the string. */
4764 if (length_check)
4765 finish_length_check (atype, iterator, obase,
4766 TREE_STRING_LENGTH (init));
4767 tree length = build_int_cst (ptrdiff_type_node,
4768 TREE_STRING_LENGTH (init));
4769
4770 /* Copy the string to the first part of the array. */
4771 tree alias_set = build_int_cst (build_pointer_type (type), 0);
4772 tree lhs = build2 (MEM_REF, TREE_TYPE (init), base, alias_set);
4773 tree stmt = build2 (MODIFY_EXPR, void_type_node, lhs, init);
4774 finish_expr_stmt (stmt);
4775
4776 /* Adjust the counter and pointer. */
4777 stmt = cp_build_binary_op (loc, MINUS_EXPR, iterator, length, complain);
4778 stmt = build2 (MODIFY_EXPR, void_type_node, iterator, stmt);
4779 finish_expr_stmt (stmt);
4780
4781 stmt = cp_build_binary_op (loc, PLUS_EXPR, base, length, complain);
4782 stmt = build2 (MODIFY_EXPR, void_type_node, base, stmt);
4783 finish_expr_stmt (stmt);
4784
4785 /* And set the rest of the array to NUL. */
4786 from_array = 0;
4787 explicit_value_init_p = true;
4788 }
8a72a046 4789 else if (from_array)
8d08fdba 4790 {
8a72a046 4791 if (init)
5a4d8044 4792 /* OK, we set base2 above. */;
95552437 4793 else if (CLASS_TYPE_P (type)
8a72a046
MM
4794 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4795 {
5ade1ed2
DG
4796 if (complain & tf_error)
4797 error ("initializer ends prematurely");
574cfaa4 4798 errors = true;
8a72a046
MM
4799 }
4800 }
8d08fdba 4801
8a72a046
MM
4802 /* Now, default-initialize any remaining elements. We don't need to
4803 do that if a) the type does not need constructing, or b) we've
094fe153
JM
4804 already initialized all the elements.
4805
4806 We do need to keep going if we're copying an array. */
4807
bd95d75f
JM
4808 if (try_const && !init
4809 && (cxx_dialect < cxx20
4810 || !default_init_uninitialized_part (inner_elt_type)))
a02f26f6
JM
4811 /* With a constexpr default constructor, which we checked for when
4812 setting try_const above, default-initialization is equivalent to
4813 value-initialization, and build_value_init gives us something more
bd95d75f
JM
4814 friendly to maybe_constant_init. Except in C++20 and up a constexpr
4815 constructor need not initialize all the members. */
a02f26f6 4816 explicit_value_init_p = true;
094fe153 4817 if (from_array
1f65a8c8 4818 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
9541ffee 4819 && ! (tree_fits_shwi_p (maxindex)
05bccae2 4820 && (num_initialized_elts
9439e9a1 4821 == tree_to_shwi (maxindex) + 1))))
8a72a046 4822 {
5453bfed 4823 /* If the ITERATOR is lesser or equal to -1, then we don't have to loop;
8a72a046 4824 we've already initialized all the elements. */
4977bab6 4825 tree for_stmt;
f1dedc31 4826 tree elt_init;
b84f4651 4827 tree to;
f1dedc31 4828
3f43ac31 4829 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17a9e380 4830 finish_init_stmt (for_stmt);
5453bfed 4831 finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator,
aab384ae 4832 build_int_cst (TREE_TYPE (iterator), -1)),
170a8bd6 4833 for_stmt, false, 0);
c743614e
JM
4834 /* We used to pass this decrement to finish_for_expr; now we add it to
4835 elt_init below so it's part of the same full-expression as the
4836 initialization, and thus happens before any potentially throwing
4837 temporary cleanups. */
4838 tree decr = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
4839 complain);
4840
8d08fdba 4841
b84f4651
MM
4842 to = build1 (INDIRECT_REF, type, base);
4843
ced2fb08
JM
4844 /* If the initializer is {}, then all elements are initialized from T{}.
4845 But for non-classes, that's the same as value-initialization. */
4846 if (empty_list)
4847 {
4848 if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type))
4849 {
08227658 4850 init = build_constructor (init_list_type_node, NULL);
ced2fb08
JM
4851 }
4852 else
4853 {
4854 init = NULL_TREE;
4855 explicit_value_init_p = true;
4856 }
4857 }
4858
8d08fdba
MS
4859 if (from_array)
4860 {
8d08fdba
MS
4861 tree from;
4862
4863 if (base2)
f91352dc
JM
4864 {
4865 from = build1 (INDIRECT_REF, itype, base2);
4866 if (xvalue)
4867 from = move (from);
70f40fea
JJ
4868 if (direct_init)
4869 from = build_tree_list (NULL_TREE, from);
f91352dc 4870 }
8d08fdba
MS
4871 else
4872 from = NULL_TREE;
4873
e278212e
PC
4874 if (TREE_CODE (type) == ARRAY_TYPE)
4875 elt_init = build_vec_init (to, NULL_TREE, from, /*val_init*/false,
4876 from_array, complain);
4877 else if (from_array == 2)
4f2e1536
MP
4878 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR,
4879 from, complain);
95552437 4880 else if (type_build_ctor_call (type))
5ade1ed2 4881 elt_init = build_aggr_init (to, from, 0, complain);
8d08fdba 4882 else if (from)
4f2e1536 4883 elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, from,
5ade1ed2 4884 complain);
8d08fdba 4885 else
8dc2b103 4886 gcc_unreachable ();
8d08fdba
MS
4887 }
4888 else if (TREE_CODE (type) == ARRAY_TYPE)
4889 {
16b53405 4890 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
0186f684
AO
4891 {
4892 if ((complain & tf_error))
4893 error_at (loc, "array must be initialized "
4894 "with a brace-enclosed initializer");
4895 elt_init = error_mark_node;
4896 }
4897 else
4898 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
4899 0, init,
4900 explicit_value_init_p,
4901 0, complain);
f1dedc31 4902 }
844ae01d 4903 else if (explicit_value_init_p)
309714d4
JM
4904 {
4905 elt_init = build_value_init (type, complain);
574cfaa4 4906 if (elt_init != error_mark_node)
6ffbf87c 4907 elt_init = cp_build_init_expr (to, elt_init);
309714d4 4908 }
f1dedc31 4909 else
844ae01d 4910 {
1f65a8c8
JM
4911 gcc_assert (type_build_ctor_call (type) || init);
4912 if (CLASS_TYPE_P (type))
4913 elt_init = build_aggr_init (to, init, 0, complain);
4914 else
4915 {
4916 if (TREE_CODE (init) == TREE_LIST)
4917 init = build_x_compound_expr_from_list (init, ELK_INIT,
4918 complain);
143aa5cc
PC
4919 elt_init = (init == error_mark_node
4920 ? error_mark_node
4921 : build2 (INIT_EXPR, type, to, init));
1f65a8c8 4922 }
844ae01d 4923 }
c8094d83 4924
574cfaa4
JM
4925 if (elt_init == error_mark_node)
4926 errors = true;
4927
c0014b07
JM
4928 if (try_const)
4929 {
a02f26f6 4930 /* FIXME refs to earlier elts */
c0014b07
JM
4931 tree e = maybe_constant_init (elt_init);
4932 if (reduced_constant_expression_p (e))
4933 {
4934 if (initializer_zerop (e))
4935 /* Don't fill the CONSTRUCTOR with zeros. */
4936 e = NULL_TREE;
4937 if (do_static_init)
4938 elt_init = NULL_TREE;
4939 }
4940 else
4941 {
4942 saw_non_const = true;
4943 if (do_static_init)
4944 e = build_zero_init (TREE_TYPE (e), NULL_TREE, true);
a02f26f6
JM
4945 else
4946 e = NULL_TREE;
c0014b07
JM
4947 }
4948
4949 if (e)
4950 {
faa9232d
JJ
4951 HOST_WIDE_INT last = tree_to_shwi (maxindex);
4952 if (num_initialized_elts <= last)
c0014b07
JM
4953 {
4954 tree field = size_int (num_initialized_elts);
faa9232d
JJ
4955 if (num_initialized_elts != last)
4956 field = build2 (RANGE_EXPR, sizetype, field,
4957 size_int (last));
c0014b07
JM
4958 CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
4959 }
4960 }
4961 }
4962
beaee0a8
JM
4963 /* [class.temporary]: "There are three contexts in which temporaries are
4964 destroyed at a different point than the end of the full-
4965 expression. The first context is when a default constructor is called
4966 to initialize an element of an array with no corresponding
4967 initializer. The second context is when a copy constructor is called
4968 to copy an element of an array while the entire array is copied. In
4969 either case, if the constructor has one or more default arguments, the
4970 destruction of every temporary created in a default argument is
4971 sequenced before the construction of the next array element, if any."
4972
4973 So, for this loop, statements are full-expressions. */
2a3398e1 4974 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
0186f684 4975 if (elt_init && !errors)
c743614e
JM
4976 elt_init = build2 (COMPOUND_EXPR, void_type_node, elt_init, decr);
4977 else
4978 elt_init = decr;
4979 finish_expr_stmt (elt_init);
2a3398e1 4980 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8d08fdba 4981
e51fbec3 4982 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false,
5ade1ed2 4983 complain));
8d08fdba 4984 if (base2)
e51fbec3 4985 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, false,
5ade1ed2 4986 complain));
0fac6b0b 4987
4977bab6 4988 finish_for_stmt (for_stmt);
8d08fdba 4989 }
8a72a046 4990
303b7406
NS
4991 /* The value of the array initialization is the array itself, RVAL
4992 is a pointer to the first element. */
325c3691 4993 finish_stmt_expr_expr (rval, stmt_expr);
f1dedc31 4994
2a3398e1 4995 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
303b7406 4996
5fb4d142
JM
4997 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
4998
4999 if (errors)
5000 return error_mark_node;
c0014b07
JM
5001
5002 if (try_const)
5003 {
5004 if (!saw_non_const)
5005 {
5006 tree const_init = build_constructor (atype, const_vec);
5007 return build2 (INIT_EXPR, atype, obase, const_init);
5008 }
5009 else if (do_static_init && !vec_safe_is_empty (const_vec))
5010 DECL_INITIAL (obase) = build_constructor (atype, const_vec);
5011 else
5012 vec_free (const_vec);
5013 }
5fb4d142 5014
9207099b
JM
5015 /* Now make the result have the correct type. */
5016 if (TREE_CODE (atype) == ARRAY_TYPE)
5017 {
cc76c502 5018 atype = build_reference_type (atype);
9207099b 5019 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
cc76c502 5020 stmt_expr = convert_from_reference (stmt_expr);
9207099b 5021 }
c8094d83 5022
f1dedc31 5023 return stmt_expr;
8d08fdba
MS
5024}
5025
86f45d2c
MM
5026/* Call the DTOR_KIND destructor for EXP. FLAGS are as for
5027 build_delete. */
298d6f60
MM
5028
5029static tree
574cfaa4
JM
5030build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
5031 tsubst_flags_t complain)
298d6f60 5032{
86f45d2c 5033 tree name;
86f45d2c
MM
5034 switch (dtor_kind)
5035 {
5036 case sfk_complete_destructor:
5037 name = complete_dtor_identifier;
5038 break;
5039
5040 case sfk_base_destructor:
5041 name = base_dtor_identifier;
5042 break;
5043
5044 case sfk_deleting_destructor:
5045 name = deleting_dtor_identifier;
5046 break;
5047
5048 default:
8dc2b103 5049 gcc_unreachable ();
86f45d2c 5050 }
4d20f490
JM
5051
5052 return build_special_member_call (exp, name,
5053 /*args=*/NULL,
5054 /*binfo=*/TREE_TYPE (exp),
5055 flags,
5056 complain);
298d6f60
MM
5057}
5058
8d08fdba
MS
5059/* Generate a call to a destructor. TYPE is the type to cast ADDR to.
5060 ADDR is an expression which yields the store to be destroyed.
86f45d2c
MM
5061 AUTO_DELETE is the name of the destructor to call, i.e., either
5062 sfk_complete_destructor, sfk_base_destructor, or
5063 sfk_deleting_destructor.
8d08fdba
MS
5064
5065 FLAGS is the logical disjunction of zero or more LOOKUP_
ade3dc07 5066 flags. See cp-tree.h for more info. */
e92cc029 5067
8d08fdba 5068tree
04e4997a
PC
5069build_delete (location_t loc, tree otype, tree addr,
5070 special_function_kind auto_delete,
574cfaa4 5071 int flags, int use_global_delete, tsubst_flags_t complain)
8d08fdba 5072{
8d08fdba 5073 tree expr;
8d08fdba
MS
5074
5075 if (addr == error_mark_node)
5076 return error_mark_node;
5077
eca7fc57
JM
5078 tree type = TYPE_MAIN_VARIANT (otype);
5079
8d08fdba
MS
5080 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
5081 set to `error_mark_node' before it gets properly cleaned up. */
5082 if (type == error_mark_node)
5083 return error_mark_node;
5084
9f613f06 5085 if (TYPE_PTR_P (type))
eca7fc57 5086 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8d08fdba 5087
eca7fc57
JM
5088 if (TREE_CODE (type) == ARRAY_TYPE)
5089 {
5090 if (TYPE_DOMAIN (type) == NULL_TREE)
5091 {
5092 if (complain & tf_error)
04e4997a 5093 error_at (loc, "unknown array size in delete");
eca7fc57
JM
5094 return error_mark_node;
5095 }
04e4997a 5096 return build_vec_delete (loc, addr, array_type_nelts (type),
eca7fc57
JM
5097 auto_delete, use_global_delete, complain);
5098 }
03a904b5 5099
9cbc7d65
JM
5100 bool deleting = (auto_delete == sfk_deleting_destructor);
5101 gcc_assert (deleting == !(flags & LOOKUP_DESTRUCTOR));
5102
eca7fc57 5103 if (TYPE_PTR_P (otype))
8d08fdba 5104 {
eca7fc57 5105 addr = mark_rvalue_use (addr);
23b4deba 5106
b1e5b86c
GB
5107 /* We don't want to warn about delete of void*, only other
5108 incomplete types. Deleting other incomplete types
5109 invokes undefined behavior, but it is not ill-formed, so
5110 compile to something that would even do The Right Thing
5111 (TM) should the type have a trivial dtor and no delete
5112 operator. */
5113 if (!VOID_TYPE_P (type))
8d08fdba 5114 {
b1e5b86c 5115 complete_type (type);
02a32ab4 5116 if (deleting
04e4997a 5117 && !verify_type_context (loc, TCTX_DEALLOCATION, type,
02a32ab4
RS
5118 !(complain & tf_error)))
5119 return error_mark_node;
5120
b1e5b86c
GB
5121 if (!COMPLETE_TYPE_P (type))
5122 {
097f82ec 5123 if (complain & tf_warning)
71205d17 5124 {
097f82ec 5125 auto_diagnostic_group d;
04e4997a
PC
5126 if (warning_at (loc, OPT_Wdelete_incomplete,
5127 "possible problem detected in invocation of "
5128 "%<operator delete%>"))
097f82ec
DM
5129 {
5130 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
04e4997a 5131 inform (loc,
a9c697b8
MS
5132 "neither the destructor nor the class-specific "
5133 "%<operator delete%> will be called, even if "
5134 "they are declared when the class is defined");
097f82ec 5135 }
71205d17 5136 }
b1e5b86c 5137 }
9cbc7d65 5138 else if (deleting && warn_delnonvdtor
e2ab8a0f
JW
5139 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
5140 && TYPE_POLYMORPHIC_P (type))
014ab419 5141 {
b2cf76f3 5142 tree dtor = CLASSTYPE_DESTRUCTOR (type);
014ab419
JW
5143 if (!dtor || !DECL_VINDEX (dtor))
5144 {
5145 if (CLASSTYPE_PURE_VIRTUALS (type))
04e4997a
PC
5146 warning_at (loc, OPT_Wdelete_non_virtual_dtor,
5147 "deleting object of abstract class type %qT"
5148 " which has non-virtual destructor"
5149 " will cause undefined behavior", type);
014ab419 5150 else
04e4997a
PC
5151 warning_at (loc, OPT_Wdelete_non_virtual_dtor,
5152 "deleting object of polymorphic class type %qT"
5153 " which has non-virtual destructor"
5154 " might cause undefined behavior", type);
014ab419
JW
5155 }
5156 }
8d08fdba 5157 }
2986ae00 5158
f4f206f4 5159 /* Throw away const and volatile on target type of addr. */
4b978f96 5160 addr = convert_force (build_pointer_type (type), addr, 0, complain);
8d08fdba 5161 }
8d08fdba
MS
5162 else
5163 {
5164 /* Don't check PROTECT here; leave that decision to the
5165 destructor. If the destructor is accessible, call it,
5166 else report error. */
574cfaa4
JM
5167 addr = cp_build_addr_expr (addr, complain);
5168 if (addr == error_mark_node)
5169 return error_mark_node;
8d08fdba 5170
4b978f96 5171 addr = convert_force (build_pointer_type (type), addr, 0, complain);
8d08fdba
MS
5172 }
5173
9cbc7d65
JM
5174 if (deleting)
5175 /* We will use ADDR multiple times so we must save it. */
5176 addr = save_expr (addr);
eca7fc57 5177
cdc18417
JM
5178 bool virtual_p = false;
5179 if (type_build_dtor_call (type))
5180 {
5181 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
5182 lazily_declare_fn (sfk_destructor, type);
5183 virtual_p = DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type));
5184 }
8d08fdba 5185
9cbc7d65
JM
5186 tree head = NULL_TREE;
5187 tree do_delete = NULL_TREE;
a6bb6b07 5188 bool destroying_delete = false;
9cbc7d65
JM
5189
5190 if (!deleting)
5191 {
5192 /* Leave do_delete null. */
5193 }
cdc18417
JM
5194 /* For `::delete x', we must not use the deleting destructor
5195 since then we would not be sure to get the global `operator
5196 delete'. */
9cbc7d65 5197 else if (use_global_delete)
cdc18417 5198 {
cdc18417
JM
5199 head = get_target_expr (build_headof (addr));
5200 /* Delete the object. */
5201 do_delete = build_op_delete_call (DELETE_EXPR,
5202 head,
5203 cxx_sizeof_nowarn (type),
5204 /*global_p=*/true,
5205 /*placement=*/NULL_TREE,
5206 /*alloc_fn=*/NULL_TREE,
5207 complain);
5208 /* Otherwise, treat this like a complete object destructor
5209 call. */
5210 auto_delete = sfk_complete_destructor;
8d08fdba 5211 }
cdc18417
JM
5212 /* If the destructor is non-virtual, there is no deleting
5213 variant. Instead, we must explicitly call the appropriate
5214 `operator delete' here. */
9cbc7d65 5215 else if (!virtual_p)
8d08fdba 5216 {
cdc18417
JM
5217 /* Build the call. */
5218 do_delete = build_op_delete_call (DELETE_EXPR,
5219 addr,
5220 cxx_sizeof_nowarn (type),
5221 /*global_p=*/false,
5222 /*placement=*/NULL_TREE,
5223 /*alloc_fn=*/NULL_TREE,
5224 complain);
5225 /* Call the complete object destructor. */
5226 auto_delete = sfk_complete_destructor;
a6bb6b07
JM
5227 if (do_delete != error_mark_node)
5228 {
5229 tree fn = get_callee_fndecl (do_delete);
5230 destroying_delete = destroying_delete_p (fn);
5231 }
cdc18417 5232 }
9cbc7d65 5233 else if (TYPE_GETS_REG_DELETE (type))
cdc18417
JM
5234 {
5235 /* Make sure we have access to the member op delete, even though
5236 we'll actually be calling it from the destructor. */
5237 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
5238 /*global_p=*/false,
5239 /*placement=*/NULL_TREE,
5240 /*alloc_fn=*/NULL_TREE,
5241 complain);
5242 }
700f8a87 5243
cf2b7020
JM
5244 if (destroying_delete)
5245 /* The operator delete will call the destructor. */
5246 expr = addr;
5247 else if (type_build_dtor_call (type))
cdc18417
JM
5248 expr = build_dtor_call (cp_build_fold_indirect_ref (addr),
5249 auto_delete, flags, complain);
5250 else
5251 expr = build_trivial_dtor_call (addr);
5252 if (expr == error_mark_node)
5253 return error_mark_node;
ade3dc07 5254
9cbc7d65 5255 if (!deleting)
04e4997a
PC
5256 {
5257 protected_set_expr_location (expr, loc);
5258 return expr;
5259 }
9cbc7d65 5260
4924293a
PP
5261 if (do_delete == error_mark_node)
5262 return error_mark_node;
8412b939 5263
cdc18417
JM
5264 if (do_delete && !TREE_SIDE_EFFECTS (expr))
5265 expr = do_delete;
5266 else if (do_delete)
5267 /* The delete operator must be called, regardless of whether
5268 the destructor throws.
8d08fdba 5269
cdc18417
JM
5270 [expr.delete]/7 The deallocation function is called
5271 regardless of whether the destructor for the object or some
5272 element of the array throws an exception. */
5273 expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete);
8d08fdba 5274
cdc18417
JM
5275 /* We need to calculate this before the dtor changes the vptr. */
5276 if (head)
5277 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
8d08fdba 5278
9cbc7d65
JM
5279 /* Handle deleting a null pointer. */
5280 warning_sentinel s (warn_address);
04e4997a 5281 tree ifexp = cp_build_binary_op (loc, NE_EXPR, addr,
9cbc7d65
JM
5282 nullptr_node, complain);
5283 ifexp = cp_fully_fold (ifexp);
5284
5285 if (ifexp == error_mark_node)
5286 return error_mark_node;
5287 /* This is a compiler generated comparison, don't emit
5288 e.g. -Wnonnull-compare warning for it. */
5289 else if (TREE_CODE (ifexp) == NE_EXPR)
65870e75 5290 suppress_warning (ifexp, OPT_Wnonnull_compare);
cdc18417 5291
9cbc7d65 5292 if (!integer_nonzerop (ifexp))
cdc18417
JM
5293 expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node);
5294
04e4997a 5295 protected_set_expr_location (expr, loc);
cdc18417 5296 return expr;
ade3dc07 5297}
8d08fdba 5298
ade3dc07
JM
5299/* At the beginning of a destructor, push cleanups that will call the
5300 destructors for our base classes and members.
2a2480e1 5301
a29e1034 5302 Called from begin_destructor_body. */
8d08fdba 5303
ade3dc07 5304void
edaf3e03 5305push_base_cleanups (void)
ade3dc07 5306{
fa743e8c
NS
5307 tree binfo, base_binfo;
5308 int i;
ade3dc07
JM
5309 tree member;
5310 tree expr;
9771b263 5311 vec<tree, va_gc> *vbases;
8d08fdba 5312
ade3dc07 5313 /* Run destructors for all virtual baseclasses. */
45e2bf2e
NS
5314 if (!ABSTRACT_CLASS_TYPE_P (current_class_type)
5315 && CLASSTYPE_VBASECLASSES (current_class_type))
ade3dc07 5316 {
ade3dc07 5317 tree cond = (condition_conversion
f293ce4b
RS
5318 (build2 (BIT_AND_EXPR, integer_type_node,
5319 current_in_charge_parm,
5320 integer_two_node)));
8d08fdba 5321
58c42dc2 5322 /* The CLASSTYPE_VBASECLASSES vector is in initialization
ade3dc07 5323 order, which is also the right order for pushing cleanups. */
9ba5ff0f 5324 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
9771b263 5325 vec_safe_iterate (vbases, i, &base_binfo); i++)
8d08fdba 5326 {
eca7fc57 5327 if (type_build_dtor_call (BINFO_TYPE (base_binfo)))
8d08fdba 5328 {
c8094d83 5329 expr = build_special_member_call (current_class_ref,
4ba126e4 5330 base_dtor_identifier,
c166b898 5331 NULL,
9ba5ff0f 5332 base_binfo,
c8094d83 5333 (LOOKUP_NORMAL
5ade1ed2 5334 | LOOKUP_NONVIRTUAL),
eca7fc57
JM
5335 tf_warning_or_error);
5336 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5337 {
5338 expr = build3 (COND_EXPR, void_type_node, cond,
632f2871 5339 expr, void_node);
eca7fc57
JM
5340 finish_decl_cleanup (NULL_TREE, expr);
5341 }
8d08fdba
MS
5342 }
5343 }
ade3dc07
JM
5344 }
5345
ade3dc07 5346 /* Take care of the remaining baseclasses. */
fa743e8c
NS
5347 for (binfo = TYPE_BINFO (current_class_type), i = 0;
5348 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
ade3dc07 5349 {
eca7fc57
JM
5350 if (BINFO_VIRTUAL_P (base_binfo)
5351 || !type_build_dtor_call (BINFO_TYPE (base_binfo)))
ade3dc07
JM
5352 continue;
5353
c8094d83 5354 expr = build_special_member_call (current_class_ref,
4ba126e4 5355 base_dtor_identifier,
c166b898 5356 NULL, base_binfo,
5ade1ed2
DG
5357 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
5358 tf_warning_or_error);
eca7fc57
JM
5359 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5360 finish_decl_cleanup (NULL_TREE, expr);
ade3dc07
JM
5361 }
5362
57ece258
JM
5363 /* Don't automatically destroy union members. */
5364 if (TREE_CODE (current_class_type) == UNION_TYPE)
5365 return;
5366
ade3dc07 5367 for (member = TYPE_FIELDS (current_class_type); member;
910ad8de 5368 member = DECL_CHAIN (member))
ade3dc07 5369 {
57ece258
JM
5370 tree this_type = TREE_TYPE (member);
5371 if (this_type == error_mark_node
2e5d2970
VR
5372 || TREE_CODE (member) != FIELD_DECL
5373 || DECL_ARTIFICIAL (member))
ade3dc07 5374 continue;
04056396 5375 if (ANON_AGGR_TYPE_P (this_type))
57ece258 5376 continue;
eca7fc57 5377 if (type_build_dtor_call (this_type))
ade3dc07 5378 {
c8094d83
MS
5379 tree this_member = (build_class_member_access_expr
5380 (current_class_ref, member,
50ad9642 5381 /*access_path=*/NULL_TREE,
5ade1ed2
DG
5382 /*preserve_reference=*/false,
5383 tf_warning_or_error));
04e4997a 5384 expr = build_delete (input_location, this_type, this_member,
ade3dc07
JM
5385 sfk_complete_destructor,
5386 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
574cfaa4 5387 0, tf_warning_or_error);
eca7fc57
JM
5388 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
5389 finish_decl_cleanup (NULL_TREE, expr);
ade3dc07 5390 }
8d08fdba
MS
5391 }
5392}
5393
8d08fdba
MS
5394/* Build a C++ vector delete expression.
5395 MAXINDEX is the number of elements to be deleted.
5396 ELT_SIZE is the nominal size of each element in the vector.
5397 BASE is the expression that should yield the store to be deleted.
8d08fdba
MS
5398 This function expands (or synthesizes) these calls itself.
5399 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
8d08fdba
MS
5400
5401 This also calls delete for virtual baseclasses of elements of the vector.
5402
5403 Update: MAXINDEX is no longer needed. The size can be extracted from the
5404 start of the vector for pointers, and from the type for arrays. We still
5405 use MAXINDEX for arrays because it happens to already have one of the
5406 values we'd have to extract. (We could use MAXINDEX with pointers to
5407 confirm the size, and trap if the numbers differ; not clear that it'd
5408 be worth bothering.) */
e92cc029 5409
8d08fdba 5410tree
04e4997a 5411build_vec_delete (location_t loc, tree base, tree maxindex,
574cfaa4
JM
5412 special_function_kind auto_delete_vec,
5413 int use_global_delete, tsubst_flags_t complain)
8d08fdba 5414{
f30432d7 5415 tree type;
49b7aacb
JM
5416 tree rval;
5417 tree base_init = NULL_TREE;
8d08fdba 5418
f30432d7 5419 type = TREE_TYPE (base);
c407792d 5420
50e10fa8 5421 if (TYPE_PTR_P (type))
8d08fdba
MS
5422 {
5423 /* Step back one from start of vector, and read dimension. */
834c6dff 5424 tree cookie_addr;
726a989a 5425 tree size_ptr_type = build_pointer_type (sizetype);
834c6dff 5426
3c784bca 5427 base = mark_rvalue_use (base);
6742d92b 5428 if (TREE_SIDE_EFFECTS (base))
49b7aacb
JM
5429 {
5430 base_init = get_target_expr (base);
5431 base = TARGET_EXPR_SLOT (base_init);
5432 }
708cae97 5433 type = strip_array_types (TREE_TYPE (type));
04e4997a 5434 cookie_addr = fold_build1_loc (loc, NEGATE_EXPR,
db3927fb 5435 sizetype, TYPE_SIZE_UNIT (sizetype));
5d49b6a7
RG
5436 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
5437 cookie_addr);
04757a2a 5438 maxindex = cp_build_fold_indirect_ref (cookie_addr);
8d08fdba 5439 }
f30432d7 5440 else if (TREE_CODE (type) == ARRAY_TYPE)
8d08fdba 5441 {
f4f206f4
KH
5442 /* Get the total number of things in the array, maxindex is a
5443 bad name. */
f30432d7 5444 maxindex = array_type_nelts_total (type);
834c6dff 5445 type = strip_array_types (type);
16b53405 5446 base = decay_conversion (base, complain);
574cfaa4
JM
5447 if (base == error_mark_node)
5448 return error_mark_node;
6742d92b 5449 if (TREE_SIDE_EFFECTS (base))
49b7aacb
JM
5450 {
5451 base_init = get_target_expr (base);
5452 base = TARGET_EXPR_SLOT (base_init);
5453 }
8d08fdba
MS
5454 }
5455 else
5456 {
574cfaa4 5457 if (base != error_mark_node && !(complain & tf_error))
04e4997a
PC
5458 error_at (loc,
5459 "type to vector delete is neither pointer or array type");
8d08fdba
MS
5460 return error_mark_node;
5461 }
8d08fdba 5462
04e4997a 5463 rval = build_vec_delete_1 (loc, base, maxindex, type, auto_delete_vec,
574cfaa4
JM
5464 use_global_delete, complain);
5465 if (base_init && rval != error_mark_node)
f293ce4b 5466 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
49b7aacb 5467
04e4997a 5468 protected_set_expr_location (rval, loc);
49b7aacb 5469 return rval;
8d08fdba 5470}
ff502317
BE
5471
5472#include "gt-cp-init.h"