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