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