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