* gcc.dg/atomic-compare-exchange-1.c,
[official-gcc.git] / gcc / cp / init.c
bloba4d8280dfd8a907cb2455aa697819703fb9d6ce9
1 /* Handle initialization things in C++.
2 Copyright (C) 1987-2013 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 "cp-tree.h"
29 #include "flags.h"
30 #include "target.h"
32 static bool begin_init_stmts (tree *, tree *);
33 static tree finish_init_stmts (bool, tree, tree);
34 static void construct_virtual_base (tree, tree);
35 static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
36 static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
37 static void perform_member_init (tree, tree);
38 static tree build_builtin_delete_call (tree);
39 static int member_init_ok_or_else (tree, tree, tree);
40 static void expand_virtual_init (tree, tree);
41 static tree sort_mem_initializers (tree, tree);
42 static tree initializing_context (tree);
43 static void expand_cleanup_for_base (tree, tree);
44 static tree dfs_initialize_vtbl_ptrs (tree, void *);
45 static tree build_field_list (tree, tree, int *);
46 static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
48 /* We are about to generate some complex initialization code.
49 Conceptually, it is all a single expression. However, we may want
50 to include conditionals, loops, and other such statement-level
51 constructs. Therefore, we build the initialization code inside a
52 statement-expression. This function starts such an expression.
53 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
54 pass them back to finish_init_stmts when the expression is
55 complete. */
57 static bool
58 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
60 bool is_global = !building_stmt_list_p ();
62 *stmt_expr_p = begin_stmt_expr ();
63 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
65 return is_global;
68 /* Finish out the statement-expression begun by the previous call to
69 begin_init_stmts. Returns the statement-expression itself. */
71 static tree
72 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
74 finish_compound_stmt (compound_stmt);
76 stmt_expr = finish_stmt_expr (stmt_expr, true);
78 gcc_assert (!building_stmt_list_p () == is_global);
80 return stmt_expr;
83 /* Constructors */
85 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
86 which we want to initialize the vtable pointer for, DATA is
87 TREE_LIST whose TREE_VALUE is the this ptr expression. */
89 static tree
90 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
92 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
93 return dfs_skip_bases;
95 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
97 tree base_ptr = TREE_VALUE ((tree) data);
99 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
100 tf_warning_or_error);
102 expand_virtual_init (binfo, base_ptr);
105 return NULL_TREE;
108 /* Initialize all the vtable pointers in the object pointed to by
109 ADDR. */
111 void
112 initialize_vtbl_ptrs (tree addr)
114 tree list;
115 tree type;
117 type = TREE_TYPE (TREE_TYPE (addr));
118 list = build_tree_list (type, addr);
120 /* Walk through the hierarchy, initializing the vptr in each base
121 class. We do these in pre-order because we can't find the virtual
122 bases for a class until we've initialized the vtbl for that
123 class. */
124 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
127 /* Return an expression for the zero-initialization of an object with
128 type T. This expression will either be a constant (in the case
129 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
130 aggregate), or NULL (in the case that T does not require
131 initialization). In either case, the value can be used as
132 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
133 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
134 is the number of elements in the array. If STATIC_STORAGE_P is
135 TRUE, initializers are only generated for entities for which
136 zero-initialization does not simply mean filling the storage with
137 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
138 subfields with bit positions at or above that bit size shouldn't
139 be added. Note that this only works when the result is assigned
140 to a base COMPONENT_REF; if we only have a pointer to the base subobject,
141 expand_assignment will end up clearing the full size of TYPE. */
143 static tree
144 build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
145 tree field_size)
147 tree init = NULL_TREE;
149 /* [dcl.init]
151 To zero-initialize an object of type T means:
153 -- if T is a scalar type, the storage is set to the value of zero
154 converted to T.
156 -- if T is a non-union class type, the storage for each nonstatic
157 data member and each base-class subobject is zero-initialized.
159 -- if T is a union type, the storage for its first data member is
160 zero-initialized.
162 -- if T is an array type, the storage for each element is
163 zero-initialized.
165 -- if T is a reference type, no initialization is performed. */
167 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
169 if (type == error_mark_node)
171 else if (static_storage_p && zero_init_p (type))
172 /* In order to save space, we do not explicitly build initializers
173 for items that do not need them. GCC's semantics are that
174 items with static storage duration that are not otherwise
175 initialized are initialized to zero. */
177 else if (TYPE_PTR_OR_PTRMEM_P (type))
178 init = convert (type, nullptr_node);
179 else if (SCALAR_TYPE_P (type))
180 init = convert (type, integer_zero_node);
181 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
183 tree field;
184 vec<constructor_elt, va_gc> *v = NULL;
186 /* Iterate over the fields, building initializations. */
187 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
189 if (TREE_CODE (field) != FIELD_DECL)
190 continue;
192 /* Don't add virtual bases for base classes if they are beyond
193 the size of the current field, that means it is present
194 somewhere else in the object. */
195 if (field_size)
197 tree bitpos = bit_position (field);
198 if (TREE_CODE (bitpos) == INTEGER_CST
199 && !tree_int_cst_lt (bitpos, field_size))
200 continue;
203 /* Note that for class types there will be FIELD_DECLs
204 corresponding to base classes as well. Thus, iterating
205 over TYPE_FIELDs will result in correct initialization of
206 all of the subobjects. */
207 if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
209 tree new_field_size
210 = (DECL_FIELD_IS_BASE (field)
211 && DECL_SIZE (field)
212 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
213 ? DECL_SIZE (field) : NULL_TREE;
214 tree value = build_zero_init_1 (TREE_TYPE (field),
215 /*nelts=*/NULL_TREE,
216 static_storage_p,
217 new_field_size);
218 if (value)
219 CONSTRUCTOR_APPEND_ELT(v, field, value);
222 /* For unions, only the first field is initialized. */
223 if (TREE_CODE (type) == UNION_TYPE)
224 break;
227 /* Build a constructor to contain the initializations. */
228 init = build_constructor (type, v);
230 else if (TREE_CODE (type) == ARRAY_TYPE)
232 tree max_index;
233 vec<constructor_elt, va_gc> *v = NULL;
235 /* Iterate over the array elements, building initializations. */
236 if (nelts)
237 max_index = fold_build2_loc (input_location,
238 MINUS_EXPR, TREE_TYPE (nelts),
239 nelts, integer_one_node);
240 else
241 max_index = array_type_nelts (type);
243 /* If we have an error_mark here, we should just return error mark
244 as we don't know the size of the array yet. */
245 if (max_index == error_mark_node)
246 return error_mark_node;
247 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
249 /* A zero-sized array, which is accepted as an extension, will
250 have an upper bound of -1. */
251 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
253 constructor_elt ce;
255 /* If this is a one element array, we just use a regular init. */
256 if (tree_int_cst_equal (size_zero_node, max_index))
257 ce.index = size_zero_node;
258 else
259 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
260 max_index);
262 ce.value = build_zero_init_1 (TREE_TYPE (type),
263 /*nelts=*/NULL_TREE,
264 static_storage_p, NULL_TREE);
265 if (ce.value)
267 vec_alloc (v, 1);
268 v->quick_push (ce);
272 /* Build a constructor to contain the initializations. */
273 init = build_constructor (type, v);
275 else if (TREE_CODE (type) == VECTOR_TYPE)
276 init = build_zero_cst (type);
277 else
278 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
280 /* In all cases, the initializer is a constant. */
281 if (init)
282 TREE_CONSTANT (init) = 1;
284 return init;
287 /* Return an expression for the zero-initialization of an object with
288 type T. This expression will either be a constant (in the case
289 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
290 aggregate), or NULL (in the case that T does not require
291 initialization). In either case, the value can be used as
292 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
293 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
294 is the number of elements in the array. If STATIC_STORAGE_P is
295 TRUE, initializers are only generated for entities for which
296 zero-initialization does not simply mean filling the storage with
297 zero bytes. */
299 tree
300 build_zero_init (tree type, tree nelts, bool static_storage_p)
302 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
305 /* Return a suitable initializer for value-initializing an object of type
306 TYPE, as described in [dcl.init]. */
308 tree
309 build_value_init (tree type, tsubst_flags_t complain)
311 /* [dcl.init]
313 To value-initialize an object of type T means:
315 - if T is a class type (clause 9) with either no default constructor
316 (12.1) or a default constructor that is user-provided or deleted,
317 then then the object is default-initialized;
319 - if T is a (possibly cv-qualified) class type without a user-provided
320 or deleted default constructor, then the object is zero-initialized
321 and the semantic constraints for default-initialization are checked,
322 and if T has a non-trivial default constructor, the object is
323 default-initialized;
325 - if T is an array type, then each element is value-initialized;
327 - otherwise, the object is zero-initialized.
329 A program that calls for default-initialization or
330 value-initialization of an entity of reference type is ill-formed. */
332 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
333 gcc_assert (!processing_template_decl
334 || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
336 if (type_build_ctor_call (type))
338 tree ctor = build_aggr_init_expr
339 (type,
340 build_special_member_call (NULL_TREE, complete_ctor_identifier,
341 NULL, type, LOOKUP_NORMAL,
342 complain));
343 if (ctor == error_mark_node
344 || type_has_user_provided_default_constructor (type))
345 return ctor;
346 else if (TYPE_HAS_COMPLEX_DFLT (type))
348 /* This is a class that needs constructing, but doesn't have
349 a user-provided constructor. So we need to zero-initialize
350 the object and then call the implicitly defined ctor.
351 This will be handled in simplify_aggr_init_expr. */
352 AGGR_INIT_ZERO_FIRST (ctor) = 1;
353 return ctor;
357 /* Discard any access checking during subobject initialization;
358 the checks are implied by the call to the ctor which we have
359 verified is OK (cpp0x/defaulted46.C). */
360 push_deferring_access_checks (dk_deferred);
361 tree r = build_value_init_noctor (type, complain);
362 pop_deferring_access_checks ();
363 return r;
366 /* Like build_value_init, but don't call the constructor for TYPE. Used
367 for base initializers. */
369 tree
370 build_value_init_noctor (tree type, tsubst_flags_t complain)
372 if (!COMPLETE_TYPE_P (type))
374 if (complain & tf_error)
375 error ("value-initialization of incomplete type %qT", type);
376 return error_mark_node;
378 /* FIXME the class and array cases should just use digest_init once it is
379 SFINAE-enabled. */
380 if (CLASS_TYPE_P (type))
382 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type));
384 if (TREE_CODE (type) != UNION_TYPE)
386 tree field;
387 vec<constructor_elt, va_gc> *v = NULL;
389 /* Iterate over the fields, building initializations. */
390 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
392 tree ftype, value;
394 if (TREE_CODE (field) != FIELD_DECL)
395 continue;
397 ftype = TREE_TYPE (field);
399 /* We could skip vfields and fields of types with
400 user-defined constructors, but I think that won't improve
401 performance at all; it should be simpler in general just
402 to zero out the entire object than try to only zero the
403 bits that actually need it. */
405 /* Note that for class types there will be FIELD_DECLs
406 corresponding to base classes as well. Thus, iterating
407 over TYPE_FIELDs will result in correct initialization of
408 all of the subobjects. */
409 value = build_value_init (ftype, complain);
411 if (value == error_mark_node)
412 return error_mark_node;
414 if (value)
415 CONSTRUCTOR_APPEND_ELT(v, field, value);
418 /* Build a constructor to contain the zero- initializations. */
419 return build_constructor (type, v);
422 else if (TREE_CODE (type) == ARRAY_TYPE)
424 vec<constructor_elt, va_gc> *v = NULL;
426 /* Iterate over the array elements, building initializations. */
427 tree max_index = array_type_nelts (type);
429 /* If we have an error_mark here, we should just return error mark
430 as we don't know the size of the array yet. */
431 if (max_index == error_mark_node)
433 if (complain & tf_error)
434 error ("cannot value-initialize array of unknown bound %qT",
435 type);
436 return error_mark_node;
438 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
440 /* A zero-sized array, which is accepted as an extension, will
441 have an upper bound of -1. */
442 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
444 constructor_elt ce;
446 /* If this is a one element array, we just use a regular init. */
447 if (tree_int_cst_equal (size_zero_node, max_index))
448 ce.index = size_zero_node;
449 else
450 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
452 ce.value = build_value_init (TREE_TYPE (type), complain);
453 if (ce.value)
455 if (ce.value == error_mark_node)
456 return error_mark_node;
458 vec_alloc (v, 1);
459 v->quick_push (ce);
461 /* We shouldn't have gotten here for anything that would need
462 non-trivial initialization, and gimplify_init_ctor_preeval
463 would need to be fixed to allow it. */
464 gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
465 && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
469 /* Build a constructor to contain the initializations. */
470 return build_constructor (type, v);
472 else if (TREE_CODE (type) == FUNCTION_TYPE)
474 if (complain & tf_error)
475 error ("value-initialization of function type %qT", type);
476 return error_mark_node;
478 else if (TREE_CODE (type) == REFERENCE_TYPE)
480 if (complain & tf_error)
481 error ("value-initialization of reference type %qT", type);
482 return error_mark_node;
485 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
488 /* Initialize current class with INIT, a TREE_LIST of
489 arguments for a target constructor. If TREE_LIST is void_type_node,
490 an empty initializer list was given. */
492 static void
493 perform_target_ctor (tree init)
495 tree decl = current_class_ref;
496 tree type = current_class_type;
498 finish_expr_stmt (build_aggr_init (decl, init,
499 LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
500 tf_warning_or_error));
501 if (type_build_dtor_call (type))
503 tree expr = build_delete (type, decl, sfk_complete_destructor,
504 LOOKUP_NORMAL
505 |LOOKUP_NONVIRTUAL
506 |LOOKUP_DESTRUCTOR,
507 0, tf_warning_or_error);
508 if (expr != error_mark_node
509 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
510 finish_eh_cleanup (expr);
514 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
515 arguments. If TREE_LIST is void_type_node, an empty initializer
516 list was given; if NULL_TREE no initializer was given. */
518 static void
519 perform_member_init (tree member, tree init)
521 tree decl;
522 tree type = TREE_TYPE (member);
524 /* Use the non-static data member initializer if there was no
525 mem-initializer for this field. */
526 if (init == NULL_TREE)
528 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
529 /* Do deferred instantiation of the NSDMI. */
530 init = (tsubst_copy_and_build
531 (DECL_INITIAL (DECL_TI_TEMPLATE (member)),
532 DECL_TI_ARGS (member),
533 tf_warning_or_error, member, /*function_p=*/false,
534 /*integral_constant_expression_p=*/false));
535 else
537 init = DECL_INITIAL (member);
538 if (init && TREE_CODE (init) == DEFAULT_ARG)
540 error ("constructor required before non-static data member "
541 "for %qD has been parsed", member);
542 init = NULL_TREE;
544 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
545 so the aggregate init code below will see a CONSTRUCTOR. */
546 if (init && TREE_CODE (init) == TARGET_EXPR
547 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
548 init = TARGET_EXPR_INITIAL (init);
549 init = break_out_target_exprs (init);
553 if (init == error_mark_node)
554 return;
556 /* Effective C++ rule 12 requires that all data members be
557 initialized. */
558 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
559 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
560 "%qD should be initialized in the member initialization list",
561 member);
563 /* Get an lvalue for the data member. */
564 decl = build_class_member_access_expr (current_class_ref, member,
565 /*access_path=*/NULL_TREE,
566 /*preserve_reference=*/true,
567 tf_warning_or_error);
568 if (decl == error_mark_node)
569 return;
571 if (warn_init_self && init && TREE_CODE (init) == TREE_LIST
572 && TREE_CHAIN (init) == NULL_TREE)
574 tree val = TREE_VALUE (init);
575 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
576 && TREE_OPERAND (val, 0) == current_class_ref)
577 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
578 OPT_Winit_self, "%qD is initialized with itself",
579 member);
582 if (init == void_type_node)
584 /* mem() means value-initialization. */
585 if (TREE_CODE (type) == ARRAY_TYPE)
587 init = build_vec_init_expr (type, init, tf_warning_or_error);
588 init = build2 (INIT_EXPR, type, decl, init);
589 finish_expr_stmt (init);
591 else
593 tree value = build_value_init (type, tf_warning_or_error);
594 if (value == error_mark_node)
595 return;
596 init = build2 (INIT_EXPR, type, decl, value);
597 finish_expr_stmt (init);
600 /* Deal with this here, as we will get confused if we try to call the
601 assignment op for an anonymous union. This can happen in a
602 synthesized copy constructor. */
603 else if (ANON_AGGR_TYPE_P (type))
605 if (init)
607 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
608 finish_expr_stmt (init);
611 else if (init
612 && (TREE_CODE (type) == REFERENCE_TYPE
613 /* Pre-digested NSDMI. */
614 || (((TREE_CODE (init) == CONSTRUCTOR
615 && TREE_TYPE (init) == type)
616 /* { } mem-initializer. */
617 || (TREE_CODE (init) == TREE_LIST
618 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
619 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init))))
620 && (CP_AGGREGATE_TYPE_P (type)
621 || is_std_init_list (type)))))
623 /* With references and list-initialization, we need to deal with
624 extending temporary lifetimes. 12.2p5: "A temporary bound to a
625 reference member in a constructor’s ctor-initializer (12.6.2)
626 persists until the constructor exits." */
627 unsigned i; tree t;
628 vec<tree, va_gc> *cleanups = make_tree_vector ();
629 if (TREE_CODE (init) == TREE_LIST)
630 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
631 tf_warning_or_error);
632 if (TREE_TYPE (init) != type)
634 if (BRACE_ENCLOSED_INITIALIZER_P (init)
635 && CP_AGGREGATE_TYPE_P (type))
636 init = reshape_init (type, init, tf_warning_or_error);
637 init = digest_init (type, init, tf_warning_or_error);
639 if (init == error_mark_node)
640 return;
641 /* A FIELD_DECL doesn't really have a suitable lifetime, but
642 make_temporary_var_for_ref_to_temp will treat it as automatic and
643 set_up_extended_ref_temp wants to use the decl in a warning. */
644 init = extend_ref_init_temps (member, init, &cleanups);
645 if (TREE_CODE (type) == ARRAY_TYPE
646 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
647 init = build_vec_init_expr (type, init, tf_warning_or_error);
648 init = build2 (INIT_EXPR, type, decl, init);
649 finish_expr_stmt (init);
650 FOR_EACH_VEC_ELT (*cleanups, i, t)
651 push_cleanup (decl, t, false);
652 release_tree_vector (cleanups);
654 else if (type_build_ctor_call (type)
655 || (init && CLASS_TYPE_P (strip_array_types (type))))
657 if (TREE_CODE (type) == ARRAY_TYPE)
659 if (init)
661 if (TREE_CHAIN (init))
662 init = error_mark_node;
663 else
664 init = TREE_VALUE (init);
665 if (BRACE_ENCLOSED_INITIALIZER_P (init))
666 init = digest_init (type, init, tf_warning_or_error);
668 if (init == NULL_TREE
669 || same_type_ignoring_top_level_qualifiers_p (type,
670 TREE_TYPE (init)))
672 init = build_vec_init_expr (type, init, tf_warning_or_error);
673 init = build2 (INIT_EXPR, type, decl, init);
674 finish_expr_stmt (init);
676 else
677 error ("invalid initializer for array member %q#D", member);
679 else
681 int flags = LOOKUP_NORMAL;
682 if (DECL_DEFAULTED_FN (current_function_decl))
683 flags |= LOOKUP_DEFAULTED;
684 if (CP_TYPE_CONST_P (type)
685 && init == NULL_TREE
686 && default_init_uninitialized_part (type))
687 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
688 vtable; still give this diagnostic. */
689 permerror (DECL_SOURCE_LOCATION (current_function_decl),
690 "uninitialized member %qD with %<const%> type %qT",
691 member, type);
692 finish_expr_stmt (build_aggr_init (decl, init, flags,
693 tf_warning_or_error));
696 else
698 if (init == NULL_TREE)
700 tree core_type;
701 /* member traversal: note it leaves init NULL */
702 if (TREE_CODE (type) == REFERENCE_TYPE)
703 permerror (DECL_SOURCE_LOCATION (current_function_decl),
704 "uninitialized reference member %qD",
705 member);
706 else if (CP_TYPE_CONST_P (type))
707 permerror (DECL_SOURCE_LOCATION (current_function_decl),
708 "uninitialized member %qD with %<const%> type %qT",
709 member, type);
711 core_type = strip_array_types (type);
713 if (CLASS_TYPE_P (core_type)
714 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
715 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
716 diagnose_uninitialized_cst_or_ref_member (core_type,
717 /*using_new=*/false,
718 /*complain=*/true);
720 else if (TREE_CODE (init) == TREE_LIST)
721 /* There was an explicit member initialization. Do some work
722 in that case. */
723 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
724 tf_warning_or_error);
726 if (init)
727 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
728 tf_warning_or_error));
731 if (type_build_dtor_call (type))
733 tree expr;
735 expr = build_class_member_access_expr (current_class_ref, member,
736 /*access_path=*/NULL_TREE,
737 /*preserve_reference=*/false,
738 tf_warning_or_error);
739 expr = build_delete (type, expr, sfk_complete_destructor,
740 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
741 tf_warning_or_error);
743 if (expr != error_mark_node
744 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
745 finish_eh_cleanup (expr);
749 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
750 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
752 static tree
753 build_field_list (tree t, tree list, int *uses_unions_p)
755 tree fields;
757 /* Note whether or not T is a union. */
758 if (TREE_CODE (t) == UNION_TYPE)
759 *uses_unions_p = 1;
761 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
763 tree fieldtype;
765 /* Skip CONST_DECLs for enumeration constants and so forth. */
766 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
767 continue;
769 fieldtype = TREE_TYPE (fields);
770 /* Keep track of whether or not any fields are unions. */
771 if (TREE_CODE (fieldtype) == UNION_TYPE)
772 *uses_unions_p = 1;
774 /* For an anonymous struct or union, we must recursively
775 consider the fields of the anonymous type. They can be
776 directly initialized from the constructor. */
777 if (ANON_AGGR_TYPE_P (fieldtype))
779 /* Add this field itself. Synthesized copy constructors
780 initialize the entire aggregate. */
781 list = tree_cons (fields, NULL_TREE, list);
782 /* And now add the fields in the anonymous aggregate. */
783 list = build_field_list (fieldtype, list, uses_unions_p);
785 /* Add this field. */
786 else if (DECL_NAME (fields))
787 list = tree_cons (fields, NULL_TREE, list);
790 return list;
793 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
794 a FIELD_DECL or BINFO in T that needs initialization. The
795 TREE_VALUE gives the initializer, or list of initializer arguments.
797 Return a TREE_LIST containing all of the initializations required
798 for T, in the order in which they should be performed. The output
799 list has the same format as the input. */
801 static tree
802 sort_mem_initializers (tree t, tree mem_inits)
804 tree init;
805 tree base, binfo, base_binfo;
806 tree sorted_inits;
807 tree next_subobject;
808 vec<tree, va_gc> *vbases;
809 int i;
810 int uses_unions_p = 0;
812 /* Build up a list of initializations. The TREE_PURPOSE of entry
813 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
814 TREE_VALUE will be the constructor arguments, or NULL if no
815 explicit initialization was provided. */
816 sorted_inits = NULL_TREE;
818 /* Process the virtual bases. */
819 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
820 vec_safe_iterate (vbases, i, &base); i++)
821 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
823 /* Process the direct bases. */
824 for (binfo = TYPE_BINFO (t), i = 0;
825 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
826 if (!BINFO_VIRTUAL_P (base_binfo))
827 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
829 /* Process the non-static data members. */
830 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
831 /* Reverse the entire list of initializations, so that they are in
832 the order that they will actually be performed. */
833 sorted_inits = nreverse (sorted_inits);
835 /* If the user presented the initializers in an order different from
836 that in which they will actually occur, we issue a warning. Keep
837 track of the next subobject which can be explicitly initialized
838 without issuing a warning. */
839 next_subobject = sorted_inits;
841 /* Go through the explicit initializers, filling in TREE_PURPOSE in
842 the SORTED_INITS. */
843 for (init = mem_inits; init; init = TREE_CHAIN (init))
845 tree subobject;
846 tree subobject_init;
848 subobject = TREE_PURPOSE (init);
850 /* If the explicit initializers are in sorted order, then
851 SUBOBJECT will be NEXT_SUBOBJECT, or something following
852 it. */
853 for (subobject_init = next_subobject;
854 subobject_init;
855 subobject_init = TREE_CHAIN (subobject_init))
856 if (TREE_PURPOSE (subobject_init) == subobject)
857 break;
859 /* Issue a warning if the explicit initializer order does not
860 match that which will actually occur.
861 ??? Are all these on the correct lines? */
862 if (warn_reorder && !subobject_init)
864 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
865 warning (OPT_Wreorder, "%q+D will be initialized after",
866 TREE_PURPOSE (next_subobject));
867 else
868 warning (OPT_Wreorder, "base %qT will be initialized after",
869 TREE_PURPOSE (next_subobject));
870 if (TREE_CODE (subobject) == FIELD_DECL)
871 warning (OPT_Wreorder, " %q+#D", subobject);
872 else
873 warning (OPT_Wreorder, " base %qT", subobject);
874 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
875 OPT_Wreorder, " when initialized here");
878 /* Look again, from the beginning of the list. */
879 if (!subobject_init)
881 subobject_init = sorted_inits;
882 while (TREE_PURPOSE (subobject_init) != subobject)
883 subobject_init = TREE_CHAIN (subobject_init);
886 /* It is invalid to initialize the same subobject more than
887 once. */
888 if (TREE_VALUE (subobject_init))
890 if (TREE_CODE (subobject) == FIELD_DECL)
891 error_at (DECL_SOURCE_LOCATION (current_function_decl),
892 "multiple initializations given for %qD",
893 subobject);
894 else
895 error_at (DECL_SOURCE_LOCATION (current_function_decl),
896 "multiple initializations given for base %qT",
897 subobject);
900 /* Record the initialization. */
901 TREE_VALUE (subobject_init) = TREE_VALUE (init);
902 next_subobject = subobject_init;
905 /* [class.base.init]
907 If a ctor-initializer specifies more than one mem-initializer for
908 multiple members of the same union (including members of
909 anonymous unions), the ctor-initializer is ill-formed.
911 Here we also splice out uninitialized union members. */
912 if (uses_unions_p)
914 tree *last_p = NULL;
915 tree *p;
916 for (p = &sorted_inits; *p; )
918 tree field;
919 tree ctx;
921 init = *p;
923 field = TREE_PURPOSE (init);
925 /* Skip base classes. */
926 if (TREE_CODE (field) != FIELD_DECL)
927 goto next;
929 /* If this is an anonymous union with no explicit initializer,
930 splice it out. */
931 if (!TREE_VALUE (init) && ANON_UNION_TYPE_P (TREE_TYPE (field)))
932 goto splice;
934 /* See if this field is a member of a union, or a member of a
935 structure contained in a union, etc. */
936 for (ctx = DECL_CONTEXT (field);
937 !same_type_p (ctx, t);
938 ctx = TYPE_CONTEXT (ctx))
939 if (TREE_CODE (ctx) == UNION_TYPE
940 || !ANON_AGGR_TYPE_P (ctx))
941 break;
942 /* If this field is not a member of a union, skip it. */
943 if (TREE_CODE (ctx) != UNION_TYPE)
944 goto next;
946 /* If this union member has no explicit initializer and no NSDMI,
947 splice it out. */
948 if (TREE_VALUE (init) || DECL_INITIAL (field))
949 /* OK. */;
950 else
951 goto splice;
953 /* It's only an error if we have two initializers for the same
954 union type. */
955 if (!last_p)
957 last_p = p;
958 goto next;
961 /* See if LAST_FIELD and the field initialized by INIT are
962 members of the same union. If so, there's a problem,
963 unless they're actually members of the same structure
964 which is itself a member of a union. For example, given:
966 union { struct { int i; int j; }; };
968 initializing both `i' and `j' makes sense. */
969 ctx = common_enclosing_class (DECL_CONTEXT (field),
970 DECL_CONTEXT (TREE_PURPOSE (*last_p)));
972 if (ctx && TREE_CODE (ctx) == UNION_TYPE)
974 /* A mem-initializer hides an NSDMI. */
975 if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
976 *last_p = TREE_CHAIN (*last_p);
977 else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
978 goto splice;
979 else
981 error_at (DECL_SOURCE_LOCATION (current_function_decl),
982 "initializations for multiple members of %qT",
983 ctx);
984 goto splice;
988 last_p = p;
990 next:
991 p = &TREE_CHAIN (*p);
992 continue;
993 splice:
994 *p = TREE_CHAIN (*p);
995 continue;
999 return sorted_inits;
1002 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1003 is a TREE_LIST giving the explicit mem-initializer-list for the
1004 constructor. The TREE_PURPOSE of each entry is a subobject (a
1005 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1006 is a TREE_LIST giving the arguments to the constructor or
1007 void_type_node for an empty list of arguments. */
1009 void
1010 emit_mem_initializers (tree mem_inits)
1012 int flags = LOOKUP_NORMAL;
1014 /* We will already have issued an error message about the fact that
1015 the type is incomplete. */
1016 if (!COMPLETE_TYPE_P (current_class_type))
1017 return;
1019 if (mem_inits
1020 && TYPE_P (TREE_PURPOSE (mem_inits))
1021 && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1023 /* Delegating constructor. */
1024 gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
1025 perform_target_ctor (TREE_VALUE (mem_inits));
1026 return;
1029 if (DECL_DEFAULTED_FN (current_function_decl)
1030 && ! DECL_INHERITED_CTOR_BASE (current_function_decl))
1031 flags |= LOOKUP_DEFAULTED;
1033 /* Sort the mem-initializers into the order in which the
1034 initializations should be performed. */
1035 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
1037 in_base_initializer = 1;
1039 /* Initialize base classes. */
1040 for (; (mem_inits
1041 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
1042 mem_inits = TREE_CHAIN (mem_inits))
1044 tree subobject = TREE_PURPOSE (mem_inits);
1045 tree arguments = TREE_VALUE (mem_inits);
1047 /* We already have issued an error message. */
1048 if (arguments == error_mark_node)
1049 continue;
1051 if (arguments == NULL_TREE)
1053 /* If these initializations are taking place in a copy constructor,
1054 the base class should probably be explicitly initialized if there
1055 is a user-defined constructor in the base class (other than the
1056 default constructor, which will be called anyway). */
1057 if (extra_warnings
1058 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1059 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1060 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1061 OPT_Wextra, "base class %q#T should be explicitly "
1062 "initialized in the copy constructor",
1063 BINFO_TYPE (subobject));
1066 /* Initialize the base. */
1067 if (BINFO_VIRTUAL_P (subobject))
1068 construct_virtual_base (subobject, arguments);
1069 else
1071 tree base_addr;
1073 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1074 subobject, 1, tf_warning_or_error);
1075 expand_aggr_init_1 (subobject, NULL_TREE,
1076 cp_build_indirect_ref (base_addr, RO_NULL,
1077 tf_warning_or_error),
1078 arguments,
1079 flags,
1080 tf_warning_or_error);
1081 expand_cleanup_for_base (subobject, NULL_TREE);
1084 in_base_initializer = 0;
1086 /* Initialize the vptrs. */
1087 initialize_vtbl_ptrs (current_class_ptr);
1089 /* Initialize the data members. */
1090 while (mem_inits)
1092 perform_member_init (TREE_PURPOSE (mem_inits),
1093 TREE_VALUE (mem_inits));
1094 mem_inits = TREE_CHAIN (mem_inits);
1098 /* Returns the address of the vtable (i.e., the value that should be
1099 assigned to the vptr) for BINFO. */
1101 tree
1102 build_vtbl_address (tree binfo)
1104 tree binfo_for = binfo;
1105 tree vtbl;
1107 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1108 /* If this is a virtual primary base, then the vtable we want to store
1109 is that for the base this is being used as the primary base of. We
1110 can't simply skip the initialization, because we may be expanding the
1111 inits of a subobject constructor where the virtual base layout
1112 can be different. */
1113 while (BINFO_PRIMARY_P (binfo_for))
1114 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1116 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1117 used. */
1118 vtbl = get_vtbl_decl_for_binfo (binfo_for);
1119 TREE_USED (vtbl) = 1;
1121 /* Now compute the address to use when initializing the vptr. */
1122 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1123 if (VAR_P (vtbl))
1124 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1126 return vtbl;
1129 /* This code sets up the virtual function tables appropriate for
1130 the pointer DECL. It is a one-ply initialization.
1132 BINFO is the exact type that DECL is supposed to be. In
1133 multiple inheritance, this might mean "C's A" if C : A, B. */
1135 static void
1136 expand_virtual_init (tree binfo, tree decl)
1138 tree vtbl, vtbl_ptr;
1139 tree vtt_index;
1141 /* Compute the initializer for vptr. */
1142 vtbl = build_vtbl_address (binfo);
1144 /* We may get this vptr from a VTT, if this is a subobject
1145 constructor or subobject destructor. */
1146 vtt_index = BINFO_VPTR_INDEX (binfo);
1147 if (vtt_index)
1149 tree vtbl2;
1150 tree vtt_parm;
1152 /* Compute the value to use, when there's a VTT. */
1153 vtt_parm = current_vtt_parm;
1154 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1155 vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error);
1156 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1158 /* The actual initializer is the VTT value only in the subobject
1159 constructor. In maybe_clone_body we'll substitute NULL for
1160 the vtt_parm in the case of the non-subobject constructor. */
1161 vtbl = build3 (COND_EXPR,
1162 TREE_TYPE (vtbl),
1163 build2 (EQ_EXPR, boolean_type_node,
1164 current_in_charge_parm, integer_zero_node),
1165 vtbl2,
1166 vtbl);
1169 /* Compute the location of the vtpr. */
1170 vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL,
1171 tf_warning_or_error),
1172 TREE_TYPE (binfo));
1173 gcc_assert (vtbl_ptr != error_mark_node);
1175 /* Assign the vtable to the vptr. */
1176 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
1177 finish_expr_stmt (cp_build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl,
1178 tf_warning_or_error));
1181 /* If an exception is thrown in a constructor, those base classes already
1182 constructed must be destroyed. This function creates the cleanup
1183 for BINFO, which has just been constructed. If FLAG is non-NULL,
1184 it is a DECL which is nonzero when this base needs to be
1185 destroyed. */
1187 static void
1188 expand_cleanup_for_base (tree binfo, tree flag)
1190 tree expr;
1192 if (!type_build_dtor_call (BINFO_TYPE (binfo)))
1193 return;
1195 /* Call the destructor. */
1196 expr = build_special_member_call (current_class_ref,
1197 base_dtor_identifier,
1198 NULL,
1199 binfo,
1200 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1201 tf_warning_or_error);
1203 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1204 return;
1206 if (flag)
1207 expr = fold_build3_loc (input_location,
1208 COND_EXPR, void_type_node,
1209 c_common_truthvalue_conversion (input_location, flag),
1210 expr, integer_zero_node);
1212 finish_eh_cleanup (expr);
1215 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1216 constructor. */
1218 static void
1219 construct_virtual_base (tree vbase, tree arguments)
1221 tree inner_if_stmt;
1222 tree exp;
1223 tree flag;
1225 /* If there are virtual base classes with destructors, we need to
1226 emit cleanups to destroy them if an exception is thrown during
1227 the construction process. These exception regions (i.e., the
1228 period during which the cleanups must occur) begin from the time
1229 the construction is complete to the end of the function. If we
1230 create a conditional block in which to initialize the
1231 base-classes, then the cleanup region for the virtual base begins
1232 inside a block, and ends outside of that block. This situation
1233 confuses the sjlj exception-handling code. Therefore, we do not
1234 create a single conditional block, but one for each
1235 initialization. (That way the cleanup regions always begin
1236 in the outer block.) We trust the back end to figure out
1237 that the FLAG will not change across initializations, and
1238 avoid doing multiple tests. */
1239 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1240 inner_if_stmt = begin_if_stmt ();
1241 finish_if_stmt_cond (flag, inner_if_stmt);
1243 /* Compute the location of the virtual base. If we're
1244 constructing virtual bases, then we must be the most derived
1245 class. Therefore, we don't have to look up the virtual base;
1246 we already know where it is. */
1247 exp = convert_to_base_statically (current_class_ref, vbase);
1249 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1250 0, tf_warning_or_error);
1251 finish_then_clause (inner_if_stmt);
1252 finish_if_stmt (inner_if_stmt);
1254 expand_cleanup_for_base (vbase, flag);
1257 /* Find the context in which this FIELD can be initialized. */
1259 static tree
1260 initializing_context (tree field)
1262 tree t = DECL_CONTEXT (field);
1264 /* Anonymous union members can be initialized in the first enclosing
1265 non-anonymous union context. */
1266 while (t && ANON_AGGR_TYPE_P (t))
1267 t = TYPE_CONTEXT (t);
1268 return t;
1271 /* Function to give error message if member initialization specification
1272 is erroneous. FIELD is the member we decided to initialize.
1273 TYPE is the type for which the initialization is being performed.
1274 FIELD must be a member of TYPE.
1276 MEMBER_NAME is the name of the member. */
1278 static int
1279 member_init_ok_or_else (tree field, tree type, tree member_name)
1281 if (field == error_mark_node)
1282 return 0;
1283 if (!field)
1285 error ("class %qT does not have any field named %qD", type,
1286 member_name);
1287 return 0;
1289 if (VAR_P (field))
1291 error ("%q#D is a static data member; it can only be "
1292 "initialized at its definition",
1293 field);
1294 return 0;
1296 if (TREE_CODE (field) != FIELD_DECL)
1298 error ("%q#D is not a non-static data member of %qT",
1299 field, type);
1300 return 0;
1302 if (initializing_context (field) != type)
1304 error ("class %qT does not have any field named %qD", type,
1305 member_name);
1306 return 0;
1309 return 1;
1312 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1313 is a _TYPE node or TYPE_DECL which names a base for that type.
1314 Check the validity of NAME, and return either the base _TYPE, base
1315 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1316 NULL_TREE and issue a diagnostic.
1318 An old style unnamed direct single base construction is permitted,
1319 where NAME is NULL. */
1321 tree
1322 expand_member_init (tree name)
1324 tree basetype;
1325 tree field;
1327 if (!current_class_ref)
1328 return NULL_TREE;
1330 if (!name)
1332 /* This is an obsolete unnamed base class initializer. The
1333 parser will already have warned about its use. */
1334 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1336 case 0:
1337 error ("unnamed initializer for %qT, which has no base classes",
1338 current_class_type);
1339 return NULL_TREE;
1340 case 1:
1341 basetype = BINFO_TYPE
1342 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1343 break;
1344 default:
1345 error ("unnamed initializer for %qT, which uses multiple inheritance",
1346 current_class_type);
1347 return NULL_TREE;
1350 else if (TYPE_P (name))
1352 basetype = TYPE_MAIN_VARIANT (name);
1353 name = TYPE_NAME (name);
1355 else if (TREE_CODE (name) == TYPE_DECL)
1356 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1357 else
1358 basetype = NULL_TREE;
1360 if (basetype)
1362 tree class_binfo;
1363 tree direct_binfo;
1364 tree virtual_binfo;
1365 int i;
1367 if (current_template_parms
1368 || same_type_p (basetype, current_class_type))
1369 return basetype;
1371 class_binfo = TYPE_BINFO (current_class_type);
1372 direct_binfo = NULL_TREE;
1373 virtual_binfo = NULL_TREE;
1375 /* Look for a direct base. */
1376 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1377 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1378 break;
1380 /* Look for a virtual base -- unless the direct base is itself
1381 virtual. */
1382 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1383 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1385 /* [class.base.init]
1387 If a mem-initializer-id is ambiguous because it designates
1388 both a direct non-virtual base class and an inherited virtual
1389 base class, the mem-initializer is ill-formed. */
1390 if (direct_binfo && virtual_binfo)
1392 error ("%qD is both a direct base and an indirect virtual base",
1393 basetype);
1394 return NULL_TREE;
1397 if (!direct_binfo && !virtual_binfo)
1399 if (CLASSTYPE_VBASECLASSES (current_class_type))
1400 error ("type %qT is not a direct or virtual base of %qT",
1401 basetype, current_class_type);
1402 else
1403 error ("type %qT is not a direct base of %qT",
1404 basetype, current_class_type);
1405 return NULL_TREE;
1408 return direct_binfo ? direct_binfo : virtual_binfo;
1410 else
1412 if (identifier_p (name))
1413 field = lookup_field (current_class_type, name, 1, false);
1414 else
1415 field = name;
1417 if (member_init_ok_or_else (field, current_class_type, name))
1418 return field;
1421 return NULL_TREE;
1424 /* This is like `expand_member_init', only it stores one aggregate
1425 value into another.
1427 INIT comes in two flavors: it is either a value which
1428 is to be stored in EXP, or it is a parameter list
1429 to go to a constructor, which will operate on EXP.
1430 If INIT is not a parameter list for a constructor, then set
1431 LOOKUP_ONLYCONVERTING.
1432 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1433 the initializer, if FLAGS is 0, then it is the (init) form.
1434 If `init' is a CONSTRUCTOR, then we emit a warning message,
1435 explaining that such initializations are invalid.
1437 If INIT resolves to a CALL_EXPR which happens to return
1438 something of the type we are looking for, then we know
1439 that we can safely use that call to perform the
1440 initialization.
1442 The virtual function table pointer cannot be set up here, because
1443 we do not really know its type.
1445 This never calls operator=().
1447 When initializing, nothing is CONST.
1449 A default copy constructor may have to be used to perform the
1450 initialization.
1452 A constructor or a conversion operator may have to be used to
1453 perform the initialization, but not both, as it would be ambiguous. */
1455 tree
1456 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1458 tree stmt_expr;
1459 tree compound_stmt;
1460 int destroy_temps;
1461 tree type = TREE_TYPE (exp);
1462 int was_const = TREE_READONLY (exp);
1463 int was_volatile = TREE_THIS_VOLATILE (exp);
1464 int is_global;
1466 if (init == error_mark_node)
1467 return error_mark_node;
1469 TREE_READONLY (exp) = 0;
1470 TREE_THIS_VOLATILE (exp) = 0;
1472 if (init && init != void_type_node
1473 && TREE_CODE (init) != TREE_LIST
1474 && !(TREE_CODE (init) == TARGET_EXPR
1475 && TARGET_EXPR_DIRECT_INIT_P (init))
1476 && !(BRACE_ENCLOSED_INITIALIZER_P (init)
1477 && CONSTRUCTOR_IS_DIRECT_INIT (init)))
1478 flags |= LOOKUP_ONLYCONVERTING;
1480 if (TREE_CODE (type) == ARRAY_TYPE)
1482 tree itype;
1484 /* An array may not be initialized use the parenthesized
1485 initialization form -- unless the initializer is "()". */
1486 if (init && TREE_CODE (init) == TREE_LIST)
1488 if (complain & tf_error)
1489 error ("bad array initializer");
1490 return error_mark_node;
1492 /* Must arrange to initialize each element of EXP
1493 from elements of INIT. */
1494 itype = init ? TREE_TYPE (init) : NULL_TREE;
1495 if (cv_qualified_p (type))
1496 TREE_TYPE (exp) = cv_unqualified (type);
1497 if (itype && cv_qualified_p (itype))
1498 TREE_TYPE (init) = cv_unqualified (itype);
1499 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1500 /*explicit_value_init_p=*/false,
1501 itype && same_type_p (TREE_TYPE (init),
1502 TREE_TYPE (exp)),
1503 complain);
1504 TREE_READONLY (exp) = was_const;
1505 TREE_THIS_VOLATILE (exp) = was_volatile;
1506 TREE_TYPE (exp) = type;
1507 if (init)
1508 TREE_TYPE (init) = itype;
1509 return stmt_expr;
1512 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
1513 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
1514 /* Just know that we've seen something for this node. */
1515 TREE_USED (exp) = 1;
1517 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1518 destroy_temps = stmts_are_full_exprs_p ();
1519 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1520 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1521 init, LOOKUP_NORMAL|flags, complain);
1522 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1523 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1524 TREE_READONLY (exp) = was_const;
1525 TREE_THIS_VOLATILE (exp) = was_volatile;
1527 return stmt_expr;
1530 static void
1531 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
1532 tsubst_flags_t complain)
1534 tree type = TREE_TYPE (exp);
1535 tree ctor_name;
1537 /* It fails because there may not be a constructor which takes
1538 its own type as the first (or only parameter), but which does
1539 take other types via a conversion. So, if the thing initializing
1540 the expression is a unit element of type X, first try X(X&),
1541 followed by initialization by X. If neither of these work
1542 out, then look hard. */
1543 tree rval;
1544 vec<tree, va_gc> *parms;
1546 /* If we have direct-initialization from an initializer list, pull
1547 it out of the TREE_LIST so the code below can see it. */
1548 if (init && TREE_CODE (init) == TREE_LIST
1549 && BRACE_ENCLOSED_INITIALIZER_P (TREE_VALUE (init))
1550 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
1552 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
1553 && TREE_CHAIN (init) == NULL_TREE);
1554 init = TREE_VALUE (init);
1557 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
1558 && CP_AGGREGATE_TYPE_P (type))
1559 /* A brace-enclosed initializer for an aggregate. In C++0x this can
1560 happen for direct-initialization, too. */
1561 init = digest_init (type, init, complain);
1563 /* A CONSTRUCTOR of the target's type is a previously digested
1564 initializer, whether that happened just above or in
1565 cp_parser_late_parsing_nsdmi.
1567 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
1568 set represents the whole initialization, so we shouldn't build up
1569 another ctor call. */
1570 if (init
1571 && (TREE_CODE (init) == CONSTRUCTOR
1572 || (TREE_CODE (init) == TARGET_EXPR
1573 && (TARGET_EXPR_DIRECT_INIT_P (init)
1574 || TARGET_EXPR_LIST_INIT_P (init))))
1575 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1577 /* Early initialization via a TARGET_EXPR only works for
1578 complete objects. */
1579 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
1581 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1582 TREE_SIDE_EFFECTS (init) = 1;
1583 finish_expr_stmt (init);
1584 return;
1587 if (init && TREE_CODE (init) != TREE_LIST
1588 && (flags & LOOKUP_ONLYCONVERTING))
1590 /* Base subobjects should only get direct-initialization. */
1591 gcc_assert (true_exp == exp);
1593 if (flags & DIRECT_BIND)
1594 /* Do nothing. We hit this in two cases: Reference initialization,
1595 where we aren't initializing a real variable, so we don't want
1596 to run a new constructor; and catching an exception, where we
1597 have already built up the constructor call so we could wrap it
1598 in an exception region. */;
1599 else
1600 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
1601 flags, complain);
1603 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1604 /* We need to protect the initialization of a catch parm with a
1605 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1606 around the TARGET_EXPR for the copy constructor. See
1607 initialize_handler_parm. */
1609 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1610 TREE_OPERAND (init, 0));
1611 TREE_TYPE (init) = void_type_node;
1613 else
1614 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1615 TREE_SIDE_EFFECTS (init) = 1;
1616 finish_expr_stmt (init);
1617 return;
1620 if (init == NULL_TREE)
1621 parms = NULL;
1622 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
1624 parms = make_tree_vector ();
1625 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1626 vec_safe_push (parms, TREE_VALUE (init));
1628 else
1629 parms = make_tree_vector_single (init);
1631 if (exp == current_class_ref && current_function_decl
1632 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
1634 /* Delegating constructor. */
1635 tree complete;
1636 tree base;
1637 tree elt; unsigned i;
1639 /* Unshare the arguments for the second call. */
1640 vec<tree, va_gc> *parms2 = make_tree_vector ();
1641 FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
1643 elt = break_out_target_exprs (elt);
1644 vec_safe_push (parms2, elt);
1646 complete = build_special_member_call (exp, complete_ctor_identifier,
1647 &parms2, binfo, flags,
1648 complain);
1649 complete = fold_build_cleanup_point_expr (void_type_node, complete);
1650 release_tree_vector (parms2);
1652 base = build_special_member_call (exp, base_ctor_identifier,
1653 &parms, binfo, flags,
1654 complain);
1655 base = fold_build_cleanup_point_expr (void_type_node, base);
1656 rval = build3 (COND_EXPR, void_type_node,
1657 build2 (EQ_EXPR, boolean_type_node,
1658 current_in_charge_parm, integer_zero_node),
1659 base,
1660 complete);
1662 else
1664 if (true_exp == exp)
1665 ctor_name = complete_ctor_identifier;
1666 else
1667 ctor_name = base_ctor_identifier;
1668 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
1669 complain);
1672 if (parms != NULL)
1673 release_tree_vector (parms);
1675 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
1677 tree fn = get_callee_fndecl (rval);
1678 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
1680 tree e = maybe_constant_init (rval);
1681 if (TREE_CONSTANT (e))
1682 rval = build2 (INIT_EXPR, type, exp, e);
1686 /* FIXME put back convert_to_void? */
1687 if (TREE_SIDE_EFFECTS (rval))
1688 finish_expr_stmt (rval);
1691 /* This function is responsible for initializing EXP with INIT
1692 (if any).
1694 BINFO is the binfo of the type for who we are performing the
1695 initialization. For example, if W is a virtual base class of A and B,
1696 and C : A, B.
1697 If we are initializing B, then W must contain B's W vtable, whereas
1698 were we initializing C, W must contain C's W vtable.
1700 TRUE_EXP is nonzero if it is the true expression being initialized.
1701 In this case, it may be EXP, or may just contain EXP. The reason we
1702 need this is because if EXP is a base element of TRUE_EXP, we
1703 don't necessarily know by looking at EXP where its virtual
1704 baseclass fields should really be pointing. But we do know
1705 from TRUE_EXP. In constructors, we don't know anything about
1706 the value being initialized.
1708 FLAGS is just passed to `build_new_method_call'. See that function
1709 for its description. */
1711 static void
1712 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
1713 tsubst_flags_t complain)
1715 tree type = TREE_TYPE (exp);
1717 gcc_assert (init != error_mark_node && type != error_mark_node);
1718 gcc_assert (building_stmt_list_p ());
1720 /* Use a function returning the desired type to initialize EXP for us.
1721 If the function is a constructor, and its first argument is
1722 NULL_TREE, know that it was meant for us--just slide exp on
1723 in and expand the constructor. Constructors now come
1724 as TARGET_EXPRs. */
1726 if (init && VAR_P (exp)
1727 && COMPOUND_LITERAL_P (init))
1729 vec<tree, va_gc> *cleanups = NULL;
1730 /* If store_init_value returns NULL_TREE, the INIT has been
1731 recorded as the DECL_INITIAL for EXP. That means there's
1732 nothing more we have to do. */
1733 init = store_init_value (exp, init, &cleanups, flags);
1734 if (init)
1735 finish_expr_stmt (init);
1736 gcc_assert (!cleanups);
1737 return;
1740 /* If an explicit -- but empty -- initializer list was present,
1741 that's value-initialization. */
1742 if (init == void_type_node)
1744 /* If the type has data but no user-provided ctor, we need to zero
1745 out the object. */
1746 if (!type_has_user_provided_constructor (type)
1747 && !is_really_empty_class (type))
1749 tree field_size = NULL_TREE;
1750 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
1751 /* Don't clobber already initialized virtual bases. */
1752 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
1753 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
1754 field_size);
1755 init = build2 (INIT_EXPR, type, exp, init);
1756 finish_expr_stmt (init);
1759 /* If we don't need to mess with the constructor at all,
1760 then we're done. */
1761 if (! type_build_ctor_call (type))
1762 return;
1764 /* Otherwise fall through and call the constructor. */
1765 init = NULL_TREE;
1768 /* We know that expand_default_init can handle everything we want
1769 at this point. */
1770 expand_default_init (binfo, true_exp, exp, init, flags, complain);
1773 /* Report an error if TYPE is not a user-defined, class type. If
1774 OR_ELSE is nonzero, give an error message. */
1777 is_class_type (tree type, int or_else)
1779 if (type == error_mark_node)
1780 return 0;
1782 if (! CLASS_TYPE_P (type))
1784 if (or_else)
1785 error ("%qT is not a class type", type);
1786 return 0;
1788 return 1;
1791 tree
1792 get_type_value (tree name)
1794 if (name == error_mark_node)
1795 return NULL_TREE;
1797 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1798 return IDENTIFIER_TYPE_VALUE (name);
1799 else
1800 return NULL_TREE;
1803 /* Build a reference to a member of an aggregate. This is not a C++
1804 `&', but really something which can have its address taken, and
1805 then act as a pointer to member, for example TYPE :: FIELD can have
1806 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
1807 this expression is the operand of "&".
1809 @@ Prints out lousy diagnostics for operator <typename>
1810 @@ fields.
1812 @@ This function should be rewritten and placed in search.c. */
1814 tree
1815 build_offset_ref (tree type, tree member, bool address_p,
1816 tsubst_flags_t complain)
1818 tree decl;
1819 tree basebinfo = NULL_TREE;
1821 /* class templates can come in as TEMPLATE_DECLs here. */
1822 if (TREE_CODE (member) == TEMPLATE_DECL)
1823 return member;
1825 if (dependent_scope_p (type) || type_dependent_expression_p (member))
1826 return build_qualified_name (NULL_TREE, type, member,
1827 /*template_p=*/false);
1829 gcc_assert (TYPE_P (type));
1830 if (! is_class_type (type, 1))
1831 return error_mark_node;
1833 gcc_assert (DECL_P (member) || BASELINK_P (member));
1834 /* Callers should call mark_used before this point. */
1835 gcc_assert (!DECL_P (member) || TREE_USED (member));
1837 type = TYPE_MAIN_VARIANT (type);
1838 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
1840 if (complain & tf_error)
1841 error ("incomplete type %qT does not have member %qD", type, member);
1842 return error_mark_node;
1845 /* Entities other than non-static members need no further
1846 processing. */
1847 if (TREE_CODE (member) == TYPE_DECL)
1848 return member;
1849 if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
1850 return convert_from_reference (member);
1852 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1854 if (complain & tf_error)
1855 error ("invalid pointer to bit-field %qD", member);
1856 return error_mark_node;
1859 /* Set up BASEBINFO for member lookup. */
1860 decl = maybe_dummy_object (type, &basebinfo);
1862 /* A lot of this logic is now handled in lookup_member. */
1863 if (BASELINK_P (member))
1865 /* Go from the TREE_BASELINK to the member function info. */
1866 tree t = BASELINK_FUNCTIONS (member);
1868 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1870 /* Get rid of a potential OVERLOAD around it. */
1871 t = OVL_CURRENT (t);
1873 /* Unique functions are handled easily. */
1875 /* For non-static member of base class, we need a special rule
1876 for access checking [class.protected]:
1878 If the access is to form a pointer to member, the
1879 nested-name-specifier shall name the derived class
1880 (or any class derived from that class). */
1881 if (address_p && DECL_P (t)
1882 && DECL_NONSTATIC_MEMBER_P (t))
1883 perform_or_defer_access_check (TYPE_BINFO (type), t, t,
1884 complain);
1885 else
1886 perform_or_defer_access_check (basebinfo, t, t,
1887 complain);
1889 if (DECL_STATIC_FUNCTION_P (t))
1890 return t;
1891 member = t;
1893 else
1894 TREE_TYPE (member) = unknown_type_node;
1896 else if (address_p && TREE_CODE (member) == FIELD_DECL)
1897 /* We need additional test besides the one in
1898 check_accessibility_of_qualified_id in case it is
1899 a pointer to non-static member. */
1900 perform_or_defer_access_check (TYPE_BINFO (type), member, member,
1901 complain);
1903 if (!address_p)
1905 /* If MEMBER is non-static, then the program has fallen afoul of
1906 [expr.prim]:
1908 An id-expression that denotes a nonstatic data member or
1909 nonstatic member function of a class can only be used:
1911 -- as part of a class member access (_expr.ref_) in which the
1912 object-expression refers to the member's class or a class
1913 derived from that class, or
1915 -- to form a pointer to member (_expr.unary.op_), or
1917 -- in the body of a nonstatic member function of that class or
1918 of a class derived from that class (_class.mfct.nonstatic_), or
1920 -- in a mem-initializer for a constructor for that class or for
1921 a class derived from that class (_class.base.init_). */
1922 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1924 /* Build a representation of the qualified name suitable
1925 for use as the operand to "&" -- even though the "&" is
1926 not actually present. */
1927 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1928 /* In Microsoft mode, treat a non-static member function as if
1929 it were a pointer-to-member. */
1930 if (flag_ms_extensions)
1932 PTRMEM_OK_P (member) = 1;
1933 return cp_build_addr_expr (member, complain);
1935 if (complain & tf_error)
1936 error ("invalid use of non-static member function %qD",
1937 TREE_OPERAND (member, 1));
1938 return error_mark_node;
1940 else if (TREE_CODE (member) == FIELD_DECL)
1942 if (complain & tf_error)
1943 error ("invalid use of non-static data member %qD", member);
1944 return error_mark_node;
1946 return member;
1949 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1950 PTRMEM_OK_P (member) = 1;
1951 return member;
1954 /* If DECL is a scalar enumeration constant or variable with a
1955 constant initializer, return the initializer (or, its initializers,
1956 recursively); otherwise, return DECL. If INTEGRAL_P, the
1957 initializer is only returned if DECL is an integral
1958 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
1959 return an aggregate constant. */
1961 static tree
1962 constant_value_1 (tree decl, bool integral_p, bool return_aggregate_cst_ok_p)
1964 while (TREE_CODE (decl) == CONST_DECL
1965 || (integral_p
1966 ? decl_constant_var_p (decl)
1967 : (VAR_P (decl)
1968 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
1970 tree init;
1971 /* If DECL is a static data member in a template
1972 specialization, we must instantiate it here. The
1973 initializer for the static data member is not processed
1974 until needed; we need it now. */
1975 mark_used (decl);
1976 mark_rvalue_use (decl);
1977 init = DECL_INITIAL (decl);
1978 if (init == error_mark_node)
1980 if (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
1981 /* Treat the error as a constant to avoid cascading errors on
1982 excessively recursive template instantiation (c++/9335). */
1983 return init;
1984 else
1985 return decl;
1987 /* Initializers in templates are generally expanded during
1988 instantiation, so before that for const int i(2)
1989 INIT is a TREE_LIST with the actual initializer as
1990 TREE_VALUE. */
1991 if (processing_template_decl
1992 && init
1993 && TREE_CODE (init) == TREE_LIST
1994 && TREE_CHAIN (init) == NULL_TREE)
1995 init = TREE_VALUE (init);
1996 if (!init
1997 || !TREE_TYPE (init)
1998 || !TREE_CONSTANT (init)
1999 || (!integral_p && !return_aggregate_cst_ok_p
2000 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2001 return an aggregate constant (of which string
2002 literals are a special case), as we do not want
2003 to make inadvertent copies of such entities, and
2004 we must be sure that their addresses are the
2005 same everywhere. */
2006 && (TREE_CODE (init) == CONSTRUCTOR
2007 || TREE_CODE (init) == STRING_CST)))
2008 break;
2009 decl = unshare_expr (init);
2011 return decl;
2014 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by
2015 constant of integral or enumeration type, then return that value.
2016 These are those variables permitted in constant expressions by
2017 [5.19/1]. */
2019 tree
2020 integral_constant_value (tree decl)
2022 return constant_value_1 (decl, /*integral_p=*/true,
2023 /*return_aggregate_cst_ok_p=*/false);
2026 /* A more relaxed version of integral_constant_value, used by the
2027 common C/C++ code. */
2029 tree
2030 decl_constant_value (tree decl)
2032 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2033 /*return_aggregate_cst_ok_p=*/true);
2036 /* A version of integral_constant_value used by the C++ front end for
2037 optimization purposes. */
2039 tree
2040 decl_constant_value_safe (tree decl)
2042 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2043 /*return_aggregate_cst_ok_p=*/false);
2046 /* Common subroutines of build_new and build_vec_delete. */
2048 /* Call the global __builtin_delete to delete ADDR. */
2050 static tree
2051 build_builtin_delete_call (tree addr)
2053 mark_used (global_delete_fndecl);
2054 return build_call_n (global_delete_fndecl, 1, addr);
2057 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2058 the type of the object being allocated; otherwise, it's just TYPE.
2059 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2060 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2061 a vector of arguments to be provided as arguments to a placement
2062 new operator. This routine performs no semantic checks; it just
2063 creates and returns a NEW_EXPR. */
2065 static tree
2066 build_raw_new_expr (vec<tree, va_gc> *placement, tree type, tree nelts,
2067 vec<tree, va_gc> *init, int use_global_new)
2069 tree init_list;
2070 tree new_expr;
2072 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2073 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2074 permits us to distinguish the case of a missing initializer "new
2075 int" from an empty initializer "new int()". */
2076 if (init == NULL)
2077 init_list = NULL_TREE;
2078 else if (init->is_empty ())
2079 init_list = void_zero_node;
2080 else
2081 init_list = build_tree_list_vec (init);
2083 new_expr = build4 (NEW_EXPR, build_pointer_type (type),
2084 build_tree_list_vec (placement), type, nelts,
2085 init_list);
2086 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2087 TREE_SIDE_EFFECTS (new_expr) = 1;
2089 return new_expr;
2092 /* Diagnose uninitialized const members or reference members of type
2093 TYPE. USING_NEW is used to disambiguate the diagnostic between a
2094 new expression without a new-initializer and a declaration. Returns
2095 the error count. */
2097 static int
2098 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2099 bool using_new, bool complain)
2101 tree field;
2102 int error_count = 0;
2104 if (type_has_user_provided_constructor (type))
2105 return 0;
2107 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2109 tree field_type;
2111 if (TREE_CODE (field) != FIELD_DECL)
2112 continue;
2114 field_type = strip_array_types (TREE_TYPE (field));
2116 if (type_has_user_provided_constructor (field_type))
2117 continue;
2119 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2121 ++ error_count;
2122 if (complain)
2124 if (DECL_CONTEXT (field) == origin)
2126 if (using_new)
2127 error ("uninitialized reference member in %q#T "
2128 "using %<new%> without new-initializer", origin);
2129 else
2130 error ("uninitialized reference member in %q#T", origin);
2132 else
2134 if (using_new)
2135 error ("uninitialized reference member in base %q#T "
2136 "of %q#T using %<new%> without new-initializer",
2137 DECL_CONTEXT (field), origin);
2138 else
2139 error ("uninitialized reference member in base %q#T "
2140 "of %q#T", DECL_CONTEXT (field), origin);
2142 inform (DECL_SOURCE_LOCATION (field),
2143 "%qD should be initialized", field);
2147 if (CP_TYPE_CONST_P (field_type))
2149 ++ error_count;
2150 if (complain)
2152 if (DECL_CONTEXT (field) == origin)
2154 if (using_new)
2155 error ("uninitialized const member in %q#T "
2156 "using %<new%> without new-initializer", origin);
2157 else
2158 error ("uninitialized const member in %q#T", origin);
2160 else
2162 if (using_new)
2163 error ("uninitialized const member in base %q#T "
2164 "of %q#T using %<new%> without new-initializer",
2165 DECL_CONTEXT (field), origin);
2166 else
2167 error ("uninitialized const member in base %q#T "
2168 "of %q#T", DECL_CONTEXT (field), origin);
2170 inform (DECL_SOURCE_LOCATION (field),
2171 "%qD should be initialized", field);
2175 if (CLASS_TYPE_P (field_type))
2176 error_count
2177 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2178 using_new, complain);
2180 return error_count;
2184 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2186 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2189 /* Call __cxa_bad_array_new_length to indicate that the size calculation
2190 overflowed. Pretend it returns sizetype so that it plays nicely in the
2191 COND_EXPR. */
2193 tree
2194 throw_bad_array_new_length (void)
2196 tree fn = get_identifier ("__cxa_throw_bad_array_new_length");
2197 if (!get_global_value_if_present (fn, &fn))
2198 fn = push_throw_library_fn (fn, build_function_type_list (sizetype,
2199 NULL_TREE));
2201 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2204 /* Call __cxa_bad_array_length to indicate that there were too many
2205 initializers. */
2207 tree
2208 throw_bad_array_length (void)
2210 tree fn = get_identifier ("__cxa_throw_bad_array_length");
2211 if (!get_global_value_if_present (fn, &fn))
2212 fn = push_throw_library_fn (fn, build_function_type_list (void_type_node,
2213 NULL_TREE));
2215 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2218 /* Generate code for a new-expression, including calling the "operator
2219 new" function, initializing the object, and, if an exception occurs
2220 during construction, cleaning up. The arguments are as for
2221 build_raw_new_expr. This may change PLACEMENT and INIT. */
2223 static tree
2224 build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
2225 vec<tree, va_gc> **init, bool globally_qualified_p,
2226 tsubst_flags_t complain)
2228 tree size, rval;
2229 /* True iff this is a call to "operator new[]" instead of just
2230 "operator new". */
2231 bool array_p = false;
2232 /* If ARRAY_P is true, the element type of the array. This is never
2233 an ARRAY_TYPE; for something like "new int[3][4]", the
2234 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
2235 TYPE. */
2236 tree elt_type;
2237 /* The type of the new-expression. (This type is always a pointer
2238 type.) */
2239 tree pointer_type;
2240 tree non_const_pointer_type;
2241 tree outer_nelts = NULL_TREE;
2242 /* For arrays, a bounds checks on the NELTS parameter. */
2243 tree outer_nelts_check = NULL_TREE;
2244 bool outer_nelts_from_type = false;
2245 double_int inner_nelts_count = double_int_one;
2246 tree alloc_call, alloc_expr;
2247 /* Size of the inner array elements. */
2248 double_int inner_size;
2249 /* The address returned by the call to "operator new". This node is
2250 a VAR_DECL and is therefore reusable. */
2251 tree alloc_node;
2252 tree alloc_fn;
2253 tree cookie_expr, init_expr;
2254 int nothrow, check_new;
2255 int use_java_new = 0;
2256 /* If non-NULL, the number of extra bytes to allocate at the
2257 beginning of the storage allocated for an array-new expression in
2258 order to store the number of elements. */
2259 tree cookie_size = NULL_TREE;
2260 tree placement_first;
2261 tree placement_expr = NULL_TREE;
2262 /* True if the function we are calling is a placement allocation
2263 function. */
2264 bool placement_allocation_fn_p;
2265 /* True if the storage must be initialized, either by a constructor
2266 or due to an explicit new-initializer. */
2267 bool is_initialized;
2268 /* The address of the thing allocated, not including any cookie. In
2269 particular, if an array cookie is in use, DATA_ADDR is the
2270 address of the first array element. This node is a VAR_DECL, and
2271 is therefore reusable. */
2272 tree data_addr;
2273 tree init_preeval_expr = NULL_TREE;
2275 if (nelts)
2277 outer_nelts = nelts;
2278 array_p = true;
2280 else if (TREE_CODE (type) == ARRAY_TYPE)
2282 /* Transforms new (T[N]) to new T[N]. The former is a GNU
2283 extension for variable N. (This also covers new T where T is
2284 a VLA typedef.) */
2285 array_p = true;
2286 nelts = array_type_nelts_top (type);
2287 outer_nelts = nelts;
2288 type = TREE_TYPE (type);
2289 outer_nelts_from_type = true;
2292 /* If our base type is an array, then make sure we know how many elements
2293 it has. */
2294 for (elt_type = type;
2295 TREE_CODE (elt_type) == ARRAY_TYPE;
2296 elt_type = TREE_TYPE (elt_type))
2298 tree inner_nelts = array_type_nelts_top (elt_type);
2299 tree inner_nelts_cst = maybe_constant_value (inner_nelts);
2300 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
2302 bool overflow;
2303 double_int result = TREE_INT_CST (inner_nelts_cst)
2304 .mul_with_sign (inner_nelts_count,
2305 false, &overflow);
2306 if (overflow)
2308 if (complain & tf_error)
2309 error ("integer overflow in array size");
2310 nelts = error_mark_node;
2312 inner_nelts_count = result;
2314 else
2316 if (complain & tf_error)
2318 error_at (EXPR_LOC_OR_HERE (inner_nelts),
2319 "array size in operator new must be constant");
2320 cxx_constant_value(inner_nelts);
2322 nelts = error_mark_node;
2324 if (nelts != error_mark_node)
2325 nelts = cp_build_binary_op (input_location,
2326 MULT_EXPR, nelts,
2327 inner_nelts_cst,
2328 complain);
2331 if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
2333 error ("variably modified type not allowed in operator new");
2334 return error_mark_node;
2337 if (nelts == error_mark_node)
2338 return error_mark_node;
2340 /* Warn if we performed the (T[N]) to T[N] transformation and N is
2341 variable. */
2342 if (outer_nelts_from_type
2343 && !TREE_CONSTANT (maybe_constant_value (outer_nelts)))
2345 if (complain & tf_warning_or_error)
2346 pedwarn(EXPR_LOC_OR_HERE (outer_nelts), OPT_Wvla,
2347 "ISO C++ does not support variable-length array types");
2348 else
2349 return error_mark_node;
2352 if (VOID_TYPE_P (elt_type))
2354 if (complain & tf_error)
2355 error ("invalid type %<void%> for new");
2356 return error_mark_node;
2359 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain))
2360 return error_mark_node;
2362 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
2364 if (*init == NULL && cxx_dialect < cxx11)
2366 bool maybe_uninitialized_error = false;
2367 /* A program that calls for default-initialization [...] of an
2368 entity of reference type is ill-formed. */
2369 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
2370 maybe_uninitialized_error = true;
2372 /* A new-expression that creates an object of type T initializes
2373 that object as follows:
2374 - If the new-initializer is omitted:
2375 -- If T is a (possibly cv-qualified) non-POD class type
2376 (or array thereof), the object is default-initialized (8.5).
2377 [...]
2378 -- Otherwise, the object created has indeterminate
2379 value. If T is a const-qualified type, or a (possibly
2380 cv-qualified) POD class type (or array thereof)
2381 containing (directly or indirectly) a member of
2382 const-qualified type, the program is ill-formed; */
2384 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
2385 maybe_uninitialized_error = true;
2387 if (maybe_uninitialized_error
2388 && diagnose_uninitialized_cst_or_ref_member (elt_type,
2389 /*using_new=*/true,
2390 complain & tf_error))
2391 return error_mark_node;
2394 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
2395 && default_init_uninitialized_part (elt_type))
2397 if (complain & tf_error)
2398 error ("uninitialized const in %<new%> of %q#T", elt_type);
2399 return error_mark_node;
2402 size = size_in_bytes (elt_type);
2403 if (array_p)
2405 /* Maximum available size in bytes. Half of the address space
2406 minus the cookie size. */
2407 double_int max_size
2408 = double_int_one.llshift (TYPE_PRECISION (sizetype) - 1,
2409 HOST_BITS_PER_DOUBLE_INT);
2410 /* Maximum number of outer elements which can be allocated. */
2411 double_int max_outer_nelts;
2412 tree max_outer_nelts_tree;
2414 gcc_assert (TREE_CODE (size) == INTEGER_CST);
2415 cookie_size = targetm.cxx.get_cookie_size (elt_type);
2416 gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
2417 gcc_checking_assert (TREE_INT_CST (cookie_size).ult (max_size));
2418 /* Unconditionally subtract the cookie size. This decreases the
2419 maximum object size and is safe even if we choose not to use
2420 a cookie after all. */
2421 max_size -= TREE_INT_CST (cookie_size);
2422 bool overflow;
2423 inner_size = TREE_INT_CST (size)
2424 .mul_with_sign (inner_nelts_count, false, &overflow);
2425 if (overflow || inner_size.ugt (max_size))
2427 if (complain & tf_error)
2428 error ("size of array is too large");
2429 return error_mark_node;
2431 max_outer_nelts = max_size.udiv (inner_size, TRUNC_DIV_EXPR);
2432 /* Only keep the top-most seven bits, to simplify encoding the
2433 constant in the instruction stream. */
2435 unsigned shift = HOST_BITS_PER_DOUBLE_INT - 7
2436 - (max_outer_nelts.high ? clz_hwi (max_outer_nelts.high)
2437 : (HOST_BITS_PER_WIDE_INT + clz_hwi (max_outer_nelts.low)));
2438 max_outer_nelts
2439 = max_outer_nelts.lrshift (shift, HOST_BITS_PER_DOUBLE_INT)
2440 .llshift (shift, HOST_BITS_PER_DOUBLE_INT);
2442 max_outer_nelts_tree = double_int_to_tree (sizetype, max_outer_nelts);
2444 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2445 outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
2446 outer_nelts,
2447 max_outer_nelts_tree);
2450 alloc_fn = NULL_TREE;
2452 /* If PLACEMENT is a single simple pointer type not passed by
2453 reference, prepare to capture it in a temporary variable. Do
2454 this now, since PLACEMENT will change in the calls below. */
2455 placement_first = NULL_TREE;
2456 if (vec_safe_length (*placement) == 1
2457 && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
2458 placement_first = (**placement)[0];
2460 /* Allocate the object. */
2461 if (vec_safe_is_empty (*placement) && TYPE_FOR_JAVA (elt_type))
2463 tree class_addr;
2464 tree class_decl;
2465 static const char alloc_name[] = "_Jv_AllocObject";
2467 if (!MAYBE_CLASS_TYPE_P (elt_type))
2469 error ("%qT isn%'t a valid Java class type", elt_type);
2470 return error_mark_node;
2473 class_decl = build_java_class_ref (elt_type);
2474 if (class_decl == error_mark_node)
2475 return error_mark_node;
2477 use_java_new = 1;
2478 if (!get_global_value_if_present (get_identifier (alloc_name),
2479 &alloc_fn))
2481 if (complain & tf_error)
2482 error ("call to Java constructor with %qs undefined", alloc_name);
2483 return error_mark_node;
2485 else if (really_overloaded_fn (alloc_fn))
2487 if (complain & tf_error)
2488 error ("%qD should never be overloaded", alloc_fn);
2489 return error_mark_node;
2491 alloc_fn = OVL_CURRENT (alloc_fn);
2492 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2493 alloc_call = cp_build_function_call_nary (alloc_fn, complain,
2494 class_addr, NULL_TREE);
2496 else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
2498 error ("Java class %q#T object allocated using placement new", elt_type);
2499 return error_mark_node;
2501 else
2503 tree fnname;
2504 tree fns;
2506 fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
2508 if (!globally_qualified_p
2509 && CLASS_TYPE_P (elt_type)
2510 && (array_p
2511 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
2512 : TYPE_HAS_NEW_OPERATOR (elt_type)))
2514 /* Use a class-specific operator new. */
2515 /* If a cookie is required, add some extra space. */
2516 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
2517 size = size_binop (PLUS_EXPR, size, cookie_size);
2518 else
2520 cookie_size = NULL_TREE;
2521 /* No size arithmetic necessary, so the size check is
2522 not needed. */
2523 if (outer_nelts_check != NULL && inner_size.is_one ())
2524 outer_nelts_check = NULL_TREE;
2526 /* Perform the overflow check. */
2527 tree errval = TYPE_MAX_VALUE (sizetype);
2528 if (cxx_dialect >= cxx11)
2529 errval = throw_bad_array_new_length ();
2530 if (outer_nelts_check != NULL_TREE)
2531 size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
2532 size, errval);
2533 /* Create the argument list. */
2534 vec_safe_insert (*placement, 0, size);
2535 /* Do name-lookup to find the appropriate operator. */
2536 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
2537 if (fns == NULL_TREE)
2539 if (complain & tf_error)
2540 error ("no suitable %qD found in class %qT", fnname, elt_type);
2541 return error_mark_node;
2543 if (TREE_CODE (fns) == TREE_LIST)
2545 if (complain & tf_error)
2547 error ("request for member %qD is ambiguous", fnname);
2548 print_candidates (fns);
2550 return error_mark_node;
2552 alloc_call = build_new_method_call (build_dummy_object (elt_type),
2553 fns, placement,
2554 /*conversion_path=*/NULL_TREE,
2555 LOOKUP_NORMAL,
2556 &alloc_fn,
2557 complain);
2559 else
2561 /* Use a global operator new. */
2562 /* See if a cookie might be required. */
2563 if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
2565 cookie_size = NULL_TREE;
2566 /* No size arithmetic necessary, so the size check is
2567 not needed. */
2568 if (outer_nelts_check != NULL && inner_size.is_one ())
2569 outer_nelts_check = NULL_TREE;
2572 alloc_call = build_operator_new_call (fnname, placement,
2573 &size, &cookie_size,
2574 outer_nelts_check,
2575 &alloc_fn, complain);
2579 if (alloc_call == error_mark_node)
2580 return error_mark_node;
2582 gcc_assert (alloc_fn != NULL_TREE);
2584 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
2585 into a temporary variable. */
2586 if (!processing_template_decl
2587 && placement_first != NULL_TREE
2588 && TREE_CODE (alloc_call) == CALL_EXPR
2589 && call_expr_nargs (alloc_call) == 2
2590 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
2591 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
2593 tree placement_arg = CALL_EXPR_ARG (alloc_call, 1);
2595 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))
2596 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))))
2598 placement_expr = get_target_expr (placement_first);
2599 CALL_EXPR_ARG (alloc_call, 1)
2600 = convert (TREE_TYPE (placement_arg), placement_expr);
2604 /* In the simple case, we can stop now. */
2605 pointer_type = build_pointer_type (type);
2606 if (!cookie_size && !is_initialized)
2607 return build_nop (pointer_type, alloc_call);
2609 /* Store the result of the allocation call in a variable so that we can
2610 use it more than once. */
2611 alloc_expr = get_target_expr (alloc_call);
2612 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2614 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
2615 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
2616 alloc_call = TREE_OPERAND (alloc_call, 1);
2618 /* Now, check to see if this function is actually a placement
2619 allocation function. This can happen even when PLACEMENT is NULL
2620 because we might have something like:
2622 struct S { void* operator new (size_t, int i = 0); };
2624 A call to `new S' will get this allocation function, even though
2625 there is no explicit placement argument. If there is more than
2626 one argument, or there are variable arguments, then this is a
2627 placement allocation function. */
2628 placement_allocation_fn_p
2629 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2630 || varargs_function_p (alloc_fn));
2632 /* Preevaluate the placement args so that we don't reevaluate them for a
2633 placement delete. */
2634 if (placement_allocation_fn_p)
2636 tree inits;
2637 stabilize_call (alloc_call, &inits);
2638 if (inits)
2639 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
2640 alloc_expr);
2643 /* unless an allocation function is declared with an empty excep-
2644 tion-specification (_except.spec_), throw(), it indicates failure to
2645 allocate storage by throwing a bad_alloc exception (clause _except_,
2646 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2647 cation function is declared with an empty exception-specification,
2648 throw(), it returns null to indicate failure to allocate storage and a
2649 non-null pointer otherwise.
2651 So check for a null exception spec on the op new we just called. */
2653 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2654 check_new = (flag_check_new || nothrow) && ! use_java_new;
2656 if (cookie_size)
2658 tree cookie;
2659 tree cookie_ptr;
2660 tree size_ptr_type;
2662 /* Adjust so we're pointing to the start of the object. */
2663 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
2665 /* Store the number of bytes allocated so that we can know how
2666 many elements to destroy later. We use the last sizeof
2667 (size_t) bytes to store the number of elements. */
2668 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
2669 cookie_ptr = fold_build_pointer_plus_loc (input_location,
2670 alloc_node, cookie_ptr);
2671 size_ptr_type = build_pointer_type (sizetype);
2672 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
2673 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2675 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
2677 if (targetm.cxx.cookie_has_size ())
2679 /* Also store the element size. */
2680 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
2681 fold_build1_loc (input_location,
2682 NEGATE_EXPR, sizetype,
2683 size_in_bytes (sizetype)));
2685 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2686 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
2687 size_in_bytes (elt_type));
2688 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
2689 cookie, cookie_expr);
2692 else
2694 cookie_expr = NULL_TREE;
2695 data_addr = alloc_node;
2698 /* Now use a pointer to the type we've actually allocated. */
2700 /* But we want to operate on a non-const version to start with,
2701 since we'll be modifying the elements. */
2702 non_const_pointer_type = build_pointer_type
2703 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
2705 data_addr = fold_convert (non_const_pointer_type, data_addr);
2706 /* Any further uses of alloc_node will want this type, too. */
2707 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
2709 /* Now initialize the allocated object. Note that we preevaluate the
2710 initialization expression, apart from the actual constructor call or
2711 assignment--we do this because we want to delay the allocation as long
2712 as possible in order to minimize the size of the exception region for
2713 placement delete. */
2714 if (is_initialized)
2716 bool stable;
2717 bool explicit_value_init_p = false;
2719 if (*init != NULL && (*init)->is_empty ())
2721 *init = NULL;
2722 explicit_value_init_p = true;
2725 if (processing_template_decl && explicit_value_init_p)
2727 /* build_value_init doesn't work in templates, and we don't need
2728 the initializer anyway since we're going to throw it away and
2729 rebuild it at instantiation time, so just build up a single
2730 constructor call to get any appropriate diagnostics. */
2731 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2732 if (type_build_ctor_call (elt_type))
2733 init_expr = build_special_member_call (init_expr,
2734 complete_ctor_identifier,
2735 init, elt_type,
2736 LOOKUP_NORMAL,
2737 complain);
2738 stable = stabilize_init (init_expr, &init_preeval_expr);
2740 else if (array_p)
2742 tree vecinit = NULL_TREE;
2743 if (vec_safe_length (*init) == 1
2744 && BRACE_ENCLOSED_INITIALIZER_P ((**init)[0])
2745 && CONSTRUCTOR_IS_DIRECT_INIT ((**init)[0]))
2747 vecinit = (**init)[0];
2748 if (CONSTRUCTOR_NELTS (vecinit) == 0)
2749 /* List-value-initialization, leave it alone. */;
2750 else
2752 tree arraytype, domain;
2753 if (TREE_CONSTANT (nelts))
2754 domain = compute_array_index_type (NULL_TREE, nelts,
2755 complain);
2756 else
2757 /* We'll check the length at runtime. */
2758 domain = NULL_TREE;
2759 arraytype = build_cplus_array_type (type, domain);
2760 vecinit = digest_init (arraytype, vecinit, complain);
2763 else if (*init)
2765 if (complain & tf_error)
2766 permerror (input_location,
2767 "parenthesized initializer in array new");
2768 else
2769 return error_mark_node;
2770 vecinit = build_tree_list_vec (*init);
2772 init_expr
2773 = build_vec_init (data_addr,
2774 cp_build_binary_op (input_location,
2775 MINUS_EXPR, outer_nelts,
2776 integer_one_node,
2777 complain),
2778 vecinit,
2779 explicit_value_init_p,
2780 /*from_array=*/0,
2781 complain);
2783 /* An array initialization is stable because the initialization
2784 of each element is a full-expression, so the temporaries don't
2785 leak out. */
2786 stable = true;
2788 else
2790 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2792 if (type_build_ctor_call (type) && !explicit_value_init_p)
2794 init_expr = build_special_member_call (init_expr,
2795 complete_ctor_identifier,
2796 init, elt_type,
2797 LOOKUP_NORMAL,
2798 complain);
2800 else if (explicit_value_init_p)
2802 /* Something like `new int()'. */
2803 tree val = build_value_init (type, complain);
2804 if (val == error_mark_node)
2805 return error_mark_node;
2806 init_expr = build2 (INIT_EXPR, type, init_expr, val);
2808 else
2810 tree ie;
2812 /* We are processing something like `new int (10)', which
2813 means allocate an int, and initialize it with 10. */
2815 ie = build_x_compound_expr_from_vec (*init, "new initializer",
2816 complain);
2817 init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie,
2818 complain);
2820 stable = stabilize_init (init_expr, &init_preeval_expr);
2823 if (init_expr == error_mark_node)
2824 return error_mark_node;
2826 /* If any part of the object initialization terminates by throwing an
2827 exception and a suitable deallocation function can be found, the
2828 deallocation function is called to free the memory in which the
2829 object was being constructed, after which the exception continues
2830 to propagate in the context of the new-expression. If no
2831 unambiguous matching deallocation function can be found,
2832 propagating the exception does not cause the object's memory to be
2833 freed. */
2834 if (flag_exceptions && ! use_java_new)
2836 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
2837 tree cleanup;
2839 /* The Standard is unclear here, but the right thing to do
2840 is to use the same method for finding deallocation
2841 functions that we use for finding allocation functions. */
2842 cleanup = (build_op_delete_call
2843 (dcode,
2844 alloc_node,
2845 size,
2846 globally_qualified_p,
2847 placement_allocation_fn_p ? alloc_call : NULL_TREE,
2848 alloc_fn,
2849 complain));
2851 if (!cleanup)
2852 /* We're done. */;
2853 else if (stable)
2854 /* This is much simpler if we were able to preevaluate all of
2855 the arguments to the constructor call. */
2857 /* CLEANUP is compiler-generated, so no diagnostics. */
2858 TREE_NO_WARNING (cleanup) = true;
2859 init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
2860 init_expr, cleanup);
2861 /* Likewise, this try-catch is compiler-generated. */
2862 TREE_NO_WARNING (init_expr) = true;
2864 else
2865 /* Ack! First we allocate the memory. Then we set our sentry
2866 variable to true, and expand a cleanup that deletes the
2867 memory if sentry is true. Then we run the constructor, and
2868 finally clear the sentry.
2870 We need to do this because we allocate the space first, so
2871 if there are any temporaries with cleanups in the
2872 constructor args and we weren't able to preevaluate them, we
2873 need this EH region to extend until end of full-expression
2874 to preserve nesting. */
2876 tree end, sentry, begin;
2878 begin = get_target_expr (boolean_true_node);
2879 CLEANUP_EH_ONLY (begin) = 1;
2881 sentry = TARGET_EXPR_SLOT (begin);
2883 /* CLEANUP is compiler-generated, so no diagnostics. */
2884 TREE_NO_WARNING (cleanup) = true;
2886 TARGET_EXPR_CLEANUP (begin)
2887 = build3 (COND_EXPR, void_type_node, sentry,
2888 cleanup, void_zero_node);
2890 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2891 sentry, boolean_false_node);
2893 init_expr
2894 = build2 (COMPOUND_EXPR, void_type_node, begin,
2895 build2 (COMPOUND_EXPR, void_type_node, init_expr,
2896 end));
2897 /* Likewise, this is compiler-generated. */
2898 TREE_NO_WARNING (init_expr) = true;
2902 else
2903 init_expr = NULL_TREE;
2905 /* Now build up the return value in reverse order. */
2907 rval = data_addr;
2909 if (init_expr)
2910 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2911 if (cookie_expr)
2912 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2914 if (rval == data_addr)
2915 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2916 and return the call (which doesn't need to be adjusted). */
2917 rval = TARGET_EXPR_INITIAL (alloc_expr);
2918 else
2920 if (check_new)
2922 tree ifexp = cp_build_binary_op (input_location,
2923 NE_EXPR, alloc_node,
2924 nullptr_node,
2925 complain);
2926 rval = build_conditional_expr (input_location, ifexp, rval,
2927 alloc_node, complain);
2930 /* Perform the allocation before anything else, so that ALLOC_NODE
2931 has been initialized before we start using it. */
2932 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2935 if (init_preeval_expr)
2936 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
2938 /* A new-expression is never an lvalue. */
2939 gcc_assert (!lvalue_p (rval));
2941 return convert (pointer_type, rval);
2944 /* Generate a representation for a C++ "new" expression. *PLACEMENT
2945 is a vector of placement-new arguments (or NULL if none). If NELTS
2946 is NULL, TYPE is the type of the storage to be allocated. If NELTS
2947 is not NULL, then this is an array-new allocation; TYPE is the type
2948 of the elements in the array and NELTS is the number of elements in
2949 the array. *INIT, if non-NULL, is the initializer for the new
2950 object, or an empty vector to indicate an initializer of "()". If
2951 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
2952 rather than just "new". This may change PLACEMENT and INIT. */
2954 tree
2955 build_new (vec<tree, va_gc> **placement, tree type, tree nelts,
2956 vec<tree, va_gc> **init, int use_global_new, tsubst_flags_t complain)
2958 tree rval;
2959 vec<tree, va_gc> *orig_placement = NULL;
2960 tree orig_nelts = NULL_TREE;
2961 vec<tree, va_gc> *orig_init = NULL;
2963 if (type == error_mark_node)
2964 return error_mark_node;
2966 if (nelts == NULL_TREE && vec_safe_length (*init) == 1
2967 /* Don't do auto deduction where it might affect mangling. */
2968 && (!processing_template_decl || at_function_scope_p ()))
2970 tree auto_node = type_uses_auto (type);
2971 if (auto_node)
2973 tree d_init = (**init)[0];
2974 d_init = resolve_nondeduced_context (d_init);
2975 type = do_auto_deduction (type, d_init, auto_node);
2979 if (processing_template_decl)
2981 if (dependent_type_p (type)
2982 || any_type_dependent_arguments_p (*placement)
2983 || (nelts && type_dependent_expression_p (nelts))
2984 || (nelts && *init)
2985 || any_type_dependent_arguments_p (*init))
2986 return build_raw_new_expr (*placement, type, nelts, *init,
2987 use_global_new);
2989 orig_placement = make_tree_vector_copy (*placement);
2990 orig_nelts = nelts;
2991 if (*init)
2992 orig_init = make_tree_vector_copy (*init);
2994 make_args_non_dependent (*placement);
2995 if (nelts)
2996 nelts = build_non_dependent_expr (nelts);
2997 make_args_non_dependent (*init);
3000 if (nelts)
3002 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
3004 if (complain & tf_error)
3005 permerror (input_location, "size in array new must have integral type");
3006 else
3007 return error_mark_node;
3009 nelts = mark_rvalue_use (nelts);
3010 nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
3013 /* ``A reference cannot be created by the new operator. A reference
3014 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3015 returned by new.'' ARM 5.3.3 */
3016 if (TREE_CODE (type) == REFERENCE_TYPE)
3018 if (complain & tf_error)
3019 error ("new cannot be applied to a reference type");
3020 else
3021 return error_mark_node;
3022 type = TREE_TYPE (type);
3025 if (TREE_CODE (type) == FUNCTION_TYPE)
3027 if (complain & tf_error)
3028 error ("new cannot be applied to a function type");
3029 return error_mark_node;
3032 /* The type allocated must be complete. If the new-type-id was
3033 "T[N]" then we are just checking that "T" is complete here, but
3034 that is equivalent, since the value of "N" doesn't matter. */
3035 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
3036 return error_mark_node;
3038 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
3039 if (rval == error_mark_node)
3040 return error_mark_node;
3042 if (processing_template_decl)
3044 tree ret = build_raw_new_expr (orig_placement, type, orig_nelts,
3045 orig_init, use_global_new);
3046 release_tree_vector (orig_placement);
3047 release_tree_vector (orig_init);
3048 return ret;
3051 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
3052 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
3053 TREE_NO_WARNING (rval) = 1;
3055 return rval;
3058 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
3060 tree
3061 build_java_class_ref (tree type)
3063 tree name = NULL_TREE, class_decl;
3064 static tree CL_suffix = NULL_TREE;
3065 if (CL_suffix == NULL_TREE)
3066 CL_suffix = get_identifier("class$");
3067 if (jclass_node == NULL_TREE)
3069 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
3070 if (jclass_node == NULL_TREE)
3072 error ("call to Java constructor, while %<jclass%> undefined");
3073 return error_mark_node;
3075 jclass_node = TREE_TYPE (jclass_node);
3078 /* Mangle the class$ field. */
3080 tree field;
3081 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3082 if (DECL_NAME (field) == CL_suffix)
3084 mangle_decl (field);
3085 name = DECL_ASSEMBLER_NAME (field);
3086 break;
3088 if (!field)
3090 error ("can%'t find %<class$%> in %qT", type);
3091 return error_mark_node;
3095 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
3096 if (class_decl == NULL_TREE)
3098 class_decl = build_decl (input_location,
3099 VAR_DECL, name, TREE_TYPE (jclass_node));
3100 TREE_STATIC (class_decl) = 1;
3101 DECL_EXTERNAL (class_decl) = 1;
3102 TREE_PUBLIC (class_decl) = 1;
3103 DECL_ARTIFICIAL (class_decl) = 1;
3104 DECL_IGNORED_P (class_decl) = 1;
3105 pushdecl_top_level (class_decl);
3106 make_decl_rtl (class_decl);
3108 return class_decl;
3111 static tree
3112 build_vec_delete_1 (tree base, tree maxindex, tree type,
3113 special_function_kind auto_delete_vec,
3114 int use_global_delete, tsubst_flags_t complain)
3116 tree virtual_size;
3117 tree ptype = build_pointer_type (type = complete_type (type));
3118 tree size_exp;
3120 /* Temporary variables used by the loop. */
3121 tree tbase, tbase_init;
3123 /* This is the body of the loop that implements the deletion of a
3124 single element, and moves temp variables to next elements. */
3125 tree body;
3127 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3128 tree loop = 0;
3130 /* This is the thing that governs what to do after the loop has run. */
3131 tree deallocate_expr = 0;
3133 /* This is the BIND_EXPR which holds the outermost iterator of the
3134 loop. It is convenient to set this variable up and test it before
3135 executing any other code in the loop.
3136 This is also the containing expression returned by this function. */
3137 tree controller = NULL_TREE;
3138 tree tmp;
3140 /* We should only have 1-D arrays here. */
3141 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
3143 if (base == error_mark_node || maxindex == error_mark_node)
3144 return error_mark_node;
3146 if (!COMPLETE_TYPE_P (type))
3148 if ((complain & tf_warning)
3149 && warning (OPT_Wdelete_incomplete,
3150 "possible problem detected in invocation of "
3151 "delete [] operator:"))
3153 cxx_incomplete_type_diagnostic (base, type, DK_WARNING);
3154 inform (input_location, "neither the destructor nor the "
3155 "class-specific operator delete [] will be called, "
3156 "even if they are declared when the class is defined");
3158 return build_builtin_delete_call (base);
3161 size_exp = size_in_bytes (type);
3163 if (! MAYBE_CLASS_TYPE_P (type))
3164 goto no_destructor;
3165 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3167 /* Make sure the destructor is callable. */
3168 if (type_build_dtor_call (type))
3170 tmp = build_delete (ptype, base, sfk_complete_destructor,
3171 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
3172 complain);
3173 if (tmp == error_mark_node)
3174 return error_mark_node;
3176 goto no_destructor;
3179 /* The below is short by the cookie size. */
3180 virtual_size = size_binop (MULT_EXPR, size_exp,
3181 convert (sizetype, maxindex));
3183 tbase = create_temporary_var (ptype);
3184 tbase_init
3185 = cp_build_modify_expr (tbase, NOP_EXPR,
3186 fold_build_pointer_plus_loc (input_location,
3187 fold_convert (ptype,
3188 base),
3189 virtual_size),
3190 complain);
3191 if (tbase_init == error_mark_node)
3192 return error_mark_node;
3193 controller = build3 (BIND_EXPR, void_type_node, tbase,
3194 NULL_TREE, NULL_TREE);
3195 TREE_SIDE_EFFECTS (controller) = 1;
3197 body = build1 (EXIT_EXPR, void_type_node,
3198 build2 (EQ_EXPR, boolean_type_node, tbase,
3199 fold_convert (ptype, base)));
3200 tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp);
3201 tmp = fold_build_pointer_plus (tbase, tmp);
3202 tmp = cp_build_modify_expr (tbase, NOP_EXPR, tmp, complain);
3203 if (tmp == error_mark_node)
3204 return error_mark_node;
3205 body = build_compound_expr (input_location, body, tmp);
3206 tmp = build_delete (ptype, tbase, sfk_complete_destructor,
3207 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
3208 complain);
3209 if (tmp == error_mark_node)
3210 return error_mark_node;
3211 body = build_compound_expr (input_location, body, tmp);
3213 loop = build1 (LOOP_EXPR, void_type_node, body);
3214 loop = build_compound_expr (input_location, tbase_init, loop);
3216 no_destructor:
3217 /* Delete the storage if appropriate. */
3218 if (auto_delete_vec == sfk_deleting_destructor)
3220 tree base_tbd;
3222 /* The below is short by the cookie size. */
3223 virtual_size = size_binop (MULT_EXPR, size_exp,
3224 convert (sizetype, maxindex));
3226 if (! TYPE_VEC_NEW_USES_COOKIE (type))
3227 /* no header */
3228 base_tbd = base;
3229 else
3231 tree cookie_size;
3233 cookie_size = targetm.cxx.get_cookie_size (type);
3234 base_tbd = cp_build_binary_op (input_location,
3235 MINUS_EXPR,
3236 cp_convert (string_type_node,
3237 base, complain),
3238 cookie_size,
3239 complain);
3240 if (base_tbd == error_mark_node)
3241 return error_mark_node;
3242 base_tbd = cp_convert (ptype, base_tbd, complain);
3243 /* True size with header. */
3244 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
3247 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
3248 base_tbd, virtual_size,
3249 use_global_delete & 1,
3250 /*placement=*/NULL_TREE,
3251 /*alloc_fn=*/NULL_TREE,
3252 complain);
3255 body = loop;
3256 if (!deallocate_expr)
3258 else if (!body)
3259 body = deallocate_expr;
3260 else
3261 body = build_compound_expr (input_location, body, deallocate_expr);
3263 if (!body)
3264 body = integer_zero_node;
3266 /* Outermost wrapper: If pointer is null, punt. */
3267 body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3268 fold_build2_loc (input_location,
3269 NE_EXPR, boolean_type_node, base,
3270 convert (TREE_TYPE (base),
3271 nullptr_node)),
3272 body, integer_zero_node);
3273 body = build1 (NOP_EXPR, void_type_node, body);
3275 if (controller)
3277 TREE_OPERAND (controller, 1) = body;
3278 body = controller;
3281 if (TREE_CODE (base) == SAVE_EXPR)
3282 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
3283 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
3285 return convert_to_void (body, ICV_CAST, complain);
3288 /* Create an unnamed variable of the indicated TYPE. */
3290 tree
3291 create_temporary_var (tree type)
3293 tree decl;
3295 decl = build_decl (input_location,
3296 VAR_DECL, NULL_TREE, type);
3297 TREE_USED (decl) = 1;
3298 DECL_ARTIFICIAL (decl) = 1;
3299 DECL_IGNORED_P (decl) = 1;
3300 DECL_CONTEXT (decl) = current_function_decl;
3302 return decl;
3305 /* Create a new temporary variable of the indicated TYPE, initialized
3306 to INIT.
3308 It is not entered into current_binding_level, because that breaks
3309 things when it comes time to do final cleanups (which take place
3310 "outside" the binding contour of the function). */
3312 tree
3313 get_temp_regvar (tree type, tree init)
3315 tree decl;
3317 decl = create_temporary_var (type);
3318 add_decl_expr (decl);
3320 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
3321 tf_warning_or_error));
3323 return decl;
3326 /* `build_vec_init' returns tree structure that performs
3327 initialization of a vector of aggregate types.
3329 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
3330 to the first element, of POINTER_TYPE.
3331 MAXINDEX is the maximum index of the array (one less than the
3332 number of elements). It is only used if BASE is a pointer or
3333 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
3335 INIT is the (possibly NULL) initializer.
3337 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
3338 elements in the array are value-initialized.
3340 FROM_ARRAY is 0 if we should init everything with INIT
3341 (i.e., every element initialized from INIT).
3342 FROM_ARRAY is 1 if we should index into INIT in parallel
3343 with initialization of DECL.
3344 FROM_ARRAY is 2 if we should index into INIT in parallel,
3345 but use assignment instead of initialization. */
3347 tree
3348 build_vec_init (tree base, tree maxindex, tree init,
3349 bool explicit_value_init_p,
3350 int from_array, tsubst_flags_t complain)
3352 tree rval;
3353 tree base2 = NULL_TREE;
3354 tree itype = NULL_TREE;
3355 tree iterator;
3356 /* The type of BASE. */
3357 tree atype = TREE_TYPE (base);
3358 /* The type of an element in the array. */
3359 tree type = TREE_TYPE (atype);
3360 /* The element type reached after removing all outer array
3361 types. */
3362 tree inner_elt_type;
3363 /* The type of a pointer to an element in the array. */
3364 tree ptype;
3365 tree stmt_expr;
3366 tree compound_stmt;
3367 int destroy_temps;
3368 tree try_block = NULL_TREE;
3369 int num_initialized_elts = 0;
3370 bool is_global;
3371 tree const_init = NULL_TREE;
3372 tree obase = base;
3373 bool xvalue = false;
3374 bool errors = false;
3375 tree length_check = NULL_TREE;
3377 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
3378 maxindex = array_type_nelts (atype);
3380 if (maxindex == NULL_TREE || maxindex == error_mark_node)
3381 return error_mark_node;
3383 if (explicit_value_init_p)
3384 gcc_assert (!init);
3386 inner_elt_type = strip_array_types (type);
3388 /* Look through the TARGET_EXPR around a compound literal. */
3389 if (init && TREE_CODE (init) == TARGET_EXPR
3390 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
3391 && from_array != 2)
3392 init = TARGET_EXPR_INITIAL (init);
3394 /* If we have a braced-init-list, make sure that the array
3395 is big enough for all the initializers. */
3396 if (init && TREE_CODE (init) == CONSTRUCTOR
3397 && CONSTRUCTOR_NELTS (init) > 0
3398 && !TREE_CONSTANT (maxindex))
3399 length_check = fold_build2 (LT_EXPR, boolean_type_node, maxindex,
3400 size_int (CONSTRUCTOR_NELTS (init) - 1));
3402 if (init
3403 && TREE_CODE (atype) == ARRAY_TYPE
3404 && TREE_CONSTANT (maxindex)
3405 && (from_array == 2
3406 ? (!CLASS_TYPE_P (inner_elt_type)
3407 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (inner_elt_type))
3408 : !TYPE_NEEDS_CONSTRUCTING (type))
3409 && ((TREE_CODE (init) == CONSTRUCTOR
3410 /* Don't do this if the CONSTRUCTOR might contain something
3411 that might throw and require us to clean up. */
3412 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
3413 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
3414 || from_array))
3416 /* Do non-default initialization of trivial arrays resulting from
3417 brace-enclosed initializers. In this case, digest_init and
3418 store_constructor will handle the semantics for us. */
3420 stmt_expr = build2 (INIT_EXPR, atype, base, init);
3421 if (length_check)
3422 stmt_expr = build3 (COND_EXPR, atype, length_check,
3423 throw_bad_array_length (),
3424 stmt_expr);
3425 return stmt_expr;
3428 maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
3429 if (TREE_CODE (atype) == ARRAY_TYPE)
3431 ptype = build_pointer_type (type);
3432 base = decay_conversion (base, complain);
3433 if (base == error_mark_node)
3434 return error_mark_node;
3435 base = cp_convert (ptype, base, complain);
3437 else
3438 ptype = atype;
3440 /* The code we are generating looks like:
3442 T* t1 = (T*) base;
3443 T* rval = t1;
3444 ptrdiff_t iterator = maxindex;
3445 try {
3446 for (; iterator != -1; --iterator) {
3447 ... initialize *t1 ...
3448 ++t1;
3450 } catch (...) {
3451 ... destroy elements that were constructed ...
3453 rval;
3456 We can omit the try and catch blocks if we know that the
3457 initialization will never throw an exception, or if the array
3458 elements do not have destructors. We can omit the loop completely if
3459 the elements of the array do not have constructors.
3461 We actually wrap the entire body of the above in a STMT_EXPR, for
3462 tidiness.
3464 When copying from array to another, when the array elements have
3465 only trivial copy constructors, we should use __builtin_memcpy
3466 rather than generating a loop. That way, we could take advantage
3467 of whatever cleverness the back end has for dealing with copies
3468 of blocks of memory. */
3470 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
3471 destroy_temps = stmts_are_full_exprs_p ();
3472 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3473 rval = get_temp_regvar (ptype, base);
3474 base = get_temp_regvar (ptype, rval);
3475 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
3477 /* If initializing one array from another, initialize element by
3478 element. We rely upon the below calls to do the argument
3479 checking. Evaluate the initializer before entering the try block. */
3480 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
3482 if (lvalue_kind (init) & clk_rvalueref)
3483 xvalue = true;
3484 base2 = decay_conversion (init, complain);
3485 if (base2 == error_mark_node)
3486 return error_mark_node;
3487 itype = TREE_TYPE (base2);
3488 base2 = get_temp_regvar (itype, base2);
3489 itype = TREE_TYPE (itype);
3492 /* Protect the entire array initialization so that we can destroy
3493 the partially constructed array if an exception is thrown.
3494 But don't do this if we're assigning. */
3495 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3496 && from_array != 2)
3498 try_block = begin_try_block ();
3501 /* If the initializer is {}, then all elements are initialized from {}.
3502 But for non-classes, that's the same as value-initialization. */
3503 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
3504 && CONSTRUCTOR_NELTS (init) == 0)
3506 if (CLASS_TYPE_P (type))
3507 /* Leave init alone. */;
3508 else
3510 init = NULL_TREE;
3511 explicit_value_init_p = true;
3515 /* Maybe pull out constant value when from_array? */
3517 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
3519 /* Do non-default initialization of non-trivial arrays resulting from
3520 brace-enclosed initializers. */
3521 unsigned HOST_WIDE_INT idx;
3522 tree field, elt;
3523 /* Should we try to create a constant initializer? */
3524 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
3525 && TREE_CONSTANT (maxindex)
3526 && (literal_type_p (inner_elt_type)
3527 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
3528 /* If the constructor already has the array type, it's been through
3529 digest_init, so we shouldn't try to do anything more. */
3530 bool digested = same_type_p (atype, TREE_TYPE (init));
3531 bool saw_non_const = false;
3532 bool saw_const = false;
3533 /* If we're initializing a static array, we want to do static
3534 initialization of any elements with constant initializers even if
3535 some are non-constant. */
3536 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
3537 vec<constructor_elt, va_gc> *new_vec;
3538 from_array = 0;
3540 if (length_check)
3542 tree throw_call;
3543 if (array_of_runtime_bound_p (atype))
3544 throw_call = throw_bad_array_length ();
3545 else
3546 throw_call = throw_bad_array_new_length ();
3547 length_check = build3 (COND_EXPR, void_type_node, length_check,
3548 throw_call, void_zero_node);
3549 finish_expr_stmt (length_check);
3552 if (try_const)
3553 vec_alloc (new_vec, CONSTRUCTOR_NELTS (init));
3554 else
3555 new_vec = NULL;
3557 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
3559 tree baseref = build1 (INDIRECT_REF, type, base);
3560 tree one_init;
3562 num_initialized_elts++;
3564 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3565 if (digested)
3566 one_init = build2 (INIT_EXPR, type, baseref, elt);
3567 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3568 one_init = build_aggr_init (baseref, elt, 0, complain);
3569 else
3570 one_init = cp_build_modify_expr (baseref, NOP_EXPR,
3571 elt, complain);
3572 if (one_init == error_mark_node)
3573 errors = true;
3574 if (try_const)
3576 tree e = one_init;
3577 if (TREE_CODE (e) == EXPR_STMT)
3578 e = TREE_OPERAND (e, 0);
3579 if (TREE_CODE (e) == CONVERT_EXPR
3580 && VOID_TYPE_P (TREE_TYPE (e)))
3581 e = TREE_OPERAND (e, 0);
3582 e = maybe_constant_init (e);
3583 if (reduced_constant_expression_p (e))
3585 CONSTRUCTOR_APPEND_ELT (new_vec, field, e);
3586 if (do_static_init)
3587 one_init = NULL_TREE;
3588 else
3589 one_init = build2 (INIT_EXPR, type, baseref, e);
3590 saw_const = true;
3592 else
3594 if (do_static_init)
3596 tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
3597 true);
3598 if (value)
3599 CONSTRUCTOR_APPEND_ELT (new_vec, field, value);
3601 saw_non_const = true;
3605 if (one_init)
3606 finish_expr_stmt (one_init);
3607 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3609 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, 0, complain);
3610 if (one_init == error_mark_node)
3611 errors = true;
3612 else
3613 finish_expr_stmt (one_init);
3615 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3616 complain);
3617 if (one_init == error_mark_node)
3618 errors = true;
3619 else
3620 finish_expr_stmt (one_init);
3623 if (try_const)
3625 if (!saw_non_const)
3626 const_init = build_constructor (atype, new_vec);
3627 else if (do_static_init && saw_const)
3628 DECL_INITIAL (obase) = build_constructor (atype, new_vec);
3629 else
3630 vec_free (new_vec);
3633 /* Any elements without explicit initializers get {}. */
3634 if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type))
3635 init = build_constructor (init_list_type_node, NULL);
3636 else
3638 init = NULL_TREE;
3639 explicit_value_init_p = true;
3642 else if (from_array)
3644 if (init)
3645 /* OK, we set base2 above. */;
3646 else if (CLASS_TYPE_P (type)
3647 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3649 if (complain & tf_error)
3650 error ("initializer ends prematurely");
3651 errors = true;
3655 /* Now, default-initialize any remaining elements. We don't need to
3656 do that if a) the type does not need constructing, or b) we've
3657 already initialized all the elements.
3659 We do need to keep going if we're copying an array. */
3661 if (from_array
3662 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
3663 && ! (host_integerp (maxindex, 0)
3664 && (num_initialized_elts
3665 == tree_low_cst (maxindex, 0) + 1))))
3667 /* If the ITERATOR is equal to -1, then we don't have to loop;
3668 we've already initialized all the elements. */
3669 tree for_stmt;
3670 tree elt_init;
3671 tree to;
3673 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
3674 finish_for_init_stmt (for_stmt);
3675 finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
3676 build_int_cst (TREE_TYPE (iterator), -1)),
3677 for_stmt, false);
3678 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3679 complain);
3680 if (elt_init == error_mark_node)
3681 errors = true;
3682 finish_for_expr (elt_init, for_stmt);
3684 to = build1 (INDIRECT_REF, type, base);
3686 if (from_array)
3688 tree from;
3690 if (base2)
3692 from = build1 (INDIRECT_REF, itype, base2);
3693 if (xvalue)
3694 from = move (from);
3696 else
3697 from = NULL_TREE;
3699 if (from_array == 2)
3700 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3701 complain);
3702 else if (type_build_ctor_call (type))
3703 elt_init = build_aggr_init (to, from, 0, complain);
3704 else if (from)
3705 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3706 complain);
3707 else
3708 gcc_unreachable ();
3710 else if (TREE_CODE (type) == ARRAY_TYPE)
3712 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
3713 sorry
3714 ("cannot initialize multi-dimensional array with initializer");
3715 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
3716 0, init,
3717 explicit_value_init_p,
3718 0, complain);
3720 else if (explicit_value_init_p)
3722 elt_init = build_value_init (type, complain);
3723 if (elt_init != error_mark_node)
3724 elt_init = build2 (INIT_EXPR, type, to, elt_init);
3726 else
3728 gcc_assert (type_build_ctor_call (type) || init);
3729 if (CLASS_TYPE_P (type))
3730 elt_init = build_aggr_init (to, init, 0, complain);
3731 else
3733 if (TREE_CODE (init) == TREE_LIST)
3734 init = build_x_compound_expr_from_list (init, ELK_INIT,
3735 complain);
3736 elt_init = build2 (INIT_EXPR, type, to, init);
3740 if (elt_init == error_mark_node)
3741 errors = true;
3743 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3744 finish_expr_stmt (elt_init);
3745 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3747 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
3748 complain));
3749 if (base2)
3750 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0,
3751 complain));
3753 finish_for_stmt (for_stmt);
3756 /* Make sure to cleanup any partially constructed elements. */
3757 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3758 && from_array != 2)
3760 tree e;
3761 tree m = cp_build_binary_op (input_location,
3762 MINUS_EXPR, maxindex, iterator,
3763 complain);
3765 /* Flatten multi-dimensional array since build_vec_delete only
3766 expects one-dimensional array. */
3767 if (TREE_CODE (type) == ARRAY_TYPE)
3768 m = cp_build_binary_op (input_location,
3769 MULT_EXPR, m,
3770 /* Avoid mixing signed and unsigned. */
3771 convert (TREE_TYPE (m),
3772 array_type_nelts_total (type)),
3773 complain);
3775 finish_cleanup_try_block (try_block);
3776 e = build_vec_delete_1 (rval, m,
3777 inner_elt_type, sfk_complete_destructor,
3778 /*use_global_delete=*/0, complain);
3779 if (e == error_mark_node)
3780 errors = true;
3781 finish_cleanup (e, try_block);
3784 /* The value of the array initialization is the array itself, RVAL
3785 is a pointer to the first element. */
3786 finish_stmt_expr_expr (rval, stmt_expr);
3788 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
3790 /* Now make the result have the correct type. */
3791 if (TREE_CODE (atype) == ARRAY_TYPE)
3793 atype = build_pointer_type (atype);
3794 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
3795 stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain);
3796 TREE_NO_WARNING (stmt_expr) = 1;
3799 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3801 if (const_init)
3802 return build2 (INIT_EXPR, atype, obase, const_init);
3803 if (errors)
3804 return error_mark_node;
3805 return stmt_expr;
3808 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3809 build_delete. */
3811 static tree
3812 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
3813 tsubst_flags_t complain)
3815 tree name;
3816 tree fn;
3817 switch (dtor_kind)
3819 case sfk_complete_destructor:
3820 name = complete_dtor_identifier;
3821 break;
3823 case sfk_base_destructor:
3824 name = base_dtor_identifier;
3825 break;
3827 case sfk_deleting_destructor:
3828 name = deleting_dtor_identifier;
3829 break;
3831 default:
3832 gcc_unreachable ();
3834 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
3835 return build_new_method_call (exp, fn,
3836 /*args=*/NULL,
3837 /*conversion_path=*/NULL_TREE,
3838 flags,
3839 /*fn_p=*/NULL,
3840 complain);
3843 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3844 ADDR is an expression which yields the store to be destroyed.
3845 AUTO_DELETE is the name of the destructor to call, i.e., either
3846 sfk_complete_destructor, sfk_base_destructor, or
3847 sfk_deleting_destructor.
3849 FLAGS is the logical disjunction of zero or more LOOKUP_
3850 flags. See cp-tree.h for more info. */
3852 tree
3853 build_delete (tree otype, tree addr, special_function_kind auto_delete,
3854 int flags, int use_global_delete, tsubst_flags_t complain)
3856 tree expr;
3858 if (addr == error_mark_node)
3859 return error_mark_node;
3861 tree type = TYPE_MAIN_VARIANT (otype);
3863 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3864 set to `error_mark_node' before it gets properly cleaned up. */
3865 if (type == error_mark_node)
3866 return error_mark_node;
3868 if (TREE_CODE (type) == POINTER_TYPE)
3869 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3871 if (TREE_CODE (type) == ARRAY_TYPE)
3873 if (TYPE_DOMAIN (type) == NULL_TREE)
3875 if (complain & tf_error)
3876 error ("unknown array size in delete");
3877 return error_mark_node;
3879 return build_vec_delete (addr, array_type_nelts (type),
3880 auto_delete, use_global_delete, complain);
3883 if (TYPE_PTR_P (otype))
3885 bool complete_p = true;
3887 addr = mark_rvalue_use (addr);
3889 /* We don't want to warn about delete of void*, only other
3890 incomplete types. Deleting other incomplete types
3891 invokes undefined behavior, but it is not ill-formed, so
3892 compile to something that would even do The Right Thing
3893 (TM) should the type have a trivial dtor and no delete
3894 operator. */
3895 if (!VOID_TYPE_P (type))
3897 complete_type (type);
3898 if (!COMPLETE_TYPE_P (type))
3900 if ((complain & tf_warning)
3901 && warning (OPT_Wdelete_incomplete,
3902 "possible problem detected in invocation of "
3903 "delete operator:"))
3905 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
3906 inform (input_location,
3907 "neither the destructor nor the class-specific "
3908 "operator delete will be called, even if they are "
3909 "declared when the class is defined");
3911 complete_p = false;
3913 else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor
3914 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
3915 && TYPE_POLYMORPHIC_P (type))
3917 tree dtor;
3918 dtor = CLASSTYPE_DESTRUCTORS (type);
3919 if (!dtor || !DECL_VINDEX (dtor))
3921 if (CLASSTYPE_PURE_VIRTUALS (type))
3922 warning (OPT_Wdelete_non_virtual_dtor,
3923 "deleting object of abstract class type %qT"
3924 " which has non-virtual destructor"
3925 " will cause undefined behaviour", type);
3926 else
3927 warning (OPT_Wdelete_non_virtual_dtor,
3928 "deleting object of polymorphic class type %qT"
3929 " which has non-virtual destructor"
3930 " might cause undefined behaviour", type);
3934 if (VOID_TYPE_P (type) || !complete_p || !MAYBE_CLASS_TYPE_P (type))
3935 /* Call the builtin operator delete. */
3936 return build_builtin_delete_call (addr);
3937 if (TREE_SIDE_EFFECTS (addr))
3938 addr = save_expr (addr);
3940 /* Throw away const and volatile on target type of addr. */
3941 addr = convert_force (build_pointer_type (type), addr, 0, complain);
3943 else
3945 /* Don't check PROTECT here; leave that decision to the
3946 destructor. If the destructor is accessible, call it,
3947 else report error. */
3948 addr = cp_build_addr_expr (addr, complain);
3949 if (addr == error_mark_node)
3950 return error_mark_node;
3951 if (TREE_SIDE_EFFECTS (addr))
3952 addr = save_expr (addr);
3954 addr = convert_force (build_pointer_type (type), addr, 0, complain);
3957 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3959 /* Make sure the destructor is callable. */
3960 if (type_build_dtor_call (type))
3962 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL,
3963 complain),
3964 sfk_complete_destructor, flags, complain);
3965 if (expr == error_mark_node)
3966 return error_mark_node;
3969 if (auto_delete != sfk_deleting_destructor)
3970 return void_zero_node;
3972 return build_op_delete_call (DELETE_EXPR, addr,
3973 cxx_sizeof_nowarn (type),
3974 use_global_delete,
3975 /*placement=*/NULL_TREE,
3976 /*alloc_fn=*/NULL_TREE,
3977 complain);
3979 else
3981 tree head = NULL_TREE;
3982 tree do_delete = NULL_TREE;
3983 tree ifexp;
3985 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
3986 lazily_declare_fn (sfk_destructor, type);
3988 /* For `::delete x', we must not use the deleting destructor
3989 since then we would not be sure to get the global `operator
3990 delete'. */
3991 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3993 /* We will use ADDR multiple times so we must save it. */
3994 addr = save_expr (addr);
3995 head = get_target_expr (build_headof (addr));
3996 /* Delete the object. */
3997 do_delete = build_builtin_delete_call (head);
3998 /* Otherwise, treat this like a complete object destructor
3999 call. */
4000 auto_delete = sfk_complete_destructor;
4002 /* If the destructor is non-virtual, there is no deleting
4003 variant. Instead, we must explicitly call the appropriate
4004 `operator delete' here. */
4005 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
4006 && auto_delete == sfk_deleting_destructor)
4008 /* We will use ADDR multiple times so we must save it. */
4009 addr = save_expr (addr);
4010 /* Build the call. */
4011 do_delete = build_op_delete_call (DELETE_EXPR,
4012 addr,
4013 cxx_sizeof_nowarn (type),
4014 /*global_p=*/false,
4015 /*placement=*/NULL_TREE,
4016 /*alloc_fn=*/NULL_TREE,
4017 complain);
4018 /* Call the complete object destructor. */
4019 auto_delete = sfk_complete_destructor;
4021 else if (auto_delete == sfk_deleting_destructor
4022 && TYPE_GETS_REG_DELETE (type))
4024 /* Make sure we have access to the member op delete, even though
4025 we'll actually be calling it from the destructor. */
4026 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
4027 /*global_p=*/false,
4028 /*placement=*/NULL_TREE,
4029 /*alloc_fn=*/NULL_TREE,
4030 complain);
4033 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, complain),
4034 auto_delete, flags, complain);
4035 if (expr == error_mark_node)
4036 return error_mark_node;
4037 if (do_delete)
4038 expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
4040 /* We need to calculate this before the dtor changes the vptr. */
4041 if (head)
4042 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
4044 if (flags & LOOKUP_DESTRUCTOR)
4045 /* Explicit destructor call; don't check for null pointer. */
4046 ifexp = integer_one_node;
4047 else
4049 /* Handle deleting a null pointer. */
4050 ifexp = fold (cp_build_binary_op (input_location,
4051 NE_EXPR, addr, nullptr_node,
4052 complain));
4053 if (ifexp == error_mark_node)
4054 return error_mark_node;
4057 if (ifexp != integer_one_node)
4058 expr = build3 (COND_EXPR, void_type_node,
4059 ifexp, expr, void_zero_node);
4061 return expr;
4065 /* At the beginning of a destructor, push cleanups that will call the
4066 destructors for our base classes and members.
4068 Called from begin_destructor_body. */
4070 void
4071 push_base_cleanups (void)
4073 tree binfo, base_binfo;
4074 int i;
4075 tree member;
4076 tree expr;
4077 vec<tree, va_gc> *vbases;
4079 /* Run destructors for all virtual baseclasses. */
4080 if (CLASSTYPE_VBASECLASSES (current_class_type))
4082 tree cond = (condition_conversion
4083 (build2 (BIT_AND_EXPR, integer_type_node,
4084 current_in_charge_parm,
4085 integer_two_node)));
4087 /* The CLASSTYPE_VBASECLASSES vector is in initialization
4088 order, which is also the right order for pushing cleanups. */
4089 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
4090 vec_safe_iterate (vbases, i, &base_binfo); i++)
4092 if (type_build_dtor_call (BINFO_TYPE (base_binfo)))
4094 expr = build_special_member_call (current_class_ref,
4095 base_dtor_identifier,
4096 NULL,
4097 base_binfo,
4098 (LOOKUP_NORMAL
4099 | LOOKUP_NONVIRTUAL),
4100 tf_warning_or_error);
4101 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
4103 expr = build3 (COND_EXPR, void_type_node, cond,
4104 expr, void_zero_node);
4105 finish_decl_cleanup (NULL_TREE, expr);
4111 /* Take care of the remaining baseclasses. */
4112 for (binfo = TYPE_BINFO (current_class_type), i = 0;
4113 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4115 if (BINFO_VIRTUAL_P (base_binfo)
4116 || !type_build_dtor_call (BINFO_TYPE (base_binfo)))
4117 continue;
4119 expr = build_special_member_call (current_class_ref,
4120 base_dtor_identifier,
4121 NULL, base_binfo,
4122 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
4123 tf_warning_or_error);
4124 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
4125 finish_decl_cleanup (NULL_TREE, expr);
4128 /* Don't automatically destroy union members. */
4129 if (TREE_CODE (current_class_type) == UNION_TYPE)
4130 return;
4132 for (member = TYPE_FIELDS (current_class_type); member;
4133 member = DECL_CHAIN (member))
4135 tree this_type = TREE_TYPE (member);
4136 if (this_type == error_mark_node
4137 || TREE_CODE (member) != FIELD_DECL
4138 || DECL_ARTIFICIAL (member))
4139 continue;
4140 if (ANON_AGGR_TYPE_P (this_type))
4141 continue;
4142 if (type_build_dtor_call (this_type))
4144 tree this_member = (build_class_member_access_expr
4145 (current_class_ref, member,
4146 /*access_path=*/NULL_TREE,
4147 /*preserve_reference=*/false,
4148 tf_warning_or_error));
4149 expr = build_delete (this_type, this_member,
4150 sfk_complete_destructor,
4151 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
4152 0, tf_warning_or_error);
4153 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
4154 finish_decl_cleanup (NULL_TREE, expr);
4159 /* Build a C++ vector delete expression.
4160 MAXINDEX is the number of elements to be deleted.
4161 ELT_SIZE is the nominal size of each element in the vector.
4162 BASE is the expression that should yield the store to be deleted.
4163 This function expands (or synthesizes) these calls itself.
4164 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
4166 This also calls delete for virtual baseclasses of elements of the vector.
4168 Update: MAXINDEX is no longer needed. The size can be extracted from the
4169 start of the vector for pointers, and from the type for arrays. We still
4170 use MAXINDEX for arrays because it happens to already have one of the
4171 values we'd have to extract. (We could use MAXINDEX with pointers to
4172 confirm the size, and trap if the numbers differ; not clear that it'd
4173 be worth bothering.) */
4175 tree
4176 build_vec_delete (tree base, tree maxindex,
4177 special_function_kind auto_delete_vec,
4178 int use_global_delete, tsubst_flags_t complain)
4180 tree type;
4181 tree rval;
4182 tree base_init = NULL_TREE;
4184 type = TREE_TYPE (base);
4186 if (TYPE_PTR_P (type))
4188 /* Step back one from start of vector, and read dimension. */
4189 tree cookie_addr;
4190 tree size_ptr_type = build_pointer_type (sizetype);
4192 base = mark_rvalue_use (base);
4193 if (TREE_SIDE_EFFECTS (base))
4195 base_init = get_target_expr (base);
4196 base = TARGET_EXPR_SLOT (base_init);
4198 type = strip_array_types (TREE_TYPE (type));
4199 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
4200 sizetype, TYPE_SIZE_UNIT (sizetype));
4201 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
4202 cookie_addr);
4203 maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, complain);
4205 else if (TREE_CODE (type) == ARRAY_TYPE)
4207 /* Get the total number of things in the array, maxindex is a
4208 bad name. */
4209 maxindex = array_type_nelts_total (type);
4210 type = strip_array_types (type);
4211 base = decay_conversion (base, complain);
4212 if (base == error_mark_node)
4213 return error_mark_node;
4214 if (TREE_SIDE_EFFECTS (base))
4216 base_init = get_target_expr (base);
4217 base = TARGET_EXPR_SLOT (base_init);
4220 else
4222 if (base != error_mark_node && !(complain & tf_error))
4223 error ("type to vector delete is neither pointer or array type");
4224 return error_mark_node;
4227 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
4228 use_global_delete, complain);
4229 if (base_init && rval != error_mark_node)
4230 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
4232 return rval;