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