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