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