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