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