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