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