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