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 5654946 : begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
66 : {
67 5654946 : bool is_global = !building_stmt_list_p ();
68 :
69 5654946 : *stmt_expr_p = begin_stmt_expr ();
70 5654946 : *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
71 :
72 5654946 : 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 5654946 : finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
80 : {
81 5654946 : finish_compound_stmt (compound_stmt);
82 :
83 5654946 : stmt_expr = finish_stmt_expr (stmt_expr, true);
84 :
85 6180461 : gcc_assert (!building_stmt_list_p () == is_global);
86 :
87 5654946 : 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 10196770 : dfs_initialize_vtbl_ptrs (tree binfo, void *data)
98 : {
99 10196770 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
100 : return dfs_skip_bases;
101 :
102 3030149 : if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
103 : {
104 1192250 : tree base_ptr = TREE_VALUE ((tree) data);
105 :
106 1192250 : base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
107 : tf_warning_or_error);
108 :
109 1192250 : 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 7855300 : initialize_vtbl_ptrs (tree addr)
120 : {
121 7855300 : tree list;
122 7855300 : tree type;
123 :
124 7855300 : type = TREE_TYPE (TREE_TYPE (addr));
125 7855300 : 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 7855300 : dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
132 7855300 : }
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 5593992 : build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
152 : tree field_size)
153 : {
154 5593992 : 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 5593992 : gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
175 :
176 : /* An initializer is unqualified. */
177 5593992 : type = cv_unqualified (type);
178 :
179 5593992 : if (type == error_mark_node)
180 : ;
181 5593960 : 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 13992 : init = get_null_reflection ();
185 5579968 : 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 5052596 : else if (TYPE_PTR_OR_PTRMEM_P (type))
192 1219457 : init = fold (convert (type, nullptr_node));
193 3833139 : else if (NULLPTR_TYPE_P (type))
194 97 : init = build_int_cst (type, 0);
195 146497 : else if (SCALAR_TYPE_P (type))
196 3686545 : init = build_zero_cst (type);
197 146497 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
198 : {
199 133276 : tree field, next;
200 133276 : vec<constructor_elt, va_gc> *v = NULL;
201 :
202 : /* Iterate over the fields, building initializations. */
203 2808865 : for (field = TYPE_FIELDS (type); field; field = next)
204 : {
205 2675589 : next = DECL_CHAIN (field);
206 :
207 2675589 : if (TREE_CODE (field) != FIELD_DECL)
208 2519624 : continue;
209 :
210 : /* For unions, only the first field is initialized. */
211 155965 : if (TREE_CODE (type) == UNION_TYPE)
212 56632 : next = NULL_TREE;
213 :
214 155965 : 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 155962 : if (field_size)
221 : {
222 19562 : tree bitpos = bit_position (field);
223 21736 : if (TREE_CODE (bitpos) == INTEGER_CST
224 19562 : && !tree_int_cst_lt (bitpos, field_size))
225 2174 : continue;
226 : }
227 :
228 : /* Don't add zero width bitfields. */
229 153788 : if (DECL_C_BIT_FIELD (field)
230 153788 : && 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 153785 : if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
238 : {
239 153758 : tree new_field_size
240 153758 : = (DECL_FIELD_IS_BASE (field)
241 12441 : && DECL_SIZE (field)
242 12441 : && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
243 166199 : ? DECL_SIZE (field) : NULL_TREE;
244 153758 : tree value = build_zero_init_1 (TREE_TYPE (field),
245 : /*nelts=*/NULL_TREE,
246 : static_storage_p,
247 : new_field_size);
248 153758 : if (value)
249 153732 : CONSTRUCTOR_APPEND_ELT(v, field, value);
250 : }
251 : }
252 :
253 : /* Build a constructor to contain the initializations. */
254 133276 : init = build_constructor (type, v);
255 133276 : CONSTRUCTOR_ZERO_PADDING_BITS (init) = 1;
256 : }
257 13221 : else if (TREE_CODE (type) == ARRAY_TYPE)
258 : {
259 365 : tree max_index;
260 365 : vec<constructor_elt, va_gc> *v = NULL;
261 :
262 : /* Iterate over the array elements, building initializations. */
263 365 : 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 365 : else if (TYPE_DOMAIN (type) == NULL_TREE)
269 15 : return NULL_TREE;
270 : else
271 350 : 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 350 : if (max_index == error_mark_node)
276 : return error_mark_node;
277 350 : 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 350 : if (!integer_minus_onep (max_index))
282 : {
283 326 : constructor_elt ce;
284 :
285 : /* If this is a one element array, we just use a regular init. */
286 326 : if (integer_zerop (max_index))
287 39 : ce.index = size_zero_node;
288 : else
289 287 : ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
290 : max_index);
291 :
292 326 : ce.value = build_zero_init_1 (TREE_TYPE (type), /*nelts=*/NULL_TREE,
293 : static_storage_p, NULL_TREE);
294 326 : if (ce.value)
295 : {
296 326 : vec_alloc (v, 1);
297 326 : v->quick_push (ce);
298 : }
299 : }
300 :
301 : /* Build a constructor to contain the initializations. */
302 350 : init = build_constructor (type, v);
303 : }
304 12856 : else if (VECTOR_TYPE_P (type))
305 12842 : 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 5066559 : if (init)
311 5066559 : 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 5417447 : build_zero_init (tree type, tree nelts, bool static_storage_p)
330 : {
331 5417447 : 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 7222143 : 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 7222143 : if (CLASS_TYPE_P (type) && type_build_ctor_call (type))
362 : {
363 1602597 : tree ctor
364 1602597 : = build_special_member_call (NULL_TREE, complete_ctor_identifier,
365 : NULL, type, LOOKUP_NORMAL, complain);
366 1602597 : if (ctor == error_mark_node || TREE_CONSTANT (ctor))
367 : return ctor;
368 1601214 : 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 1601208 : tree fn = NULL_TREE;
372 1601208 : if (TREE_CODE (ctor) == CALL_EXPR)
373 1291463 : fn = get_callee_fndecl (ctor);
374 1601208 : ctor = build_aggr_init_expr (type, ctor);
375 1601208 : if (fn && user_provided_p (fn))
376 : return ctor;
377 675095 : 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 326370 : AGGR_INIT_ZERO_FIRST (ctor) = 1;
384 326370 : 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 5968271 : push_deferring_access_checks (dk_deferred);
392 5968271 : tree r = build_value_init_noctor (type, complain);
393 5968271 : pop_deferring_access_checks ();
394 5968271 : 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 5968271 : build_value_init_noctor (tree type, tsubst_flags_t complain)
402 : {
403 5968271 : 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 5968268 : if (CLASS_TYPE_P (type))
412 : {
413 1473100 : gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type)
414 : || errorcount != 0);
415 :
416 1473100 : if (TREE_CODE (type) != UNION_TYPE)
417 : {
418 1461274 : tree field;
419 1461274 : vec<constructor_elt, va_gc> *v = NULL;
420 :
421 : /* Iterate over the fields, building initializations. */
422 27251878 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
423 : {
424 25790605 : tree ftype, value;
425 :
426 25790605 : if (TREE_CODE (field) != FIELD_DECL)
427 25158548 : continue;
428 :
429 632057 : ftype = TREE_TYPE (field);
430 :
431 632057 : if (ftype == error_mark_node)
432 9 : continue;
433 :
434 : /* Ignore flexible array members for value initialization. */
435 632054 : if (TREE_CODE (ftype) == ARRAY_TYPE
436 10617 : && !COMPLETE_TYPE_P (ftype)
437 6 : && !TYPE_DOMAIN (ftype)
438 6 : && COMPLETE_TYPE_P (TREE_TYPE (ftype))
439 632054 : && (next_aggregate_field (DECL_CHAIN (field))
440 : == NULL_TREE))
441 6 : continue;
442 :
443 : /* Ignore unnamed zero-width bitfields. */
444 635650 : if (DECL_UNNAMED_BIT_FIELD (field)
445 632055 : && 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 632039 : if (ANON_AGGR_TYPE_P (ftype))
453 99 : 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 631940 : value = build_value_init (ftype, complain);
469 631940 : value = maybe_constant_init (value);
470 : }
471 :
472 632039 : if (value == error_mark_node)
473 : return error_mark_node;
474 :
475 632038 : 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 632038 : 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 1461273 : tree ret = build_constructor (type, v);
486 1461273 : CONSTRUCTOR_ZERO_PADDING_BITS (ret) = 1;
487 1461273 : return ret;
488 : }
489 : }
490 4495168 : else if (TREE_CODE (type) == ARRAY_TYPE)
491 : {
492 10649 : vec<constructor_elt, va_gc> *v = NULL;
493 :
494 : /* Iterate over the array elements, building initializations. */
495 10649 : 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 10649 : 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 10649 : 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 10649 : if (!tree_int_cst_equal (max_index, integer_minus_one_node))
511 : {
512 10649 : constructor_elt ce;
513 :
514 : /* If this is a one element array, we just use a regular init. */
515 10649 : if (tree_int_cst_equal (size_zero_node, max_index))
516 193 : ce.index = size_zero_node;
517 : else
518 10456 : ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
519 :
520 10649 : ce.value = build_value_init (TREE_TYPE (type), complain);
521 10649 : ce.value = maybe_constant_init (ce.value);
522 10649 : if (ce.value == error_mark_node)
523 0 : return error_mark_node;
524 :
525 10649 : vec_alloc (v, 1);
526 10649 : 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 10649 : 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 10649 : return build_constructor (type, v);
537 : }
538 4484519 : else if (TREE_CODE (type) == FUNCTION_TYPE)
539 : {
540 9 : if (complain & tf_error)
541 0 : error ("value-initialization of function type %qT", type);
542 9 : return error_mark_node;
543 : }
544 4484510 : else if (TYPE_REF_P (type))
545 : {
546 169 : if (complain & tf_error)
547 19 : error ("value-initialization of reference type %qT", type);
548 169 : return error_mark_node;
549 : }
550 :
551 4496167 : 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 365946 : perform_target_ctor (tree init)
560 : {
561 365946 : tree decl = current_class_ref;
562 365946 : tree type = current_class_type;
563 :
564 365946 : init = build_aggr_init (decl, init, LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
565 : tf_warning_or_error);
566 365946 : finish_expr_stmt (init);
567 365946 : if (type_build_dtor_call (type))
568 : {
569 265059 : 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 265059 : if (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
576 : {
577 80 : 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 80 : expr = build_if_in_charge (expr, base);
584 : }
585 265059 : if (expr != error_mark_node
586 265059 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
587 255337 : finish_eh_cleanup (expr);
588 : }
589 365946 : 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 2373383 : maybe_instantiate_nsdmi_init (tree member, tsubst_flags_t complain)
597 : {
598 2373383 : tree init = DECL_INITIAL (member);
599 :
600 : /* tsubst_decl uses void_node to indicate an uninstantiated DMI. */
601 2373383 : 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 124976 : complain &= tf_warning_or_error;
606 :
607 124976 : init = DECL_INITIAL (DECL_TI_TEMPLATE (member));
608 124976 : location_t expr_loc
609 124976 : = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member));
610 124976 : if (TREE_CODE (init) == DEFERRED_PARSE)
611 : /* Unparsed. */;
612 : /* Check recursive instantiation. */
613 124976 : 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 124967 : cp_evaluated ev;
623 :
624 124967 : location_t sloc = input_location;
625 124967 : input_location = expr_loc;
626 :
627 124967 : DECL_INSTANTIATING_NSDMI_P (member) = 1;
628 :
629 124967 : bool pushed = false;
630 124967 : tree ctx = type_context_for_name_lookup (member);
631 :
632 124967 : bool push_to_top = maybe_push_to_top_level (member);
633 124967 : if (!currently_open_class (ctx))
634 : {
635 124952 : push_nested_class (ctx);
636 124952 : push_deferring_access_checks (dk_no_deferred);
637 124952 : pushed = true;
638 : }
639 :
640 124967 : inject_this_parameter (ctx, TYPE_UNQUALIFIED);
641 :
642 124967 : start_lambda_scope (member);
643 :
644 : /* Do deferred instantiation of the NSDMI. */
645 124967 : init = tsubst_expr (init, DECL_TI_ARGS (member), complain, member);
646 124967 : init = digest_nsdmi_init (member, init, complain);
647 :
648 124967 : finish_lambda_scope ();
649 :
650 124967 : DECL_INSTANTIATING_NSDMI_P (member) = 0;
651 :
652 124967 : if (init != error_mark_node)
653 124908 : DECL_INITIAL (member) = init;
654 :
655 124967 : if (pushed)
656 : {
657 124952 : pop_deferring_access_checks ();
658 124952 : pop_nested_class ();
659 : }
660 124967 : maybe_pop_from_top_level (push_to_top);
661 :
662 124967 : input_location = sloc;
663 124967 : }
664 : }
665 :
666 2373383 : return init;
667 : }
668 :
669 : /* Return the non-static data initializer for FIELD_DECL MEMBER. */
670 :
671 : tree
672 1830055 : get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
673 : {
674 1830055 : tree save_ccp = current_class_ptr;
675 1830055 : tree save_ccr = current_class_ref;
676 :
677 1830055 : tree init = maybe_instantiate_nsdmi_init (member, complain);
678 :
679 1830055 : if (init && TREE_CODE (init) == DEFERRED_PARSE)
680 : {
681 20 : if (complain & tf_error)
682 : {
683 19 : auto_diagnostic_group d;
684 19 : error ("default member initializer for %qD required before the end "
685 : "of its enclosing class", member);
686 19 : inform (location_of (init), "defined here");
687 19 : DECL_INITIAL (member) = error_mark_node;
688 19 : }
689 20 : init = error_mark_node;
690 : }
691 :
692 1830055 : if (in_ctor)
693 : {
694 1393437 : current_class_ptr = save_ccp;
695 1393437 : 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 436618 : current_class_ref = build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (member));
702 436618 : 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 1830055 : 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 1830055 : bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init));
712 7524 : if (simple_target)
713 7524 : init = TARGET_EXPR_INITIAL (init);
714 1830055 : init = break_out_target_exprs (init, /*loc*/true);
715 1830055 : 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 95729 : TARGET_EXPR_DIRECT_INIT_P (init) = in_ctor;
719 1830055 : if (simple_target && TREE_CODE (init) != CONSTRUCTOR)
720 : /* Now put it back so C++17 copy elision works. */
721 1331 : init = get_target_expr (init);
722 :
723 1830055 : set_target_expr_eliding (init);
724 :
725 1830055 : current_class_ptr = save_ccp;
726 1830055 : current_class_ref = save_ccr;
727 1830055 : return init;
728 1830055 : }
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 617458 : maybe_reject_flexarray_init (tree member, tree init)
735 : {
736 617458 : tree type = TREE_TYPE (member);
737 :
738 617458 : if (!init
739 617427 : || TREE_CODE (type) != ARRAY_TYPE
740 646940 : || 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 296 : find_list_begin (tree init)
763 : {
764 299 : STRIP_NOPS (init);
765 598 : while (TREE_CODE (init) == COMPOUND_EXPR)
766 0 : init = TREE_OPERAND (init, 1);
767 299 : STRIP_NOPS (init);
768 299 : 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 296 : if (TREE_CODE (init) == CALL_EXPR)
779 293 : if (tree fn = get_callee_fndecl (init))
780 293 : if (id_equal (DECL_NAME (fn), "begin")
781 293 : && is_std_init_list (DECL_CONTEXT (fn)))
782 21 : 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 4327189 : maybe_warn_list_ctor (tree member, tree init)
791 : {
792 4327189 : tree memtype = TREE_TYPE (member);
793 3891552 : if (!init || !TYPE_PTR_P (memtype)
794 5919090 : || !is_list_ctor (current_function_decl))
795 : return;
796 :
797 306 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl);
798 306 : parm = TREE_VALUE (parm);
799 306 : tree initlist = non_reference (parm);
800 :
801 : /* Do not warn if the parameter is an lvalue reference to non-const. */
802 291 : if (TYPE_REF_P (parm) && !TYPE_REF_IS_RVALUE (parm)
803 325 : && !CP_TYPE_CONST_P (initlist))
804 : return;
805 :
806 293 : tree targs = CLASSTYPE_TI_ARGS (initlist);
807 293 : tree elttype = TREE_VEC_ELT (targs, 0);
808 :
809 293 : if (!same_type_ignoring_top_level_qualifiers_p
810 293 : (TREE_TYPE (memtype), elttype))
811 : return;
812 :
813 293 : tree begin = find_list_begin (init);
814 293 : 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 135357 : find_uninit_fields_r (tree *tp, int *walk_subtrees, void *data)
839 : {
840 135357 : find_uninit_data *d = static_cast<find_uninit_data *>(data);
841 135357 : hash_set<tree> *uninitialized = d->uninitialized;
842 135357 : tree init = *tp;
843 135357 : const tree_code code = TREE_CODE (init);
844 :
845 : /* No need to look into types or unevaluated operands. */
846 135357 : if (TYPE_P (init) || unevaluated_p (code))
847 : {
848 14 : *walk_subtrees = false;
849 14 : return NULL_TREE;
850 : }
851 :
852 135343 : 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 5549 : gcc_fallthrough ();
866 : /* If we're just taking the address of an object, it doesn't matter
867 : whether it's been initialized. */
868 5549 : case ADDR_EXPR:
869 5549 : *walk_subtrees = false;
870 5549 : return NULL_TREE;
871 129794 : default:
872 129794 : break;
873 : }
874 :
875 : /* We'd need data flow info to avoid false positives. */
876 129794 : if (truth_value_p (code))
877 298 : goto give_up;
878 : /* Attempt to handle a simple a{b}, but no more. */
879 129496 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
880 : {
881 1977 : if (CONSTRUCTOR_NELTS (init) == 1
882 1971 : && !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 127519 : else if (code == CALL_EXPR)
889 : {
890 9994 : tree fn = get_callee_fndecl (init);
891 9994 : if (fn && DECL_IOBJ_MEMBER_FUNCTION_P (fn))
892 : {
893 4187 : tree op = CALL_EXPR_ARG (init, 0);
894 4187 : if (TREE_CODE (op) == ADDR_EXPR)
895 1155 : op = TREE_OPERAND (op, 0);
896 4187 : temp_override<tree> ovr (d->member, DECL_ARGUMENTS (fn));
897 4187 : cp_walk_tree_without_duplicates (&op, find_uninit_fields_r, data);
898 4187 : }
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 19981 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
911 7711 : goto give_up;
912 : }
913 :
914 : /* If we find FIELD in the uninitialized set, we warn. */
915 121600 : if (code == COMPONENT_REF)
916 : {
917 3101 : tree field = TREE_OPERAND (init, 1);
918 3101 : tree type = TYPE_P (d->member) ? d->member : TREE_TYPE (d->member);
919 :
920 : /* We're initializing a reference member with itself. */
921 3101 : 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 3098 : else if (cp_tree_equal (TREE_OPERAND (init, 0), current_class_ref)
925 3098 : && 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 8194 : give_up:
942 8194 : *walk_subtrees = false;
943 8194 : uninitialized->empty ();
944 8194 : return integer_zero_node;
945 : }
946 :
947 : /* Wrapper around find_uninit_fields_r above. */
948 :
949 : static void
950 1426635 : find_uninit_fields (tree *t, hash_set<tree> *uninitialized, tree member)
951 : {
952 1426635 : if (!uninitialized->is_empty ())
953 : {
954 49018 : find_uninit_data data = { uninitialized, member };
955 49018 : cp_walk_tree_without_duplicates (t, find_uninit_fields_r, &data);
956 : }
957 1426635 : }
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 717 : can_init_array_with_p (tree type, tree init)
965 : {
966 717 : if (!init)
967 : /* Value-init, OK. */
968 : return true;
969 83 : 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 80 : 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 6936036 : perform_member_init (tree member, tree init, hash_set<tree> &uninitialized)
999 : {
1000 6936036 : tree decl;
1001 6936036 : 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 6936036 : if (init == NULL_TREE)
1006 1393437 : init = get_nsdmi (member, /*ctor*/true, tf_warning_or_error);
1007 :
1008 6936036 : if (init == error_mark_node)
1009 : return;
1010 :
1011 : /* Effective C++ rule 12 requires that all data members be
1012 : initialized. */
1013 6936011 : if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
1014 0 : warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
1015 : "%qD should be initialized in the member initialization list",
1016 : member);
1017 :
1018 : /* Get an lvalue for the data member. */
1019 6936011 : decl = build_class_member_access_expr (current_class_ref, member,
1020 : /*access_path=*/NULL_TREE,
1021 : /*preserve_reference=*/true,
1022 : tf_warning_or_error);
1023 6936011 : if (decl == error_mark_node)
1024 : return;
1025 :
1026 6863290 : if ((warn_init_self || warn_uninitialized || warn_self_move)
1027 73732 : && init
1028 57441 : && TREE_CODE (init) == TREE_LIST
1029 6987892 : && TREE_CHAIN (init) == NULL_TREE)
1030 : {
1031 49257 : tree val = TREE_VALUE (init);
1032 : /* Handle references. */
1033 49257 : if (REFERENCE_REF_P (val))
1034 3548 : val = TREE_OPERAND (val, 0);
1035 2073 : if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
1036 51007 : && TREE_OPERAND (val, 0) == current_class_ref)
1037 27 : warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1038 27 : OPT_Winit_self, "%qD is initialized with itself",
1039 : member);
1040 98460 : else if (!maybe_warn_self_move (input_location, member,
1041 49230 : TREE_VALUE (init)))
1042 49224 : find_uninit_fields (&val, &uninitialized, decl);
1043 : }
1044 :
1045 6936005 : if (array_of_unknown_bound_p (type))
1046 : {
1047 52 : maybe_reject_flexarray_init (member, init);
1048 52 : return;
1049 : }
1050 :
1051 6935953 : if (init && TREE_CODE (init) == TREE_LIST)
1052 : {
1053 : /* A(): a{e} */
1054 5115883 : if (DIRECT_LIST_INIT_P (TREE_VALUE (init)))
1055 494516 : init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
1056 : tf_warning_or_error);
1057 : /* We are trying to initialize an array from a ()-list. If we
1058 : should attempt to do so, conjure up a CONSTRUCTOR. */
1059 4621367 : else if (TREE_CODE (type) == ARRAY_TYPE
1060 : /* P0960 is a C++20 feature. */
1061 188 : && cxx_dialect >= cxx20)
1062 146 : init = do_aggregate_paren_init (init, type);
1063 4621221 : else if (!CLASS_TYPE_P (type))
1064 3330866 : init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
1065 : tf_warning_or_error);
1066 : /* If we're initializing a class from a ()-list, leave the TREE_LIST
1067 : alone: we might call an appropriate constructor, or (in C++20)
1068 : do aggregate-initialization. */
1069 : }
1070 :
1071 : /* Assume we are initializing the member. */
1072 6935953 : bool member_initialized_p = true;
1073 :
1074 6935953 : if (init == void_type_node)
1075 : {
1076 : /* mem() means value-initialization. */
1077 426689 : if (TREE_CODE (type) == ARRAY_TYPE)
1078 : {
1079 229 : init = build_vec_init_expr (type, init, tf_warning_or_error);
1080 229 : init = cp_build_init_expr (decl, init);
1081 229 : finish_expr_stmt (init);
1082 : }
1083 : else
1084 : {
1085 426460 : tree value = build_value_init (type, tf_warning_or_error);
1086 426460 : if (value == error_mark_node)
1087 : return;
1088 426448 : init = cp_build_init_expr (decl, value);
1089 426448 : finish_expr_stmt (init);
1090 : }
1091 : }
1092 : /* Deal with this here, as we will get confused if we try to call the
1093 : assignment op for an anonymous union. This can happen in a
1094 : synthesized copy constructor. */
1095 6509264 : else if (ANON_AGGR_TYPE_P (type))
1096 : {
1097 12 : if (init)
1098 : {
1099 12 : init = cp_build_init_expr (decl, TREE_VALUE (init));
1100 12 : finish_expr_stmt (init);
1101 : }
1102 : }
1103 6509252 : else if (init
1104 6509252 : && (TYPE_REF_P (type)
1105 5650746 : || (TREE_CODE (init) == CONSTRUCTOR
1106 606106 : && (CP_AGGREGATE_TYPE_P (type)
1107 471744 : || is_std_init_list (type)))))
1108 : {
1109 : /* With references and list-initialization, we need to deal with
1110 : extending temporary lifetimes. 12.2p5: "A temporary bound to a
1111 : reference member in a constructor's ctor-initializer (12.6.2)
1112 : persists until the constructor exits." */
1113 189061 : unsigned i; tree t;
1114 189061 : releasing_vec cleanups;
1115 189061 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1116 : {
1117 23066 : if (BRACE_ENCLOSED_INITIALIZER_P (init)
1118 99564 : && CP_AGGREGATE_TYPE_P (type))
1119 23015 : init = reshape_init (type, init, tf_warning_or_error);
1120 76498 : init = digest_init (type, init, tf_warning_or_error);
1121 : }
1122 189061 : if (init == error_mark_node)
1123 : return;
1124 189029 : if (is_empty_field (member)
1125 189029 : && !TREE_SIDE_EFFECTS (init))
1126 : /* Don't add trivial initialization of an empty base/field, as they
1127 : might not be ordered the way the back-end expects. */
1128 : return;
1129 : /* A FIELD_DECL doesn't really have a suitable lifetime, but
1130 : make_temporary_var_for_ref_to_temp will treat it as automatic and
1131 : set_up_extended_ref_temp wants to use the decl in a warning. */
1132 166510 : init = extend_ref_init_temps (member, init, &cleanups);
1133 166510 : if (TREE_CODE (type) == ARRAY_TYPE
1134 166510 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
1135 4 : init = build_vec_init_expr (type, init, tf_warning_or_error);
1136 166510 : init = cp_build_init_expr (decl, init);
1137 166510 : finish_expr_stmt (init);
1138 333110 : FOR_EACH_VEC_ELT (*cleanups, i, t)
1139 90 : push_cleanup (NULL_TREE, t, false);
1140 22551 : }
1141 6320191 : else if (type_build_ctor_call (type)
1142 6320191 : || (init && CLASS_TYPE_P (strip_array_types (type))))
1143 : {
1144 1993002 : if (TREE_CODE (type) == ARRAY_TYPE)
1145 : {
1146 717 : if (can_init_array_with_p (type, init))
1147 : {
1148 714 : if (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1149 : {
1150 : /* Initialize the array only if it's not a flexible
1151 : array member (i.e., if it has an upper bound). */
1152 714 : init = build_vec_init_expr (type, init, tf_warning_or_error);
1153 714 : init = cp_build_init_expr (decl, init);
1154 714 : finish_expr_stmt (init);
1155 : }
1156 : }
1157 : else
1158 3 : error ("invalid initializer for array member %q#D", member);
1159 : }
1160 : else
1161 : {
1162 1992285 : int flags = LOOKUP_NORMAL;
1163 1992285 : if (DECL_DEFAULTED_FN (current_function_decl))
1164 322992 : flags |= LOOKUP_DEFAULTED;
1165 1992285 : if (CP_TYPE_CONST_P (type)
1166 1177 : && init == NULL_TREE
1167 1992332 : && default_init_uninitialized_part (type))
1168 : {
1169 : /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
1170 : vtable; still give this diagnostic. */
1171 3 : auto_diagnostic_group d;
1172 3 : if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1173 : "uninitialized const member in %q#T", type))
1174 3 : inform (DECL_SOURCE_LOCATION (member),
1175 : "%q#D should be initialized", member );
1176 3 : }
1177 1992285 : finish_expr_stmt (build_aggr_init (decl, init, flags,
1178 : tf_warning_or_error));
1179 : }
1180 : }
1181 : else
1182 : {
1183 4327189 : if (init == NULL_TREE)
1184 : {
1185 435637 : tree core_type;
1186 : /* member traversal: note it leaves init NULL */
1187 435637 : if (TYPE_REF_P (type))
1188 : {
1189 7 : auto_diagnostic_group d;
1190 7 : if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1191 : "uninitialized reference member in %q#T", type))
1192 7 : inform (DECL_SOURCE_LOCATION (member),
1193 : "%q#D should be initialized", member);
1194 7 : }
1195 435630 : else if (CP_TYPE_CONST_P (type))
1196 : {
1197 13 : auto_diagnostic_group d;
1198 13 : if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
1199 : "uninitialized const member in %q#T", type))
1200 13 : inform (DECL_SOURCE_LOCATION (member),
1201 : "%q#D should be initialized", member );
1202 13 : }
1203 :
1204 435637 : core_type = strip_array_types (type);
1205 :
1206 38523 : if (CLASS_TYPE_P (core_type)
1207 474151 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
1208 38512 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
1209 3 : diagnose_uninitialized_cst_or_ref_member (core_type,
1210 : /*using_new=*/false,
1211 : /*complain=*/true);
1212 :
1213 : /* We left the member uninitialized. */
1214 : member_initialized_p = false;
1215 : }
1216 :
1217 4327189 : maybe_warn_list_ctor (member, init);
1218 :
1219 4327189 : if (init)
1220 3891552 : finish_expr_stmt (cp_build_modify_expr (input_location, decl,
1221 : INIT_EXPR, init,
1222 : tf_warning_or_error));
1223 : }
1224 :
1225 6913390 : if (member_initialized_p && warn_uninitialized)
1226 : /* This member is now initialized, remove it from the uninitialized
1227 : set. */
1228 60954 : uninitialized.remove (member);
1229 :
1230 6913390 : if (type_build_dtor_call (type))
1231 : {
1232 1188134 : tree expr;
1233 :
1234 1188134 : expr = build_class_member_access_expr (current_class_ref, member,
1235 : /*access_path=*/NULL_TREE,
1236 : /*preserve_reference=*/false,
1237 : tf_warning_or_error);
1238 1188134 : expr = build_delete (input_location,
1239 : type, expr, sfk_complete_destructor,
1240 : LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
1241 : tf_warning_or_error);
1242 :
1243 1188134 : if (expr != error_mark_node
1244 1188134 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1245 1023769 : finish_eh_cleanup (expr);
1246 : }
1247 : }
1248 :
1249 : /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
1250 : the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
1251 :
1252 : static tree
1253 6542362 : build_field_list (tree t, tree list, int *uses_unions_or_anon_p)
1254 : {
1255 6542362 : tree fields;
1256 :
1257 : /* Note whether or not T is a union. */
1258 6542362 : if (TREE_CODE (t) == UNION_TYPE)
1259 342596 : *uses_unions_or_anon_p = 1;
1260 :
1261 277499241 : for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
1262 : {
1263 270956879 : tree fieldtype;
1264 :
1265 : /* Skip CONST_DECLs for enumeration constants and so forth. */
1266 270956879 : if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
1267 262869153 : continue;
1268 :
1269 8087726 : fieldtype = TREE_TYPE (fields);
1270 :
1271 : /* For an anonymous struct or union, we must recursively
1272 : consider the fields of the anonymous type. They can be
1273 : directly initialized from the constructor. */
1274 8087726 : if (ANON_AGGR_TYPE_P (fieldtype))
1275 : {
1276 : /* Add this field itself. Synthesized copy constructors
1277 : initialize the entire aggregate. */
1278 377132 : list = tree_cons (fields, NULL_TREE, list);
1279 : /* And now add the fields in the anonymous aggregate. */
1280 377132 : list = build_field_list (fieldtype, list, uses_unions_or_anon_p);
1281 377132 : *uses_unions_or_anon_p = 1;
1282 : }
1283 : /* Add this field. */
1284 7710594 : else if (DECL_NAME (fields))
1285 7710552 : list = tree_cons (fields, NULL_TREE, list);
1286 : }
1287 :
1288 6542362 : return list;
1289 : }
1290 :
1291 : /* Return the innermost aggregate scope for FIELD, whether that is
1292 : the enclosing class or an anonymous aggregate within it. */
1293 :
1294 : static tree
1295 1417607 : innermost_aggr_scope (tree field)
1296 : {
1297 1417607 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1298 21 : return TREE_TYPE (field);
1299 : else
1300 1417586 : return DECL_CONTEXT (field);
1301 : }
1302 :
1303 : /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
1304 : a FIELD_DECL or BINFO in T that needs initialization. The
1305 : TREE_VALUE gives the initializer, or list of initializer arguments.
1306 :
1307 : Return a TREE_LIST containing all of the initializations required
1308 : for T, in the order in which they should be performed. The output
1309 : list has the same format as the input. */
1310 :
1311 : static tree
1312 6165230 : sort_mem_initializers (tree t, tree mem_inits)
1313 : {
1314 6165230 : tree init;
1315 6165230 : tree base, binfo, base_binfo;
1316 6165230 : tree sorted_inits;
1317 6165230 : tree next_subobject;
1318 6165230 : vec<tree, va_gc> *vbases;
1319 6165230 : int i;
1320 6165230 : int uses_unions_or_anon_p = 0;
1321 :
1322 : /* Build up a list of initializations. The TREE_PURPOSE of entry
1323 : will be the subobject (a FIELD_DECL or BINFO) to initialize. The
1324 : TREE_VALUE will be the constructor arguments, or NULL if no
1325 : explicit initialization was provided. */
1326 6165230 : sorted_inits = NULL_TREE;
1327 :
1328 : /* Process the virtual bases. */
1329 6165230 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
1330 6188429 : vec_safe_iterate (vbases, i, &base); i++)
1331 23199 : sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
1332 :
1333 : /* Process the direct bases. */
1334 8602812 : for (binfo = TYPE_BINFO (t), i = 0;
1335 8602812 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1336 2437582 : if (!BINFO_VIRTUAL_P (base_binfo))
1337 2430209 : sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
1338 :
1339 : /* Process the non-static data members. */
1340 6165230 : sorted_inits = build_field_list (t, sorted_inits, &uses_unions_or_anon_p);
1341 : /* Reverse the entire list of initializations, so that they are in
1342 : the order that they will actually be performed. */
1343 6165230 : sorted_inits = nreverse (sorted_inits);
1344 :
1345 : /* If the user presented the initializers in an order different from
1346 : that in which they will actually occur, we issue a warning. Keep
1347 : track of the next subobject which can be explicitly initialized
1348 : without issuing a warning. */
1349 6165230 : next_subobject = sorted_inits;
1350 :
1351 : /* Go through the explicit initializers, filling in TREE_PURPOSE in
1352 : the SORTED_INITS. */
1353 13153093 : for (init = mem_inits; init; init = TREE_CHAIN (init))
1354 : {
1355 6987863 : tree subobject;
1356 6987863 : tree subobject_init;
1357 :
1358 6987863 : subobject = TREE_PURPOSE (init);
1359 :
1360 : /* If the explicit initializers are in sorted order, then
1361 : SUBOBJECT will be NEXT_SUBOBJECT, or something following
1362 : it. */
1363 6987863 : for (subobject_init = next_subobject;
1364 9766359 : subobject_init;
1365 2778496 : subobject_init = TREE_CHAIN (subobject_init))
1366 9766179 : if (TREE_PURPOSE (subobject_init) == subobject)
1367 : break;
1368 :
1369 : /* Issue a warning if the explicit initializer order does not
1370 : match that which will actually occur.
1371 : ??? Are all these on the correct lines? */
1372 6987863 : if (warn_reorder && !subobject_init)
1373 : {
1374 12 : if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
1375 6 : warning_at (DECL_SOURCE_LOCATION (TREE_PURPOSE (next_subobject)),
1376 6 : OPT_Wreorder, "%qD will be initialized after",
1377 6 : TREE_PURPOSE (next_subobject));
1378 : else
1379 6 : warning (OPT_Wreorder, "base %qT will be initialized after",
1380 6 : TREE_PURPOSE (next_subobject));
1381 12 : if (TREE_CODE (subobject) == FIELD_DECL)
1382 3 : warning_at (DECL_SOURCE_LOCATION (subobject),
1383 3 : OPT_Wreorder, " %q#D", subobject);
1384 : else
1385 9 : warning (OPT_Wreorder, " base %qT", subobject);
1386 12 : warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1387 12 : OPT_Wreorder, " when initialized here");
1388 : }
1389 :
1390 : /* Look again, from the beginning of the list. */
1391 6923211 : if (!subobject_init)
1392 : {
1393 : subobject_init = sorted_inits;
1394 243 : while (TREE_PURPOSE (subobject_init) != subobject)
1395 63 : subobject_init = TREE_CHAIN (subobject_init);
1396 : }
1397 :
1398 : /* It is invalid to initialize the same subobject more than
1399 : once. */
1400 6987863 : if (TREE_VALUE (subobject_init))
1401 : {
1402 0 : if (TREE_CODE (subobject) == FIELD_DECL)
1403 0 : error_at (DECL_SOURCE_LOCATION (current_function_decl),
1404 : "multiple initializations given for %qD",
1405 : subobject);
1406 : else
1407 0 : error_at (DECL_SOURCE_LOCATION (current_function_decl),
1408 : "multiple initializations given for base %qT",
1409 : subobject);
1410 : }
1411 :
1412 : /* Record the initialization. */
1413 6987863 : TREE_VALUE (subobject_init) = TREE_VALUE (init);
1414 : /* Carry over the dummy TREE_TYPE node containing the source location. */
1415 6987863 : TREE_TYPE (subobject_init) = TREE_TYPE (init);
1416 6987863 : next_subobject = subobject_init;
1417 : }
1418 :
1419 : /* [class.base.init]
1420 :
1421 : If a ctor-initializer specifies more than one mem-initializer for
1422 : multiple members of the same union (including members of
1423 : anonymous unions), the ctor-initializer is ill-formed.
1424 :
1425 : Here we also splice out uninitialized union members. */
1426 6165230 : if (uses_unions_or_anon_p)
1427 : {
1428 : tree *last_p = NULL;
1429 : tree *p;
1430 2128079 : for (p = &sorted_inits; *p; )
1431 : {
1432 1785537 : tree field;
1433 1785537 : tree ctx;
1434 :
1435 1785537 : init = *p;
1436 :
1437 1785537 : field = TREE_PURPOSE (init);
1438 :
1439 : /* Skip base classes. */
1440 1785537 : if (TREE_CODE (field) != FIELD_DECL)
1441 584 : goto next;
1442 :
1443 : /* If this is an anonymous aggregate with no explicit initializer,
1444 : splice it out. */
1445 1784953 : if (!TREE_VALUE (init) && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1446 377120 : goto splice;
1447 :
1448 : /* See if this field is a member of a union, or a member of a
1449 : structure contained in a union, etc. */
1450 1407833 : ctx = innermost_aggr_scope (field);
1451 :
1452 : /* If this field is not a member of a union, skip it. */
1453 1407833 : if (TREE_CODE (ctx) != UNION_TYPE
1454 1407833 : && !ANON_AGGR_TYPE_P (ctx))
1455 578795 : goto next;
1456 :
1457 : /* If this union member has no explicit initializer and no NSDMI,
1458 : splice it out. */
1459 829038 : if (TREE_VALUE (init) || DECL_INITIAL (field))
1460 : /* OK. */;
1461 : else
1462 764862 : goto splice;
1463 :
1464 : /* It's only an error if we have two initializers for the same
1465 : union type. */
1466 64176 : if (!last_p)
1467 : {
1468 54402 : last_p = p;
1469 54402 : goto next;
1470 : }
1471 :
1472 : /* See if LAST_FIELD and the field initialized by INIT are
1473 : members of the same union (or the union itself). If so, there's
1474 : a problem, unless they're actually members of the same structure
1475 : which is itself a member of a union. For example, given:
1476 :
1477 : union { struct { int i; int j; }; };
1478 :
1479 : initializing both `i' and `j' makes sense. */
1480 9774 : ctx = common_enclosing_class
1481 9774 : (innermost_aggr_scope (field),
1482 9774 : innermost_aggr_scope (TREE_PURPOSE (*last_p)));
1483 :
1484 9774 : if (ctx && (TREE_CODE (ctx) == UNION_TYPE
1485 111 : || ctx == TREE_TYPE (TREE_PURPOSE (*last_p))))
1486 : {
1487 : /* A mem-initializer hides an NSDMI. */
1488 9666 : if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
1489 9639 : *last_p = TREE_CHAIN (*last_p);
1490 27 : else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
1491 9 : goto splice;
1492 : else
1493 : {
1494 18 : error_at (DECL_SOURCE_LOCATION (current_function_decl),
1495 : "initializations for multiple members of %qT",
1496 : ctx);
1497 18 : goto splice;
1498 : }
1499 : }
1500 :
1501 : last_p = p;
1502 :
1503 643528 : next:
1504 643528 : p = &TREE_CHAIN (*p);
1505 643528 : continue;
1506 1142009 : splice:
1507 1142009 : *p = TREE_CHAIN (*p);
1508 643528 : }
1509 : }
1510 :
1511 6165230 : return sorted_inits;
1512 : }
1513 :
1514 : /* Callback for cp_walk_tree to mark all PARM_DECLs in a tree as read. */
1515 :
1516 : static tree
1517 258 : mark_exp_read_r (tree *tp, int *, void *)
1518 : {
1519 258 : tree t = *tp;
1520 258 : if (TREE_CODE (t) == PARM_DECL)
1521 45 : mark_exp_read (t);
1522 258 : return NULL_TREE;
1523 : }
1524 :
1525 : /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1526 : is a TREE_LIST giving the explicit mem-initializer-list for the
1527 : constructor. The TREE_PURPOSE of each entry is a subobject (a
1528 : FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1529 : is a TREE_LIST giving the arguments to the constructor or
1530 : void_type_node for an empty list of arguments. */
1531 :
1532 : void
1533 6531176 : emit_mem_initializers (tree mem_inits)
1534 : {
1535 6531176 : int flags = LOOKUP_NORMAL;
1536 :
1537 : /* We will already have issued an error message about the fact that
1538 : the type is incomplete. */
1539 6531176 : if (!COMPLETE_TYPE_P (current_class_type))
1540 365946 : return;
1541 :
1542 : /* Keep a set holding fields that are not initialized. */
1543 6531176 : hash_set<tree> uninitialized;
1544 :
1545 : /* Initially that is all of them. */
1546 6531176 : if (warn_uninitialized)
1547 54306 : for (tree f = next_aggregate_field (TYPE_FIELDS (current_class_type));
1548 145926 : f != NULL_TREE;
1549 91620 : f = next_aggregate_field (DECL_CHAIN (f)))
1550 91620 : if (!DECL_ARTIFICIAL (f)
1551 91620 : && !is_really_empty_class (TREE_TYPE (f), /*ignore_vptr*/false))
1552 77573 : uninitialized.add (f);
1553 :
1554 6531176 : if (mem_inits
1555 5307675 : && TYPE_P (TREE_PURPOSE (mem_inits))
1556 6897122 : && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1557 : {
1558 : /* Delegating constructor. */
1559 365946 : gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
1560 365946 : tree ctor = perform_target_ctor (TREE_VALUE (mem_inits));
1561 365946 : find_uninit_fields (&ctor, &uninitialized, current_class_type);
1562 365946 : return;
1563 : }
1564 :
1565 6165230 : if (DECL_DEFAULTED_FN (current_function_decl)
1566 6834576 : && ! DECL_INHERITED_CTOR (current_function_decl))
1567 : flags |= LOOKUP_DEFAULTED;
1568 :
1569 : /* Sort the mem-initializers into the order in which the
1570 : initializations should be performed. */
1571 6165230 : mem_inits = sort_mem_initializers (current_class_type, mem_inits);
1572 :
1573 6165230 : in_base_initializer = 1;
1574 :
1575 : /* Initialize base classes. */
1576 6165230 : for (; (mem_inits
1577 15448947 : && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
1578 2453408 : mem_inits = TREE_CHAIN (mem_inits))
1579 : {
1580 2453408 : tree subobject = TREE_PURPOSE (mem_inits);
1581 2453408 : tree arguments = TREE_VALUE (mem_inits);
1582 :
1583 : /* We already have issued an error message. */
1584 2453408 : if (arguments == error_mark_node)
1585 3 : continue;
1586 :
1587 : /* Suppress access control when calling the inherited ctor. */
1588 4906810 : bool inherited_base = (DECL_INHERITED_CTOR (current_function_decl)
1589 54556 : && flag_new_inheriting_ctors
1590 2507952 : && arguments);
1591 54457 : if (inherited_base)
1592 54457 : push_deferring_access_checks (dk_deferred);
1593 :
1594 2453405 : if (arguments == NULL_TREE)
1595 : {
1596 : /* If these initializations are taking place in a copy constructor,
1597 : the base class should probably be explicitly initialized if there
1598 : is a user-defined constructor in the base class (other than the
1599 : default constructor, which will be called anyway). */
1600 1008159 : if (extra_warnings
1601 13230 : && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1602 1008214 : && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1603 6 : warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1604 3 : OPT_Wextra, "base class %q#T should be explicitly "
1605 : "initialized in the copy constructor",
1606 3 : BINFO_TYPE (subobject));
1607 : }
1608 :
1609 : /* Initialize the base. */
1610 2453405 : if (!BINFO_VIRTUAL_P (subobject))
1611 : {
1612 2430206 : tree base_addr;
1613 :
1614 2430206 : base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1615 : subobject, 1, tf_warning_or_error);
1616 2430206 : expand_aggr_init_1 (subobject, NULL_TREE,
1617 : cp_build_fold_indirect_ref (base_addr),
1618 : arguments,
1619 : flags,
1620 : tf_warning_or_error);
1621 2430206 : expand_cleanup_for_base (subobject, NULL_TREE);
1622 2430206 : if (STATEMENT_LIST_TAIL (cur_stmt_list))
1623 2022930 : find_uninit_fields (&STATEMENT_LIST_TAIL (cur_stmt_list)->stmt,
1624 1011465 : &uninitialized, BINFO_TYPE (subobject));
1625 : }
1626 23199 : else if (!ABSTRACT_CLASS_TYPE_P (current_class_type))
1627 : /* C++14 DR1658 Means we do not have to construct vbases of
1628 : abstract classes. */
1629 23010 : construct_virtual_base (subobject, arguments);
1630 : else
1631 : /* When not constructing vbases of abstract classes, at least mark
1632 : the arguments expressions as read to avoid
1633 : -Wunused-but-set-parameter false positives. */
1634 189 : cp_walk_tree (&arguments, mark_exp_read_r, NULL, NULL);
1635 :
1636 2453405 : if (inherited_base)
1637 54457 : pop_deferring_access_checks ();
1638 : }
1639 6165230 : in_base_initializer = 0;
1640 :
1641 : /* Initialize the vptrs. */
1642 6165230 : initialize_vtbl_ptrs (current_class_ptr);
1643 :
1644 : /* Initialize the data members. */
1645 19266496 : while (mem_inits)
1646 : {
1647 : /* If this initializer was explicitly provided, then the dummy TREE_TYPE
1648 : node contains the source location. */
1649 6936036 : iloc_sentinel ils (EXPR_LOCATION (TREE_TYPE (mem_inits)));
1650 :
1651 6936036 : perform_member_init (TREE_PURPOSE (mem_inits),
1652 6936036 : TREE_VALUE (mem_inits),
1653 : uninitialized);
1654 :
1655 6936036 : mem_inits = TREE_CHAIN (mem_inits);
1656 6936036 : }
1657 6531176 : }
1658 :
1659 : /* Returns the address of the vtable (i.e., the value that should be
1660 : assigned to the vptr) for BINFO. */
1661 :
1662 : tree
1663 1192253 : build_vtbl_address (tree binfo)
1664 : {
1665 1192253 : tree binfo_for = binfo;
1666 1192253 : tree vtbl;
1667 :
1668 1192253 : if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1669 : /* If this is a virtual primary base, then the vtable we want to store
1670 : is that for the base this is being used as the primary base of. We
1671 : can't simply skip the initialization, because we may be expanding the
1672 : inits of a subobject constructor where the virtual base layout
1673 : can be different. */
1674 54392 : while (BINFO_PRIMARY_P (binfo_for))
1675 14488 : binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1676 :
1677 : /* Figure out what vtable BINFO's vtable is based on, and mark it as
1678 : used. */
1679 1192253 : vtbl = get_vtbl_decl_for_binfo (binfo_for);
1680 1192253 : TREE_USED (vtbl) = true;
1681 :
1682 : /* Now compute the address to use when initializing the vptr. */
1683 1192253 : vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1684 1192253 : if (VAR_P (vtbl))
1685 0 : vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1686 :
1687 1192253 : return vtbl;
1688 : }
1689 :
1690 : /* This code sets up the virtual function tables appropriate for
1691 : the pointer DECL. It is a one-ply initialization.
1692 :
1693 : BINFO is the exact type that DECL is supposed to be. In
1694 : multiple inheritance, this might mean "C's A" if C : A, B. */
1695 :
1696 : static void
1697 1192250 : expand_virtual_init (tree binfo, tree decl)
1698 : {
1699 1192250 : tree vtbl, vtbl_ptr;
1700 1192250 : tree vtt_index;
1701 :
1702 : /* Compute the initializer for vptr. */
1703 1192250 : vtbl = build_vtbl_address (binfo);
1704 :
1705 : /* We may get this vptr from a VTT, if this is a subobject
1706 : constructor or subobject destructor. */
1707 1192250 : vtt_index = BINFO_VPTR_INDEX (binfo);
1708 1192250 : if (vtt_index)
1709 : {
1710 60377 : tree vtbl2;
1711 60377 : tree vtt_parm;
1712 :
1713 : /* Compute the value to use, when there's a VTT. */
1714 60377 : vtt_parm = current_vtt_parm;
1715 60377 : vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1716 60377 : vtbl2 = cp_build_fold_indirect_ref (vtbl2);
1717 60377 : vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1718 :
1719 : /* The actual initializer is the VTT value only in the subobject
1720 : constructor. In maybe_clone_body we'll substitute NULL for
1721 : the vtt_parm in the case of the non-subobject constructor. */
1722 60377 : vtbl = build_if_in_charge (vtbl, vtbl2);
1723 : }
1724 :
1725 : /* Compute the location of the vtpr. */
1726 1192250 : vtbl_ptr = build_vfield_ref (cp_build_fold_indirect_ref (decl),
1727 1192250 : TREE_TYPE (binfo));
1728 1192250 : gcc_assert (vtbl_ptr != error_mark_node);
1729 :
1730 : /* Assign the vtable to the vptr. */
1731 1192250 : vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
1732 1192250 : finish_expr_stmt (cp_build_modify_expr (input_location, vtbl_ptr, NOP_EXPR,
1733 : vtbl, tf_warning_or_error));
1734 1192250 : }
1735 :
1736 : /* If an exception is thrown in a constructor, those base classes already
1737 : constructed must be destroyed. This function creates the cleanup
1738 : for BINFO, which has just been constructed. If FLAG is non-NULL,
1739 : it is a DECL which is nonzero when this base needs to be
1740 : destroyed. */
1741 :
1742 : static void
1743 2453216 : expand_cleanup_for_base (tree binfo, tree flag)
1744 : {
1745 2453216 : tree expr;
1746 :
1747 2453216 : if (!type_build_dtor_call (BINFO_TYPE (binfo)))
1748 : return;
1749 :
1750 : /* Call the destructor. */
1751 1156176 : expr = build_special_member_call (current_class_ref,
1752 : base_dtor_identifier,
1753 : NULL,
1754 : binfo,
1755 : LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1756 : tf_warning_or_error);
1757 :
1758 1156176 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1759 : return;
1760 :
1761 1010745 : if (flag)
1762 19439 : expr = fold_build3_loc (input_location,
1763 : COND_EXPR, void_type_node,
1764 : c_common_truthvalue_conversion (input_location, flag),
1765 : expr, integer_zero_node);
1766 :
1767 1010745 : finish_eh_cleanup (expr);
1768 : }
1769 :
1770 : /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1771 : constructor. */
1772 :
1773 : static void
1774 23010 : construct_virtual_base (tree vbase, tree arguments)
1775 : {
1776 23010 : tree inner_if_stmt;
1777 23010 : tree exp;
1778 23010 : tree flag;
1779 :
1780 : /* If there are virtual base classes with destructors, we need to
1781 : emit cleanups to destroy them if an exception is thrown during
1782 : the construction process. These exception regions (i.e., the
1783 : period during which the cleanups must occur) begin from the time
1784 : the construction is complete to the end of the function. If we
1785 : create a conditional block in which to initialize the
1786 : base-classes, then the cleanup region for the virtual base begins
1787 : inside a block, and ends outside of that block. This situation
1788 : confuses the sjlj exception-handling code. Therefore, we do not
1789 : create a single conditional block, but one for each
1790 : initialization. (That way the cleanup regions always begin
1791 : in the outer block.) We trust the back end to figure out
1792 : that the FLAG will not change across initializations, and
1793 : avoid doing multiple tests. */
1794 23010 : flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1795 23010 : inner_if_stmt = begin_if_stmt ();
1796 23010 : finish_if_stmt_cond (flag, inner_if_stmt);
1797 :
1798 : /* Compute the location of the virtual base. If we're
1799 : constructing virtual bases, then we must be the most derived
1800 : class. Therefore, we don't have to look up the virtual base;
1801 : we already know where it is. */
1802 23010 : exp = convert_to_base_statically (current_class_ref, vbase);
1803 :
1804 23010 : expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1805 : 0, tf_warning_or_error);
1806 23010 : finish_then_clause (inner_if_stmt);
1807 23010 : finish_if_stmt (inner_if_stmt);
1808 :
1809 23010 : expand_cleanup_for_base (vbase, flag);
1810 23010 : }
1811 :
1812 : /* Find the context in which this FIELD can be initialized. */
1813 :
1814 : static tree
1815 20802836 : initializing_context (tree field)
1816 : {
1817 20802836 : tree t = DECL_CONTEXT (field);
1818 :
1819 : /* Anonymous union members can be initialized in the first enclosing
1820 : non-anonymous union context. */
1821 20963850 : while (t && ANON_AGGR_TYPE_P (t))
1822 161014 : t = TYPE_CONTEXT (t);
1823 20802836 : return t;
1824 : }
1825 :
1826 : /* Function to give error message if member initialization specification
1827 : is erroneous. FIELD is the member we decided to initialize.
1828 : TYPE is the type for which the initialization is being performed.
1829 : FIELD must be a member of TYPE.
1830 :
1831 : MEMBER_NAME is the name of the member. */
1832 :
1833 : static int
1834 20802866 : member_init_ok_or_else (tree field, tree type, tree member_name)
1835 : {
1836 20802866 : if (field == error_mark_node)
1837 : return 0;
1838 20802851 : if (!field)
1839 : {
1840 12 : error ("class %qT does not have any field named %qD", type,
1841 : member_name);
1842 12 : return 0;
1843 : }
1844 20802839 : if (VAR_P (field))
1845 : {
1846 0 : error ("%q#D is a static data member; it can only be "
1847 : "initialized at its definition",
1848 : field);
1849 0 : return 0;
1850 : }
1851 20802839 : if (TREE_CODE (field) != FIELD_DECL)
1852 : {
1853 3 : error ("%q#D is not a non-static data member of %qT",
1854 : field, type);
1855 3 : return 0;
1856 : }
1857 20802836 : if (initializing_context (field) != type)
1858 : {
1859 3 : error ("class %qT does not have any field named %qD", type,
1860 : member_name);
1861 3 : return 0;
1862 : }
1863 :
1864 : return 1;
1865 : }
1866 :
1867 : /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1868 : is a _TYPE node or TYPE_DECL which names a base for that type.
1869 : Check the validity of NAME, and return either the base _TYPE, base
1870 : binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1871 : NULL_TREE and issue a diagnostic.
1872 :
1873 : An old style unnamed direct single base construction is permitted,
1874 : where NAME is NULL. */
1875 :
1876 : tree
1877 28118864 : expand_member_init (tree name)
1878 : {
1879 28118864 : tree basetype;
1880 28118864 : tree field;
1881 :
1882 28118864 : if (!current_class_ref)
1883 : return NULL_TREE;
1884 :
1885 28118859 : if (!name)
1886 : {
1887 : /* This is an obsolete unnamed base class initializer. The
1888 : parser will already have warned about its use. */
1889 6 : switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1890 : {
1891 0 : case 0:
1892 0 : error ("unnamed initializer for %qT, which has no base classes",
1893 : current_class_type);
1894 0 : return NULL_TREE;
1895 6 : case 1:
1896 6 : basetype = BINFO_TYPE
1897 : (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1898 6 : break;
1899 0 : default:
1900 0 : error ("unnamed initializer for %qT, which uses multiple inheritance",
1901 : current_class_type);
1902 0 : return NULL_TREE;
1903 : }
1904 : }
1905 28118853 : else if (TYPE_P (name))
1906 : {
1907 660435 : basetype = TYPE_MAIN_VARIANT (name);
1908 660435 : name = TYPE_NAME (name);
1909 : }
1910 27458418 : else if (TREE_CODE (name) == TYPE_DECL)
1911 6655552 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1912 : else
1913 : basetype = NULL_TREE;
1914 :
1915 7315993 : if (basetype)
1916 : {
1917 7315993 : tree class_binfo;
1918 7315993 : tree direct_binfo;
1919 7315993 : tree virtual_binfo;
1920 7315993 : int i;
1921 :
1922 7315993 : if (current_template_parms
1923 7315993 : || same_type_p (basetype, current_class_type))
1924 : return basetype;
1925 :
1926 1278344 : class_binfo = TYPE_BINFO (current_class_type);
1927 1278344 : direct_binfo = NULL_TREE;
1928 1278344 : virtual_binfo = NULL_TREE;
1929 :
1930 : /* Look for a direct base. */
1931 1370615 : for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1932 1370554 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1933 : break;
1934 :
1935 : /* Look for a virtual base -- unless the direct base is itself
1936 : virtual. */
1937 1278344 : if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1938 1278114 : virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1939 :
1940 : /* [class.base.init]
1941 :
1942 : If a mem-initializer-id is ambiguous because it designates
1943 : both a direct non-virtual base class and an inherited virtual
1944 : base class, the mem-initializer is ill-formed. */
1945 1278344 : if (direct_binfo && virtual_binfo)
1946 : {
1947 0 : error ("%qD is both a direct base and an indirect virtual base",
1948 : basetype);
1949 0 : return NULL_TREE;
1950 : }
1951 :
1952 61 : if (!direct_binfo && !virtual_binfo)
1953 : {
1954 9 : if (CLASSTYPE_VBASECLASSES (current_class_type))
1955 0 : error ("type %qT is not a direct or virtual base of %qT",
1956 : basetype, current_class_type);
1957 : else
1958 9 : error ("type %qT is not a direct base of %qT",
1959 : basetype, current_class_type);
1960 : return NULL_TREE;
1961 : }
1962 :
1963 : return direct_binfo ? direct_binfo : virtual_binfo;
1964 : }
1965 : else
1966 : {
1967 20802866 : if (identifier_p (name))
1968 17670721 : field = lookup_field (current_class_type, name, 1, false);
1969 : else
1970 : field = name;
1971 :
1972 20802866 : if (member_init_ok_or_else (field, current_class_type, name))
1973 20802833 : return field;
1974 : }
1975 :
1976 : return NULL_TREE;
1977 : }
1978 :
1979 : /* This is like `expand_member_init', only it stores one aggregate
1980 : value into another.
1981 :
1982 : INIT comes in two flavors: it is either a value which
1983 : is to be stored in EXP, or it is a parameter list
1984 : to go to a constructor, which will operate on EXP.
1985 : If INIT is not a parameter list for a constructor, then set
1986 : LOOKUP_ONLYCONVERTING.
1987 : If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1988 : the initializer, if FLAGS is 0, then it is the (init) form.
1989 : If `init' is a CONSTRUCTOR, then we emit a warning message,
1990 : explaining that such initializations are invalid.
1991 :
1992 : If INIT resolves to a CALL_EXPR which happens to return
1993 : something of the type we are looking for, then we know
1994 : that we can safely use that call to perform the
1995 : initialization.
1996 :
1997 : The virtual function table pointer cannot be set up here, because
1998 : we do not really know its type.
1999 :
2000 : This never calls operator=().
2001 :
2002 : When initializing, nothing is CONST.
2003 :
2004 : A default copy constructor may have to be used to perform the
2005 : initialization.
2006 :
2007 : A constructor or a conversion operator may have to be used to
2008 : perform the initialization, but not both, as it would be ambiguous. */
2009 :
2010 : tree
2011 5652956 : build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
2012 : {
2013 5652956 : tree stmt_expr;
2014 5652956 : tree compound_stmt;
2015 5652956 : int destroy_temps;
2016 5652956 : tree type = TREE_TYPE (exp);
2017 5652956 : int was_const = TREE_READONLY (exp);
2018 5652956 : int was_volatile = TREE_THIS_VOLATILE (exp);
2019 5652956 : int is_global;
2020 :
2021 5652956 : if (init == error_mark_node)
2022 : return error_mark_node;
2023 :
2024 5652794 : location_t init_loc = (init
2025 5652794 : ? cp_expr_loc_or_input_loc (init)
2026 980648 : : location_of (exp));
2027 :
2028 5652794 : TREE_READONLY (exp) = 0;
2029 5652794 : TREE_THIS_VOLATILE (exp) = 0;
2030 :
2031 5652794 : if (TREE_CODE (type) == ARRAY_TYPE)
2032 : {
2033 11253 : tree itype = init ? TREE_TYPE (init) : NULL_TREE;
2034 11253 : int from_array = 0;
2035 :
2036 11253 : if (DECL_DECOMPOSITION_P (exp))
2037 : {
2038 10448 : from_array = 1;
2039 10448 : init = mark_rvalue_use (init);
2040 10448 : if (init
2041 10448 : && DECL_P (tree_strip_any_location_wrapper (init))
2042 20856 : && !(flags & LOOKUP_ONLYCONVERTING))
2043 : {
2044 : /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init
2045 : recognizes it as direct-initialization. */
2046 30 : init = build_constructor_single (init_list_type_node,
2047 : NULL_TREE, init);
2048 30 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
2049 : }
2050 : }
2051 : else
2052 : {
2053 : /* Must arrange to initialize each element of EXP
2054 : from elements of INIT. */
2055 805 : if (cv_qualified_p (type))
2056 87 : TREE_TYPE (exp) = cv_unqualified (type);
2057 805 : if (itype && cv_qualified_p (itype))
2058 61 : TREE_TYPE (init) = cv_unqualified (itype);
2059 805 : from_array = (itype && same_type_p (TREE_TYPE (init),
2060 : TREE_TYPE (exp)));
2061 :
2062 75 : if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)
2063 880 : && (!from_array
2064 72 : || (TREE_CODE (init) != CONSTRUCTOR
2065 : /* Can happen, eg, handling the compound-literals
2066 : extension (ext/complit12.C). */
2067 11 : && TREE_CODE (init) != TARGET_EXPR)))
2068 : {
2069 5 : if (complain & tf_error)
2070 5 : error_at (init_loc, "array must be initialized "
2071 : "with a brace-enclosed initializer");
2072 5 : return error_mark_node;
2073 : }
2074 : }
2075 :
2076 11248 : stmt_expr = build_vec_init (exp, NULL_TREE, init,
2077 : /*explicit_value_init_p=*/false,
2078 : from_array,
2079 : complain);
2080 11248 : TREE_READONLY (exp) = was_const;
2081 11248 : TREE_THIS_VOLATILE (exp) = was_volatile;
2082 11248 : TREE_TYPE (exp) = type;
2083 : /* Restore the type of init unless it was used directly. */
2084 11248 : if (init && TREE_CODE (stmt_expr) != INIT_EXPR)
2085 69 : TREE_TYPE (init) = itype;
2086 : return stmt_expr;
2087 : }
2088 :
2089 5641541 : if (is_copy_initialization (init))
2090 1569719 : flags |= LOOKUP_ONLYCONVERTING;
2091 :
2092 5641541 : is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
2093 5641541 : destroy_temps = stmts_are_full_exprs_p ();
2094 5641541 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2095 5641541 : bool ok = expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
2096 : init, LOOKUP_NORMAL|flags, complain);
2097 5641541 : stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
2098 5641541 : current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
2099 5641541 : TREE_READONLY (exp) = was_const;
2100 5641541 : TREE_THIS_VOLATILE (exp) = was_volatile;
2101 5641541 : if (!ok)
2102 2208 : return error_mark_node;
2103 :
2104 2360245 : if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
2105 3279088 : && TREE_SIDE_EFFECTS (stmt_expr)
2106 8897020 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
2107 : /* Just know that we've seen something for this node. */
2108 3257681 : TREE_USED (exp) = 1;
2109 :
2110 : return stmt_expr;
2111 : }
2112 :
2113 : static bool
2114 8094146 : expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
2115 : tsubst_flags_t complain)
2116 : {
2117 8094146 : tree type = TREE_TYPE (exp);
2118 :
2119 : /* It fails because there may not be a constructor which takes
2120 : its own type as the first (or only parameter), but which does
2121 : take other types via a conversion. So, if the thing initializing
2122 : the expression is a unit element of type X, first try X(X&),
2123 : followed by initialization by X. If neither of these work
2124 : out, then look hard. */
2125 8094146 : tree rval;
2126 8094146 : vec<tree, va_gc> *parms;
2127 :
2128 : /* If we have direct-initialization from an initializer list, pull
2129 : it out of the TREE_LIST so the code below can see it. */
2130 5867415 : if (init && TREE_CODE (init) == TREE_LIST
2131 11603841 : && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2132 : {
2133 76531 : gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
2134 : && TREE_CHAIN (init) == NULL_TREE);
2135 76531 : init = TREE_VALUE (init);
2136 : /* Only call reshape_init if it has not been called earlier
2137 : by the callers. */
2138 76531 : if (BRACE_ENCLOSED_INITIALIZER_P (init) && CP_AGGREGATE_TYPE_P (type))
2139 38841 : init = reshape_init (type, init, complain);
2140 : }
2141 :
2142 5867415 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
2143 8889189 : && CP_AGGREGATE_TYPE_P (type))
2144 : /* A brace-enclosed initializer for an aggregate. In C++0x this can
2145 : happen for direct-initialization, too. */
2146 38775 : init = digest_init (type, init, complain);
2147 :
2148 8094146 : if (init == error_mark_node)
2149 : return false;
2150 :
2151 : /* A CONSTRUCTOR of the target's type is a previously digested
2152 : initializer, whether that happened just above or in
2153 : cp_parser_late_parsing_nsdmi.
2154 :
2155 : A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
2156 : set represents the whole initialization, so we shouldn't build up
2157 : another ctor call. */
2158 8094146 : if (init
2159 5867415 : && (TREE_CODE (init) == CONSTRUCTOR
2160 5068914 : || (TREE_CODE (init) == TARGET_EXPR
2161 1157400 : && (TARGET_EXPR_DIRECT_INIT_P (init)
2162 1086699 : || TARGET_EXPR_LIST_INIT_P (init))))
2163 8963369 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
2164 : {
2165 : /* Early initialization via a TARGET_EXPR only works for
2166 : complete objects. */
2167 112955 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
2168 :
2169 112955 : init = cp_build_init_expr (exp, init);
2170 112955 : TREE_SIDE_EFFECTS (init) = 1;
2171 112955 : finish_expr_stmt (init);
2172 112955 : return true;
2173 : }
2174 :
2175 5754460 : if (init && TREE_CODE (init) != TREE_LIST
2176 2321296 : && (flags & LOOKUP_ONLYCONVERTING)
2177 9547152 : && !unsafe_return_slot_p (exp))
2178 : {
2179 : /* Base subobjects should only get direct-initialization. */
2180 1565961 : gcc_assert (true_exp == exp);
2181 :
2182 1565961 : if (flags & DIRECT_BIND)
2183 : /* Do nothing. We hit this in two cases: Reference initialization,
2184 : where we aren't initializing a real variable, so we don't want
2185 : to run a new constructor; and catching an exception, where we
2186 : have already built up the constructor call so we could wrap it
2187 : in an exception region. */;
2188 : else
2189 : {
2190 1565446 : init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
2191 : flags, complain | tf_no_cleanup);
2192 1565446 : if (init == error_mark_node)
2193 : return false;
2194 : }
2195 :
2196 : /* We need to protect the initialization of a catch parm with a
2197 : call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
2198 : around the TARGET_EXPR for the copy constructor. See
2199 : initialize_handler_parm. */
2200 1564402 : tree *p = &init;
2201 1564402 : while (TREE_CODE (*p) == MUST_NOT_THROW_EXPR
2202 1564978 : || TREE_CODE (*p) == CLEANUP_POINT_EXPR)
2203 : {
2204 : /* Avoid voidify_wrapper_expr making a temporary. */
2205 576 : TREE_TYPE (*p) = void_type_node;
2206 576 : p = &TREE_OPERAND (*p, 0);
2207 : }
2208 1564402 : *p = cp_build_init_expr (exp, *p);
2209 1564402 : finish_expr_stmt (init);
2210 1564402 : return true;
2211 : }
2212 :
2213 6415230 : if (init == NULL_TREE)
2214 2226731 : parms = NULL;
2215 4188499 : else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
2216 : {
2217 3433164 : parms = make_tree_vector ();
2218 7719428 : for (; init != NULL_TREE; init = TREE_CHAIN (init))
2219 4286264 : vec_safe_push (parms, TREE_VALUE (init));
2220 : }
2221 : else
2222 755335 : parms = make_tree_vector_single (init);
2223 :
2224 6781176 : if (exp == current_class_ref && current_function_decl
2225 6781176 : && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
2226 : {
2227 : /* Delegating constructor. */
2228 100 : tree complete;
2229 100 : tree base;
2230 100 : tree elt; unsigned i;
2231 :
2232 : /* Unshare the arguments for the second call. */
2233 100 : releasing_vec parms2;
2234 405 : FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
2235 : {
2236 215 : elt = break_out_target_exprs (elt);
2237 215 : vec_safe_push (parms2, elt);
2238 : }
2239 100 : complete = build_special_member_call (exp, complete_ctor_identifier,
2240 : &parms2, binfo, flags,
2241 : complain);
2242 100 : complete = fold_build_cleanup_point_expr (void_type_node, complete);
2243 :
2244 100 : base = build_special_member_call (exp, base_ctor_identifier,
2245 : &parms, binfo, flags,
2246 : complain);
2247 100 : base = fold_build_cleanup_point_expr (void_type_node, base);
2248 100 : if (complete == error_mark_node || base == error_mark_node)
2249 0 : return false;
2250 100 : rval = build_if_in_charge (complete, base);
2251 100 : }
2252 : else
2253 : {
2254 6415130 : tree ctor_name = (true_exp == exp
2255 6415130 : ? complete_ctor_identifier : base_ctor_identifier);
2256 :
2257 6415130 : rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
2258 : complain);
2259 6415130 : if (rval == error_mark_node)
2260 : return false;
2261 : }
2262 :
2263 6414563 : if (parms != NULL)
2264 4205201 : release_tree_vector (parms);
2265 :
2266 6414563 : if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
2267 : {
2268 3206954 : tree fn = get_callee_fndecl (rval);
2269 3206954 : if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
2270 : {
2271 1786453 : tree e = maybe_constant_init (rval, exp);
2272 1786453 : if (TREE_CONSTANT (e))
2273 495563 : rval = cp_build_init_expr (exp, e);
2274 : }
2275 : }
2276 :
2277 : /* FIXME put back convert_to_void? */
2278 6414563 : if (TREE_SIDE_EFFECTS (rval))
2279 5938410 : finish_expr_stmt (rval);
2280 :
2281 : return true;
2282 : }
2283 :
2284 : /* This function is responsible for initializing EXP with INIT
2285 : (if any). Returns true on success, false on failure.
2286 :
2287 : BINFO is the binfo of the type for who we are performing the
2288 : initialization. For example, if W is a virtual base class of A and B,
2289 : and C : A, B.
2290 : If we are initializing B, then W must contain B's W vtable, whereas
2291 : were we initializing C, W must contain C's W vtable.
2292 :
2293 : TRUE_EXP is nonzero if it is the true expression being initialized.
2294 : In this case, it may be EXP, or may just contain EXP. The reason we
2295 : need this is because if EXP is a base element of TRUE_EXP, we
2296 : don't necessarily know by looking at EXP where its virtual
2297 : baseclass fields should really be pointing. But we do know
2298 : from TRUE_EXP. In constructors, we don't know anything about
2299 : the value being initialized.
2300 :
2301 : FLAGS is just passed to `build_new_method_call'. See that function
2302 : for its description. */
2303 :
2304 : static bool
2305 8094757 : expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
2306 : tsubst_flags_t complain)
2307 : {
2308 8094757 : tree type = TREE_TYPE (exp);
2309 :
2310 8094757 : gcc_assert (init != error_mark_node && type != error_mark_node);
2311 8094757 : gcc_assert (building_stmt_list_p ());
2312 :
2313 : /* Use a function returning the desired type to initialize EXP for us.
2314 : If the function is a constructor, and its first argument is
2315 : NULL_TREE, know that it was meant for us--just slide exp on
2316 : in and expand the constructor. Constructors now come
2317 : as TARGET_EXPRs. */
2318 :
2319 6106842 : if (init && VAR_P (exp)
2320 10765031 : && COMPOUND_LITERAL_P (init))
2321 : {
2322 163 : vec<tree, va_gc> *cleanups = NULL;
2323 : /* If store_init_value returns NULL_TREE, the INIT has been
2324 : recorded as the DECL_INITIAL for EXP. That means there's
2325 : nothing more we have to do. */
2326 163 : init = store_init_value (exp, init, &cleanups, flags);
2327 163 : if (init)
2328 0 : finish_expr_stmt (init);
2329 163 : gcc_assert (!cleanups);
2330 163 : return true;
2331 : }
2332 :
2333 : /* List-initialization from {} becomes value-initialization for non-aggregate
2334 : classes with default constructors. Handle this here when we're
2335 : initializing a base, so protected access works. */
2336 8094594 : if (exp != true_exp && init && TREE_CODE (init) == TREE_LIST)
2337 : {
2338 1207082 : tree elt = TREE_VALUE (init);
2339 38977 : if (DIRECT_LIST_INIT_P (elt)
2340 38893 : && CONSTRUCTOR_ELTS (elt) == 0
2341 42 : && CLASSTYPE_NON_AGGREGATE (type)
2342 1207105 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2343 23 : init = void_type_node;
2344 : }
2345 :
2346 : /* If an explicit -- but empty -- initializer list was present,
2347 : that's value-initialization. */
2348 8094594 : if (init == void_type_node)
2349 : {
2350 : /* If the type has data but no user-provided default ctor, we need to zero
2351 : out the object. */
2352 239264 : if (type_has_non_user_provided_default_constructor (type)
2353 239264 : && !is_really_empty_class (type, /*ignore_vptr*/true))
2354 : {
2355 22461 : tree field_size = NULL_TREE;
2356 22461 : if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
2357 : /* Don't clobber already initialized virtual bases. */
2358 37 : field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
2359 22461 : init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
2360 : field_size);
2361 22461 : init = cp_build_init_expr (exp, init);
2362 22461 : finish_expr_stmt (init);
2363 : }
2364 :
2365 : /* If we don't need to mess with the constructor at all,
2366 : then we're done. */
2367 239264 : if (! type_build_ctor_call (type))
2368 : return true;
2369 :
2370 : /* Otherwise fall through and call the constructor. */
2371 : init = NULL_TREE;
2372 : }
2373 :
2374 : /* We know that expand_default_init can handle everything we want
2375 : at this point. */
2376 8094146 : return expand_default_init (binfo, true_exp, exp, init, flags, complain);
2377 : }
2378 :
2379 : /* Report an error if TYPE is not a user-defined, class type. If
2380 : OR_ELSE is nonzero, give an error message. */
2381 :
2382 : int
2383 127360365 : is_class_type (tree type, int or_else)
2384 : {
2385 127360365 : if (type == error_mark_node)
2386 : return 0;
2387 :
2388 127336927 : if (! CLASS_TYPE_P (type))
2389 : {
2390 563 : if (or_else)
2391 5 : error ("%qT is not a class type", type);
2392 : return 0;
2393 : }
2394 : return 1;
2395 : }
2396 :
2397 : /* Returns true iff the initializer INIT represents copy-initialization
2398 : (and therefore we must set LOOKUP_ONLYCONVERTING when processing it). */
2399 :
2400 : bool
2401 134415329 : is_copy_initialization (tree init)
2402 : {
2403 116622369 : return (init && init != void_type_node
2404 116621265 : && TREE_CODE (init) != TREE_LIST
2405 107807962 : && !(TREE_CODE (init) == TARGET_EXPR
2406 2251689 : && TARGET_EXPR_DIRECT_INIT_P (init))
2407 242152545 : && !DIRECT_LIST_INIT_P (init));
2408 : }
2409 :
2410 : /* Build a reference to a member of an aggregate. This is not a C++
2411 : `&', but really something which can have its address taken, and
2412 : then act as a pointer to member, for example TYPE :: FIELD can have
2413 : its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
2414 : this expression is the operand of "&".
2415 :
2416 : @@ Prints out lousy diagnostics for operator <typename>
2417 : @@ fields.
2418 :
2419 : @@ This function should be rewritten and placed in search.cc. */
2420 :
2421 : tree
2422 167948 : build_offset_ref (tree type, tree member, bool address_p,
2423 : tsubst_flags_t complain)
2424 : {
2425 167948 : tree decl;
2426 167948 : tree basebinfo = NULL_TREE;
2427 :
2428 : /* class templates can come in as TEMPLATE_DECLs here. */
2429 167948 : if (TREE_CODE (member) == TEMPLATE_DECL)
2430 : return member;
2431 :
2432 167948 : if (dependent_scope_p (type) || type_dependent_expression_p (member))
2433 55265 : return build_qualified_name (NULL_TREE, type, member,
2434 55265 : /*template_p=*/false);
2435 :
2436 112683 : gcc_assert (TYPE_P (type));
2437 112683 : if (! is_class_type (type, 1))
2438 0 : return error_mark_node;
2439 :
2440 112683 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2441 : /* Callers should call mark_used before this point, except for functions. */
2442 112683 : gcc_assert (!DECL_P (member) || TREE_USED (member)
2443 : || TREE_CODE (member) == FUNCTION_DECL);
2444 :
2445 112683 : type = TYPE_MAIN_VARIANT (type);
2446 112683 : if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
2447 : {
2448 0 : if (complain & tf_error)
2449 0 : error ("incomplete type %qT does not have member %qD", type, member);
2450 0 : return error_mark_node;
2451 : }
2452 :
2453 : /* Entities other than non-static members need no further
2454 : processing. */
2455 112683 : if (TREE_CODE (member) == TYPE_DECL)
2456 : return member;
2457 112683 : if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
2458 906 : return convert_from_reference (member);
2459 :
2460 111777 : if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
2461 : {
2462 2 : if (complain & tf_error)
2463 2 : error ("invalid pointer to bit-field %qD", member);
2464 2 : return error_mark_node;
2465 : }
2466 :
2467 : /* Set up BASEBINFO for member lookup. */
2468 111775 : decl = maybe_dummy_object (type, &basebinfo);
2469 :
2470 : /* A lot of this logic is now handled in lookup_member. */
2471 111775 : if (BASELINK_P (member))
2472 : {
2473 : /* Go from the TREE_BASELINK to the member function info. */
2474 109009 : tree t = BASELINK_FUNCTIONS (member);
2475 :
2476 109009 : if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
2477 : {
2478 : /* Get rid of a potential OVERLOAD around it. */
2479 107813 : t = OVL_FIRST (t);
2480 :
2481 : /* Unique functions are handled easily. */
2482 :
2483 : /* For non-static member of base class, we need a special rule
2484 : for access checking [class.protected]:
2485 :
2486 : If the access is to form a pointer to member, the
2487 : nested-name-specifier shall name the derived class
2488 : (or any class derived from that class). */
2489 107813 : bool ok;
2490 104362 : if (address_p && DECL_P (t)
2491 212175 : && DECL_NONSTATIC_MEMBER_P (t))
2492 60598 : ok = perform_or_defer_access_check (TYPE_BINFO (type), t, t,
2493 : complain);
2494 : else
2495 47215 : ok = perform_or_defer_access_check (basebinfo, t, t,
2496 : complain);
2497 107813 : if (!ok)
2498 3 : return error_mark_node;
2499 107810 : if (DECL_STATIC_FUNCTION_P (t))
2500 : return member;
2501 : member = t;
2502 : }
2503 : else
2504 1196 : TREE_TYPE (member) = unknown_type_node;
2505 : }
2506 2766 : else if (address_p && TREE_CODE (member) == FIELD_DECL)
2507 : {
2508 : /* We need additional test besides the one in
2509 : check_accessibility_of_qualified_id in case it is
2510 : a pointer to non-static member. */
2511 2636 : if (!perform_or_defer_access_check (TYPE_BINFO (type), member, member,
2512 : complain))
2513 0 : return error_mark_node;
2514 : }
2515 :
2516 64593 : if (!address_p)
2517 : {
2518 : /* If MEMBER is non-static, then the program has fallen afoul of
2519 : [expr.prim]:
2520 :
2521 : An id-expression that denotes a non-static data member or
2522 : non-static member function of a class can only be used:
2523 :
2524 : -- as part of a class member access (_expr.ref_) in which the
2525 : object-expression refers to the member's class or a class
2526 : derived from that class, or
2527 :
2528 : -- to form a pointer to member (_expr.unary.op_), or
2529 :
2530 : -- in the body of a non-static member function of that class or
2531 : of a class derived from that class (_class.mfct.non-static_), or
2532 :
2533 : -- in a mem-initializer for a constructor for that class or for
2534 : a class derived from that class (_class.base.init_). */
2535 149 : if (DECL_OBJECT_MEMBER_FUNCTION_P (member))
2536 : {
2537 : /* Build a representation of the qualified name suitable
2538 : for use as the operand to "&" -- even though the "&" is
2539 : not actually present. */
2540 36 : member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
2541 : /* In Microsoft mode, treat a non-static member function as if
2542 : it were a pointer-to-member. */
2543 36 : if (flag_ms_extensions)
2544 : {
2545 6 : PTRMEM_OK_P (member) = 1;
2546 6 : return cp_build_addr_expr (member, complain);
2547 : }
2548 30 : if (complain & tf_error)
2549 27 : error ("invalid use of non-static member function %qD",
2550 27 : TREE_OPERAND (member, 1));
2551 30 : return error_mark_node;
2552 : }
2553 113 : else if (TREE_CODE (member) == FIELD_DECL)
2554 : {
2555 0 : if (complain & tf_error)
2556 0 : error ("invalid use of non-static data member %qD", member);
2557 0 : return error_mark_node;
2558 : }
2559 : return member;
2560 : }
2561 :
2562 64444 : member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
2563 64444 : PTRMEM_OK_P (member) = 1;
2564 64444 : return member;
2565 : }
2566 :
2567 : /* If DECL is a scalar enumeration constant or variable with a
2568 : constant initializer, return the initializer (or, its initializers,
2569 : recursively); otherwise, return DECL. If STRICT_P, the
2570 : initializer is only returned if DECL is a
2571 : constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
2572 : return an aggregate constant. If UNSHARE_P, return an unshared
2573 : copy of the initializer. */
2574 :
2575 : static tree
2576 903034037 : constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p,
2577 : bool unshare_p)
2578 : {
2579 903034037 : while (TREE_CODE (decl) == CONST_DECL
2580 1016739132 : || decl_constant_var_p (decl)
2581 1940481643 : || (!strict_p && VAR_P (decl)
2582 100640936 : && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl))))
2583 : {
2584 157649233 : tree init;
2585 : /* If DECL is a static data member in a template
2586 : specialization, we must instantiate it here. The
2587 : initializer for the static data member is not processed
2588 : until needed; we need it now. */
2589 157649233 : mark_used (decl, tf_none);
2590 157649233 : init = DECL_INITIAL (decl);
2591 157649233 : if (init == error_mark_node)
2592 : {
2593 1697 : if (TREE_CODE (decl) == CONST_DECL
2594 3394 : || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2595 : /* Treat the error as a constant to avoid cascading errors on
2596 : excessively recursive template instantiation (c++/9335). */
2597 1697 : return init;
2598 : else
2599 : return decl;
2600 : }
2601 : /* Initializers in templates are generally expanded during
2602 : instantiation, so before that for const int i(2)
2603 : INIT is a TREE_LIST with the actual initializer as
2604 : TREE_VALUE. */
2605 157647536 : if (processing_template_decl
2606 385389 : && init
2607 385389 : && TREE_CODE (init) == TREE_LIST
2608 157647536 : && TREE_CHAIN (init) == NULL_TREE)
2609 0 : init = TREE_VALUE (init);
2610 : /* Instantiate a non-dependent initializer for user variables. We
2611 : mustn't do this for the temporary for an array compound literal;
2612 : trying to instantiate the initializer will keep creating new
2613 : temporaries until we crash. Probably it's not useful to do it for
2614 : other artificial variables, either. */
2615 157647536 : if (!DECL_ARTIFICIAL (decl))
2616 156830081 : init = instantiate_non_dependent_or_null (init);
2617 157647536 : if (!init
2618 145433296 : || !TREE_TYPE (init)
2619 145433212 : || !TREE_CONSTANT (init)
2620 287430119 : || (!return_aggregate_cst_ok_p
2621 : /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2622 : return an aggregate constant (of which string
2623 : literals are a special case), as we do not want
2624 : to make inadvertent copies of such entities, and
2625 : we must be sure that their addresses are the
2626 : same everywhere. */
2627 5562590 : && (TREE_CODE (init) == CONSTRUCTOR
2628 5562590 : || TREE_CODE (init) == STRING_CST)))
2629 : break;
2630 : /* Don't return a CONSTRUCTOR for a variable with partial run-time
2631 : initialization, since it doesn't represent the entire value.
2632 : Similarly for VECTOR_CSTs created by cp_folding those
2633 : CONSTRUCTORs. */
2634 129782583 : if ((TREE_CODE (init) == CONSTRUCTOR
2635 126834170 : || TREE_CODE (init) == VECTOR_CST)
2636 132731359 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2637 : break;
2638 : /* If the variable has a dynamic initializer, don't use its
2639 : DECL_INITIAL which doesn't reflect the real value. */
2640 129782562 : if (VAR_P (decl)
2641 113705157 : && TREE_STATIC (decl)
2642 105634845 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
2643 129782624 : && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2644 : break;
2645 : decl = init;
2646 : }
2647 903029643 : return unshare_p ? unshare_expr (decl) : decl;
2648 : }
2649 :
2650 : /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant
2651 : of integral or enumeration type, or a constexpr variable of scalar type,
2652 : then return that value. These are those variables permitted in constant
2653 : expressions by [5.19/1]. */
2654 :
2655 : tree
2656 490205050 : scalar_constant_value (tree decl)
2657 : {
2658 490205050 : return constant_value_1 (decl, /*strict_p=*/true,
2659 : /*return_aggregate_cst_ok_p=*/false,
2660 490205050 : /*unshare_p=*/true);
2661 : }
2662 :
2663 : /* Like scalar_constant_value, but can also return aggregate initializers.
2664 : If UNSHARE_P, return an unshared copy of the initializer. */
2665 :
2666 : tree
2667 147947331 : decl_really_constant_value (tree decl, bool unshare_p /*= true*/)
2668 : {
2669 147947331 : return constant_value_1 (decl, /*strict_p=*/true,
2670 : /*return_aggregate_cst_ok_p=*/true,
2671 147944634 : /*unshare_p=*/unshare_p);
2672 : }
2673 :
2674 : /* A more relaxed version of decl_really_constant_value, used by the
2675 : common C/C++ code. */
2676 :
2677 : tree
2678 264881656 : decl_constant_value (tree decl, bool unshare_p)
2679 : {
2680 264881656 : return constant_value_1 (decl, /*strict_p=*/processing_template_decl,
2681 : /*return_aggregate_cst_ok_p=*/true,
2682 264881656 : /*unshare_p=*/unshare_p);
2683 : }
2684 :
2685 : tree
2686 264532272 : decl_constant_value (tree decl)
2687 : {
2688 264532272 : return decl_constant_value (decl, /*unshare_p=*/true);
2689 : }
2690 :
2691 : /* Common subroutines of build_new and build_vec_delete. */
2692 :
2693 : /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2694 : the type of the object being allocated; otherwise, it's just TYPE.
2695 : INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2696 : user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2697 : a vector of arguments to be provided as arguments to a placement
2698 : new operator. This routine performs no semantic checks; it just
2699 : creates and returns a NEW_EXPR. */
2700 :
2701 : static tree
2702 933461 : build_raw_new_expr (location_t loc, vec<tree, va_gc> *placement, tree type,
2703 : tree nelts, vec<tree, va_gc> *init, int use_global_new)
2704 : {
2705 933461 : tree init_list;
2706 933461 : tree new_expr;
2707 :
2708 : /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2709 : If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2710 : permits us to distinguish the case of a missing initializer "new
2711 : int" from an empty initializer "new int()". */
2712 933461 : if (init == NULL)
2713 : init_list = NULL_TREE;
2714 604841 : else if (init->is_empty ())
2715 109786 : init_list = void_node;
2716 : else
2717 495055 : init_list = build_tree_list_vec (init);
2718 :
2719 933461 : new_expr = build4_loc (loc, NEW_EXPR, build_pointer_type (type),
2720 : build_tree_list_vec (placement), type, nelts,
2721 : init_list);
2722 933461 : NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2723 933461 : TREE_SIDE_EFFECTS (new_expr) = 1;
2724 :
2725 933461 : return new_expr;
2726 : }
2727 :
2728 : /* Diagnose uninitialized const members or reference members of type
2729 : TYPE. USING_NEW is used to disambiguate the diagnostic between a
2730 : new expression without a new-initializer and a declaration. Returns
2731 : the error count. */
2732 :
2733 : static int
2734 65 : diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2735 : bool using_new, bool complain)
2736 : {
2737 65 : tree field;
2738 65 : int error_count = 0;
2739 :
2740 65 : if (type_has_user_provided_constructor (type))
2741 : return 0;
2742 :
2743 222 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2744 : {
2745 159 : tree field_type;
2746 :
2747 159 : if (TREE_CODE (field) != FIELD_DECL)
2748 82 : continue;
2749 :
2750 77 : field_type = strip_array_types (TREE_TYPE (field));
2751 :
2752 77 : if (type_has_user_provided_constructor (field_type))
2753 3 : continue;
2754 :
2755 74 : if (TYPE_REF_P (field_type))
2756 : {
2757 24 : ++ error_count;
2758 24 : if (complain)
2759 : {
2760 22 : auto_diagnostic_group d;
2761 22 : if (DECL_CONTEXT (field) == origin)
2762 : {
2763 14 : if (using_new)
2764 6 : error ("uninitialized reference member in %q#T "
2765 : "using %<new%> without new-initializer", origin);
2766 : else
2767 8 : error ("uninitialized reference member in %q#T", origin);
2768 : }
2769 : else
2770 : {
2771 8 : if (using_new)
2772 3 : error ("uninitialized reference member in base %q#T "
2773 : "of %q#T using %<new%> without new-initializer",
2774 3 : DECL_CONTEXT (field), origin);
2775 : else
2776 5 : error ("uninitialized reference member in base %q#T "
2777 5 : "of %q#T", DECL_CONTEXT (field), origin);
2778 : }
2779 22 : inform (DECL_SOURCE_LOCATION (field),
2780 : "%q#D should be initialized", field);
2781 22 : }
2782 : }
2783 :
2784 74 : if (CP_TYPE_CONST_P (field_type))
2785 : {
2786 32 : ++ error_count;
2787 32 : if (complain)
2788 : {
2789 30 : auto_diagnostic_group d;
2790 30 : if (DECL_CONTEXT (field) == origin)
2791 : {
2792 23 : if (using_new)
2793 9 : error ("uninitialized const member in %q#T "
2794 : "using %<new%> without new-initializer", origin);
2795 : else
2796 14 : error ("uninitialized const member in %q#T", origin);
2797 : }
2798 : else
2799 : {
2800 7 : if (using_new)
2801 3 : error ("uninitialized const member in base %q#T "
2802 : "of %q#T using %<new%> without new-initializer",
2803 3 : DECL_CONTEXT (field), origin);
2804 : else
2805 4 : error ("uninitialized const member in base %q#T "
2806 4 : "of %q#T", DECL_CONTEXT (field), origin);
2807 : }
2808 30 : inform (DECL_SOURCE_LOCATION (field),
2809 : "%q#D should be initialized", field);
2810 30 : }
2811 : }
2812 :
2813 74 : if (CLASS_TYPE_P (field_type))
2814 16 : error_count
2815 16 : += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2816 : using_new, complain);
2817 : }
2818 : return error_count;
2819 : }
2820 :
2821 : int
2822 49 : diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2823 : {
2824 49 : return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2825 : }
2826 :
2827 : /* Call __cxa_bad_array_new_length to indicate that the size calculation
2828 : overflowed. */
2829 :
2830 : tree
2831 21116 : throw_bad_array_new_length (void)
2832 : {
2833 21116 : if (!fn)
2834 : {
2835 11052 : tree name = get_identifier ("__cxa_throw_bad_array_new_length");
2836 :
2837 11052 : fn = get_global_binding (name);
2838 11052 : if (!fn)
2839 11052 : fn = push_throw_library_fn
2840 11052 : (name, build_function_type_list (void_type_node, NULL_TREE));
2841 : }
2842 :
2843 21116 : return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2844 : }
2845 :
2846 : /* Attempt to verify that the argument, OPER, of a placement new expression
2847 : refers to an object sufficiently large for an object of TYPE or an array
2848 : of NELTS of such objects when NELTS is non-null, and issue a warning when
2849 : it does not. SIZE specifies the size needed to construct the object or
2850 : array and captures the result of NELTS * sizeof (TYPE). (SIZE could be
2851 : greater when the array under construction requires a cookie to store
2852 : NELTS. GCC's placement new expression stores the cookie when invoking
2853 : a user-defined placement new operator function but not the default one.
2854 : Placement new expressions with user-defined placement new operator are
2855 : not diagnosed since we don't know how they use the buffer (this could
2856 : be a future extension). */
2857 : static void
2858 674562 : warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper)
2859 : {
2860 674562 : location_t loc = cp_expr_loc_or_input_loc (oper);
2861 :
2862 674562 : STRIP_NOPS (oper);
2863 :
2864 : /* Using a function argument or a (non-array) variable as an argument
2865 : to placement new is not checked since it's unknown what it might
2866 : point to. */
2867 674562 : if (TREE_CODE (oper) == PARM_DECL
2868 : || VAR_P (oper)
2869 : || TREE_CODE (oper) == COMPONENT_REF)
2870 673590 : return;
2871 :
2872 : /* Evaluate any constant expressions. */
2873 458333 : size = fold_non_dependent_expr (size);
2874 :
2875 458333 : access_ref ref;
2876 458333 : ref.eval = [](tree x){ return fold_non_dependent_expr (x); };
2877 458333 : ref.trail1special = warn_placement_new < 2;
2878 458333 : tree objsize = compute_objsize (oper, 1, &ref);
2879 458333 : if (!objsize)
2880 : return;
2881 :
2882 : /* We can only draw conclusions if ref.deref == -1,
2883 : i.e. oper is the address of the object. */
2884 458239 : if (ref.deref != -1)
2885 : return;
2886 :
2887 2676 : offset_int bytes_avail = wi::to_offset (objsize);
2888 2676 : offset_int bytes_need;
2889 :
2890 2676 : if (CONSTANT_CLASS_P (size))
2891 2643 : bytes_need = wi::to_offset (size);
2892 33 : else if (nelts && CONSTANT_CLASS_P (nelts))
2893 0 : bytes_need = (wi::to_offset (nelts)
2894 0 : * wi::to_offset (TYPE_SIZE_UNIT (type)));
2895 33 : else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2896 30 : bytes_need = wi::to_offset (TYPE_SIZE_UNIT (type));
2897 : else
2898 : {
2899 : /* The type is a VLA. */
2900 : return;
2901 : }
2902 :
2903 2673 : if (bytes_avail >= bytes_need)
2904 : return;
2905 :
2906 : /* True when the size to mention in the warning is exact as opposed
2907 : to "at least N". */
2908 975 : const bool exact_size = (ref.offrng[0] == ref.offrng[1]
2909 975 : || ref.sizrng[1] - ref.offrng[0] == 0);
2910 :
2911 975 : tree opertype = ref.ref ? TREE_TYPE (ref.ref) : TREE_TYPE (oper);
2912 975 : bool warned = false;
2913 975 : if (nelts)
2914 402 : nelts = fold_for_warn (nelts);
2915 :
2916 975 : auto_diagnostic_group d;
2917 975 : if (nelts)
2918 402 : if (CONSTANT_CLASS_P (nelts))
2919 399 : warned = warning_at (loc, OPT_Wplacement_new_,
2920 : (exact_size
2921 : ? G_("placement new constructing an object "
2922 : "of type %<%T [%wu]%> and size %qwu "
2923 : "in a region of type %qT and size %qwi")
2924 : : G_("placement new constructing an object "
2925 : "of type %<%T [%wu]%> and size %qwu "
2926 : "in a region of type %qT and size "
2927 : "at most %qwu")),
2928 : type, tree_to_uhwi (nelts),
2929 : bytes_need.to_uhwi (),
2930 : opertype, bytes_avail.to_uhwi ());
2931 : else
2932 9 : warned = warning_at (loc, OPT_Wplacement_new_,
2933 : (exact_size
2934 : ? G_("placement new constructing an array "
2935 : "of objects of type %qT and size %qwu "
2936 : "in a region of type %qT and size %qwi")
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 "
2940 : "at most %qwu")),
2941 : type, bytes_need.to_uhwi (), opertype,
2942 : bytes_avail.to_uhwi ());
2943 : else
2944 603 : warned = warning_at (loc, OPT_Wplacement_new_,
2945 : (exact_size
2946 : ? G_("placement new constructing an object "
2947 : "of type %qT and size %qwu in a region "
2948 : "of type %qT and size %qwi")
2949 : : G_("placement new constructing an object "
2950 : "of type %qT "
2951 : "and size %qwu in a region of type %qT "
2952 : "and size at most %qwu")),
2953 : type, bytes_need.to_uhwi (), opertype,
2954 : bytes_avail.to_uhwi ());
2955 :
2956 975 : if (!warned || !ref.ref)
2957 3 : return;
2958 :
2959 972 : if (ref.offrng[0] == 0 || !ref.offset_bounded ())
2960 : /* Avoid mentioning the offset when its lower bound is zero
2961 : or when it's impossibly large. */
2962 525 : inform (DECL_SOURCE_LOCATION (ref.ref),
2963 : "%qD declared here", ref.ref);
2964 447 : else if (ref.offrng[0] == ref.offrng[1])
2965 438 : inform (DECL_SOURCE_LOCATION (ref.ref),
2966 : "at offset %wi from %qD declared here",
2967 : ref.offrng[0].to_shwi (), ref.ref);
2968 : else
2969 9 : inform (DECL_SOURCE_LOCATION (ref.ref),
2970 : "at offset [%wi, %wi] from %qD declared here",
2971 : ref.offrng[0].to_shwi (), ref.offrng[1].to_shwi (), ref.ref);
2972 975 : }
2973 :
2974 : /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */
2975 :
2976 : bool
2977 2806127 : type_has_new_extended_alignment (tree t)
2978 : {
2979 2806127 : return (aligned_new_threshold
2980 2806127 : && TYPE_ALIGN_UNIT (t) > (unsigned)aligned_new_threshold);
2981 : }
2982 :
2983 : /* Return the alignment we expect malloc to guarantee. This should just be
2984 : MALLOC_ABI_ALIGNMENT, but that macro defaults to only BITS_PER_WORD for some
2985 : reason, so don't let the threshold be smaller than max_align_t_align. */
2986 :
2987 : unsigned
2988 82830 : malloc_alignment ()
2989 : {
2990 84258 : return MAX (max_align_t_align(), MALLOC_ABI_ALIGNMENT);
2991 : }
2992 :
2993 : /* Determine whether an allocation function is a namespace-scope
2994 : non-replaceable placement new function. See DR 1748. */
2995 : bool
2996 36360872 : std_placement_new_fn_p (tree alloc_fn)
2997 : {
2998 72721744 : if (DECL_NAMESPACE_SCOPE_P (alloc_fn)
2999 18362784 : && IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_fn))
3000 38204794 : && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (alloc_fn))
3001 : {
3002 1259683 : tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3003 1259683 : if (first_arg
3004 1259671 : && (TREE_VALUE (first_arg) == ptr_type_node)
3005 2518965 : && (TREE_CHAIN (first_arg) == void_list_node))
3006 1259282 : return true;
3007 : }
3008 : return false;
3009 : }
3010 :
3011 : /* For element type ELT_TYPE, return the appropriate type of the heap object
3012 : containing such element(s). COOKIE_SIZE is the size of cookie in bytes.
3013 : Return
3014 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
3015 : where N is nothing (flexible array member) if ITYPE2 is NULL, otherwise
3016 : the array has ITYPE2 as its TYPE_DOMAIN. */
3017 :
3018 : tree
3019 19 : build_new_constexpr_heap_type (tree elt_type, tree cookie_size, tree itype2)
3020 : {
3021 19 : gcc_assert (tree_fits_uhwi_p (cookie_size));
3022 19 : unsigned HOST_WIDE_INT csz = tree_to_uhwi (cookie_size);
3023 19 : csz /= int_size_in_bytes (sizetype);
3024 19 : tree itype1 = build_index_type (size_int (csz - 1));
3025 19 : tree atype1 = build_cplus_array_type (sizetype, itype1);
3026 19 : tree atype2 = build_cplus_array_type (elt_type, itype2);
3027 19 : tree rtype = cxx_make_type (RECORD_TYPE);
3028 19 : tree fld1 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype1);
3029 19 : tree fld2 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype2);
3030 19 : DECL_FIELD_CONTEXT (fld1) = rtype;
3031 19 : DECL_FIELD_CONTEXT (fld2) = rtype;
3032 19 : DECL_ARTIFICIAL (fld1) = true;
3033 19 : DECL_ARTIFICIAL (fld2) = true;
3034 19 : TYPE_FIELDS (rtype) = fld1;
3035 19 : DECL_CHAIN (fld1) = fld2;
3036 19 : TYPE_ARTIFICIAL (rtype) = true;
3037 19 : layout_type (rtype);
3038 :
3039 19 : tree decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, heap_identifier, rtype);
3040 19 : TYPE_NAME (rtype) = decl;
3041 19 : TYPE_STUB_DECL (rtype) = decl;
3042 19 : DECL_CONTEXT (decl) = NULL_TREE;
3043 19 : DECL_ARTIFICIAL (decl) = true;
3044 19 : layout_decl (decl, 0);
3045 :
3046 19 : return rtype;
3047 : }
3048 :
3049 : /* Help the constexpr code to find the right type for the heap variable
3050 : by adding a NOP_EXPR around ALLOC_CALL if needed for cookie_size.
3051 : Return ALLOC_CALL or ALLOC_CALL cast to a pointer to
3052 : struct { size_t[cookie_size/sizeof(size_t)]; elt_type[]; }. */
3053 :
3054 : static tree
3055 228 : maybe_wrap_new_for_constexpr (tree alloc_call, tree elt_type, tree cookie_size)
3056 : {
3057 228 : if (cxx_dialect < cxx20)
3058 : return alloc_call;
3059 :
3060 175 : if (current_function_decl != NULL_TREE
3061 175 : && !DECL_DECLARED_CONSTEXPR_P (current_function_decl))
3062 : return alloc_call;
3063 :
3064 10 : tree call_expr = extract_call_expr (alloc_call);
3065 10 : if (call_expr == error_mark_node)
3066 : return alloc_call;
3067 :
3068 10 : tree alloc_call_fndecl = cp_get_callee_fndecl_nofold (call_expr);
3069 10 : if (alloc_call_fndecl == NULL_TREE
3070 10 : || !IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_call_fndecl))
3071 20 : || CP_DECL_CONTEXT (alloc_call_fndecl) != global_namespace)
3072 : return alloc_call;
3073 :
3074 10 : tree rtype = build_new_constexpr_heap_type (elt_type, cookie_size,
3075 : NULL_TREE);
3076 10 : return build_nop (build_pointer_type (rtype), alloc_call);
3077 : }
3078 :
3079 : /* Generate code for a new-expression, including calling the "operator
3080 : new" function, initializing the object, and, if an exception occurs
3081 : during construction, cleaning up. The arguments are as for
3082 : build_raw_new_expr. This may change PLACEMENT and INIT.
3083 : TYPE is the type of the object being constructed, possibly an array
3084 : of NELTS elements when NELTS is non-null (in "new T[NELTS]", T may
3085 : be an array of the form U[inner], with the whole expression being
3086 : "new U[NELTS][inner]"). */
3087 :
3088 : static tree
3089 1083758 : build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
3090 : vec<tree, va_gc> **init, bool globally_qualified_p,
3091 : tsubst_flags_t complain)
3092 : {
3093 1083758 : tree size, rval;
3094 : /* True iff this is a call to "operator new[]" instead of just
3095 : "operator new". */
3096 1083758 : bool array_p = false;
3097 : /* If ARRAY_P is true, the element type of the array. This is never
3098 : an ARRAY_TYPE; for something like "new int[3][4]", the
3099 : ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
3100 : TYPE. */
3101 1083758 : tree elt_type;
3102 : /* The type of the new-expression. (This type is always a pointer
3103 : type.) */
3104 1083758 : tree pointer_type;
3105 1083758 : tree non_const_pointer_type;
3106 : /* The most significant array bound in int[OUTER_NELTS][inner]. */
3107 1083758 : tree outer_nelts = NULL_TREE;
3108 : /* For arrays with a non-constant number of elements, a bounds checks
3109 : on the NELTS parameter to avoid integer overflow at runtime. */
3110 1083758 : tree outer_nelts_check = NULL_TREE;
3111 1083758 : bool outer_nelts_from_type = false;
3112 : /* Number of the "inner" elements in "new T[OUTER_NELTS][inner]". */
3113 1083758 : offset_int inner_nelts_count = 1;
3114 1083758 : tree alloc_call, alloc_expr;
3115 : /* Size of the inner array elements (those with constant dimensions). */
3116 1083758 : offset_int inner_size;
3117 : /* The address returned by the call to "operator new". This node is
3118 : a VAR_DECL and is therefore reusable. */
3119 1083758 : tree alloc_node;
3120 1083758 : tree alloc_fn;
3121 1083758 : tree cookie_expr, init_expr;
3122 1083758 : int nothrow, check_new;
3123 : /* If non-NULL, the number of extra bytes to allocate at the
3124 : beginning of the storage allocated for an array-new expression in
3125 : order to store the number of elements. */
3126 1083758 : tree cookie_size = NULL_TREE;
3127 1083758 : tree placement_first;
3128 1083758 : tree placement_expr = NULL_TREE;
3129 : /* True if the function we are calling is a placement allocation
3130 : function. */
3131 1083758 : bool placement_allocation_fn_p;
3132 : /* True if the storage must be initialized, either by a constructor
3133 : or due to an explicit new-initializer. */
3134 1083758 : bool is_initialized;
3135 : /* The address of the thing allocated, not including any cookie. In
3136 : particular, if an array cookie is in use, DATA_ADDR is the
3137 : address of the first array element. This node is a VAR_DECL, and
3138 : is therefore reusable. */
3139 1083758 : tree data_addr;
3140 1083758 : tree orig_type = type;
3141 :
3142 1083758 : if (nelts)
3143 : {
3144 : outer_nelts = nelts;
3145 : array_p = true;
3146 : }
3147 995496 : else if (TREE_CODE (type) == ARRAY_TYPE)
3148 : {
3149 : /* Transforms new (T[N]) to new T[N]. The former is a GNU
3150 : extension for variable N. (This also covers new T where T is
3151 : a VLA typedef.) */
3152 249 : array_p = true;
3153 249 : nelts = array_type_nelts_top (type);
3154 249 : outer_nelts = nelts;
3155 249 : type = TREE_TYPE (type);
3156 249 : outer_nelts_from_type = true;
3157 : }
3158 :
3159 : /* Lots of logic below depends on whether we have a constant number of
3160 : elements, so go ahead and fold it now. */
3161 1083758 : const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain);
3162 :
3163 : /* If our base type is an array, then make sure we know how many elements
3164 : it has. */
3165 1083758 : for (elt_type = type;
3166 1085298 : TREE_CODE (elt_type) == ARRAY_TYPE;
3167 1540 : elt_type = TREE_TYPE (elt_type))
3168 : {
3169 1540 : tree inner_nelts = array_type_nelts_top (elt_type);
3170 1540 : tree inner_nelts_cst = maybe_constant_value (inner_nelts);
3171 1540 : if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
3172 : {
3173 1510 : wi::overflow_type overflow;
3174 1510 : offset_int result = wi::mul (wi::to_offset (inner_nelts_cst),
3175 : inner_nelts_count, SIGNED, &overflow);
3176 1510 : if (overflow)
3177 : {
3178 0 : if (complain & tf_error)
3179 0 : error ("integer overflow in array size");
3180 0 : nelts = error_mark_node;
3181 : }
3182 1510 : inner_nelts_count = result;
3183 : }
3184 : else
3185 : {
3186 30 : if (complain & tf_error)
3187 : {
3188 30 : error_at (cp_expr_loc_or_input_loc (inner_nelts),
3189 : "array size in new-expression must be constant");
3190 30 : cxx_constant_value(inner_nelts);
3191 : }
3192 30 : nelts = error_mark_node;
3193 : }
3194 1540 : if (nelts != error_mark_node)
3195 1510 : nelts = cp_build_binary_op (input_location,
3196 : MULT_EXPR, nelts,
3197 : inner_nelts_cst,
3198 : complain);
3199 : }
3200 :
3201 1083758 : if (!verify_type_context (input_location, TCTX_ALLOCATION, elt_type,
3202 1083758 : !(complain & tf_error)))
3203 0 : return error_mark_node;
3204 :
3205 1083758 : if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
3206 : {
3207 0 : error ("variably modified type not allowed in new-expression");
3208 0 : return error_mark_node;
3209 : }
3210 :
3211 1083758 : if (nelts == error_mark_node)
3212 : return error_mark_node;
3213 :
3214 : /* Warn if we performed the (T[N]) to T[N] transformation and N is
3215 : variable. */
3216 1083728 : if (outer_nelts_from_type
3217 1083728 : && !TREE_CONSTANT (cst_outer_nelts))
3218 : {
3219 12 : if (complain & tf_warning_or_error)
3220 : {
3221 21 : pedwarn (cp_expr_loc_or_input_loc (outer_nelts), OPT_Wvla,
3222 12 : typedef_variant_p (orig_type)
3223 : ? G_("non-constant array new length must be specified "
3224 : "directly, not by %<typedef%>")
3225 : : G_("non-constant array new length must be specified "
3226 : "without parentheses around the type-id"));
3227 : }
3228 : else
3229 : return error_mark_node;
3230 : }
3231 :
3232 1083728 : if (VOID_TYPE_P (elt_type))
3233 : {
3234 0 : if (complain & tf_error)
3235 0 : error ("invalid type %<void%> for %<new%>");
3236 0 : return error_mark_node;
3237 : }
3238 :
3239 1083728 : if (is_std_init_list (elt_type) && !cp_unevaluated_operand)
3240 7 : warning (OPT_Winit_list_lifetime,
3241 : "%<new%> of %<initializer_list%> does not "
3242 : "extend the lifetime of the underlying array");
3243 :
3244 1083728 : if (abstract_virtuals_error (ACU_NEW, elt_type, complain))
3245 10 : return error_mark_node;
3246 :
3247 1083718 : is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
3248 :
3249 1083718 : if (*init == NULL && cxx_dialect < cxx11)
3250 : {
3251 2576 : bool maybe_uninitialized_error = false;
3252 : /* A program that calls for default-initialization [...] of an
3253 : entity of reference type is ill-formed. */
3254 2576 : if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
3255 2576 : maybe_uninitialized_error = true;
3256 :
3257 : /* A new-expression that creates an object of type T initializes
3258 : that object as follows:
3259 : - If the new-initializer is omitted:
3260 : -- If T is a (possibly cv-qualified) non-POD class type
3261 : (or array thereof), the object is default-initialized (8.5).
3262 : [...]
3263 : -- Otherwise, the object created has indeterminate
3264 : value. If T is a const-qualified type, or a (possibly
3265 : cv-qualified) POD class type (or array thereof)
3266 : containing (directly or indirectly) a member of
3267 : const-qualified type, the program is ill-formed; */
3268 :
3269 2576 : if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
3270 : maybe_uninitialized_error = true;
3271 :
3272 2558 : if (maybe_uninitialized_error
3273 2576 : && diagnose_uninitialized_cst_or_ref_member (elt_type,
3274 : /*using_new=*/true,
3275 : complain & tf_error))
3276 20 : return error_mark_node;
3277 : }
3278 :
3279 1083749 : if (CP_TYPE_CONST_P (elt_type) && *init == NULL
3280 1083734 : && default_init_uninitialized_part (elt_type))
3281 : {
3282 29 : if (complain & tf_error)
3283 18 : error ("uninitialized const in %<new%> of %q#T", elt_type);
3284 29 : return error_mark_node;
3285 : }
3286 :
3287 1083669 : size = size_in_bytes (elt_type);
3288 1083669 : if (array_p)
3289 : {
3290 : /* Maximum available size in bytes. Half of the address space
3291 : minus the cookie size. */
3292 88471 : offset_int max_size
3293 88471 : = wi::set_bit_in_zero <offset_int> (TYPE_PRECISION (sizetype) - 1);
3294 : /* Maximum number of outer elements which can be allocated. */
3295 88471 : offset_int max_outer_nelts;
3296 88471 : tree max_outer_nelts_tree;
3297 :
3298 88471 : gcc_assert (TREE_CODE (size) == INTEGER_CST);
3299 88471 : cookie_size = targetm.cxx.get_cookie_size (elt_type);
3300 88471 : gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
3301 88471 : gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size));
3302 : /* Unconditionally subtract the cookie size. This decreases the
3303 : maximum object size and is safe even if we choose not to use
3304 : a cookie after all. */
3305 88471 : max_size -= wi::to_offset (cookie_size);
3306 88471 : wi::overflow_type overflow;
3307 88471 : inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED,
3308 : &overflow);
3309 176942 : if (overflow || wi::gtu_p (inner_size, max_size))
3310 : {
3311 138 : if (complain & tf_error)
3312 : {
3313 138 : cst_size_error error;
3314 138 : if (overflow)
3315 : error = cst_size_overflow;
3316 : else
3317 : {
3318 138 : error = cst_size_too_big;
3319 138 : size = size_binop (MULT_EXPR, size,
3320 : wide_int_to_tree (sizetype,
3321 : inner_nelts_count));
3322 138 : size = cp_fully_fold (size);
3323 : }
3324 138 : invalid_array_size_error (input_location, error, size,
3325 : /*name=*/NULL_TREE);
3326 : }
3327 324 : return error_mark_node;
3328 : }
3329 :
3330 88333 : max_outer_nelts = wi::udiv_trunc (max_size, inner_size);
3331 88333 : max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts);
3332 :
3333 88333 : size = build2 (MULT_EXPR, sizetype, size, nelts);
3334 :
3335 88333 : if (TREE_CODE (cst_outer_nelts) == INTEGER_CST)
3336 : {
3337 3100 : if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts))
3338 : {
3339 : /* When the array size is constant, check it at compile time
3340 : to make sure it doesn't exceed the implementation-defined
3341 : maximum, as required by C++ 14 (in C++ 11 this requirement
3342 : isn't explicitly stated but it's enforced anyway -- see
3343 : grokdeclarator in cp/decl.cc). */
3344 186 : if (complain & tf_error)
3345 : {
3346 186 : size = cp_fully_fold (size);
3347 186 : invalid_array_size_error (input_location, cst_size_too_big,
3348 : size, NULL_TREE);
3349 : }
3350 186 : return error_mark_node;
3351 : }
3352 : }
3353 : else
3354 : {
3355 : /* When a runtime check is necessary because the array size
3356 : isn't constant, keep only the top-most seven bits (starting
3357 : with the most significant non-zero bit) of the maximum size
3358 : to compare the array size against, to simplify encoding the
3359 : constant maximum size in the instruction stream. */
3360 :
3361 85233 : unsigned shift = (max_outer_nelts.get_precision ()) - 7
3362 85233 : - wi::clz (max_outer_nelts);
3363 85233 : max_outer_nelts = (max_outer_nelts >> shift) << shift;
3364 :
3365 85233 : outer_nelts_check = build2 (LE_EXPR, boolean_type_node,
3366 : outer_nelts,
3367 : max_outer_nelts_tree);
3368 : }
3369 : }
3370 :
3371 1083345 : tree align_arg = NULL_TREE;
3372 1083345 : if (type_has_new_extended_alignment (elt_type))
3373 : {
3374 9439 : unsigned align = TYPE_ALIGN_UNIT (elt_type);
3375 : /* Also consider the alignment of the cookie, if any. */
3376 9439 : if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
3377 6 : align = MAX (align, TYPE_ALIGN_UNIT (size_type_node));
3378 9439 : align_arg = build_int_cst (align_type_node, align);
3379 : }
3380 :
3381 1083345 : alloc_fn = NULL_TREE;
3382 :
3383 : /* If PLACEMENT is a single simple pointer type not passed by
3384 : reference, prepare to capture it in a temporary variable. Do
3385 : this now, since PLACEMENT will change in the calls below. */
3386 1083345 : placement_first = NULL_TREE;
3387 1083345 : if (vec_safe_length (*placement) == 1
3388 675407 : && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
3389 : placement_first = (**placement)[0];
3390 :
3391 1083345 : bool member_new_p = false;
3392 :
3393 : /* Allocate the object. */
3394 1083345 : tree fnname;
3395 1083345 : tree fns;
3396 :
3397 1083345 : fnname = ovl_op_identifier (false, array_p ? VEC_NEW_EXPR : NEW_EXPR);
3398 :
3399 2349594 : member_new_p = !globally_qualified_p
3400 182904 : && CLASS_TYPE_P (elt_type)
3401 1197787 : && (array_p
3402 114442 : ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
3403 92772 : : TYPE_HAS_NEW_OPERATOR (elt_type));
3404 :
3405 182904 : bool member_delete_p = (!globally_qualified_p
3406 182904 : && CLASS_TYPE_P (elt_type)
3407 114442 : && (array_p
3408 114442 : ? TYPE_GETS_VEC_DELETE (elt_type)
3409 92772 : : TYPE_GETS_REG_DELETE (elt_type)));
3410 :
3411 1083345 : if (member_new_p)
3412 : {
3413 : /* Use a class-specific operator new. */
3414 : /* If a cookie is required, add some extra space. */
3415 606 : if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
3416 8 : size = build2 (PLUS_EXPR, sizetype, size, cookie_size);
3417 : else
3418 : {
3419 598 : cookie_size = NULL_TREE;
3420 : /* No size arithmetic necessary, so the size check is
3421 : not needed. */
3422 598 : if (outer_nelts_check != NULL && inner_size == 1)
3423 1 : outer_nelts_check = NULL_TREE;
3424 : }
3425 : /* Perform the overflow check. */
3426 606 : tree errval = TYPE_MAX_VALUE (sizetype);
3427 606 : if (cxx_dialect >= cxx11 && flag_exceptions)
3428 419 : errval = throw_bad_array_new_length ();
3429 606 : if (outer_nelts_check != NULL_TREE)
3430 9 : size = build3 (COND_EXPR, sizetype, outer_nelts_check, size, errval);
3431 606 : size = fold_to_constant (size);
3432 : /* Create the argument list. */
3433 606 : vec_safe_insert (*placement, 0, size);
3434 : /* Do name-lookup to find the appropriate operator. */
3435 606 : fns = lookup_fnfields (elt_type, fnname, /*protect=*/2, complain);
3436 606 : if (fns == NULL_TREE)
3437 : {
3438 0 : if (complain & tf_error)
3439 0 : error ("no suitable %qD found in class %qT", fnname, elt_type);
3440 0 : return error_mark_node;
3441 : }
3442 606 : if (TREE_CODE (fns) == TREE_LIST)
3443 : {
3444 3 : if (complain & tf_error)
3445 : {
3446 3 : auto_diagnostic_group d;
3447 3 : error ("request for member %qD is ambiguous", fnname);
3448 3 : print_candidates (input_location, fns);
3449 3 : }
3450 3 : return error_mark_node;
3451 : }
3452 603 : tree dummy = build_dummy_object (elt_type);
3453 603 : alloc_call = NULL_TREE;
3454 603 : if (align_arg)
3455 : {
3456 0 : vec<tree, va_gc> *align_args
3457 0 : = vec_copy_and_insert (*placement, align_arg, 1);
3458 0 : alloc_call
3459 0 : = build_new_method_call (dummy, fns, &align_args,
3460 : /*conversion_path=*/NULL_TREE,
3461 : LOOKUP_NORMAL, &alloc_fn, tf_none);
3462 : /* If no matching function is found and the allocated object type
3463 : has new-extended alignment, the alignment argument is removed
3464 : from the argument list, and overload resolution is performed
3465 : again. */
3466 0 : if (alloc_call == error_mark_node)
3467 0 : alloc_call = NULL_TREE;
3468 : }
3469 0 : if (!alloc_call)
3470 603 : alloc_call = build_new_method_call (dummy, fns, placement,
3471 : /*conversion_path=*/NULL_TREE,
3472 : LOOKUP_NORMAL,
3473 : &alloc_fn, complain);
3474 : }
3475 : else
3476 : {
3477 : /* Use a global operator new. */
3478 : /* See if a cookie might be required. */
3479 1082739 : if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
3480 : {
3481 1082471 : cookie_size = NULL_TREE;
3482 : /* No size arithmetic necessary, so the size check is
3483 : not needed. */
3484 1082471 : if (outer_nelts_check != NULL && inner_size == 1)
3485 64132 : outer_nelts_check = NULL_TREE;
3486 : }
3487 :
3488 1082739 : size = fold_to_constant (size);
3489 : /* If size is zero e.g. due to type having zero size, try to
3490 : preserve outer_nelts for constant expression evaluation
3491 : purposes. */
3492 1082739 : if (integer_zerop (size) && outer_nelts)
3493 72 : size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts);
3494 :
3495 1082739 : alloc_call = build_operator_new_call (fnname, placement,
3496 : &size, &cookie_size,
3497 : align_arg, outer_nelts_check,
3498 : &alloc_fn, complain);
3499 : }
3500 :
3501 1083342 : if (alloc_call == error_mark_node)
3502 : return error_mark_node;
3503 :
3504 1083327 : gcc_assert (alloc_fn != NULL_TREE);
3505 :
3506 : /* Now, check to see if this function is actually a placement
3507 : allocation function. This can happen even when PLACEMENT is NULL
3508 : because we might have something like:
3509 :
3510 : struct S { void* operator new (size_t, int i = 0); };
3511 :
3512 : A call to `new S' will get this allocation function, even though
3513 : there is no explicit placement argument. If there is more than
3514 : one argument, or there are variable arguments, then this is a
3515 : placement allocation function. */
3516 1083327 : placement_allocation_fn_p
3517 1083327 : = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
3518 1083327 : || varargs_function_p (alloc_fn));
3519 :
3520 1083327 : if (complain & tf_warning_or_error
3521 629669 : && warn_aligned_new
3522 6416 : && !placement_allocation_fn_p
3523 2733 : && TYPE_ALIGN (elt_type) > malloc_alignment ()
3524 15 : && (warn_aligned_new > 1
3525 13 : || CP_DECL_CONTEXT (alloc_fn) == global_namespace)
3526 1083333 : && !aligned_allocation_fn_p (alloc_fn))
3527 : {
3528 6 : auto_diagnostic_group d;
3529 6 : if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended "
3530 6 : "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type)))
3531 : {
3532 4 : inform (input_location, "uses %qD, which does not have an alignment "
3533 : "parameter", alloc_fn);
3534 4 : if (!aligned_new_threshold)
3535 4 : inform (input_location, "use %<-faligned-new%> to enable C++17 "
3536 : "over-aligned new support");
3537 : }
3538 6 : }
3539 :
3540 : /* If we found a simple case of PLACEMENT_EXPR above, then copy it
3541 : into a temporary variable. */
3542 1083327 : if (!processing_template_decl
3543 985849 : && TREE_CODE (alloc_call) == CALL_EXPR
3544 985849 : && call_expr_nargs (alloc_call) == 2
3545 684653 : && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
3546 1767980 : && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
3547 : {
3548 674804 : tree placement = CALL_EXPR_ARG (alloc_call, 1);
3549 :
3550 674804 : if (placement_first != NULL_TREE
3551 674804 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))
3552 673849 : || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement)))))
3553 : {
3554 673829 : placement_expr = get_internal_target_expr (placement_first);
3555 673829 : CALL_EXPR_ARG (alloc_call, 1)
3556 1347658 : = fold_convert (TREE_TYPE (placement), placement_expr);
3557 : }
3558 :
3559 674804 : if (!member_new_p
3560 1349398 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1)))))
3561 : {
3562 : /* Attempt to make the warning point at the operator new argument. */
3563 674562 : if (placement_first)
3564 673634 : placement = placement_first;
3565 :
3566 674562 : warn_placement_new_too_small (orig_type, nelts, size, placement);
3567 : }
3568 : }
3569 :
3570 1083327 : alloc_expr = alloc_call;
3571 1083327 : if (cookie_size)
3572 228 : alloc_expr = maybe_wrap_new_for_constexpr (alloc_expr, type,
3573 : cookie_size);
3574 :
3575 1083327 : const bool std_placement = std_placement_new_fn_p (alloc_fn);
3576 :
3577 : /* Clobber the object now that the constructor won't do it in
3578 : start_preparsed_function. This is most important for activating an array
3579 : in a union (c++/121068), but should also help the optimizers. */
3580 1083327 : const bool do_clobber
3581 1083327 : = (flag_lifetime_dse > 1
3582 1083212 : && !processing_template_decl
3583 985742 : && !is_empty_type (elt_type)
3584 978414 : && !integer_zerop (TYPE_SIZE (type))
3585 978399 : && (!outer_nelts || !integer_zerop (cst_outer_nelts))
3586 2061681 : && (!*init || CLASS_TYPE_P (elt_type)));
3587 :
3588 : /* In the simple case, we can stop now. */
3589 1083327 : pointer_type = build_pointer_type (type);
3590 1083327 : if (!cookie_size && !is_initialized && !member_delete_p && !do_clobber)
3591 64776 : return build_nop (pointer_type, alloc_expr);
3592 :
3593 : /* Store the result of the allocation call in a variable so that we can
3594 : use it more than once. */
3595 1018551 : alloc_expr = get_internal_target_expr (alloc_expr);
3596 1018551 : alloc_node = TARGET_EXPR_SLOT (alloc_expr);
3597 :
3598 : /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
3599 1018551 : while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
3600 0 : alloc_call = TREE_OPERAND (alloc_call, 1);
3601 :
3602 : /* Preevaluate the placement args so that we don't reevaluate them for a
3603 : placement delete. */
3604 1018551 : if (placement_allocation_fn_p)
3605 : {
3606 684349 : tree inits;
3607 684349 : stabilize_call (alloc_call, &inits);
3608 684349 : if (inits)
3609 682927 : alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
3610 : alloc_expr);
3611 : }
3612 :
3613 : /* unless an allocation function is declared with an empty excep-
3614 : tion-specification (_except.spec_), throw(), it indicates failure to
3615 : allocate storage by throwing a bad_alloc exception (clause _except_,
3616 : _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
3617 : cation function is declared with an empty exception-specification,
3618 : throw(), it returns null to indicate failure to allocate storage and a
3619 : non-null pointer otherwise.
3620 :
3621 : So check for a null exception spec on the op new we just called. */
3622 :
3623 1018551 : nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
3624 1018551 : check_new = flag_check_new || (nothrow && !std_placement);
3625 :
3626 1018551 : if (cookie_size)
3627 : {
3628 228 : tree cookie;
3629 228 : tree cookie_ptr;
3630 228 : tree size_ptr_type;
3631 :
3632 : /* Adjust so we're pointing to the start of the object. */
3633 228 : data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
3634 :
3635 : /* Store the number of bytes allocated so that we can know how
3636 : many elements to destroy later. We use the last sizeof
3637 : (size_t) bytes to store the number of elements. */
3638 228 : cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
3639 228 : cookie_ptr = fold_build_pointer_plus_loc (input_location,
3640 : alloc_node, cookie_ptr);
3641 228 : size_ptr_type = build_pointer_type (sizetype);
3642 228 : cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
3643 228 : cookie = cp_build_fold_indirect_ref (cookie_ptr);
3644 :
3645 228 : cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
3646 :
3647 228 : if (targetm.cxx.cookie_has_size ())
3648 : {
3649 : /* Also store the element size. */
3650 0 : cookie_ptr = fold_build_pointer_plus (cookie_ptr,
3651 : fold_build1_loc (input_location,
3652 : NEGATE_EXPR, sizetype,
3653 : size_in_bytes (sizetype)));
3654 :
3655 0 : cookie = cp_build_fold_indirect_ref (cookie_ptr);
3656 0 : cookie = build2 (MODIFY_EXPR, sizetype, cookie,
3657 : size_in_bytes (elt_type));
3658 0 : cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
3659 : cookie, cookie_expr);
3660 : }
3661 : }
3662 : else
3663 : {
3664 : cookie_expr = NULL_TREE;
3665 : data_addr = alloc_node;
3666 : }
3667 :
3668 : /* Now use a pointer to the type we've actually allocated. */
3669 :
3670 : /* But we want to operate on a non-const version to start with,
3671 : since we'll be modifying the elements. */
3672 1018551 : non_const_pointer_type = build_pointer_type
3673 1018551 : (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
3674 :
3675 1018551 : data_addr = fold_convert (non_const_pointer_type, data_addr);
3676 : /* Any further uses of alloc_node will want this type, too. */
3677 1018551 : alloc_node = fold_convert (non_const_pointer_type, alloc_node);
3678 :
3679 1018551 : tree clobber_expr = NULL_TREE;
3680 1018551 : if (do_clobber)
3681 : {
3682 595183 : if (array_p && TREE_CODE (cst_outer_nelts) != INTEGER_CST)
3683 : {
3684 : /* Clobber each element rather than the array at once. */
3685 : /* But for now, limit a clobber loop to placement new during
3686 : constant-evaluation, as cddce1 thinks it might be infinite, leading
3687 : to bogus warnings on Wstringop-overflow-4.C (2025-09-30). We
3688 : need it in constexpr for constexpr-new4a.C. */
3689 35 : if (std_placement && current_function_decl
3690 11117 : && maybe_constexpr_fn (current_function_decl))
3691 : {
3692 2 : tree clobber = build_clobber (elt_type, CLOBBER_OBJECT_BEGIN);
3693 2 : CONSTRUCTOR_IS_DIRECT_INIT (clobber) = true;
3694 2 : tree maxindex = cp_build_binary_op (input_location,
3695 : MINUS_EXPR, outer_nelts,
3696 : integer_one_node,
3697 : complain);
3698 2 : clobber_expr = build_vec_init (data_addr, maxindex, clobber,
3699 : /*valinit*/false, /*from_arr*/0,
3700 : complain, nullptr);
3701 2 : clobber_expr = wrap_with_if_consteval (clobber_expr);
3702 : }
3703 : }
3704 : else
3705 : {
3706 2045 : tree targ = data_addr;
3707 2045 : tree ttype = type;
3708 : /* Clobber the array as a whole, except that for a one-element array
3709 : just clobber the element type, to avoid problems with code like
3710 : construct_at that uses new T[1] for array T to get a pointer to
3711 : the array. */
3712 2045 : if (array_p && !integer_onep (cst_outer_nelts))
3713 : {
3714 1677 : tree dom
3715 1677 : = compute_array_index_type (NULL_TREE,
3716 : const_cast<tree> (cst_outer_nelts),
3717 : complain);
3718 1677 : ttype = build_cplus_array_type (type, dom);
3719 1677 : tree ptype = build_pointer_type (ttype);
3720 1677 : targ = fold_convert (ptype, targ);
3721 : }
3722 584101 : targ = cp_build_fold_indirect_ref (targ);
3723 584101 : tree clobber = build_clobber (ttype, CLOBBER_OBJECT_BEGIN);
3724 584101 : CONSTRUCTOR_IS_DIRECT_INIT (clobber) = true;
3725 584101 : clobber_expr = cp_build_init_expr (targ, clobber);
3726 : }
3727 : }
3728 :
3729 : /* Now initialize the allocated object. Note that we preevaluate the
3730 : initialization expression, apart from the actual constructor call or
3731 : assignment--we do this because we want to delay the allocation as long
3732 : as possible in order to minimize the size of the exception region for
3733 : placement delete. */
3734 1018551 : if (is_initialized)
3735 : {
3736 853989 : bool explicit_value_init_p = false;
3737 :
3738 853989 : if (*init != NULL && (*init)->is_empty ())
3739 : {
3740 165297 : *init = NULL;
3741 165297 : explicit_value_init_p = true;
3742 : }
3743 :
3744 853989 : if (processing_template_decl)
3745 : {
3746 : /* Avoid an ICE when converting to a base in build_simple_base_path.
3747 : We'll throw this all away anyway, and build_new will create
3748 : a NEW_EXPR. */
3749 34360 : tree t = fold_convert (build_pointer_type (elt_type), data_addr);
3750 : /* build_value_init doesn't work in templates, and we don't need
3751 : the initializer anyway since we're going to throw it away and
3752 : rebuild it at instantiation time, so just build up a single
3753 : constructor call to get any appropriate diagnostics. */
3754 34360 : init_expr = cp_build_fold_indirect_ref (t);
3755 34360 : if (type_build_ctor_call (elt_type))
3756 34315 : init_expr = build_special_member_call (init_expr,
3757 : complete_ctor_identifier,
3758 : init, elt_type,
3759 : LOOKUP_NORMAL,
3760 : complain);
3761 : }
3762 819629 : else if (array_p)
3763 : {
3764 10363 : tree vecinit = NULL_TREE;
3765 10363 : const size_t len = vec_safe_length (*init);
3766 375 : if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0]))
3767 : {
3768 366 : vecinit = (**init)[0];
3769 366 : if (CONSTRUCTOR_NELTS (vecinit) == 0)
3770 : /* List-value-initialization, leave it alone. */;
3771 : else
3772 : {
3773 289 : tree arraytype, domain;
3774 289 : if (TREE_CONSTANT (nelts))
3775 276 : domain = compute_array_index_type (NULL_TREE, nelts,
3776 : complain);
3777 : else
3778 : /* We'll check the length at runtime. */
3779 : domain = NULL_TREE;
3780 289 : arraytype = build_cplus_array_type (type, domain);
3781 : /* If we have new char[4]{"foo"}, we have to reshape
3782 : so that the STRING_CST isn't wrapped in { }. */
3783 289 : vecinit = reshape_init (arraytype, vecinit, complain);
3784 : /* The middle end doesn't cope with the location wrapper
3785 : around a STRING_CST. */
3786 289 : STRIP_ANY_LOCATION_WRAPPER (vecinit);
3787 289 : vecinit = digest_init (arraytype, vecinit, complain);
3788 : }
3789 : }
3790 9997 : else if (*init)
3791 : {
3792 9 : if (complain & tf_error)
3793 8 : error ("parenthesized initializer in array new");
3794 9 : return error_mark_node;
3795 : }
3796 :
3797 : /* Collect flags for disabling subobject cleanups once the complete
3798 : object is fully constructed. */
3799 10354 : vec<tree, va_gc> *flags = make_tree_vector ();
3800 :
3801 10354 : init_expr
3802 10354 : = build_vec_init (data_addr,
3803 10354 : cp_build_binary_op (input_location,
3804 : MINUS_EXPR, outer_nelts,
3805 : integer_one_node,
3806 : complain),
3807 : vecinit,
3808 : explicit_value_init_p,
3809 : /*from_array=*/0,
3810 : complain,
3811 : &flags);
3812 :
3813 31304 : for (tree f : flags)
3814 : {
3815 242 : tree cl = build_disable_temp_cleanup (f);
3816 242 : cl = convert_to_void (cl, ICV_STATEMENT, complain);
3817 242 : init_expr = build2 (COMPOUND_EXPR, void_type_node,
3818 : init_expr, cl);
3819 : }
3820 10354 : release_tree_vector (flags);
3821 : }
3822 : else
3823 : {
3824 809266 : init_expr = cp_build_fold_indirect_ref (data_addr);
3825 :
3826 809266 : if (type_build_ctor_call (type) && !explicit_value_init_p)
3827 : {
3828 230171 : init_expr = build_special_member_call (init_expr,
3829 : complete_ctor_identifier,
3830 : init, elt_type,
3831 : LOOKUP_NORMAL,
3832 : complain|tf_no_cleanup);
3833 : }
3834 579095 : else if (explicit_value_init_p)
3835 : {
3836 : /* Something like `new int()'. NO_CLEANUP is needed so
3837 : we don't try and build a (possibly ill-formed)
3838 : destructor. */
3839 155692 : tree val = build_value_init (type, complain | tf_no_cleanup);
3840 155692 : if (val == error_mark_node)
3841 25 : return error_mark_node;
3842 155667 : init_expr = cp_build_init_expr (init_expr, val);
3843 : }
3844 : else
3845 : {
3846 423403 : tree ie;
3847 :
3848 : /* We are processing something like `new int (10)', which
3849 : means allocate an int, and initialize it with 10.
3850 :
3851 : In C++20, also handle `new A(1, 2)'. */
3852 423403 : if (cxx_dialect >= cxx20
3853 423019 : && AGGREGATE_TYPE_P (type)
3854 465602 : && (*init)->length () > 1)
3855 : {
3856 92 : ie = build_constructor_from_vec (init_list_type_node, *init);
3857 92 : CONSTRUCTOR_IS_DIRECT_INIT (ie) = true;
3858 92 : CONSTRUCTOR_IS_PAREN_INIT (ie) = true;
3859 92 : ie = digest_init (type, ie, complain);
3860 : }
3861 : else
3862 423311 : ie = build_x_compound_expr_from_vec (*init, "new initializer",
3863 : complain);
3864 423403 : init_expr = cp_build_modify_expr (input_location, init_expr,
3865 : INIT_EXPR, ie, complain);
3866 : }
3867 : /* If the initializer uses C++14 aggregate NSDMI that refer to the
3868 : object being initialized, replace them now and don't try to
3869 : preevaluate. */
3870 809241 : bool had_placeholder = false;
3871 809241 : if (!processing_template_decl
3872 809241 : && TREE_CODE (init_expr) == INIT_EXPR)
3873 587351 : TREE_OPERAND (init_expr, 1)
3874 1174702 : = replace_placeholders (TREE_OPERAND (init_expr, 1),
3875 587351 : TREE_OPERAND (init_expr, 0),
3876 : &had_placeholder);
3877 : }
3878 :
3879 853955 : if (init_expr == error_mark_node)
3880 : return error_mark_node;
3881 : }
3882 : else
3883 : init_expr = NULL_TREE;
3884 :
3885 : /* If any part of the object initialization terminates by throwing an
3886 : exception and a suitable deallocation function can be found, the
3887 : deallocation function is called to free the memory in which the
3888 : object was being constructed, after which the exception continues
3889 : to propagate in the context of the new-expression. If no
3890 : unambiguous matching deallocation function can be found,
3891 : propagating the exception does not cause the object's memory to be
3892 : freed. */
3893 1018287 : if (flag_exceptions && (init_expr || member_delete_p))
3894 : {
3895 853079 : enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
3896 853079 : tree cleanup;
3897 :
3898 : /* The Standard is unclear here, but the right thing to do
3899 : is to use the same method for finding deallocation
3900 : functions that we use for finding allocation functions. */
3901 853079 : cleanup
3902 853079 : = build_op_delete_call (dcode, alloc_node, size, globally_qualified_p,
3903 : placement_allocation_fn_p
3904 : ? alloc_call : NULL_TREE,
3905 : placement_allocation_fn_p ? *placement : NULL,
3906 : alloc_fn, complain);
3907 :
3908 853079 : if (cleanup && init_expr && !processing_template_decl)
3909 : /* Ack! First we allocate the memory. Then we set our sentry
3910 : variable to true, and expand a cleanup that deletes the
3911 : memory if sentry is true. Then we run the constructor, and
3912 : finally clear the sentry.
3913 :
3914 : We need to do this because we allocate the space first, so
3915 : if there are any temporaries with cleanups in the
3916 : constructor args, we need this EH region to extend until
3917 : end of full-expression to preserve nesting.
3918 :
3919 : We used to try to evaluate the args first to avoid this, but
3920 : since C++17 [expr.new] says that "The invocation of the
3921 : allocation function is sequenced before the evaluations of
3922 : expressions in the new-initializer." */
3923 : {
3924 817713 : tree end, sentry, begin;
3925 :
3926 817713 : begin = get_internal_target_expr (boolean_true_node);
3927 :
3928 817713 : sentry = TARGET_EXPR_SLOT (begin);
3929 :
3930 : /* CLEANUP is compiler-generated, so no diagnostics. */
3931 817713 : suppress_warning (cleanup);
3932 :
3933 817713 : TARGET_EXPR_CLEANUP (begin)
3934 817713 : = build3 (COND_EXPR, void_type_node, sentry,
3935 : cleanup, void_node);
3936 :
3937 817713 : end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
3938 : sentry, boolean_false_node);
3939 :
3940 817713 : init_expr
3941 817713 : = build2 (COMPOUND_EXPR, void_type_node, begin,
3942 : build2 (COMPOUND_EXPR, void_type_node, init_expr,
3943 : end));
3944 : /* Likewise, this is compiler-generated. */
3945 817713 : suppress_warning (init_expr);
3946 : }
3947 : }
3948 :
3949 : /* Now build up the return value in reverse order. */
3950 :
3951 853922 : rval = data_addr;
3952 :
3953 853922 : if (init_expr)
3954 853725 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
3955 1018287 : if (clobber_expr)
3956 584029 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), clobber_expr, rval);
3957 1018287 : if (cookie_expr)
3958 228 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
3959 :
3960 1018287 : suppress_warning (rval, OPT_Wunused_value);
3961 :
3962 1018287 : if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
3963 : /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
3964 : and return the call (which doesn't need to be adjusted). */
3965 1722 : rval = TARGET_EXPR_INITIAL (alloc_expr);
3966 : else
3967 : {
3968 : /* Skip the null-check when rval == data_addr: the resulting conditional
3969 : would be "alloc_node != nullptr ? alloc_node : alloc_node", triggering
3970 : -Wduplicated-branches (PR125422). */
3971 1016565 : if (rval == data_addr)
3972 : check_new = 0;
3973 :
3974 1016517 : if (check_new)
3975 : {
3976 240 : tree ifexp = cp_build_binary_op (input_location,
3977 : NE_EXPR, alloc_node,
3978 : nullptr_node,
3979 : complain);
3980 240 : rval = build_conditional_expr (input_location, ifexp, rval,
3981 : alloc_node, complain);
3982 : /* If there's no offset between data_addr and alloc_node, append it
3983 : to help -Wmismatched-new-delete at -O0. */
3984 240 : if (!cookie_size)
3985 228 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_node),
3986 : rval, alloc_node);
3987 : }
3988 :
3989 : /* Perform the allocation before anything else, so that ALLOC_NODE
3990 : has been initialized before we start using it. */
3991 1016565 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
3992 : }
3993 :
3994 : /* A new-expression is never an lvalue. */
3995 1018287 : gcc_assert (!obvalue_p (rval));
3996 :
3997 1018287 : return convert (pointer_type, rval);
3998 : }
3999 :
4000 : /* Generate a representation for a C++ "new" expression. *PLACEMENT
4001 : is a vector of placement-new arguments (or NULL if none). If NELTS
4002 : is NULL, TYPE is the type of the storage to be allocated. If NELTS
4003 : is not NULL, then this is an array-new allocation; TYPE is the type
4004 : of the elements in the array and NELTS is the number of elements in
4005 : the array. *INIT, if non-NULL, is the initializer for the new
4006 : object, or an empty vector to indicate an initializer of "()". If
4007 : USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
4008 : rather than just "new". This may change PLACEMENT and INIT. */
4009 :
4010 : tree
4011 1920425 : build_new (location_t loc, vec<tree, va_gc> **placement, tree type,
4012 : tree nelts, vec<tree, va_gc> **init, int use_global_new,
4013 : tsubst_flags_t complain)
4014 : {
4015 1920425 : tree rval;
4016 1920425 : vec<tree, va_gc> *orig_placement = NULL;
4017 1920425 : tree orig_nelts = NULL_TREE;
4018 1920425 : vec<tree, va_gc> *orig_init = NULL;
4019 :
4020 1920425 : if (type == error_mark_node)
4021 : return error_mark_node;
4022 :
4023 1920206 : if (nelts == NULL_TREE
4024 : /* Don't do auto deduction where it might affect mangling. */
4025 1920206 : && (!processing_template_decl || at_function_scope_p ()))
4026 : {
4027 1624151 : tree auto_node = type_uses_auto (type);
4028 1624151 : if (auto_node)
4029 : {
4030 108 : tree d_init = NULL_TREE;
4031 108 : const size_t len = vec_safe_length (*init);
4032 : /* E.g. new auto(x) must have exactly one element, or
4033 : a {} initializer will have one element. */
4034 99 : if (len == 1)
4035 : {
4036 84 : d_init = (**init)[0];
4037 84 : d_init = resolve_nondeduced_context (d_init, complain);
4038 : }
4039 : /* For the rest, e.g. new A(1, 2, 3), create a list. */
4040 24 : else if (len > 1)
4041 : {
4042 : unsigned int n;
4043 : tree t;
4044 : tree *pp = &d_init;
4045 21 : FOR_EACH_VEC_ELT (**init, n, t)
4046 : {
4047 15 : t = resolve_nondeduced_context (t, complain);
4048 15 : *pp = build_tree_list (NULL_TREE, t);
4049 15 : pp = &TREE_CHAIN (*pp);
4050 : }
4051 : }
4052 108 : type = do_auto_deduction (type, d_init, auto_node, complain);
4053 : }
4054 : }
4055 :
4056 1920206 : if (processing_template_decl)
4057 : {
4058 933491 : if (dependent_type_p (type)
4059 107549 : || any_type_dependent_arguments_p (*placement)
4060 107546 : || (nelts && type_dependent_expression_p (nelts))
4061 74012 : || (nelts && *init)
4062 1031063 : || any_type_dependent_arguments_p (*init))
4063 835989 : return build_raw_new_expr (loc, *placement, type, nelts, *init,
4064 835989 : use_global_new);
4065 :
4066 97502 : orig_placement = make_tree_vector_copy (*placement);
4067 97502 : orig_nelts = nelts;
4068 97502 : if (*init)
4069 : {
4070 23435 : orig_init = make_tree_vector_copy (*init);
4071 : /* Also copy any CONSTRUCTORs in *init, since reshape_init and
4072 : digest_init clobber them in place. */
4073 93598 : for (unsigned i = 0; i < orig_init->length(); ++i)
4074 : {
4075 46728 : tree e = (**init)[i];
4076 46728 : if (TREE_CODE (e) == CONSTRUCTOR)
4077 21 : (**init)[i] = copy_node (e);
4078 : }
4079 : }
4080 : }
4081 :
4082 1084217 : if (nelts)
4083 : {
4084 88592 : location_t nelts_loc = cp_expr_loc_or_loc (nelts, loc);
4085 88592 : if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
4086 : {
4087 9 : if (complain & tf_error)
4088 6 : permerror (nelts_loc,
4089 : "size in array new must have integral type");
4090 : else
4091 3 : return error_mark_node;
4092 : }
4093 :
4094 : /* Try to determine the constant value only for the purposes
4095 : of the diagnostic below but continue to use the original
4096 : value and handle const folding later. */
4097 88589 : const_tree cst_nelts = fold_non_dependent_expr (nelts, complain);
4098 :
4099 : /* The expression in a noptr-new-declarator is erroneous if it's of
4100 : non-class type and its value before converting to std::size_t is
4101 : less than zero. ... If the expression is a constant expression,
4102 : the program is ill-formed. */
4103 88589 : if (TREE_CODE (cst_nelts) == INTEGER_CST
4104 88589 : && !valid_array_size_p (nelts_loc, cst_nelts, NULL_TREE,
4105 : complain & tf_error))
4106 324 : return error_mark_node;
4107 :
4108 88265 : nelts = mark_rvalue_use (nelts);
4109 88265 : nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
4110 : }
4111 :
4112 : /* ``A reference cannot be created by the new operator. A reference
4113 : is not an object (8.2.2, 8.4.3), so a pointer to it could not be
4114 : returned by new.'' ARM 5.3.3 */
4115 1083890 : if (TYPE_REF_P (type))
4116 : {
4117 60 : if (complain & tf_error)
4118 12 : error_at (loc, "new cannot be applied to a reference type");
4119 : else
4120 48 : return error_mark_node;
4121 12 : type = TREE_TYPE (type);
4122 : }
4123 :
4124 1083842 : if (TREE_CODE (type) == FUNCTION_TYPE)
4125 : {
4126 21 : if (complain & tf_error)
4127 3 : error_at (loc, "new cannot be applied to a function type");
4128 21 : return error_mark_node;
4129 : }
4130 :
4131 : /* P1009: Array size deduction in new-expressions. */
4132 1083821 : const bool array_p = TREE_CODE (type) == ARRAY_TYPE;
4133 1083821 : if (*init
4134 : /* If the array didn't specify its bound, we have to deduce it. */
4135 1083821 : && ((array_p && !TYPE_DOMAIN (type))
4136 : /* For C++20 array with parenthesized-init, we have to process
4137 : the parenthesized-list. But don't do it for (), which is
4138 : value-initialization, and INIT should stay empty. */
4139 757843 : || (cxx_dialect >= cxx20
4140 754627 : && (array_p || nelts)
4141 9736 : && !(*init)->is_empty ())))
4142 : {
4143 : /* This means we have 'new T[]()'. */
4144 335 : if ((*init)->is_empty ())
4145 : {
4146 9 : tree ctor = build_constructor (init_list_type_node, NULL);
4147 9 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
4148 9 : vec_safe_push (*init, ctor);
4149 : }
4150 335 : tree &elt = (**init)[0];
4151 : /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */
4152 335 : if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20)
4153 : {
4154 74 : tree ctor = build_constructor_from_vec (init_list_type_node, *init);
4155 74 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
4156 74 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
4157 74 : elt = ctor;
4158 : /* We've squashed all the vector elements into the first one;
4159 : truncate the rest. */
4160 74 : (*init)->truncate (1);
4161 : }
4162 : /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */
4163 335 : if (array_p && !TYPE_DOMAIN (type))
4164 : {
4165 : /* We need to reshape before deducing the bounds to handle code like
4166 :
4167 : struct S { int x, y; };
4168 : new S[]{1, 2, 3, 4};
4169 :
4170 : which should deduce S[2]. But don't change ELT itself: we want to
4171 : pass a list-initializer to build_new_1, even for STRING_CSTs. */
4172 143 : tree e = elt;
4173 143 : if (BRACE_ENCLOSED_INITIALIZER_P (e))
4174 140 : e = reshape_init (type, e, complain);
4175 143 : cp_complete_array_type (&type, e, /*do_default*/false);
4176 : }
4177 : }
4178 :
4179 : /* The type allocated must be complete. If the new-type-id was
4180 : "T[N]" then we are just checking that "T" is complete here, but
4181 : that is equivalent, since the value of "N" doesn't matter. */
4182 1083821 : if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
4183 63 : return error_mark_node;
4184 :
4185 1083758 : rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
4186 1083758 : if (rval == error_mark_node)
4187 : return error_mark_node;
4188 :
4189 1083063 : if (processing_template_decl)
4190 : {
4191 97472 : tree ret = build_raw_new_expr (loc, orig_placement, type, orig_nelts,
4192 : orig_init, use_global_new);
4193 97472 : release_tree_vector (orig_placement);
4194 97472 : release_tree_vector (orig_init);
4195 97472 : return ret;
4196 : }
4197 :
4198 : /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
4199 985591 : rval = build1_loc (loc, NOP_EXPR, TREE_TYPE (rval), rval);
4200 985591 : suppress_warning (rval, OPT_Wunused_value);
4201 :
4202 985591 : return rval;
4203 : }
4204 :
4205 : static tree
4206 28983 : build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
4207 : special_function_kind auto_delete_vec,
4208 : int use_global_delete, tsubst_flags_t complain,
4209 : bool in_cleanup = false)
4210 : {
4211 28983 : tree virtual_size;
4212 28983 : tree ptype = build_pointer_type (type = complete_type (type));
4213 28983 : tree size_exp;
4214 :
4215 : /* Temporary variables used by the loop. */
4216 28983 : tree tbase, tbase_init;
4217 :
4218 : /* This is the body of the loop that implements the deletion of a
4219 : single element, and moves temp variables to next elements. */
4220 28983 : tree body;
4221 :
4222 : /* This is the LOOP_EXPR that governs the deletion of the elements. */
4223 28983 : tree loop = 0;
4224 :
4225 : /* This is the thing that governs what to do after the loop has run. */
4226 28983 : tree deallocate_expr = 0;
4227 :
4228 : /* This is the BIND_EXPR which holds the outermost iterator of the
4229 : loop. It is convenient to set this variable up and test it before
4230 : executing any other code in the loop.
4231 : This is also the containing expression returned by this function. */
4232 28983 : tree controller = NULL_TREE;
4233 28983 : tree tmp;
4234 :
4235 : /* We should only have 1-D arrays here. */
4236 28983 : gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
4237 :
4238 28983 : if (base == error_mark_node || maxindex == error_mark_node)
4239 : return error_mark_node;
4240 :
4241 28983 : if (!verify_type_context (loc, TCTX_DEALLOCATION, type,
4242 28983 : !(complain & tf_error)))
4243 0 : return error_mark_node;
4244 :
4245 28983 : if (!COMPLETE_TYPE_P (type))
4246 : {
4247 27 : if (cxx_dialect > cxx23)
4248 : {
4249 21 : if (complain & tf_error)
4250 : {
4251 21 : auto_diagnostic_group d;
4252 21 : int saved_errorcount = errorcount;
4253 21 : if (permerror_opt (loc, OPT_Wdelete_incomplete,
4254 : "operator %<delete []%> used on "
4255 : "incomplete type"))
4256 : {
4257 14 : cxx_incomplete_type_inform (type);
4258 14 : if (errorcount != saved_errorcount)
4259 8 : return error_mark_node;
4260 : }
4261 21 : }
4262 : else
4263 0 : return error_mark_node;
4264 : }
4265 6 : else if (complain & tf_warning)
4266 : {
4267 6 : auto_diagnostic_group d;
4268 6 : if (warning_at (loc, OPT_Wdelete_incomplete,
4269 : "possible problem detected in invocation of "
4270 : "operator %<delete []%>"))
4271 : {
4272 4 : cxx_incomplete_type_diagnostic (base, type,
4273 : diagnostics::kind::warning);
4274 4 : inform (loc, "neither the destructor nor the "
4275 : "class-specific operator %<delete []%> will be called, "
4276 : "even if they are declared when the class is defined");
4277 : }
4278 6 : }
4279 : /* This size won't actually be used. */
4280 19 : size_exp = size_one_node;
4281 19 : goto no_destructor;
4282 : }
4283 :
4284 28956 : size_exp = size_in_bytes (type);
4285 :
4286 28956 : if (! MAYBE_CLASS_TYPE_P (type))
4287 2607 : goto no_destructor;
4288 26349 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
4289 : {
4290 : /* Make sure the destructor is callable. */
4291 19247 : if (type_build_dtor_call (type))
4292 : {
4293 60 : tmp = build_delete (loc, ptype, base, sfk_complete_destructor,
4294 : LOOKUP_NORMAL|LOOKUP_DESTRUCTOR|LOOKUP_NONVIRTUAL,
4295 : 1, complain);
4296 60 : if (tmp == error_mark_node)
4297 : return error_mark_node;
4298 : }
4299 19235 : goto no_destructor;
4300 : }
4301 :
4302 : /* The below is short by the cookie size. */
4303 7102 : virtual_size = size_binop (MULT_EXPR, size_exp,
4304 : fold_convert (sizetype, maxindex));
4305 :
4306 7102 : tbase = create_temporary_var (ptype);
4307 7102 : DECL_INITIAL (tbase)
4308 7102 : = fold_build_pointer_plus_loc (loc, fold_convert (ptype, base),
4309 : virtual_size);
4310 7102 : tbase_init = build_stmt (loc, DECL_EXPR, tbase);
4311 7102 : controller = build3 (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
4312 7102 : TREE_SIDE_EFFECTS (controller) = 1;
4313 7102 : BIND_EXPR_VEC_DTOR (controller) = true;
4314 :
4315 7102 : body = build1 (EXIT_EXPR, void_type_node,
4316 : build2 (EQ_EXPR, boolean_type_node, tbase,
4317 : fold_convert (ptype, base)));
4318 7102 : tmp = fold_build1_loc (loc, NEGATE_EXPR, sizetype, size_exp);
4319 7102 : tmp = fold_build_pointer_plus (tbase, tmp);
4320 7102 : tmp = cp_build_modify_expr (loc, tbase, NOP_EXPR, tmp, complain);
4321 7102 : if (tmp == error_mark_node)
4322 : return error_mark_node;
4323 7102 : body = build_compound_expr (loc, body, tmp);
4324 : /* [expr.delete]/3: "In an array delete expression, if the dynamic type of
4325 : the object to be deleted is not similar to its static type, the behavior
4326 : is undefined." So we can set LOOKUP_NONVIRTUAL. */
4327 7102 : tmp = build_delete (loc, ptype, tbase, sfk_complete_destructor,
4328 : LOOKUP_NORMAL|LOOKUP_DESTRUCTOR|LOOKUP_NONVIRTUAL,
4329 : 1, complain);
4330 7102 : if (tmp == error_mark_node)
4331 : return error_mark_node;
4332 7102 : body = build_compound_expr (loc, body, tmp);
4333 :
4334 7102 : loop = build1 (LOOP_EXPR, void_type_node, body);
4335 :
4336 : /* If one destructor throws, keep trying to clean up the rest, unless we're
4337 : already in a build_vec_init cleanup. */
4338 7091 : if (flag_exceptions && !in_cleanup && !processing_template_decl
4339 12627 : && !expr_noexcept_p (tmp, tf_none))
4340 : {
4341 150 : loop = build2 (TRY_CATCH_EXPR, void_type_node, loop,
4342 : unshare_expr (loop));
4343 : /* Tell honor_protect_cleanup_actions to discard this on the
4344 : exceptional path. */
4345 150 : TRY_CATCH_IS_CLEANUP (loop) = true;
4346 : }
4347 :
4348 7102 : loop = build_compound_expr (loc, tbase_init, loop);
4349 :
4350 28963 : no_destructor:
4351 : /* Delete the storage if appropriate. */
4352 28963 : if (auto_delete_vec == sfk_deleting_destructor)
4353 : {
4354 21954 : tree base_tbd;
4355 :
4356 : /* The below is short by the cookie size. */
4357 21954 : virtual_size = size_binop (MULT_EXPR, size_exp,
4358 : fold_convert (sizetype, maxindex));
4359 :
4360 21954 : if (! TYPE_VEC_NEW_USES_COOKIE (type))
4361 : /* no header */
4362 : base_tbd = base;
4363 : else
4364 : {
4365 144 : tree cookie_size;
4366 :
4367 144 : cookie_size = targetm.cxx.get_cookie_size (type);
4368 144 : base_tbd = cp_build_binary_op (loc,
4369 : MINUS_EXPR,
4370 : cp_convert (string_type_node,
4371 : base, complain),
4372 : cookie_size,
4373 : complain);
4374 144 : if (base_tbd == error_mark_node)
4375 : return error_mark_node;
4376 144 : base_tbd = cp_convert (ptype, base_tbd, complain);
4377 : /* True size with header. */
4378 144 : virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
4379 : }
4380 :
4381 21954 : deallocate_expr
4382 21954 : = build_op_delete_call (VEC_DELETE_EXPR, base_tbd, virtual_size,
4383 : use_global_delete & 1, /*placement=*/NULL_TREE,
4384 : /*placement_args=*/NULL,
4385 : /*alloc_fn=*/NULL_TREE, complain);
4386 : }
4387 :
4388 28963 : body = loop;
4389 28963 : if (deallocate_expr == error_mark_node)
4390 : return error_mark_node;
4391 28963 : else if (!deallocate_expr)
4392 : ;
4393 21954 : 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 28963 : if (!body)
4401 48 : body = integer_zero_node;
4402 :
4403 : /* Outermost wrapper: If pointer is null, punt. */
4404 28963 : tree cond = build2_loc (loc, NE_EXPR, boolean_type_node, base,
4405 28963 : 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 28963 : suppress_warning (cond, OPT_Wnonnull_compare);
4409 28963 : body = build3_loc (loc, COND_EXPR, void_type_node,
4410 : cond, body, integer_zero_node);
4411 28963 : COND_EXPR_IS_VEC_DELETE (body) = true;
4412 28963 : body = build1 (NOP_EXPR, void_type_node, body);
4413 :
4414 28963 : if (controller)
4415 : {
4416 7102 : TREE_OPERAND (controller, 1) = body;
4417 7102 : body = controller;
4418 : }
4419 :
4420 28963 : 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 28963 : return convert_to_void (body, ICV_CAST, complain);
4425 : }
4426 :
4427 : /* Create an unnamed variable of the indicated TYPE. */
4428 :
4429 : tree
4430 225576 : create_temporary_var (tree type)
4431 : {
4432 225576 : tree decl;
4433 :
4434 225576 : decl = build_decl (input_location,
4435 : VAR_DECL, NULL_TREE, type);
4436 225576 : TREE_USED (decl) = 1;
4437 225576 : DECL_ARTIFICIAL (decl) = 1;
4438 225576 : DECL_IGNORED_P (decl) = 1;
4439 225576 : DECL_CONTEXT (decl) = current_function_decl;
4440 :
4441 225576 : 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 62897 : get_temp_regvar (tree type, tree init)
4453 : {
4454 62897 : tree decl;
4455 :
4456 62897 : decl = create_temporary_var (type);
4457 62897 : add_decl_expr (decl);
4458 :
4459 62897 : finish_expr_stmt (cp_build_modify_expr (input_location, decl, INIT_EXPR,
4460 : init, tf_warning_or_error));
4461 :
4462 62897 : 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 13 : if (flag_exceptions)
4487 : {
4488 13 : tree c = fold_build2 (LT_EXPR, boolean_type_node, iterator,
4489 : nelts);
4490 13 : c = build3 (COND_EXPR, void_type_node, c,
4491 : throw_bad_array_new_length (), void_node);
4492 13 : finish_expr_stmt (c);
4493 : }
4494 : /* Don't check an array new when -fno-exceptions. */
4495 : }
4496 62 : else if (sanitize_flags_p (SANITIZE_BOUNDS)
4497 62 : && 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 2799 : find_temps_r (tree *tp, int *walk_subtrees, void *data)
4510 : {
4511 2799 : vec<tree*> &temps = *static_cast<auto_vec<tree*> *>(data);
4512 2799 : tree t = *tp;
4513 2799 : if (TREE_CODE (t) == TARGET_EXPR
4514 2799 : && !TARGET_EXPR_ELIDING_P (t))
4515 65 : temps.safe_push (tp);
4516 2734 : else if (TYPE_P (t))
4517 0 : *walk_subtrees = 0;
4518 :
4519 2799 : 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 69319 : find_allocator_temps_r (tree *tp, int *walk_subtrees, void *data)
4527 : {
4528 69319 : vec<tree*> &temps = *static_cast<auto_vec<tree*> *>(data);
4529 69319 : tree t = *tp;
4530 69319 : if (TYPE_P (t))
4531 : {
4532 46 : *walk_subtrees = 0;
4533 46 : 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 69273 : tree fn = cp_get_callee_fndecl_nofold (t);
4539 14870 : if (fn && DECL_CONSTRUCTOR_P (fn)
4540 72909 : && decl_in_std_namespace_p (TYPE_NAME (DECL_CONTEXT (fn))))
4541 : {
4542 1855 : int nargs = call_expr_nargs (t);
4543 4260 : for (int i = 1; i < nargs; ++i)
4544 : {
4545 2405 : tree arg = get_nth_callarg (t, i);
4546 2405 : tree atype = TREE_TYPE (arg);
4547 2405 : if (TREE_CODE (atype) == REFERENCE_TYPE
4548 2405 : && is_std_allocator (TREE_TYPE (atype)))
4549 : {
4550 655 : STRIP_NOPS (arg);
4551 655 : if (TREE_CODE (arg) == ADDR_EXPR)
4552 : {
4553 655 : tree *ap = &TREE_OPERAND (arg, 0);
4554 655 : if (TREE_CODE (*ap) == TARGET_EXPR)
4555 655 : 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 3103 : combine_allocator_temps (tree &init, tree &alloc_obj)
4581 : {
4582 3103 : auto_vec<tree*> temps;
4583 3103 : cp_walk_tree_without_duplicates (&init, find_allocator_temps_r, &temps);
4584 5024 : for (tree *p : temps)
4585 : {
4586 655 : if (!alloc_obj)
4587 190 : alloc_obj = *p;
4588 : else
4589 465 : *p = alloc_obj;
4590 : }
4591 3103 : }
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 23905 : 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 23905 : tree rval;
4622 23905 : tree base2 = NULL_TREE;
4623 23905 : tree itype = NULL_TREE;
4624 23905 : tree iterator;
4625 : /* The type of BASE. */
4626 23905 : tree atype = TREE_TYPE (base);
4627 : /* The type of an element in the array. */
4628 23905 : tree type = TREE_TYPE (atype);
4629 : /* The element type reached after removing all outer array
4630 : types. */
4631 23905 : tree inner_elt_type;
4632 : /* The type of a pointer to an element in the array. */
4633 23905 : tree ptype;
4634 23905 : tree stmt_expr;
4635 23905 : tree compound_stmt;
4636 23905 : int destroy_temps;
4637 23905 : HOST_WIDE_INT num_initialized_elts = 0;
4638 23905 : bool is_global;
4639 23905 : tree obase = base;
4640 23905 : bool xvalue = false;
4641 23905 : bool errors = false;
4642 23905 : location_t loc = (init ? cp_expr_loc_or_input_loc (init)
4643 11632 : : location_of (base));
4644 :
4645 23905 : if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
4646 13549 : maxindex = array_type_nelts_minus_one (atype);
4647 :
4648 23905 : if (maxindex == NULL_TREE || maxindex == error_mark_node)
4649 0 : return error_mark_node;
4650 :
4651 23905 : maxindex = maybe_constant_value (maxindex);
4652 23905 : if (explicit_value_init_p)
4653 9743 : gcc_assert (!init);
4654 :
4655 23905 : inner_elt_type = strip_array_types (type);
4656 :
4657 : /* Look through the TARGET_EXPR around a compound literal. */
4658 12273 : if (init && TREE_CODE (init) == TARGET_EXPR
4659 152 : && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
4660 147 : && from_array != 2
4661 24052 : && (same_type_ignoring_top_level_qualifiers_p
4662 147 : (TREE_TYPE (init), atype)))
4663 24 : init = TARGET_EXPR_INITIAL (init);
4664 :
4665 23905 : if (tree vi = get_vec_init_expr (init))
4666 5 : init = VEC_INIT_EXPR_INIT (vi);
4667 :
4668 23905 : bool direct_init = false;
4669 11640 : if (from_array && init && BRACE_ENCLOSED_INITIALIZER_P (init)
4670 23956 : && 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 23905 : if (init && TREE_CODE (init) == CONSTRUCTOR)
4683 : 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 10847 : bool length_check = (init
4688 12273 : && (TREE_CODE (init) == STRING_CST
4689 12191 : || (TREE_CODE (init) == CONSTRUCTOR
4690 1426 : && CONSTRUCTOR_NELTS (init) > 0))
4691 1261 : && !TREE_CONSTANT (maxindex));
4692 :
4693 : if (init
4694 12273 : && TREE_CODE (atype) == ARRAY_TYPE
4695 11905 : && TREE_CONSTANT (maxindex)
4696 11820 : && !vla_type_p (type)
4697 40 : && (from_array == 2
4698 40 : ? vec_copy_assign_is_trivial (inner_elt_type, init)
4699 11762 : : !TYPE_NEEDS_CONSTRUCTING (type))
4700 22256 : && ((TREE_CODE (init) == CONSTRUCTOR
4701 17 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
4702 14 : || (same_type_ignoring_top_level_qualifiers_p
4703 14 : (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 17 : && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
4707 14 : || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
4708 10443 : || 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 10448 : if (BRACE_ENCLOSED_INITIALIZER_P (init))
4715 3 : init = digest_init (atype, init, complain);
4716 10448 : stmt_expr = cp_build_init_expr (base, init);
4717 10448 : return stmt_expr;
4718 : }
4719 :
4720 13457 : maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
4721 13457 : maxindex = fold_simple (maxindex);
4722 :
4723 13457 : if (TREE_CODE (atype) == ARRAY_TYPE)
4724 : {
4725 3101 : ptype = build_pointer_type (type);
4726 3101 : base = decay_conversion (base, complain);
4727 3101 : if (base == error_mark_node)
4728 : return error_mark_node;
4729 3101 : base = cp_convert (ptype, base, complain);
4730 : }
4731 : else
4732 : ptype = atype;
4733 :
4734 13457 : if (integer_all_onesp (maxindex))
4735 : {
4736 : /* Shortcut zero element case to avoid unneeded constructor synthesis. */
4737 52 : if (init && TREE_SIDE_EFFECTS (init))
4738 0 : base = build2 (COMPOUND_EXPR, ptype, init, base);
4739 : 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 13405 : is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
4773 13405 : destroy_temps = stmts_are_full_exprs_p ();
4774 13405 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
4775 13405 : rval = get_temp_regvar (ptype, base);
4776 13405 : base = get_temp_regvar (ptype, rval);
4777 13405 : tree iterator_targ = get_internal_target_expr (maxindex);
4778 13405 : add_stmt (iterator_targ);
4779 13405 : 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 13405 : 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 13405 : if (flag_exceptions
4807 : /* And don't clean up from clobbers, the actual initialization will
4808 : follow as a separate build_vec_init. */
4809 13342 : && !(init && TREE_CLOBBER_P (init))
4810 13340 : && from_array != 2
4811 26707 : && type_build_dtor_call (type))
4812 : {
4813 1577 : tree e;
4814 1577 : 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 1577 : if (TREE_CODE (type) == ARRAY_TYPE)
4821 212 : m = cp_build_binary_op (input_location,
4822 : MULT_EXPR, m,
4823 : /* Avoid mixing signed and unsigned. */
4824 106 : convert (TREE_TYPE (m),
4825 : array_type_nelts_total (type)),
4826 : complain);
4827 :
4828 1577 : 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 1577 : if (e == error_mark_node)
4833 : errors = true;
4834 1571 : else if (TREE_SIDE_EFFECTS (e))
4835 : {
4836 1563 : TARGET_EXPR_CLEANUP (iterator_targ) = e;
4837 1563 : 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 1563 : if (cleanup_flags)
4853 1992 : vec_safe_push (*cleanup_flags,
4854 996 : build_tree_list (rval, build_zero_cst (ptype)));
4855 : }
4856 : }
4857 :
4858 : /* Should we try to create a constant initializer? */
4859 13405 : bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
4860 3086 : && TREE_CONSTANT (maxindex)
4861 2991 : && (init ? TREE_CODE (init) == CONSTRUCTOR
4862 : : (type_has_constexpr_default_constructor
4863 1619 : (inner_elt_type)
4864 : /* Value-initialization of scalars is constexpr. */
4865 1165 : || (explicit_value_init_p
4866 85 : && SCALAR_TYPE_P (inner_elt_type))))
4867 17958 : && (literal_type_p (inner_elt_type)
4868 737 : || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
4869 13405 : vec<constructor_elt, va_gc> *const_vec = NULL;
4870 13405 : 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 13405 : bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
4875 :
4876 13405 : if (init
4877 1804 : && TREE_CODE (init) == CONSTRUCTOR
4878 1394 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4879 1069 : && INTEGRAL_TYPE_P (type)
4880 13567 : && 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 162 : tree new_init = braced_lists_to_strings (TREE_TYPE (init), init);
4889 162 : if (!try_const || TREE_CODE (new_init) == CONSTRUCTOR)
4890 : init = new_init;
4891 : }
4892 :
4893 15209 : bool empty_list = false;
4894 1804 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
4895 13728 : && 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 13204 : 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 1184 : 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 1184 : unsigned int raw_idx = -1;
4911 1184 : 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 1184 : bool digested = (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4915 1184 : && same_type_p (type, TREE_TYPE (TREE_TYPE (init))));
4916 1184 : from_array = 0;
4917 :
4918 1184 : if (length_check)
4919 100 : finish_length_check (atype, iterator, obase, CONSTRUCTOR_NELTS (init));
4920 :
4921 1184 : if (try_const)
4922 854 : vec_alloc (const_vec, CONSTRUCTOR_NELTS (init));
4923 :
4924 1184 : tree alloc_obj = NULL_TREE;
4925 :
4926 4366 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
4927 : {
4928 3182 : tree one_init;
4929 :
4930 3182 : 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 3176 : 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 3176 : gcc_checking_assert (!target_expr_needs_replace (elt));
5002 :
5003 3176 : if (digested)
5004 2980 : one_init = cp_build_init_expr (baseref, elt);
5005 196 : else if (tree vi = get_vec_init_expr (elt))
5006 24 : one_init = expand_vec_init_expr (baseref, vi, complain,
5007 : cleanup_flags);
5008 172 : else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
5009 160 : 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 3176 : if (one_init == error_mark_node)
5014 0 : errors = true;
5015 3176 : if (try_const)
5016 : {
5017 1148 : if (!field)
5018 0 : field = size_int (num_initialized_elts);
5019 1148 : tree e = maybe_constant_init (one_init);
5020 1148 : if (reduced_constant_expression_p (e))
5021 : {
5022 131 : CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
5023 131 : if (do_static_init)
5024 73 : one_init = NULL_TREE;
5025 : else
5026 58 : one_init = cp_build_init_expr (baseref, e);
5027 : }
5028 : else
5029 : {
5030 1017 : if (do_static_init)
5031 : {
5032 31 : tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
5033 : true);
5034 31 : if (value)
5035 0 : CONSTRUCTOR_APPEND_ELT (const_vec, field, value);
5036 : }
5037 : saw_non_const = true;
5038 : }
5039 : }
5040 :
5041 3176 : tree end = NULL_TREE, body = NULL_TREE;
5042 3176 : 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 3176 : if (one_init)
5057 : {
5058 : /* Only create one std::allocator temporary. */
5059 3103 : combine_allocator_temps (one_init, alloc_obj);
5060 3103 : finish_expr_stmt (one_init);
5061 : }
5062 :
5063 3176 : one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, false,
5064 : complain);
5065 3176 : if (one_init == error_mark_node)
5066 : errors = true;
5067 : else
5068 3176 : finish_expr_stmt (one_init);
5069 :
5070 3176 : one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
5071 : complain);
5072 3176 : if (one_init == error_mark_node)
5073 : errors = true;
5074 : else
5075 3176 : finish_expr_stmt (one_init);
5076 :
5077 3176 : 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 3170 : num_initialized_elts++;
5090 : }
5091 :
5092 : /* Any elements without explicit initializers get T{}. */
5093 1184 : if (!TREE_CLOBBER_P (init))
5094 1182 : empty_list = true;
5095 1184 : }
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 11932 : 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 13405 : if (try_const && !init
5144 13405 : && (cxx_dialect < cxx20
5145 429 : || !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 13405 : if (from_array
5153 13405 : || ((type_build_ctor_call (type) || init || explicit_value_init_p)
5154 13087 : && ! (tree_fits_shwi_p (maxindex)
5155 : && (num_initialized_elts
5156 3414 : == 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 12429 : tree for_stmt;
5161 12429 : tree elt_init;
5162 12429 : tree to;
5163 :
5164 12429 : for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
5165 12429 : finish_init_stmt (for_stmt);
5166 12429 : finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator,
5167 12429 : 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 12429 : tree decr = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false,
5174 : complain);
5175 :
5176 :
5177 12429 : 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 12429 : if (empty_list)
5182 : {
5183 407 : if (cxx_dialect >= cxx11
5184 407 : && (CLASS_TYPE_P (type)
5185 116 : || TREE_CODE (type) == ARRAY_TYPE))
5186 : {
5187 308 : 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 12429 : 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 12111 : else if (TREE_CODE (type) == ARRAY_TYPE)
5238 : {
5239 41 : if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)
5240 255 : && !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 255 : elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
5249 : 0, init,
5250 : explicit_value_init_p,
5251 : 0, complain);
5252 : }
5253 11856 : else if (explicit_value_init_p)
5254 : {
5255 10149 : elt_init = build_value_init (type, complain);
5256 10149 : if (elt_init != error_mark_node)
5257 10149 : elt_init = cp_build_init_expr (to, elt_init);
5258 : }
5259 : else
5260 : {
5261 1707 : gcc_assert (type_build_ctor_call (type) || init);
5262 1707 : if (CLASS_TYPE_P (type))
5263 1699 : elt_init = build_aggr_init (to, init, 0, complain);
5264 : else
5265 : {
5266 8 : if (TREE_CODE (init) == TREE_LIST)
5267 0 : init = build_x_compound_expr_from_list (init, ELK_INIT,
5268 : complain);
5269 8 : elt_init = (init == error_mark_node
5270 8 : ? error_mark_node
5271 2 : : build2 (INIT_EXPR, type, to, init));
5272 : }
5273 : }
5274 :
5275 12429 : if (elt_init == error_mark_node)
5276 44 : errors = true;
5277 :
5278 12429 : if (try_const)
5279 : {
5280 : /* FIXME refs to earlier elts */
5281 671 : tree e = maybe_constant_init (elt_init);
5282 671 : if (reduced_constant_expression_p (e))
5283 : {
5284 411 : if (initializer_zerop (e))
5285 : /* Don't fill the CONSTRUCTOR with zeros. */
5286 233 : e = NULL_TREE;
5287 411 : if (do_static_init)
5288 39 : elt_init = NULL_TREE;
5289 : }
5290 : else
5291 : {
5292 260 : saw_non_const = true;
5293 260 : if (do_static_init)
5294 17 : e = build_zero_init (TREE_TYPE (e), NULL_TREE, true);
5295 : else
5296 : e = NULL_TREE;
5297 : }
5298 :
5299 428 : if (e)
5300 : {
5301 178 : HOST_WIDE_INT last = tree_to_shwi (maxindex);
5302 178 : if (num_initialized_elts <= last)
5303 : {
5304 178 : tree field = size_int (num_initialized_elts);
5305 178 : if (num_initialized_elts != last)
5306 132 : field = build2 (RANGE_EXPR, sizetype, field,
5307 132 : size_int (last));
5308 178 : 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 12429 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5325 12429 : if (elt_init && !errors)
5326 12340 : elt_init = build2 (COMPOUND_EXPR, void_type_node, elt_init, decr);
5327 : else
5328 : elt_init = decr;
5329 12429 : finish_expr_stmt (elt_init);
5330 12429 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
5331 :
5332 12429 : finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false,
5333 : complain));
5334 12429 : if (base2)
5335 343 : finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, false,
5336 : complain));
5337 :
5338 12429 : 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 13405 : finish_stmt_expr_expr (rval, stmt_expr);
5344 :
5345 13405 : stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
5346 :
5347 13405 : current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
5348 :
5349 13405 : if (errors)
5350 50 : return error_mark_node;
5351 :
5352 13355 : if (try_const)
5353 : {
5354 1006 : if (!saw_non_const)
5355 : {
5356 : /* If we're not generating the loop, we don't need to reset the
5357 : iterator. */
5358 488 : if (cleanup_flags
5359 488 : && !vec_safe_is_empty (*cleanup_flags))
5360 : {
5361 2 : auto l = (*cleanup_flags)->last ();
5362 2 : gcc_assert (TREE_PURPOSE (l) == rval);
5363 2 : (*cleanup_flags)->pop ();
5364 : }
5365 488 : tree const_init = build_constructor (atype, const_vec);
5366 488 : return build2 (INIT_EXPR, atype, obase, const_init);
5367 : }
5368 518 : else if (do_static_init && !vec_safe_is_empty (const_vec))
5369 5 : DECL_INITIAL (obase) = build_constructor (atype, const_vec);
5370 : else
5371 513 : vec_free (const_vec);
5372 : }
5373 :
5374 : /* Now make the result have the correct type. */
5375 12867 : if (TREE_CODE (atype) == ARRAY_TYPE)
5376 : {
5377 2583 : atype = build_reference_type (atype);
5378 2583 : stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
5379 2583 : stmt_expr = convert_from_reference (stmt_expr);
5380 : }
5381 :
5382 12867 : 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 9023857 : build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
5390 : tsubst_flags_t complain)
5391 : {
5392 9023857 : tree name;
5393 9023857 : switch (dtor_kind)
5394 : {
5395 8972124 : case sfk_complete_destructor:
5396 8972124 : name = complete_dtor_identifier;
5397 8972124 : break;
5398 :
5399 80 : case sfk_base_destructor:
5400 80 : name = base_dtor_identifier;
5401 80 : break;
5402 :
5403 51653 : case sfk_deleting_destructor:
5404 51653 : name = deleting_dtor_identifier;
5405 51653 : break;
5406 :
5407 0 : default:
5408 0 : gcc_unreachable ();
5409 : }
5410 :
5411 9023857 : return build_special_member_call (exp, name,
5412 : /*args=*/NULL,
5413 9023857 : /*binfo=*/TREE_TYPE (exp),
5414 : flags,
5415 9023857 : 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 9046056 : 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 9046056 : tree expr;
5433 :
5434 9046056 : if (addr == error_mark_node)
5435 : return error_mark_node;
5436 :
5437 9046056 : 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 9046056 : if (type == error_mark_node)
5442 : return error_mark_node;
5443 :
5444 9046056 : if (TYPE_PTR_P (type))
5445 7130613 : type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5446 :
5447 9046056 : if (TREE_CODE (type) == ARRAY_TYPE)
5448 : {
5449 5455 : 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 5452 : return build_vec_delete (loc, addr, array_type_nelts_minus_one (type),
5456 5452 : auto_delete, use_global_delete, complain);
5457 : }
5458 :
5459 9040601 : bool deleting = (auto_delete == sfk_deleting_destructor);
5460 9040601 : gcc_assert (deleting == !(flags & LOOKUP_DESTRUCTOR));
5461 :
5462 9040601 : if (TYPE_PTR_P (otype))
5463 : {
5464 7130605 : 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 7130605 : if (!VOID_TYPE_P (type))
5473 : {
5474 7130586 : complete_type (type);
5475 7130586 : if (deleting
5476 7241242 : && !verify_type_context (loc, TCTX_DEALLOCATION, type,
5477 110656 : !(complain & tf_error)))
5478 0 : return error_mark_node;
5479 :
5480 7130586 : if (!COMPLETE_TYPE_P (type))
5481 : {
5482 24 : if (cxx_dialect > cxx23)
5483 : {
5484 20 : if (complain & tf_error)
5485 : {
5486 20 : auto_diagnostic_group d;
5487 20 : int saved_errorcount = errorcount;
5488 20 : if (permerror_opt (loc, OPT_Wdelete_incomplete,
5489 : "operator %<delete%> used on "
5490 : "incomplete type"))
5491 : {
5492 14 : cxx_incomplete_type_inform (type);
5493 14 : if (errorcount != saved_errorcount)
5494 8 : return error_mark_node;
5495 : }
5496 20 : }
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 110632 : else if (deleting && warn_delnonvdtor
5517 1073 : && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
5518 7131556 : && 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 7130597 : 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 1909996 : addr = cp_build_addr_expr (addr, complain);
5546 1909996 : if (addr == error_mark_node)
5547 : return error_mark_node;
5548 :
5549 1909996 : addr = convert_force (build_pointer_type (type), addr, 0, complain);
5550 : }
5551 :
5552 9040593 : tree addr_expr = NULL_TREE;
5553 9040593 : if (deleting)
5554 : /* We will use ADDR multiple times so we must save it. */
5555 : {
5556 110667 : addr_expr = get_internal_target_expr (addr);
5557 110667 : addr = TARGET_EXPR_SLOT (addr_expr);
5558 : }
5559 :
5560 9040593 : bool virtual_p = false;
5561 9040593 : if (type_build_dtor_call (type))
5562 : {
5563 9023863 : if (CLASSTYPE_LAZY_DESTRUCTOR (type))
5564 108274 : lazily_declare_fn (sfk_destructor, type);
5565 9023863 : virtual_p = DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type));
5566 : }
5567 :
5568 9040593 : tree head = NULL_TREE;
5569 9040593 : tree do_delete = NULL_TREE;
5570 9040593 : bool destroying_delete = false;
5571 :
5572 9040593 : 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 110667 : else if (use_global_delete)
5580 : {
5581 36 : head = get_internal_target_expr (build_headof (addr));
5582 : /* Delete the object. */
5583 36 : do_delete
5584 36 : = build_op_delete_call (DELETE_EXPR, head, cxx_sizeof_nowarn (type),
5585 : /*global_p=*/true, /*placement=*/NULL_TREE,
5586 : /*placement_args=*/NULL,
5587 : /*alloc_fn=*/NULL_TREE, complain);
5588 : /* Otherwise, treat this like a complete object destructor
5589 : call. */
5590 36 : auto_delete = sfk_complete_destructor;
5591 : }
5592 : /* If the destructor is non-virtual, there is no deleting
5593 : variant. Instead, we must explicitly call the appropriate
5594 : `operator delete' here. */
5595 110631 : else if (!virtual_p)
5596 : {
5597 : /* Build the call. */
5598 58978 : do_delete
5599 58978 : = build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
5600 : /*global_p=*/false, /*placement=*/NULL_TREE,
5601 : /*placement_args=*/NULL,
5602 : /*alloc_fn=*/NULL_TREE, complain);
5603 : /* Call the complete object destructor. */
5604 58978 : auto_delete = sfk_complete_destructor;
5605 58978 : if (do_delete != error_mark_node)
5606 : {
5607 58957 : tree fn = get_callee_fndecl (do_delete);
5608 58957 : destroying_delete = destroying_delete_p (fn);
5609 : }
5610 : }
5611 51653 : else if (TYPE_GETS_REG_DELETE (type))
5612 : /* Make sure we have access to the member op delete, even though
5613 : we'll actually be calling it from the destructor. */
5614 12 : build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
5615 : /*global_p=*/false, /*placement=*/NULL_TREE,
5616 : /*placement_args=*/NULL, /*alloc_fn=*/NULL_TREE,
5617 : complain);
5618 :
5619 59005 : if (destroying_delete)
5620 : /* The operator delete will call the destructor. */
5621 : expr = addr;
5622 9040580 : else if (type_build_dtor_call (type))
5623 9023857 : expr = build_dtor_call (cp_build_fold_indirect_ref (addr),
5624 : auto_delete, flags, complain);
5625 : else
5626 16723 : expr = build_trivial_dtor_call (addr);
5627 9040593 : if (expr == error_mark_node)
5628 : return error_mark_node;
5629 :
5630 9040308 : if (!deleting)
5631 : {
5632 8929647 : protected_set_expr_location (expr, loc);
5633 8929647 : return expr;
5634 : }
5635 :
5636 110661 : if (do_delete == error_mark_node)
5637 : return error_mark_node;
5638 :
5639 110640 : if (do_delete && !TREE_SIDE_EFFECTS (expr))
5640 : expr = do_delete;
5641 110606 : else if (do_delete)
5642 : /* The delete operator must be called, regardless of whether
5643 : the destructor throws.
5644 :
5645 : [expr.delete]/7 The deallocation function is called
5646 : regardless of whether the destructor for the object or some
5647 : element of the array throws an exception. */
5648 58953 : expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete);
5649 :
5650 : /* We need to calculate this before the dtor changes the vptr. */
5651 110640 : if (head)
5652 30 : expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
5653 :
5654 : /* Handle deleting a null pointer. */
5655 110640 : warning_sentinel s (warn_address);
5656 110640 : tree ifexp = cp_build_binary_op (loc, NE_EXPR, addr,
5657 : nullptr_node, complain);
5658 110640 : ifexp = cp_fully_fold (ifexp);
5659 :
5660 110640 : if (ifexp == error_mark_node)
5661 : return error_mark_node;
5662 : /* This is a compiler generated comparison, don't emit
5663 : e.g. -Wnonnull-compare warning for it. */
5664 110640 : else if (TREE_CODE (ifexp) == NE_EXPR)
5665 110640 : suppress_warning (ifexp, OPT_Wnonnull_compare);
5666 :
5667 110640 : if (!integer_nonzerop (ifexp))
5668 110640 : expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node);
5669 :
5670 110640 : if (addr_expr)
5671 110640 : expr = cp_build_compound_expr (addr_expr, expr, tf_none);
5672 :
5673 110640 : protected_set_expr_location (expr, loc);
5674 110640 : return expr;
5675 110640 : }
5676 :
5677 : /* At the beginning of a destructor, push cleanups that will call the
5678 : destructors for our base classes and members.
5679 :
5680 : Called from begin_destructor_body. */
5681 :
5682 : void
5683 1690070 : push_base_cleanups (void)
5684 : {
5685 1690070 : tree binfo, base_binfo;
5686 1690070 : int i;
5687 1690070 : tree member;
5688 1690070 : tree expr;
5689 1690070 : vec<tree, va_gc> *vbases;
5690 :
5691 : /* Run destructors for all virtual baseclasses. */
5692 1690070 : if (!ABSTRACT_CLASS_TYPE_P (current_class_type)
5693 3308166 : && CLASSTYPE_VBASECLASSES (current_class_type))
5694 : {
5695 7828 : tree cond = (condition_conversion
5696 7828 : (build2 (BIT_AND_EXPR, integer_type_node,
5697 7828 : current_in_charge_parm,
5698 : integer_two_node)));
5699 :
5700 : /* The CLASSTYPE_VBASECLASSES vector is in initialization
5701 : order, which is also the right order for pushing cleanups. */
5702 27118 : for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
5703 27118 : vec_safe_iterate (vbases, i, &base_binfo); i++)
5704 : {
5705 19290 : if (type_build_dtor_call (BINFO_TYPE (base_binfo)))
5706 : {
5707 19098 : expr = build_special_member_call (current_class_ref,
5708 : base_dtor_identifier,
5709 : NULL,
5710 : base_binfo,
5711 : (LOOKUP_NORMAL
5712 : | LOOKUP_NONVIRTUAL),
5713 : tf_warning_or_error);
5714 19098 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5715 : {
5716 19098 : expr = build3 (COND_EXPR, void_type_node, cond,
5717 : expr, void_node);
5718 19098 : finish_decl_cleanup (NULL_TREE, expr);
5719 : }
5720 : }
5721 : }
5722 : }
5723 :
5724 : /* Take care of the remaining baseclasses. */
5725 2491224 : for (binfo = TYPE_BINFO (current_class_type), i = 0;
5726 2491224 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5727 : {
5728 801154 : if (BINFO_VIRTUAL_P (base_binfo)
5729 801154 : || !type_build_dtor_call (BINFO_TYPE (base_binfo)))
5730 330420 : continue;
5731 :
5732 470734 : expr = build_special_member_call (current_class_ref,
5733 : base_dtor_identifier,
5734 : NULL, base_binfo,
5735 : LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
5736 : tf_warning_or_error);
5737 470734 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
5738 470688 : finish_decl_cleanup (NULL_TREE, expr);
5739 : }
5740 :
5741 : /* Don't automatically destroy union members. */
5742 1690070 : if (TREE_CODE (current_class_type) == UNION_TYPE)
5743 1690070 : return;
5744 :
5745 63774546 : for (member = TYPE_FIELDS (current_class_type); member;
5746 62085129 : member = DECL_CHAIN (member))
5747 : {
5748 62085129 : tree this_type = TREE_TYPE (member);
5749 122217050 : if (this_type == error_mark_node
5750 62085126 : || TREE_CODE (member) != FIELD_DECL
5751 64895497 : || DECL_ARTIFICIAL (member))
5752 60131921 : continue;
5753 1953208 : if (ANON_AGGR_TYPE_P (this_type))
5754 85750 : continue;
5755 1867458 : if (type_build_dtor_call (this_type))
5756 : {
5757 427078 : tree this_member = (build_class_member_access_expr
5758 427078 : (current_class_ref, member,
5759 : /*access_path=*/NULL_TREE,
5760 : /*preserve_reference=*/false,
5761 : tf_warning_or_error));
5762 427078 : expr = build_delete (input_location, this_type, this_member,
5763 : sfk_complete_destructor,
5764 : LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
5765 : 0, tf_warning_or_error);
5766 427078 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
5767 398792 : finish_decl_cleanup (NULL_TREE, expr);
5768 : }
5769 : }
5770 : }
5771 :
5772 : /* Build a C++ vector delete expression.
5773 : MAXINDEX is the number of elements to be deleted.
5774 : ELT_SIZE is the nominal size of each element in the vector.
5775 : BASE is the expression that should yield the store to be deleted.
5776 : This function expands (or synthesizes) these calls itself.
5777 : AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
5778 :
5779 : This also calls delete for virtual baseclasses of elements of the vector.
5780 :
5781 : Update: MAXINDEX is no longer needed. The size can be extracted from the
5782 : start of the vector for pointers, and from the type for arrays. We still
5783 : use MAXINDEX for arrays because it happens to already have one of the
5784 : values we'd have to extract. (We could use MAXINDEX with pointers to
5785 : confirm the size, and trap if the numbers differ; not clear that it'd
5786 : be worth bothering.) */
5787 :
5788 : tree
5789 27406 : build_vec_delete (location_t loc, tree base, tree maxindex,
5790 : special_function_kind auto_delete_vec,
5791 : int use_global_delete, tsubst_flags_t complain)
5792 : {
5793 27406 : tree type;
5794 27406 : tree rval;
5795 27406 : tree base_init = NULL_TREE;
5796 :
5797 27406 : type = TREE_TYPE (base);
5798 :
5799 27406 : if (TYPE_PTR_P (type))
5800 : {
5801 : /* Step back one from start of vector, and read dimension. */
5802 21962 : tree cookie_addr;
5803 21962 : tree size_ptr_type = build_pointer_type (sizetype);
5804 :
5805 21962 : base = mark_rvalue_use (base);
5806 21962 : if (TREE_SIDE_EFFECTS (base))
5807 : {
5808 9410 : base_init = get_internal_target_expr (base);
5809 9410 : base = TARGET_EXPR_SLOT (base_init);
5810 : }
5811 21962 : type = strip_array_types (TREE_TYPE (type));
5812 21962 : cookie_addr = fold_build1_loc (loc, NEGATE_EXPR,
5813 21962 : sizetype, TYPE_SIZE_UNIT (sizetype));
5814 21962 : cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
5815 : cookie_addr);
5816 21962 : maxindex = cp_build_fold_indirect_ref (cookie_addr);
5817 : }
5818 5444 : else if (TREE_CODE (type) == ARRAY_TYPE)
5819 : {
5820 : /* Get the total number of things in the array, maxindex is a
5821 : bad name. */
5822 5444 : maxindex = array_type_nelts_total (type);
5823 5444 : type = strip_array_types (type);
5824 5444 : base = decay_conversion (base, complain);
5825 5444 : if (base == error_mark_node)
5826 : return error_mark_node;
5827 5444 : if (TREE_SIDE_EFFECTS (base))
5828 : {
5829 0 : base_init = get_internal_target_expr (base);
5830 0 : base = TARGET_EXPR_SLOT (base_init);
5831 : }
5832 : }
5833 : else
5834 : {
5835 0 : if (base != error_mark_node && !(complain & tf_error))
5836 0 : error_at (loc,
5837 : "type to vector delete is neither pointer or array type");
5838 0 : return error_mark_node;
5839 : }
5840 :
5841 27406 : rval = build_vec_delete_1 (loc, base, maxindex, type, auto_delete_vec,
5842 : use_global_delete, complain);
5843 27406 : if (base_init && rval != error_mark_node)
5844 9410 : rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
5845 :
5846 27406 : protected_set_expr_location (rval, loc);
5847 27406 : return rval;
5848 : }
5849 :
5850 : #include "gt-cp-init.h"
|