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