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