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