Line data Source code
1 : /* Handle initialization things in C++.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 : Contributed by Michael Tiemann (tiemann@cygnus.com)
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : /* High-level class interface. */
22 :
23 : #include "config.h"
24 : #include "system.h"
25 : #include "coretypes.h"
26 : #include "target.h"
27 : #include "cp-tree.h"
28 : #include "stringpool.h"
29 : #include "varasm.h"
30 : #include "gimplify.h"
31 : #include "c-family/c-ubsan.h"
32 : #include "intl.h"
33 : #include "attribs.h"
34 : #include "asan.h"
35 : #include "stor-layout.h"
36 : #include "pointer-query.h"
37 :
38 : static bool begin_init_stmts (tree *, tree *);
39 : static tree finish_init_stmts (bool, tree, tree);
40 : static void construct_virtual_base (tree, tree);
41 : static bool expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
42 : static bool expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
43 : static int member_init_ok_or_else (tree, tree, tree);
44 : static void expand_virtual_init (tree, tree);
45 : static tree sort_mem_initializers (tree, tree);
46 : static tree initializing_context (tree);
47 : static void expand_cleanup_for_base (tree, tree);
48 : static tree dfs_initialize_vtbl_ptrs (tree, void *);
49 : static tree build_field_list (tree, tree, int *);
50 : static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
51 :
52 : static GTY(()) tree fn;
53 :
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 :
63 : static bool
64 5664404 : begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
65 : {
66 5664404 : bool is_global = !building_stmt_list_p ();
67 :
68 5664404 : *stmt_expr_p = begin_stmt_expr ();
69 5664404 : *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
70 :
71 5664404 : return is_global;
72 : }
73 :
74 : /* Finish out the statement-expression begun by the previous call to
75 : begin_init_stmts. Returns the statement-expression itself. */
76 :
77 : static tree
78 5664404 : finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
79 : {
80 5664404 : finish_compound_stmt (compound_stmt);
81 :
82 5664404 : stmt_expr = finish_stmt_expr (stmt_expr, true);
83 :
84 6190810 : gcc_assert (!building_stmt_list_p () == is_global);
85 :
86 5664404 : return stmt_expr;
87 : }
88 :
89 : /* Constructors */
90 :
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. */
94 :
95 : static tree
96 10210831 : dfs_initialize_vtbl_ptrs (tree binfo, void *data)
97 : {
98 10210831 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
99 : return dfs_skip_bases;
100 :
101 3035209 : if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
102 : {
103 1194365 : tree base_ptr = TREE_VALUE ((tree) data);
104 :
105 1194365 : base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
106 : tf_warning_or_error);
107 :
108 1194365 : expand_virtual_init (binfo, base_ptr);
109 : }
110 :
111 : return NULL_TREE;
112 : }
113 :
114 : /* Initialize all the vtable pointers in the object pointed to by
115 : ADDR. */
116 :
117 : void
118 7865772 : initialize_vtbl_ptrs (tree addr)
119 : {
120 7865772 : tree list;
121 7865772 : tree type;
122 :
123 7865772 : type = TREE_TYPE (TREE_TYPE (addr));
124 7865772 : list = build_tree_list (type, addr);
125 :
126 : /* Walk through the hierarchy, initializing the vptr in each base
127 : class. We do these in pre-order because we can't find the virtual
128 : bases for a class until we've initialized the vtbl for that
129 : class. */
130 7865772 : dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
131 7865772 : }
132 :
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
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
142 : zero-initialization does not simply mean filling the storage with
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
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. */
148 :
149 : static tree
150 5609461 : build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
151 : tree field_size)
152 : {
153 5609461 : tree init = NULL_TREE;
154 :
155 : /* [dcl.init]
156 :
157 : To zero-initialize an object of type T means:
158 :
159 : -- if T is a scalar type, the storage is set to the value of zero
160 : converted to T.
161 :
162 : -- if T is a non-union class type, the storage for each non-static
163 : data member and each base-class subobject is zero-initialized.
164 :
165 : -- if T is a union type, the storage for its first data member is
166 : zero-initialized.
167 :
168 : -- if T is an array type, the storage for each element is
169 : zero-initialized.
170 :
171 : -- if T is a reference type, no initialization is performed. */
172 :
173 5609461 : gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
174 :
175 : /* An initializer is unqualified. */
176 5609461 : type = cv_unqualified (type);
177 :
178 5609461 : if (type == error_mark_node)
179 : ;
180 5609429 : else if (REFLECTION_TYPE_P (type))
181 : /* [dcl.init.general]: "if T is std::meta::info, the object is initialized
182 : to a null reflection value". */
183 14101 : init = get_null_reflection ();
184 5595328 : else if (static_storage_p && zero_init_p (type))
185 : /* In order to save space, we do not explicitly build initializers
186 : for items that do not need them. GCC's semantics are that
187 : items with static storage duration that are not otherwise
188 : initialized are initialized to zero. */
189 : ;
190 5067309 : else if (TYPE_PTR_OR_PTRMEM_P (type))
191 1224250 : init = fold (convert (type, nullptr_node));
192 3843059 : else if (NULLPTR_TYPE_P (type))
193 97 : init = build_int_cst (type, 0);
194 147940 : else if (SCALAR_TYPE_P (type))
195 3695022 : init = build_zero_cst (type);
196 147940 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
197 : {
198 134719 : tree field, next;
199 134719 : vec<constructor_elt, va_gc> *v = NULL;
200 :
201 : /* Iterate over the fields, building initializations. */
202 2854942 : for (field = TYPE_FIELDS (type); field; field = next)
203 : {
204 2720223 : next = DECL_CHAIN (field);
205 :
206 2720223 : if (TREE_CODE (field) != FIELD_DECL)
207 2559612 : continue;
208 :
209 : /* For unions, only the first field is initialized. */
210 160611 : if (TREE_CODE (type) == UNION_TYPE)
211 56682 : next = NULL_TREE;
212 :
213 160611 : if (TREE_TYPE (field) == error_mark_node)
214 3 : continue;
215 :
216 : /* Don't add virtual bases for base classes if they are beyond
217 : the size of the current field, that means it is present
218 : somewhere else in the object. */
219 160608 : if (field_size)
220 : {
221 19578 : tree bitpos = bit_position (field);
222 21756 : if (TREE_CODE (bitpos) == INTEGER_CST
223 19578 : && !tree_int_cst_lt (bitpos, field_size))
224 2178 : continue;
225 : }
226 :
227 : /* Don't add zero width bitfields. */
228 158430 : if (DECL_C_BIT_FIELD (field)
229 158430 : && integer_zerop (DECL_SIZE (field)))
230 3 : continue;
231 :
232 : /* Note that for class types there will be FIELD_DECLs
233 : corresponding to base classes as well. Thus, iterating
234 : over TYPE_FIELDs will result in correct initialization of
235 : all of the subobjects. */
236 158427 : if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
237 : {
238 158400 : tree new_field_size
239 158400 : = (DECL_FIELD_IS_BASE (field)
240 12457 : && DECL_SIZE (field)
241 12457 : && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
242 170857 : ? DECL_SIZE (field) : NULL_TREE;
243 158400 : tree value = build_zero_init_1 (TREE_TYPE (field),
244 : /*nelts=*/NULL_TREE,
245 : static_storage_p,
246 : new_field_size);
247 158400 : if (value)
248 158374 : CONSTRUCTOR_APPEND_ELT(v, field, value);
249 : }
250 : }
251 :
252 : /* Build a constructor to contain the initializations. */
253 134719 : init = build_constructor (type, v);
254 134719 : CONSTRUCTOR_ZERO_PADDING_BITS (init) = 1;
255 : }
256 13221 : else if (TREE_CODE (type) == ARRAY_TYPE)
257 : {
258 365 : tree max_index;
259 365 : vec<constructor_elt, va_gc> *v = NULL;
260 :
261 : /* Iterate over the array elements, building initializations. */
262 365 : if (nelts)
263 0 : max_index = fold_build2_loc (input_location, MINUS_EXPR,
264 0 : TREE_TYPE (nelts), nelts,
265 0 : build_one_cst (TREE_TYPE (nelts)));
266 : /* Treat flexible array members like [0] arrays. */
267 365 : else if (TYPE_DOMAIN (type) == NULL_TREE)
268 15 : return NULL_TREE;
269 : else
270 350 : max_index = array_type_nelts_minus_one (type);
271 :
272 : /* If we have an error_mark here, we should just return error mark
273 : as we don't know the size of the array yet. */
274 350 : if (max_index == error_mark_node)
275 : return error_mark_node;
276 350 : gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
277 :
278 : /* A zero-sized array, which is accepted as an extension, will
279 : have an upper bound of -1. */
280 350 : if (!integer_minus_onep (max_index))
281 : {
282 326 : constructor_elt ce;
283 :
284 : /* If this is a one element array, we just use a regular init. */
285 326 : if (integer_zerop (max_index))
286 39 : ce.index = size_zero_node;
287 : else
288 287 : ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
289 : max_index);
290 :
291 326 : ce.value = build_zero_init_1 (TREE_TYPE (type), /*nelts=*/NULL_TREE,
292 : static_storage_p, NULL_TREE);
293 326 : if (ce.value)
294 : {
295 326 : vec_alloc (v, 1);
296 326 : v->quick_push (ce);
297 : }
298 : }
299 :
300 : /* Build a constructor to contain the initializations. */
301 350 : init = build_constructor (type, v);
302 : }
303 12856 : else if (VECTOR_TYPE_P (type))
304 12842 : init = build_zero_cst (type);
305 : else
306 14 : gcc_assert (TYPE_REF_P (type));
307 :
308 : /* In all cases, the initializer is a constant. */
309 5081381 : if (init)
310 5081381 : TREE_CONSTANT (init) = 1;
311 :
312 : return init;
313 : }
314 :
315 : /* Return an expression for the zero-initialization of an object with
316 : type T. This expression will either be a constant (in the case
317 : that T is a scalar), or a CONSTRUCTOR (in the case that T is an
318 : aggregate), or NULL (in the case that T does not require
319 : initialization). In either case, the value can be used as
320 : DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
321 : initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
322 : is the number of elements in the array. If STATIC_STORAGE_P is
323 : TRUE, initializers are only generated for entities for which
324 : zero-initialization does not simply mean filling the storage with
325 : zero bytes. */
326 :
327 : tree
328 5426982 : build_zero_init (tree type, tree nelts, bool static_storage_p)
329 : {
330 5426982 : return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
331 : }
332 :
333 : /* Return a suitable initializer for value-initializing an object of type
334 : TYPE, as described in [dcl.init]. */
335 :
336 : tree
337 7233707 : build_value_init (tree type, tsubst_flags_t complain)
338 : {
339 : /* [dcl.init]
340 :
341 : To value-initialize an object of type T means:
342 :
343 : - if T is a class type (clause 9) with either no default constructor
344 : (12.1) or a default constructor that is user-provided or deleted,
345 : then the object is default-initialized;
346 :
347 : - if T is a (possibly cv-qualified) class type without a user-provided
348 : or deleted default constructor, then the object is zero-initialized
349 : and the semantic constraints for default-initialization are checked,
350 : and if T has a non-trivial default constructor, the object is
351 : default-initialized;
352 :
353 : - if T is an array type, then each element is value-initialized;
354 :
355 : - otherwise, the object is zero-initialized.
356 :
357 : A program that calls for default-initialization or
358 : value-initialization of an entity of reference type is ill-formed. */
359 :
360 7233707 : if (CLASS_TYPE_P (type) && type_build_ctor_call (type))
361 : {
362 1604801 : tree ctor
363 1604801 : = build_special_member_call (NULL_TREE, complete_ctor_identifier,
364 : NULL, type, LOOKUP_NORMAL, complain);
365 1604801 : if (ctor == error_mark_node || TREE_CONSTANT (ctor))
366 : return ctor;
367 1603410 : if (processing_template_decl)
368 : /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
369 6 : return build_min (CAST_EXPR, type, NULL_TREE);
370 1603404 : tree fn = NULL_TREE;
371 1603404 : if (TREE_CODE (ctor) == CALL_EXPR)
372 1293234 : fn = get_callee_fndecl (ctor);
373 1603404 : ctor = build_aggr_init_expr (type, ctor);
374 1603404 : if (fn && user_provided_p (fn))
375 : return ctor;
376 676029 : else if (TYPE_HAS_COMPLEX_DFLT (type))
377 : {
378 : /* This is a class that needs constructing, but doesn't have
379 : a user-provided constructor. So we need to zero-initialize
380 : the object and then call the implicitly defined ctor.
381 : This will be handled in simplify_aggr_init_expr. */
382 326858 : AGGR_INIT_ZERO_FIRST (ctor) = 1;
383 326858 : return ctor;
384 : }
385 : }
386 :
387 : /* Discard any access checking during subobject initialization;
388 : the checks are implied by the call to the ctor which we have
389 : verified is OK (cpp0x/defaulted46.C). */
390 5978077 : push_deferring_access_checks (dk_deferred);
391 5978077 : tree r = build_value_init_noctor (type, complain);
392 5978077 : pop_deferring_access_checks ();
393 5978077 : return r;
394 : }
395 :
396 : /* Like build_value_init, but don't call the constructor for TYPE. Used
397 : for base initializers. */
398 :
399 : tree
400 5978077 : build_value_init_noctor (tree type, tsubst_flags_t complain)
401 : {
402 5978077 : if (!COMPLETE_TYPE_P (type))
403 : {
404 3 : if (complain & tf_error)
405 3 : error ("value-initialization of incomplete type %qT", type);
406 3 : return error_mark_node;
407 : }
408 : /* FIXME the class and array cases should just use digest_init once it is
409 : SFINAE-enabled. */
410 5978074 : if (CLASS_TYPE_P (type))
411 : {
412 1475023 : gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type)
413 : || errorcount != 0);
414 :
415 1475023 : if (TREE_CODE (type) != UNION_TYPE)
416 : {
417 1463187 : tree field;
418 1463187 : vec<constructor_elt, va_gc> *v = NULL;
419 :
420 : /* Iterate over the fields, building initializations. */
421 27283268 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
422 : {
423 25820082 : tree ftype, value;
424 :
425 25820082 : if (TREE_CODE (field) != FIELD_DECL)
426 25187220 : continue;
427 :
428 632862 : ftype = TREE_TYPE (field);
429 :
430 632862 : if (ftype == error_mark_node)
431 9 : continue;
432 :
433 : /* Ignore flexible array members for value initialization. */
434 632859 : if (TREE_CODE (ftype) == ARRAY_TYPE
435 10627 : && !COMPLETE_TYPE_P (ftype)
436 6 : && !TYPE_DOMAIN (ftype)
437 6 : && COMPLETE_TYPE_P (TREE_TYPE (ftype))
438 632859 : && (next_aggregate_field (DECL_CHAIN (field))
439 : == NULL_TREE))
440 6 : continue;
441 :
442 : /* Ignore unnamed zero-width bitfields. */
443 636483 : if (DECL_UNNAMED_BIT_FIELD (field)
444 632860 : && integer_zerop (DECL_SIZE (field)))
445 3 : continue;
446 :
447 : /* Zero-initialize anonymous union and struct members. The
448 : default constructor doesn't exist for those and if NSDMIs
449 : are used, the containing type's default constructor is
450 : non-trivial. */
451 632844 : if (ANON_AGGR_TYPE_P (ftype))
452 99 : value = build_zero_init (ftype, NULL_TREE,
453 : /*static_storage_p=*/false);
454 :
455 : /* We could skip vfields and fields of types with
456 : user-defined constructors, but I think that won't improve
457 : performance at all; it should be simpler in general just
458 : to zero out the entire object than try to only zero the
459 : bits that actually need it. */
460 :
461 : /* Note that for class types there will be FIELD_DECLs
462 : corresponding to base classes as well. Thus, iterating
463 : over TYPE_FIELDs will result in correct initialization of
464 : all of the subobjects. */
465 : else
466 : {
467 632745 : value = build_value_init (ftype, complain);
468 632745 : value = maybe_constant_init (value);
469 : }
470 :
471 632844 : if (value == error_mark_node)
472 : return error_mark_node;
473 :
474 632843 : CONSTRUCTOR_APPEND_ELT (v, field, value);
475 :
476 : /* We shouldn't have gotten here for anything that would need
477 : non-trivial initialization, and gimplify_init_ctor_preeval
478 : would need to be fixed to allow it. */
479 632843 : gcc_assert (TREE_CODE (value) != TARGET_EXPR
480 : && TREE_CODE (value) != AGGR_INIT_EXPR);
481 : }
482 :
483 : /* Build a constructor to contain the zero- initializations. */
484 1463186 : tree ret = build_constructor (type, v);
485 1463186 : CONSTRUCTOR_ZERO_PADDING_BITS (ret) = 1;
486 1463186 : return ret;
487 : }
488 : }
489 4503051 : else if (TREE_CODE (type) == ARRAY_TYPE)
490 : {
491 10659 : vec<constructor_elt, va_gc> *v = NULL;
492 :
493 : /* Iterate over the array elements, building initializations. */
494 10659 : tree max_index = array_type_nelts_minus_one (type);
495 :
496 : /* If we have an error_mark here, we should just return error mark
497 : as we don't know the size of the array yet. */
498 10659 : if (max_index == error_mark_node)
499 : {
500 0 : if (complain & tf_error)
501 0 : error ("cannot value-initialize array of unknown bound %qT",
502 : type);
503 0 : return error_mark_node;
504 : }
505 10659 : gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
506 :
507 : /* A zero-sized array, which is accepted as an extension, will
508 : have an upper bound of -1. */
509 10659 : if (!tree_int_cst_equal (max_index, integer_minus_one_node))
510 : {
511 10659 : constructor_elt ce;
512 :
513 : /* If this is a one element array, we just use a regular init. */
514 10659 : if (tree_int_cst_equal (size_zero_node, max_index))
515 193 : ce.index = size_zero_node;
516 : else
517 10466 : ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
518 :
519 10659 : ce.value = build_value_init (TREE_TYPE (type), complain);
520 10659 : ce.value = maybe_constant_init (ce.value);
521 10659 : if (ce.value == error_mark_node)
522 0 : return error_mark_node;
523 :
524 10659 : vec_alloc (v, 1);
525 10659 : v->quick_push (ce);
526 :
527 : /* We shouldn't have gotten here for anything that would need
528 : non-trivial initialization, and gimplify_init_ctor_preeval
529 : would need to be fixed to allow it. */
530 10659 : gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
531 : && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
532 : }
533 :
534 : /* Build a constructor to contain the initializations. */
535 10659 : return build_constructor (type, v);
536 : }
537 4492392 : else if (TREE_CODE (type) == FUNCTION_TYPE)
538 : {
539 9 : if (complain & tf_error)
540 0 : error ("value-initialization of function type %qT", type);
541 9 : return error_mark_node;
542 : }
543 4492383 : else if (TYPE_REF_P (type))
544 : {
545 169 : if (complain & tf_error)
546 19 : error ("value-initialization of reference type %qT", type);
547 169 : return error_mark_node;
548 : }
549 :
550 4504050 : return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
551 : }
552 :
553 : /* Initialize current class with INIT, a TREE_LIST of arguments for
554 : a target constructor. If TREE_LIST is void_type_node, an empty
555 : initializer list was given. Return the target constructor. */
556 :
557 : static tree
558 366965 : perform_target_ctor (tree init)
559 : {
560 366965 : tree decl = current_class_ref;
561 366965 : tree type = current_class_type;
562 :
563 366965 : init = build_aggr_init (decl, init, LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
564 : tf_warning_or_error);
565 366965 : finish_expr_stmt (init);
566 366965 : if (type_build_dtor_call (type))
567 : {
568 265956 : tree expr = build_delete (input_location,
569 : type, decl, sfk_complete_destructor,
570 : LOOKUP_NORMAL
571 : |LOOKUP_NONVIRTUAL
572 : |LOOKUP_DESTRUCTOR,
573 : 0, tf_warning_or_error);
574 265956 : if (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
575 : {
576 80 : tree base = build_delete (input_location,
577 : type, decl, sfk_base_destructor,
578 : LOOKUP_NORMAL
579 : |LOOKUP_NONVIRTUAL
580 : |LOOKUP_DESTRUCTOR,
581 : 0, tf_warning_or_error);
582 80 : expr = build_if_in_charge (expr, base);
583 : }
584 265956 : if (expr != error_mark_node
585 265956 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
586 256224 : finish_eh_cleanup (expr);
587 : }
588 366965 : return init;
589 : }
590 :
591 : /* Instantiate the default member initializer of MEMBER, if needed.
592 : Only get_nsdmi should use the return value of this function. */
593 :
594 : tree
595 2381047 : maybe_instantiate_nsdmi_init (tree member, tsubst_flags_t complain)
596 : {
597 2381047 : tree init = DECL_INITIAL (member);
598 :
599 : /* tsubst_decl uses void_node to indicate an uninstantiated DMI. */
600 2381047 : if (init == void_node)
601 : {
602 : /* Clear any special tsubst flags; the result of NSDMI instantiation
603 : should be independent of the substitution context. */
604 125210 : complain &= tf_warning_or_error;
605 :
606 125210 : init = DECL_INITIAL (DECL_TI_TEMPLATE (member));
607 125210 : location_t expr_loc
608 125210 : = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member));
609 125210 : if (TREE_CODE (init) == DEFERRED_PARSE)
610 : /* Unparsed. */;
611 : /* Check recursive instantiation. */
612 125210 : else if (DECL_INSTANTIATING_NSDMI_P (member))
613 : {
614 9 : if (complain & tf_error)
615 9 : error_at (expr_loc, "recursive instantiation of default member "
616 : "initializer for %qD", member);
617 9 : init = error_mark_node;
618 : }
619 : else
620 : {
621 125201 : cp_evaluated ev;
622 :
623 125201 : location_t sloc = input_location;
624 125201 : input_location = expr_loc;
625 :
626 125201 : DECL_INSTANTIATING_NSDMI_P (member) = 1;
627 :
628 125201 : bool pushed = false;
629 125201 : tree ctx = type_context_for_name_lookup (member);
630 :
631 125201 : bool push_to_top = maybe_push_to_top_level (member);
632 125201 : if (!currently_open_class (ctx))
633 : {
634 125186 : push_nested_class (ctx);
635 125186 : push_deferring_access_checks (dk_no_deferred);
636 125186 : pushed = true;
637 : }
638 :
639 125201 : inject_this_parameter (ctx, TYPE_UNQUALIFIED);
640 :
641 125201 : start_lambda_scope (member);
642 :
643 : /* Do deferred instantiation of the NSDMI. */
644 125201 : init = tsubst_expr (init, DECL_TI_ARGS (member), complain, member);
645 125201 : init = digest_nsdmi_init (member, init, complain);
646 :
647 125201 : finish_lambda_scope ();
648 :
649 125201 : DECL_INSTANTIATING_NSDMI_P (member) = 0;
650 :
651 125201 : if (init != error_mark_node)
652 125142 : DECL_INITIAL (member) = init;
653 :
654 125201 : if (pushed)
655 : {
656 125186 : pop_deferring_access_checks ();
657 125186 : pop_nested_class ();
658 : }
659 125201 : maybe_pop_from_top_level (push_to_top);
660 :
661 125201 : input_location = sloc;
662 125201 : }
663 : }
664 :
665 2381047 : return init;
666 : }
667 :
668 : /* Return the non-static data initializer for FIELD_DECL MEMBER. */
669 :
670 : tree
671 1836595 : get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
672 : {
673 1836595 : tree save_ccp = current_class_ptr;
674 1836595 : tree save_ccr = current_class_ref;
675 :
676 1836595 : tree init = maybe_instantiate_nsdmi_init (member, complain);
677 :
678 1836595 : if (init && TREE_CODE (init) == DEFERRED_PARSE)
679 : {
680 20 : if (complain & tf_error)
681 : {
682 19 : auto_diagnostic_group d;
683 19 : error ("default member initializer for %qD required before the end "
684 : "of its enclosing class", member);
685 19 : inform (location_of (init), "defined here");
686 19 : DECL_INITIAL (member) = error_mark_node;
687 19 : }
688 20 : init = error_mark_node;
689 : }
690 :
691 1836595 : if (in_ctor)
692 : {
693 1397422 : current_class_ptr = save_ccp;
694 1397422 : current_class_ref = save_ccr;
695 : }
696 : else
697 : {
698 : /* Use a PLACEHOLDER_EXPR when we don't have a 'this' parameter to
699 : refer to; constexpr evaluation knows what to do with it. */
700 439173 : current_class_ref = build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (member));
701 439173 : current_class_ptr = build_address (current_class_ref);
702 : }
703 :
704 : /* Clear processing_template_decl for sake of break_out_target_exprs;
705 : INIT is always non-templated. */
706 1836595 : processing_template_decl_sentinel ptds;
707 :
708 : /* Strip redundant TARGET_EXPR so we don't need to remap it, and
709 : so the aggregate init code below will see a CONSTRUCTOR. */
710 1836595 : bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init));
711 7530 : if (simple_target)
712 7530 : init = TARGET_EXPR_INITIAL (init);
713 1836595 : init = break_out_target_exprs (init, /*loc*/true);
714 1836595 : if (init && TREE_CODE (init) == TARGET_EXPR)
715 : /* In a constructor, this expresses the full initialization, prevent
716 : perform_member_init from calling another constructor (58162). */
717 95834 : TARGET_EXPR_DIRECT_INIT_P (init) = in_ctor;
718 1836595 : if (simple_target && TREE_CODE (init) != CONSTRUCTOR)
719 : /* Now put it back so C++17 copy elision works. */
720 1331 : init = get_target_expr (init);
721 :
722 1836595 : set_target_expr_eliding (init);
723 :
724 1836595 : current_class_ptr = save_ccp;
725 1836595 : current_class_ref = save_ccr;
726 1836595 : return init;
727 1836595 : }
728 :
729 : /* Diagnose the flexible array MEMBER if its INITializer is non-null
730 : and return true if so. Otherwise return false. */
731 :
732 : bool
733 620280 : maybe_reject_flexarray_init (tree member, tree init)
734 : {
735 620280 : tree type = TREE_TYPE (member);
736 :
737 620280 : if (!init
738 620249 : || TREE_CODE (type) != ARRAY_TYPE
739 649798 : || TYPE_DOMAIN (type))
740 : return false;
741 :
742 : /* Point at the flexible array member declaration if it's initialized
743 : in-class, and at the ctor if it's initialized in a ctor member
744 : initializer list. */
745 54 : location_t loc;
746 54 : if (DECL_INITIAL (member) == init
747 54 : || !current_function_decl
748 75 : || DECL_DEFAULTED_FN (current_function_decl))
749 36 : loc = DECL_SOURCE_LOCATION (member);
750 : else
751 18 : loc = DECL_SOURCE_LOCATION (current_function_decl);
752 :
753 54 : error_at (loc, "initializer for flexible array member %q#D", member);
754 54 : return true;
755 : }
756 :
757 : /* If INIT's value can come from a call to std::initializer_list<T>::begin,
758 : return that function. Otherwise, NULL_TREE. */
759 :
760 : static tree
761 296 : find_list_begin (tree init)
762 : {
763 299 : STRIP_NOPS (init);
764 598 : while (TREE_CODE (init) == COMPOUND_EXPR)
765 0 : init = TREE_OPERAND (init, 1);
766 299 : STRIP_NOPS (init);
767 299 : if (TREE_CODE (init) == COND_EXPR)
768 : {
769 3 : tree left = TREE_OPERAND (init, 1);
770 3 : if (!left)
771 0 : left = TREE_OPERAND (init, 0);
772 3 : left = find_list_begin (left);
773 3 : if (left)
774 : return left;
775 3 : return find_list_begin (TREE_OPERAND (init, 2));
776 : }
777 296 : if (TREE_CODE (init) == CALL_EXPR)
778 293 : if (tree fn = get_callee_fndecl (init))
779 293 : if (id_equal (DECL_NAME (fn), "begin")
780 293 : && is_std_init_list (DECL_CONTEXT (fn)))
781 21 : return fn;
782 : return NULL_TREE;
783 : }
784 :
785 : /* If INIT initializing MEMBER is copying the address of the underlying array
786 : of an initializer_list, warn. */
787 :
788 : static void
789 4330683 : maybe_warn_list_ctor (tree member, tree init)
790 : {
791 4330683 : tree memtype = TREE_TYPE (member);
792 3894374 : if (!init || !TYPE_PTR_P (memtype)
793 5922964 : || !is_list_ctor (current_function_decl))
794 : return;
795 :
796 306 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl);
797 306 : parm = TREE_VALUE (parm);
798 306 : tree initlist = non_reference (parm);
799 :
800 : /* Do not warn if the parameter is an lvalue reference to non-const. */
801 291 : if (TYPE_REF_P (parm) && !TYPE_REF_IS_RVALUE (parm)
802 325 : && !CP_TYPE_CONST_P (initlist))
803 : return;
804 :
805 293 : tree targs = CLASSTYPE_TI_ARGS (initlist);
806 293 : tree elttype = TREE_VEC_ELT (targs, 0);
807 :
808 293 : if (!same_type_ignoring_top_level_qualifiers_p
809 293 : (TREE_TYPE (memtype), elttype))
810 : return;
811 :
812 293 : tree begin = find_list_begin (init);
813 293 : if (!begin)
814 : return;
815 :
816 21 : location_t loc = cp_expr_loc_or_input_loc (init);
817 21 : warning_at (loc, OPT_Winit_list_lifetime,
818 : "initializing %qD from %qE does not extend the lifetime "
819 : "of the underlying array", member, begin);
820 : }
821 :
822 : /* Data structure for find_uninit_fields_r, below. */
823 :
824 : struct find_uninit_data {
825 : /* The set tracking the yet-uninitialized members. */
826 : hash_set<tree> *uninitialized;
827 : /* The data member we are currently initializing. It can be either
828 : a type (initializing a base class/delegating constructors), or
829 : a COMPONENT_REF. */
830 : tree member;
831 : };
832 :
833 : /* walk_tree callback that warns about using uninitialized data in
834 : a member-initializer-list. */
835 :
836 : static tree
837 135273 : find_uninit_fields_r (tree *tp, int *walk_subtrees, void *data)
838 : {
839 135273 : find_uninit_data *d = static_cast<find_uninit_data *>(data);
840 135273 : hash_set<tree> *uninitialized = d->uninitialized;
841 135273 : tree init = *tp;
842 135273 : const tree_code code = TREE_CODE (init);
843 :
844 : /* No need to look into types or unevaluated operands. */
845 135273 : if (TYPE_P (init) || unevaluated_p (code))
846 : {
847 14 : *walk_subtrees = false;
848 14 : return NULL_TREE;
849 : }
850 :
851 135259 : switch (code)
852 : {
853 : /* We'd need data flow info to avoid false positives. */
854 431 : case COND_EXPR:
855 431 : case VEC_COND_EXPR:
856 431 : case BIND_EXPR:
857 : /* We might see a MODIFY_EXPR in cases like S() : a((b = 42)), c(b) { }
858 : where the initializer for 'a' surreptitiously initializes 'b'. Let's
859 : not bother with these complicated scenarios in the front end. */
860 431 : case MODIFY_EXPR:
861 : /* Don't attempt to handle statement-expressions, either. */
862 431 : case STATEMENT_LIST:
863 431 : uninitialized->empty ();
864 5553 : gcc_fallthrough ();
865 : /* If we're just taking the address of an object, it doesn't matter
866 : whether it's been initialized. */
867 5553 : case ADDR_EXPR:
868 5553 : *walk_subtrees = false;
869 5553 : return NULL_TREE;
870 129706 : default:
871 129706 : break;
872 : }
873 :
874 : /* We'd need data flow info to avoid false positives. */
875 129706 : if (truth_value_p (code))
876 298 : goto give_up;
877 : /* Attempt to handle a simple a{b}, but no more. */
878 129408 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
879 : {
880 1977 : if (CONSTRUCTOR_NELTS (init) == 1
881 1971 : && !BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (init, 0)->value))
882 : init = CONSTRUCTOR_ELT (init, 0)->value;
883 : else
884 185 : goto give_up;
885 : }
886 : /* Warn about uninitialized 'this'. */
887 127431 : else if (code == CALL_EXPR)
888 : {
889 9990 : tree fn = get_callee_fndecl (init);
890 9990 : if (fn && DECL_IOBJ_MEMBER_FUNCTION_P (fn))
891 : {
892 4183 : tree op = CALL_EXPR_ARG (init, 0);
893 4183 : if (TREE_CODE (op) == ADDR_EXPR)
894 1155 : op = TREE_OPERAND (op, 0);
895 4183 : temp_override<tree> ovr (d->member, DECL_ARGUMENTS (fn));
896 4183 : cp_walk_tree_without_duplicates (&op, find_uninit_fields_r, data);
897 4183 : }
898 : /* Functions (whether static or nonstatic member) may have side effects
899 : and initialize other members; it's not the front end's job to try to
900 : figure it out. But don't give up for constructors: we still want to
901 : warn when initializing base classes:
902 :
903 : struct D : public B {
904 : int x;
905 : D() : B(x) {}
906 : };
907 :
908 : so carry on to detect that 'x' is used uninitialized. */
909 19973 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
910 7711 : goto give_up;
911 : }
912 :
913 : /* If we find FIELD in the uninitialized set, we warn. */
914 121512 : if (code == COMPONENT_REF)
915 : {
916 3101 : tree field = TREE_OPERAND (init, 1);
917 3101 : tree type = TYPE_P (d->member) ? d->member : TREE_TYPE (d->member);
918 :
919 : /* We're initializing a reference member with itself. */
920 3101 : if (TYPE_REF_P (type) && cp_tree_equal (d->member, init))
921 3 : warning_at (EXPR_LOCATION (init), OPT_Winit_self,
922 : "%qD is initialized with itself", field);
923 3098 : else if (cp_tree_equal (TREE_OPERAND (init, 0), current_class_ref)
924 3098 : && uninitialized->contains (field))
925 : {
926 192 : if (TYPE_REF_P (TREE_TYPE (field)))
927 9 : warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
928 : "reference %qD is not yet bound to a value when used "
929 : "here", field);
930 21 : else if ((!INDIRECT_TYPE_P (type) || is_this_parameter (d->member))
931 192 : && !conv_binds_to_reference_parm_p (type, init))
932 162 : warning_at (EXPR_LOCATION (init), OPT_Wuninitialized,
933 : "member %qD is used uninitialized", field);
934 192 : *walk_subtrees = false;
935 : }
936 : }
937 :
938 : return NULL_TREE;
939 :
940 8194 : give_up:
941 8194 : *walk_subtrees = false;
942 8194 : uninitialized->empty ();
943 8194 : return integer_zero_node;
944 : }
945 :
946 : /* Wrapper around find_uninit_fields_r above. */
947 :
948 : static void
949 1428919 : find_uninit_fields (tree *t, hash_set<tree> *uninitialized, tree member)
950 : {
951 1428919 : if (!uninitialized->is_empty ())
952 : {
953 48992 : find_uninit_data data = { uninitialized, member };
954 48992 : cp_walk_tree_without_duplicates (t, find_uninit_fields_r, &data);
955 : }
956 1428919 : }
957 :
958 : /* Return true if it's OK to initialize an array TYPE from INIT. Mere mortals
959 : can't copy arrays, but the compiler can do so with a VEC_INIT_EXPR in
960 : certain cases. */
961 :
962 : static bool
963 719 : can_init_array_with_p (tree type, tree init)
964 : {
965 719 : if (!init)
966 : /* Value-init, OK. */
967 : return true;
968 83 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init)))
969 : return false;
970 : /* We're called from synthesize_method, and we're processing the
971 : mem-initializers of a constructor. */
972 80 : if (DECL_DEFAULTED_FN (current_function_decl))
973 : return true;
974 30 : if (TREE_CODE (init) == TARGET_EXPR)
975 : {
976 18 : init = TARGET_EXPR_INITIAL (init);
977 : /* As an extension, we allow copying from a compound literal. */
978 18 : if (TREE_CODE (init) == CONSTRUCTOR)
979 9 : return CONSTRUCTOR_C99_COMPOUND_LITERAL (init);
980 : /* VEC_INIT_EXPR is used for non-constant initialization of trailing
981 : elements with no explicit initializers. */
982 9 : else if (TREE_CODE (init) == VEC_INIT_EXPR)
983 : return true;
984 : }
985 :
986 12 : permerror (input_location, "array must be initialized "
987 : "with a brace-enclosed initializer");
988 12 : return true;
989 : }
990 :
991 : /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
992 : arguments. If TREE_LIST is void_type_node, an empty initializer
993 : list was given; if NULL_TREE no initializer was given. UNINITIALIZED
994 : is the hash set that tracks uninitialized fields. */
995 :
996 : static void
997 6942720 : perform_member_init (tree member, tree init, hash_set<tree> &uninitialized)
998 : {
999 6942720 : tree decl;
1000 6942720 : tree type = TREE_TYPE (member);
1001 :
1002 : /* Use the non-static data member initializer if there was no
1003 : mem-initializer for this field. */
1004 6942720 : if (init == NULL_TREE)
1005 1397422 : init = get_nsdmi (member, /*ctor*/true, tf_warning_or_error);
1006 :
1007 6942720 : if (init == error_mark_node)
1008 : return;
1009 :
1010 : /* Effective C++ rule 12 requires that all data members be
1011 : initialized. */
1012 6942695 : if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
1013 0 : warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
1014 : "%qD should be initialized in the member initialization list",
1015 : member);
1016 :
1017 : /* Get an lvalue for the data member. */
1018 6942695 : decl = build_class_member_access_expr (current_class_ref, member,
1019 : /*access_path=*/NULL_TREE,
1020 : /*preserve_reference=*/true,
1021 : tf_warning_or_error);
1022 6942695 : if (decl == error_mark_node)
1023 : return;
1024 :
1025 6869986 : if ((warn_init_self || warn_uninitialized || warn_self_move)
1026 73720 : && init
1027 57427 : && TREE_CODE (init) == TREE_LIST
1028 6994546 : && TREE_CHAIN (init) == NULL_TREE)
1029 : {
1030 49227 : tree val = TREE_VALUE (init);
1031 : /* Handle references. */
1032 49227 : if (REFERENCE_REF_P (val))
1033 3548 : val = TREE_OPERAND (val, 0);
1034 2073 : if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
1035 50977 : && TREE_OPERAND (val, 0) == current_class_ref)
1036 27 : warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1037 27 : OPT_Winit_self, "%qD is initialized with itself",
1038 : member);
1039 98400 : else if (!maybe_warn_self_move (input_location, member,
1040 49200 : TREE_VALUE (init)))
1041 49194 : find_uninit_fields (&val, &uninitialized, decl);
1042 : }
1043 :
1044 6942689 : if (array_of_unknown_bound_p (type))
1045 : {
1046 52 : maybe_reject_flexarray_init (member, init);
1047 52 : return;
1048 : }
1049 :
1050 6942637 : if (init && TREE_CODE (init) == TREE_LIST)
1051 : {
1052 : /* A(): a{e} */
1053 5118100 : if (DIRECT_LIST_INIT_P (TREE_VALUE (init)))
1054 495270 : init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
1055 : tf_warning_or_error);
1056 : /* We are trying to initialize an array from a ()-list. If we
1057 : should attempt to do so, conjure up a CONSTRUCTOR. */
1058 4622830 : else if (TREE_CODE (type) == ARRAY_TYPE
1059 : /* P0960 is a C++20 feature. */
1060 188 : && cxx_dialect >= cxx20)
1061 146 : init = do_aggregate_paren_init (init, type);
1062 4622684 : else if (!CLASS_TYPE_P (type))
1063 3330760 : init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
1064 : tf_warning_or_error);
1065 : /* If we're initializing a class from a ()-list, leave the TREE_LIST
1066 : alone: we might call an appropriate constructor, or (in C++20)
1067 : do aggregate-initialization. */
1068 : }
1069 :
1070 : /* Assume we are initializing the member. */
1071 6942637 : bool member_initialized_p = true;
1072 :
1073 6942637 : if (init == void_type_node)
1074 : {
1075 : /* mem() means value-initialization. */
1076 427171 : if (TREE_CODE (type) == ARRAY_TYPE)
1077 : {
1078 229 : init = build_vec_init_expr (type, init, tf_warning_or_error);
1079 229 : init = cp_build_init_expr (decl, init);
1080 229 : finish_expr_stmt (init);
1081 : }
1082 : else
1083 : {
1084 426942 : tree value = build_value_init (type, tf_warning_or_error);
1085 426942 : if (value == error_mark_node)
1086 : return;
1087 426930 : init = cp_build_init_expr (decl, value);
1088 426930 : finish_expr_stmt (init);
1089 : }
1090 : }
1091 : /* Deal with this here, as we will get confused if we try to call the
1092 : assignment op for an anonymous union. This can happen in a
1093 : synthesized copy constructor. */
1094 6515466 : else if (ANON_AGGR_TYPE_P (type))
1095 : {
1096 12 : if (init)
1097 : {
1098 12 : init = cp_build_init_expr (decl, TREE_VALUE (init));
1099 12 : finish_expr_stmt (init);
1100 : }
1101 : }
1102 6515454 : else if (init
1103 6515454 : && (TYPE_REF_P (type)
1104 5655716 : || (TREE_CODE (init) == CONSTRUCTOR
1105 607013 : && (CP_AGGREGATE_TYPE_P (type)
1106 472484 : || is_std_init_list (type)))))
1107 : {
1108 : /* With references and list-initialization, we need to deal with
1109 : extending temporary lifetimes. 12.2p5: "A temporary bound to a
1110 : reference member in a constructor's ctor-initializer (12.6.2)
1111 : persists until the constructor exits." */
1112 189305 : unsigned i; tree t;
1113 378610 : releasing_vec cleanups;
1114 189305 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1115 : {
1116 23086 : if (BRACE_ENCLOSED_INITIALIZER_P (init)
1117 99681 : && CP_AGGREGATE_TYPE_P (type))
1118 23035 : init = reshape_init (type, init, tf_warning_or_error);
1119 76595 : init = digest_init (type, init, tf_warning_or_error);
1120 : }
1121 189305 : if (init == error_mark_node)
1122 : return;
1123 189273 : if (is_empty_field (member)
1124 189273 : && !TREE_SIDE_EFFECTS (init))
1125 : /* Don't add trivial initialization of an empty base/field, as they
1126 : might not be ordered the way the back-end expects. */
1127 : return;
1128 : /* A FIELD_DECL doesn't really have a suitable lifetime, but
1129 : make_temporary_var_for_ref_to_temp will treat it as automatic and
1130 : set_up_extended_ref_temp wants to use the decl in a warning. */
1131 166734 : init = extend_ref_init_temps (member, init, &cleanups);
1132 166734 : if (TREE_CODE (type) == ARRAY_TYPE
1133 166734 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
1134 4 : init = build_vec_init_expr (type, init, tf_warning_or_error);
1135 166734 : init = cp_build_init_expr (decl, init);
1136 166734 : finish_expr_stmt (init);
1137 333558 : FOR_EACH_VEC_ELT (*cleanups, i, t)
1138 90 : push_cleanup (NULL_TREE, t, false);
1139 22571 : }
1140 6326149 : else if (type_build_ctor_call (type)
1141 6326149 : || (init && CLASS_TYPE_P (strip_array_types (type))))
1142 : {
1143 1995466 : if (TREE_CODE (type) == ARRAY_TYPE)
1144 : {
1145 719 : if (can_init_array_with_p (type, init))
1146 : {
1147 716 : if (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1148 : {
1149 : /* Initialize the array only if it's not a flexible
1150 : array member (i.e., if it has an upper bound). */
1151 716 : init = build_vec_init_expr (type, init, tf_warning_or_error);
1152 716 : init = cp_build_init_expr (decl, init);
1153 716 : finish_expr_stmt (init);
1154 : }
1155 : }
1156 : else
1157 3 : error ("invalid initializer for array member %q#D", member);
1158 : }
1159 : else
1160 : {
1161 1994747 : int flags = LOOKUP_NORMAL;
1162 1994747 : if (DECL_DEFAULTED_FN (current_function_decl))
1163 323443 : flags |= LOOKUP_DEFAULTED;
1164 1994747 : if (CP_TYPE_CONST_P (type)
1165 1177 : && init == NULL_TREE
1166 1994794 : && default_init_uninitialized_part (type))
1167 : {
1168 : /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
1169 : vtable; still give this diagnostic. */
1170 3 : auto_diagnostic_group d;
1171 3 : if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1172 : "uninitialized const member in %q#T", type))
1173 3 : inform (DECL_SOURCE_LOCATION (member),
1174 : "%q#D should be initialized", member );
1175 3 : }
1176 1994747 : finish_expr_stmt (build_aggr_init (decl, init, flags,
1177 : tf_warning_or_error));
1178 : }
1179 : }
1180 : else
1181 : {
1182 4330683 : if (init == NULL_TREE)
1183 : {
1184 436309 : tree core_type;
1185 : /* member traversal: note it leaves init NULL */
1186 436309 : if (TYPE_REF_P (type))
1187 : {
1188 7 : auto_diagnostic_group d;
1189 7 : if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1190 : "uninitialized reference member in %q#T", type))
1191 7 : inform (DECL_SOURCE_LOCATION (member),
1192 : "%q#D should be initialized", member);
1193 7 : }
1194 436302 : else if (CP_TYPE_CONST_P (type))
1195 : {
1196 13 : auto_diagnostic_group d;
1197 13 : if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1198 : "uninitialized const member in %q#T", type))
1199 13 : inform (DECL_SOURCE_LOCATION (member),
1200 : "%q#D should be initialized", member );
1201 13 : }
1202 :
1203 436309 : core_type = strip_array_types (type);
1204 :
1205 38588 : if (CLASS_TYPE_P (core_type)
1206 474888 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
1207 38577 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
1208 3 : diagnose_uninitialized_cst_or_ref_member (core_type,
1209 : /*using_new=*/false,
1210 : /*complain=*/true);
1211 :
1212 : /* We left the member uninitialized. */
1213 : member_initialized_p = false;
1214 : }
1215 :
1216 4330683 : maybe_warn_list_ctor (member, init);
1217 :
1218 4330683 : if (init)
1219 3894374 : finish_expr_stmt (cp_build_modify_expr (input_location, decl,
1220 : INIT_EXPR, init,
1221 : tf_warning_or_error));
1222 : }
1223 :
1224 6920054 : if (member_initialized_p && warn_uninitialized)
1225 : /* This member is now initialized, remove it from the uninitialized
1226 : set. */
1227 60942 : uninitialized.remove (member);
1228 :
1229 6920054 : if (type_build_dtor_call (type))
1230 : {
1231 1189614 : tree expr;
1232 :
1233 1189614 : expr = build_class_member_access_expr (current_class_ref, member,
1234 : /*access_path=*/NULL_TREE,
1235 : /*preserve_reference=*/false,
1236 : tf_warning_or_error);
1237 1189614 : expr = build_delete (input_location,
1238 : type, expr, sfk_complete_destructor,
1239 : LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
1240 : tf_warning_or_error);
1241 :
1242 1189614 : if (expr != error_mark_node
1243 1189614 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1244 1025058 : finish_eh_cleanup (expr);
1245 : }
1246 : }
1247 :
1248 : /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
1249 : the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
1250 :
1251 : static tree
1252 6550721 : build_field_list (tree t, tree list, int *uses_unions_or_anon_p)
1253 : {
1254 6550721 : tree fields;
1255 :
1256 : /* Note whether or not T is a union. */
1257 6550721 : if (TREE_CODE (t) == UNION_TYPE)
1258 343143 : *uses_unions_or_anon_p = 1;
1259 :
1260 277869675 : for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
1261 : {
1262 271318954 : tree fieldtype;
1263 :
1264 : /* Skip CONST_DECLs for enumeration constants and so forth. */
1265 271318954 : if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
1266 263222573 : continue;
1267 :
1268 8096381 : fieldtype = TREE_TYPE (fields);
1269 :
1270 : /* For an anonymous struct or union, we must recursively
1271 : consider the fields of the anonymous type. They can be
1272 : directly initialized from the constructor. */
1273 8096381 : if (ANON_AGGR_TYPE_P (fieldtype))
1274 : {
1275 : /* Add this field itself. Synthesized copy constructors
1276 : initialize the entire aggregate. */
1277 377713 : list = tree_cons (fields, NULL_TREE, list);
1278 : /* And now add the fields in the anonymous aggregate. */
1279 377713 : list = build_field_list (fieldtype, list, uses_unions_or_anon_p);
1280 377713 : *uses_unions_or_anon_p = 1;
1281 : }
1282 : /* Add this field. */
1283 7718668 : else if (DECL_NAME (fields))
1284 7718626 : list = tree_cons (fields, NULL_TREE, list);
1285 : }
1286 :
1287 6550721 : return list;
1288 : }
1289 :
1290 : /* Return the innermost aggregate scope for FIELD, whether that is
1291 : the enclosing class or an anonymous aggregate within it. */
1292 :
1293 : static tree
1294 1419926 : innermost_aggr_scope (tree field)
1295 : {
1296 1419926 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1297 21 : return TREE_TYPE (field);
1298 : else
1299 1419905 : return DECL_CONTEXT (field);
1300 : }
1301 :
1302 : /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
1303 : a FIELD_DECL or BINFO in T that needs initialization. The
1304 : TREE_VALUE gives the initializer, or list of initializer arguments.
1305 :
1306 : Return a TREE_LIST containing all of the initializations required
1307 : for T, in the order in which they should be performed. The output
1308 : list has the same format as the input. */
1309 :
1310 : static tree
1311 6173008 : sort_mem_initializers (tree t, tree mem_inits)
1312 : {
1313 6173008 : tree init;
1314 6173008 : tree base, binfo, base_binfo;
1315 6173008 : tree sorted_inits;
1316 6173008 : tree next_subobject;
1317 6173008 : vec<tree, va_gc> *vbases;
1318 6173008 : int i;
1319 6173008 : int uses_unions_or_anon_p = 0;
1320 :
1321 : /* Build up a list of initializations. The TREE_PURPOSE of entry
1322 : will be the subobject (a FIELD_DECL or BINFO) to initialize. The
1323 : TREE_VALUE will be the constructor arguments, or NULL if no
1324 : explicit initialization was provided. */
1325 6173008 : sorted_inits = NULL_TREE;
1326 :
1327 : /* Process the virtual bases. */
1328 6173008 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
1329 6196270 : vec_safe_iterate (vbases, i, &base); i++)
1330 23262 : sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
1331 :
1332 : /* Process the direct bases. */
1333 8613910 : for (binfo = TYPE_BINFO (t), i = 0;
1334 8613910 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1335 2440902 : if (!BINFO_VIRTUAL_P (base_binfo))
1336 2433488 : sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
1337 :
1338 : /* Process the non-static data members. */
1339 6173008 : sorted_inits = build_field_list (t, sorted_inits, &uses_unions_or_anon_p);
1340 : /* Reverse the entire list of initializations, so that they are in
1341 : the order that they will actually be performed. */
1342 6173008 : sorted_inits = nreverse (sorted_inits);
1343 :
1344 : /* If the user presented the initializers in an order different from
1345 : that in which they will actually occur, we issue a warning. Keep
1346 : track of the next subobject which can be explicitly initialized
1347 : without issuing a warning. */
1348 6173008 : next_subobject = sorted_inits;
1349 :
1350 : /* Go through the explicit initializers, filling in TREE_PURPOSE in
1351 : the SORTED_INITS. */
1352 13165536 : for (init = mem_inits; init; init = TREE_CHAIN (init))
1353 : {
1354 6992528 : tree subobject;
1355 6992528 : tree subobject_init;
1356 :
1357 6992528 : subobject = TREE_PURPOSE (init);
1358 :
1359 : /* If the explicit initializers are in sorted order, then
1360 : SUBOBJECT will be NEXT_SUBOBJECT, or something following
1361 : it. */
1362 6992528 : for (subobject_init = next_subobject;
1363 9771292 : subobject_init;
1364 2778764 : subobject_init = TREE_CHAIN (subobject_init))
1365 9771112 : if (TREE_PURPOSE (subobject_init) == subobject)
1366 : break;
1367 :
1368 : /* Issue a warning if the explicit initializer order does not
1369 : match that which will actually occur.
1370 : ??? Are all these on the correct lines? */
1371 6992528 : if (warn_reorder && !subobject_init)
1372 : {
1373 12 : if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
1374 6 : warning_at (DECL_SOURCE_LOCATION (TREE_PURPOSE (next_subobject)),
1375 6 : OPT_Wreorder, "%qD will be initialized after",
1376 6 : TREE_PURPOSE (next_subobject));
1377 : else
1378 6 : warning (OPT_Wreorder, "base %qT will be initialized after",
1379 6 : TREE_PURPOSE (next_subobject));
1380 12 : if (TREE_CODE (subobject) == FIELD_DECL)
1381 3 : warning_at (DECL_SOURCE_LOCATION (subobject),
1382 3 : OPT_Wreorder, " %q#D", subobject);
1383 : else
1384 9 : warning (OPT_Wreorder, " base %qT", subobject);
1385 12 : warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1386 12 : OPT_Wreorder, " when initialized here");
1387 : }
1388 :
1389 : /* Look again, from the beginning of the list. */
1390 6927906 : if (!subobject_init)
1391 : {
1392 : subobject_init = sorted_inits;
1393 243 : while (TREE_PURPOSE (subobject_init) != subobject)
1394 63 : subobject_init = TREE_CHAIN (subobject_init);
1395 : }
1396 :
1397 : /* It is invalid to initialize the same subobject more than
1398 : once. */
1399 6992528 : if (TREE_VALUE (subobject_init))
1400 : {
1401 0 : if (TREE_CODE (subobject) == FIELD_DECL)
1402 0 : error_at (DECL_SOURCE_LOCATION (current_function_decl),
1403 : "multiple initializations given for %qD",
1404 : subobject);
1405 : else
1406 0 : error_at (DECL_SOURCE_LOCATION (current_function_decl),
1407 : "multiple initializations given for base %qT",
1408 : subobject);
1409 : }
1410 :
1411 : /* Record the initialization. */
1412 6992528 : TREE_VALUE (subobject_init) = TREE_VALUE (init);
1413 : /* Carry over the dummy TREE_TYPE node containing the source location. */
1414 6992528 : TREE_TYPE (subobject_init) = TREE_TYPE (init);
1415 6992528 : next_subobject = subobject_init;
1416 : }
1417 :
1418 : /* [class.base.init]
1419 :
1420 : If a ctor-initializer specifies more than one mem-initializer for
1421 : multiple members of the same union (including members of
1422 : anonymous unions), the ctor-initializer is ill-formed.
1423 :
1424 : Here we also splice out uninitialized union members. */
1425 6173008 : if (uses_unions_or_anon_p)
1426 : {
1427 : tree *last_p = NULL;
1428 : tree *p;
1429 2131516 : for (p = &sorted_inits; *p; )
1430 : {
1431 1788427 : tree field;
1432 1788427 : tree ctx;
1433 :
1434 1788427 : init = *p;
1435 :
1436 1788427 : field = TREE_PURPOSE (init);
1437 :
1438 : /* Skip base classes. */
1439 1788427 : if (TREE_CODE (field) != FIELD_DECL)
1440 584 : goto next;
1441 :
1442 : /* If this is an anonymous aggregate with no explicit initializer,
1443 : splice it out. */
1444 1787843 : if (!TREE_VALUE (init) && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1445 377701 : goto splice;
1446 :
1447 : /* See if this field is a member of a union, or a member of a
1448 : structure contained in a union, etc. */
1449 1410142 : ctx = innermost_aggr_scope (field);
1450 :
1451 : /* If this field is not a member of a union, skip it. */
1452 1410142 : if (TREE_CODE (ctx) != UNION_TYPE
1453 1410142 : && !ANON_AGGR_TYPE_P (ctx))
1454 579647 : goto next;
1455 :
1456 : /* If this union member has no explicit initializer and no NSDMI,
1457 : splice it out. */
1458 830495 : if (TREE_VALUE (init) || DECL_INITIAL (field))
1459 : /* OK. */;
1460 : else
1461 766242 : goto splice;
1462 :
1463 : /* It's only an error if we have two initializers for the same
1464 : union type. */
1465 64253 : if (!last_p)
1466 : {
1467 54469 : last_p = p;
1468 54469 : goto next;
1469 : }
1470 :
1471 : /* See if LAST_FIELD and the field initialized by INIT are
1472 : members of the same union (or the union itself). If so, there's
1473 : a problem, unless they're actually members of the same structure
1474 : which is itself a member of a union. For example, given:
1475 :
1476 : union { struct { int i; int j; }; };
1477 :
1478 : initializing both `i' and `j' makes sense. */
1479 9784 : ctx = common_enclosing_class
1480 9784 : (innermost_aggr_scope (field),
1481 9784 : innermost_aggr_scope (TREE_PURPOSE (*last_p)));
1482 :
1483 9784 : if (ctx && (TREE_CODE (ctx) == UNION_TYPE
1484 111 : || ctx == TREE_TYPE (TREE_PURPOSE (*last_p))))
1485 : {
1486 : /* A mem-initializer hides an NSDMI. */
1487 9676 : if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
1488 9649 : *last_p = TREE_CHAIN (*last_p);
1489 27 : else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
1490 9 : goto splice;
1491 : else
1492 : {
1493 18 : error_at (DECL_SOURCE_LOCATION (current_function_decl),
1494 : "initializations for multiple members of %qT",
1495 : ctx);
1496 18 : goto splice;
1497 : }
1498 : }
1499 :
1500 : last_p = p;
1501 :
1502 644457 : next:
1503 644457 : p = &TREE_CHAIN (*p);
1504 644457 : continue;
1505 1143970 : splice:
1506 1143970 : *p = TREE_CHAIN (*p);
1507 644457 : }
1508 : }
1509 :
1510 6173008 : return sorted_inits;
1511 : }
1512 :
1513 : /* Callback for cp_walk_tree to mark all PARM_DECLs in a tree as read. */
1514 :
1515 : static tree
1516 258 : mark_exp_read_r (tree *tp, int *, void *)
1517 : {
1518 258 : tree t = *tp;
1519 258 : if (TREE_CODE (t) == PARM_DECL)
1520 45 : mark_exp_read (t);
1521 258 : return NULL_TREE;
1522 : }
1523 :
1524 : /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1525 : is a TREE_LIST giving the explicit mem-initializer-list for the
1526 : constructor. The TREE_PURPOSE of each entry is a subobject (a
1527 : FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1528 : is a TREE_LIST giving the arguments to the constructor or
1529 : void_type_node for an empty list of arguments. */
1530 :
1531 : void
1532 6539973 : emit_mem_initializers (tree mem_inits)
1533 : {
1534 6539973 : int flags = LOOKUP_NORMAL;
1535 :
1536 : /* We will already have issued an error message about the fact that
1537 : the type is incomplete. */
1538 6539973 : if (!COMPLETE_TYPE_P (current_class_type))
1539 366965 : return;
1540 :
1541 : /* Keep a set holding fields that are not initialized. */
1542 6539973 : hash_set<tree> uninitialized;
1543 :
1544 : /* Initially that is all of them. */
1545 6539973 : if (warn_uninitialized)
1546 54308 : for (tree f = next_aggregate_field (TYPE_FIELDS (current_class_type));
1547 145932 : f != NULL_TREE;
1548 91624 : f = next_aggregate_field (DECL_CHAIN (f)))
1549 91624 : if (!DECL_ARTIFICIAL (f)
1550 91624 : && !is_really_empty_class (TREE_TYPE (f), /*ignore_vptr*/false))
1551 77577 : uninitialized.add (f);
1552 :
1553 6539973 : if (mem_inits
1554 5314044 : && TYPE_P (TREE_PURPOSE (mem_inits))
1555 6906938 : && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1556 : {
1557 : /* Delegating constructor. */
1558 366965 : gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
1559 366965 : tree ctor = perform_target_ctor (TREE_VALUE (mem_inits));
1560 366965 : find_uninit_fields (&ctor, &uninitialized, current_class_type);
1561 366965 : return;
1562 : }
1563 :
1564 6173008 : if (DECL_DEFAULTED_FN (current_function_decl)
1565 6843867 : && ! DECL_INHERITED_CTOR (current_function_decl))
1566 : flags |= LOOKUP_DEFAULTED;
1567 :
1568 : /* Sort the mem-initializers into the order in which the
1569 : initializations should be performed. */
1570 6173008 : mem_inits = sort_mem_initializers (current_class_type, mem_inits);
1571 :
1572 6173008 : in_base_initializer = 1;
1573 :
1574 : /* Initialize base classes. */
1575 6173008 : for (; (mem_inits
1576 15468602 : && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
1577 2456750 : mem_inits = TREE_CHAIN (mem_inits))
1578 : {
1579 2456750 : tree subobject = TREE_PURPOSE (mem_inits);
1580 2456750 : tree arguments = TREE_VALUE (mem_inits);
1581 :
1582 : /* We already have issued an error message. */
1583 2456750 : if (arguments == error_mark_node)
1584 3 : continue;
1585 :
1586 : /* Suppress access control when calling the inherited ctor. */
1587 4913494 : bool inherited_base = (DECL_INHERITED_CTOR (current_function_decl)
1588 54625 : && flag_new_inheriting_ctors
1589 2511363 : && arguments);
1590 54526 : if (inherited_base)
1591 54526 : push_deferring_access_checks (dk_deferred);
1592 :
1593 2456747 : if (arguments == NULL_TREE)
1594 : {
1595 : /* If these initializations are taking place in a copy constructor,
1596 : the base class should probably be explicitly initialized if there
1597 : is a user-defined constructor in the base class (other than the
1598 : default constructor, which will be called anyway). */
1599 1009535 : if (extra_warnings
1600 13230 : && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1601 1009590 : && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1602 6 : warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1603 3 : OPT_Wextra, "base class %q#T should be explicitly "
1604 : "initialized in the copy constructor",
1605 3 : BINFO_TYPE (subobject));
1606 : }
1607 :
1608 : /* Initialize the base. */
1609 2456747 : if (!BINFO_VIRTUAL_P (subobject))
1610 : {
1611 2433485 : tree base_addr;
1612 :
1613 2433485 : base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1614 : subobject, 1, tf_warning_or_error);
1615 2433485 : expand_aggr_init_1 (subobject, NULL_TREE,
1616 : cp_build_fold_indirect_ref (base_addr),
1617 : arguments,
1618 : flags,
1619 : tf_warning_or_error);
1620 2433485 : expand_cleanup_for_base (subobject, NULL_TREE);
1621 2433485 : if (STATEMENT_LIST_TAIL (cur_stmt_list))
1622 2025520 : find_uninit_fields (&STATEMENT_LIST_TAIL (cur_stmt_list)->stmt,
1623 1012760 : &uninitialized, BINFO_TYPE (subobject));
1624 : }
1625 23262 : else if (!ABSTRACT_CLASS_TYPE_P (current_class_type))
1626 : /* C++14 DR1658 Means we do not have to construct vbases of
1627 : abstract classes. */
1628 23073 : construct_virtual_base (subobject, arguments);
1629 : else
1630 : /* When not constructing vbases of abstract classes, at least mark
1631 : the arguments expressions as read to avoid
1632 : -Wunused-but-set-parameter false positives. */
1633 189 : cp_walk_tree (&arguments, mark_exp_read_r, NULL, NULL);
1634 :
1635 2456747 : if (inherited_base)
1636 54526 : pop_deferring_access_checks ();
1637 : }
1638 6173008 : in_base_initializer = 0;
1639 :
1640 : /* Initialize the vptrs. */
1641 6173008 : initialize_vtbl_ptrs (current_class_ptr);
1642 :
1643 : /* Initialize the data members. */
1644 19288736 : while (mem_inits)
1645 : {
1646 : /* If this initializer was explicitly provided, then the dummy TREE_TYPE
1647 : node contains the source location. */
1648 6942720 : iloc_sentinel ils (EXPR_LOCATION (TREE_TYPE (mem_inits)));
1649 :
1650 6942720 : perform_member_init (TREE_PURPOSE (mem_inits),
1651 6942720 : TREE_VALUE (mem_inits),
1652 : uninitialized);
1653 :
1654 6942720 : mem_inits = TREE_CHAIN (mem_inits);
1655 6942720 : }
1656 6539973 : }
1657 :
1658 : /* Returns the address of the vtable (i.e., the value that should be
1659 : assigned to the vptr) for BINFO. */
1660 :
1661 : tree
1662 1194368 : build_vtbl_address (tree binfo)
1663 : {
1664 1194368 : tree binfo_for = binfo;
1665 1194368 : tree vtbl;
1666 :
1667 1194368 : if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1668 : /* If this is a virtual primary base, then the vtable we want to store
1669 : is that for the base this is being used as the primary base of. We
1670 : can't simply skip the initialization, because we may be expanding the
1671 : inits of a subobject constructor where the virtual base layout
1672 : can be different. */
1673 54558 : while (BINFO_PRIMARY_P (binfo_for))
1674 14528 : binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1675 :
1676 : /* Figure out what vtable BINFO's vtable is based on, and mark it as
1677 : used. */
1678 1194368 : vtbl = get_vtbl_decl_for_binfo (binfo_for);
1679 1194368 : TREE_USED (vtbl) = true;
1680 :
1681 : /* Now compute the address to use when initializing the vptr. */
1682 1194368 : vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1683 1194368 : if (VAR_P (vtbl))
1684 0 : vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1685 :
1686 1194368 : return vtbl;
1687 : }
1688 :
1689 : /* This code sets up the virtual function tables appropriate for
1690 : the pointer DECL. It is a one-ply initialization.
1691 :
1692 : BINFO is the exact type that DECL is supposed to be. In
1693 : multiple inheritance, this might mean "C's A" if C : A, B. */
1694 :
1695 : static void
1696 1194365 : expand_virtual_init (tree binfo, tree decl)
1697 : {
1698 1194365 : tree vtbl, vtbl_ptr;
1699 1194365 : tree vtt_index;
1700 :
1701 : /* Compute the initializer for vptr. */
1702 1194365 : vtbl = build_vtbl_address (binfo);
1703 :
1704 : /* We may get this vptr from a VTT, if this is a subobject
1705 : constructor or subobject destructor. */
1706 1194365 : vtt_index = BINFO_VPTR_INDEX (binfo);
1707 1194365 : if (vtt_index)
1708 : {
1709 60575 : tree vtbl2;
1710 60575 : tree vtt_parm;
1711 :
1712 : /* Compute the value to use, when there's a VTT. */
1713 60575 : vtt_parm = current_vtt_parm;
1714 60575 : vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1715 60575 : vtbl2 = cp_build_fold_indirect_ref (vtbl2);
1716 60575 : vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1717 :
1718 : /* The actual initializer is the VTT value only in the subobject
1719 : constructor. In maybe_clone_body we'll substitute NULL for
1720 : the vtt_parm in the case of the non-subobject constructor. */
1721 60575 : vtbl = build_if_in_charge (vtbl, vtbl2);
1722 : }
1723 :
1724 : /* Compute the location of the vtpr. */
1725 1194365 : vtbl_ptr = build_vfield_ref (cp_build_fold_indirect_ref (decl),
1726 1194365 : TREE_TYPE (binfo));
1727 1194365 : gcc_assert (vtbl_ptr != error_mark_node);
1728 :
1729 : /* Assign the vtable to the vptr. */
1730 1194365 : vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
1731 1194365 : finish_expr_stmt (cp_build_modify_expr (input_location, vtbl_ptr, NOP_EXPR,
1732 : vtbl, tf_warning_or_error));
1733 1194365 : }
1734 :
1735 : /* If an exception is thrown in a constructor, those base classes already
1736 : constructed must be destroyed. This function creates the cleanup
1737 : for BINFO, which has just been constructed. If FLAG is non-NULL,
1738 : it is a DECL which is nonzero when this base needs to be
1739 : destroyed. */
1740 :
1741 : static void
1742 2456558 : expand_cleanup_for_base (tree binfo, tree flag)
1743 : {
1744 2456558 : tree expr;
1745 :
1746 2456558 : if (!type_build_dtor_call (BINFO_TYPE (binfo)))
1747 : return;
1748 :
1749 : /* Call the destructor. */
1750 1157892 : expr = build_special_member_call (current_class_ref,
1751 : base_dtor_identifier,
1752 : NULL,
1753 : binfo,
1754 : LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1755 : tf_warning_or_error);
1756 :
1757 1157892 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1758 : return;
1759 :
1760 1012304 : if (flag)
1761 19502 : expr = fold_build3_loc (input_location,
1762 : COND_EXPR, void_type_node,
1763 : c_common_truthvalue_conversion (input_location, flag),
1764 : expr, integer_zero_node);
1765 :
1766 1012304 : finish_eh_cleanup (expr);
1767 : }
1768 :
1769 : /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1770 : constructor. */
1771 :
1772 : static void
1773 23073 : construct_virtual_base (tree vbase, tree arguments)
1774 : {
1775 23073 : tree inner_if_stmt;
1776 23073 : tree exp;
1777 23073 : tree flag;
1778 :
1779 : /* If there are virtual base classes with destructors, we need to
1780 : emit cleanups to destroy them if an exception is thrown during
1781 : the construction process. These exception regions (i.e., the
1782 : period during which the cleanups must occur) begin from the time
1783 : the construction is complete to the end of the function. If we
1784 : create a conditional block in which to initialize the
1785 : base-classes, then the cleanup region for the virtual base begins
1786 : inside a block, and ends outside of that block. This situation
1787 : confuses the sjlj exception-handling code. Therefore, we do not
1788 : create a single conditional block, but one for each
1789 : initialization. (That way the cleanup regions always begin
1790 : in the outer block.) We trust the back end to figure out
1791 : that the FLAG will not change across initializations, and
1792 : avoid doing multiple tests. */
1793 23073 : flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1794 23073 : inner_if_stmt = begin_if_stmt ();
1795 23073 : finish_if_stmt_cond (flag, inner_if_stmt);
1796 :
1797 : /* Compute the location of the virtual base. If we're
1798 : constructing virtual bases, then we must be the most derived
1799 : class. Therefore, we don't have to look up the virtual base;
1800 : we already know where it is. */
1801 23073 : exp = convert_to_base_statically (current_class_ref, vbase);
1802 :
1803 23073 : expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1804 : 0, tf_warning_or_error);
1805 23073 : finish_then_clause (inner_if_stmt);
1806 23073 : finish_if_stmt (inner_if_stmt);
1807 :
1808 23073 : expand_cleanup_for_base (vbase, flag);
1809 23073 : }
1810 :
1811 : /* Find the context in which this FIELD can be initialized. */
1812 :
1813 : static tree
1814 20826304 : initializing_context (tree field)
1815 : {
1816 20826304 : tree t = DECL_CONTEXT (field);
1817 :
1818 : /* Anonymous union members can be initialized in the first enclosing
1819 : non-anonymous union context. */
1820 20987686 : while (t && ANON_AGGR_TYPE_P (t))
1821 161382 : t = TYPE_CONTEXT (t);
1822 20826304 : return t;
1823 : }
1824 :
1825 : /* Function to give error message if member initialization specification
1826 : is erroneous. FIELD is the member we decided to initialize.
1827 : TYPE is the type for which the initialization is being performed.
1828 : FIELD must be a member of TYPE.
1829 :
1830 : MEMBER_NAME is the name of the member. */
1831 :
1832 : static int
1833 20826334 : member_init_ok_or_else (tree field, tree type, tree member_name)
1834 : {
1835 20826334 : if (field == error_mark_node)
1836 : return 0;
1837 20826319 : if (!field)
1838 : {
1839 12 : error ("class %qT does not have any field named %qD", type,
1840 : member_name);
1841 12 : return 0;
1842 : }
1843 20826307 : if (VAR_P (field))
1844 : {
1845 0 : error ("%q#D is a static data member; it can only be "
1846 : "initialized at its definition",
1847 : field);
1848 0 : return 0;
1849 : }
1850 20826307 : if (TREE_CODE (field) != FIELD_DECL)
1851 : {
1852 3 : error ("%q#D is not a non-static data member of %qT",
1853 : field, type);
1854 3 : return 0;
1855 : }
1856 20826304 : if (initializing_context (field) != type)
1857 : {
1858 3 : error ("class %qT does not have any field named %qD", type,
1859 : member_name);
1860 3 : return 0;
1861 : }
1862 :
1863 : return 1;
1864 : }
1865 :
1866 : /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1867 : is a _TYPE node or TYPE_DECL which names a base for that type.
1868 : Check the validity of NAME, and return either the base _TYPE, base
1869 : binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1870 : NULL_TREE and issue a diagnostic.
1871 :
1872 : An old style unnamed direct single base construction is permitted,
1873 : where NAME is NULL. */
1874 :
1875 : tree
1876 28153093 : expand_member_init (tree name)
1877 : {
1878 28153093 : tree basetype;
1879 28153093 : tree field;
1880 :
1881 28153093 : if (!current_class_ref)
1882 : return NULL_TREE;
1883 :
1884 28153088 : if (!name)
1885 : {
1886 : /* This is an obsolete unnamed base class initializer. The
1887 : parser will already have warned about its use. */
1888 6 : switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1889 : {
1890 0 : case 0:
1891 0 : error ("unnamed initializer for %qT, which has no base classes",
1892 : current_class_type);
1893 0 : return NULL_TREE;
1894 6 : case 1:
1895 6 : basetype = BINFO_TYPE
1896 : (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1897 6 : break;
1898 0 : default:
1899 0 : error ("unnamed initializer for %qT, which uses multiple inheritance",
1900 : current_class_type);
1901 0 : return NULL_TREE;
1902 : }
1903 : }
1904 28153082 : else if (TYPE_P (name))
1905 : {
1906 661311 : basetype = TYPE_MAIN_VARIANT (name);
1907 661311 : name = TYPE_NAME (name);
1908 : }
1909 27491771 : else if (TREE_CODE (name) == TYPE_DECL)
1910 6665437 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1911 : else
1912 : basetype = NULL_TREE;
1913 :
1914 7326754 : if (basetype)
1915 : {
1916 7326754 : tree class_binfo;
1917 7326754 : tree direct_binfo;
1918 7326754 : tree virtual_binfo;
1919 7326754 : int i;
1920 :
1921 7326754 : if (current_template_parms
1922 7326754 : || same_type_p (basetype, current_class_type))
1923 : return basetype;
1924 :
1925 1280116 : class_binfo = TYPE_BINFO (current_class_type);
1926 1280116 : direct_binfo = NULL_TREE;
1927 1280116 : virtual_binfo = NULL_TREE;
1928 :
1929 : /* Look for a direct base. */
1930 1372471 : for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1931 1372410 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1932 : break;
1933 :
1934 : /* Look for a virtual base -- unless the direct base is itself
1935 : virtual. */
1936 1280116 : if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1937 1279886 : virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1938 :
1939 : /* [class.base.init]
1940 :
1941 : If a mem-initializer-id is ambiguous because it designates
1942 : both a direct non-virtual base class and an inherited virtual
1943 : base class, the mem-initializer is ill-formed. */
1944 1280116 : if (direct_binfo && virtual_binfo)
1945 : {
1946 0 : error ("%qD is both a direct base and an indirect virtual base",
1947 : basetype);
1948 0 : return NULL_TREE;
1949 : }
1950 :
1951 61 : if (!direct_binfo && !virtual_binfo)
1952 : {
1953 9 : if (CLASSTYPE_VBASECLASSES (current_class_type))
1954 0 : error ("type %qT is not a direct or virtual base of %qT",
1955 : basetype, current_class_type);
1956 : else
1957 9 : error ("type %qT is not a direct base of %qT",
1958 : basetype, current_class_type);
1959 : return NULL_TREE;
1960 : }
1961 :
1962 : return direct_binfo ? direct_binfo : virtual_binfo;
1963 : }
1964 : else
1965 : {
1966 20826334 : if (identifier_p (name))
1967 17689957 : field = lookup_field (current_class_type, name, 1, false);
1968 : else
1969 : field = name;
1970 :
1971 20826334 : if (member_init_ok_or_else (field, current_class_type, name))
1972 20826301 : return field;
1973 : }
1974 :
1975 : return NULL_TREE;
1976 : }
1977 :
1978 : /* This is like `expand_member_init', only it stores one aggregate
1979 : value into another.
1980 :
1981 : INIT comes in two flavors: it is either a value which
1982 : is to be stored in EXP, or it is a parameter list
1983 : to go to a constructor, which will operate on EXP.
1984 : If INIT is not a parameter list for a constructor, then set
1985 : LOOKUP_ONLYCONVERTING.
1986 : If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1987 : the initializer, if FLAGS is 0, then it is the (init) form.
1988 : If `init' is a CONSTRUCTOR, then we emit a warning message,
1989 : explaining that such initializations are invalid.
1990 :
1991 : If INIT resolves to a CALL_EXPR which happens to return
1992 : something of the type we are looking for, then we know
1993 : that we can safely use that call to perform the
1994 : initialization.
1995 :
1996 : The virtual function table pointer cannot be set up here, because
1997 : we do not really know its type.
1998 :
1999 : This never calls operator=().
2000 :
2001 : When initializing, nothing is CONST.
2002 :
2003 : A default copy constructor may have to be used to perform the
2004 : initialization.
2005 :
2006 : A constructor or a conversion operator may have to be used to
2007 : perform the initialization, but not both, as it would be ambiguous. */
2008 :
2009 : tree
2010 5662402 : build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
2011 : {
2012 5662402 : tree stmt_expr;
2013 5662402 : tree compound_stmt;
2014 5662402 : int destroy_temps;
2015 5662402 : tree type = TREE_TYPE (exp);
2016 5662402 : int was_const = TREE_READONLY (exp);
2017 5662402 : int was_volatile = TREE_THIS_VOLATILE (exp);
2018 5662402 : int is_global;
2019 :
2020 5662402 : if (init == error_mark_node)
2021 : return error_mark_node;
2022 :
2023 5662240 : location_t init_loc = (init
2024 5662240 : ? cp_expr_loc_or_input_loc (init)
2025 982419 : : location_of (exp));
2026 :
2027 5662240 : TREE_READONLY (exp) = 0;
2028 5662240 : TREE_THIS_VOLATILE (exp) = 0;
2029 :
2030 5662240 : if (TREE_CODE (type) == ARRAY_TYPE)
2031 : {
2032 11251 : tree itype = init ? TREE_TYPE (init) : NULL_TREE;
2033 11251 : int from_array = 0;
2034 :
2035 11251 : if (DECL_DECOMPOSITION_P (exp))
2036 : {
2037 10448 : from_array = 1;
2038 10448 : init = mark_rvalue_use (init);
2039 10448 : if (init
2040 10448 : && DECL_P (tree_strip_any_location_wrapper (init))
2041 20856 : && !(flags & LOOKUP_ONLYCONVERTING))
2042 : {
2043 : /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init
2044 : recognizes it as direct-initialization. */
2045 30 : init = build_constructor_single (init_list_type_node,
2046 : NULL_TREE, init);
2047 30 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
2048 : }
2049 : }
2050 : else
2051 : {
2052 : /* Must arrange to initialize each element of EXP
2053 : from elements of INIT. */
2054 803 : if (cv_qualified_p (type))
2055 87 : TREE_TYPE (exp) = cv_unqualified (type);
2056 803 : if (itype && cv_qualified_p (itype))
2057 61 : TREE_TYPE (init) = cv_unqualified (itype);
2058 803 : from_array = (itype && same_type_p (TREE_TYPE (init),
2059 : TREE_TYPE (exp)));
2060 :
2061 75 : if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)
2062 878 : && (!from_array
2063 72 : || (TREE_CODE (init) != CONSTRUCTOR
2064 : /* Can happen, eg, handling the compound-literals
2065 : extension (ext/complit12.C). */
2066 11 : && TREE_CODE (init) != TARGET_EXPR)))
2067 : {
2068 5 : if (complain & tf_error)
2069 5 : error_at (init_loc, "array must be initialized "
2070 : "with a brace-enclosed initializer");
2071 5 : return error_mark_node;
2072 : }
2073 : }
2074 :
2075 11246 : stmt_expr = build_vec_init (exp, NULL_TREE, init,
2076 : /*explicit_value_init_p=*/false,
2077 : from_array,
2078 : complain);
2079 11246 : TREE_READONLY (exp) = was_const;
2080 11246 : TREE_THIS_VOLATILE (exp) = was_volatile;
2081 11246 : TREE_TYPE (exp) = type;
2082 : /* Restore the type of init unless it was used directly. */
2083 11246 : if (init && TREE_CODE (stmt_expr) != INIT_EXPR)
2084 69 : TREE_TYPE (init) = itype;
2085 : return stmt_expr;
2086 : }
2087 :
2088 5650989 : if (is_copy_initialization (init))
2089 1572741 : flags |= LOOKUP_ONLYCONVERTING;
2090 :
2091 5650989 : is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
2092 5650989 : destroy_temps = stmts_are_full_exprs_p ();
2093 5650989 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2094 5650989 : bool ok = expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
2095 : init, LOOKUP_NORMAL|flags, complain);
2096 5650989 : stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
2097 5650989 : current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
2098 5650989 : TREE_READONLY (exp) = was_const;
2099 5650989 : TREE_THIS_VOLATILE (exp) = was_volatile;
2100 5650989 : if (!ok)
2101 2208 : return error_mark_node;
2102 :
2103 2363726 : if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
2104 3285055 : && TREE_SIDE_EFFECTS (stmt_expr)
2105 8912408 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
2106 : /* Just know that we've seen something for this node. */
2107 3263621 : TREE_USED (exp) = 1;
2108 :
2109 : return stmt_expr;
2110 : }
2111 :
2112 : static bool
2113 8106934 : expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
2114 : tsubst_flags_t complain)
2115 : {
2116 8106934 : tree type = TREE_TYPE (exp);
2117 :
2118 : /* It fails because there may not be a constructor which takes
2119 : its own type as the first (or only parameter), but which does
2120 : take other types via a conversion. So, if the thing initializing
2121 : the expression is a unit element of type X, first try X(X&),
2122 : followed by initialization by X. If neither of these work
2123 : out, then look hard. */
2124 8106934 : tree rval;
2125 8106934 : vec<tree, va_gc> *parms;
2126 :
2127 : /* If we have direct-initialization from an initializer list, pull
2128 : it out of the TREE_LIST so the code below can see it. */
2129 5876190 : if (init && TREE_CODE (init) == TREE_LIST
2130 11621392 : && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2131 : {
2132 76612 : gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
2133 : && TREE_CHAIN (init) == NULL_TREE);
2134 76612 : init = TREE_VALUE (init);
2135 : /* Only call reshape_init if it has not been called earlier
2136 : by the callers. */
2137 76612 : if (BRACE_ENCLOSED_INITIALIZER_P (init) && CP_AGGREGATE_TYPE_P (type))
2138 38882 : init = reshape_init (type, init, complain);
2139 : }
2140 :
2141 5876190 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
2142 8902971 : && CP_AGGREGATE_TYPE_P (type))
2143 : /* A brace-enclosed initializer for an aggregate. In C++0x this can
2144 : happen for direct-initialization, too. */
2145 38816 : init = digest_init (type, init, complain);
2146 :
2147 8106934 : if (init == error_mark_node)
2148 : return false;
2149 :
2150 : /* A CONSTRUCTOR of the target's type is a previously digested
2151 : initializer, whether that happened just above or in
2152 : cp_parser_late_parsing_nsdmi.
2153 :
2154 : A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
2155 : set represents the whole initialization, so we shouldn't build up
2156 : another ctor call. */
2157 8106934 : if (init
2158 5876190 : && (TREE_CODE (init) == CONSTRUCTOR
2159 5076679 : || (TREE_CODE (init) == TARGET_EXPR
2160 1159726 : && (TARGET_EXPR_DIRECT_INIT_P (init)
2161 1088946 : || TARGET_EXPR_LIST_INIT_P (init))))
2162 8977246 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
2163 : {
2164 : /* Early initialization via a TARGET_EXPR only works for
2165 : complete objects. */
2166 113091 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
2167 :
2168 113091 : init = cp_build_init_expr (exp, init);
2169 113091 : TREE_SIDE_EFFECTS (init) = 1;
2170 113091 : finish_expr_stmt (init);
2171 113091 : return true;
2172 : }
2173 :
2174 5763099 : if (init && TREE_CODE (init) != TREE_LIST
2175 2325253 : && (flags & LOOKUP_ONLYCONVERTING)
2176 9562808 : && !unsafe_return_slot_p (exp))
2177 : {
2178 : /* Base subobjects should only get direct-initialization. */
2179 1568965 : gcc_assert (true_exp == exp);
2180 :
2181 1568965 : if (flags & DIRECT_BIND)
2182 : /* Do nothing. We hit this in two cases: Reference initialization,
2183 : where we aren't initializing a real variable, so we don't want
2184 : to run a new constructor; and catching an exception, where we
2185 : have already built up the constructor call so we could wrap it
2186 : in an exception region. */;
2187 : else
2188 : {
2189 1568450 : init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
2190 : flags, complain | tf_no_cleanup);
2191 1568450 : if (init == error_mark_node)
2192 : return false;
2193 : }
2194 :
2195 : /* We need to protect the initialization of a catch parm with a
2196 : call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
2197 : around the TARGET_EXPR for the copy constructor. See
2198 : initialize_handler_parm. */
2199 1567406 : tree *p = &init;
2200 1567406 : while (TREE_CODE (*p) == MUST_NOT_THROW_EXPR
2201 1567982 : || TREE_CODE (*p) == CLEANUP_POINT_EXPR)
2202 : {
2203 : /* Avoid voidify_wrapper_expr making a temporary. */
2204 576 : TREE_TYPE (*p) = void_type_node;
2205 576 : p = &TREE_OPERAND (*p, 0);
2206 : }
2207 1567406 : *p = cp_build_init_expr (exp, *p);
2208 1567406 : finish_expr_stmt (init);
2209 1567406 : return true;
2210 : }
2211 :
2212 6424878 : if (init == NULL_TREE)
2213 2230744 : parms = NULL;
2214 4194134 : else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
2215 : {
2216 3437846 : parms = make_tree_vector ();
2217 7729973 : for (; init != NULL_TREE; init = TREE_CHAIN (init))
2218 4292127 : vec_safe_push (parms, TREE_VALUE (init));
2219 : }
2220 : else
2221 756288 : parms = make_tree_vector_single (init);
2222 :
2223 6791843 : if (exp == current_class_ref && current_function_decl
2224 6791843 : && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
2225 : {
2226 : /* Delegating constructor. */
2227 100 : tree complete;
2228 100 : tree base;
2229 100 : tree elt; unsigned i;
2230 :
2231 : /* Unshare the arguments for the second call. */
2232 100 : releasing_vec parms2;
2233 405 : FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
2234 : {
2235 215 : elt = break_out_target_exprs (elt);
2236 215 : vec_safe_push (parms2, elt);
2237 : }
2238 100 : complete = build_special_member_call (exp, complete_ctor_identifier,
2239 : &parms2, binfo, flags,
2240 : complain);
2241 100 : complete = fold_build_cleanup_point_expr (void_type_node, complete);
2242 :
2243 100 : base = build_special_member_call (exp, base_ctor_identifier,
2244 : &parms, binfo, flags,
2245 : complain);
2246 100 : base = fold_build_cleanup_point_expr (void_type_node, base);
2247 100 : if (complete == error_mark_node || base == error_mark_node)
2248 0 : return false;
2249 100 : rval = build_if_in_charge (complete, base);
2250 100 : }
2251 : else
2252 : {
2253 6424778 : tree ctor_name = (true_exp == exp
2254 6424778 : ? complete_ctor_identifier : base_ctor_identifier);
2255 :
2256 6424778 : rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
2257 : complain);
2258 6424778 : if (rval == error_mark_node)
2259 : return false;
2260 : }
2261 :
2262 6424211 : if (parms != NULL)
2263 4210851 : release_tree_vector (parms);
2264 :
2265 6424211 : if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
2266 : {
2267 3212237 : tree fn = get_callee_fndecl (rval);
2268 3212237 : if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
2269 : {
2270 1790159 : tree e = maybe_constant_init (rval, exp);
2271 1790159 : if (TREE_CONSTANT (e))
2272 496357 : rval = cp_build_init_expr (exp, e);
2273 : }
2274 : }
2275 :
2276 : /* FIXME put back convert_to_void? */
2277 6424211 : if (TREE_SIDE_EFFECTS (rval))
2278 5947516 : finish_expr_stmt (rval);
2279 :
2280 : return true;
2281 : }
2282 :
2283 : /* This function is responsible for initializing EXP with INIT
2284 : (if any). Returns true on success, false on failure.
2285 :
2286 : BINFO is the binfo of the type for who we are performing the
2287 : initialization. For example, if W is a virtual base class of A and B,
2288 : and C : A, B.
2289 : If we are initializing B, then W must contain B's W vtable, whereas
2290 : were we initializing C, W must contain C's W vtable.
2291 :
2292 : TRUE_EXP is nonzero if it is the true expression being initialized.
2293 : In this case, it may be EXP, or may just contain EXP. The reason we
2294 : need this is because if EXP is a base element of TRUE_EXP, we
2295 : don't necessarily know by looking at EXP where its virtual
2296 : baseclass fields should really be pointing. But we do know
2297 : from TRUE_EXP. In constructors, we don't know anything about
2298 : the value being initialized.
2299 :
2300 : FLAGS is just passed to `build_new_method_call'. See that function
2301 : for its description. */
2302 :
2303 : static bool
2304 8107547 : expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
2305 : tsubst_flags_t complain)
2306 : {
2307 8107547 : tree type = TREE_TYPE (exp);
2308 :
2309 8107547 : gcc_assert (init != error_mark_node && type != error_mark_node);
2310 8107547 : gcc_assert (building_stmt_list_p ());
2311 :
2312 : /* Use a function returning the desired type to initialize EXP for us.
2313 : If the function is a constructor, and its first argument is
2314 : NULL_TREE, know that it was meant for us--just slide exp on
2315 : in and expand the constructor. Constructors now come
2316 : as TARGET_EXPRs. */
2317 :
2318 6116483 : if (init && VAR_P (exp)
2319 10782496 : && COMPOUND_LITERAL_P (init))
2320 : {
2321 165 : vec<tree, va_gc> *cleanups = NULL;
2322 : /* If store_init_value returns NULL_TREE, the INIT has been
2323 : recorded as the DECL_INITIAL for EXP. That means there's
2324 : nothing more we have to do. */
2325 165 : init = store_init_value (exp, init, &cleanups, flags);
2326 165 : if (init)
2327 0 : finish_expr_stmt (init);
2328 165 : gcc_assert (!cleanups);
2329 165 : return true;
2330 : }
2331 :
2332 : /* List-initialization from {} becomes value-initialization for non-aggregate
2333 : classes with default constructors. Handle this here when we're
2334 : initializing a base, so protected access works. */
2335 8107382 : if (exp != true_exp && init && TREE_CODE (init) == TREE_LIST)
2336 : {
2337 1208772 : tree elt = TREE_VALUE (init);
2338 39018 : if (DIRECT_LIST_INIT_P (elt)
2339 38934 : && CONSTRUCTOR_ELTS (elt) == 0
2340 42 : && CLASSTYPE_NON_AGGREGATE (type)
2341 1208795 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2342 23 : init = void_type_node;
2343 : }
2344 :
2345 : /* If an explicit -- but empty -- initializer list was present,
2346 : that's value-initialization. */
2347 8107382 : if (init == void_type_node)
2348 : {
2349 : /* If the type has data but no user-provided default ctor, we need to zero
2350 : out the object. */
2351 240128 : if (type_has_non_user_provided_default_constructor (type)
2352 240128 : && !is_really_empty_class (type, /*ignore_vptr*/true))
2353 : {
2354 23753 : tree field_size = NULL_TREE;
2355 23753 : if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
2356 : /* Don't clobber already initialized virtual bases. */
2357 37 : field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
2358 23753 : init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
2359 : field_size);
2360 23753 : init = cp_build_init_expr (exp, init);
2361 23753 : finish_expr_stmt (init);
2362 : }
2363 :
2364 : /* If we don't need to mess with the constructor at all,
2365 : then we're done. */
2366 240128 : if (! type_build_ctor_call (type))
2367 : return true;
2368 :
2369 : /* Otherwise fall through and call the constructor. */
2370 : init = NULL_TREE;
2371 : }
2372 :
2373 : /* We know that expand_default_init can handle everything we want
2374 : at this point. */
2375 8106934 : return expand_default_init (binfo, true_exp, exp, init, flags, complain);
2376 : }
2377 :
2378 : /* Report an error if TYPE is not a user-defined, class type. If
2379 : OR_ELSE is nonzero, give an error message. */
2380 :
2381 : int
2382 127566603 : is_class_type (tree type, int or_else)
2383 : {
2384 127566603 : if (type == error_mark_node)
2385 : return 0;
2386 :
2387 127543127 : if (! CLASS_TYPE_P (type))
2388 : {
2389 563 : if (or_else)
2390 5 : error ("%qT is not a class type", type);
2391 : return 0;
2392 : }
2393 : return 1;
2394 : }
2395 :
2396 : /* Returns true iff the initializer INIT represents copy-initialization
2397 : (and therefore we must set LOOKUP_ONLYCONVERTING when processing it). */
2398 :
2399 : bool
2400 134576952 : is_copy_initialization (tree init)
2401 : {
2402 116787223 : return (init && init != void_type_node
2403 116785531 : && TREE_CODE (init) != TREE_LIST
2404 107960824 : && !(TREE_CODE (init) == TARGET_EXPR
2405 2256287 : && TARGET_EXPR_DIRECT_INIT_P (init))
2406 242466951 : && !DIRECT_LIST_INIT_P (init));
2407 : }
2408 :
2409 : /* Build a reference to a member of an aggregate. This is not a C++
2410 : `&', but really something which can have its address taken, and
2411 : then act as a pointer to member, for example TYPE :: FIELD can have
2412 : its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
2413 : this expression is the operand of "&".
2414 :
2415 : @@ Prints out lousy diagnostics for operator <typename>
2416 : @@ fields.
2417 :
2418 : @@ This function should be rewritten and placed in search.cc. */
2419 :
2420 : tree
2421 168189 : build_offset_ref (tree type, tree member, bool address_p,
2422 : tsubst_flags_t complain)
2423 : {
2424 168189 : tree decl;
2425 168189 : tree basebinfo = NULL_TREE;
2426 :
2427 : /* class templates can come in as TEMPLATE_DECLs here. */
2428 168189 : if (TREE_CODE (member) == TEMPLATE_DECL)
2429 : return member;
2430 :
2431 168189 : if (dependent_scope_p (type) || type_dependent_expression_p (member))
2432 55369 : return build_qualified_name (NULL_TREE, type, member,
2433 55369 : /*template_p=*/false);
2434 :
2435 112820 : gcc_assert (TYPE_P (type));
2436 112820 : if (! is_class_type (type, 1))
2437 0 : return error_mark_node;
2438 :
2439 112820 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2440 : /* Callers should call mark_used before this point, except for functions. */
2441 112820 : gcc_assert (!DECL_P (member) || TREE_USED (member)
2442 : || TREE_CODE (member) == FUNCTION_DECL);
2443 :
2444 112820 : type = TYPE_MAIN_VARIANT (type);
2445 112820 : if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
2446 : {
2447 0 : if (complain & tf_error)
2448 0 : error ("incomplete type %qT does not have member %qD", type, member);
2449 0 : return error_mark_node;
2450 : }
2451 :
2452 : /* Entities other than non-static members need no further
2453 : processing. */
2454 112820 : if (TREE_CODE (member) == TYPE_DECL)
2455 : return member;
2456 112820 : if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
2457 906 : return convert_from_reference (member);
2458 :
2459 111914 : if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
2460 : {
2461 2 : if (complain & tf_error)
2462 2 : error ("invalid pointer to bit-field %qD", member);
2463 2 : return error_mark_node;
2464 : }
2465 :
2466 : /* Set up BASEBINFO for member lookup. */
2467 111912 : decl = maybe_dummy_object (type, &basebinfo);
2468 :
2469 : /* A lot of this logic is now handled in lookup_member. */
2470 111912 : if (BASELINK_P (member))
2471 : {
2472 : /* Go from the TREE_BASELINK to the member function info. */
2473 109146 : tree t = BASELINK_FUNCTIONS (member);
2474 :
2475 109146 : if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
2476 : {
2477 : /* Get rid of a potential OVERLOAD around it. */
2478 107950 : t = OVL_FIRST (t);
2479 :
2480 : /* Unique functions are handled easily. */
2481 :
2482 : /* For non-static member of base class, we need a special rule
2483 : for access checking [class.protected]:
2484 :
2485 : If the access is to form a pointer to member, the
2486 : nested-name-specifier shall name the derived class
2487 : (or any class derived from that class). */
2488 107950 : bool ok;
2489 104478 : if (address_p && DECL_P (t)
2490 212428 : && DECL_NONSTATIC_MEMBER_P (t))
2491 60674 : ok = perform_or_defer_access_check (TYPE_BINFO (type), t, t,
2492 : complain);
2493 : else
2494 47276 : ok = perform_or_defer_access_check (basebinfo, t, t,
2495 : complain);
2496 107950 : if (!ok)
2497 3 : return error_mark_node;
2498 107947 : if (DECL_STATIC_FUNCTION_P (t))
2499 : return member;
2500 : member = t;
2501 : }
2502 : else
2503 1196 : TREE_TYPE (member) = unknown_type_node;
2504 : }
2505 2766 : else if (address_p && TREE_CODE (member) == FIELD_DECL)
2506 : {
2507 : /* We need additional test besides the one in
2508 : check_accessibility_of_qualified_id in case it is
2509 : a pointer to non-static member. */
2510 2636 : if (!perform_or_defer_access_check (TYPE_BINFO (type), member, member,
2511 : complain))
2512 0 : return error_mark_node;
2513 : }
2514 :
2515 64669 : if (!address_p)
2516 : {
2517 : /* If MEMBER is non-static, then the program has fallen afoul of
2518 : [expr.prim]:
2519 :
2520 : An id-expression that denotes a non-static data member or
2521 : non-static member function of a class can only be used:
2522 :
2523 : -- as part of a class member access (_expr.ref_) in which the
2524 : object-expression refers to the member's class or a class
2525 : derived from that class, or
2526 :
2527 : -- to form a pointer to member (_expr.unary.op_), or
2528 :
2529 : -- in the body of a non-static member function of that class or
2530 : of a class derived from that class (_class.mfct.non-static_), or
2531 :
2532 : -- in a mem-initializer for a constructor for that class or for
2533 : a class derived from that class (_class.base.init_). */
2534 149 : if (DECL_OBJECT_MEMBER_FUNCTION_P (member))
2535 : {
2536 : /* Build a representation of the qualified name suitable
2537 : for use as the operand to "&" -- even though the "&" is
2538 : not actually present. */
2539 36 : member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
2540 : /* In Microsoft mode, treat a non-static member function as if
2541 : it were a pointer-to-member. */
2542 36 : if (flag_ms_extensions)
2543 : {
2544 6 : PTRMEM_OK_P (member) = 1;
2545 6 : return cp_build_addr_expr (member, complain);
2546 : }
2547 30 : if (complain & tf_error)
2548 27 : error ("invalid use of non-static member function %qD",
2549 27 : TREE_OPERAND (member, 1));
2550 30 : return error_mark_node;
2551 : }
2552 113 : else if (TREE_CODE (member) == FIELD_DECL)
2553 : {
2554 0 : if (complain & tf_error)
2555 0 : error ("invalid use of non-static data member %qD", member);
2556 0 : return error_mark_node;
2557 : }
2558 : return member;
2559 : }
2560 :
2561 64520 : member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
2562 64520 : PTRMEM_OK_P (member) = 1;
2563 64520 : return member;
2564 : }
2565 :
2566 : /* If DECL is a scalar enumeration constant or variable with a
2567 : constant initializer, return the initializer (or, its initializers,
2568 : recursively); otherwise, return DECL. If STRICT_P, the
2569 : initializer is only returned if DECL is a
2570 : constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
2571 : return an aggregate constant. If UNSHARE_P, return an unshared
2572 : copy of the initializer. */
2573 :
2574 : static tree
2575 904280010 : constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p,
2576 : bool unshare_p)
2577 : {
2578 904280010 : while (TREE_CODE (decl) == CONST_DECL
2579 1018139122 : || decl_constant_var_p (decl)
2580 1943155114 : || (!strict_p && VAR_P (decl)
2581 100775309 : && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl))))
2582 : {
2583 157861828 : tree init;
2584 : /* If DECL is a static data member in a template
2585 : specialization, we must instantiate it here. The
2586 : initializer for the static data member is not processed
2587 : until needed; we need it now. */
2588 157861828 : mark_used (decl, tf_none);
2589 157861828 : init = DECL_INITIAL (decl);
2590 157861828 : if (init == error_mark_node)
2591 : {
2592 1697 : if (TREE_CODE (decl) == CONST_DECL
2593 3394 : || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2594 : /* Treat the error as a constant to avoid cascading errors on
2595 : excessively recursive template instantiation (c++/9335). */
2596 1697 : return init;
2597 : else
2598 : return decl;
2599 : }
2600 : /* Initializers in templates are generally expanded during
2601 : instantiation, so before that for const int i(2)
2602 : INIT is a TREE_LIST with the actual initializer as
2603 : TREE_VALUE. */
2604 157860131 : if (processing_template_decl
2605 385984 : && init
2606 385984 : && TREE_CODE (init) == TREE_LIST
2607 157860131 : && TREE_CHAIN (init) == NULL_TREE)
2608 0 : init = TREE_VALUE (init);
2609 : /* Instantiate a non-dependent initializer for user variables. We
2610 : mustn't do this for the temporary for an array compound literal;
2611 : trying to instantiate the initializer will keep creating new
2612 : temporaries until we crash. Probably it's not useful to do it for
2613 : other artificial variables, either. */
2614 157860131 : if (!DECL_ARTIFICIAL (decl))
2615 157040660 : init = instantiate_non_dependent_or_null (init);
2616 157860131 : if (!init
2617 145628971 : || !TREE_TYPE (init)
2618 145628887 : || !TREE_CONSTANT (init)
2619 287819149 : || (!return_aggregate_cst_ok_p
2620 : /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2621 : return an aggregate constant (of which string
2622 : literals are a special case), as we do not want
2623 : to make inadvertent copies of such entities, and
2624 : we must be sure that their addresses are the
2625 : same everywhere. */
2626 5570141 : && (TREE_CODE (init) == CONSTRUCTOR
2627 5570141 : || TREE_CODE (init) == STRING_CST)))
2628 : break;
2629 : /* Don't return a CONSTRUCTOR for a variable with partial run-time
2630 : initialization, since it doesn't represent the entire value.
2631 : Similarly for VECTOR_CSTs created by cp_folding those
2632 : CONSTRUCTORs. */
2633 129959018 : if ((TREE_CODE (init) == CONSTRUCTOR
2634 127010174 : || TREE_CODE (init) == VECTOR_CST)
2635 132908225 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2636 : break;
2637 : /* If the variable has a dynamic initializer, don't use its
2638 : DECL_INITIAL which doesn't reflect the real value. */
2639 129958997 : if (VAR_P (decl)
2640 113859174 : && TREE_STATIC (decl)
2641 105779079 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
2642 129959059 : && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2643 : break;
2644 : decl = init;
2645 : }
2646 904275616 : return unshare_p ? unshare_expr (decl) : decl;
2647 : }
2648 :
2649 : /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant
2650 : of integral or enumeration type, or a constexpr variable of scalar type,
2651 : then return that value. These are those variables permitted in constant
2652 : expressions by [5.19/1]. */
2653 :
2654 : tree
2655 490869477 : scalar_constant_value (tree decl)
2656 : {
2657 490869477 : return constant_value_1 (decl, /*strict_p=*/true,
2658 : /*return_aggregate_cst_ok_p=*/false,
2659 490869477 : /*unshare_p=*/true);
2660 : }
2661 :
2662 : /* Like scalar_constant_value, but can also return aggregate initializers.
2663 : If UNSHARE_P, return an unshared copy of the initializer. */
2664 :
2665 : tree
2666 148157604 : decl_really_constant_value (tree decl, bool unshare_p /*= true*/)
2667 : {
2668 148157604 : return constant_value_1 (decl, /*strict_p=*/true,
2669 : /*return_aggregate_cst_ok_p=*/true,
2670 148154907 : /*unshare_p=*/unshare_p);
2671 : }
2672 :
2673 : /* A more relaxed version of decl_really_constant_value, used by the
2674 : common C/C++ code. */
2675 :
2676 : tree
2677 265252929 : decl_constant_value (tree decl, bool unshare_p)
2678 : {
2679 265252929 : return constant_value_1 (decl, /*strict_p=*/processing_template_decl,
2680 : /*return_aggregate_cst_ok_p=*/true,
2681 265252929 : /*unshare_p=*/unshare_p);
2682 : }
2683 :
2684 : tree
2685 264902963 : decl_constant_value (tree decl)
2686 : {
2687 264902963 : return decl_constant_value (decl, /*unshare_p=*/true);
2688 : }
2689 :
2690 : /* Common subroutines of build_new and build_vec_delete. */
2691 :
2692 : /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2693 : the type of the object being allocated; otherwise, it's just TYPE.
2694 : INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2695 : user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2696 : a vector of arguments to be provided as arguments to a placement
2697 : new operator. This routine performs no semantic checks; it just
2698 : creates and returns a NEW_EXPR. */
2699 :
2700 : static tree
2701 934654 : build_raw_new_expr (location_t loc, vec<tree, va_gc> *placement, tree type,
2702 : tree nelts, vec<tree, va_gc> *init, int use_global_new)
2703 : {
2704 934654 : tree init_list;
2705 934654 : tree new_expr;
2706 :
2707 : /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2708 : If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2709 : permits us to distinguish the case of a missing initializer "new
2710 : int" from an empty initializer "new int()". */
2711 934654 : if (init == NULL)
2712 : init_list = NULL_TREE;
2713 605643 : else if (init->is_empty ())
2714 109914 : init_list = void_node;
2715 : else
2716 495729 : init_list = build_tree_list_vec (init);
2717 :
2718 934654 : new_expr = build4_loc (loc, NEW_EXPR, build_pointer_type (type),
2719 : build_tree_list_vec (placement), type, nelts,
2720 : init_list);
2721 934654 : NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2722 934654 : TREE_SIDE_EFFECTS (new_expr) = 1;
2723 :
2724 934654 : return new_expr;
2725 : }
2726 :
2727 : /* Diagnose uninitialized const members or reference members of type
2728 : TYPE. USING_NEW is used to disambiguate the diagnostic between a
2729 : new expression without a new-initializer and a declaration. Returns
2730 : the error count. */
2731 :
2732 : static int
2733 65 : diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2734 : bool using_new, bool complain)
2735 : {
2736 65 : tree field;
2737 65 : int error_count = 0;
2738 :
2739 65 : if (type_has_user_provided_constructor (type))
2740 : return 0;
2741 :
2742 222 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2743 : {
2744 159 : tree field_type;
2745 :
2746 159 : if (TREE_CODE (field) != FIELD_DECL)
2747 82 : continue;
2748 :
2749 77 : field_type = strip_array_types (TREE_TYPE (field));
2750 :
2751 77 : if (type_has_user_provided_constructor (field_type))
2752 3 : continue;
2753 :
2754 74 : if (TYPE_REF_P (field_type))
2755 : {
2756 24 : ++ error_count;
2757 24 : if (complain)
2758 : {
2759 22 : auto_diagnostic_group d;
2760 22 : if (DECL_CONTEXT (field) == origin)
2761 : {
2762 14 : if (using_new)
2763 6 : error ("uninitialized reference member in %q#T "
2764 : "using %<new%> without new-initializer", origin);
2765 : else
2766 8 : error ("uninitialized reference member in %q#T", origin);
2767 : }
2768 : else
2769 : {
2770 8 : if (using_new)
2771 3 : error ("uninitialized reference member in base %q#T "
2772 : "of %q#T using %<new%> without new-initializer",
2773 3 : DECL_CONTEXT (field), origin);
2774 : else
2775 5 : error ("uninitialized reference member in base %q#T "
2776 5 : "of %q#T", DECL_CONTEXT (field), origin);
2777 : }
2778 22 : inform (DECL_SOURCE_LOCATION (field),
2779 : "%q#D should be initialized", field);
2780 22 : }
2781 : }
2782 :
2783 74 : if (CP_TYPE_CONST_P (field_type))
2784 : {
2785 32 : ++ error_count;
2786 32 : if (complain)
2787 : {
2788 30 : auto_diagnostic_group d;
2789 30 : if (DECL_CONTEXT (field) == origin)
2790 : {
2791 23 : if (using_new)
2792 9 : error ("uninitialized const member in %q#T "
2793 : "using %<new%> without new-initializer", origin);
2794 : else
2795 14 : error ("uninitialized const member in %q#T", origin);
2796 : }
2797 : else
2798 : {
2799 7 : if (using_new)
2800 3 : error ("uninitialized const member in base %q#T "
2801 : "of %q#T using %<new%> without new-initializer",
2802 3 : DECL_CONTEXT (field), origin);
2803 : else
2804 4 : error ("uninitialized const member in base %q#T "
2805 4 : "of %q#T", DECL_CONTEXT (field), origin);
2806 : }
2807 30 : inform (DECL_SOURCE_LOCATION (field),
2808 : "%q#D should be initialized", field);
2809 30 : }
2810 : }
2811 :
2812 74 : if (CLASS_TYPE_P (field_type))
2813 16 : error_count
2814 16 : += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2815 : using_new, complain);
2816 : }
2817 : return error_count;
2818 : }
2819 :
2820 : int
2821 49 : diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2822 : {
2823 49 : return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2824 : }
2825 :
2826 : /* Call __cxa_bad_array_new_length to indicate that the size calculation
2827 : overflowed. */
2828 :
2829 : tree
2830 21136 : throw_bad_array_new_length (void)
2831 : {
2832 21136 : if (!fn)
2833 : {
2834 11062 : tree name = get_identifier ("__cxa_throw_bad_array_new_length");
2835 :
2836 11062 : fn = get_global_binding (name);
2837 11062 : if (!fn)
2838 11062 : fn = push_throw_library_fn
2839 11062 : (name, build_function_type_list (void_type_node, NULL_TREE));
2840 : }
2841 :
2842 21136 : return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2843 : }
2844 :
2845 : /* Attempt to verify that the argument, OPER, of a placement new expression
2846 : refers to an object sufficiently large for an object of TYPE or an array
2847 : of NELTS of such objects when NELTS is non-null, and issue a warning when
2848 : it does not. SIZE specifies the size needed to construct the object or
2849 : array and captures the result of NELTS * sizeof (TYPE). (SIZE could be
2850 : greater when the array under construction requires a cookie to store
2851 : NELTS. GCC's placement new expression stores the cookie when invoking
2852 : a user-defined placement new operator function but not the default one.
2853 : Placement new expressions with user-defined placement new operator are
2854 : not diagnosed since we don't know how they use the buffer (this could
2855 : be a future extension). */
2856 : static void
2857 675559 : warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper)
2858 : {
2859 675559 : location_t loc = cp_expr_loc_or_input_loc (oper);
2860 :
2861 675559 : STRIP_NOPS (oper);
2862 :
2863 : /* Using a function argument or a (non-array) variable as an argument
2864 : to placement new is not checked since it's unknown what it might
2865 : point to. */
2866 675559 : if (TREE_CODE (oper) == PARM_DECL
2867 : || VAR_P (oper)
2868 : || TREE_CODE (oper) == COMPONENT_REF)
2869 674587 : return;
2870 :
2871 : /* Evaluate any constant expressions. */
2872 459013 : size = fold_non_dependent_expr (size);
2873 :
2874 459013 : access_ref ref;
2875 459013 : ref.eval = [](tree x){ return fold_non_dependent_expr (x); };
2876 459013 : ref.trail1special = warn_placement_new < 2;
2877 459013 : tree objsize = compute_objsize (oper, 1, &ref);
2878 459013 : if (!objsize)
2879 : return;
2880 :
2881 : /* We can only draw conclusions if ref.deref == -1,
2882 : i.e. oper is the address of the object. */
2883 458919 : if (ref.deref != -1)
2884 : return;
2885 :
2886 2676 : offset_int bytes_avail = wi::to_offset (objsize);
2887 2676 : offset_int bytes_need;
2888 :
2889 2676 : if (CONSTANT_CLASS_P (size))
2890 2643 : bytes_need = wi::to_offset (size);
2891 33 : else if (nelts && CONSTANT_CLASS_P (nelts))
2892 0 : bytes_need = (wi::to_offset (nelts)
2893 0 : * wi::to_offset (TYPE_SIZE_UNIT (type)));
2894 33 : else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2895 30 : bytes_need = wi::to_offset (TYPE_SIZE_UNIT (type));
2896 : else
2897 : {
2898 : /* The type is a VLA. */
2899 : return;
2900 : }
2901 :
2902 2673 : if (bytes_avail >= bytes_need)
2903 : return;
2904 :
2905 : /* True when the size to mention in the warning is exact as opposed
2906 : to "at least N". */
2907 975 : const bool exact_size = (ref.offrng[0] == ref.offrng[1]
2908 975 : || ref.sizrng[1] - ref.offrng[0] == 0);
2909 :
2910 975 : tree opertype = ref.ref ? TREE_TYPE (ref.ref) : TREE_TYPE (oper);
2911 975 : bool warned = false;
2912 975 : if (nelts)
2913 402 : nelts = fold_for_warn (nelts);
2914 :
2915 975 : auto_diagnostic_group d;
2916 975 : if (nelts)
2917 402 : if (CONSTANT_CLASS_P (nelts))
2918 399 : warned = warning_at (loc, OPT_Wplacement_new_,
2919 : (exact_size
2920 : ? G_("placement new constructing an object "
2921 : "of type %<%T [%wu]%> and size %qwu "
2922 : "in a region of type %qT and size %qwi")
2923 : : G_("placement new constructing an object "
2924 : "of type %<%T [%wu]%> and size %qwu "
2925 : "in a region of type %qT and size "
2926 : "at most %qwu")),
2927 : type, tree_to_uhwi (nelts),
2928 : bytes_need.to_uhwi (),
2929 : opertype, bytes_avail.to_uhwi ());
2930 : else
2931 9 : warned = warning_at (loc, OPT_Wplacement_new_,
2932 : (exact_size
2933 : ? G_("placement new constructing an array "
2934 : "of objects of type %qT and size %qwu "
2935 : "in a region of type %qT and size %qwi")
2936 : : G_("placement new constructing an array "
2937 : "of objects of type %qT and size %qwu "
2938 : "in a region of type %qT and size "
2939 : "at most %qwu")),
2940 : type, bytes_need.to_uhwi (), opertype,
2941 : bytes_avail.to_uhwi ());
2942 : else
2943 603 : warned = warning_at (loc, OPT_Wplacement_new_,
2944 : (exact_size
2945 : ? G_("placement new constructing an object "
2946 : "of type %qT and size %qwu in a region "
2947 : "of type %qT and size %qwi")
2948 : : G_("placement new constructing an object "
2949 : "of type %qT "
2950 : "and size %qwu in a region of type %qT "
2951 : "and size at most %qwu")),
2952 : type, bytes_need.to_uhwi (), opertype,
2953 : bytes_avail.to_uhwi ());
2954 :
2955 975 : if (!warned || !ref.ref)
2956 3 : return;
2957 :
2958 972 : if (ref.offrng[0] == 0 || !ref.offset_bounded ())
2959 : /* Avoid mentioning the offset when its lower bound is zero
2960 : or when it's impossibly large. */
2961 525 : inform (DECL_SOURCE_LOCATION (ref.ref),
2962 : "%qD declared here", ref.ref);
2963 447 : else if (ref.offrng[0] == ref.offrng[1])
2964 438 : inform (DECL_SOURCE_LOCATION (ref.ref),
2965 : "at offset %wi from %qD declared here",
2966 : ref.offrng[0].to_shwi (), ref.ref);
2967 : else
2968 9 : inform (DECL_SOURCE_LOCATION (ref.ref),
2969 : "at offset [%wi, %wi] from %qD declared here",
2970 : ref.offrng[0].to_shwi (), ref.offrng[1].to_shwi (), ref.ref);
2971 975 : }
2972 :
2973 : /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */
2974 :
2975 : bool
2976 2811068 : type_has_new_extended_alignment (tree t)
2977 : {
2978 2811068 : return (aligned_new_threshold
2979 2811068 : && TYPE_ALIGN_UNIT (t) > (unsigned)aligned_new_threshold);
2980 : }
2981 :
2982 : /* Return the alignment we expect malloc to guarantee. This should just be
2983 : MALLOC_ABI_ALIGNMENT, but that macro defaults to only BITS_PER_WORD for some
2984 : reason, so don't let the threshold be smaller than max_align_t_align. */
2985 :
2986 : unsigned
2987 82790 : malloc_alignment ()
2988 : {
2989 84218 : return MAX (max_align_t_align(), MALLOC_ABI_ALIGNMENT);
2990 : }
2991 :
2992 : /* Determine whether an allocation function is a namespace-scope
2993 : non-replaceable placement new function. See DR 1748. */
2994 : bool
2995 36406416 : std_placement_new_fn_p (tree alloc_fn)
2996 : {
2997 72812832 : if (DECL_NAMESPACE_SCOPE_P (alloc_fn)
2998 18388142 : && IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_fn))
2999 38252574 : && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (alloc_fn))
3000 : {
3001 1260991 : tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3002 1260991 : if (first_arg
3003 1260979 : && (TREE_VALUE (first_arg) == ptr_type_node)
3004 2521581 : && (TREE_CHAIN (first_arg) == void_list_node))
3005 1260590 : return true;
3006 : }
3007 : return false;
3008 : }
3009 :
3010 : /* For element type ELT_TYPE, return the appropriate type of the heap object
3011 : containing such element(s). COOKIE_SIZE is the size of cookie in bytes.
3012 : Return
3013 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
3014 : where N is nothing (flexible array member) if ITYPE2 is NULL, otherwise
3015 : the array has ITYPE2 as its TYPE_DOMAIN. */
3016 :
3017 : tree
3018 19 : build_new_constexpr_heap_type (tree elt_type, tree cookie_size, tree itype2)
3019 : {
3020 19 : gcc_assert (tree_fits_uhwi_p (cookie_size));
3021 19 : unsigned HOST_WIDE_INT csz = tree_to_uhwi (cookie_size);
3022 19 : csz /= int_size_in_bytes (sizetype);
3023 19 : tree itype1 = build_index_type (size_int (csz - 1));
3024 19 : tree atype1 = build_cplus_array_type (sizetype, itype1);
3025 19 : tree atype2 = build_cplus_array_type (elt_type, itype2);
3026 19 : tree rtype = cxx_make_type (RECORD_TYPE);
3027 19 : tree fld1 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype1);
3028 19 : tree fld2 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype2);
3029 19 : DECL_FIELD_CONTEXT (fld1) = rtype;
3030 19 : DECL_FIELD_CONTEXT (fld2) = rtype;
3031 19 : DECL_ARTIFICIAL (fld1) = true;
3032 19 : DECL_ARTIFICIAL (fld2) = true;
3033 19 : TYPE_FIELDS (rtype) = fld1;
3034 19 : DECL_CHAIN (fld1) = fld2;
3035 19 : TYPE_ARTIFICIAL (rtype) = true;
3036 19 : layout_type (rtype);
3037 :
3038 19 : tree decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, heap_identifier, rtype);
3039 19 : TYPE_NAME (rtype) = decl;
3040 19 : TYPE_STUB_DECL (rtype) = decl;
3041 19 : DECL_CONTEXT (decl) = NULL_TREE;
3042 19 : DECL_ARTIFICIAL (decl) = true;
3043 19 : layout_decl (decl, 0);
3044 :
3045 19 : return rtype;
3046 : }
3047 :
3048 : /* Help the constexpr code to find the right type for the heap variable
3049 : by adding a NOP_EXPR around ALLOC_CALL if needed for cookie_size.
3050 : Return ALLOC_CALL or ALLOC_CALL cast to a pointer to
3051 : struct { size_t[cookie_size/sizeof(size_t)]; elt_type[]; }. */
3052 :
3053 : static tree
3054 228 : maybe_wrap_new_for_constexpr (tree alloc_call, tree elt_type, tree cookie_size)
3055 : {
3056 228 : if (cxx_dialect < cxx20)
3057 : return alloc_call;
3058 :
3059 175 : if (current_function_decl != NULL_TREE
3060 175 : && !DECL_DECLARED_CONSTEXPR_P (current_function_decl))
3061 : return alloc_call;
3062 :
3063 10 : tree call_expr = extract_call_expr (alloc_call);
3064 10 : if (call_expr == error_mark_node)
3065 : return alloc_call;
3066 :
3067 10 : tree alloc_call_fndecl = cp_get_callee_fndecl_nofold (call_expr);
3068 10 : if (alloc_call_fndecl == NULL_TREE
3069 10 : || !IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_call_fndecl))
3070 20 : || CP_DECL_CONTEXT (alloc_call_fndecl) != global_namespace)
3071 : return alloc_call;
3072 :
3073 10 : tree rtype = build_new_constexpr_heap_type (elt_type, cookie_size,
3074 : NULL_TREE);
3075 10 : return build_nop (build_pointer_type (rtype), alloc_call);
3076 : }
3077 :
3078 : /* Generate code for a new-expression, including calling the "operator
3079 : new" function, initializing the object, and, if an exception occurs
3080 : during construction, cleaning up. The arguments are as for
3081 : build_raw_new_expr. This may change PLACEMENT and INIT.
3082 : TYPE is the type of the object being constructed, possibly an array
3083 : of NELTS elements when NELTS is non-null (in "new T[NELTS]", T may
3084 : be an array of the form U[inner], with the whole expression being
3085 : "new U[NELTS][inner]"). */
3086 :
3087 : static tree
3088 1085402 : build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
3089 : vec<tree, va_gc> **init, bool globally_qualified_p,
3090 : tsubst_flags_t complain)
3091 : {
3092 1085402 : tree size, rval;
3093 : /* True iff this is a call to "operator new[]" instead of just
3094 : "operator new". */
3095 1085402 : bool array_p = false;
3096 : /* If ARRAY_P is true, the element type of the array. This is never
3097 : an ARRAY_TYPE; for something like "new int[3][4]", the
3098 : ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
3099 : TYPE. */
3100 1085402 : tree elt_type;
3101 : /* The type of the new-expression. (This type is always a pointer
3102 : type.) */
3103 1085402 : tree pointer_type;
3104 1085402 : tree non_const_pointer_type;
3105 : /* The most significant array bound in int[OUTER_NELTS][inner]. */
3106 1085402 : tree outer_nelts = NULL_TREE;
3107 : /* For arrays with a non-constant number of elements, a bounds checks
3108 : on the NELTS parameter to avoid integer overflow at runtime. */
3109 1085402 : tree outer_nelts_check = NULL_TREE;
3110 1085402 : bool outer_nelts_from_type = false;
3111 : /* Number of the "inner" elements in "new T[OUTER_NELTS][inner]". */
3112 1085402 : offset_int inner_nelts_count = 1;
3113 1085402 : tree alloc_call, alloc_expr;
3114 : /* Size of the inner array elements (those with constant dimensions). */
3115 1085402 : offset_int inner_size;
3116 : /* The address returned by the call to "operator new". This node is
3117 : a VAR_DECL and is therefore reusable. */
3118 1085402 : tree alloc_node;
3119 1085402 : tree alloc_fn;
3120 1085402 : tree cookie_expr, init_expr;
3121 1085402 : int nothrow, check_new;
3122 : /* If non-NULL, the number of extra bytes to allocate at the
3123 : beginning of the storage allocated for an array-new expression in
3124 : order to store the number of elements. */
3125 1085402 : tree cookie_size = NULL_TREE;
3126 1085402 : tree placement_first;
3127 1085402 : tree placement_expr = NULL_TREE;
3128 : /* True if the function we are calling is a placement allocation
3129 : function. */
3130 1085402 : bool placement_allocation_fn_p;
3131 : /* True if the storage must be initialized, either by a constructor
3132 : or due to an explicit new-initializer. */
3133 1085402 : bool is_initialized;
3134 : /* The address of the thing allocated, not including any cookie. In
3135 : particular, if an array cookie is in use, DATA_ADDR is the
3136 : address of the first array element. This node is a VAR_DECL, and
3137 : is therefore reusable. */
3138 1085402 : tree data_addr;
3139 1085402 : tree orig_type = type;
3140 :
3141 1085402 : if (nelts)
3142 : {
3143 : outer_nelts = nelts;
3144 : array_p = true;
3145 : }
3146 997036 : else if (TREE_CODE (type) == ARRAY_TYPE)
3147 : {
3148 : /* Transforms new (T[N]) to new T[N]. The former is a GNU
3149 : extension for variable N. (This also covers new T where T is
3150 : a VLA typedef.) */
3151 249 : array_p = true;
3152 249 : nelts = array_type_nelts_top (type);
3153 249 : outer_nelts = nelts;
3154 249 : type = TREE_TYPE (type);
3155 249 : outer_nelts_from_type = true;
3156 : }
3157 :
3158 : /* Lots of logic below depends on whether we have a constant number of
3159 : elements, so go ahead and fold it now. */
3160 1085402 : const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain);
3161 :
3162 : /* If our base type is an array, then make sure we know how many elements
3163 : it has. */
3164 1085402 : for (elt_type = type;
3165 1086942 : TREE_CODE (elt_type) == ARRAY_TYPE;
3166 1540 : elt_type = TREE_TYPE (elt_type))
3167 : {
3168 1540 : tree inner_nelts = array_type_nelts_top (elt_type);
3169 1540 : tree inner_nelts_cst = maybe_constant_value (inner_nelts);
3170 1540 : if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
3171 : {
3172 1510 : wi::overflow_type overflow;
3173 1510 : offset_int result = wi::mul (wi::to_offset (inner_nelts_cst),
3174 : inner_nelts_count, SIGNED, &overflow);
3175 1510 : if (overflow)
3176 : {
3177 0 : if (complain & tf_error)
3178 0 : error ("integer overflow in array size");
3179 0 : nelts = error_mark_node;
3180 : }
3181 1510 : inner_nelts_count = result;
3182 : }
3183 : else
3184 : {
3185 30 : if (complain & tf_error)
3186 : {
3187 30 : error_at (cp_expr_loc_or_input_loc (inner_nelts),
3188 : "array size in new-expression must be constant");
3189 30 : cxx_constant_value(inner_nelts);
3190 : }
3191 30 : nelts = error_mark_node;
3192 : }
3193 1540 : if (nelts != error_mark_node)
3194 1510 : nelts = cp_build_binary_op (input_location,
3195 : MULT_EXPR, nelts,
3196 : inner_nelts_cst,
3197 : complain);
3198 : }
3199 :
3200 1085402 : if (!verify_type_context (input_location, TCTX_ALLOCATION, elt_type,
3201 1085402 : !(complain & tf_error)))
3202 0 : return error_mark_node;
3203 :
3204 1085402 : if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
3205 : {
3206 0 : error ("variably modified type not allowed in new-expression");
3207 0 : return error_mark_node;
3208 : }
3209 :
3210 1085402 : if (nelts == error_mark_node)
3211 : return error_mark_node;
3212 :
3213 : /* Warn if we performed the (T[N]) to T[N] transformation and N is
3214 : variable. */
3215 1085372 : if (outer_nelts_from_type
3216 1085372 : && !TREE_CONSTANT (cst_outer_nelts))
3217 : {
3218 12 : if (complain & tf_warning_or_error)
3219 : {
3220 21 : pedwarn (cp_expr_loc_or_input_loc (outer_nelts), OPT_Wvla,
3221 12 : typedef_variant_p (orig_type)
3222 : ? G_("non-constant array new length must be specified "
3223 : "directly, not by %<typedef%>")
3224 : : G_("non-constant array new length must be specified "
3225 : "without parentheses around the type-id"));
3226 : }
3227 : else
3228 : return error_mark_node;
3229 : }
3230 :
3231 1085372 : if (VOID_TYPE_P (elt_type))
3232 : {
3233 0 : if (complain & tf_error)
3234 0 : error ("invalid type %<void%> for %<new%>");
3235 0 : return error_mark_node;
3236 : }
3237 :
3238 1085372 : if (is_std_init_list (elt_type) && !cp_unevaluated_operand)
3239 7 : warning (OPT_Winit_list_lifetime,
3240 : "%<new%> of %<initializer_list%> does not "
3241 : "extend the lifetime of the underlying array");
3242 :
3243 1085372 : if (abstract_virtuals_error (ACU_NEW, elt_type, complain))
3244 10 : return error_mark_node;
3245 :
3246 1085362 : is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
3247 :
3248 1085362 : if (*init == NULL && cxx_dialect < cxx11)
3249 : {
3250 2578 : bool maybe_uninitialized_error = false;
3251 : /* A program that calls for default-initialization [...] of an
3252 : entity of reference type is ill-formed. */
3253 2578 : if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
3254 2578 : maybe_uninitialized_error = true;
3255 :
3256 : /* A new-expression that creates an object of type T initializes
3257 : that object as follows:
3258 : - If the new-initializer is omitted:
3259 : -- If T is a (possibly cv-qualified) non-POD class type
3260 : (or array thereof), the object is default-initialized (8.5).
3261 : [...]
3262 : -- Otherwise, the object created has indeterminate
3263 : value. If T is a const-qualified type, or a (possibly
3264 : cv-qualified) POD class type (or array thereof)
3265 : containing (directly or indirectly) a member of
3266 : const-qualified type, the program is ill-formed; */
3267 :
3268 2578 : if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
3269 : maybe_uninitialized_error = true;
3270 :
3271 2560 : if (maybe_uninitialized_error
3272 2578 : && diagnose_uninitialized_cst_or_ref_member (elt_type,
3273 : /*using_new=*/true,
3274 : complain & tf_error))
3275 20 : return error_mark_node;
3276 : }
3277 :
3278 1085393 : if (CP_TYPE_CONST_P (elt_type) && *init == NULL
3279 1085378 : && default_init_uninitialized_part (elt_type))
3280 : {
3281 29 : if (complain & tf_error)
3282 18 : error ("uninitialized const in %<new%> of %q#T", elt_type);
3283 29 : return error_mark_node;
3284 : }
3285 :
3286 1085313 : size = size_in_bytes (elt_type);
3287 1085313 : if (array_p)
3288 : {
3289 : /* Maximum available size in bytes. Half of the address space
3290 : minus the cookie size. */
3291 88575 : offset_int max_size
3292 88575 : = wi::set_bit_in_zero <offset_int> (TYPE_PRECISION (sizetype) - 1);
3293 : /* Maximum number of outer elements which can be allocated. */
3294 88575 : offset_int max_outer_nelts;
3295 88575 : tree max_outer_nelts_tree;
3296 :
3297 88575 : gcc_assert (TREE_CODE (size) == INTEGER_CST);
3298 88575 : cookie_size = targetm.cxx.get_cookie_size (elt_type);
3299 88575 : gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
3300 88575 : gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size));
3301 : /* Unconditionally subtract the cookie size. This decreases the
3302 : maximum object size and is safe even if we choose not to use
3303 : a cookie after all. */
3304 88575 : max_size -= wi::to_offset (cookie_size);
3305 88575 : wi::overflow_type overflow;
3306 88575 : inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED,
3307 : &overflow);
3308 177150 : if (overflow || wi::gtu_p (inner_size, max_size))
3309 : {
3310 138 : if (complain & tf_error)
3311 : {
3312 138 : cst_size_error error;
3313 138 : if (overflow)
3314 : error = cst_size_overflow;
3315 : else
3316 : {
3317 138 : error = cst_size_too_big;
3318 138 : size = size_binop (MULT_EXPR, size,
3319 : wide_int_to_tree (sizetype,
3320 : inner_nelts_count));
3321 138 : size = cp_fully_fold (size);
3322 : }
3323 138 : invalid_array_size_error (input_location, error, size,
3324 : /*name=*/NULL_TREE);
3325 : }
3326 324 : return error_mark_node;
3327 : }
3328 :
3329 88437 : max_outer_nelts = wi::udiv_trunc (max_size, inner_size);
3330 88437 : max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts);
3331 :
3332 88437 : size = build2 (MULT_EXPR, sizetype, size, nelts);
3333 :
3334 88437 : if (TREE_CODE (cst_outer_nelts) == INTEGER_CST)
3335 : {
3336 3106 : if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts))
3337 : {
3338 : /* When the array size is constant, check it at compile time
3339 : to make sure it doesn't exceed the implementation-defined
3340 : maximum, as required by C++ 14 (in C++ 11 this requirement
3341 : isn't explicitly stated but it's enforced anyway -- see
3342 : grokdeclarator in cp/decl.cc). */
3343 186 : if (complain & tf_error)
3344 : {
3345 186 : size = cp_fully_fold (size);
3346 186 : invalid_array_size_error (input_location, cst_size_too_big,
3347 : size, NULL_TREE);
3348 : }
3349 186 : return error_mark_node;
3350 : }
3351 : }
3352 : else
3353 : {
3354 : /* When a runtime check is necessary because the array size
3355 : isn't constant, keep only the top-most seven bits (starting
3356 : with the most significant non-zero bit) of the maximum size
3357 : to compare the array size against, to simplify encoding the
3358 : constant maximum size in the instruction stream. */
3359 :
3360 85331 : unsigned shift = (max_outer_nelts.get_precision ()) - 7
3361 85331 : - wi::clz (max_outer_nelts);
3362 85331 : max_outer_nelts = (max_outer_nelts >> shift) << shift;
3363 :
3364 85331 : outer_nelts_check = build2 (LE_EXPR, boolean_type_node,
3365 : outer_nelts,
3366 : max_outer_nelts_tree);
3367 : }
3368 : }
3369 :
3370 1084989 : tree align_arg = NULL_TREE;
3371 1084989 : if (type_has_new_extended_alignment (elt_type))
3372 : {
3373 9449 : unsigned align = TYPE_ALIGN_UNIT (elt_type);
3374 : /* Also consider the alignment of the cookie, if any. */
3375 9449 : if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
3376 6 : align = MAX (align, TYPE_ALIGN_UNIT (size_type_node));
3377 9449 : align_arg = build_int_cst (align_type_node, align);
3378 : }
3379 :
3380 1084989 : alloc_fn = NULL_TREE;
3381 :
3382 : /* If PLACEMENT is a single simple pointer type not passed by
3383 : reference, prepare to capture it in a temporary variable. Do
3384 : this now, since PLACEMENT will change in the calls below. */
3385 1084989 : placement_first = NULL_TREE;
3386 1084989 : if (vec_safe_length (*placement) == 1
3387 676404 : && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
3388 : placement_first = (**placement)[0];
3389 :
3390 1084989 : bool member_new_p = false;
3391 :
3392 : /* Allocate the object. */
3393 1084989 : tree fnname;
3394 1084989 : tree fns;
3395 :
3396 1084989 : fnname = ovl_op_identifier (false, array_p ? VEC_NEW_EXPR : NEW_EXPR);
3397 :
3398 2353161 : member_new_p = !globally_qualified_p
3399 183183 : && CLASS_TYPE_P (elt_type)
3400 1199629 : && (array_p
3401 114640 : ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
3402 92947 : : TYPE_HAS_NEW_OPERATOR (elt_type));
3403 :
3404 183183 : bool member_delete_p = (!globally_qualified_p
3405 183183 : && CLASS_TYPE_P (elt_type)
3406 114640 : && (array_p
3407 114640 : ? TYPE_GETS_VEC_DELETE (elt_type)
3408 92947 : : TYPE_GETS_REG_DELETE (elt_type)));
3409 :
3410 1084989 : if (member_new_p)
3411 : {
3412 : /* Use a class-specific operator new. */
3413 : /* If a cookie is required, add some extra space. */
3414 606 : if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
3415 8 : size = build2 (PLUS_EXPR, sizetype, size, cookie_size);
3416 : else
3417 : {
3418 598 : cookie_size = NULL_TREE;
3419 : /* No size arithmetic necessary, so the size check is
3420 : not needed. */
3421 598 : if (outer_nelts_check != NULL && inner_size == 1)
3422 1 : outer_nelts_check = NULL_TREE;
3423 : }
3424 : /* Perform the overflow check. */
3425 606 : tree errval = TYPE_MAX_VALUE (sizetype);
3426 606 : if (cxx_dialect >= cxx11 && flag_exceptions)
3427 419 : errval = throw_bad_array_new_length ();
3428 606 : if (outer_nelts_check != NULL_TREE)
3429 9 : size = build3 (COND_EXPR, sizetype, outer_nelts_check, size, errval);
3430 606 : size = fold_to_constant (size);
3431 : /* Create the argument list. */
3432 606 : vec_safe_insert (*placement, 0, size);
3433 : /* Do name-lookup to find the appropriate operator. */
3434 606 : fns = lookup_fnfields (elt_type, fnname, /*protect=*/2, complain);
3435 606 : if (fns == NULL_TREE)
3436 : {
3437 0 : if (complain & tf_error)
3438 0 : error ("no suitable %qD found in class %qT", fnname, elt_type);
3439 0 : return error_mark_node;
3440 : }
3441 606 : if (TREE_CODE (fns) == TREE_LIST)
3442 : {
3443 3 : if (complain & tf_error)
3444 : {
3445 3 : auto_diagnostic_group d;
3446 3 : error ("request for member %qD is ambiguous", fnname);
3447 3 : print_candidates (input_location, fns);
3448 3 : }
3449 3 : return error_mark_node;
3450 : }
3451 603 : tree dummy = build_dummy_object (elt_type);
3452 603 : alloc_call = NULL_TREE;
3453 603 : if (align_arg)
3454 : {
3455 0 : vec<tree, va_gc> *align_args
3456 0 : = vec_copy_and_insert (*placement, align_arg, 1);
3457 0 : alloc_call
3458 0 : = build_new_method_call (dummy, fns, &align_args,
3459 : /*conversion_path=*/NULL_TREE,
3460 : LOOKUP_NORMAL, &alloc_fn, tf_none);
3461 : /* If no matching function is found and the allocated object type
3462 : has new-extended alignment, the alignment argument is removed
3463 : from the argument list, and overload resolution is performed
3464 : again. */
3465 0 : if (alloc_call == error_mark_node)
3466 0 : alloc_call = NULL_TREE;
3467 : }
3468 0 : if (!alloc_call)
3469 603 : alloc_call = build_new_method_call (dummy, fns, placement,
3470 : /*conversion_path=*/NULL_TREE,
3471 : LOOKUP_NORMAL,
3472 : &alloc_fn, complain);
3473 : }
3474 : else
3475 : {
3476 : /* Use a global operator new. */
3477 : /* See if a cookie might be required. */
3478 1084383 : if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
3479 : {
3480 1084115 : cookie_size = NULL_TREE;
3481 : /* No size arithmetic necessary, so the size check is
3482 : not needed. */
3483 1084115 : if (outer_nelts_check != NULL && inner_size == 1)
3484 64210 : outer_nelts_check = NULL_TREE;
3485 : }
3486 :
3487 1084383 : size = fold_to_constant (size);
3488 : /* If size is zero e.g. due to type having zero size, try to
3489 : preserve outer_nelts for constant expression evaluation
3490 : purposes. */
3491 1084383 : if (integer_zerop (size) && outer_nelts)
3492 78 : size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts);
3493 :
3494 1084383 : alloc_call = build_operator_new_call (fnname, placement,
3495 : &size, &cookie_size,
3496 : align_arg, outer_nelts_check,
3497 : &alloc_fn, complain);
3498 : }
3499 :
3500 1084986 : if (alloc_call == error_mark_node)
3501 : return error_mark_node;
3502 :
3503 1084971 : gcc_assert (alloc_fn != NULL_TREE);
3504 :
3505 : /* Now, check to see if this function is actually a placement
3506 : allocation function. This can happen even when PLACEMENT is NULL
3507 : because we might have something like:
3508 :
3509 : struct S { void* operator new (size_t, int i = 0); };
3510 :
3511 : A call to `new S' will get this allocation function, even though
3512 : there is no explicit placement argument. If there is more than
3513 : one argument, or there are variable arguments, then this is a
3514 : placement allocation function. */
3515 1084971 : placement_allocation_fn_p
3516 1084971 : = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
3517 1084971 : || varargs_function_p (alloc_fn));
3518 :
3519 1084971 : if (complain & tf_warning_or_error
3520 630577 : && warn_aligned_new
3521 6416 : && !placement_allocation_fn_p
3522 2733 : && TYPE_ALIGN (elt_type) > malloc_alignment ()
3523 15 : && (warn_aligned_new > 1
3524 13 : || CP_DECL_CONTEXT (alloc_fn) == global_namespace)
3525 1084977 : && !aligned_allocation_fn_p (alloc_fn))
3526 : {
3527 6 : auto_diagnostic_group d;
3528 6 : if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended "
3529 6 : "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type)))
3530 : {
3531 4 : inform (input_location, "uses %qD, which does not have an alignment "
3532 : "parameter", alloc_fn);
3533 4 : if (!aligned_new_threshold)
3534 4 : inform (input_location, "use %<-faligned-new%> to enable C++17 "
3535 : "over-aligned new support");
3536 : }
3537 6 : }
3538 :
3539 : /* If we found a simple case of PLACEMENT_EXPR above, then copy it
3540 : into a temporary variable. */
3541 1084971 : if (!processing_template_decl
3542 987382 : && TREE_CODE (alloc_call) == CALL_EXPR
3543 987382 : && call_expr_nargs (alloc_call) == 2
3544 685660 : && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
3545 1770631 : && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
3546 : {
3547 675801 : tree placement = CALL_EXPR_ARG (alloc_call, 1);
3548 :
3549 675801 : if (placement_first != NULL_TREE
3550 675801 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))
3551 674846 : || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))))
3552 : {
3553 674826 : placement_expr = get_internal_target_expr (placement_first);
3554 674826 : CALL_EXPR_ARG (alloc_call, 1)
3555 1349652 : = fold_convert (TREE_TYPE (placement), placement_expr);
3556 : }
3557 :
3558 675801 : if (!member_new_p
3559 1351392 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1)))))
3560 : {
3561 : /* Attempt to make the warning point at the operator new argument. */
3562 675559 : if (placement_first)
3563 674631 : placement = placement_first;
3564 :
3565 675559 : warn_placement_new_too_small (orig_type, nelts, size, placement);
3566 : }
3567 : }
3568 :
3569 1084971 : alloc_expr = alloc_call;
3570 1084971 : if (cookie_size)
3571 228 : alloc_expr = maybe_wrap_new_for_constexpr (alloc_expr, type,
3572 : cookie_size);
3573 :
3574 1084971 : const bool std_placement = std_placement_new_fn_p (alloc_fn);
3575 :
3576 : /* Clobber the object now that the constructor won't do it in
3577 : start_preparsed_function. This is most important for activating an array
3578 : in a union (c++/121068), but should also help the optimizers. */
3579 1084971 : const bool do_clobber
3580 1084971 : = (flag_lifetime_dse > 1
3581 1084856 : && !processing_template_decl
3582 987275 : && !is_empty_type (elt_type)
3583 979940 : && !integer_zerop (TYPE_SIZE (type))
3584 979925 : && (!outer_nelts || !integer_zerop (cst_outer_nelts))
3585 2064845 : && (!*init || CLASS_TYPE_P (elt_type)));
3586 :
3587 : /* In the simple case, we can stop now. */
3588 1084971 : pointer_type = build_pointer_type (type);
3589 1084971 : if (!cookie_size && !is_initialized && !member_delete_p && !do_clobber)
3590 64856 : return build_nop (pointer_type, alloc_expr);
3591 :
3592 : /* Store the result of the allocation call in a variable so that we can
3593 : use it more than once. */
3594 1020115 : alloc_expr = get_internal_target_expr (alloc_expr);
3595 1020115 : alloc_node = TARGET_EXPR_SLOT (alloc_expr);
3596 :
3597 : /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
3598 1020115 : while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
3599 0 : alloc_call = TREE_OPERAND (alloc_call, 1);
3600 :
3601 : /* Preevaluate the placement args so that we don't reevaluate them for a
3602 : placement delete. */
3603 1020115 : if (placement_allocation_fn_p)
3604 : {
3605 685356 : tree inits;
3606 685356 : stabilize_call (alloc_call, &inits);
3607 685356 : if (inits)
3608 683934 : alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
3609 : alloc_expr);
3610 : }
3611 :
3612 : /* unless an allocation function is declared with an empty excep-
3613 : tion-specification (_except.spec_), throw(), it indicates failure to
3614 : allocate storage by throwing a bad_alloc exception (clause _except_,
3615 : _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
3616 : cation function is declared with an empty exception-specification,
3617 : throw(), it returns null to indicate failure to allocate storage and a
3618 : non-null pointer otherwise.
3619 :
3620 : So check for a null exception spec on the op new we just called. */
3621 :
3622 1020115 : nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
3623 1020115 : check_new = flag_check_new || (nothrow && !std_placement);
3624 :
3625 1020115 : if (cookie_size)
3626 : {
3627 228 : tree cookie;
3628 228 : tree cookie_ptr;
3629 228 : tree size_ptr_type;
3630 :
3631 : /* Adjust so we're pointing to the start of the object. */
3632 228 : data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
3633 :
3634 : /* Store the number of bytes allocated so that we can know how
3635 : many elements to destroy later. We use the last sizeof
3636 : (size_t) bytes to store the number of elements. */
3637 228 : cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
3638 228 : cookie_ptr = fold_build_pointer_plus_loc (input_location,
3639 : alloc_node, cookie_ptr);
3640 228 : size_ptr_type = build_pointer_type (sizetype);
3641 228 : cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
3642 228 : cookie = cp_build_fold_indirect_ref (cookie_ptr);
3643 :
3644 228 : cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
3645 :
3646 228 : if (targetm.cxx.cookie_has_size ())
3647 : {
3648 : /* Also store the element size. */
3649 0 : cookie_ptr = fold_build_pointer_plus (cookie_ptr,
3650 : fold_build1_loc (input_location,
3651 : NEGATE_EXPR, sizetype,
3652 : size_in_bytes (sizetype)));
3653 :
3654 0 : cookie = cp_build_fold_indirect_ref (cookie_ptr);
3655 0 : cookie = build2 (MODIFY_EXPR, sizetype, cookie,
3656 : size_in_bytes (elt_type));
3657 0 : cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
3658 : cookie, cookie_expr);
3659 : }
3660 : }
3661 : else
3662 : {
3663 : cookie_expr = NULL_TREE;
3664 : data_addr = alloc_node;
3665 : }
3666 :
3667 : /* Now use a pointer to the type we've actually allocated. */
3668 :
3669 : /* But we want to operate on a non-const version to start with,
3670 : since we'll be modifying the elements. */
3671 1020115 : non_const_pointer_type = build_pointer_type
3672 1020115 : (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
3673 :
3674 1020115 : data_addr = fold_convert (non_const_pointer_type, data_addr);
3675 : /* Any further uses of alloc_node will want this type, too. */
3676 1020115 : alloc_node = fold_convert (non_const_pointer_type, alloc_node);
3677 :
3678 1020115 : tree clobber_expr = NULL_TREE;
3679 1020115 : if (do_clobber)
3680 : {
3681 595996 : if (array_p && TREE_CODE (cst_outer_nelts) != INTEGER_CST)
3682 : {
3683 : /* Clobber each element rather than the array at once. */
3684 : /* But for now, limit a clobber loop to placement new during
3685 : constant-evaluation, as cddce1 thinks it might be infinite, leading
3686 : to bogus warnings on Wstringop-overflow-4.C (2025-09-30). We
3687 : need it in constexpr for constexpr-new4a.C. */
3688 35 : if (std_placement && current_function_decl
3689 11130 : && maybe_constexpr_fn (current_function_decl))
3690 : {
3691 2 : tree clobber = build_clobber (elt_type, CLOBBER_OBJECT_BEGIN);
3692 2 : CONSTRUCTOR_IS_DIRECT_INIT (clobber) = true;
3693 2 : tree maxindex = cp_build_binary_op (input_location,
3694 : MINUS_EXPR, outer_nelts,
3695 : integer_one_node,
3696 : complain);
3697 2 : clobber_expr = build_vec_init (data_addr, maxindex, clobber,
3698 : /*valinit*/false, /*from_arr*/0,
3699 : complain, nullptr);
3700 2 : clobber_expr = wrap_with_if_consteval (clobber_expr);
3701 : }
3702 : }
3703 : else
3704 : {
3705 2045 : tree targ = data_addr;
3706 2045 : tree ttype = type;
3707 : /* Clobber the array as a whole, except that for a one-element array
3708 : just clobber the element type, to avoid problems with code like
3709 : construct_at that uses new T[1] for array T to get a pointer to
3710 : the array. */
3711 2045 : if (array_p && !integer_onep (cst_outer_nelts))
3712 : {
3713 1677 : tree dom
3714 1677 : = compute_array_index_type (NULL_TREE,
3715 : const_cast<tree> (cst_outer_nelts),
3716 : complain);
3717 1677 : ttype = build_cplus_array_type (type, dom);
3718 1677 : tree ptype = build_pointer_type (ttype);
3719 1677 : targ = fold_convert (ptype, targ);
3720 : }
3721 584901 : targ = cp_build_fold_indirect_ref (targ);
3722 584901 : tree clobber = build_clobber (ttype, CLOBBER_OBJECT_BEGIN);
3723 584901 : CONSTRUCTOR_IS_DIRECT_INIT (clobber) = true;
3724 584901 : clobber_expr = cp_build_init_expr (targ, clobber);
3725 : }
3726 : }
3727 :
3728 : /* Now initialize the allocated object. Note that we preevaluate the
3729 : initialization expression, apart from the actual constructor call or
3730 : assignment--we do this because we want to delay the allocation as long
3731 : as possible in order to minimize the size of the exception region for
3732 : placement delete. */
3733 1020115 : if (is_initialized)
3734 : {
3735 855301 : bool explicit_value_init_p = false;
3736 :
3737 855301 : if (*init != NULL && (*init)->is_empty ())
3738 : {
3739 165480 : *init = NULL;
3740 165480 : explicit_value_init_p = true;
3741 : }
3742 :
3743 855301 : if (processing_template_decl)
3744 : {
3745 : /* Avoid an ICE when converting to a base in build_simple_base_path.
3746 : We'll throw this all away anyway, and build_new will create
3747 : a NEW_EXPR. */
3748 34396 : tree t = fold_convert (build_pointer_type (elt_type), data_addr);
3749 : /* build_value_init doesn't work in templates, and we don't need
3750 : the initializer anyway since we're going to throw it away and
3751 : rebuild it at instantiation time, so just build up a single
3752 : constructor call to get any appropriate diagnostics. */
3753 34396 : init_expr = cp_build_fold_indirect_ref (t);
3754 34396 : if (type_build_ctor_call (elt_type))
3755 34351 : init_expr = build_special_member_call (init_expr,
3756 : complete_ctor_identifier,
3757 : init, elt_type,
3758 : LOOKUP_NORMAL,
3759 : complain);
3760 : }
3761 820905 : else if (array_p)
3762 : {
3763 10373 : tree vecinit = NULL_TREE;
3764 10373 : const size_t len = vec_safe_length (*init);
3765 375 : if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0]))
3766 : {
3767 366 : vecinit = (**init)[0];
3768 366 : if (CONSTRUCTOR_NELTS (vecinit) == 0)
3769 : /* List-value-initialization, leave it alone. */;
3770 : else
3771 : {
3772 289 : tree arraytype, domain;
3773 289 : if (TREE_CONSTANT (nelts))
3774 276 : domain = compute_array_index_type (NULL_TREE, nelts,
3775 : complain);
3776 : else
3777 : /* We'll check the length at runtime. */
3778 : domain = NULL_TREE;
3779 289 : arraytype = build_cplus_array_type (type, domain);
3780 : /* If we have new char[4]{"foo"}, we have to reshape
3781 : so that the STRING_CST isn't wrapped in { }. */
3782 289 : vecinit = reshape_init (arraytype, vecinit, complain);
3783 : /* The middle end doesn't cope with the location wrapper
3784 : around a STRING_CST. */
3785 289 : STRIP_ANY_LOCATION_WRAPPER (vecinit);
3786 289 : vecinit = digest_init (arraytype, vecinit, complain);
3787 : }
3788 : }
3789 10007 : else if (*init)
3790 : {
3791 9 : if (complain & tf_error)
3792 8 : error ("parenthesized initializer in array new");
3793 9 : return error_mark_node;
3794 : }
3795 :
3796 : /* Collect flags for disabling subobject cleanups once the complete
3797 : object is fully constructed. */
3798 10364 : vec<tree, va_gc> *flags = make_tree_vector ();
3799 :
3800 10364 : init_expr
3801 10364 : = build_vec_init (data_addr,
3802 10364 : cp_build_binary_op (input_location,
3803 : MINUS_EXPR, outer_nelts,
3804 : integer_one_node,
3805 : complain),
3806 : vecinit,
3807 : explicit_value_init_p,
3808 : /*from_array=*/0,
3809 : complain,
3810 : &flags);
3811 :
3812 31334 : for (tree f : flags)
3813 : {
3814 242 : tree cl = build_disable_temp_cleanup (f);
3815 242 : cl = convert_to_void (cl, ICV_STATEMENT, complain);
3816 242 : init_expr = build2 (COMPOUND_EXPR, void_type_node,
3817 : init_expr, cl);
3818 : }
3819 10364 : release_tree_vector (flags);
3820 : }
3821 : else
3822 : {
3823 810532 : init_expr = cp_build_fold_indirect_ref (data_addr);
3824 :
3825 810532 : if (type_build_ctor_call (type) && !explicit_value_init_p)
3826 : {
3827 230504 : init_expr = build_special_member_call (init_expr,
3828 : complete_ctor_identifier,
3829 : init, elt_type,
3830 : LOOKUP_NORMAL,
3831 : complain|tf_no_cleanup);
3832 : }
3833 580028 : else if (explicit_value_init_p)
3834 : {
3835 : /* Something like `new int()'. NO_CLEANUP is needed so
3836 : we don't try and build a (possibly ill-formed)
3837 : destructor. */
3838 155865 : tree val = build_value_init (type, complain | tf_no_cleanup);
3839 155865 : if (val == error_mark_node)
3840 25 : return error_mark_node;
3841 155840 : init_expr = cp_build_init_expr (init_expr, val);
3842 : }
3843 : else
3844 : {
3845 424163 : tree ie;
3846 :
3847 : /* We are processing something like `new int (10)', which
3848 : means allocate an int, and initialize it with 10.
3849 :
3850 : In C++20, also handle `new A(1, 2)'. */
3851 424163 : if (cxx_dialect >= cxx20
3852 423777 : && AGGREGATE_TYPE_P (type)
3853 466417 : && (*init)->length () > 1)
3854 : {
3855 92 : ie = build_constructor_from_vec (init_list_type_node, *init);
3856 92 : CONSTRUCTOR_IS_DIRECT_INIT (ie) = true;
3857 92 : CONSTRUCTOR_IS_PAREN_INIT (ie) = true;
3858 92 : ie = digest_init (type, ie, complain);
3859 : }
3860 : else
3861 424071 : ie = build_x_compound_expr_from_vec (*init, "new initializer",
3862 : complain);
3863 424163 : init_expr = cp_build_modify_expr (input_location, init_expr,
3864 : INIT_EXPR, ie, complain);
3865 : }
3866 : /* If the initializer uses C++14 aggregate NSDMI that refer to the
3867 : object being initialized, replace them now and don't try to
3868 : preevaluate. */
3869 810507 : bool had_placeholder = false;
3870 810507 : if (!processing_template_decl
3871 810507 : && TREE_CODE (init_expr) == INIT_EXPR)
3872 588279 : TREE_OPERAND (init_expr, 1)
3873 1176558 : = replace_placeholders (TREE_OPERAND (init_expr, 1),
3874 588279 : TREE_OPERAND (init_expr, 0),
3875 : &had_placeholder);
3876 : }
3877 :
3878 855267 : if (init_expr == error_mark_node)
3879 : return error_mark_node;
3880 : }
3881 : else
3882 : init_expr = NULL_TREE;
3883 :
3884 : /* If any part of the object initialization terminates by throwing an
3885 : exception and a suitable deallocation function can be found, the
3886 : deallocation function is called to free the memory in which the
3887 : object was being constructed, after which the exception continues
3888 : to propagate in the context of the new-expression. If no
3889 : unambiguous matching deallocation function can be found,
3890 : propagating the exception does not cause the object's memory to be
3891 : freed. */
3892 1019851 : if (flag_exceptions && (init_expr || member_delete_p))
3893 : {
3894 854391 : enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
3895 854391 : tree cleanup;
3896 :
3897 : /* The Standard is unclear here, but the right thing to do
3898 : is to use the same method for finding deallocation
3899 : functions that we use for finding allocation functions. */
3900 854391 : cleanup
3901 854391 : = build_op_delete_call (dcode, alloc_node, size, globally_qualified_p,
3902 : placement_allocation_fn_p
3903 : ? alloc_call : NULL_TREE,
3904 : placement_allocation_fn_p ? *placement : NULL,
3905 : alloc_fn, complain);
3906 :
3907 854391 : if (cleanup && init_expr && !processing_template_decl)
3908 : /* Ack! First we allocate the memory. Then we set our sentry
3909 : variable to true, and expand a cleanup that deletes the
3910 : memory if sentry is true. Then we run the constructor, and
3911 : finally clear the sentry.
3912 :
3913 : We need to do this because we allocate the space first, so
3914 : if there are any temporaries with cleanups in the
3915 : constructor args, we need this EH region to extend until
3916 : end of full-expression to preserve nesting.
3917 :
3918 : We used to try to evaluate the args first to avoid this, but
3919 : since C++17 [expr.new] says that "The invocation of the
3920 : allocation function is sequenced before the evaluations of
3921 : expressions in the new-initializer." */
3922 : {
3923 818989 : tree end, sentry, begin;
3924 :
3925 818989 : begin = get_internal_target_expr (boolean_true_node);
3926 :
3927 818989 : sentry = TARGET_EXPR_SLOT (begin);
3928 :
3929 : /* CLEANUP is compiler-generated, so no diagnostics. */
3930 818989 : suppress_warning (cleanup);
3931 :
3932 818989 : TARGET_EXPR_CLEANUP (begin)
3933 818989 : = build3 (COND_EXPR, void_type_node, sentry,
3934 : cleanup, void_node);
3935 :
3936 818989 : end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
3937 : sentry, boolean_false_node);
3938 :
3939 818989 : init_expr
3940 818989 : = build2 (COMPOUND_EXPR, void_type_node, begin,
3941 : build2 (COMPOUND_EXPR, void_type_node, init_expr,
3942 : end));
3943 : /* Likewise, this is compiler-generated. */
3944 818989 : suppress_warning (init_expr);
3945 : }
3946 : }
3947 :
3948 : /* Now build up the return value in reverse order. */
3949 :
3950 855234 : rval = data_addr;
3951 :
3952 855234 : if (init_expr)
3953 855037 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
3954 1019851 : if (clobber_expr)
3955 584829 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), clobber_expr, rval);
3956 1019851 : if (cookie_expr)
3957 228 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
3958 :
3959 1019851 : suppress_warning (rval, OPT_Wunused_value);
3960 :
3961 1019851 : if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
3962 : /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
3963 : and return the call (which doesn't need to be adjusted). */
3964 1725 : rval = TARGET_EXPR_INITIAL (alloc_expr);
3965 : else
3966 : {
3967 : /* Skip the null-check when rval == data_addr: the resulting conditional
3968 : would be "alloc_node != nullptr ? alloc_node : alloc_node", triggering
3969 : -Wduplicated-branches (PR125422). */
3970 1018126 : if (rval == data_addr)
3971 : check_new = 0;
3972 :
3973 1018078 : if (check_new)
3974 : {
3975 240 : tree ifexp = cp_build_binary_op (input_location,
3976 : NE_EXPR, alloc_node,
3977 : nullptr_node,
3978 : complain);
3979 240 : rval = build_conditional_expr (input_location, ifexp, rval,
3980 : alloc_node, complain);
3981 : /* If there's no offset between data_addr and alloc_node, append it
3982 : to help -Wmismatched-new-delete at -O0. */
3983 240 : if (!cookie_size)
3984 228 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_node),
3985 : rval, alloc_node);
3986 : }
3987 :
3988 : /* Perform the allocation before anything else, so that ALLOC_NODE
3989 : has been initialized before we start using it. */
3990 1018126 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
3991 : }
3992 :
3993 : /* A new-expression is never an lvalue. */
3994 1019851 : gcc_assert (!obvalue_p (rval));
3995 :
3996 1019851 : return convert (pointer_type, rval);
3997 : }
3998 :
3999 : /* Generate a representation for a C++ "new" expression. *PLACEMENT
4000 : is a vector of placement-new arguments (or NULL if none). If NELTS
4001 : is NULL, TYPE is the type of the storage to be allocated. If NELTS
4002 : is not NULL, then this is an array-new allocation; TYPE is the type
4003 : of the elements in the array and NELTS is the number of elements in
4004 : the array. *INIT, if non-NULL, is the initializer for the new
4005 : object, or an empty vector to indicate an initializer of "()". If
4006 : USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
4007 : rather than just "new". This may change PLACEMENT and INIT. */
4008 :
4009 : tree
4010 1923151 : build_new (location_t loc, vec<tree, va_gc> **placement, tree type,
4011 : tree nelts, vec<tree, va_gc> **init, int use_global_new,
4012 : tsubst_flags_t complain)
4013 : {
4014 1923151 : tree rval;
4015 1923151 : vec<tree, va_gc> *orig_placement = NULL;
4016 1923151 : tree orig_nelts = NULL_TREE;
4017 1923151 : vec<tree, va_gc> *orig_init = NULL;
4018 :
4019 1923151 : if (type == error_mark_node)
4020 : return error_mark_node;
4021 :
4022 1922932 : if (nelts == NULL_TREE
4023 : /* Don't do auto deduction where it might affect mangling. */
4024 1922932 : && (!processing_template_decl || at_function_scope_p ()))
4025 : {
4026 1626483 : tree auto_node = type_uses_auto (type);
4027 1626483 : if (auto_node)
4028 : {
4029 108 : tree d_init = NULL_TREE;
4030 108 : const size_t len = vec_safe_length (*init);
4031 : /* E.g. new auto(x) must have exactly one element, or
4032 : a {} initializer will have one element. */
4033 99 : if (len == 1)
4034 : {
4035 84 : d_init = (**init)[0];
4036 84 : d_init = resolve_nondeduced_context (d_init, complain);
4037 : }
4038 : /* For the rest, e.g. new A(1, 2, 3), create a list. */
4039 24 : else if (len > 1)
4040 : {
4041 : unsigned int n;
4042 : tree t;
4043 : tree *pp = &d_init;
4044 21 : FOR_EACH_VEC_ELT (**init, n, t)
4045 : {
4046 15 : t = resolve_nondeduced_context (t, complain);
4047 15 : *pp = build_tree_list (NULL_TREE, t);
4048 15 : pp = &TREE_CHAIN (*pp);
4049 : }
4050 : }
4051 108 : type = do_auto_deduction (type, d_init, auto_node, complain);
4052 : }
4053 : }
4054 :
4055 1922932 : if (processing_template_decl)
4056 : {
4057 934684 : if (dependent_type_p (type)
4058 107670 : || any_type_dependent_arguments_p (*placement)
4059 107667 : || (nelts && type_dependent_expression_p (nelts))
4060 74097 : || (nelts && *init)
4061 1032367 : || any_type_dependent_arguments_p (*init))
4062 837071 : return build_raw_new_expr (loc, *placement, type, nelts, *init,
4063 837071 : use_global_new);
4064 :
4065 97613 : orig_placement = make_tree_vector_copy (*placement);
4066 97613 : orig_nelts = nelts;
4067 97613 : if (*init)
4068 : {
4069 23461 : orig_init = make_tree_vector_copy (*init);
4070 : /* Also copy any CONSTRUCTORs in *init, since reshape_init and
4071 : digest_init clobber them in place. */
4072 93702 : for (unsigned i = 0; i < orig_init->length(); ++i)
4073 : {
4074 46780 : tree e = (**init)[i];
4075 46780 : if (TREE_CODE (e) == CONSTRUCTOR)
4076 21 : (**init)[i] = copy_node (e);
4077 : }
4078 : }
4079 : }
4080 :
4081 1085861 : if (nelts)
4082 : {
4083 88696 : location_t nelts_loc = cp_expr_loc_or_loc (nelts, loc);
4084 88696 : if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
4085 : {
4086 9 : if (complain & tf_error)
4087 6 : permerror (nelts_loc,
4088 : "size in array new must have integral type");
4089 : else
4090 3 : return error_mark_node;
4091 : }
4092 :
4093 : /* Try to determine the constant value only for the purposes
4094 : of the diagnostic below but continue to use the original
4095 : value and handle const folding later. */
4096 88693 : const_tree cst_nelts = fold_non_dependent_expr (nelts, complain);
4097 :
4098 : /* The expression in a noptr-new-declarator is erroneous if it's of
4099 : non-class type and its value before converting to std::size_t is
4100 : less than zero. ... If the expression is a constant expression,
4101 : the program is ill-formed. */
4102 88693 : if (TREE_CODE (cst_nelts) == INTEGER_CST
4103 88693 : && !valid_array_size_p (nelts_loc, cst_nelts, NULL_TREE,
4104 : complain & tf_error))
4105 324 : return error_mark_node;
4106 :
4107 88369 : nelts = mark_rvalue_use (nelts);
4108 88369 : nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
4109 : }
4110 :
4111 : /* ``A reference cannot be created by the new operator. A reference
4112 : is not an object (8.2.2, 8.4.3), so a pointer to it could not be
4113 : returned by new.'' ARM 5.3.3 */
4114 1085534 : if (TYPE_REF_P (type))
4115 : {
4116 60 : if (complain & tf_error)
4117 12 : error_at (loc, "new cannot be applied to a reference type");
4118 : else
4119 48 : return error_mark_node;
4120 12 : type = TREE_TYPE (type);
4121 : }
4122 :
4123 1085486 : if (TREE_CODE (type) == FUNCTION_TYPE)
4124 : {
4125 21 : if (complain & tf_error)
4126 3 : error_at (loc, "new cannot be applied to a function type");
4127 21 : return error_mark_node;
4128 : }
4129 :
4130 : /* P1009: Array size deduction in new-expressions. */
4131 1085465 : const bool array_p = TREE_CODE (type) == ARRAY_TYPE;
4132 1085465 : if (*init
4133 : /* If the array didn't specify its bound, we have to deduce it. */
4134 1085465 : && ((array_p && !TYPE_DOMAIN (type))
4135 : /* For C++20 array with parenthesized-init, we have to process
4136 : the parenthesized-list. But don't do it for (), which is
4137 : value-initialization, and INIT should stay empty. */
4138 759011 : || (cxx_dialect >= cxx20
4139 755792 : && (array_p || nelts)
4140 9746 : && !(*init)->is_empty ())))
4141 : {
4142 : /* This means we have 'new T[]()'. */
4143 335 : if ((*init)->is_empty ())
4144 : {
4145 9 : tree ctor = build_constructor (init_list_type_node, NULL);
4146 9 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
4147 9 : vec_safe_push (*init, ctor);
4148 : }
4149 335 : tree &elt = (**init)[0];
4150 : /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */
4151 335 : if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20)
4152 : {
4153 74 : tree ctor = build_constructor_from_vec (init_list_type_node, *init);
4154 74 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
4155 74 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
4156 74 : elt = ctor;
4157 : /* We've squashed all the vector elements into the first one;
4158 : truncate the rest. */
4159 74 : (*init)->truncate (1);
4160 : }
4161 : /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */
4162 335 : if (array_p && !TYPE_DOMAIN (type))
4163 : {
4164 : /* We need to reshape before deducing the bounds to handle code like
4165 :
4166 : struct S { int x, y; };
4167 : new S[]{1, 2, 3, 4};
4168 :
4169 : which should deduce S[2]. But don't change ELT itself: we want to
4170 : pass a list-initializer to build_new_1, even for STRING_CSTs. */
4171 143 : tree e = elt;
4172 143 : if (BRACE_ENCLOSED_INITIALIZER_P (e))
4173 140 : e = reshape_init (type, e, complain);
4174 143 : cp_complete_array_type (&type, e, /*do_default*/false);
4175 : }
4176 : }
4177 :
4178 : /* The type allocated must be complete. If the new-type-id was
4179 : "T[N]" then we are just checking that "T" is complete here, but
4180 : that is equivalent, since the value of "N" doesn't matter. */
4181 1085465 : if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
4182 63 : return error_mark_node;
4183 :
4184 1085402 : rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
4185 1085402 : if (rval == error_mark_node)
4186 : return error_mark_node;
4187 :
4188 1084707 : if (processing_template_decl)
4189 : {
4190 97583 : tree ret = build_raw_new_expr (loc, orig_placement, type, orig_nelts,
4191 : orig_init, use_global_new);
4192 97583 : release_tree_vector (orig_placement);
4193 97583 : release_tree_vector (orig_init);
4194 97583 : return ret;
4195 : }
4196 :
4197 : /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
4198 987124 : rval = build1_loc (loc, NOP_EXPR, TREE_TYPE (rval), rval);
4199 987124 : suppress_warning (rval, OPT_Wunused_value);
4200 :
4201 987124 : return rval;
4202 : }
4203 :
4204 : static tree
4205 29027 : build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
4206 : special_function_kind auto_delete_vec,
4207 : int use_global_delete, tsubst_flags_t complain,
4208 : bool in_cleanup = false)
4209 : {
4210 29027 : tree virtual_size;
4211 29027 : tree ptype = build_pointer_type (type = complete_type (type));
4212 29027 : tree size_exp;
4213 :
4214 : /* Temporary variables used by the loop. */
4215 29027 : tree tbase, tbase_init;
4216 :
4217 : /* This is the body of the loop that implements the deletion of a
4218 : single element, and moves temp variables to next elements. */
4219 29027 : tree body;
4220 :
4221 : /* This is the LOOP_EXPR that governs the deletion of the elements. */
4222 29027 : tree loop = 0;
4223 :
4224 : /* This is the thing that governs what to do after the loop has run. */
4225 29027 : tree deallocate_expr = 0;
4226 :
4227 : /* This is the BIND_EXPR which holds the outermost iterator of the
4228 : loop. It is convenient to set this variable up and test it before
4229 : executing any other code in the loop.
4230 : This is also the containing expression returned by this function. */
4231 29027 : tree controller = NULL_TREE;
4232 29027 : tree tmp;
4233 :
4234 : /* We should only have 1-D arrays here. */
4235 29027 : gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
4236 :
4237 29027 : if (base == error_mark_node || maxindex == error_mark_node)
4238 : return error_mark_node;
4239 :
4240 29027 : if (!verify_type_context (loc, TCTX_DEALLOCATION, type,
4241 29027 : !(complain & tf_error)))
4242 0 : return error_mark_node;
4243 :
4244 29027 : if (!COMPLETE_TYPE_P (type))
4245 : {
4246 27 : if (cxx_dialect > cxx23)
4247 : {
4248 21 : if (complain & tf_error)
4249 : {
4250 21 : auto_diagnostic_group d;
4251 21 : int saved_errorcount = errorcount;
4252 21 : if (permerror_opt (loc, OPT_Wdelete_incomplete,
4253 : "operator %<delete []%> used on "
4254 : "incomplete type"))
4255 : {
4256 14 : cxx_incomplete_type_inform (type);
4257 14 : if (errorcount != saved_errorcount)
4258 8 : return error_mark_node;
4259 : }
4260 21 : }
4261 : else
4262 0 : return error_mark_node;
4263 : }
4264 6 : else if (complain & tf_warning)
4265 : {
4266 6 : auto_diagnostic_group d;
4267 6 : if (warning_at (loc, OPT_Wdelete_incomplete,
4268 : "possible problem detected in invocation of "
4269 : "operator %<delete []%>"))
4270 : {
4271 4 : cxx_incomplete_type_diagnostic (base, type,
4272 : diagnostics::kind::warning);
4273 4 : inform (loc, "neither the destructor nor the "
4274 : "class-specific operator %<delete []%> will be called, "
4275 : "even if they are declared when the class is defined");
4276 : }
4277 6 : }
4278 : /* This size won't actually be used. */
4279 19 : size_exp = size_one_node;
4280 19 : goto no_destructor;
4281 : }
4282 :
4283 29000 : size_exp = size_in_bytes (type);
4284 :
4285 29000 : if (! MAYBE_CLASS_TYPE_P (type))
4286 2616 : goto no_destructor;
4287 26384 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
4288 : {
4289 : /* Make sure the destructor is callable. */
4290 19270 : if (type_build_dtor_call (type))
4291 : {
4292 60 : tmp = build_delete (loc, ptype, base, sfk_complete_destructor,
4293 : LOOKUP_NORMAL|LOOKUP_DESTRUCTOR|LOOKUP_NONVIRTUAL,
4294 : 1, complain);
4295 60 : if (tmp == error_mark_node)
4296 : return error_mark_node;
4297 : }
4298 19258 : goto no_destructor;
4299 : }
4300 :
4301 : /* The below is short by the cookie size. */
4302 7114 : virtual_size = size_binop (MULT_EXPR, size_exp,
4303 : fold_convert (sizetype, maxindex));
4304 :
4305 7114 : tbase = create_temporary_var (ptype);
4306 7114 : DECL_INITIAL (tbase)
4307 7114 : = fold_build_pointer_plus_loc (loc, fold_convert (ptype, base),
4308 : virtual_size);
4309 7114 : tbase_init = build_stmt (loc, DECL_EXPR, tbase);
4310 7114 : controller = build3 (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
4311 7114 : TREE_SIDE_EFFECTS (controller) = 1;
4312 7114 : BIND_EXPR_VEC_DTOR (controller) = true;
4313 :
4314 7114 : body = build1 (EXIT_EXPR, void_type_node,
4315 : build2 (EQ_EXPR, boolean_type_node, tbase,
4316 : fold_convert (ptype, base)));
4317 7114 : tmp = fold_build1_loc (loc, NEGATE_EXPR, sizetype, size_exp);
4318 7114 : tmp = fold_build_pointer_plus (tbase, tmp);
4319 7114 : tmp = cp_build_modify_expr (loc, tbase, NOP_EXPR, tmp, complain);
4320 7114 : if (tmp == error_mark_node)
4321 : return error_mark_node;
4322 7114 : body = build_compound_expr (loc, body, tmp);
4323 : /* [expr.delete]/3: "In an array delete expression, if the dynamic type of
4324 : the object to be deleted is not similar to its static type, the behavior
4325 : is undefined." So we can set LOOKUP_NONVIRTUAL. */
4326 7114 : tmp = build_delete (loc, ptype, tbase, sfk_complete_destructor,
4327 : LOOKUP_NORMAL|LOOKUP_DESTRUCTOR|LOOKUP_NONVIRTUAL,
4328 : 1, complain);
4329 7114 : if (tmp == error_mark_node)
4330 : return error_mark_node;
4331 7114 : body = build_compound_expr (loc, body, tmp);
4332 :
4333 7114 : loop = build1 (LOOP_EXPR, void_type_node, body);
4334 :
4335 : /* If one destructor throws, keep trying to clean up the rest, unless we're
4336 : already in a build_vec_init cleanup. */
4337 7103 : if (flag_exceptions && !in_cleanup && !processing_template_decl
4338 12651 : && !expr_noexcept_p (tmp, tf_none))
4339 : {
4340 150 : loop = build2 (TRY_CATCH_EXPR, void_type_node, loop,
4341 : unshare_expr (loop));
4342 : /* Tell honor_protect_cleanup_actions to discard this on the
4343 : exceptional path. */
4344 150 : TRY_CATCH_IS_CLEANUP (loop) = true;
4345 : }
4346 :
4347 7114 : loop = build_compound_expr (loc, tbase_init, loop);
4348 :
4349 29007 : no_destructor:
4350 : /* Delete the storage if appropriate. */
4351 29007 : if (auto_delete_vec == sfk_deleting_destructor)
4352 : {
4353 21986 : tree base_tbd;
4354 :
4355 : /* The below is short by the cookie size. */
4356 21986 : virtual_size = size_binop (MULT_EXPR, size_exp,
4357 : fold_convert (sizetype, maxindex));
4358 :
4359 21986 : if (! TYPE_VEC_NEW_USES_COOKIE (type))
4360 : /* no header */
4361 : base_tbd = base;
4362 : else
4363 : {
4364 144 : tree cookie_size;
4365 :
4366 144 : cookie_size = targetm.cxx.get_cookie_size (type);
4367 144 : base_tbd = cp_build_binary_op (loc,
4368 : MINUS_EXPR,
4369 : cp_convert (string_type_node,
4370 : base, complain),
4371 : cookie_size,
4372 : complain);
4373 144 : if (base_tbd == error_mark_node)
4374 : return error_mark_node;
4375 144 : base_tbd = cp_convert (ptype, base_tbd, complain);
4376 : /* True size with header. */
4377 144 : virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
4378 : }
4379 :
4380 21986 : deallocate_expr
4381 21986 : = build_op_delete_call (VEC_DELETE_EXPR, base_tbd, virtual_size,
4382 : use_global_delete & 1, /*placement=*/NULL_TREE,
4383 : /*placement_args=*/NULL,
4384 : /*alloc_fn=*/NULL_TREE, complain);
4385 : }
4386 :
4387 29007 : body = loop;
4388 29007 : if (deallocate_expr == error_mark_node)
4389 : return error_mark_node;
4390 29007 : else if (!deallocate_expr)
4391 : ;
4392 21986 : else if (!body)
4393 : body = deallocate_expr;
4394 : else
4395 : /* The delete operator must be called, even if a destructor
4396 : throws. */
4397 141 : body = build2 (TRY_FINALLY_EXPR, void_type_node, body, deallocate_expr);
4398 :
4399 29007 : if (!body)
4400 48 : body = integer_zero_node;
4401 :
4402 : /* Outermost wrapper: If pointer is null, punt. */
4403 29007 : tree cond = build2_loc (loc, NE_EXPR, boolean_type_node, base,
4404 29007 : fold_convert (TREE_TYPE (base), nullptr_node));
4405 : /* This is a compiler generated comparison, don't emit
4406 : e.g. -Wnonnull-compare warning for it. */
4407 29007 : suppress_warning (cond, OPT_Wnonnull_compare);
4408 29007 : body = build3_loc (loc, COND_EXPR, void_type_node,
4409 : cond, body, integer_zero_node);
4410 29007 : COND_EXPR_IS_VEC_DELETE (body) = true;
4411 29007 : body = build1 (NOP_EXPR, void_type_node, body);
4412 :
4413 29007 : if (controller)
4414 : {
4415 7114 : TREE_OPERAND (controller, 1) = body;
4416 7114 : body = controller;
4417 : }
4418 :
4419 29007 : if (TREE_CODE (base) == SAVE_EXPR)
4420 : /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
4421 0 : body = build2 (COMPOUND_EXPR, void_type_node, base, body);
4422 :
4423 29007 : return convert_to_void (body, ICV_CAST, complain);
4424 : }
4425 :
4426 : /* Create an unnamed variable of the indicated TYPE. */
4427 :
4428 : tree
4429 225967 : create_temporary_var (tree type)
4430 : {
4431 225967 : tree decl;
4432 :
4433 225967 : decl = build_decl (input_location,
4434 : VAR_DECL, NULL_TREE, type);
4435 225967 : TREE_USED (decl) = 1;
4436 225967 : DECL_ARTIFICIAL (decl) = 1;
4437 225967 : DECL_IGNORED_P (decl) = 1;
4438 225967 : DECL_CONTEXT (decl) = current_function_decl;
4439 :
4440 225967 : return decl;
4441 : }
4442 :
4443 : /* Create a new temporary variable of the indicated TYPE, initialized
4444 : to INIT.
4445 :
4446 : It is not entered into current_binding_level, because that breaks
4447 : things when it comes time to do final cleanups (which take place
4448 : "outside" the binding contour of the function). */
4449 :
4450 : tree
4451 62977 : get_temp_regvar (tree type, tree init)
4452 : {
4453 62977 : tree decl;
4454 :
4455 62977 : decl = create_temporary_var (type);
4456 62977 : add_decl_expr (decl);
4457 :
4458 62977 : finish_expr_stmt (cp_build_modify_expr (input_location, decl, INIT_EXPR,
4459 : init, tf_warning_or_error));
4460 :
4461 62977 : return decl;
4462 : }
4463 :
4464 : /* Subroutine of build_vec_init. Returns true if assigning to an array of
4465 : INNER_ELT_TYPE from INIT is trivial. */
4466 :
4467 : static bool
4468 40 : vec_copy_assign_is_trivial (tree inner_elt_type, tree init)
4469 : {
4470 40 : tree fromtype = inner_elt_type;
4471 40 : if (lvalue_p (init))
4472 34 : fromtype = cp_build_reference_type (fromtype, /*rval*/false);
4473 40 : return is_trivially_xible (MODIFY_EXPR, inner_elt_type, fromtype);
4474 : }
4475 :
4476 : /* Subroutine of build_vec_init: Check that the array has at least N
4477 : elements. Other parameters are local variables in build_vec_init. */
4478 :
4479 : void
4480 75 : finish_length_check (tree atype, tree iterator, tree obase, unsigned n)
4481 : {
4482 75 : tree nelts = build_int_cst (ptrdiff_type_node, n - 1);
4483 75 : if (TREE_CODE (atype) != ARRAY_TYPE)
4484 : {
4485 13 : if (flag_exceptions)
4486 : {
4487 13 : tree c = fold_build2 (LT_EXPR, boolean_type_node, iterator,
4488 : nelts);
4489 13 : c = build3 (COND_EXPR, void_type_node, c,
4490 : throw_bad_array_new_length (), void_node);
4491 13 : finish_expr_stmt (c);
4492 : }
4493 : /* Don't check an array new when -fno-exceptions. */
4494 : }
4495 62 : else if (sanitize_flags_p (SANITIZE_BOUNDS)
4496 62 : && current_function_decl != NULL_TREE)
4497 : {
4498 : /* Make sure the last element of the initializer is in bounds. */
4499 3 : finish_expr_stmt
4500 3 : (ubsan_instrument_bounds
4501 : (input_location, obase, &nelts, /*ignore_off_by_one*/false));
4502 : }
4503 75 : }
4504 :
4505 : /* walk_tree callback to collect temporaries in an expression. */
4506 :
4507 : tree
4508 2799 : find_temps_r (tree *tp, int *walk_subtrees, void *data)
4509 : {
4510 2799 : vec<tree*> &temps = *static_cast<auto_vec<tree*> *>(data);
4511 2799 : tree t = *tp;
4512 2799 : if (TREE_CODE (t) == TARGET_EXPR
4513 2799 : && !TARGET_EXPR_ELIDING_P (t))
4514 65 : temps.safe_push (tp);
4515 2734 : else if (TYPE_P (t))
4516 0 : *walk_subtrees = 0;
4517 :
4518 2799 : return NULL_TREE;
4519 : }
4520 :
4521 : /* walk_tree callback to collect temporaries in an expression that
4522 : are allocator arguments to standard library classes. */
4523 :
4524 : static tree
4525 69319 : find_allocator_temps_r (tree *tp, int *walk_subtrees, void *data)
4526 : {
4527 69319 : vec<tree*> &temps = *static_cast<auto_vec<tree*> *>(data);
4528 69319 : tree t = *tp;
4529 69319 : if (TYPE_P (t))
4530 : {
4531 46 : *walk_subtrees = 0;
4532 46 : return NULL_TREE;
4533 : }
4534 :
4535 : /* If this is a call to a constructor for a std:: class, look for
4536 : a reference-to-allocator argument. */
4537 69273 : tree fn = cp_get_callee_fndecl_nofold (t);
4538 14870 : if (fn && DECL_CONSTRUCTOR_P (fn)
4539 72909 : && decl_in_std_namespace_p (TYPE_NAME (DECL_CONTEXT (fn))))
4540 : {
4541 1855 : int nargs = call_expr_nargs (t);
4542 4260 : for (int i = 1; i < nargs; ++i)
4543 : {
4544 2405 : tree arg = get_nth_callarg (t, i);
4545 2405 : tree atype = TREE_TYPE (arg);
4546 2405 : if (TREE_CODE (atype) == REFERENCE_TYPE
4547 2405 : && is_std_allocator (TREE_TYPE (atype)))
4548 : {
4549 655 : STRIP_NOPS (arg);
4550 655 : if (TREE_CODE (arg) == ADDR_EXPR)
4551 : {
4552 655 : tree *ap = &TREE_OPERAND (arg, 0);
4553 655 : if (TREE_CODE (*ap) == TARGET_EXPR)
4554 655 : temps.safe_push (ap);
4555 : }
4556 : }
4557 : }
4558 : }
4559 :
4560 : return NULL_TREE;
4561 : }
4562 :
4563 : /* If INIT initializes a standard library class, and involves a temporary
4564 : std::allocator<T>, use ALLOC_OBJ for all such temporaries.
4565 :
4566 : Note that this can clobber the input to build_vec_init; no unsharing is
4567 : done. To make this safe we use the TARGET_EXPR in all places rather than
4568 : pulling out the TARGET_EXPR_SLOT.
4569 :
4570 : Used by build_vec_init when initializing an array of e.g. strings to reuse
4571 : the same temporary allocator for all of the strings. We can do this because
4572 : std::allocator has no data and the standard library doesn't care about the
4573 : address of allocator objects.
4574 :
4575 : ??? Add an attribute to allow users to assert the same property for other
4576 : classes, i.e. one object of the type is interchangeable with any other? */
4577 :
4578 : static void
4579 3103 : combine_allocator_temps (tree &init, tree &alloc_obj)
4580 : {
4581 3103 : auto_vec<tree*> temps;
4582 3103 : cp_walk_tree_without_duplicates (&init, find_allocator_temps_r, &temps);
4583 5024 : for (tree *p : temps)
4584 : {
4585 655 : if (!alloc_obj)
4586 190 : alloc_obj = *p;
4587 : else
4588 465 : *p = alloc_obj;
4589 : }
4590 3103 : }
4591 :
4592 : /* `build_vec_init' returns tree structure that performs
4593 : initialization of a vector of aggregate types.
4594 :
4595 : BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
4596 : to the first element, of POINTER_TYPE.
4597 : MAXINDEX is the maximum index of the array (one less than the
4598 : number of elements). It is only used if BASE is a pointer or
4599 : TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
4600 :
4601 : INIT is the (possibly NULL) initializer.
4602 :
4603 : If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
4604 : elements in the array are value-initialized.
4605 :
4606 : FROM_ARRAY is 0 if we should init everything with INIT
4607 : (i.e., every element initialized from INIT).
4608 : FROM_ARRAY is 1 if we should index into INIT in parallel
4609 : with initialization of DECL.
4610 : FROM_ARRAY is 2 if we should index into INIT in parallel,
4611 : but use assignment instead of initialization. */
4612 :
4613 : tree
4614 23915 : build_vec_init (tree base, tree maxindex, tree init,
4615 : bool explicit_value_init_p,
4616 : int from_array,
4617 : tsubst_flags_t complain,
4618 : vec<tree, va_gc>** cleanup_flags /* = nullptr */)
4619 : {
4620 23915 : tree rval;
4621 23915 : tree base2 = NULL_TREE;
4622 23915 : tree itype = NULL_TREE;
4623 23915 : tree iterator;
4624 : /* The type of BASE. */
4625 23915 : tree atype = TREE_TYPE (base);
4626 : /* The type of an element in the array. */
4627 23915 : tree type = TREE_TYPE (atype);
4628 : /* The element type reached after removing all outer array
4629 : types. */
4630 23915 : tree inner_elt_type;
4631 : /* The type of a pointer to an element in the array. */
4632 23915 : tree ptype;
4633 23915 : tree stmt_expr;
4634 23915 : tree compound_stmt;
4635 23915 : int destroy_temps;
4636 23915 : HOST_WIDE_INT num_initialized_elts = 0;
4637 23915 : bool is_global;
4638 23915 : tree obase = base;
4639 23915 : bool xvalue = false;
4640 23915 : bool errors = false;
4641 23915 : location_t loc = (init ? cp_expr_loc_or_input_loc (init)
4642 11642 : : location_of (base));
4643 :
4644 23915 : if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
4645 13549 : maxindex = array_type_nelts_minus_one (atype);
4646 :
4647 23915 : if (maxindex == NULL_TREE || maxindex == error_mark_node)
4648 0 : return error_mark_node;
4649 :
4650 23915 : maxindex = maybe_constant_value (maxindex);
4651 23915 : if (explicit_value_init_p)
4652 9753 : gcc_assert (!init);
4653 :
4654 23915 : inner_elt_type = strip_array_types (type);
4655 :
4656 : /* Look through the TARGET_EXPR around a compound literal. */
4657 12273 : if (init && TREE_CODE (init) == TARGET_EXPR
4658 152 : && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
4659 147 : && from_array != 2
4660 24062 : && (same_type_ignoring_top_level_qualifiers_p
4661 147 : (TREE_TYPE (init), atype)))
4662 24 : init = TARGET_EXPR_INITIAL (init);
4663 :
4664 23915 : if (tree vi = get_vec_init_expr (init))
4665 5 : init = VEC_INIT_EXPR_INIT (vi);
4666 :
4667 23915 : bool direct_init = false;
4668 11640 : if (from_array && init && BRACE_ENCLOSED_INITIALIZER_P (init)
4669 23966 : && CONSTRUCTOR_NELTS (init) == 1)
4670 : {
4671 51 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
4672 51 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
4673 51 : && TREE_CODE (elt) != VEC_INIT_EXPR)
4674 : {
4675 51 : direct_init = DIRECT_LIST_INIT_P (init);
4676 : init = elt;
4677 : }
4678 : }
4679 :
4680 : /* from_array doesn't apply to initialization from CONSTRUCTOR. */
4681 23915 : if (init && TREE_CODE (init) == CONSTRUCTOR)
4682 : from_array = 0;
4683 :
4684 : /* If we have a braced-init-list or string constant, make sure that the array
4685 : is big enough for all the initializers. */
4686 10847 : bool length_check = (init
4687 12273 : && (TREE_CODE (init) == STRING_CST
4688 12191 : || (TREE_CODE (init) == CONSTRUCTOR
4689 1426 : && CONSTRUCTOR_NELTS (init) > 0))
4690 1261 : && !TREE_CONSTANT (maxindex));
4691 :
4692 : if (init
4693 12273 : && TREE_CODE (atype) == ARRAY_TYPE
4694 11905 : && TREE_CONSTANT (maxindex)
4695 11820 : && !vla_type_p (type)
4696 40 : && (from_array == 2
4697 40 : ? vec_copy_assign_is_trivial (inner_elt_type, init)
4698 11762 : : !TYPE_NEEDS_CONSTRUCTING (type))
4699 22256 : && ((TREE_CODE (init) == CONSTRUCTOR
4700 17 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
4701 14 : || (same_type_ignoring_top_level_qualifiers_p
4702 14 : (atype, TREE_TYPE (init))))
4703 : /* Don't do this if the CONSTRUCTOR might contain something
4704 : that might throw and require us to clean up. */
4705 17 : && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
4706 14 : || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
4707 10443 : || from_array))
4708 : {
4709 : /* Do non-default initialization of trivial arrays resulting from
4710 : brace-enclosed initializers. In this case, digest_init and
4711 : store_constructor will handle the semantics for us. */
4712 :
4713 10448 : if (BRACE_ENCLOSED_INITIALIZER_P (init))
4714 3 : init = digest_init (atype, init, complain);
4715 10448 : stmt_expr = cp_build_init_expr (base, init);
4716 10448 : return stmt_expr;
4717 : }
4718 :
4719 13467 : maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
4720 13467 : maxindex = fold_simple (maxindex);
4721 :
4722 13467 : if (TREE_CODE (atype) == ARRAY_TYPE)
4723 : {
4724 3101 : ptype = build_pointer_type (type);
4725 3101 : base = decay_conversion (base, complain);
4726 3101 : if (base == error_mark_node)
4727 : return error_mark_node;
4728 3101 : base = cp_convert (ptype, base, complain);
4729 : }
4730 : else
4731 : ptype = atype;
4732 :
4733 13467 : if (integer_all_onesp (maxindex))
4734 : {
4735 : /* Shortcut zero element case to avoid unneeded constructor synthesis. */
4736 52 : if (init && TREE_SIDE_EFFECTS (init))
4737 0 : base = build2 (COMPOUND_EXPR, ptype, init, base);
4738 : return base;
4739 : }
4740 :
4741 : /* The code we are generating looks like:
4742 : ({
4743 : T* t1 = (T*) base;
4744 : T* rval = t1;
4745 : ptrdiff_t iterator = maxindex;
4746 : try {
4747 : for (; iterator != -1; --iterator) {
4748 : ... initialize *t1 ...
4749 : ++t1;
4750 : }
4751 : } catch (...) {
4752 : ... destroy elements that were constructed ...
4753 : }
4754 : rval;
4755 : })
4756 :
4757 : We can omit the try and catch blocks if we know that the
4758 : initialization will never throw an exception, or if the array
4759 : elements do not have destructors. We can omit the loop completely if
4760 : the elements of the array do not have constructors.
4761 :
4762 : We actually wrap the entire body of the above in a STMT_EXPR, for
4763 : tidiness.
4764 :
4765 : When copying from array to another, when the array elements have
4766 : only trivial copy constructors, we should use __builtin_memcpy
4767 : rather than generating a loop. That way, we could take advantage
4768 : of whatever cleverness the back end has for dealing with copies
4769 : of blocks of memory. */
4770 :
4771 13415 : is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
4772 13415 : destroy_temps = stmts_are_full_exprs_p ();
4773 13415 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4774 13415 : rval = get_temp_regvar (ptype, base);
4775 13415 : base = get_temp_regvar (ptype, rval);
4776 13415 : tree iterator_targ = get_internal_target_expr (maxindex);
4777 13415 : add_stmt (iterator_targ);
4778 13415 : iterator = TARGET_EXPR_SLOT (iterator_targ);
4779 :
4780 : /* If initializing one array from another, initialize element by
4781 : element. We rely upon the below calls to do the argument
4782 : checking. Evaluate the initializer before entering the try block. */
4783 13415 : if (from_array)
4784 : {
4785 343 : if (lvalue_kind (init) & clk_rvalueref)
4786 32 : xvalue = true;
4787 343 : if (TREE_CODE (init) == TARGET_EXPR)
4788 : {
4789 : /* Avoid error in decay_conversion. */
4790 123 : base2 = decay_conversion (TARGET_EXPR_SLOT (init), complain);
4791 123 : base2 = cp_build_compound_expr (init, base2, tf_none);
4792 : }
4793 : else
4794 220 : base2 = decay_conversion (init, complain);
4795 343 : if (base2 == error_mark_node)
4796 : return error_mark_node;
4797 343 : itype = TREE_TYPE (base2);
4798 343 : base2 = get_temp_regvar (itype, base2);
4799 343 : itype = TREE_TYPE (itype);
4800 : }
4801 :
4802 : /* Protect the entire array initialization so that we can destroy
4803 : the partially constructed array if an exception is thrown.
4804 : But don't do this if we're assigning. */
4805 13415 : if (flag_exceptions
4806 : /* And don't clean up from clobbers, the actual initialization will
4807 : follow as a separate build_vec_init. */
4808 13352 : && !(init && TREE_CLOBBER_P (init))
4809 13350 : && from_array != 2
4810 26727 : && type_build_dtor_call (type))
4811 : {
4812 1577 : tree e;
4813 1577 : tree m = cp_build_binary_op (input_location,
4814 : MINUS_EXPR, maxindex, iterator,
4815 : complain);
4816 :
4817 : /* Flatten multi-dimensional array since build_vec_delete only
4818 : expects one-dimensional array. */
4819 1577 : if (TREE_CODE (type) == ARRAY_TYPE)
4820 212 : m = cp_build_binary_op (input_location,
4821 : MULT_EXPR, m,
4822 : /* Avoid mixing signed and unsigned. */
4823 106 : convert (TREE_TYPE (m),
4824 : array_type_nelts_total (type)),
4825 : complain);
4826 :
4827 1577 : e = build_vec_delete_1 (input_location, rval, m,
4828 : inner_elt_type, sfk_complete_destructor,
4829 : /*use_global_delete=*/0, complain,
4830 : /*in_cleanup*/true);
4831 1577 : if (e == error_mark_node)
4832 : errors = true;
4833 1571 : else if (TREE_SIDE_EFFECTS (e))
4834 : {
4835 1563 : TARGET_EXPR_CLEANUP (iterator_targ) = e;
4836 1563 : CLEANUP_EH_ONLY (iterator_targ) = true;
4837 :
4838 : /* Since we push this cleanup before doing any initialization,
4839 : cleanups for any temporaries in the initialization are naturally
4840 : within our cleanup region, so we don't want
4841 : wrap_temporary_cleanups to do anything for arrays. But if the
4842 : array is a subobject, we need to tell split_nonconstant_init or
4843 : cp_genericize_target_expr how to turn off this cleanup in favor
4844 : of the cleanup for the complete object.
4845 :
4846 : ??? For an array temporary such as an initializer_list backing
4847 : array, it would avoid redundancy to leave this cleanup active,
4848 : clear CLEANUP_EH_ONLY, and not build another cleanup for the
4849 : temporary itself. But that breaks when gimplify_target_expr adds
4850 : a clobber cleanup that runs before the build_vec_init cleanup. */
4851 1563 : if (cleanup_flags)
4852 1992 : vec_safe_push (*cleanup_flags,
4853 996 : build_tree_list (rval, build_zero_cst (ptype)));
4854 : }
4855 : }
4856 :
4857 : /* Should we try to create a constant initializer? */
4858 13415 : bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
4859 3086 : && TREE_CONSTANT (maxindex)
4860 2991 : && (init ? TREE_CODE (init) == CONSTRUCTOR
4861 : : (type_has_constexpr_default_constructor
4862 1619 : (inner_elt_type)
4863 : /* Value-initialization of scalars is constexpr. */
4864 1165 : || (explicit_value_init_p
4865 85 : && SCALAR_TYPE_P (inner_elt_type))))
4866 17968 : && (literal_type_p (inner_elt_type)
4867 737 : || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
4868 13415 : vec<constructor_elt, va_gc> *const_vec = NULL;
4869 13415 : bool saw_non_const = false;
4870 : /* If we're initializing a static array, we want to do static
4871 : initialization of any elements with constant initializers even if
4872 : some are non-constant. */
4873 13415 : bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
4874 :
4875 13415 : if (init
4876 1804 : && TREE_CODE (init) == CONSTRUCTOR
4877 1394 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4878 1069 : && INTEGRAL_TYPE_P (type)
4879 13577 : && same_type_p (type, TREE_TYPE (TREE_TYPE (init))))
4880 : {
4881 : /* If RAW_DATA_CST is in the CONSTRUCTOR, we want to optimize
4882 : INTEGER_CST, RAW_DATA_CST, INTEGER_CST into just
4883 : RAW_DATA_CST larger by 2 chars if possible. But at least
4884 : for now punt if braced_lists_to_strings instead turns it
4885 : into a STRING_CST in the try_const case, as the STRING_CST
4886 : case doesn't handle try_const. */
4887 162 : tree new_init = braced_lists_to_strings (TREE_TYPE (init), init);
4888 162 : if (!try_const || TREE_CODE (new_init) == CONSTRUCTOR)
4889 : init = new_init;
4890 : }
4891 :
4892 15219 : bool empty_list = false;
4893 1804 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
4894 13738 : && CONSTRUCTOR_NELTS (init) == 0)
4895 : /* Skip over the handling of non-empty init lists. */
4896 : empty_list = true;
4897 :
4898 : /* Maybe pull out constant value when from_array? */
4899 :
4900 13214 : else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
4901 : {
4902 : /* Do non-default initialization of non-trivial arrays resulting from
4903 : brace-enclosed initializers. */
4904 1184 : unsigned HOST_WIDE_INT idx;
4905 : /* Used in RAW_DATA_CST handling below if we need to expand it
4906 : (not digested char-sized integer type). It is -1 when not peeling
4907 : off such RAW_DATA_CST, otherwise indicates which index from
4908 : the RAW_DATA_CST has been handled most recently. */
4909 1184 : unsigned int raw_idx = -1;
4910 1184 : tree field, elt;
4911 : /* If the constructor already has the array type, it's been through
4912 : digest_init, so we shouldn't try to do anything more. */
4913 1184 : bool digested = (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4914 1184 : && same_type_p (type, TREE_TYPE (TREE_TYPE (init))));
4915 1184 : from_array = 0;
4916 :
4917 1184 : if (length_check)
4918 100 : finish_length_check (atype, iterator, obase, CONSTRUCTOR_NELTS (init));
4919 :
4920 1184 : if (try_const)
4921 854 : vec_alloc (const_vec, CONSTRUCTOR_NELTS (init));
4922 :
4923 1184 : tree alloc_obj = NULL_TREE;
4924 :
4925 4366 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
4926 : {
4927 3182 : tree one_init;
4928 :
4929 3182 : if (TREE_CODE (elt) == RAW_DATA_CST)
4930 : {
4931 6 : if (digested
4932 6 : && (TREE_CODE (type) == INTEGER_TYPE
4933 0 : || is_byte_access_type (type))
4934 12 : && TYPE_PRECISION (type) == CHAR_BIT)
4935 : {
4936 : /* If possible, handle RAW_DATA_CST as ARRAY_TYPE
4937 : copy from ctor to MEM_REF. */
4938 6 : tree atype
4939 6 : = build_array_of_n_type (type, RAW_DATA_LENGTH (elt));
4940 6 : tree alias_set
4941 6 : = build_int_cst (build_pointer_type (type), 0);
4942 6 : tree lhs = build2 (MEM_REF, atype, base, alias_set);
4943 6 : tree ctor
4944 6 : = build_constructor_single (atype, bitsize_zero_node,
4945 : copy_node (elt));
4946 6 : one_init = build2 (MODIFY_EXPR, void_type_node, lhs, ctor);
4947 :
4948 6 : if (try_const)
4949 : {
4950 0 : if (!field)
4951 0 : field = size_int (num_initialized_elts);
4952 0 : CONSTRUCTOR_APPEND_ELT (const_vec, field, elt);
4953 0 : if (do_static_init)
4954 0 : one_init = NULL_TREE;
4955 : }
4956 :
4957 6 : if (one_init)
4958 6 : finish_expr_stmt (one_init);
4959 :
4960 : /* Adjust the counter and pointer. */
4961 6 : tree length = build_int_cst (ptrdiff_type_node,
4962 6 : RAW_DATA_LENGTH (elt));
4963 6 : one_init = cp_build_binary_op (loc, MINUS_EXPR, iterator,
4964 : length, complain);
4965 6 : gcc_assert (one_init != error_mark_node);
4966 6 : one_init = build2 (MODIFY_EXPR, void_type_node, iterator,
4967 : one_init);
4968 6 : finish_expr_stmt (one_init);
4969 :
4970 6 : one_init = cp_build_binary_op (loc, PLUS_EXPR, base, length,
4971 : complain);
4972 6 : gcc_assert (one_init != error_mark_node);
4973 6 : one_init = build2 (MODIFY_EXPR, void_type_node, base,
4974 : one_init);
4975 6 : finish_expr_stmt (one_init);
4976 :
4977 6 : num_initialized_elts += RAW_DATA_LENGTH (elt);
4978 6 : continue;
4979 6 : }
4980 : else
4981 : {
4982 : /* Otherwise peel it off into separate constants. */
4983 0 : tree orig_elt = elt;
4984 0 : elt = build_int_cst (TREE_TYPE (elt),
4985 0 : RAW_DATA_UCHAR_ELT (elt, ++raw_idx));
4986 0 : if (raw_idx && field)
4987 0 : field = size_binop (PLUS_EXPR, field,
4988 : bitsize_int (raw_idx));
4989 0 : if (raw_idx + 1 == (unsigned) RAW_DATA_LENGTH (orig_elt))
4990 : raw_idx = -1;
4991 : else
4992 0 : --idx;
4993 : }
4994 : }
4995 :
4996 3176 : tree baseref = build1 (INDIRECT_REF, type, base);
4997 :
4998 : /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can
4999 : handle cleanup flags properly. */
5000 3176 : gcc_checking_assert (!target_expr_needs_replace (elt));
5001 :
5002 3176 : if (digested)
5003 2980 : one_init = cp_build_init_expr (baseref, elt);
5004 196 : else if (tree vi = get_vec_init_expr (elt))
5005 24 : one_init = expand_vec_init_expr (baseref, vi, complain,
5006 : cleanup_flags);
5007 172 : else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
5008 160 : one_init = build_aggr_init (baseref, elt, 0, complain);
5009 : else
5010 12 : one_init = cp_build_modify_expr (input_location, baseref,
5011 : NOP_EXPR, elt, complain);
5012 3176 : if (one_init == error_mark_node)
5013 0 : errors = true;
5014 3176 : if (try_const)
5015 : {
5016 1148 : if (!field)
5017 0 : field = size_int (num_initialized_elts);
5018 1148 : tree e = maybe_constant_init (one_init);
5019 1148 : if (reduced_constant_expression_p (e))
5020 : {
5021 131 : CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
5022 131 : if (do_static_init)
5023 73 : one_init = NULL_TREE;
5024 : else
5025 58 : one_init = cp_build_init_expr (baseref, e);
5026 : }
5027 : else
5028 : {
5029 1017 : if (do_static_init)
5030 : {
5031 31 : tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
5032 : true);
5033 31 : if (value)
5034 0 : CONSTRUCTOR_APPEND_ELT (const_vec, field, value);
5035 : }
5036 : saw_non_const = true;
5037 : }
5038 : }
5039 :
5040 3176 : tree end = NULL_TREE, body = NULL_TREE;
5041 3176 : if (field && TREE_CODE (field) == RANGE_EXPR)
5042 : {
5043 6 : tree sub
5044 6 : = cp_build_binary_op (loc, MINUS_EXPR, iterator,
5045 6 : build_int_cst (TREE_TYPE (iterator),
5046 6 : range_expr_nelts (field)),
5047 : complain);
5048 6 : gcc_assert (sub != error_mark_node);
5049 6 : end = get_internal_target_expr (sub);
5050 6 : add_stmt (end);
5051 :
5052 6 : body = push_stmt_list ();
5053 : }
5054 :
5055 3176 : if (one_init)
5056 : {
5057 : /* Only create one std::allocator temporary. */
5058 3103 : combine_allocator_temps (one_init, alloc_obj);
5059 3103 : finish_expr_stmt (one_init);
5060 : }
5061 :
5062 3176 : one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, false,
5063 : complain);
5064 3176 : if (one_init == error_mark_node)
5065 : errors = true;
5066 : else
5067 3176 : finish_expr_stmt (one_init);
5068 :
5069 3176 : one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
5070 : complain);
5071 3176 : if (one_init == error_mark_node)
5072 : errors = true;
5073 : else
5074 3176 : finish_expr_stmt (one_init);
5075 :
5076 3176 : if (field && TREE_CODE (field) == RANGE_EXPR)
5077 : {
5078 6 : tree exit = build1 (EXIT_EXPR, void_type_node,
5079 : build2 (EQ_EXPR, boolean_type_node,
5080 6 : iterator, TARGET_EXPR_SLOT (end)));
5081 6 : add_stmt (exit);
5082 6 : body = pop_stmt_list (body);
5083 6 : tree loop = build1 (LOOP_EXPR, void_type_node, body);
5084 6 : add_stmt (loop);
5085 6 : num_initialized_elts += range_expr_nelts (field);
5086 6 : }
5087 : else
5088 3170 : num_initialized_elts++;
5089 : }
5090 :
5091 : /* Any elements without explicit initializers get T{}. */
5092 1184 : if (!TREE_CLOBBER_P (init))
5093 1182 : empty_list = true;
5094 1184 : }
5095 419 : else if (init && TREE_CODE (init) == STRING_CST)
5096 : {
5097 : /* Check that the array is at least as long as the string. */
5098 88 : if (length_check)
5099 25 : finish_length_check (atype, iterator, obase,
5100 25 : TREE_STRING_LENGTH (init));
5101 88 : tree length = build_int_cst (ptrdiff_type_node,
5102 88 : TREE_STRING_LENGTH (init));
5103 :
5104 : /* Copy the string to the first part of the array. */
5105 88 : tree alias_set = build_int_cst (build_pointer_type (type), 0);
5106 88 : tree lhs = build2 (MEM_REF, TREE_TYPE (init), base, alias_set);
5107 88 : tree stmt = build2 (MODIFY_EXPR, void_type_node, lhs, init);
5108 88 : finish_expr_stmt (stmt);
5109 :
5110 : /* Adjust the counter and pointer. */
5111 88 : stmt = cp_build_binary_op (loc, MINUS_EXPR, iterator, length, complain);
5112 88 : stmt = build2 (MODIFY_EXPR, void_type_node, iterator, stmt);
5113 88 : finish_expr_stmt (stmt);
5114 :
5115 88 : stmt = cp_build_binary_op (loc, PLUS_EXPR, base, length, complain);
5116 88 : stmt = build2 (MODIFY_EXPR, void_type_node, base, stmt);
5117 88 : finish_expr_stmt (stmt);
5118 :
5119 : /* And set the rest of the array to NUL. */
5120 88 : from_array = 0;
5121 88 : explicit_value_init_p = true;
5122 88 : }
5123 11942 : else if (from_array)
5124 : {
5125 318 : if (init)
5126 : /* OK, we set base2 above. */;
5127 0 : else if (CLASS_TYPE_P (type)
5128 0 : && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
5129 : {
5130 0 : if (complain & tf_error)
5131 0 : error ("initializer ends prematurely");
5132 : errors = true;
5133 : }
5134 : }
5135 :
5136 : /* Now, default-initialize any remaining elements. We don't need to
5137 : do that if a) the type does not need constructing, or b) we've
5138 : already initialized all the elements.
5139 :
5140 : We do need to keep going if we're copying an array. */
5141 :
5142 13415 : if (try_const && !init
5143 13415 : && (cxx_dialect < cxx20
5144 429 : || !default_init_uninitialized_part (inner_elt_type)))
5145 : /* With a constexpr default constructor, which we checked for when
5146 : setting try_const above, default-initialization is equivalent to
5147 : value-initialization, and build_value_init gives us something more
5148 : friendly to maybe_constant_init. Except in C++20 and up a constexpr
5149 : constructor need not initialize all the members. */
5150 : explicit_value_init_p = true;
5151 13415 : if (from_array
5152 13415 : || ((type_build_ctor_call (type) || init || explicit_value_init_p)
5153 13097 : && ! (tree_fits_shwi_p (maxindex)
5154 : && (num_initialized_elts
5155 3414 : == tree_to_shwi (maxindex) + 1))))
5156 : {
5157 : /* If the ITERATOR is lesser or equal to -1, then we don't have to loop;
5158 : we've already initialized all the elements. */
5159 12439 : tree for_stmt;
5160 12439 : tree elt_init;
5161 12439 : tree to;
5162 :
5163 12439 : for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
5164 12439 : finish_init_stmt (for_stmt);
5165 12439 : finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator,
5166 12439 : build_int_cst (TREE_TYPE (iterator), -1)),
5167 : for_stmt, false, 0, false);
5168 : /* We used to pass this decrement to finish_for_expr; now we add it to
5169 : elt_init below so it's part of the same full-expression as the
5170 : initialization, and thus happens before any potentially throwing
5171 : temporary cleanups. */
5172 12439 : tree decr = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
5173 : complain);
5174 :
5175 :
5176 12439 : to = build1 (INDIRECT_REF, type, base);
5177 :
5178 : /* If the initializer is {}, then all elements are initialized from T{}.
5179 : But for non-classes, that's the same as value-initialization. */
5180 12439 : if (empty_list)
5181 : {
5182 407 : if (cxx_dialect >= cxx11
5183 407 : && (CLASS_TYPE_P (type)
5184 116 : || TREE_CODE (type) == ARRAY_TYPE))
5185 : {
5186 308 : init = build_constructor (init_list_type_node, NULL);
5187 : }
5188 : else
5189 : {
5190 : init = NULL_TREE;
5191 : explicit_value_init_p = true;
5192 : }
5193 : }
5194 :
5195 12439 : if (from_array)
5196 : {
5197 318 : tree from;
5198 :
5199 318 : if (base2)
5200 : {
5201 318 : from = build1 (INDIRECT_REF, itype, base2);
5202 318 : if (xvalue)
5203 32 : from = move (from);
5204 318 : if (direct_init)
5205 : {
5206 39 : if (TREE_CODE (type) == ARRAY_TYPE)
5207 : {
5208 : /* Wrap the initializer in a CONSTRUCTOR so that
5209 : build_vec_init recognizes it as
5210 : direct-initialization. */
5211 12 : from = build_constructor_single (init_list_type_node,
5212 : NULL_TREE, from);
5213 12 : CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
5214 : }
5215 : else
5216 27 : from = build_tree_list (NULL_TREE, from);
5217 : }
5218 : }
5219 : else
5220 : from = NULL_TREE;
5221 :
5222 318 : if (TREE_CODE (type) == ARRAY_TYPE)
5223 42 : elt_init = build_vec_init (to, NULL_TREE, from, /*val_init*/false,
5224 : from_array, complain);
5225 276 : else if (from_array == 2)
5226 37 : elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR,
5227 : from, complain);
5228 239 : else if (type_build_ctor_call (type))
5229 236 : elt_init = build_aggr_init (to, from, 0, complain);
5230 3 : else if (from)
5231 3 : elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, from,
5232 : complain);
5233 : else
5234 0 : gcc_unreachable ();
5235 : }
5236 12121 : else if (TREE_CODE (type) == ARRAY_TYPE)
5237 : {
5238 41 : if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)
5239 255 : && !TREE_CLOBBER_P (init))
5240 : {
5241 0 : if ((complain & tf_error))
5242 0 : error_at (loc, "array must be initialized "
5243 : "with a brace-enclosed initializer");
5244 0 : elt_init = error_mark_node;
5245 : }
5246 : else
5247 255 : elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
5248 : 0, init,
5249 : explicit_value_init_p,
5250 : 0, complain);
5251 : }
5252 11866 : else if (explicit_value_init_p)
5253 : {
5254 10159 : elt_init = build_value_init (type, complain);
5255 10159 : if (elt_init != error_mark_node)
5256 10159 : elt_init = cp_build_init_expr (to, elt_init);
5257 : }
5258 : else
5259 : {
5260 1707 : gcc_assert (type_build_ctor_call (type) || init);
5261 1707 : if (CLASS_TYPE_P (type))
5262 1699 : elt_init = build_aggr_init (to, init, 0, complain);
5263 : else
5264 : {
5265 8 : if (TREE_CODE (init) == TREE_LIST)
5266 0 : init = build_x_compound_expr_from_list (init, ELK_INIT,
5267 : complain);
5268 8 : elt_init = (init == error_mark_node
5269 8 : ? error_mark_node
5270 2 : : build2 (INIT_EXPR, type, to, init));
5271 : }
5272 : }
5273 :
5274 12439 : if (elt_init == error_mark_node)
5275 44 : errors = true;
5276 :
5277 12439 : if (try_const)
5278 : {
5279 : /* FIXME refs to earlier elts */
5280 671 : tree e = maybe_constant_init (elt_init);
5281 671 : if (reduced_constant_expression_p (e))
5282 : {
5283 411 : if (initializer_zerop (e))
5284 : /* Don't fill the CONSTRUCTOR with zeros. */
5285 233 : e = NULL_TREE;
5286 411 : if (do_static_init)
5287 37 : elt_init = NULL_TREE;
5288 : }
5289 : else
5290 : {
5291 260 : saw_non_const = true;
5292 260 : if (do_static_init)
5293 17 : e = build_zero_init (TREE_TYPE (e), NULL_TREE, true);
5294 : else
5295 : e = NULL_TREE;
5296 : }
5297 :
5298 428 : if (e)
5299 : {
5300 178 : HOST_WIDE_INT last = tree_to_shwi (maxindex);
5301 178 : if (num_initialized_elts <= last)
5302 : {
5303 178 : tree field = size_int (num_initialized_elts);
5304 178 : if (num_initialized_elts != last)
5305 132 : field = build2 (RANGE_EXPR, sizetype, field,
5306 132 : size_int (last));
5307 178 : CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
5308 : }
5309 : }
5310 : }
5311 :
5312 : /* [class.temporary]: "There are three contexts in which temporaries are
5313 : destroyed at a different point than the end of the full-
5314 : expression. The first context is when a default constructor is called
5315 : to initialize an element of an array with no corresponding
5316 : initializer. The second context is when a copy constructor is called
5317 : to copy an element of an array while the entire array is copied. In
5318 : either case, if the constructor has one or more default arguments, the
5319 : destruction of every temporary created in a default argument is
5320 : sequenced before the construction of the next array element, if any."
5321 :
5322 : So, for this loop, statements are full-expressions. */
5323 12439 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5324 12439 : if (elt_init && !errors)
5325 12352 : elt_init = build2 (COMPOUND_EXPR, void_type_node, elt_init, decr);
5326 : else
5327 : elt_init = decr;
5328 12439 : finish_expr_stmt (elt_init);
5329 12439 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
5330 :
5331 12439 : finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false,
5332 : complain));
5333 12439 : if (base2)
5334 343 : finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, false,
5335 : complain));
5336 :
5337 12439 : finish_for_stmt (for_stmt);
5338 : }
5339 :
5340 : /* The value of the array initialization is the array itself, RVAL
5341 : is a pointer to the first element. */
5342 13415 : finish_stmt_expr_expr (rval, stmt_expr);
5343 :
5344 13415 : stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
5345 :
5346 13415 : current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
5347 :
5348 13415 : if (errors)
5349 50 : return error_mark_node;
5350 :
5351 13365 : if (try_const)
5352 : {
5353 1006 : if (!saw_non_const)
5354 : {
5355 : /* If we're not generating the loop, we don't need to reset the
5356 : iterator. */
5357 488 : if (cleanup_flags
5358 488 : && !vec_safe_is_empty (*cleanup_flags))
5359 : {
5360 2 : auto l = (*cleanup_flags)->last ();
5361 2 : gcc_assert (TREE_PURPOSE (l) == rval);
5362 2 : (*cleanup_flags)->pop ();
5363 : }
5364 488 : tree const_init = build_constructor (atype, const_vec);
5365 488 : return build2 (INIT_EXPR, atype, obase, const_init);
5366 : }
5367 518 : else if (do_static_init && !vec_safe_is_empty (const_vec))
5368 5 : DECL_INITIAL (obase) = build_constructor (atype, const_vec);
5369 : else
5370 513 : vec_free (const_vec);
5371 : }
5372 :
5373 : /* Now make the result have the correct type. */
5374 12877 : if (TREE_CODE (atype) == ARRAY_TYPE)
5375 : {
5376 2583 : atype = build_reference_type (atype);
5377 2583 : stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
5378 2583 : stmt_expr = convert_from_reference (stmt_expr);
5379 : }
5380 :
5381 12877 : return stmt_expr;
5382 : }
5383 :
5384 : /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
5385 : build_delete. */
5386 :
5387 : static tree
5388 9036633 : build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
5389 : tsubst_flags_t complain)
5390 : {
5391 9036633 : tree name;
5392 9036633 : switch (dtor_kind)
5393 : {
5394 8984846 : case sfk_complete_destructor:
5395 8984846 : name = complete_dtor_identifier;
5396 8984846 : break;
5397 :
5398 80 : case sfk_base_destructor:
5399 80 : name = base_dtor_identifier;
5400 80 : break;
5401 :
5402 51707 : case sfk_deleting_destructor:
5403 51707 : name = deleting_dtor_identifier;
5404 51707 : break;
5405 :
5406 0 : default:
5407 0 : gcc_unreachable ();
5408 : }
5409 :
5410 9036633 : return build_special_member_call (exp, name,
5411 : /*args=*/NULL,
5412 9036633 : /*binfo=*/TREE_TYPE (exp),
5413 : flags,
5414 9036633 : complain);
5415 : }
5416 :
5417 : /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
5418 : ADDR is an expression which yields the store to be destroyed.
5419 : AUTO_DELETE is the name of the destructor to call, i.e., either
5420 : sfk_complete_destructor, sfk_base_destructor, or
5421 : sfk_deleting_destructor.
5422 :
5423 : FLAGS is the logical disjunction of zero or more LOOKUP_
5424 : flags. See cp-tree.h for more info. */
5425 :
5426 : tree
5427 9058859 : build_delete (location_t loc, tree otype, tree addr,
5428 : special_function_kind auto_delete,
5429 : int flags, int use_global_delete, tsubst_flags_t complain)
5430 : {
5431 9058859 : tree expr;
5432 :
5433 9058859 : if (addr == error_mark_node)
5434 : return error_mark_node;
5435 :
5436 9058859 : tree type = TYPE_MAIN_VARIANT (otype);
5437 :
5438 : /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
5439 : set to `error_mark_node' before it gets properly cleaned up. */
5440 9058859 : if (type == error_mark_node)
5441 : return error_mark_node;
5442 :
5443 9058859 : if (TYPE_PTR_P (type))
5444 7140441 : type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5445 :
5446 9058859 : if (TREE_CODE (type) == ARRAY_TYPE)
5447 : {
5448 5467 : if (TYPE_DOMAIN (type) == NULL_TREE)
5449 : {
5450 3 : if (complain & tf_error)
5451 3 : error_at (loc, "unknown array size in delete");
5452 3 : return error_mark_node;
5453 : }
5454 5464 : return build_vec_delete (loc, addr, array_type_nelts_minus_one (type),
5455 5464 : auto_delete, use_global_delete, complain);
5456 : }
5457 :
5458 9053392 : bool deleting = (auto_delete == sfk_deleting_destructor);
5459 9053392 : gcc_assert (deleting == !(flags & LOOKUP_DESTRUCTOR));
5460 :
5461 9053392 : if (TYPE_PTR_P (otype))
5462 : {
5463 7140433 : addr = mark_rvalue_use (addr);
5464 :
5465 : /* We don't want to warn about delete of void*, only other
5466 : incomplete types. Deleting other incomplete types
5467 : invokes undefined behavior, but it is not ill-formed, so
5468 : compile to something that would even do The Right Thing
5469 : (TM) should the type have a trivial dtor and no delete
5470 : operator. */
5471 7140433 : if (!VOID_TYPE_P (type))
5472 : {
5473 7140414 : complete_type (type);
5474 7140414 : if (deleting
5475 7251225 : && !verify_type_context (loc, TCTX_DEALLOCATION, type,
5476 110811 : !(complain & tf_error)))
5477 0 : return error_mark_node;
5478 :
5479 7140414 : if (!COMPLETE_TYPE_P (type))
5480 : {
5481 24 : if (cxx_dialect > cxx23)
5482 : {
5483 20 : if (complain & tf_error)
5484 : {
5485 20 : auto_diagnostic_group d;
5486 20 : int saved_errorcount = errorcount;
5487 20 : if (permerror_opt (loc, OPT_Wdelete_incomplete,
5488 : "operator %<delete%> used on "
5489 : "incomplete type"))
5490 : {
5491 14 : cxx_incomplete_type_inform (type);
5492 14 : if (errorcount != saved_errorcount)
5493 8 : return error_mark_node;
5494 : }
5495 20 : }
5496 : else
5497 0 : return error_mark_node;
5498 : }
5499 4 : else if (complain & tf_warning)
5500 : {
5501 4 : auto_diagnostic_group d;
5502 4 : if (warning_at (loc, OPT_Wdelete_incomplete,
5503 : "possible problem detected in invocation of "
5504 : "%<operator delete%>"))
5505 : {
5506 4 : cxx_incomplete_type_diagnostic (addr, type,
5507 : diagnostics::kind::warning);
5508 4 : inform (loc,
5509 : "neither the destructor nor the class-specific "
5510 : "%<operator delete%> will be called, even if "
5511 : "they are declared when the class is defined");
5512 : }
5513 4 : }
5514 : }
5515 110787 : else if (deleting && warn_delnonvdtor
5516 1073 : && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
5517 7141384 : && TYPE_POLYMORPHIC_P (type))
5518 : {
5519 521 : tree dtor = CLASSTYPE_DESTRUCTOR (type);
5520 1039 : if (!dtor || !DECL_VINDEX (dtor))
5521 : {
5522 15 : if (CLASSTYPE_PURE_VIRTUALS (type))
5523 3 : warning_at (loc, OPT_Wdelete_non_virtual_dtor,
5524 : "deleting object of abstract class type %qT"
5525 : " which has non-virtual destructor"
5526 : " will cause undefined behavior", type);
5527 : else
5528 12 : warning_at (loc, OPT_Wdelete_non_virtual_dtor,
5529 : "deleting object of polymorphic class type %qT"
5530 : " which has non-virtual destructor"
5531 : " might cause undefined behavior", type);
5532 : }
5533 : }
5534 : }
5535 :
5536 : /* Throw away const and volatile on target type of addr. */
5537 7140425 : addr = convert_force (build_pointer_type (type), addr, 0, complain);
5538 : }
5539 : else
5540 : {
5541 : /* Don't check PROTECT here; leave that decision to the
5542 : destructor. If the destructor is accessible, call it,
5543 : else report error. */
5544 1912959 : addr = cp_build_addr_expr (addr, complain);
5545 1912959 : if (addr == error_mark_node)
5546 : return error_mark_node;
5547 :
5548 1912959 : addr = convert_force (build_pointer_type (type), addr, 0, complain);
5549 : }
5550 :
5551 9053384 : tree addr_expr = NULL_TREE;
5552 9053384 : if (deleting)
5553 : /* We will use ADDR multiple times so we must save it. */
5554 : {
5555 110822 : addr_expr = get_internal_target_expr (addr);
5556 110822 : addr = TARGET_EXPR_SLOT (addr_expr);
5557 : }
5558 :
5559 9053384 : bool virtual_p = false;
5560 9053384 : if (type_build_dtor_call (type))
5561 : {
5562 9036639 : if (CLASSTYPE_LAZY_DESTRUCTOR (type))
5563 108398 : lazily_declare_fn (sfk_destructor, type);
5564 9036639 : virtual_p = DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type));
5565 : }
5566 :
5567 9053384 : tree head = NULL_TREE;
5568 9053384 : tree do_delete = NULL_TREE;
5569 9053384 : bool destroying_delete = false;
5570 :
5571 9053384 : if (!deleting)
5572 : {
5573 : /* Leave do_delete null. */
5574 : }
5575 : /* For `::delete x', we must not use the deleting destructor
5576 : since then we would not be sure to get the global `operator
5577 : delete'. */
5578 110822 : else if (use_global_delete)
5579 : {
5580 36 : head = get_internal_target_expr (build_headof (addr));
5581 : /* Delete the object. */
5582 36 : do_delete
5583 36 : = build_op_delete_call (DELETE_EXPR, head, cxx_sizeof_nowarn (type),
5584 : /*global_p=*/true, /*placement=*/NULL_TREE,
5585 : /*placement_args=*/NULL,
5586 : /*alloc_fn=*/NULL_TREE, complain);
5587 : /* Otherwise, treat this like a complete object destructor
5588 : call. */
5589 36 : auto_delete = sfk_complete_destructor;
5590 : }
5591 : /* If the destructor is non-virtual, there is no deleting
5592 : variant. Instead, we must explicitly call the appropriate
5593 : `operator delete' here. */
5594 110786 : else if (!virtual_p)
5595 : {
5596 : /* Build the call. */
5597 59079 : do_delete
5598 59079 : = build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
5599 : /*global_p=*/false, /*placement=*/NULL_TREE,
5600 : /*placement_args=*/NULL,
5601 : /*alloc_fn=*/NULL_TREE, complain);
5602 : /* Call the complete object destructor. */
5603 59079 : auto_delete = sfk_complete_destructor;
5604 59079 : if (do_delete != error_mark_node)
5605 : {
5606 59058 : tree fn = get_callee_fndecl (do_delete);
5607 59058 : destroying_delete = destroying_delete_p (fn);
5608 : }
5609 : }
5610 51707 : else if (TYPE_GETS_REG_DELETE (type))
5611 : /* Make sure we have access to the member op delete, even though
5612 : we'll actually be calling it from the destructor. */
5613 12 : build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
5614 : /*global_p=*/false, /*placement=*/NULL_TREE,
5615 : /*placement_args=*/NULL, /*alloc_fn=*/NULL_TREE,
5616 : complain);
5617 :
5618 59106 : if (destroying_delete)
5619 : /* The operator delete will call the destructor. */
5620 : expr = addr;
5621 9053371 : else if (type_build_dtor_call (type))
5622 9036633 : expr = build_dtor_call (cp_build_fold_indirect_ref (addr),
5623 : auto_delete, flags, complain);
5624 : else
5625 16738 : expr = build_trivial_dtor_call (addr);
5626 9053384 : if (expr == error_mark_node)
5627 : return error_mark_node;
5628 :
5629 9053099 : if (!deleting)
5630 : {
5631 8942283 : protected_set_expr_location (expr, loc);
5632 8942283 : return expr;
5633 : }
5634 :
5635 110816 : if (do_delete == error_mark_node)
5636 : return error_mark_node;
5637 :
5638 110795 : if (do_delete && !TREE_SIDE_EFFECTS (expr))
5639 : expr = do_delete;
5640 110761 : else if (do_delete)
5641 : /* The delete operator must be called, regardless of whether
5642 : the destructor throws.
5643 :
5644 : [expr.delete]/7 The deallocation function is called
5645 : regardless of whether the destructor for the object or some
5646 : element of the array throws an exception. */
5647 59054 : expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete);
5648 :
5649 : /* We need to calculate this before the dtor changes the vptr. */
5650 110795 : if (head)
5651 30 : expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
5652 :
5653 : /* Handle deleting a null pointer. */
5654 110795 : warning_sentinel s (warn_address);
5655 110795 : tree ifexp = cp_build_binary_op (loc, NE_EXPR, addr,
5656 : nullptr_node, complain);
5657 110795 : ifexp = cp_fully_fold (ifexp);
5658 :
5659 110795 : if (ifexp == error_mark_node)
5660 : return error_mark_node;
5661 : /* This is a compiler generated comparison, don't emit
5662 : e.g. -Wnonnull-compare warning for it. */
5663 110795 : else if (TREE_CODE (ifexp) == NE_EXPR)
5664 110795 : suppress_warning (ifexp, OPT_Wnonnull_compare);
5665 :
5666 110795 : if (!integer_nonzerop (ifexp))
5667 110795 : expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node);
5668 :
5669 110795 : if (addr_expr)
5670 110795 : expr = cp_build_compound_expr (addr_expr, expr, tf_none);
5671 :
5672 110795 : protected_set_expr_location (expr, loc);
5673 110795 : return expr;
5674 110795 : }
5675 :
5676 : /* At the beginning of a destructor, push cleanups that will call the
5677 : destructors for our base classes and members.
5678 :
5679 : Called from begin_destructor_body. */
5680 :
5681 : void
5682 1692764 : push_base_cleanups (void)
5683 : {
5684 1692764 : tree binfo, base_binfo;
5685 1692764 : int i;
5686 1692764 : tree member;
5687 1692764 : tree expr;
5688 1692764 : vec<tree, va_gc> *vbases;
5689 :
5690 : /* Run destructors for all virtual baseclasses. */
5691 1692764 : if (!ABSTRACT_CLASS_TYPE_P (current_class_type)
5692 3313472 : && CLASSTYPE_VBASECLASSES (current_class_type))
5693 : {
5694 7864 : tree cond = (condition_conversion
5695 7864 : (build2 (BIT_AND_EXPR, integer_type_node,
5696 7864 : current_in_charge_parm,
5697 : integer_two_node)));
5698 :
5699 : /* The CLASSTYPE_VBASECLASSES vector is in initialization
5700 : order, which is also the right order for pushing cleanups. */
5701 27217 : for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
5702 27217 : vec_safe_iterate (vbases, i, &base_binfo); i++)
5703 : {
5704 19353 : if (type_build_dtor_call (BINFO_TYPE (base_binfo)))
5705 : {
5706 19161 : expr = build_special_member_call (current_class_ref,
5707 : base_dtor_identifier,
5708 : NULL,
5709 : base_binfo,
5710 : (LOOKUP_NORMAL
5711 : | LOOKUP_NONVIRTUAL),
5712 : tf_warning_or_error);
5713 19161 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5714 : {
5715 19161 : expr = build3 (COND_EXPR, void_type_node, cond,
5716 : expr, void_node);
5717 19161 : finish_decl_cleanup (NULL_TREE, expr);
5718 : }
5719 : }
5720 : }
5721 : }
5722 :
5723 : /* Take care of the remaining baseclasses. */
5724 2495167 : for (binfo = TYPE_BINFO (current_class_type), i = 0;
5725 2495167 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5726 : {
5727 802403 : if (BINFO_VIRTUAL_P (base_binfo)
5728 802403 : || !type_build_dtor_call (BINFO_TYPE (base_binfo)))
5729 330877 : continue;
5730 :
5731 471526 : expr = build_special_member_call (current_class_ref,
5732 : base_dtor_identifier,
5733 : NULL, base_binfo,
5734 : LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
5735 : tf_warning_or_error);
5736 471526 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5737 471480 : finish_decl_cleanup (NULL_TREE, expr);
5738 : }
5739 :
5740 : /* Don't automatically destroy union members. */
5741 1692764 : if (TREE_CODE (current_class_type) == UNION_TYPE)
5742 1692764 : return;
5743 :
5744 63874465 : for (member = TYPE_FIELDS (current_class_type); member;
5745 62182353 : member = DECL_CHAIN (member))
5746 : {
5747 62182353 : tree this_type = TREE_TYPE (member);
5748 122408446 : if (this_type == error_mark_node
5749 62182350 : || TREE_CODE (member) != FIELD_DECL
5750 64997131 : || DECL_ARTIFICIAL (member))
5751 60226093 : continue;
5752 1956260 : if (ANON_AGGR_TYPE_P (this_type))
5753 85911 : continue;
5754 1870349 : if (type_build_dtor_call (this_type))
5755 : {
5756 427631 : tree this_member = (build_class_member_access_expr
5757 427631 : (current_class_ref, member,
5758 : /*access_path=*/NULL_TREE,
5759 : /*preserve_reference=*/false,
5760 : tf_warning_or_error));
5761 427631 : expr = build_delete (input_location, this_type, this_member,
5762 : sfk_complete_destructor,
5763 : LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
5764 : 0, tf_warning_or_error);
5765 427631 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
5766 399318 : finish_decl_cleanup (NULL_TREE, expr);
5767 : }
5768 : }
5769 : }
5770 :
5771 : /* Build a C++ vector delete expression.
5772 : MAXINDEX is the number of elements to be deleted.
5773 : ELT_SIZE is the nominal size of each element in the vector.
5774 : BASE is the expression that should yield the store to be deleted.
5775 : This function expands (or synthesizes) these calls itself.
5776 : AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
5777 :
5778 : This also calls delete for virtual baseclasses of elements of the vector.
5779 :
5780 : Update: MAXINDEX is no longer needed. The size can be extracted from the
5781 : start of the vector for pointers, and from the type for arrays. We still
5782 : use MAXINDEX for arrays because it happens to already have one of the
5783 : values we'd have to extract. (We could use MAXINDEX with pointers to
5784 : confirm the size, and trap if the numbers differ; not clear that it'd
5785 : be worth bothering.) */
5786 :
5787 : tree
5788 27450 : build_vec_delete (location_t loc, tree base, tree maxindex,
5789 : special_function_kind auto_delete_vec,
5790 : int use_global_delete, tsubst_flags_t complain)
5791 : {
5792 27450 : tree type;
5793 27450 : tree rval;
5794 27450 : tree base_init = NULL_TREE;
5795 :
5796 27450 : type = TREE_TYPE (base);
5797 :
5798 27450 : if (TYPE_PTR_P (type))
5799 : {
5800 : /* Step back one from start of vector, and read dimension. */
5801 21994 : tree cookie_addr;
5802 21994 : tree size_ptr_type = build_pointer_type (sizetype);
5803 :
5804 21994 : base = mark_rvalue_use (base);
5805 21994 : if (TREE_SIDE_EFFECTS (base))
5806 : {
5807 9420 : base_init = get_internal_target_expr (base);
5808 9420 : base = TARGET_EXPR_SLOT (base_init);
5809 : }
5810 21994 : type = strip_array_types (TREE_TYPE (type));
5811 21994 : cookie_addr = fold_build1_loc (loc, NEGATE_EXPR,
5812 21994 : sizetype, TYPE_SIZE_UNIT (sizetype));
5813 21994 : cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
5814 : cookie_addr);
5815 21994 : maxindex = cp_build_fold_indirect_ref (cookie_addr);
5816 : }
5817 5456 : else if (TREE_CODE (type) == ARRAY_TYPE)
5818 : {
5819 : /* Get the total number of things in the array, maxindex is a
5820 : bad name. */
5821 5456 : maxindex = array_type_nelts_total (type);
5822 5456 : type = strip_array_types (type);
5823 5456 : base = decay_conversion (base, complain);
5824 5456 : if (base == error_mark_node)
5825 : return error_mark_node;
5826 5456 : if (TREE_SIDE_EFFECTS (base))
5827 : {
5828 0 : base_init = get_internal_target_expr (base);
5829 0 : base = TARGET_EXPR_SLOT (base_init);
5830 : }
5831 : }
5832 : else
5833 : {
5834 0 : if (base != error_mark_node && !(complain & tf_error))
5835 0 : error_at (loc,
5836 : "type to vector delete is neither pointer or array type");
5837 0 : return error_mark_node;
5838 : }
5839 :
5840 27450 : rval = build_vec_delete_1 (loc, base, maxindex, type, auto_delete_vec,
5841 : use_global_delete, complain);
5842 27450 : if (base_init && rval != error_mark_node)
5843 9420 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
5844 :
5845 27450 : protected_set_expr_location (rval, loc);
5846 27450 : return rval;
5847 : }
5848 :
5849 : #include "gt-cp-init.h"
|