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