PR c/50347
[official-gcc.git] / gcc / cp / init.c
blobf85776c5ec7581e0df91e875ff62db3131dcee13
1 /* Handle initialization things in C++.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
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.
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.
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/>. */
21 /* High-level class interface. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "varasm.h"
30 #include "cp-tree.h"
31 #include "flags.h"
32 #include "target.h"
33 #include "gimplify.h"
35 static bool begin_init_stmts (tree *, tree *);
36 static tree finish_init_stmts (bool, tree, tree);
37 static void construct_virtual_base (tree, tree);
38 static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
39 static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
40 static void perform_member_init (tree, tree);
41 static tree build_builtin_delete_call (tree);
42 static int member_init_ok_or_else (tree, tree, tree);
43 static void expand_virtual_init (tree, tree);
44 static tree sort_mem_initializers (tree, tree);
45 static tree initializing_context (tree);
46 static void expand_cleanup_for_base (tree, tree);
47 static tree dfs_initialize_vtbl_ptrs (tree, void *);
48 static tree build_field_list (tree, tree, int *);
49 static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
51 /* We are about to generate some complex initialization code.
52 Conceptually, it is all a single expression. However, we may want
53 to include conditionals, loops, and other such statement-level
54 constructs. Therefore, we build the initialization code inside a
55 statement-expression. This function starts such an expression.
56 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
57 pass them back to finish_init_stmts when the expression is
58 complete. */
60 static bool
61 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
63 bool is_global = !building_stmt_list_p ();
65 *stmt_expr_p = begin_stmt_expr ();
66 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
68 return is_global;
71 /* Finish out the statement-expression begun by the previous call to
72 begin_init_stmts. Returns the statement-expression itself. */
74 static tree
75 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
77 finish_compound_stmt (compound_stmt);
79 stmt_expr = finish_stmt_expr (stmt_expr, true);
81 gcc_assert (!building_stmt_list_p () == is_global);
83 return stmt_expr;
86 /* Constructors */
88 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
89 which we want to initialize the vtable pointer for, DATA is
90 TREE_LIST whose TREE_VALUE is the this ptr expression. */
92 static tree
93 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
95 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
96 return dfs_skip_bases;
98 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
100 tree base_ptr = TREE_VALUE ((tree) data);
102 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
103 tf_warning_or_error);
105 expand_virtual_init (binfo, base_ptr);
108 return NULL_TREE;
111 /* Initialize all the vtable pointers in the object pointed to by
112 ADDR. */
114 void
115 initialize_vtbl_ptrs (tree addr)
117 tree list;
118 tree type;
120 type = TREE_TYPE (TREE_TYPE (addr));
121 list = build_tree_list (type, addr);
123 /* Walk through the hierarchy, initializing the vptr in each base
124 class. We do these in pre-order because we can't find the virtual
125 bases for a class until we've initialized the vtbl for that
126 class. */
127 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
130 /* Return an expression for the zero-initialization of an object with
131 type T. This expression will either be a constant (in the case
132 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
133 aggregate), or NULL (in the case that T does not require
134 initialization). In either case, the value can be used as
135 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
136 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
137 is the number of elements in the array. If STATIC_STORAGE_P is
138 TRUE, initializers are only generated for entities for which
139 zero-initialization does not simply mean filling the storage with
140 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
141 subfields with bit positions at or above that bit size shouldn't
142 be added. Note that this only works when the result is assigned
143 to a base COMPONENT_REF; if we only have a pointer to the base subobject,
144 expand_assignment will end up clearing the full size of TYPE. */
146 static tree
147 build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
148 tree field_size)
150 tree init = NULL_TREE;
152 /* [dcl.init]
154 To zero-initialize an object of type T means:
156 -- if T is a scalar type, the storage is set to the value of zero
157 converted to T.
159 -- if T is a non-union class type, the storage for each nonstatic
160 data member and each base-class subobject is zero-initialized.
162 -- if T is a union type, the storage for its first data member is
163 zero-initialized.
165 -- if T is an array type, the storage for each element is
166 zero-initialized.
168 -- if T is a reference type, no initialization is performed. */
170 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
172 if (type == error_mark_node)
174 else if (static_storage_p && zero_init_p (type))
175 /* In order to save space, we do not explicitly build initializers
176 for items that do not need them. GCC's semantics are that
177 items with static storage duration that are not otherwise
178 initialized are initialized to zero. */
180 else if (TYPE_PTR_OR_PTRMEM_P (type))
181 init = convert (type, nullptr_node);
182 else if (SCALAR_TYPE_P (type))
183 init = convert (type, integer_zero_node);
184 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
186 tree field;
187 vec<constructor_elt, va_gc> *v = NULL;
189 /* Iterate over the fields, building initializations. */
190 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
192 if (TREE_CODE (field) != FIELD_DECL)
193 continue;
195 if (TREE_TYPE (field) == error_mark_node)
196 continue;
198 /* Don't add virtual bases for base classes if they are beyond
199 the size of the current field, that means it is present
200 somewhere else in the object. */
201 if (field_size)
203 tree bitpos = bit_position (field);
204 if (TREE_CODE (bitpos) == INTEGER_CST
205 && !tree_int_cst_lt (bitpos, field_size))
206 continue;
209 /* Note that for class types there will be FIELD_DECLs
210 corresponding to base classes as well. Thus, iterating
211 over TYPE_FIELDs will result in correct initialization of
212 all of the subobjects. */
213 if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
215 tree new_field_size
216 = (DECL_FIELD_IS_BASE (field)
217 && DECL_SIZE (field)
218 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
219 ? DECL_SIZE (field) : NULL_TREE;
220 tree value = build_zero_init_1 (TREE_TYPE (field),
221 /*nelts=*/NULL_TREE,
222 static_storage_p,
223 new_field_size);
224 if (value)
225 CONSTRUCTOR_APPEND_ELT(v, field, value);
228 /* For unions, only the first field is initialized. */
229 if (TREE_CODE (type) == UNION_TYPE)
230 break;
233 /* Build a constructor to contain the initializations. */
234 init = build_constructor (type, v);
236 else if (TREE_CODE (type) == ARRAY_TYPE)
238 tree max_index;
239 vec<constructor_elt, va_gc> *v = NULL;
241 /* Iterate over the array elements, building initializations. */
242 if (nelts)
243 max_index = fold_build2_loc (input_location,
244 MINUS_EXPR, TREE_TYPE (nelts),
245 nelts, integer_one_node);
246 else
247 max_index = array_type_nelts (type);
249 /* If we have an error_mark here, we should just return error mark
250 as we don't know the size of the array yet. */
251 if (max_index == error_mark_node)
252 return error_mark_node;
253 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
255 /* A zero-sized array, which is accepted as an extension, will
256 have an upper bound of -1. */
257 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
259 constructor_elt ce;
261 /* If this is a one element array, we just use a regular init. */
262 if (tree_int_cst_equal (size_zero_node, max_index))
263 ce.index = size_zero_node;
264 else
265 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
266 max_index);
268 ce.value = build_zero_init_1 (TREE_TYPE (type),
269 /*nelts=*/NULL_TREE,
270 static_storage_p, NULL_TREE);
271 if (ce.value)
273 vec_alloc (v, 1);
274 v->quick_push (ce);
278 /* Build a constructor to contain the initializations. */
279 init = build_constructor (type, v);
281 else if (TREE_CODE (type) == VECTOR_TYPE)
282 init = build_zero_cst (type);
283 else
284 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
286 /* In all cases, the initializer is a constant. */
287 if (init)
288 TREE_CONSTANT (init) = 1;
290 return init;
293 /* Return an expression for the zero-initialization of an object with
294 type T. This expression will either be a constant (in the case
295 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
296 aggregate), or NULL (in the case that T does not require
297 initialization). In either case, the value can be used as
298 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
299 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
300 is the number of elements in the array. If STATIC_STORAGE_P is
301 TRUE, initializers are only generated for entities for which
302 zero-initialization does not simply mean filling the storage with
303 zero bytes. */
305 tree
306 build_zero_init (tree type, tree nelts, bool static_storage_p)
308 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
311 /* Return a suitable initializer for value-initializing an object of type
312 TYPE, as described in [dcl.init]. */
314 tree
315 build_value_init (tree type, tsubst_flags_t complain)
317 /* [dcl.init]
319 To value-initialize an object of type T means:
321 - if T is a class type (clause 9) with either no default constructor
322 (12.1) or a default constructor that is user-provided or deleted,
323 then then the object is default-initialized;
325 - if T is a (possibly cv-qualified) class type without a user-provided
326 or deleted default constructor, then the object is zero-initialized
327 and the semantic constraints for default-initialization are checked,
328 and if T has a non-trivial default constructor, the object is
329 default-initialized;
331 - if T is an array type, then each element is value-initialized;
333 - otherwise, the object is zero-initialized.
335 A program that calls for default-initialization or
336 value-initialization of an entity of reference type is ill-formed. */
338 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
339 gcc_assert (!processing_template_decl
340 || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
342 if (type_build_ctor_call (type))
344 tree ctor = build_aggr_init_expr
345 (type,
346 build_special_member_call (NULL_TREE, complete_ctor_identifier,
347 NULL, type, LOOKUP_NORMAL,
348 complain));
349 if (ctor == error_mark_node
350 || type_has_user_provided_default_constructor (type))
351 return ctor;
352 else if (TYPE_HAS_COMPLEX_DFLT (type))
354 /* This is a class that needs constructing, but doesn't have
355 a user-provided constructor. So we need to zero-initialize
356 the object and then call the implicitly defined ctor.
357 This will be handled in simplify_aggr_init_expr. */
358 AGGR_INIT_ZERO_FIRST (ctor) = 1;
359 return ctor;
363 /* Discard any access checking during subobject initialization;
364 the checks are implied by the call to the ctor which we have
365 verified is OK (cpp0x/defaulted46.C). */
366 push_deferring_access_checks (dk_deferred);
367 tree r = build_value_init_noctor (type, complain);
368 pop_deferring_access_checks ();
369 return r;
372 /* Like build_value_init, but don't call the constructor for TYPE. Used
373 for base initializers. */
375 tree
376 build_value_init_noctor (tree type, tsubst_flags_t complain)
378 if (!COMPLETE_TYPE_P (type))
380 if (complain & tf_error)
381 error ("value-initialization of incomplete type %qT", type);
382 return error_mark_node;
384 /* FIXME the class and array cases should just use digest_init once it is
385 SFINAE-enabled. */
386 if (CLASS_TYPE_P (type))
388 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type)
389 || errorcount != 0);
391 if (TREE_CODE (type) != UNION_TYPE)
393 tree field;
394 vec<constructor_elt, va_gc> *v = NULL;
396 /* Iterate over the fields, building initializations. */
397 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
399 tree ftype, value;
401 if (TREE_CODE (field) != FIELD_DECL)
402 continue;
404 ftype = TREE_TYPE (field);
406 if (ftype == error_mark_node)
407 continue;
409 /* We could skip vfields and fields of types with
410 user-defined constructors, but I think that won't improve
411 performance at all; it should be simpler in general just
412 to zero out the entire object than try to only zero the
413 bits that actually need it. */
415 /* Note that for class types there will be FIELD_DECLs
416 corresponding to base classes as well. Thus, iterating
417 over TYPE_FIELDs will result in correct initialization of
418 all of the subobjects. */
419 value = build_value_init (ftype, complain);
421 if (value == error_mark_node)
422 return error_mark_node;
424 if (value)
425 CONSTRUCTOR_APPEND_ELT(v, field, value);
428 /* Build a constructor to contain the zero- initializations. */
429 return build_constructor (type, v);
432 else if (TREE_CODE (type) == ARRAY_TYPE)
434 vec<constructor_elt, va_gc> *v = NULL;
436 /* Iterate over the array elements, building initializations. */
437 tree max_index = array_type_nelts (type);
439 /* If we have an error_mark here, we should just return error mark
440 as we don't know the size of the array yet. */
441 if (max_index == error_mark_node)
443 if (complain & tf_error)
444 error ("cannot value-initialize array of unknown bound %qT",
445 type);
446 return error_mark_node;
448 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
450 /* A zero-sized array, which is accepted as an extension, will
451 have an upper bound of -1. */
452 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
454 constructor_elt ce;
456 /* If this is a one element array, we just use a regular init. */
457 if (tree_int_cst_equal (size_zero_node, max_index))
458 ce.index = size_zero_node;
459 else
460 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
462 ce.value = build_value_init (TREE_TYPE (type), complain);
463 if (ce.value)
465 if (ce.value == error_mark_node)
466 return error_mark_node;
468 vec_alloc (v, 1);
469 v->quick_push (ce);
471 /* We shouldn't have gotten here for anything that would need
472 non-trivial initialization, and gimplify_init_ctor_preeval
473 would need to be fixed to allow it. */
474 gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
475 && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
479 /* Build a constructor to contain the initializations. */
480 return build_constructor (type, v);
482 else if (TREE_CODE (type) == FUNCTION_TYPE)
484 if (complain & tf_error)
485 error ("value-initialization of function type %qT", type);
486 return error_mark_node;
488 else if (TREE_CODE (type) == REFERENCE_TYPE)
490 if (complain & tf_error)
491 error ("value-initialization of reference type %qT", type);
492 return error_mark_node;
495 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
498 /* Initialize current class with INIT, a TREE_LIST of
499 arguments for a target constructor. If TREE_LIST is void_type_node,
500 an empty initializer list was given. */
502 static void
503 perform_target_ctor (tree init)
505 tree decl = current_class_ref;
506 tree type = current_class_type;
508 finish_expr_stmt (build_aggr_init (decl, init,
509 LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
510 tf_warning_or_error));
511 if (type_build_dtor_call (type))
513 tree expr = build_delete (type, decl, sfk_complete_destructor,
514 LOOKUP_NORMAL
515 |LOOKUP_NONVIRTUAL
516 |LOOKUP_DESTRUCTOR,
517 0, tf_warning_or_error);
518 if (expr != error_mark_node
519 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
520 finish_eh_cleanup (expr);
524 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
525 arguments. If TREE_LIST is void_type_node, an empty initializer
526 list was given; if NULL_TREE no initializer was given. */
528 static void
529 perform_member_init (tree member, tree init)
531 tree decl;
532 tree type = TREE_TYPE (member);
534 /* Use the non-static data member initializer if there was no
535 mem-initializer for this field. */
536 if (init == NULL_TREE)
538 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
539 /* Do deferred instantiation of the NSDMI. */
540 init = (tsubst_copy_and_build
541 (DECL_INITIAL (DECL_TI_TEMPLATE (member)),
542 DECL_TI_ARGS (member),
543 tf_warning_or_error, member, /*function_p=*/false,
544 /*integral_constant_expression_p=*/false));
545 else
547 init = DECL_INITIAL (member);
548 if (init && TREE_CODE (init) == DEFAULT_ARG)
550 error ("constructor required before non-static data member "
551 "for %qD has been parsed", member);
552 init = NULL_TREE;
554 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
555 so the aggregate init code below will see a CONSTRUCTOR. */
556 if (init && TREE_CODE (init) == TARGET_EXPR
557 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
558 init = TARGET_EXPR_INITIAL (init);
559 init = break_out_target_exprs (init);
563 if (init == error_mark_node)
564 return;
566 /* Effective C++ rule 12 requires that all data members be
567 initialized. */
568 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
569 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
570 "%qD should be initialized in the member initialization list",
571 member);
573 /* Get an lvalue for the data member. */
574 decl = build_class_member_access_expr (current_class_ref, member,
575 /*access_path=*/NULL_TREE,
576 /*preserve_reference=*/true,
577 tf_warning_or_error);
578 if (decl == error_mark_node)
579 return;
581 if (warn_init_self && init && TREE_CODE (init) == TREE_LIST
582 && TREE_CHAIN (init) == NULL_TREE)
584 tree val = TREE_VALUE (init);
585 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
586 && TREE_OPERAND (val, 0) == current_class_ref)
587 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
588 OPT_Winit_self, "%qD is initialized with itself",
589 member);
592 if (init == void_type_node)
594 /* mem() means value-initialization. */
595 if (TREE_CODE (type) == ARRAY_TYPE)
597 init = build_vec_init_expr (type, init, tf_warning_or_error);
598 init = build2 (INIT_EXPR, type, decl, init);
599 finish_expr_stmt (init);
601 else
603 tree value = build_value_init (type, tf_warning_or_error);
604 if (value == error_mark_node)
605 return;
606 init = build2 (INIT_EXPR, type, decl, value);
607 finish_expr_stmt (init);
610 /* Deal with this here, as we will get confused if we try to call the
611 assignment op for an anonymous union. This can happen in a
612 synthesized copy constructor. */
613 else if (ANON_AGGR_TYPE_P (type))
615 if (init)
617 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
618 finish_expr_stmt (init);
621 else if (init
622 && (TREE_CODE (type) == REFERENCE_TYPE
623 /* Pre-digested NSDMI. */
624 || (((TREE_CODE (init) == CONSTRUCTOR
625 && TREE_TYPE (init) == type)
626 /* { } mem-initializer. */
627 || (TREE_CODE (init) == TREE_LIST
628 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
629 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init))))
630 && (CP_AGGREGATE_TYPE_P (type)
631 || is_std_init_list (type)))))
633 /* With references and list-initialization, we need to deal with
634 extending temporary lifetimes. 12.2p5: "A temporary bound to a
635 reference member in a constructor’s ctor-initializer (12.6.2)
636 persists until the constructor exits." */
637 unsigned i; tree t;
638 vec<tree, va_gc> *cleanups = make_tree_vector ();
639 if (TREE_CODE (init) == TREE_LIST)
640 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
641 tf_warning_or_error);
642 if (TREE_TYPE (init) != type)
644 if (BRACE_ENCLOSED_INITIALIZER_P (init)
645 && CP_AGGREGATE_TYPE_P (type))
646 init = reshape_init (type, init, tf_warning_or_error);
647 init = digest_init (type, init, tf_warning_or_error);
649 if (init == error_mark_node)
650 return;
651 /* A FIELD_DECL doesn't really have a suitable lifetime, but
652 make_temporary_var_for_ref_to_temp will treat it as automatic and
653 set_up_extended_ref_temp wants to use the decl in a warning. */
654 init = extend_ref_init_temps (member, init, &cleanups);
655 if (TREE_CODE (type) == ARRAY_TYPE
656 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
657 init = build_vec_init_expr (type, init, tf_warning_or_error);
658 init = build2 (INIT_EXPR, type, decl, init);
659 finish_expr_stmt (init);
660 FOR_EACH_VEC_ELT (*cleanups, i, t)
661 push_cleanup (decl, t, false);
662 release_tree_vector (cleanups);
664 else if (type_build_ctor_call (type)
665 || (init && CLASS_TYPE_P (strip_array_types (type))))
667 if (TREE_CODE (type) == ARRAY_TYPE)
669 if (init)
671 if (TREE_CHAIN (init))
672 init = error_mark_node;
673 else
674 init = TREE_VALUE (init);
675 if (BRACE_ENCLOSED_INITIALIZER_P (init))
676 init = digest_init (type, init, tf_warning_or_error);
678 if (init == NULL_TREE
679 || same_type_ignoring_top_level_qualifiers_p (type,
680 TREE_TYPE (init)))
682 init = build_vec_init_expr (type, init, tf_warning_or_error);
683 init = build2 (INIT_EXPR, type, decl, init);
684 finish_expr_stmt (init);
686 else
687 error ("invalid initializer for array member %q#D", member);
689 else
691 int flags = LOOKUP_NORMAL;
692 if (DECL_DEFAULTED_FN (current_function_decl))
693 flags |= LOOKUP_DEFAULTED;
694 if (CP_TYPE_CONST_P (type)
695 && init == NULL_TREE
696 && default_init_uninitialized_part (type))
697 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
698 vtable; still give this diagnostic. */
699 permerror (DECL_SOURCE_LOCATION (current_function_decl),
700 "uninitialized member %qD with %<const%> type %qT",
701 member, type);
702 finish_expr_stmt (build_aggr_init (decl, init, flags,
703 tf_warning_or_error));
706 else
708 if (init == NULL_TREE)
710 tree core_type;
711 /* member traversal: note it leaves init NULL */
712 if (TREE_CODE (type) == REFERENCE_TYPE)
713 permerror (DECL_SOURCE_LOCATION (current_function_decl),
714 "uninitialized reference member %qD",
715 member);
716 else if (CP_TYPE_CONST_P (type))
717 permerror (DECL_SOURCE_LOCATION (current_function_decl),
718 "uninitialized member %qD with %<const%> type %qT",
719 member, type);
721 core_type = strip_array_types (type);
723 if (CLASS_TYPE_P (core_type)
724 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
725 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
726 diagnose_uninitialized_cst_or_ref_member (core_type,
727 /*using_new=*/false,
728 /*complain=*/true);
730 else if (TREE_CODE (init) == TREE_LIST)
731 /* There was an explicit member initialization. Do some work
732 in that case. */
733 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
734 tf_warning_or_error);
736 if (init)
737 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
738 tf_warning_or_error));
741 if (type_build_dtor_call (type))
743 tree expr;
745 expr = build_class_member_access_expr (current_class_ref, member,
746 /*access_path=*/NULL_TREE,
747 /*preserve_reference=*/false,
748 tf_warning_or_error);
749 expr = build_delete (type, expr, sfk_complete_destructor,
750 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
751 tf_warning_or_error);
753 if (expr != error_mark_node
754 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
755 finish_eh_cleanup (expr);
759 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
760 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
762 static tree
763 build_field_list (tree t, tree list, int *uses_unions_p)
765 tree fields;
767 /* Note whether or not T is a union. */
768 if (TREE_CODE (t) == UNION_TYPE)
769 *uses_unions_p = 1;
771 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
773 tree fieldtype;
775 /* Skip CONST_DECLs for enumeration constants and so forth. */
776 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
777 continue;
779 fieldtype = TREE_TYPE (fields);
780 /* Keep track of whether or not any fields are unions. */
781 if (TREE_CODE (fieldtype) == UNION_TYPE)
782 *uses_unions_p = 1;
784 /* For an anonymous struct or union, we must recursively
785 consider the fields of the anonymous type. They can be
786 directly initialized from the constructor. */
787 if (ANON_AGGR_TYPE_P (fieldtype))
789 /* Add this field itself. Synthesized copy constructors
790 initialize the entire aggregate. */
791 list = tree_cons (fields, NULL_TREE, list);
792 /* And now add the fields in the anonymous aggregate. */
793 list = build_field_list (fieldtype, list, uses_unions_p);
795 /* Add this field. */
796 else if (DECL_NAME (fields))
797 list = tree_cons (fields, NULL_TREE, list);
800 return list;
803 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
804 a FIELD_DECL or BINFO in T that needs initialization. The
805 TREE_VALUE gives the initializer, or list of initializer arguments.
807 Return a TREE_LIST containing all of the initializations required
808 for T, in the order in which they should be performed. The output
809 list has the same format as the input. */
811 static tree
812 sort_mem_initializers (tree t, tree mem_inits)
814 tree init;
815 tree base, binfo, base_binfo;
816 tree sorted_inits;
817 tree next_subobject;
818 vec<tree, va_gc> *vbases;
819 int i;
820 int uses_unions_p = 0;
822 /* Build up a list of initializations. The TREE_PURPOSE of entry
823 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
824 TREE_VALUE will be the constructor arguments, or NULL if no
825 explicit initialization was provided. */
826 sorted_inits = NULL_TREE;
828 /* Process the virtual bases. */
829 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
830 vec_safe_iterate (vbases, i, &base); i++)
831 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
833 /* Process the direct bases. */
834 for (binfo = TYPE_BINFO (t), i = 0;
835 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
836 if (!BINFO_VIRTUAL_P (base_binfo))
837 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
839 /* Process the non-static data members. */
840 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
841 /* Reverse the entire list of initializations, so that they are in
842 the order that they will actually be performed. */
843 sorted_inits = nreverse (sorted_inits);
845 /* If the user presented the initializers in an order different from
846 that in which they will actually occur, we issue a warning. Keep
847 track of the next subobject which can be explicitly initialized
848 without issuing a warning. */
849 next_subobject = sorted_inits;
851 /* Go through the explicit initializers, filling in TREE_PURPOSE in
852 the SORTED_INITS. */
853 for (init = mem_inits; init; init = TREE_CHAIN (init))
855 tree subobject;
856 tree subobject_init;
858 subobject = TREE_PURPOSE (init);
860 /* If the explicit initializers are in sorted order, then
861 SUBOBJECT will be NEXT_SUBOBJECT, or something following
862 it. */
863 for (subobject_init = next_subobject;
864 subobject_init;
865 subobject_init = TREE_CHAIN (subobject_init))
866 if (TREE_PURPOSE (subobject_init) == subobject)
867 break;
869 /* Issue a warning if the explicit initializer order does not
870 match that which will actually occur.
871 ??? Are all these on the correct lines? */
872 if (warn_reorder && !subobject_init)
874 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
875 warning (OPT_Wreorder, "%q+D will be initialized after",
876 TREE_PURPOSE (next_subobject));
877 else
878 warning (OPT_Wreorder, "base %qT will be initialized after",
879 TREE_PURPOSE (next_subobject));
880 if (TREE_CODE (subobject) == FIELD_DECL)
881 warning (OPT_Wreorder, " %q+#D", subobject);
882 else
883 warning (OPT_Wreorder, " base %qT", subobject);
884 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
885 OPT_Wreorder, " when initialized here");
888 /* Look again, from the beginning of the list. */
889 if (!subobject_init)
891 subobject_init = sorted_inits;
892 while (TREE_PURPOSE (subobject_init) != subobject)
893 subobject_init = TREE_CHAIN (subobject_init);
896 /* It is invalid to initialize the same subobject more than
897 once. */
898 if (TREE_VALUE (subobject_init))
900 if (TREE_CODE (subobject) == FIELD_DECL)
901 error_at (DECL_SOURCE_LOCATION (current_function_decl),
902 "multiple initializations given for %qD",
903 subobject);
904 else
905 error_at (DECL_SOURCE_LOCATION (current_function_decl),
906 "multiple initializations given for base %qT",
907 subobject);
910 /* Record the initialization. */
911 TREE_VALUE (subobject_init) = TREE_VALUE (init);
912 next_subobject = subobject_init;
915 /* [class.base.init]
917 If a ctor-initializer specifies more than one mem-initializer for
918 multiple members of the same union (including members of
919 anonymous unions), the ctor-initializer is ill-formed.
921 Here we also splice out uninitialized union members. */
922 if (uses_unions_p)
924 tree *last_p = NULL;
925 tree *p;
926 for (p = &sorted_inits; *p; )
928 tree field;
929 tree ctx;
931 init = *p;
933 field = TREE_PURPOSE (init);
935 /* Skip base classes. */
936 if (TREE_CODE (field) != FIELD_DECL)
937 goto next;
939 /* If this is an anonymous union with no explicit initializer,
940 splice it out. */
941 if (!TREE_VALUE (init) && ANON_UNION_TYPE_P (TREE_TYPE (field)))
942 goto splice;
944 /* See if this field is a member of a union, or a member of a
945 structure contained in a union, etc. */
946 for (ctx = DECL_CONTEXT (field);
947 !same_type_p (ctx, t);
948 ctx = TYPE_CONTEXT (ctx))
949 if (TREE_CODE (ctx) == UNION_TYPE
950 || !ANON_AGGR_TYPE_P (ctx))
951 break;
952 /* If this field is not a member of a union, skip it. */
953 if (TREE_CODE (ctx) != UNION_TYPE)
954 goto next;
956 /* If this union member has no explicit initializer and no NSDMI,
957 splice it out. */
958 if (TREE_VALUE (init) || DECL_INITIAL (field))
959 /* OK. */;
960 else
961 goto splice;
963 /* It's only an error if we have two initializers for the same
964 union type. */
965 if (!last_p)
967 last_p = p;
968 goto next;
971 /* See if LAST_FIELD and the field initialized by INIT are
972 members of the same union. If so, there's a problem,
973 unless they're actually members of the same structure
974 which is itself a member of a union. For example, given:
976 union { struct { int i; int j; }; };
978 initializing both `i' and `j' makes sense. */
979 ctx = common_enclosing_class (DECL_CONTEXT (field),
980 DECL_CONTEXT (TREE_PURPOSE (*last_p)));
982 if (ctx && TREE_CODE (ctx) == UNION_TYPE)
984 /* A mem-initializer hides an NSDMI. */
985 if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
986 *last_p = TREE_CHAIN (*last_p);
987 else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
988 goto splice;
989 else
991 error_at (DECL_SOURCE_LOCATION (current_function_decl),
992 "initializations for multiple members of %qT",
993 ctx);
994 goto splice;
998 last_p = p;
1000 next:
1001 p = &TREE_CHAIN (*p);
1002 continue;
1003 splice:
1004 *p = TREE_CHAIN (*p);
1005 continue;
1009 return sorted_inits;
1012 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1013 is a TREE_LIST giving the explicit mem-initializer-list for the
1014 constructor. The TREE_PURPOSE of each entry is a subobject (a
1015 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1016 is a TREE_LIST giving the arguments to the constructor or
1017 void_type_node for an empty list of arguments. */
1019 void
1020 emit_mem_initializers (tree mem_inits)
1022 int flags = LOOKUP_NORMAL;
1024 /* We will already have issued an error message about the fact that
1025 the type is incomplete. */
1026 if (!COMPLETE_TYPE_P (current_class_type))
1027 return;
1029 if (mem_inits
1030 && TYPE_P (TREE_PURPOSE (mem_inits))
1031 && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1033 /* Delegating constructor. */
1034 gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
1035 perform_target_ctor (TREE_VALUE (mem_inits));
1036 return;
1039 if (DECL_DEFAULTED_FN (current_function_decl)
1040 && ! DECL_INHERITED_CTOR_BASE (current_function_decl))
1041 flags |= LOOKUP_DEFAULTED;
1043 /* Sort the mem-initializers into the order in which the
1044 initializations should be performed. */
1045 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
1047 in_base_initializer = 1;
1049 /* Initialize base classes. */
1050 for (; (mem_inits
1051 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
1052 mem_inits = TREE_CHAIN (mem_inits))
1054 tree subobject = TREE_PURPOSE (mem_inits);
1055 tree arguments = TREE_VALUE (mem_inits);
1057 /* We already have issued an error message. */
1058 if (arguments == error_mark_node)
1059 continue;
1061 if (arguments == NULL_TREE)
1063 /* If these initializations are taking place in a copy constructor,
1064 the base class should probably be explicitly initialized if there
1065 is a user-defined constructor in the base class (other than the
1066 default constructor, which will be called anyway). */
1067 if (extra_warnings
1068 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1069 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1070 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1071 OPT_Wextra, "base class %q#T should be explicitly "
1072 "initialized in the copy constructor",
1073 BINFO_TYPE (subobject));
1076 /* Initialize the base. */
1077 if (BINFO_VIRTUAL_P (subobject))
1078 construct_virtual_base (subobject, arguments);
1079 else
1081 tree base_addr;
1083 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1084 subobject, 1, tf_warning_or_error);
1085 expand_aggr_init_1 (subobject, NULL_TREE,
1086 cp_build_indirect_ref (base_addr, RO_NULL,
1087 tf_warning_or_error),
1088 arguments,
1089 flags,
1090 tf_warning_or_error);
1091 expand_cleanup_for_base (subobject, NULL_TREE);
1094 in_base_initializer = 0;
1096 /* Initialize the vptrs. */
1097 initialize_vtbl_ptrs (current_class_ptr);
1099 /* Initialize the data members. */
1100 while (mem_inits)
1102 perform_member_init (TREE_PURPOSE (mem_inits),
1103 TREE_VALUE (mem_inits));
1104 mem_inits = TREE_CHAIN (mem_inits);
1108 /* Returns the address of the vtable (i.e., the value that should be
1109 assigned to the vptr) for BINFO. */
1111 tree
1112 build_vtbl_address (tree binfo)
1114 tree binfo_for = binfo;
1115 tree vtbl;
1117 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1118 /* If this is a virtual primary base, then the vtable we want to store
1119 is that for the base this is being used as the primary base of. We
1120 can't simply skip the initialization, because we may be expanding the
1121 inits of a subobject constructor where the virtual base layout
1122 can be different. */
1123 while (BINFO_PRIMARY_P (binfo_for))
1124 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1126 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1127 used. */
1128 vtbl = get_vtbl_decl_for_binfo (binfo_for);
1129 if (tree dtor = CLASSTYPE_DESTRUCTORS (DECL_CONTEXT (vtbl)))
1130 if (!TREE_USED (vtbl) && DECL_VIRTUAL_P (dtor) && DECL_DEFAULTED_FN (dtor))
1131 /* Make sure the destructor gets synthesized so that it can be
1132 inlined after devirtualization even if the vtable is never
1133 emitted. */
1134 note_vague_linkage_fn (dtor);
1135 TREE_USED (vtbl) = true;
1137 /* Now compute the address to use when initializing the vptr. */
1138 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1139 if (VAR_P (vtbl))
1140 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1142 return vtbl;
1145 /* This code sets up the virtual function tables appropriate for
1146 the pointer DECL. It is a one-ply initialization.
1148 BINFO is the exact type that DECL is supposed to be. In
1149 multiple inheritance, this might mean "C's A" if C : A, B. */
1151 static void
1152 expand_virtual_init (tree binfo, tree decl)
1154 tree vtbl, vtbl_ptr;
1155 tree vtt_index;
1157 /* Compute the initializer for vptr. */
1158 vtbl = build_vtbl_address (binfo);
1160 /* We may get this vptr from a VTT, if this is a subobject
1161 constructor or subobject destructor. */
1162 vtt_index = BINFO_VPTR_INDEX (binfo);
1163 if (vtt_index)
1165 tree vtbl2;
1166 tree vtt_parm;
1168 /* Compute the value to use, when there's a VTT. */
1169 vtt_parm = current_vtt_parm;
1170 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1171 vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error);
1172 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1174 /* The actual initializer is the VTT value only in the subobject
1175 constructor. In maybe_clone_body we'll substitute NULL for
1176 the vtt_parm in the case of the non-subobject constructor. */
1177 vtbl = build3 (COND_EXPR,
1178 TREE_TYPE (vtbl),
1179 build2 (EQ_EXPR, boolean_type_node,
1180 current_in_charge_parm, integer_zero_node),
1181 vtbl2,
1182 vtbl);
1185 /* Compute the location of the vtpr. */
1186 vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL,
1187 tf_warning_or_error),
1188 TREE_TYPE (binfo));
1189 gcc_assert (vtbl_ptr != error_mark_node);
1191 /* Assign the vtable to the vptr. */
1192 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
1193 finish_expr_stmt (cp_build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl,
1194 tf_warning_or_error));
1197 /* If an exception is thrown in a constructor, those base classes already
1198 constructed must be destroyed. This function creates the cleanup
1199 for BINFO, which has just been constructed. If FLAG is non-NULL,
1200 it is a DECL which is nonzero when this base needs to be
1201 destroyed. */
1203 static void
1204 expand_cleanup_for_base (tree binfo, tree flag)
1206 tree expr;
1208 if (!type_build_dtor_call (BINFO_TYPE (binfo)))
1209 return;
1211 /* Call the destructor. */
1212 expr = build_special_member_call (current_class_ref,
1213 base_dtor_identifier,
1214 NULL,
1215 binfo,
1216 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1217 tf_warning_or_error);
1219 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1220 return;
1222 if (flag)
1223 expr = fold_build3_loc (input_location,
1224 COND_EXPR, void_type_node,
1225 c_common_truthvalue_conversion (input_location, flag),
1226 expr, integer_zero_node);
1228 finish_eh_cleanup (expr);
1231 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1232 constructor. */
1234 static void
1235 construct_virtual_base (tree vbase, tree arguments)
1237 tree inner_if_stmt;
1238 tree exp;
1239 tree flag;
1241 /* If there are virtual base classes with destructors, we need to
1242 emit cleanups to destroy them if an exception is thrown during
1243 the construction process. These exception regions (i.e., the
1244 period during which the cleanups must occur) begin from the time
1245 the construction is complete to the end of the function. If we
1246 create a conditional block in which to initialize the
1247 base-classes, then the cleanup region for the virtual base begins
1248 inside a block, and ends outside of that block. This situation
1249 confuses the sjlj exception-handling code. Therefore, we do not
1250 create a single conditional block, but one for each
1251 initialization. (That way the cleanup regions always begin
1252 in the outer block.) We trust the back end to figure out
1253 that the FLAG will not change across initializations, and
1254 avoid doing multiple tests. */
1255 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1256 inner_if_stmt = begin_if_stmt ();
1257 finish_if_stmt_cond (flag, inner_if_stmt);
1259 /* Compute the location of the virtual base. If we're
1260 constructing virtual bases, then we must be the most derived
1261 class. Therefore, we don't have to look up the virtual base;
1262 we already know where it is. */
1263 exp = convert_to_base_statically (current_class_ref, vbase);
1265 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1266 0, tf_warning_or_error);
1267 finish_then_clause (inner_if_stmt);
1268 finish_if_stmt (inner_if_stmt);
1270 expand_cleanup_for_base (vbase, flag);
1273 /* Find the context in which this FIELD can be initialized. */
1275 static tree
1276 initializing_context (tree field)
1278 tree t = DECL_CONTEXT (field);
1280 /* Anonymous union members can be initialized in the first enclosing
1281 non-anonymous union context. */
1282 while (t && ANON_AGGR_TYPE_P (t))
1283 t = TYPE_CONTEXT (t);
1284 return t;
1287 /* Function to give error message if member initialization specification
1288 is erroneous. FIELD is the member we decided to initialize.
1289 TYPE is the type for which the initialization is being performed.
1290 FIELD must be a member of TYPE.
1292 MEMBER_NAME is the name of the member. */
1294 static int
1295 member_init_ok_or_else (tree field, tree type, tree member_name)
1297 if (field == error_mark_node)
1298 return 0;
1299 if (!field)
1301 error ("class %qT does not have any field named %qD", type,
1302 member_name);
1303 return 0;
1305 if (VAR_P (field))
1307 error ("%q#D is a static data member; it can only be "
1308 "initialized at its definition",
1309 field);
1310 return 0;
1312 if (TREE_CODE (field) != FIELD_DECL)
1314 error ("%q#D is not a non-static data member of %qT",
1315 field, type);
1316 return 0;
1318 if (initializing_context (field) != type)
1320 error ("class %qT does not have any field named %qD", type,
1321 member_name);
1322 return 0;
1325 return 1;
1328 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1329 is a _TYPE node or TYPE_DECL which names a base for that type.
1330 Check the validity of NAME, and return either the base _TYPE, base
1331 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1332 NULL_TREE and issue a diagnostic.
1334 An old style unnamed direct single base construction is permitted,
1335 where NAME is NULL. */
1337 tree
1338 expand_member_init (tree name)
1340 tree basetype;
1341 tree field;
1343 if (!current_class_ref)
1344 return NULL_TREE;
1346 if (!name)
1348 /* This is an obsolete unnamed base class initializer. The
1349 parser will already have warned about its use. */
1350 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1352 case 0:
1353 error ("unnamed initializer for %qT, which has no base classes",
1354 current_class_type);
1355 return NULL_TREE;
1356 case 1:
1357 basetype = BINFO_TYPE
1358 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1359 break;
1360 default:
1361 error ("unnamed initializer for %qT, which uses multiple inheritance",
1362 current_class_type);
1363 return NULL_TREE;
1366 else if (TYPE_P (name))
1368 basetype = TYPE_MAIN_VARIANT (name);
1369 name = TYPE_NAME (name);
1371 else if (TREE_CODE (name) == TYPE_DECL)
1372 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1373 else
1374 basetype = NULL_TREE;
1376 if (basetype)
1378 tree class_binfo;
1379 tree direct_binfo;
1380 tree virtual_binfo;
1381 int i;
1383 if (current_template_parms
1384 || same_type_p (basetype, current_class_type))
1385 return basetype;
1387 class_binfo = TYPE_BINFO (current_class_type);
1388 direct_binfo = NULL_TREE;
1389 virtual_binfo = NULL_TREE;
1391 /* Look for a direct base. */
1392 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1393 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1394 break;
1396 /* Look for a virtual base -- unless the direct base is itself
1397 virtual. */
1398 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1399 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1401 /* [class.base.init]
1403 If a mem-initializer-id is ambiguous because it designates
1404 both a direct non-virtual base class and an inherited virtual
1405 base class, the mem-initializer is ill-formed. */
1406 if (direct_binfo && virtual_binfo)
1408 error ("%qD is both a direct base and an indirect virtual base",
1409 basetype);
1410 return NULL_TREE;
1413 if (!direct_binfo && !virtual_binfo)
1415 if (CLASSTYPE_VBASECLASSES (current_class_type))
1416 error ("type %qT is not a direct or virtual base of %qT",
1417 basetype, current_class_type);
1418 else
1419 error ("type %qT is not a direct base of %qT",
1420 basetype, current_class_type);
1421 return NULL_TREE;
1424 return direct_binfo ? direct_binfo : virtual_binfo;
1426 else
1428 if (identifier_p (name))
1429 field = lookup_field (current_class_type, name, 1, false);
1430 else
1431 field = name;
1433 if (member_init_ok_or_else (field, current_class_type, name))
1434 return field;
1437 return NULL_TREE;
1440 /* This is like `expand_member_init', only it stores one aggregate
1441 value into another.
1443 INIT comes in two flavors: it is either a value which
1444 is to be stored in EXP, or it is a parameter list
1445 to go to a constructor, which will operate on EXP.
1446 If INIT is not a parameter list for a constructor, then set
1447 LOOKUP_ONLYCONVERTING.
1448 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1449 the initializer, if FLAGS is 0, then it is the (init) form.
1450 If `init' is a CONSTRUCTOR, then we emit a warning message,
1451 explaining that such initializations are invalid.
1453 If INIT resolves to a CALL_EXPR which happens to return
1454 something of the type we are looking for, then we know
1455 that we can safely use that call to perform the
1456 initialization.
1458 The virtual function table pointer cannot be set up here, because
1459 we do not really know its type.
1461 This never calls operator=().
1463 When initializing, nothing is CONST.
1465 A default copy constructor may have to be used to perform the
1466 initialization.
1468 A constructor or a conversion operator may have to be used to
1469 perform the initialization, but not both, as it would be ambiguous. */
1471 tree
1472 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1474 tree stmt_expr;
1475 tree compound_stmt;
1476 int destroy_temps;
1477 tree type = TREE_TYPE (exp);
1478 int was_const = TREE_READONLY (exp);
1479 int was_volatile = TREE_THIS_VOLATILE (exp);
1480 int is_global;
1482 if (init == error_mark_node)
1483 return error_mark_node;
1485 TREE_READONLY (exp) = 0;
1486 TREE_THIS_VOLATILE (exp) = 0;
1488 if (init && init != void_type_node
1489 && TREE_CODE (init) != TREE_LIST
1490 && !(TREE_CODE (init) == TARGET_EXPR
1491 && TARGET_EXPR_DIRECT_INIT_P (init))
1492 && !(BRACE_ENCLOSED_INITIALIZER_P (init)
1493 && CONSTRUCTOR_IS_DIRECT_INIT (init)))
1494 flags |= LOOKUP_ONLYCONVERTING;
1496 if (TREE_CODE (type) == ARRAY_TYPE)
1498 tree itype;
1500 /* An array may not be initialized use the parenthesized
1501 initialization form -- unless the initializer is "()". */
1502 if (init && TREE_CODE (init) == TREE_LIST)
1504 if (complain & tf_error)
1505 error ("bad array initializer");
1506 return error_mark_node;
1508 /* Must arrange to initialize each element of EXP
1509 from elements of INIT. */
1510 itype = init ? TREE_TYPE (init) : NULL_TREE;
1511 if (cv_qualified_p (type))
1512 TREE_TYPE (exp) = cv_unqualified (type);
1513 if (itype && cv_qualified_p (itype))
1514 TREE_TYPE (init) = cv_unqualified (itype);
1515 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1516 /*explicit_value_init_p=*/false,
1517 itype && same_type_p (TREE_TYPE (init),
1518 TREE_TYPE (exp)),
1519 complain);
1520 TREE_READONLY (exp) = was_const;
1521 TREE_THIS_VOLATILE (exp) = was_volatile;
1522 TREE_TYPE (exp) = type;
1523 /* Restore the type of init unless it was used directly. */
1524 if (init && TREE_CODE (stmt_expr) != INIT_EXPR)
1525 TREE_TYPE (init) = itype;
1526 return stmt_expr;
1529 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
1530 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
1531 /* Just know that we've seen something for this node. */
1532 TREE_USED (exp) = 1;
1534 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1535 destroy_temps = stmts_are_full_exprs_p ();
1536 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1537 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1538 init, LOOKUP_NORMAL|flags, complain);
1539 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1540 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1541 TREE_READONLY (exp) = was_const;
1542 TREE_THIS_VOLATILE (exp) = was_volatile;
1544 return stmt_expr;
1547 static void
1548 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
1549 tsubst_flags_t complain)
1551 tree type = TREE_TYPE (exp);
1552 tree ctor_name;
1554 /* It fails because there may not be a constructor which takes
1555 its own type as the first (or only parameter), but which does
1556 take other types via a conversion. So, if the thing initializing
1557 the expression is a unit element of type X, first try X(X&),
1558 followed by initialization by X. If neither of these work
1559 out, then look hard. */
1560 tree rval;
1561 vec<tree, va_gc> *parms;
1563 /* If we have direct-initialization from an initializer list, pull
1564 it out of the TREE_LIST so the code below can see it. */
1565 if (init && TREE_CODE (init) == TREE_LIST
1566 && BRACE_ENCLOSED_INITIALIZER_P (TREE_VALUE (init))
1567 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
1569 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
1570 && TREE_CHAIN (init) == NULL_TREE);
1571 init = TREE_VALUE (init);
1574 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
1575 && CP_AGGREGATE_TYPE_P (type))
1576 /* A brace-enclosed initializer for an aggregate. In C++0x this can
1577 happen for direct-initialization, too. */
1578 init = digest_init (type, init, complain);
1580 /* A CONSTRUCTOR of the target's type is a previously digested
1581 initializer, whether that happened just above or in
1582 cp_parser_late_parsing_nsdmi.
1584 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
1585 set represents the whole initialization, so we shouldn't build up
1586 another ctor call. */
1587 if (init
1588 && (TREE_CODE (init) == CONSTRUCTOR
1589 || (TREE_CODE (init) == TARGET_EXPR
1590 && (TARGET_EXPR_DIRECT_INIT_P (init)
1591 || TARGET_EXPR_LIST_INIT_P (init))))
1592 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1594 /* Early initialization via a TARGET_EXPR only works for
1595 complete objects. */
1596 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
1598 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1599 TREE_SIDE_EFFECTS (init) = 1;
1600 finish_expr_stmt (init);
1601 return;
1604 if (init && TREE_CODE (init) != TREE_LIST
1605 && (flags & LOOKUP_ONLYCONVERTING))
1607 /* Base subobjects should only get direct-initialization. */
1608 gcc_assert (true_exp == exp);
1610 if (flags & DIRECT_BIND)
1611 /* Do nothing. We hit this in two cases: Reference initialization,
1612 where we aren't initializing a real variable, so we don't want
1613 to run a new constructor; and catching an exception, where we
1614 have already built up the constructor call so we could wrap it
1615 in an exception region. */;
1616 else
1617 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
1618 flags, complain);
1620 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1621 /* We need to protect the initialization of a catch parm with a
1622 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1623 around the TARGET_EXPR for the copy constructor. See
1624 initialize_handler_parm. */
1626 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1627 TREE_OPERAND (init, 0));
1628 TREE_TYPE (init) = void_type_node;
1630 else
1631 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1632 TREE_SIDE_EFFECTS (init) = 1;
1633 finish_expr_stmt (init);
1634 return;
1637 if (init == NULL_TREE)
1638 parms = NULL;
1639 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
1641 parms = make_tree_vector ();
1642 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1643 vec_safe_push (parms, TREE_VALUE (init));
1645 else
1646 parms = make_tree_vector_single (init);
1648 if (exp == current_class_ref && current_function_decl
1649 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
1651 /* Delegating constructor. */
1652 tree complete;
1653 tree base;
1654 tree elt; unsigned i;
1656 /* Unshare the arguments for the second call. */
1657 vec<tree, va_gc> *parms2 = make_tree_vector ();
1658 FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
1660 elt = break_out_target_exprs (elt);
1661 vec_safe_push (parms2, elt);
1663 complete = build_special_member_call (exp, complete_ctor_identifier,
1664 &parms2, binfo, flags,
1665 complain);
1666 complete = fold_build_cleanup_point_expr (void_type_node, complete);
1667 release_tree_vector (parms2);
1669 base = build_special_member_call (exp, base_ctor_identifier,
1670 &parms, binfo, flags,
1671 complain);
1672 base = fold_build_cleanup_point_expr (void_type_node, base);
1673 rval = build3 (COND_EXPR, void_type_node,
1674 build2 (EQ_EXPR, boolean_type_node,
1675 current_in_charge_parm, integer_zero_node),
1676 base,
1677 complete);
1679 else
1681 if (true_exp == exp)
1682 ctor_name = complete_ctor_identifier;
1683 else
1684 ctor_name = base_ctor_identifier;
1685 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
1686 complain);
1689 if (parms != NULL)
1690 release_tree_vector (parms);
1692 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
1694 tree fn = get_callee_fndecl (rval);
1695 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
1697 tree e = maybe_constant_init (rval);
1698 if (TREE_CONSTANT (e))
1699 rval = build2 (INIT_EXPR, type, exp, e);
1703 /* FIXME put back convert_to_void? */
1704 if (TREE_SIDE_EFFECTS (rval))
1705 finish_expr_stmt (rval);
1708 /* This function is responsible for initializing EXP with INIT
1709 (if any).
1711 BINFO is the binfo of the type for who we are performing the
1712 initialization. For example, if W is a virtual base class of A and B,
1713 and C : A, B.
1714 If we are initializing B, then W must contain B's W vtable, whereas
1715 were we initializing C, W must contain C's W vtable.
1717 TRUE_EXP is nonzero if it is the true expression being initialized.
1718 In this case, it may be EXP, or may just contain EXP. The reason we
1719 need this is because if EXP is a base element of TRUE_EXP, we
1720 don't necessarily know by looking at EXP where its virtual
1721 baseclass fields should really be pointing. But we do know
1722 from TRUE_EXP. In constructors, we don't know anything about
1723 the value being initialized.
1725 FLAGS is just passed to `build_new_method_call'. See that function
1726 for its description. */
1728 static void
1729 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
1730 tsubst_flags_t complain)
1732 tree type = TREE_TYPE (exp);
1734 gcc_assert (init != error_mark_node && type != error_mark_node);
1735 gcc_assert (building_stmt_list_p ());
1737 /* Use a function returning the desired type to initialize EXP for us.
1738 If the function is a constructor, and its first argument is
1739 NULL_TREE, know that it was meant for us--just slide exp on
1740 in and expand the constructor. Constructors now come
1741 as TARGET_EXPRs. */
1743 if (init && VAR_P (exp)
1744 && COMPOUND_LITERAL_P (init))
1746 vec<tree, va_gc> *cleanups = NULL;
1747 /* If store_init_value returns NULL_TREE, the INIT has been
1748 recorded as the DECL_INITIAL for EXP. That means there's
1749 nothing more we have to do. */
1750 init = store_init_value (exp, init, &cleanups, flags);
1751 if (init)
1752 finish_expr_stmt (init);
1753 gcc_assert (!cleanups);
1754 return;
1757 /* If an explicit -- but empty -- initializer list was present,
1758 that's value-initialization. */
1759 if (init == void_type_node)
1761 /* If the type has data but no user-provided ctor, we need to zero
1762 out the object. */
1763 if (!type_has_user_provided_constructor (type)
1764 && !is_really_empty_class (type))
1766 tree field_size = NULL_TREE;
1767 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
1768 /* Don't clobber already initialized virtual bases. */
1769 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
1770 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
1771 field_size);
1772 init = build2 (INIT_EXPR, type, exp, init);
1773 finish_expr_stmt (init);
1776 /* If we don't need to mess with the constructor at all,
1777 then we're done. */
1778 if (! type_build_ctor_call (type))
1779 return;
1781 /* Otherwise fall through and call the constructor. */
1782 init = NULL_TREE;
1785 /* We know that expand_default_init can handle everything we want
1786 at this point. */
1787 expand_default_init (binfo, true_exp, exp, init, flags, complain);
1790 /* Report an error if TYPE is not a user-defined, class type. If
1791 OR_ELSE is nonzero, give an error message. */
1794 is_class_type (tree type, int or_else)
1796 if (type == error_mark_node)
1797 return 0;
1799 if (! CLASS_TYPE_P (type))
1801 if (or_else)
1802 error ("%qT is not a class type", type);
1803 return 0;
1805 return 1;
1808 tree
1809 get_type_value (tree name)
1811 if (name == error_mark_node)
1812 return NULL_TREE;
1814 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1815 return IDENTIFIER_TYPE_VALUE (name);
1816 else
1817 return NULL_TREE;
1820 /* Build a reference to a member of an aggregate. This is not a C++
1821 `&', but really something which can have its address taken, and
1822 then act as a pointer to member, for example TYPE :: FIELD can have
1823 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
1824 this expression is the operand of "&".
1826 @@ Prints out lousy diagnostics for operator <typename>
1827 @@ fields.
1829 @@ This function should be rewritten and placed in search.c. */
1831 tree
1832 build_offset_ref (tree type, tree member, bool address_p,
1833 tsubst_flags_t complain)
1835 tree decl;
1836 tree basebinfo = NULL_TREE;
1838 /* class templates can come in as TEMPLATE_DECLs here. */
1839 if (TREE_CODE (member) == TEMPLATE_DECL)
1840 return member;
1842 if (dependent_scope_p (type) || type_dependent_expression_p (member))
1843 return build_qualified_name (NULL_TREE, type, member,
1844 /*template_p=*/false);
1846 gcc_assert (TYPE_P (type));
1847 if (! is_class_type (type, 1))
1848 return error_mark_node;
1850 gcc_assert (DECL_P (member) || BASELINK_P (member));
1851 /* Callers should call mark_used before this point. */
1852 gcc_assert (!DECL_P (member) || TREE_USED (member));
1854 type = TYPE_MAIN_VARIANT (type);
1855 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
1857 if (complain & tf_error)
1858 error ("incomplete type %qT does not have member %qD", type, member);
1859 return error_mark_node;
1862 /* Entities other than non-static members need no further
1863 processing. */
1864 if (TREE_CODE (member) == TYPE_DECL)
1865 return member;
1866 if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
1867 return convert_from_reference (member);
1869 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1871 if (complain & tf_error)
1872 error ("invalid pointer to bit-field %qD", member);
1873 return error_mark_node;
1876 /* Set up BASEBINFO for member lookup. */
1877 decl = maybe_dummy_object (type, &basebinfo);
1879 /* A lot of this logic is now handled in lookup_member. */
1880 if (BASELINK_P (member))
1882 /* Go from the TREE_BASELINK to the member function info. */
1883 tree t = BASELINK_FUNCTIONS (member);
1885 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1887 /* Get rid of a potential OVERLOAD around it. */
1888 t = OVL_CURRENT (t);
1890 /* Unique functions are handled easily. */
1892 /* For non-static member of base class, we need a special rule
1893 for access checking [class.protected]:
1895 If the access is to form a pointer to member, the
1896 nested-name-specifier shall name the derived class
1897 (or any class derived from that class). */
1898 if (address_p && DECL_P (t)
1899 && DECL_NONSTATIC_MEMBER_P (t))
1900 perform_or_defer_access_check (TYPE_BINFO (type), t, t,
1901 complain);
1902 else
1903 perform_or_defer_access_check (basebinfo, t, t,
1904 complain);
1906 if (DECL_STATIC_FUNCTION_P (t))
1907 return t;
1908 member = t;
1910 else
1911 TREE_TYPE (member) = unknown_type_node;
1913 else if (address_p && TREE_CODE (member) == FIELD_DECL)
1914 /* We need additional test besides the one in
1915 check_accessibility_of_qualified_id in case it is
1916 a pointer to non-static member. */
1917 perform_or_defer_access_check (TYPE_BINFO (type), member, member,
1918 complain);
1920 if (!address_p)
1922 /* If MEMBER is non-static, then the program has fallen afoul of
1923 [expr.prim]:
1925 An id-expression that denotes a nonstatic data member or
1926 nonstatic member function of a class can only be used:
1928 -- as part of a class member access (_expr.ref_) in which the
1929 object-expression refers to the member's class or a class
1930 derived from that class, or
1932 -- to form a pointer to member (_expr.unary.op_), or
1934 -- in the body of a nonstatic member function of that class or
1935 of a class derived from that class (_class.mfct.nonstatic_), or
1937 -- in a mem-initializer for a constructor for that class or for
1938 a class derived from that class (_class.base.init_). */
1939 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1941 /* Build a representation of the qualified name suitable
1942 for use as the operand to "&" -- even though the "&" is
1943 not actually present. */
1944 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1945 /* In Microsoft mode, treat a non-static member function as if
1946 it were a pointer-to-member. */
1947 if (flag_ms_extensions)
1949 PTRMEM_OK_P (member) = 1;
1950 return cp_build_addr_expr (member, complain);
1952 if (complain & tf_error)
1953 error ("invalid use of non-static member function %qD",
1954 TREE_OPERAND (member, 1));
1955 return error_mark_node;
1957 else if (TREE_CODE (member) == FIELD_DECL)
1959 if (complain & tf_error)
1960 error ("invalid use of non-static data member %qD", member);
1961 return error_mark_node;
1963 return member;
1966 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1967 PTRMEM_OK_P (member) = 1;
1968 return member;
1971 /* If DECL is a scalar enumeration constant or variable with a
1972 constant initializer, return the initializer (or, its initializers,
1973 recursively); otherwise, return DECL. If INTEGRAL_P, the
1974 initializer is only returned if DECL is an integral
1975 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
1976 return an aggregate constant. */
1978 static tree
1979 constant_value_1 (tree decl, bool integral_p, bool return_aggregate_cst_ok_p)
1981 while (TREE_CODE (decl) == CONST_DECL
1982 || (integral_p
1983 ? decl_constant_var_p (decl)
1984 : (VAR_P (decl)
1985 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
1987 tree init;
1988 /* If DECL is a static data member in a template
1989 specialization, we must instantiate it here. The
1990 initializer for the static data member is not processed
1991 until needed; we need it now. */
1992 mark_used (decl);
1993 mark_rvalue_use (decl);
1994 init = DECL_INITIAL (decl);
1995 if (init == error_mark_node)
1997 if (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
1998 /* Treat the error as a constant to avoid cascading errors on
1999 excessively recursive template instantiation (c++/9335). */
2000 return init;
2001 else
2002 return decl;
2004 /* Initializers in templates are generally expanded during
2005 instantiation, so before that for const int i(2)
2006 INIT is a TREE_LIST with the actual initializer as
2007 TREE_VALUE. */
2008 if (processing_template_decl
2009 && init
2010 && TREE_CODE (init) == TREE_LIST
2011 && TREE_CHAIN (init) == NULL_TREE)
2012 init = TREE_VALUE (init);
2013 if (!init
2014 || !TREE_TYPE (init)
2015 || !TREE_CONSTANT (init)
2016 || (!integral_p && !return_aggregate_cst_ok_p
2017 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2018 return an aggregate constant (of which string
2019 literals are a special case), as we do not want
2020 to make inadvertent copies of such entities, and
2021 we must be sure that their addresses are the
2022 same everywhere. */
2023 && (TREE_CODE (init) == CONSTRUCTOR
2024 || TREE_CODE (init) == STRING_CST)))
2025 break;
2026 decl = unshare_expr (init);
2028 return decl;
2031 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by
2032 constant of integral or enumeration type, then return that value.
2033 These are those variables permitted in constant expressions by
2034 [5.19/1]. */
2036 tree
2037 integral_constant_value (tree decl)
2039 return constant_value_1 (decl, /*integral_p=*/true,
2040 /*return_aggregate_cst_ok_p=*/false);
2043 /* A more relaxed version of integral_constant_value, used by the
2044 common C/C++ code. */
2046 tree
2047 decl_constant_value (tree decl)
2049 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2050 /*return_aggregate_cst_ok_p=*/true);
2053 /* A version of integral_constant_value used by the C++ front end for
2054 optimization purposes. */
2056 tree
2057 decl_constant_value_safe (tree decl)
2059 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2060 /*return_aggregate_cst_ok_p=*/false);
2063 /* Common subroutines of build_new and build_vec_delete. */
2065 /* Call the global __builtin_delete to delete ADDR. */
2067 static tree
2068 build_builtin_delete_call (tree addr)
2070 mark_used (global_delete_fndecl);
2071 return build_call_n (global_delete_fndecl, 1, addr);
2074 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2075 the type of the object being allocated; otherwise, it's just TYPE.
2076 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2077 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2078 a vector of arguments to be provided as arguments to a placement
2079 new operator. This routine performs no semantic checks; it just
2080 creates and returns a NEW_EXPR. */
2082 static tree
2083 build_raw_new_expr (vec<tree, va_gc> *placement, tree type, tree nelts,
2084 vec<tree, va_gc> *init, int use_global_new)
2086 tree init_list;
2087 tree new_expr;
2089 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2090 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2091 permits us to distinguish the case of a missing initializer "new
2092 int" from an empty initializer "new int()". */
2093 if (init == NULL)
2094 init_list = NULL_TREE;
2095 else if (init->is_empty ())
2096 init_list = void_zero_node;
2097 else
2098 init_list = build_tree_list_vec (init);
2100 new_expr = build4 (NEW_EXPR, build_pointer_type (type),
2101 build_tree_list_vec (placement), type, nelts,
2102 init_list);
2103 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2104 TREE_SIDE_EFFECTS (new_expr) = 1;
2106 return new_expr;
2109 /* Diagnose uninitialized const members or reference members of type
2110 TYPE. USING_NEW is used to disambiguate the diagnostic between a
2111 new expression without a new-initializer and a declaration. Returns
2112 the error count. */
2114 static int
2115 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2116 bool using_new, bool complain)
2118 tree field;
2119 int error_count = 0;
2121 if (type_has_user_provided_constructor (type))
2122 return 0;
2124 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2126 tree field_type;
2128 if (TREE_CODE (field) != FIELD_DECL)
2129 continue;
2131 field_type = strip_array_types (TREE_TYPE (field));
2133 if (type_has_user_provided_constructor (field_type))
2134 continue;
2136 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2138 ++ error_count;
2139 if (complain)
2141 if (DECL_CONTEXT (field) == origin)
2143 if (using_new)
2144 error ("uninitialized reference member in %q#T "
2145 "using %<new%> without new-initializer", origin);
2146 else
2147 error ("uninitialized reference member in %q#T", origin);
2149 else
2151 if (using_new)
2152 error ("uninitialized reference member in base %q#T "
2153 "of %q#T using %<new%> without new-initializer",
2154 DECL_CONTEXT (field), origin);
2155 else
2156 error ("uninitialized reference member in base %q#T "
2157 "of %q#T", DECL_CONTEXT (field), origin);
2159 inform (DECL_SOURCE_LOCATION (field),
2160 "%q#D should be initialized", field);
2164 if (CP_TYPE_CONST_P (field_type))
2166 ++ error_count;
2167 if (complain)
2169 if (DECL_CONTEXT (field) == origin)
2171 if (using_new)
2172 error ("uninitialized const member in %q#T "
2173 "using %<new%> without new-initializer", origin);
2174 else
2175 error ("uninitialized const member in %q#T", origin);
2177 else
2179 if (using_new)
2180 error ("uninitialized const member in base %q#T "
2181 "of %q#T using %<new%> without new-initializer",
2182 DECL_CONTEXT (field), origin);
2183 else
2184 error ("uninitialized const member in base %q#T "
2185 "of %q#T", DECL_CONTEXT (field), origin);
2187 inform (DECL_SOURCE_LOCATION (field),
2188 "%q#D should be initialized", field);
2192 if (CLASS_TYPE_P (field_type))
2193 error_count
2194 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2195 using_new, complain);
2197 return error_count;
2201 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2203 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2206 /* Call __cxa_bad_array_new_length to indicate that the size calculation
2207 overflowed. Pretend it returns sizetype so that it plays nicely in the
2208 COND_EXPR. */
2210 tree
2211 throw_bad_array_new_length (void)
2213 tree fn = get_identifier ("__cxa_throw_bad_array_new_length");
2214 if (!get_global_value_if_present (fn, &fn))
2215 fn = push_throw_library_fn (fn, build_function_type_list (sizetype,
2216 NULL_TREE));
2218 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2221 /* Call __cxa_bad_array_length to indicate that there were too many
2222 initializers. */
2224 tree
2225 throw_bad_array_length (void)
2227 tree fn = get_identifier ("__cxa_throw_bad_array_length");
2228 if (!get_global_value_if_present (fn, &fn))
2229 fn = push_throw_library_fn (fn, build_function_type_list (void_type_node,
2230 NULL_TREE));
2232 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2235 /* Generate code for a new-expression, including calling the "operator
2236 new" function, initializing the object, and, if an exception occurs
2237 during construction, cleaning up. The arguments are as for
2238 build_raw_new_expr. This may change PLACEMENT and INIT. */
2240 static tree
2241 build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
2242 vec<tree, va_gc> **init, bool globally_qualified_p,
2243 tsubst_flags_t complain)
2245 tree size, rval;
2246 /* True iff this is a call to "operator new[]" instead of just
2247 "operator new". */
2248 bool array_p = false;
2249 /* If ARRAY_P is true, the element type of the array. This is never
2250 an ARRAY_TYPE; for something like "new int[3][4]", the
2251 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
2252 TYPE. */
2253 tree elt_type;
2254 /* The type of the new-expression. (This type is always a pointer
2255 type.) */
2256 tree pointer_type;
2257 tree non_const_pointer_type;
2258 tree outer_nelts = NULL_TREE;
2259 /* For arrays, a bounds checks on the NELTS parameter. */
2260 tree outer_nelts_check = NULL_TREE;
2261 bool outer_nelts_from_type = false;
2262 double_int inner_nelts_count = double_int_one;
2263 tree alloc_call, alloc_expr;
2264 /* Size of the inner array elements. */
2265 double_int inner_size;
2266 /* The address returned by the call to "operator new". This node is
2267 a VAR_DECL and is therefore reusable. */
2268 tree alloc_node;
2269 tree alloc_fn;
2270 tree cookie_expr, init_expr;
2271 int nothrow, check_new;
2272 int use_java_new = 0;
2273 /* If non-NULL, the number of extra bytes to allocate at the
2274 beginning of the storage allocated for an array-new expression in
2275 order to store the number of elements. */
2276 tree cookie_size = NULL_TREE;
2277 tree placement_first;
2278 tree placement_expr = NULL_TREE;
2279 /* True if the function we are calling is a placement allocation
2280 function. */
2281 bool placement_allocation_fn_p;
2282 /* True if the storage must be initialized, either by a constructor
2283 or due to an explicit new-initializer. */
2284 bool is_initialized;
2285 /* The address of the thing allocated, not including any cookie. In
2286 particular, if an array cookie is in use, DATA_ADDR is the
2287 address of the first array element. This node is a VAR_DECL, and
2288 is therefore reusable. */
2289 tree data_addr;
2290 tree init_preeval_expr = NULL_TREE;
2291 tree orig_type = type;
2293 if (nelts)
2295 outer_nelts = nelts;
2296 array_p = true;
2298 else if (TREE_CODE (type) == ARRAY_TYPE)
2300 /* Transforms new (T[N]) to new T[N]. The former is a GNU
2301 extension for variable N. (This also covers new T where T is
2302 a VLA typedef.) */
2303 array_p = true;
2304 nelts = array_type_nelts_top (type);
2305 outer_nelts = nelts;
2306 type = TREE_TYPE (type);
2307 outer_nelts_from_type = true;
2310 /* If our base type is an array, then make sure we know how many elements
2311 it has. */
2312 for (elt_type = type;
2313 TREE_CODE (elt_type) == ARRAY_TYPE;
2314 elt_type = TREE_TYPE (elt_type))
2316 tree inner_nelts = array_type_nelts_top (elt_type);
2317 tree inner_nelts_cst = maybe_constant_value (inner_nelts);
2318 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
2320 bool overflow;
2321 double_int result = TREE_INT_CST (inner_nelts_cst)
2322 .mul_with_sign (inner_nelts_count,
2323 false, &overflow);
2324 if (overflow)
2326 if (complain & tf_error)
2327 error ("integer overflow in array size");
2328 nelts = error_mark_node;
2330 inner_nelts_count = result;
2332 else
2334 if (complain & tf_error)
2336 error_at (EXPR_LOC_OR_LOC (inner_nelts, input_location),
2337 "array size in new-expression must be constant");
2338 cxx_constant_value(inner_nelts);
2340 nelts = error_mark_node;
2342 if (nelts != error_mark_node)
2343 nelts = cp_build_binary_op (input_location,
2344 MULT_EXPR, nelts,
2345 inner_nelts_cst,
2346 complain);
2349 if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
2351 error ("variably modified type not allowed in new-expression");
2352 return error_mark_node;
2355 if (nelts == error_mark_node)
2356 return error_mark_node;
2358 /* Warn if we performed the (T[N]) to T[N] transformation and N is
2359 variable. */
2360 if (outer_nelts_from_type
2361 && !TREE_CONSTANT (maybe_constant_value (outer_nelts)))
2363 if (complain & tf_warning_or_error)
2365 const char *msg;
2366 if (typedef_variant_p (orig_type))
2367 msg = ("non-constant array new length must be specified "
2368 "directly, not by typedef");
2369 else
2370 msg = ("non-constant array new length must be specified "
2371 "without parentheses around the type-id");
2372 pedwarn (EXPR_LOC_OR_LOC (outer_nelts, input_location),
2373 OPT_Wvla, msg);
2375 else
2376 return error_mark_node;
2379 if (VOID_TYPE_P (elt_type))
2381 if (complain & tf_error)
2382 error ("invalid type %<void%> for new");
2383 return error_mark_node;
2386 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain))
2387 return error_mark_node;
2389 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
2391 if (*init == NULL && cxx_dialect < cxx11)
2393 bool maybe_uninitialized_error = false;
2394 /* A program that calls for default-initialization [...] of an
2395 entity of reference type is ill-formed. */
2396 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
2397 maybe_uninitialized_error = true;
2399 /* A new-expression that creates an object of type T initializes
2400 that object as follows:
2401 - If the new-initializer is omitted:
2402 -- If T is a (possibly cv-qualified) non-POD class type
2403 (or array thereof), the object is default-initialized (8.5).
2404 [...]
2405 -- Otherwise, the object created has indeterminate
2406 value. If T is a const-qualified type, or a (possibly
2407 cv-qualified) POD class type (or array thereof)
2408 containing (directly or indirectly) a member of
2409 const-qualified type, the program is ill-formed; */
2411 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
2412 maybe_uninitialized_error = true;
2414 if (maybe_uninitialized_error
2415 && diagnose_uninitialized_cst_or_ref_member (elt_type,
2416 /*using_new=*/true,
2417 complain & tf_error))
2418 return error_mark_node;
2421 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
2422 && default_init_uninitialized_part (elt_type))
2424 if (complain & tf_error)
2425 error ("uninitialized const in %<new%> of %q#T", elt_type);
2426 return error_mark_node;
2429 size = size_in_bytes (elt_type);
2430 if (array_p)
2432 /* Maximum available size in bytes. Half of the address space
2433 minus the cookie size. */
2434 double_int max_size
2435 = double_int_one.llshift (TYPE_PRECISION (sizetype) - 1,
2436 HOST_BITS_PER_DOUBLE_INT);
2437 /* Maximum number of outer elements which can be allocated. */
2438 double_int max_outer_nelts;
2439 tree max_outer_nelts_tree;
2441 gcc_assert (TREE_CODE (size) == INTEGER_CST);
2442 cookie_size = targetm.cxx.get_cookie_size (elt_type);
2443 gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
2444 gcc_checking_assert (TREE_INT_CST (cookie_size).ult (max_size));
2445 /* Unconditionally subtract the cookie size. This decreases the
2446 maximum object size and is safe even if we choose not to use
2447 a cookie after all. */
2448 max_size -= TREE_INT_CST (cookie_size);
2449 bool overflow;
2450 inner_size = TREE_INT_CST (size)
2451 .mul_with_sign (inner_nelts_count, false, &overflow);
2452 if (overflow || inner_size.ugt (max_size))
2454 if (complain & tf_error)
2455 error ("size of array is too large");
2456 return error_mark_node;
2458 max_outer_nelts = max_size.udiv (inner_size, TRUNC_DIV_EXPR);
2459 /* Only keep the top-most seven bits, to simplify encoding the
2460 constant in the instruction stream. */
2462 unsigned shift = HOST_BITS_PER_DOUBLE_INT - 7
2463 - (max_outer_nelts.high ? clz_hwi (max_outer_nelts.high)
2464 : (HOST_BITS_PER_WIDE_INT + clz_hwi (max_outer_nelts.low)));
2465 max_outer_nelts
2466 = max_outer_nelts.lrshift (shift, HOST_BITS_PER_DOUBLE_INT)
2467 .llshift (shift, HOST_BITS_PER_DOUBLE_INT);
2469 max_outer_nelts_tree = double_int_to_tree (sizetype, max_outer_nelts);
2471 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2472 outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
2473 outer_nelts,
2474 max_outer_nelts_tree);
2477 alloc_fn = NULL_TREE;
2479 /* If PLACEMENT is a single simple pointer type not passed by
2480 reference, prepare to capture it in a temporary variable. Do
2481 this now, since PLACEMENT will change in the calls below. */
2482 placement_first = NULL_TREE;
2483 if (vec_safe_length (*placement) == 1
2484 && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
2485 placement_first = (**placement)[0];
2487 /* Allocate the object. */
2488 if (vec_safe_is_empty (*placement) && TYPE_FOR_JAVA (elt_type))
2490 tree class_addr;
2491 tree class_decl;
2492 static const char alloc_name[] = "_Jv_AllocObject";
2494 if (!MAYBE_CLASS_TYPE_P (elt_type))
2496 error ("%qT isn%'t a valid Java class type", elt_type);
2497 return error_mark_node;
2500 class_decl = build_java_class_ref (elt_type);
2501 if (class_decl == error_mark_node)
2502 return error_mark_node;
2504 use_java_new = 1;
2505 if (!get_global_value_if_present (get_identifier (alloc_name),
2506 &alloc_fn))
2508 if (complain & tf_error)
2509 error ("call to Java constructor with %qs undefined", alloc_name);
2510 return error_mark_node;
2512 else if (really_overloaded_fn (alloc_fn))
2514 if (complain & tf_error)
2515 error ("%qD should never be overloaded", alloc_fn);
2516 return error_mark_node;
2518 alloc_fn = OVL_CURRENT (alloc_fn);
2519 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2520 alloc_call = cp_build_function_call_nary (alloc_fn, complain,
2521 class_addr, NULL_TREE);
2523 else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
2525 error ("Java class %q#T object allocated using placement new", elt_type);
2526 return error_mark_node;
2528 else
2530 tree fnname;
2531 tree fns;
2533 fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
2535 if (!globally_qualified_p
2536 && CLASS_TYPE_P (elt_type)
2537 && (array_p
2538 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
2539 : TYPE_HAS_NEW_OPERATOR (elt_type)))
2541 /* Use a class-specific operator new. */
2542 /* If a cookie is required, add some extra space. */
2543 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
2544 size = size_binop (PLUS_EXPR, size, cookie_size);
2545 else
2547 cookie_size = NULL_TREE;
2548 /* No size arithmetic necessary, so the size check is
2549 not needed. */
2550 if (outer_nelts_check != NULL && inner_size.is_one ())
2551 outer_nelts_check = NULL_TREE;
2553 /* Perform the overflow check. */
2554 tree errval = TYPE_MAX_VALUE (sizetype);
2555 if (cxx_dialect >= cxx11 && flag_exceptions)
2556 errval = throw_bad_array_new_length ();
2557 if (outer_nelts_check != NULL_TREE)
2558 size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
2559 size, errval);
2560 /* Create the argument list. */
2561 vec_safe_insert (*placement, 0, size);
2562 /* Do name-lookup to find the appropriate operator. */
2563 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
2564 if (fns == NULL_TREE)
2566 if (complain & tf_error)
2567 error ("no suitable %qD found in class %qT", fnname, elt_type);
2568 return error_mark_node;
2570 if (TREE_CODE (fns) == TREE_LIST)
2572 if (complain & tf_error)
2574 error ("request for member %qD is ambiguous", fnname);
2575 print_candidates (fns);
2577 return error_mark_node;
2579 alloc_call = build_new_method_call (build_dummy_object (elt_type),
2580 fns, placement,
2581 /*conversion_path=*/NULL_TREE,
2582 LOOKUP_NORMAL,
2583 &alloc_fn,
2584 complain);
2586 else
2588 /* Use a global operator new. */
2589 /* See if a cookie might be required. */
2590 if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
2592 cookie_size = NULL_TREE;
2593 /* No size arithmetic necessary, so the size check is
2594 not needed. */
2595 if (outer_nelts_check != NULL && inner_size.is_one ())
2596 outer_nelts_check = NULL_TREE;
2599 alloc_call = build_operator_new_call (fnname, placement,
2600 &size, &cookie_size,
2601 outer_nelts_check,
2602 &alloc_fn, complain);
2606 if (alloc_call == error_mark_node)
2607 return error_mark_node;
2609 gcc_assert (alloc_fn != NULL_TREE);
2611 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
2612 into a temporary variable. */
2613 if (!processing_template_decl
2614 && placement_first != NULL_TREE
2615 && TREE_CODE (alloc_call) == CALL_EXPR
2616 && call_expr_nargs (alloc_call) == 2
2617 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
2618 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
2620 tree placement_arg = CALL_EXPR_ARG (alloc_call, 1);
2622 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))
2623 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))))
2625 placement_expr = get_target_expr (placement_first);
2626 CALL_EXPR_ARG (alloc_call, 1)
2627 = convert (TREE_TYPE (placement_arg), placement_expr);
2631 /* In the simple case, we can stop now. */
2632 pointer_type = build_pointer_type (type);
2633 if (!cookie_size && !is_initialized)
2634 return build_nop (pointer_type, alloc_call);
2636 /* Store the result of the allocation call in a variable so that we can
2637 use it more than once. */
2638 alloc_expr = get_target_expr (alloc_call);
2639 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2641 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
2642 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
2643 alloc_call = TREE_OPERAND (alloc_call, 1);
2645 /* Now, check to see if this function is actually a placement
2646 allocation function. This can happen even when PLACEMENT is NULL
2647 because we might have something like:
2649 struct S { void* operator new (size_t, int i = 0); };
2651 A call to `new S' will get this allocation function, even though
2652 there is no explicit placement argument. If there is more than
2653 one argument, or there are variable arguments, then this is a
2654 placement allocation function. */
2655 placement_allocation_fn_p
2656 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2657 || varargs_function_p (alloc_fn));
2659 /* Preevaluate the placement args so that we don't reevaluate them for a
2660 placement delete. */
2661 if (placement_allocation_fn_p)
2663 tree inits;
2664 stabilize_call (alloc_call, &inits);
2665 if (inits)
2666 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
2667 alloc_expr);
2670 /* unless an allocation function is declared with an empty excep-
2671 tion-specification (_except.spec_), throw(), it indicates failure to
2672 allocate storage by throwing a bad_alloc exception (clause _except_,
2673 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2674 cation function is declared with an empty exception-specification,
2675 throw(), it returns null to indicate failure to allocate storage and a
2676 non-null pointer otherwise.
2678 So check for a null exception spec on the op new we just called. */
2680 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2681 check_new = (flag_check_new || nothrow) && ! use_java_new;
2683 if (cookie_size)
2685 tree cookie;
2686 tree cookie_ptr;
2687 tree size_ptr_type;
2689 /* Adjust so we're pointing to the start of the object. */
2690 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
2692 /* Store the number of bytes allocated so that we can know how
2693 many elements to destroy later. We use the last sizeof
2694 (size_t) bytes to store the number of elements. */
2695 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
2696 cookie_ptr = fold_build_pointer_plus_loc (input_location,
2697 alloc_node, cookie_ptr);
2698 size_ptr_type = build_pointer_type (sizetype);
2699 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
2700 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2702 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
2704 if (targetm.cxx.cookie_has_size ())
2706 /* Also store the element size. */
2707 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
2708 fold_build1_loc (input_location,
2709 NEGATE_EXPR, sizetype,
2710 size_in_bytes (sizetype)));
2712 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2713 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
2714 size_in_bytes (elt_type));
2715 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
2716 cookie, cookie_expr);
2719 else
2721 cookie_expr = NULL_TREE;
2722 data_addr = alloc_node;
2725 /* Now use a pointer to the type we've actually allocated. */
2727 /* But we want to operate on a non-const version to start with,
2728 since we'll be modifying the elements. */
2729 non_const_pointer_type = build_pointer_type
2730 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
2732 data_addr = fold_convert (non_const_pointer_type, data_addr);
2733 /* Any further uses of alloc_node will want this type, too. */
2734 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
2736 /* Now initialize the allocated object. Note that we preevaluate the
2737 initialization expression, apart from the actual constructor call or
2738 assignment--we do this because we want to delay the allocation as long
2739 as possible in order to minimize the size of the exception region for
2740 placement delete. */
2741 if (is_initialized)
2743 bool stable;
2744 bool explicit_value_init_p = false;
2746 if (*init != NULL && (*init)->is_empty ())
2748 *init = NULL;
2749 explicit_value_init_p = true;
2752 if (processing_template_decl && explicit_value_init_p)
2754 /* build_value_init doesn't work in templates, and we don't need
2755 the initializer anyway since we're going to throw it away and
2756 rebuild it at instantiation time, so just build up a single
2757 constructor call to get any appropriate diagnostics. */
2758 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2759 if (type_build_ctor_call (elt_type))
2760 init_expr = build_special_member_call (init_expr,
2761 complete_ctor_identifier,
2762 init, elt_type,
2763 LOOKUP_NORMAL,
2764 complain);
2765 stable = stabilize_init (init_expr, &init_preeval_expr);
2767 else if (array_p)
2769 tree vecinit = NULL_TREE;
2770 if (vec_safe_length (*init) == 1
2771 && BRACE_ENCLOSED_INITIALIZER_P ((**init)[0])
2772 && CONSTRUCTOR_IS_DIRECT_INIT ((**init)[0]))
2774 vecinit = (**init)[0];
2775 if (CONSTRUCTOR_NELTS (vecinit) == 0)
2776 /* List-value-initialization, leave it alone. */;
2777 else
2779 tree arraytype, domain;
2780 if (TREE_CONSTANT (nelts))
2781 domain = compute_array_index_type (NULL_TREE, nelts,
2782 complain);
2783 else
2784 /* We'll check the length at runtime. */
2785 domain = NULL_TREE;
2786 arraytype = build_cplus_array_type (type, domain);
2787 vecinit = digest_init (arraytype, vecinit, complain);
2790 else if (*init)
2792 if (complain & tf_error)
2793 permerror (input_location,
2794 "parenthesized initializer in array new");
2795 else
2796 return error_mark_node;
2797 vecinit = build_tree_list_vec (*init);
2799 init_expr
2800 = build_vec_init (data_addr,
2801 cp_build_binary_op (input_location,
2802 MINUS_EXPR, outer_nelts,
2803 integer_one_node,
2804 complain),
2805 vecinit,
2806 explicit_value_init_p,
2807 /*from_array=*/0,
2808 complain);
2810 /* An array initialization is stable because the initialization
2811 of each element is a full-expression, so the temporaries don't
2812 leak out. */
2813 stable = true;
2815 else
2817 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2819 if (type_build_ctor_call (type) && !explicit_value_init_p)
2821 init_expr = build_special_member_call (init_expr,
2822 complete_ctor_identifier,
2823 init, elt_type,
2824 LOOKUP_NORMAL,
2825 complain);
2827 else if (explicit_value_init_p)
2829 /* Something like `new int()'. */
2830 tree val = build_value_init (type, complain);
2831 if (val == error_mark_node)
2832 return error_mark_node;
2833 init_expr = build2 (INIT_EXPR, type, init_expr, val);
2835 else
2837 tree ie;
2839 /* We are processing something like `new int (10)', which
2840 means allocate an int, and initialize it with 10. */
2842 ie = build_x_compound_expr_from_vec (*init, "new initializer",
2843 complain);
2844 init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie,
2845 complain);
2847 stable = stabilize_init (init_expr, &init_preeval_expr);
2850 if (init_expr == error_mark_node)
2851 return error_mark_node;
2853 /* If any part of the object initialization terminates by throwing an
2854 exception and a suitable deallocation function can be found, the
2855 deallocation function is called to free the memory in which the
2856 object was being constructed, after which the exception continues
2857 to propagate in the context of the new-expression. If no
2858 unambiguous matching deallocation function can be found,
2859 propagating the exception does not cause the object's memory to be
2860 freed. */
2861 if (flag_exceptions && ! use_java_new)
2863 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
2864 tree cleanup;
2866 /* The Standard is unclear here, but the right thing to do
2867 is to use the same method for finding deallocation
2868 functions that we use for finding allocation functions. */
2869 cleanup = (build_op_delete_call
2870 (dcode,
2871 alloc_node,
2872 size,
2873 globally_qualified_p,
2874 placement_allocation_fn_p ? alloc_call : NULL_TREE,
2875 alloc_fn,
2876 complain));
2878 if (!cleanup)
2879 /* We're done. */;
2880 else if (stable)
2881 /* This is much simpler if we were able to preevaluate all of
2882 the arguments to the constructor call. */
2884 /* CLEANUP is compiler-generated, so no diagnostics. */
2885 TREE_NO_WARNING (cleanup) = true;
2886 init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
2887 init_expr, cleanup);
2888 /* Likewise, this try-catch is compiler-generated. */
2889 TREE_NO_WARNING (init_expr) = true;
2891 else
2892 /* Ack! First we allocate the memory. Then we set our sentry
2893 variable to true, and expand a cleanup that deletes the
2894 memory if sentry is true. Then we run the constructor, and
2895 finally clear the sentry.
2897 We need to do this because we allocate the space first, so
2898 if there are any temporaries with cleanups in the
2899 constructor args and we weren't able to preevaluate them, we
2900 need this EH region to extend until end of full-expression
2901 to preserve nesting. */
2903 tree end, sentry, begin;
2905 begin = get_target_expr (boolean_true_node);
2906 CLEANUP_EH_ONLY (begin) = 1;
2908 sentry = TARGET_EXPR_SLOT (begin);
2910 /* CLEANUP is compiler-generated, so no diagnostics. */
2911 TREE_NO_WARNING (cleanup) = true;
2913 TARGET_EXPR_CLEANUP (begin)
2914 = build3 (COND_EXPR, void_type_node, sentry,
2915 cleanup, void_zero_node);
2917 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2918 sentry, boolean_false_node);
2920 init_expr
2921 = build2 (COMPOUND_EXPR, void_type_node, begin,
2922 build2 (COMPOUND_EXPR, void_type_node, init_expr,
2923 end));
2924 /* Likewise, this is compiler-generated. */
2925 TREE_NO_WARNING (init_expr) = true;
2929 else
2930 init_expr = NULL_TREE;
2932 /* Now build up the return value in reverse order. */
2934 rval = data_addr;
2936 if (init_expr)
2937 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2938 if (cookie_expr)
2939 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2941 if (rval == data_addr)
2942 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2943 and return the call (which doesn't need to be adjusted). */
2944 rval = TARGET_EXPR_INITIAL (alloc_expr);
2945 else
2947 if (check_new)
2949 tree ifexp = cp_build_binary_op (input_location,
2950 NE_EXPR, alloc_node,
2951 nullptr_node,
2952 complain);
2953 rval = build_conditional_expr (input_location, ifexp, rval,
2954 alloc_node, complain);
2957 /* Perform the allocation before anything else, so that ALLOC_NODE
2958 has been initialized before we start using it. */
2959 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2962 if (init_preeval_expr)
2963 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
2965 /* A new-expression is never an lvalue. */
2966 gcc_assert (!lvalue_p (rval));
2968 return convert (pointer_type, rval);
2971 /* Generate a representation for a C++ "new" expression. *PLACEMENT
2972 is a vector of placement-new arguments (or NULL if none). If NELTS
2973 is NULL, TYPE is the type of the storage to be allocated. If NELTS
2974 is not NULL, then this is an array-new allocation; TYPE is the type
2975 of the elements in the array and NELTS is the number of elements in
2976 the array. *INIT, if non-NULL, is the initializer for the new
2977 object, or an empty vector to indicate an initializer of "()". If
2978 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
2979 rather than just "new". This may change PLACEMENT and INIT. */
2981 tree
2982 build_new (vec<tree, va_gc> **placement, tree type, tree nelts,
2983 vec<tree, va_gc> **init, int use_global_new, tsubst_flags_t complain)
2985 tree rval;
2986 vec<tree, va_gc> *orig_placement = NULL;
2987 tree orig_nelts = NULL_TREE;
2988 vec<tree, va_gc> *orig_init = NULL;
2990 if (type == error_mark_node)
2991 return error_mark_node;
2993 if (nelts == NULL_TREE && vec_safe_length (*init) == 1
2994 /* Don't do auto deduction where it might affect mangling. */
2995 && (!processing_template_decl || at_function_scope_p ()))
2997 tree auto_node = type_uses_auto (type);
2998 if (auto_node)
3000 tree d_init = (**init)[0];
3001 d_init = resolve_nondeduced_context (d_init);
3002 type = do_auto_deduction (type, d_init, auto_node);
3006 if (processing_template_decl)
3008 if (dependent_type_p (type)
3009 || any_type_dependent_arguments_p (*placement)
3010 || (nelts && type_dependent_expression_p (nelts))
3011 || (nelts && *init)
3012 || any_type_dependent_arguments_p (*init))
3013 return build_raw_new_expr (*placement, type, nelts, *init,
3014 use_global_new);
3016 orig_placement = make_tree_vector_copy (*placement);
3017 orig_nelts = nelts;
3018 if (*init)
3019 orig_init = make_tree_vector_copy (*init);
3021 make_args_non_dependent (*placement);
3022 if (nelts)
3023 nelts = build_non_dependent_expr (nelts);
3024 make_args_non_dependent (*init);
3027 if (nelts)
3029 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
3031 if (complain & tf_error)
3032 permerror (input_location, "size in array new must have integral type");
3033 else
3034 return error_mark_node;
3036 nelts = mark_rvalue_use (nelts);
3037 nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
3040 /* ``A reference cannot be created by the new operator. A reference
3041 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3042 returned by new.'' ARM 5.3.3 */
3043 if (TREE_CODE (type) == REFERENCE_TYPE)
3045 if (complain & tf_error)
3046 error ("new cannot be applied to a reference type");
3047 else
3048 return error_mark_node;
3049 type = TREE_TYPE (type);
3052 if (TREE_CODE (type) == FUNCTION_TYPE)
3054 if (complain & tf_error)
3055 error ("new cannot be applied to a function type");
3056 return error_mark_node;
3059 /* The type allocated must be complete. If the new-type-id was
3060 "T[N]" then we are just checking that "T" is complete here, but
3061 that is equivalent, since the value of "N" doesn't matter. */
3062 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
3063 return error_mark_node;
3065 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
3066 if (rval == error_mark_node)
3067 return error_mark_node;
3069 if (processing_template_decl)
3071 tree ret = build_raw_new_expr (orig_placement, type, orig_nelts,
3072 orig_init, use_global_new);
3073 release_tree_vector (orig_placement);
3074 release_tree_vector (orig_init);
3075 return ret;
3078 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
3079 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
3080 TREE_NO_WARNING (rval) = 1;
3082 return rval;
3085 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
3087 tree
3088 build_java_class_ref (tree type)
3090 tree name = NULL_TREE, class_decl;
3091 static tree CL_suffix = NULL_TREE;
3092 if (CL_suffix == NULL_TREE)
3093 CL_suffix = get_identifier("class$");
3094 if (jclass_node == NULL_TREE)
3096 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
3097 if (jclass_node == NULL_TREE)
3099 error ("call to Java constructor, while %<jclass%> undefined");
3100 return error_mark_node;
3102 jclass_node = TREE_TYPE (jclass_node);
3105 /* Mangle the class$ field. */
3107 tree field;
3108 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3109 if (DECL_NAME (field) == CL_suffix)
3111 mangle_decl (field);
3112 name = DECL_ASSEMBLER_NAME (field);
3113 break;
3115 if (!field)
3117 error ("can%'t find %<class$%> in %qT", type);
3118 return error_mark_node;
3122 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
3123 if (class_decl == NULL_TREE)
3125 class_decl = build_decl (input_location,
3126 VAR_DECL, name, TREE_TYPE (jclass_node));
3127 TREE_STATIC (class_decl) = 1;
3128 DECL_EXTERNAL (class_decl) = 1;
3129 TREE_PUBLIC (class_decl) = 1;
3130 DECL_ARTIFICIAL (class_decl) = 1;
3131 DECL_IGNORED_P (class_decl) = 1;
3132 pushdecl_top_level (class_decl);
3133 make_decl_rtl (class_decl);
3135 return class_decl;
3138 static tree
3139 build_vec_delete_1 (tree base, tree maxindex, tree type,
3140 special_function_kind auto_delete_vec,
3141 int use_global_delete, tsubst_flags_t complain)
3143 tree virtual_size;
3144 tree ptype = build_pointer_type (type = complete_type (type));
3145 tree size_exp;
3147 /* Temporary variables used by the loop. */
3148 tree tbase, tbase_init;
3150 /* This is the body of the loop that implements the deletion of a
3151 single element, and moves temp variables to next elements. */
3152 tree body;
3154 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3155 tree loop = 0;
3157 /* This is the thing that governs what to do after the loop has run. */
3158 tree deallocate_expr = 0;
3160 /* This is the BIND_EXPR which holds the outermost iterator of the
3161 loop. It is convenient to set this variable up and test it before
3162 executing any other code in the loop.
3163 This is also the containing expression returned by this function. */
3164 tree controller = NULL_TREE;
3165 tree tmp;
3167 /* We should only have 1-D arrays here. */
3168 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
3170 if (base == error_mark_node || maxindex == error_mark_node)
3171 return error_mark_node;
3173 if (!COMPLETE_TYPE_P (type))
3175 if ((complain & tf_warning)
3176 && warning (OPT_Wdelete_incomplete,
3177 "possible problem detected in invocation of "
3178 "delete [] operator:"))
3180 cxx_incomplete_type_diagnostic (base, type, DK_WARNING);
3181 inform (input_location, "neither the destructor nor the "
3182 "class-specific operator delete [] will be called, "
3183 "even if they are declared when the class is defined");
3185 return build_builtin_delete_call (base);
3188 size_exp = size_in_bytes (type);
3190 if (! MAYBE_CLASS_TYPE_P (type))
3191 goto no_destructor;
3192 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3194 /* Make sure the destructor is callable. */
3195 if (type_build_dtor_call (type))
3197 tmp = build_delete (ptype, base, sfk_complete_destructor,
3198 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
3199 complain);
3200 if (tmp == error_mark_node)
3201 return error_mark_node;
3203 goto no_destructor;
3206 /* The below is short by the cookie size. */
3207 virtual_size = size_binop (MULT_EXPR, size_exp,
3208 convert (sizetype, maxindex));
3210 tbase = create_temporary_var (ptype);
3211 tbase_init
3212 = cp_build_modify_expr (tbase, NOP_EXPR,
3213 fold_build_pointer_plus_loc (input_location,
3214 fold_convert (ptype,
3215 base),
3216 virtual_size),
3217 complain);
3218 if (tbase_init == error_mark_node)
3219 return error_mark_node;
3220 controller = build3 (BIND_EXPR, void_type_node, tbase,
3221 NULL_TREE, NULL_TREE);
3222 TREE_SIDE_EFFECTS (controller) = 1;
3224 body = build1 (EXIT_EXPR, void_type_node,
3225 build2 (EQ_EXPR, boolean_type_node, tbase,
3226 fold_convert (ptype, base)));
3227 tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp);
3228 tmp = fold_build_pointer_plus (tbase, tmp);
3229 tmp = cp_build_modify_expr (tbase, NOP_EXPR, tmp, complain);
3230 if (tmp == error_mark_node)
3231 return error_mark_node;
3232 body = build_compound_expr (input_location, body, tmp);
3233 tmp = build_delete (ptype, tbase, sfk_complete_destructor,
3234 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
3235 complain);
3236 if (tmp == error_mark_node)
3237 return error_mark_node;
3238 body = build_compound_expr (input_location, body, tmp);
3240 loop = build1 (LOOP_EXPR, void_type_node, body);
3241 loop = build_compound_expr (input_location, tbase_init, loop);
3243 no_destructor:
3244 /* Delete the storage if appropriate. */
3245 if (auto_delete_vec == sfk_deleting_destructor)
3247 tree base_tbd;
3249 /* The below is short by the cookie size. */
3250 virtual_size = size_binop (MULT_EXPR, size_exp,
3251 convert (sizetype, maxindex));
3253 if (! TYPE_VEC_NEW_USES_COOKIE (type))
3254 /* no header */
3255 base_tbd = base;
3256 else
3258 tree cookie_size;
3260 cookie_size = targetm.cxx.get_cookie_size (type);
3261 base_tbd = cp_build_binary_op (input_location,
3262 MINUS_EXPR,
3263 cp_convert (string_type_node,
3264 base, complain),
3265 cookie_size,
3266 complain);
3267 if (base_tbd == error_mark_node)
3268 return error_mark_node;
3269 base_tbd = cp_convert (ptype, base_tbd, complain);
3270 /* True size with header. */
3271 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
3274 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
3275 base_tbd, virtual_size,
3276 use_global_delete & 1,
3277 /*placement=*/NULL_TREE,
3278 /*alloc_fn=*/NULL_TREE,
3279 complain);
3282 body = loop;
3283 if (!deallocate_expr)
3285 else if (!body)
3286 body = deallocate_expr;
3287 else
3288 body = build_compound_expr (input_location, body, deallocate_expr);
3290 if (!body)
3291 body = integer_zero_node;
3293 /* Outermost wrapper: If pointer is null, punt. */
3294 body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3295 fold_build2_loc (input_location,
3296 NE_EXPR, boolean_type_node, base,
3297 convert (TREE_TYPE (base),
3298 nullptr_node)),
3299 body, integer_zero_node);
3300 body = build1 (NOP_EXPR, void_type_node, body);
3302 if (controller)
3304 TREE_OPERAND (controller, 1) = body;
3305 body = controller;
3308 if (TREE_CODE (base) == SAVE_EXPR)
3309 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
3310 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
3312 return convert_to_void (body, ICV_CAST, complain);
3315 /* Create an unnamed variable of the indicated TYPE. */
3317 tree
3318 create_temporary_var (tree type)
3320 tree decl;
3322 decl = build_decl (input_location,
3323 VAR_DECL, NULL_TREE, type);
3324 TREE_USED (decl) = 1;
3325 DECL_ARTIFICIAL (decl) = 1;
3326 DECL_IGNORED_P (decl) = 1;
3327 DECL_CONTEXT (decl) = current_function_decl;
3329 return decl;
3332 /* Create a new temporary variable of the indicated TYPE, initialized
3333 to INIT.
3335 It is not entered into current_binding_level, because that breaks
3336 things when it comes time to do final cleanups (which take place
3337 "outside" the binding contour of the function). */
3339 tree
3340 get_temp_regvar (tree type, tree init)
3342 tree decl;
3344 decl = create_temporary_var (type);
3345 add_decl_expr (decl);
3347 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
3348 tf_warning_or_error));
3350 return decl;
3353 /* `build_vec_init' returns tree structure that performs
3354 initialization of a vector of aggregate types.
3356 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
3357 to the first element, of POINTER_TYPE.
3358 MAXINDEX is the maximum index of the array (one less than the
3359 number of elements). It is only used if BASE is a pointer or
3360 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
3362 INIT is the (possibly NULL) initializer.
3364 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
3365 elements in the array are value-initialized.
3367 FROM_ARRAY is 0 if we should init everything with INIT
3368 (i.e., every element initialized from INIT).
3369 FROM_ARRAY is 1 if we should index into INIT in parallel
3370 with initialization of DECL.
3371 FROM_ARRAY is 2 if we should index into INIT in parallel,
3372 but use assignment instead of initialization. */
3374 tree
3375 build_vec_init (tree base, tree maxindex, tree init,
3376 bool explicit_value_init_p,
3377 int from_array, tsubst_flags_t complain)
3379 tree rval;
3380 tree base2 = NULL_TREE;
3381 tree itype = NULL_TREE;
3382 tree iterator;
3383 /* The type of BASE. */
3384 tree atype = TREE_TYPE (base);
3385 /* The type of an element in the array. */
3386 tree type = TREE_TYPE (atype);
3387 /* The element type reached after removing all outer array
3388 types. */
3389 tree inner_elt_type;
3390 /* The type of a pointer to an element in the array. */
3391 tree ptype;
3392 tree stmt_expr;
3393 tree compound_stmt;
3394 int destroy_temps;
3395 tree try_block = NULL_TREE;
3396 int num_initialized_elts = 0;
3397 bool is_global;
3398 tree const_init = NULL_TREE;
3399 tree obase = base;
3400 bool xvalue = false;
3401 bool errors = false;
3402 tree length_check = NULL_TREE;
3404 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
3405 maxindex = array_type_nelts (atype);
3407 if (maxindex == NULL_TREE || maxindex == error_mark_node)
3408 return error_mark_node;
3410 if (explicit_value_init_p)
3411 gcc_assert (!init);
3413 inner_elt_type = strip_array_types (type);
3415 /* Look through the TARGET_EXPR around a compound literal. */
3416 if (init && TREE_CODE (init) == TARGET_EXPR
3417 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
3418 && from_array != 2)
3419 init = TARGET_EXPR_INITIAL (init);
3421 /* If we have a braced-init-list, make sure that the array
3422 is big enough for all the initializers. */
3423 if (init && TREE_CODE (init) == CONSTRUCTOR
3424 && CONSTRUCTOR_NELTS (init) > 0
3425 && !TREE_CONSTANT (maxindex)
3426 && flag_exceptions)
3427 length_check = fold_build2 (LT_EXPR, boolean_type_node, maxindex,
3428 size_int (CONSTRUCTOR_NELTS (init) - 1));
3430 if (init
3431 && TREE_CODE (atype) == ARRAY_TYPE
3432 && TREE_CONSTANT (maxindex)
3433 && (from_array == 2
3434 ? (!CLASS_TYPE_P (inner_elt_type)
3435 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (inner_elt_type))
3436 : !TYPE_NEEDS_CONSTRUCTING (type))
3437 && ((TREE_CODE (init) == CONSTRUCTOR
3438 /* Don't do this if the CONSTRUCTOR might contain something
3439 that might throw and require us to clean up. */
3440 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
3441 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
3442 || from_array))
3444 /* Do non-default initialization of trivial arrays resulting from
3445 brace-enclosed initializers. In this case, digest_init and
3446 store_constructor will handle the semantics for us. */
3448 if (BRACE_ENCLOSED_INITIALIZER_P (init))
3449 init = digest_init (atype, init, complain);
3450 stmt_expr = build2 (INIT_EXPR, atype, base, init);
3451 if (length_check)
3452 stmt_expr = build3 (COND_EXPR, atype, length_check,
3453 throw_bad_array_length (),
3454 stmt_expr);
3455 return stmt_expr;
3458 maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
3459 if (TREE_CODE (atype) == ARRAY_TYPE)
3461 ptype = build_pointer_type (type);
3462 base = decay_conversion (base, complain);
3463 if (base == error_mark_node)
3464 return error_mark_node;
3465 base = cp_convert (ptype, base, complain);
3467 else
3468 ptype = atype;
3470 /* The code we are generating looks like:
3472 T* t1 = (T*) base;
3473 T* rval = t1;
3474 ptrdiff_t iterator = maxindex;
3475 try {
3476 for (; iterator != -1; --iterator) {
3477 ... initialize *t1 ...
3478 ++t1;
3480 } catch (...) {
3481 ... destroy elements that were constructed ...
3483 rval;
3486 We can omit the try and catch blocks if we know that the
3487 initialization will never throw an exception, or if the array
3488 elements do not have destructors. We can omit the loop completely if
3489 the elements of the array do not have constructors.
3491 We actually wrap the entire body of the above in a STMT_EXPR, for
3492 tidiness.
3494 When copying from array to another, when the array elements have
3495 only trivial copy constructors, we should use __builtin_memcpy
3496 rather than generating a loop. That way, we could take advantage
3497 of whatever cleverness the back end has for dealing with copies
3498 of blocks of memory. */
3500 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
3501 destroy_temps = stmts_are_full_exprs_p ();
3502 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3503 rval = get_temp_regvar (ptype, base);
3504 base = get_temp_regvar (ptype, rval);
3505 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
3507 /* If initializing one array from another, initialize element by
3508 element. We rely upon the below calls to do the argument
3509 checking. Evaluate the initializer before entering the try block. */
3510 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
3512 if (lvalue_kind (init) & clk_rvalueref)
3513 xvalue = true;
3514 base2 = decay_conversion (init, complain);
3515 if (base2 == error_mark_node)
3516 return error_mark_node;
3517 itype = TREE_TYPE (base2);
3518 base2 = get_temp_regvar (itype, base2);
3519 itype = TREE_TYPE (itype);
3522 /* Protect the entire array initialization so that we can destroy
3523 the partially constructed array if an exception is thrown.
3524 But don't do this if we're assigning. */
3525 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3526 && from_array != 2)
3528 try_block = begin_try_block ();
3531 /* If the initializer is {}, then all elements are initialized from {}.
3532 But for non-classes, that's the same as value-initialization. */
3533 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
3534 && CONSTRUCTOR_NELTS (init) == 0)
3536 if (CLASS_TYPE_P (type))
3537 /* Leave init alone. */;
3538 else
3540 init = NULL_TREE;
3541 explicit_value_init_p = true;
3545 /* Maybe pull out constant value when from_array? */
3547 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
3549 /* Do non-default initialization of non-trivial arrays resulting from
3550 brace-enclosed initializers. */
3551 unsigned HOST_WIDE_INT idx;
3552 tree field, elt;
3553 /* Should we try to create a constant initializer? */
3554 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
3555 && TREE_CONSTANT (maxindex)
3556 && (literal_type_p (inner_elt_type)
3557 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
3558 /* If the constructor already has the array type, it's been through
3559 digest_init, so we shouldn't try to do anything more. */
3560 bool digested = same_type_p (atype, TREE_TYPE (init));
3561 bool saw_non_const = false;
3562 bool saw_const = false;
3563 /* If we're initializing a static array, we want to do static
3564 initialization of any elements with constant initializers even if
3565 some are non-constant. */
3566 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
3567 vec<constructor_elt, va_gc> *new_vec;
3568 from_array = 0;
3570 if (length_check)
3572 tree throw_call;
3573 if (array_of_runtime_bound_p (atype))
3574 throw_call = throw_bad_array_length ();
3575 else
3576 throw_call = throw_bad_array_new_length ();
3577 length_check = build3 (COND_EXPR, void_type_node, length_check,
3578 throw_call, void_zero_node);
3579 finish_expr_stmt (length_check);
3582 if (try_const)
3583 vec_alloc (new_vec, CONSTRUCTOR_NELTS (init));
3584 else
3585 new_vec = NULL;
3587 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
3589 tree baseref = build1 (INDIRECT_REF, type, base);
3590 tree one_init;
3592 num_initialized_elts++;
3594 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3595 if (digested)
3596 one_init = build2 (INIT_EXPR, type, baseref, elt);
3597 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3598 one_init = build_aggr_init (baseref, elt, 0, complain);
3599 else
3600 one_init = cp_build_modify_expr (baseref, NOP_EXPR,
3601 elt, complain);
3602 if (one_init == error_mark_node)
3603 errors = true;
3604 if (try_const)
3606 tree e = one_init;
3607 if (TREE_CODE (e) == EXPR_STMT)
3608 e = TREE_OPERAND (e, 0);
3609 if (TREE_CODE (e) == CONVERT_EXPR
3610 && VOID_TYPE_P (TREE_TYPE (e)))
3611 e = TREE_OPERAND (e, 0);
3612 e = maybe_constant_init (e);
3613 if (reduced_constant_expression_p (e))
3615 CONSTRUCTOR_APPEND_ELT (new_vec, field, e);
3616 if (do_static_init)
3617 one_init = NULL_TREE;
3618 else
3619 one_init = build2 (INIT_EXPR, type, baseref, e);
3620 saw_const = true;
3622 else
3624 if (do_static_init)
3626 tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
3627 true);
3628 if (value)
3629 CONSTRUCTOR_APPEND_ELT (new_vec, field, value);
3631 saw_non_const = true;
3635 if (one_init)
3636 finish_expr_stmt (one_init);
3637 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3639 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, 0, complain);
3640 if (one_init == error_mark_node)
3641 errors = true;
3642 else
3643 finish_expr_stmt (one_init);
3645 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3646 complain);
3647 if (one_init == error_mark_node)
3648 errors = true;
3649 else
3650 finish_expr_stmt (one_init);
3653 if (try_const)
3655 if (!saw_non_const)
3656 const_init = build_constructor (atype, new_vec);
3657 else if (do_static_init && saw_const)
3658 DECL_INITIAL (obase) = build_constructor (atype, new_vec);
3659 else
3660 vec_free (new_vec);
3663 /* Any elements without explicit initializers get {}. */
3664 if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type))
3665 init = build_constructor (init_list_type_node, NULL);
3666 else
3668 init = NULL_TREE;
3669 explicit_value_init_p = true;
3672 else if (from_array)
3674 if (init)
3675 /* OK, we set base2 above. */;
3676 else if (CLASS_TYPE_P (type)
3677 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3679 if (complain & tf_error)
3680 error ("initializer ends prematurely");
3681 errors = true;
3685 /* Now, default-initialize any remaining elements. We don't need to
3686 do that if a) the type does not need constructing, or b) we've
3687 already initialized all the elements.
3689 We do need to keep going if we're copying an array. */
3691 if (from_array
3692 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
3693 && ! (tree_fits_shwi_p (maxindex)
3694 && (num_initialized_elts
3695 == tree_to_shwi (maxindex) + 1))))
3697 /* If the ITERATOR is equal to -1, then we don't have to loop;
3698 we've already initialized all the elements. */
3699 tree for_stmt;
3700 tree elt_init;
3701 tree to;
3703 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
3704 finish_for_init_stmt (for_stmt);
3705 finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
3706 build_int_cst (TREE_TYPE (iterator), -1)),
3707 for_stmt, false);
3708 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3709 complain);
3710 if (elt_init == error_mark_node)
3711 errors = true;
3712 finish_for_expr (elt_init, for_stmt);
3714 to = build1 (INDIRECT_REF, type, base);
3716 if (from_array)
3718 tree from;
3720 if (base2)
3722 from = build1 (INDIRECT_REF, itype, base2);
3723 if (xvalue)
3724 from = move (from);
3726 else
3727 from = NULL_TREE;
3729 if (from_array == 2)
3730 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3731 complain);
3732 else if (type_build_ctor_call (type))
3733 elt_init = build_aggr_init (to, from, 0, complain);
3734 else if (from)
3735 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3736 complain);
3737 else
3738 gcc_unreachable ();
3740 else if (TREE_CODE (type) == ARRAY_TYPE)
3742 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
3743 sorry
3744 ("cannot initialize multi-dimensional array with initializer");
3745 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
3746 0, init,
3747 explicit_value_init_p,
3748 0, complain);
3750 else if (explicit_value_init_p)
3752 elt_init = build_value_init (type, complain);
3753 if (elt_init != error_mark_node)
3754 elt_init = build2 (INIT_EXPR, type, to, elt_init);
3756 else
3758 gcc_assert (type_build_ctor_call (type) || init);
3759 if (CLASS_TYPE_P (type))
3760 elt_init = build_aggr_init (to, init, 0, complain);
3761 else
3763 if (TREE_CODE (init) == TREE_LIST)
3764 init = build_x_compound_expr_from_list (init, ELK_INIT,
3765 complain);
3766 elt_init = build2 (INIT_EXPR, type, to, init);
3770 if (elt_init == error_mark_node)
3771 errors = true;
3773 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3774 finish_expr_stmt (elt_init);
3775 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3777 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
3778 complain));
3779 if (base2)
3780 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0,
3781 complain));
3783 finish_for_stmt (for_stmt);
3786 /* Make sure to cleanup any partially constructed elements. */
3787 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3788 && from_array != 2)
3790 tree e;
3791 tree m = cp_build_binary_op (input_location,
3792 MINUS_EXPR, maxindex, iterator,
3793 complain);
3795 /* Flatten multi-dimensional array since build_vec_delete only
3796 expects one-dimensional array. */
3797 if (TREE_CODE (type) == ARRAY_TYPE)
3798 m = cp_build_binary_op (input_location,
3799 MULT_EXPR, m,
3800 /* Avoid mixing signed and unsigned. */
3801 convert (TREE_TYPE (m),
3802 array_type_nelts_total (type)),
3803 complain);
3805 finish_cleanup_try_block (try_block);
3806 e = build_vec_delete_1 (rval, m,
3807 inner_elt_type, sfk_complete_destructor,
3808 /*use_global_delete=*/0, complain);
3809 if (e == error_mark_node)
3810 errors = true;
3811 finish_cleanup (e, try_block);
3814 /* The value of the array initialization is the array itself, RVAL
3815 is a pointer to the first element. */
3816 finish_stmt_expr_expr (rval, stmt_expr);
3818 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
3820 /* Now make the result have the correct type. */
3821 if (TREE_CODE (atype) == ARRAY_TYPE)
3823 atype = build_pointer_type (atype);
3824 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
3825 stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain);
3826 TREE_NO_WARNING (stmt_expr) = 1;
3829 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3831 if (const_init)
3832 return build2 (INIT_EXPR, atype, obase, const_init);
3833 if (errors)
3834 return error_mark_node;
3835 return stmt_expr;
3838 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3839 build_delete. */
3841 static tree
3842 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
3843 tsubst_flags_t complain)
3845 tree name;
3846 tree fn;
3847 switch (dtor_kind)
3849 case sfk_complete_destructor:
3850 name = complete_dtor_identifier;
3851 break;
3853 case sfk_base_destructor:
3854 name = base_dtor_identifier;
3855 break;
3857 case sfk_deleting_destructor:
3858 name = deleting_dtor_identifier;
3859 break;
3861 default:
3862 gcc_unreachable ();
3864 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
3865 return build_new_method_call (exp, fn,
3866 /*args=*/NULL,
3867 /*conversion_path=*/NULL_TREE,
3868 flags,
3869 /*fn_p=*/NULL,
3870 complain);
3873 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3874 ADDR is an expression which yields the store to be destroyed.
3875 AUTO_DELETE is the name of the destructor to call, i.e., either
3876 sfk_complete_destructor, sfk_base_destructor, or
3877 sfk_deleting_destructor.
3879 FLAGS is the logical disjunction of zero or more LOOKUP_
3880 flags. See cp-tree.h for more info. */
3882 tree
3883 build_delete (tree otype, tree addr, special_function_kind auto_delete,
3884 int flags, int use_global_delete, tsubst_flags_t complain)
3886 tree expr;
3888 if (addr == error_mark_node)
3889 return error_mark_node;
3891 tree type = TYPE_MAIN_VARIANT (otype);
3893 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3894 set to `error_mark_node' before it gets properly cleaned up. */
3895 if (type == error_mark_node)
3896 return error_mark_node;
3898 if (TREE_CODE (type) == POINTER_TYPE)
3899 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3901 if (TREE_CODE (type) == ARRAY_TYPE)
3903 if (TYPE_DOMAIN (type) == NULL_TREE)
3905 if (complain & tf_error)
3906 error ("unknown array size in delete");
3907 return error_mark_node;
3909 return build_vec_delete (addr, array_type_nelts (type),
3910 auto_delete, use_global_delete, complain);
3913 if (TYPE_PTR_P (otype))
3915 bool complete_p = true;
3917 addr = mark_rvalue_use (addr);
3919 /* We don't want to warn about delete of void*, only other
3920 incomplete types. Deleting other incomplete types
3921 invokes undefined behavior, but it is not ill-formed, so
3922 compile to something that would even do The Right Thing
3923 (TM) should the type have a trivial dtor and no delete
3924 operator. */
3925 if (!VOID_TYPE_P (type))
3927 complete_type (type);
3928 if (!COMPLETE_TYPE_P (type))
3930 if ((complain & tf_warning)
3931 && warning (OPT_Wdelete_incomplete,
3932 "possible problem detected in invocation of "
3933 "delete operator:"))
3935 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
3936 inform (input_location,
3937 "neither the destructor nor the class-specific "
3938 "operator delete will be called, even if they are "
3939 "declared when the class is defined");
3941 complete_p = false;
3943 else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor
3944 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
3945 && TYPE_POLYMORPHIC_P (type))
3947 tree dtor;
3948 dtor = CLASSTYPE_DESTRUCTORS (type);
3949 if (!dtor || !DECL_VINDEX (dtor))
3951 if (CLASSTYPE_PURE_VIRTUALS (type))
3952 warning (OPT_Wdelete_non_virtual_dtor,
3953 "deleting object of abstract class type %qT"
3954 " which has non-virtual destructor"
3955 " will cause undefined behaviour", type);
3956 else
3957 warning (OPT_Wdelete_non_virtual_dtor,
3958 "deleting object of polymorphic class type %qT"
3959 " which has non-virtual destructor"
3960 " might cause undefined behaviour", type);
3964 if (VOID_TYPE_P (type) || !complete_p || !MAYBE_CLASS_TYPE_P (type))
3965 /* Call the builtin operator delete. */
3966 return build_builtin_delete_call (addr);
3967 if (TREE_SIDE_EFFECTS (addr))
3968 addr = save_expr (addr);
3970 /* Throw away const and volatile on target type of addr. */
3971 addr = convert_force (build_pointer_type (type), addr, 0, complain);
3973 else
3975 /* Don't check PROTECT here; leave that decision to the
3976 destructor. If the destructor is accessible, call it,
3977 else report error. */
3978 addr = cp_build_addr_expr (addr, complain);
3979 if (addr == error_mark_node)
3980 return error_mark_node;
3981 if (TREE_SIDE_EFFECTS (addr))
3982 addr = save_expr (addr);
3984 addr = convert_force (build_pointer_type (type), addr, 0, complain);
3987 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3989 /* Make sure the destructor is callable. */
3990 if (type_build_dtor_call (type))
3992 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL,
3993 complain),
3994 sfk_complete_destructor, flags, complain);
3995 if (expr == error_mark_node)
3996 return error_mark_node;
3999 if (auto_delete != sfk_deleting_destructor)
4000 return void_zero_node;
4002 return build_op_delete_call (DELETE_EXPR, addr,
4003 cxx_sizeof_nowarn (type),
4004 use_global_delete,
4005 /*placement=*/NULL_TREE,
4006 /*alloc_fn=*/NULL_TREE,
4007 complain);
4009 else
4011 tree head = NULL_TREE;
4012 tree do_delete = NULL_TREE;
4013 tree ifexp;
4015 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
4016 lazily_declare_fn (sfk_destructor, type);
4018 /* For `::delete x', we must not use the deleting destructor
4019 since then we would not be sure to get the global `operator
4020 delete'. */
4021 if (use_global_delete && auto_delete == sfk_deleting_destructor)
4023 /* We will use ADDR multiple times so we must save it. */
4024 addr = save_expr (addr);
4025 head = get_target_expr (build_headof (addr));
4026 /* Delete the object. */
4027 do_delete = build_builtin_delete_call (head);
4028 /* Otherwise, treat this like a complete object destructor
4029 call. */
4030 auto_delete = sfk_complete_destructor;
4032 /* If the destructor is non-virtual, there is no deleting
4033 variant. Instead, we must explicitly call the appropriate
4034 `operator delete' here. */
4035 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
4036 && auto_delete == sfk_deleting_destructor)
4038 /* We will use ADDR multiple times so we must save it. */
4039 addr = save_expr (addr);
4040 /* Build the call. */
4041 do_delete = build_op_delete_call (DELETE_EXPR,
4042 addr,
4043 cxx_sizeof_nowarn (type),
4044 /*global_p=*/false,
4045 /*placement=*/NULL_TREE,
4046 /*alloc_fn=*/NULL_TREE,
4047 complain);
4048 /* Call the complete object destructor. */
4049 auto_delete = sfk_complete_destructor;
4051 else if (auto_delete == sfk_deleting_destructor
4052 && TYPE_GETS_REG_DELETE (type))
4054 /* Make sure we have access to the member op delete, even though
4055 we'll actually be calling it from the destructor. */
4056 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
4057 /*global_p=*/false,
4058 /*placement=*/NULL_TREE,
4059 /*alloc_fn=*/NULL_TREE,
4060 complain);
4063 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, complain),
4064 auto_delete, flags, complain);
4065 if (expr == error_mark_node)
4066 return error_mark_node;
4067 if (do_delete)
4068 expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
4070 /* We need to calculate this before the dtor changes the vptr. */
4071 if (head)
4072 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
4074 if (flags & LOOKUP_DESTRUCTOR)
4075 /* Explicit destructor call; don't check for null pointer. */
4076 ifexp = integer_one_node;
4077 else
4079 /* Handle deleting a null pointer. */
4080 ifexp = fold (cp_build_binary_op (input_location,
4081 NE_EXPR, addr, nullptr_node,
4082 complain));
4083 if (ifexp == error_mark_node)
4084 return error_mark_node;
4087 if (ifexp != integer_one_node)
4088 expr = build3 (COND_EXPR, void_type_node,
4089 ifexp, expr, void_zero_node);
4091 return expr;
4095 /* At the beginning of a destructor, push cleanups that will call the
4096 destructors for our base classes and members.
4098 Called from begin_destructor_body. */
4100 void
4101 push_base_cleanups (void)
4103 tree binfo, base_binfo;
4104 int i;
4105 tree member;
4106 tree expr;
4107 vec<tree, va_gc> *vbases;
4109 /* Run destructors for all virtual baseclasses. */
4110 if (CLASSTYPE_VBASECLASSES (current_class_type))
4112 tree cond = (condition_conversion
4113 (build2 (BIT_AND_EXPR, integer_type_node,
4114 current_in_charge_parm,
4115 integer_two_node)));
4117 /* The CLASSTYPE_VBASECLASSES vector is in initialization
4118 order, which is also the right order for pushing cleanups. */
4119 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
4120 vec_safe_iterate (vbases, i, &base_binfo); i++)
4122 if (type_build_dtor_call (BINFO_TYPE (base_binfo)))
4124 expr = build_special_member_call (current_class_ref,
4125 base_dtor_identifier,
4126 NULL,
4127 base_binfo,
4128 (LOOKUP_NORMAL
4129 | LOOKUP_NONVIRTUAL),
4130 tf_warning_or_error);
4131 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
4133 expr = build3 (COND_EXPR, void_type_node, cond,
4134 expr, void_zero_node);
4135 finish_decl_cleanup (NULL_TREE, expr);
4141 /* Take care of the remaining baseclasses. */
4142 for (binfo = TYPE_BINFO (current_class_type), i = 0;
4143 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4145 if (BINFO_VIRTUAL_P (base_binfo)
4146 || !type_build_dtor_call (BINFO_TYPE (base_binfo)))
4147 continue;
4149 expr = build_special_member_call (current_class_ref,
4150 base_dtor_identifier,
4151 NULL, base_binfo,
4152 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
4153 tf_warning_or_error);
4154 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
4155 finish_decl_cleanup (NULL_TREE, expr);
4158 /* Don't automatically destroy union members. */
4159 if (TREE_CODE (current_class_type) == UNION_TYPE)
4160 return;
4162 for (member = TYPE_FIELDS (current_class_type); member;
4163 member = DECL_CHAIN (member))
4165 tree this_type = TREE_TYPE (member);
4166 if (this_type == error_mark_node
4167 || TREE_CODE (member) != FIELD_DECL
4168 || DECL_ARTIFICIAL (member))
4169 continue;
4170 if (ANON_AGGR_TYPE_P (this_type))
4171 continue;
4172 if (type_build_dtor_call (this_type))
4174 tree this_member = (build_class_member_access_expr
4175 (current_class_ref, member,
4176 /*access_path=*/NULL_TREE,
4177 /*preserve_reference=*/false,
4178 tf_warning_or_error));
4179 expr = build_delete (this_type, this_member,
4180 sfk_complete_destructor,
4181 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
4182 0, tf_warning_or_error);
4183 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
4184 finish_decl_cleanup (NULL_TREE, expr);
4189 /* Build a C++ vector delete expression.
4190 MAXINDEX is the number of elements to be deleted.
4191 ELT_SIZE is the nominal size of each element in the vector.
4192 BASE is the expression that should yield the store to be deleted.
4193 This function expands (or synthesizes) these calls itself.
4194 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
4196 This also calls delete for virtual baseclasses of elements of the vector.
4198 Update: MAXINDEX is no longer needed. The size can be extracted from the
4199 start of the vector for pointers, and from the type for arrays. We still
4200 use MAXINDEX for arrays because it happens to already have one of the
4201 values we'd have to extract. (We could use MAXINDEX with pointers to
4202 confirm the size, and trap if the numbers differ; not clear that it'd
4203 be worth bothering.) */
4205 tree
4206 build_vec_delete (tree base, tree maxindex,
4207 special_function_kind auto_delete_vec,
4208 int use_global_delete, tsubst_flags_t complain)
4210 tree type;
4211 tree rval;
4212 tree base_init = NULL_TREE;
4214 type = TREE_TYPE (base);
4216 if (TYPE_PTR_P (type))
4218 /* Step back one from start of vector, and read dimension. */
4219 tree cookie_addr;
4220 tree size_ptr_type = build_pointer_type (sizetype);
4222 base = mark_rvalue_use (base);
4223 if (TREE_SIDE_EFFECTS (base))
4225 base_init = get_target_expr (base);
4226 base = TARGET_EXPR_SLOT (base_init);
4228 type = strip_array_types (TREE_TYPE (type));
4229 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
4230 sizetype, TYPE_SIZE_UNIT (sizetype));
4231 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
4232 cookie_addr);
4233 maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, complain);
4235 else if (TREE_CODE (type) == ARRAY_TYPE)
4237 /* Get the total number of things in the array, maxindex is a
4238 bad name. */
4239 maxindex = array_type_nelts_total (type);
4240 type = strip_array_types (type);
4241 base = decay_conversion (base, complain);
4242 if (base == error_mark_node)
4243 return error_mark_node;
4244 if (TREE_SIDE_EFFECTS (base))
4246 base_init = get_target_expr (base);
4247 base = TARGET_EXPR_SLOT (base_init);
4250 else
4252 if (base != error_mark_node && !(complain & tf_error))
4253 error ("type to vector delete is neither pointer or array type");
4254 return error_mark_node;
4257 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
4258 use_global_delete, complain);
4259 if (base_init && rval != error_mark_node)
4260 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
4262 return rval;