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