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