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