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