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