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