2013-06-18 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / init.c
blob4edd150d91360b3d3b22fda5d6619bf9fb92c462
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 tree build_vtbl_address (tree);
47 static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
49 /* We are about to generate some complex initialization code.
50 Conceptually, it is all a single expression. However, we may want
51 to include conditionals, loops, and other such statement-level
52 constructs. Therefore, we build the initialization code inside a
53 statement-expression. This function starts such an expression.
54 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
55 pass them back to finish_init_stmts when the expression is
56 complete. */
58 static bool
59 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
61 bool is_global = !building_stmt_list_p ();
63 *stmt_expr_p = begin_stmt_expr ();
64 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
66 return is_global;
69 /* Finish out the statement-expression begun by the previous call to
70 begin_init_stmts. Returns the statement-expression itself. */
72 static tree
73 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
75 finish_compound_stmt (compound_stmt);
77 stmt_expr = finish_stmt_expr (stmt_expr, true);
79 gcc_assert (!building_stmt_list_p () == is_global);
81 return stmt_expr;
84 /* Constructors */
86 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
87 which we want to initialize the vtable pointer for, DATA is
88 TREE_LIST whose TREE_VALUE is the this ptr expression. */
90 static tree
91 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
93 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
94 return dfs_skip_bases;
96 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
98 tree base_ptr = TREE_VALUE ((tree) data);
100 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
101 tf_warning_or_error);
103 expand_virtual_init (binfo, base_ptr);
106 return NULL_TREE;
109 /* Initialize all the vtable pointers in the object pointed to by
110 ADDR. */
112 void
113 initialize_vtbl_ptrs (tree addr)
115 tree list;
116 tree type;
118 type = TREE_TYPE (TREE_TYPE (addr));
119 list = build_tree_list (type, addr);
121 /* Walk through the hierarchy, initializing the vptr in each base
122 class. We do these in pre-order because we can't find the virtual
123 bases for a class until we've initialized the vtbl for that
124 class. */
125 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
128 /* Return an expression for the zero-initialization of an object with
129 type T. This expression will either be a constant (in the case
130 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
131 aggregate), or NULL (in the case that T does not require
132 initialization). In either case, the value can be used as
133 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
134 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
135 is the number of elements in the array. If STATIC_STORAGE_P is
136 TRUE, initializers are only generated for entities for which
137 zero-initialization does not simply mean filling the storage with
138 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
139 subfields with bit positions at or above that bit size shouldn't
140 be added. Note that this only works when the result is assigned
141 to a base COMPONENT_REF; if we only have a pointer to the base subobject,
142 expand_assignment will end up clearing the full size of TYPE. */
144 static tree
145 build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
146 tree field_size)
148 tree init = NULL_TREE;
150 /* [dcl.init]
152 To zero-initialize an object of type T means:
154 -- if T is a scalar type, the storage is set to the value of zero
155 converted to T.
157 -- if T is a non-union class type, the storage for each nonstatic
158 data member and each base-class subobject is zero-initialized.
160 -- if T is a union type, the storage for its first data member is
161 zero-initialized.
163 -- if T is an array type, the storage for each element is
164 zero-initialized.
166 -- if T is a reference type, no initialization is performed. */
168 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
170 if (type == error_mark_node)
172 else if (static_storage_p && zero_init_p (type))
173 /* In order to save space, we do not explicitly build initializers
174 for items that do not need them. GCC's semantics are that
175 items with static storage duration that are not otherwise
176 initialized are initialized to zero. */
178 else if (TYPE_PTR_OR_PTRMEM_P (type))
179 init = convert (type, nullptr_node);
180 else if (SCALAR_TYPE_P (type))
181 init = convert (type, integer_zero_node);
182 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
184 tree field;
185 vec<constructor_elt, va_gc> *v = NULL;
187 /* Iterate over the fields, building initializations. */
188 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
190 if (TREE_CODE (field) != FIELD_DECL)
191 continue;
193 /* Don't add virtual bases for base classes if they are beyond
194 the size of the current field, that means it is present
195 somewhere else in the object. */
196 if (field_size)
198 tree bitpos = bit_position (field);
199 if (TREE_CODE (bitpos) == INTEGER_CST
200 && !tree_int_cst_lt (bitpos, field_size))
201 continue;
204 /* Note that for class types there will be FIELD_DECLs
205 corresponding to base classes as well. Thus, iterating
206 over TYPE_FIELDs will result in correct initialization of
207 all of the subobjects. */
208 if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
210 tree new_field_size
211 = (DECL_FIELD_IS_BASE (field)
212 && DECL_SIZE (field)
213 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
214 ? DECL_SIZE (field) : NULL_TREE;
215 tree value = build_zero_init_1 (TREE_TYPE (field),
216 /*nelts=*/NULL_TREE,
217 static_storage_p,
218 new_field_size);
219 if (value)
220 CONSTRUCTOR_APPEND_ELT(v, field, value);
223 /* For unions, only the first field is initialized. */
224 if (TREE_CODE (type) == UNION_TYPE)
225 break;
228 /* Build a constructor to contain the initializations. */
229 init = build_constructor (type, v);
231 else if (TREE_CODE (type) == ARRAY_TYPE)
233 tree max_index;
234 vec<constructor_elt, va_gc> *v = NULL;
236 /* Iterate over the array elements, building initializations. */
237 if (nelts)
238 max_index = fold_build2_loc (input_location,
239 MINUS_EXPR, TREE_TYPE (nelts),
240 nelts, integer_one_node);
241 else
242 max_index = array_type_nelts (type);
244 /* If we have an error_mark here, we should just return error mark
245 as we don't know the size of the array yet. */
246 if (max_index == error_mark_node)
247 return error_mark_node;
248 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
250 /* A zero-sized array, which is accepted as an extension, will
251 have an upper bound of -1. */
252 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
254 constructor_elt ce;
256 /* If this is a one element array, we just use a regular init. */
257 if (tree_int_cst_equal (size_zero_node, max_index))
258 ce.index = size_zero_node;
259 else
260 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
261 max_index);
263 ce.value = build_zero_init_1 (TREE_TYPE (type),
264 /*nelts=*/NULL_TREE,
265 static_storage_p, NULL_TREE);
266 if (ce.value)
268 vec_alloc (v, 1);
269 v->quick_push (ce);
273 /* Build a constructor to contain the initializations. */
274 init = build_constructor (type, v);
276 else if (TREE_CODE (type) == VECTOR_TYPE)
277 init = build_zero_cst (type);
278 else
279 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
281 /* In all cases, the initializer is a constant. */
282 if (init)
283 TREE_CONSTANT (init) = 1;
285 return init;
288 /* Return an expression for the zero-initialization of an object with
289 type T. This expression will either be a constant (in the case
290 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
291 aggregate), or NULL (in the case that T does not require
292 initialization). In either case, the value can be used as
293 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
294 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
295 is the number of elements in the array. If STATIC_STORAGE_P is
296 TRUE, initializers are only generated for entities for which
297 zero-initialization does not simply mean filling the storage with
298 zero bytes. */
300 tree
301 build_zero_init (tree type, tree nelts, bool static_storage_p)
303 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
306 /* Return a suitable initializer for value-initializing an object of type
307 TYPE, as described in [dcl.init]. */
309 tree
310 build_value_init (tree type, tsubst_flags_t complain)
312 /* [dcl.init]
314 To value-initialize an object of type T means:
316 - if T is a class type (clause 9) with a user-provided constructor
317 (12.1), then the default constructor for T is called (and the
318 initialization is ill-formed if T has no accessible default
319 constructor);
321 - if T is a non-union class type without a user-provided constructor,
322 then every non-static data member and base-class component of T is
323 value-initialized;92)
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 92) Value-initialization for such a class object may be implemented by
333 zero-initializing the object and then calling the default
334 constructor. */
336 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
337 gcc_assert (!processing_template_decl
338 || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
340 if (CLASS_TYPE_P (type))
342 /* Instead of the above, only consider the user-providedness of the
343 default constructor itself so value-initializing a class with an
344 explicitly defaulted default constructor and another user-provided
345 constructor works properly (c++std-core-19883). */
346 if (type_has_user_provided_default_constructor (type)
347 || (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
348 && type_has_user_provided_constructor (type)))
349 return build_aggr_init_expr
350 (type,
351 build_special_member_call (NULL_TREE, complete_ctor_identifier,
352 NULL, type, LOOKUP_NORMAL,
353 complain));
354 else if (TYPE_HAS_COMPLEX_DFLT (type))
356 /* This is a class that needs constructing, but doesn't have
357 a user-provided constructor. So we need to zero-initialize
358 the object and then call the implicitly defined ctor.
359 This will be handled in simplify_aggr_init_expr. */
360 tree ctor = build_special_member_call
361 (NULL_TREE, complete_ctor_identifier,
362 NULL, type, LOOKUP_NORMAL, complain);
363 ctor = build_aggr_init_expr (type, ctor);
364 if (ctor != error_mark_node)
365 AGGR_INIT_ZERO_FIRST (ctor) = 1;
366 return ctor;
369 return build_value_init_noctor (type, complain);
372 /* Like build_value_init, but don't call the constructor for TYPE. Used
373 for base initializers. */
375 tree
376 build_value_init_noctor (tree type, tsubst_flags_t complain)
378 if (!COMPLETE_TYPE_P (type))
380 if (complain & tf_error)
381 error ("value-initialization of incomplete type %qT", type);
382 return error_mark_node;
384 /* FIXME the class and array cases should just use digest_init once it is
385 SFINAE-enabled. */
386 if (CLASS_TYPE_P (type))
388 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type));
390 if (TREE_CODE (type) != UNION_TYPE)
392 tree field;
393 vec<constructor_elt, va_gc> *v = NULL;
395 /* Iterate over the fields, building initializations. */
396 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
398 tree ftype, value;
400 if (TREE_CODE (field) != FIELD_DECL)
401 continue;
403 ftype = TREE_TYPE (field);
405 /* We could skip vfields and fields of types with
406 user-defined constructors, but I think that won't improve
407 performance at all; it should be simpler in general just
408 to zero out the entire object than try to only zero the
409 bits that actually need it. */
411 /* Note that for class types there will be FIELD_DECLs
412 corresponding to base classes as well. Thus, iterating
413 over TYPE_FIELDs will result in correct initialization of
414 all of the subobjects. */
415 value = build_value_init (ftype, complain);
417 if (value == error_mark_node)
418 return error_mark_node;
420 if (value)
421 CONSTRUCTOR_APPEND_ELT(v, field, value);
424 /* Build a constructor to contain the zero- initializations. */
425 return build_constructor (type, v);
428 else if (TREE_CODE (type) == ARRAY_TYPE)
430 vec<constructor_elt, va_gc> *v = NULL;
432 /* Iterate over the array elements, building initializations. */
433 tree max_index = array_type_nelts (type);
435 /* If we have an error_mark here, we should just return error mark
436 as we don't know the size of the array yet. */
437 if (max_index == error_mark_node)
439 if (complain & tf_error)
440 error ("cannot value-initialize array of unknown bound %qT",
441 type);
442 return error_mark_node;
444 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
446 /* A zero-sized array, which is accepted as an extension, will
447 have an upper bound of -1. */
448 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
450 constructor_elt ce;
452 /* If this is a one element array, we just use a regular init. */
453 if (tree_int_cst_equal (size_zero_node, max_index))
454 ce.index = size_zero_node;
455 else
456 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
458 ce.value = build_value_init (TREE_TYPE (type), complain);
459 if (ce.value)
461 if (ce.value == error_mark_node)
462 return error_mark_node;
464 vec_alloc (v, 1);
465 v->quick_push (ce);
467 /* We shouldn't have gotten here for anything that would need
468 non-trivial initialization, and gimplify_init_ctor_preeval
469 would need to be fixed to allow it. */
470 gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
471 && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
475 /* Build a constructor to contain the initializations. */
476 return build_constructor (type, v);
478 else if (TREE_CODE (type) == FUNCTION_TYPE)
480 if (complain & tf_error)
481 error ("value-initialization of function type %qT", type);
482 return error_mark_node;
484 else if (TREE_CODE (type) == REFERENCE_TYPE)
486 if (complain & tf_error)
487 error ("value-initialization of reference type %qT", type);
488 return error_mark_node;
491 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
494 /* Initialize current class with INIT, a TREE_LIST of
495 arguments for a target constructor. If TREE_LIST is void_type_node,
496 an empty initializer list was given. */
498 static void
499 perform_target_ctor (tree init)
501 tree decl = current_class_ref;
502 tree type = current_class_type;
504 finish_expr_stmt (build_aggr_init (decl, init, LOOKUP_NORMAL,
505 tf_warning_or_error));
506 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
508 tree expr = build_delete (type, decl, sfk_complete_destructor,
509 LOOKUP_NORMAL
510 |LOOKUP_NONVIRTUAL
511 |LOOKUP_DESTRUCTOR,
512 0, tf_warning_or_error);
513 if (expr != error_mark_node)
514 finish_eh_cleanup (expr);
518 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
519 arguments. If TREE_LIST is void_type_node, an empty initializer
520 list was given; if NULL_TREE no initializer was given. */
522 static void
523 perform_member_init (tree member, tree init)
525 tree decl;
526 tree type = TREE_TYPE (member);
528 /* Use the non-static data member initializer if there was no
529 mem-initializer for this field. */
530 if (init == NULL_TREE)
532 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
533 /* Do deferred instantiation of the NSDMI. */
534 init = (tsubst_copy_and_build
535 (DECL_INITIAL (DECL_TI_TEMPLATE (member)),
536 DECL_TI_ARGS (member),
537 tf_warning_or_error, member, /*function_p=*/false,
538 /*integral_constant_expression_p=*/false));
539 else
541 init = DECL_INITIAL (member);
542 if (init && TREE_CODE (init) == DEFAULT_ARG)
544 error ("constructor required before non-static data member "
545 "for %qD has been parsed", member);
546 init = NULL_TREE;
548 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
549 so the aggregate init code below will see a CONSTRUCTOR. */
550 if (init && TREE_CODE (init) == TARGET_EXPR
551 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
552 init = TARGET_EXPR_INITIAL (init);
553 init = break_out_target_exprs (init);
557 if (init == error_mark_node)
558 return;
560 /* Effective C++ rule 12 requires that all data members be
561 initialized. */
562 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
563 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
564 "%qD should be initialized in the member initialization list",
565 member);
567 /* Get an lvalue for the data member. */
568 decl = build_class_member_access_expr (current_class_ref, member,
569 /*access_path=*/NULL_TREE,
570 /*preserve_reference=*/true,
571 tf_warning_or_error);
572 if (decl == error_mark_node)
573 return;
575 if (warn_init_self && init && TREE_CODE (init) == TREE_LIST
576 && TREE_CHAIN (init) == NULL_TREE)
578 tree val = TREE_VALUE (init);
579 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
580 && TREE_OPERAND (val, 0) == current_class_ref)
581 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
582 OPT_Winit_self, "%qD is initialized with itself",
583 member);
586 if (init == void_type_node)
588 /* mem() means value-initialization. */
589 if (TREE_CODE (type) == ARRAY_TYPE)
591 init = build_vec_init_expr (type, init, tf_warning_or_error);
592 init = build2 (INIT_EXPR, type, decl, init);
593 finish_expr_stmt (init);
595 else
597 tree value = build_value_init (type, tf_warning_or_error);
598 if (value == error_mark_node)
599 return;
600 init = build2 (INIT_EXPR, type, decl, value);
601 finish_expr_stmt (init);
604 /* Deal with this here, as we will get confused if we try to call the
605 assignment op for an anonymous union. This can happen in a
606 synthesized copy constructor. */
607 else if (ANON_AGGR_TYPE_P (type))
609 if (init)
611 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
612 finish_expr_stmt (init);
615 else if (init
616 && (TREE_CODE (type) == REFERENCE_TYPE
617 /* Pre-digested NSDMI. */
618 || (((TREE_CODE (init) == CONSTRUCTOR
619 && TREE_TYPE (init) == type)
620 /* { } mem-initializer. */
621 || (TREE_CODE (init) == TREE_LIST
622 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
623 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init))))
624 && (CP_AGGREGATE_TYPE_P (type)
625 || is_std_init_list (type)))))
627 /* With references and list-initialization, we need to deal with
628 extending temporary lifetimes. 12.2p5: "A temporary bound to a
629 reference member in a constructor’s ctor-initializer (12.6.2)
630 persists until the constructor exits." */
631 unsigned i; tree t;
632 vec<tree, va_gc> *cleanups = make_tree_vector ();
633 if (TREE_CODE (init) == TREE_LIST)
634 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
635 tf_warning_or_error);
636 if (TREE_TYPE (init) != type)
638 if (BRACE_ENCLOSED_INITIALIZER_P (init)
639 && CP_AGGREGATE_TYPE_P (type))
640 init = reshape_init (type, init, tf_warning_or_error);
641 init = digest_init (type, init, tf_warning_or_error);
643 if (init == error_mark_node)
644 return;
645 /* A FIELD_DECL doesn't really have a suitable lifetime, but
646 make_temporary_var_for_ref_to_temp will treat it as automatic and
647 set_up_extended_ref_temp wants to use the decl in a warning. */
648 init = extend_ref_init_temps (member, init, &cleanups);
649 if (TREE_CODE (type) == ARRAY_TYPE
650 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
651 init = build_vec_init_expr (type, init, tf_warning_or_error);
652 init = build2 (INIT_EXPR, type, decl, init);
653 finish_expr_stmt (init);
654 FOR_EACH_VEC_ELT (*cleanups, i, t)
655 push_cleanup (decl, t, false);
656 release_tree_vector (cleanups);
658 else if (type_build_ctor_call (type)
659 || (init && CLASS_TYPE_P (strip_array_types (type))))
661 if (TREE_CODE (type) == ARRAY_TYPE)
663 if (init)
665 if (TREE_CHAIN (init))
666 init = error_mark_node;
667 else
668 init = TREE_VALUE (init);
669 if (BRACE_ENCLOSED_INITIALIZER_P (init))
670 init = digest_init (type, init, tf_warning_or_error);
672 if (init == NULL_TREE
673 || same_type_ignoring_top_level_qualifiers_p (type,
674 TREE_TYPE (init)))
676 init = build_vec_init_expr (type, init, tf_warning_or_error);
677 init = build2 (INIT_EXPR, type, decl, init);
678 finish_expr_stmt (init);
680 else
681 error ("invalid initializer for array member %q#D", member);
683 else
685 int flags = LOOKUP_NORMAL;
686 if (DECL_DEFAULTED_FN (current_function_decl))
687 flags |= LOOKUP_DEFAULTED;
688 if (CP_TYPE_CONST_P (type)
689 && init == NULL_TREE
690 && default_init_uninitialized_part (type))
691 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
692 vtable; still give this diagnostic. */
693 permerror (DECL_SOURCE_LOCATION (current_function_decl),
694 "uninitialized member %qD with %<const%> type %qT",
695 member, type);
696 finish_expr_stmt (build_aggr_init (decl, init, flags,
697 tf_warning_or_error));
700 else
702 if (init == NULL_TREE)
704 tree core_type;
705 /* member traversal: note it leaves init NULL */
706 if (TREE_CODE (type) == REFERENCE_TYPE)
707 permerror (DECL_SOURCE_LOCATION (current_function_decl),
708 "uninitialized reference member %qD",
709 member);
710 else if (CP_TYPE_CONST_P (type))
711 permerror (DECL_SOURCE_LOCATION (current_function_decl),
712 "uninitialized member %qD with %<const%> type %qT",
713 member, type);
715 core_type = strip_array_types (type);
717 if (CLASS_TYPE_P (core_type)
718 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
719 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
720 diagnose_uninitialized_cst_or_ref_member (core_type,
721 /*using_new=*/false,
722 /*complain=*/true);
724 else if (TREE_CODE (init) == TREE_LIST)
725 /* There was an explicit member initialization. Do some work
726 in that case. */
727 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
728 tf_warning_or_error);
730 if (init)
731 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
732 tf_warning_or_error));
735 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
737 tree expr;
739 expr = build_class_member_access_expr (current_class_ref, member,
740 /*access_path=*/NULL_TREE,
741 /*preserve_reference=*/false,
742 tf_warning_or_error);
743 expr = build_delete (type, expr, sfk_complete_destructor,
744 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
745 tf_warning_or_error);
747 if (expr != error_mark_node)
748 finish_eh_cleanup (expr);
752 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
753 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
755 static tree
756 build_field_list (tree t, tree list, int *uses_unions_p)
758 tree fields;
760 /* Note whether or not T is a union. */
761 if (TREE_CODE (t) == UNION_TYPE)
762 *uses_unions_p = 1;
764 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
766 tree fieldtype;
768 /* Skip CONST_DECLs for enumeration constants and so forth. */
769 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
770 continue;
772 fieldtype = TREE_TYPE (fields);
773 /* Keep track of whether or not any fields are unions. */
774 if (TREE_CODE (fieldtype) == UNION_TYPE)
775 *uses_unions_p = 1;
777 /* For an anonymous struct or union, we must recursively
778 consider the fields of the anonymous type. They can be
779 directly initialized from the constructor. */
780 if (ANON_AGGR_TYPE_P (fieldtype))
782 /* Add this field itself. Synthesized copy constructors
783 initialize the entire aggregate. */
784 list = tree_cons (fields, NULL_TREE, list);
785 /* And now add the fields in the anonymous aggregate. */
786 list = build_field_list (fieldtype, list, uses_unions_p);
788 /* Add this field. */
789 else if (DECL_NAME (fields))
790 list = tree_cons (fields, NULL_TREE, list);
793 return list;
796 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
797 a FIELD_DECL or BINFO in T that needs initialization. The
798 TREE_VALUE gives the initializer, or list of initializer arguments.
800 Return a TREE_LIST containing all of the initializations required
801 for T, in the order in which they should be performed. The output
802 list has the same format as the input. */
804 static tree
805 sort_mem_initializers (tree t, tree mem_inits)
807 tree init;
808 tree base, binfo, base_binfo;
809 tree sorted_inits;
810 tree next_subobject;
811 vec<tree, va_gc> *vbases;
812 int i;
813 int uses_unions_p = 0;
815 /* Build up a list of initializations. The TREE_PURPOSE of entry
816 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
817 TREE_VALUE will be the constructor arguments, or NULL if no
818 explicit initialization was provided. */
819 sorted_inits = NULL_TREE;
821 /* Process the virtual bases. */
822 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
823 vec_safe_iterate (vbases, i, &base); i++)
824 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
826 /* Process the direct bases. */
827 for (binfo = TYPE_BINFO (t), i = 0;
828 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
829 if (!BINFO_VIRTUAL_P (base_binfo))
830 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
832 /* Process the non-static data members. */
833 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
834 /* Reverse the entire list of initializations, so that they are in
835 the order that they will actually be performed. */
836 sorted_inits = nreverse (sorted_inits);
838 /* If the user presented the initializers in an order different from
839 that in which they will actually occur, we issue a warning. Keep
840 track of the next subobject which can be explicitly initialized
841 without issuing a warning. */
842 next_subobject = sorted_inits;
844 /* Go through the explicit initializers, filling in TREE_PURPOSE in
845 the SORTED_INITS. */
846 for (init = mem_inits; init; init = TREE_CHAIN (init))
848 tree subobject;
849 tree subobject_init;
851 subobject = TREE_PURPOSE (init);
853 /* If the explicit initializers are in sorted order, then
854 SUBOBJECT will be NEXT_SUBOBJECT, or something following
855 it. */
856 for (subobject_init = next_subobject;
857 subobject_init;
858 subobject_init = TREE_CHAIN (subobject_init))
859 if (TREE_PURPOSE (subobject_init) == subobject)
860 break;
862 /* Issue a warning if the explicit initializer order does not
863 match that which will actually occur.
864 ??? Are all these on the correct lines? */
865 if (warn_reorder && !subobject_init)
867 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
868 warning (OPT_Wreorder, "%q+D will be initialized after",
869 TREE_PURPOSE (next_subobject));
870 else
871 warning (OPT_Wreorder, "base %qT will be initialized after",
872 TREE_PURPOSE (next_subobject));
873 if (TREE_CODE (subobject) == FIELD_DECL)
874 warning (OPT_Wreorder, " %q+#D", subobject);
875 else
876 warning (OPT_Wreorder, " base %qT", subobject);
877 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
878 OPT_Wreorder, " when initialized here");
881 /* Look again, from the beginning of the list. */
882 if (!subobject_init)
884 subobject_init = sorted_inits;
885 while (TREE_PURPOSE (subobject_init) != subobject)
886 subobject_init = TREE_CHAIN (subobject_init);
889 /* It is invalid to initialize the same subobject more than
890 once. */
891 if (TREE_VALUE (subobject_init))
893 if (TREE_CODE (subobject) == FIELD_DECL)
894 error_at (DECL_SOURCE_LOCATION (current_function_decl),
895 "multiple initializations given for %qD",
896 subobject);
897 else
898 error_at (DECL_SOURCE_LOCATION (current_function_decl),
899 "multiple initializations given for base %qT",
900 subobject);
903 /* Record the initialization. */
904 TREE_VALUE (subobject_init) = TREE_VALUE (init);
905 next_subobject = subobject_init;
908 /* [class.base.init]
910 If a ctor-initializer specifies more than one mem-initializer for
911 multiple members of the same union (including members of
912 anonymous unions), the ctor-initializer is ill-formed.
914 Here we also splice out uninitialized union members. */
915 if (uses_unions_p)
917 tree *last_p = NULL;
918 tree *p;
919 for (p = &sorted_inits; *p; )
921 tree field;
922 tree ctx;
924 init = *p;
926 field = TREE_PURPOSE (init);
928 /* Skip base classes. */
929 if (TREE_CODE (field) != FIELD_DECL)
930 goto next;
932 /* If this is an anonymous union with no explicit initializer,
933 splice it out. */
934 if (!TREE_VALUE (init) && ANON_UNION_TYPE_P (TREE_TYPE (field)))
935 goto splice;
937 /* See if this field is a member of a union, or a member of a
938 structure contained in a union, etc. */
939 for (ctx = DECL_CONTEXT (field);
940 !same_type_p (ctx, t);
941 ctx = TYPE_CONTEXT (ctx))
942 if (TREE_CODE (ctx) == UNION_TYPE
943 || !ANON_AGGR_TYPE_P (ctx))
944 break;
945 /* If this field is not a member of a union, skip it. */
946 if (TREE_CODE (ctx) != UNION_TYPE)
947 goto next;
949 /* If this union member has no explicit initializer and no NSDMI,
950 splice it out. */
951 if (TREE_VALUE (init) || DECL_INITIAL (field))
952 /* OK. */;
953 else
954 goto splice;
956 /* It's only an error if we have two initializers for the same
957 union type. */
958 if (!last_p)
960 last_p = p;
961 goto next;
964 /* See if LAST_FIELD and the field initialized by INIT are
965 members of the same union. If so, there's a problem,
966 unless they're actually members of the same structure
967 which is itself a member of a union. For example, given:
969 union { struct { int i; int j; }; };
971 initializing both `i' and `j' makes sense. */
972 ctx = common_enclosing_class (DECL_CONTEXT (field),
973 DECL_CONTEXT (TREE_PURPOSE (*last_p)));
975 if (ctx && TREE_CODE (ctx) == UNION_TYPE)
977 /* A mem-initializer hides an NSDMI. */
978 if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
979 *last_p = TREE_CHAIN (*last_p);
980 else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
981 goto splice;
982 else
983 error_at (DECL_SOURCE_LOCATION (current_function_decl),
984 "initializations for multiple members of %qT",
985 ctx);
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 static 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_HAS_TRIVIAL_DESTRUCTOR (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);
1202 if (flag)
1203 expr = fold_build3_loc (input_location,
1204 COND_EXPR, void_type_node,
1205 c_common_truthvalue_conversion (input_location, flag),
1206 expr, integer_zero_node);
1208 finish_eh_cleanup (expr);
1211 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1212 constructor. */
1214 static void
1215 construct_virtual_base (tree vbase, tree arguments)
1217 tree inner_if_stmt;
1218 tree exp;
1219 tree flag;
1221 /* If there are virtual base classes with destructors, we need to
1222 emit cleanups to destroy them if an exception is thrown during
1223 the construction process. These exception regions (i.e., the
1224 period during which the cleanups must occur) begin from the time
1225 the construction is complete to the end of the function. If we
1226 create a conditional block in which to initialize the
1227 base-classes, then the cleanup region for the virtual base begins
1228 inside a block, and ends outside of that block. This situation
1229 confuses the sjlj exception-handling code. Therefore, we do not
1230 create a single conditional block, but one for each
1231 initialization. (That way the cleanup regions always begin
1232 in the outer block.) We trust the back end to figure out
1233 that the FLAG will not change across initializations, and
1234 avoid doing multiple tests. */
1235 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1236 inner_if_stmt = begin_if_stmt ();
1237 finish_if_stmt_cond (flag, inner_if_stmt);
1239 /* Compute the location of the virtual base. If we're
1240 constructing virtual bases, then we must be the most derived
1241 class. Therefore, we don't have to look up the virtual base;
1242 we already know where it is. */
1243 exp = convert_to_base_statically (current_class_ref, vbase);
1245 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1246 0, tf_warning_or_error);
1247 finish_then_clause (inner_if_stmt);
1248 finish_if_stmt (inner_if_stmt);
1250 expand_cleanup_for_base (vbase, flag);
1253 /* Find the context in which this FIELD can be initialized. */
1255 static tree
1256 initializing_context (tree field)
1258 tree t = DECL_CONTEXT (field);
1260 /* Anonymous union members can be initialized in the first enclosing
1261 non-anonymous union context. */
1262 while (t && ANON_AGGR_TYPE_P (t))
1263 t = TYPE_CONTEXT (t);
1264 return t;
1267 /* Function to give error message if member initialization specification
1268 is erroneous. FIELD is the member we decided to initialize.
1269 TYPE is the type for which the initialization is being performed.
1270 FIELD must be a member of TYPE.
1272 MEMBER_NAME is the name of the member. */
1274 static int
1275 member_init_ok_or_else (tree field, tree type, tree member_name)
1277 if (field == error_mark_node)
1278 return 0;
1279 if (!field)
1281 error ("class %qT does not have any field named %qD", type,
1282 member_name);
1283 return 0;
1285 if (VAR_P (field))
1287 error ("%q#D is a static data member; it can only be "
1288 "initialized at its definition",
1289 field);
1290 return 0;
1292 if (TREE_CODE (field) != FIELD_DECL)
1294 error ("%q#D is not a non-static data member of %qT",
1295 field, type);
1296 return 0;
1298 if (initializing_context (field) != type)
1300 error ("class %qT does not have any field named %qD", type,
1301 member_name);
1302 return 0;
1305 return 1;
1308 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1309 is a _TYPE node or TYPE_DECL which names a base for that type.
1310 Check the validity of NAME, and return either the base _TYPE, base
1311 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1312 NULL_TREE and issue a diagnostic.
1314 An old style unnamed direct single base construction is permitted,
1315 where NAME is NULL. */
1317 tree
1318 expand_member_init (tree name)
1320 tree basetype;
1321 tree field;
1323 if (!current_class_ref)
1324 return NULL_TREE;
1326 if (!name)
1328 /* This is an obsolete unnamed base class initializer. The
1329 parser will already have warned about its use. */
1330 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1332 case 0:
1333 error ("unnamed initializer for %qT, which has no base classes",
1334 current_class_type);
1335 return NULL_TREE;
1336 case 1:
1337 basetype = BINFO_TYPE
1338 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1339 break;
1340 default:
1341 error ("unnamed initializer for %qT, which uses multiple inheritance",
1342 current_class_type);
1343 return NULL_TREE;
1346 else if (TYPE_P (name))
1348 basetype = TYPE_MAIN_VARIANT (name);
1349 name = TYPE_NAME (name);
1351 else if (TREE_CODE (name) == TYPE_DECL)
1352 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1353 else
1354 basetype = NULL_TREE;
1356 if (basetype)
1358 tree class_binfo;
1359 tree direct_binfo;
1360 tree virtual_binfo;
1361 int i;
1363 if (current_template_parms
1364 || same_type_p (basetype, current_class_type))
1365 return basetype;
1367 class_binfo = TYPE_BINFO (current_class_type);
1368 direct_binfo = NULL_TREE;
1369 virtual_binfo = NULL_TREE;
1371 /* Look for a direct base. */
1372 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1373 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1374 break;
1376 /* Look for a virtual base -- unless the direct base is itself
1377 virtual. */
1378 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1379 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1381 /* [class.base.init]
1383 If a mem-initializer-id is ambiguous because it designates
1384 both a direct non-virtual base class and an inherited virtual
1385 base class, the mem-initializer is ill-formed. */
1386 if (direct_binfo && virtual_binfo)
1388 error ("%qD is both a direct base and an indirect virtual base",
1389 basetype);
1390 return NULL_TREE;
1393 if (!direct_binfo && !virtual_binfo)
1395 if (CLASSTYPE_VBASECLASSES (current_class_type))
1396 error ("type %qT is not a direct or virtual base of %qT",
1397 basetype, current_class_type);
1398 else
1399 error ("type %qT is not a direct base of %qT",
1400 basetype, current_class_type);
1401 return NULL_TREE;
1404 return direct_binfo ? direct_binfo : virtual_binfo;
1406 else
1408 if (identifier_p (name))
1409 field = lookup_field (current_class_type, name, 1, false);
1410 else
1411 field = name;
1413 if (member_init_ok_or_else (field, current_class_type, name))
1414 return field;
1417 return NULL_TREE;
1420 /* This is like `expand_member_init', only it stores one aggregate
1421 value into another.
1423 INIT comes in two flavors: it is either a value which
1424 is to be stored in EXP, or it is a parameter list
1425 to go to a constructor, which will operate on EXP.
1426 If INIT is not a parameter list for a constructor, then set
1427 LOOKUP_ONLYCONVERTING.
1428 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1429 the initializer, if FLAGS is 0, then it is the (init) form.
1430 If `init' is a CONSTRUCTOR, then we emit a warning message,
1431 explaining that such initializations are invalid.
1433 If INIT resolves to a CALL_EXPR which happens to return
1434 something of the type we are looking for, then we know
1435 that we can safely use that call to perform the
1436 initialization.
1438 The virtual function table pointer cannot be set up here, because
1439 we do not really know its type.
1441 This never calls operator=().
1443 When initializing, nothing is CONST.
1445 A default copy constructor may have to be used to perform the
1446 initialization.
1448 A constructor or a conversion operator may have to be used to
1449 perform the initialization, but not both, as it would be ambiguous. */
1451 tree
1452 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1454 tree stmt_expr;
1455 tree compound_stmt;
1456 int destroy_temps;
1457 tree type = TREE_TYPE (exp);
1458 int was_const = TREE_READONLY (exp);
1459 int was_volatile = TREE_THIS_VOLATILE (exp);
1460 int is_global;
1462 if (init == error_mark_node)
1463 return error_mark_node;
1465 TREE_READONLY (exp) = 0;
1466 TREE_THIS_VOLATILE (exp) = 0;
1468 if (init && TREE_CODE (init) != TREE_LIST
1469 && !(TREE_CODE (init) == TARGET_EXPR
1470 && TARGET_EXPR_DIRECT_INIT_P (init))
1471 && !(BRACE_ENCLOSED_INITIALIZER_P (init)
1472 && CONSTRUCTOR_IS_DIRECT_INIT (init)))
1473 flags |= LOOKUP_ONLYCONVERTING;
1475 if (TREE_CODE (type) == ARRAY_TYPE)
1477 tree itype;
1479 /* An array may not be initialized use the parenthesized
1480 initialization form -- unless the initializer is "()". */
1481 if (init && TREE_CODE (init) == TREE_LIST)
1483 if (complain & tf_error)
1484 error ("bad array initializer");
1485 return error_mark_node;
1487 /* Must arrange to initialize each element of EXP
1488 from elements of INIT. */
1489 itype = init ? TREE_TYPE (init) : NULL_TREE;
1490 if (cv_qualified_p (type))
1491 TREE_TYPE (exp) = cv_unqualified (type);
1492 if (itype && cv_qualified_p (itype))
1493 TREE_TYPE (init) = cv_unqualified (itype);
1494 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1495 /*explicit_value_init_p=*/false,
1496 itype && same_type_p (TREE_TYPE (init),
1497 TREE_TYPE (exp)),
1498 complain);
1499 TREE_READONLY (exp) = was_const;
1500 TREE_THIS_VOLATILE (exp) = was_volatile;
1501 TREE_TYPE (exp) = type;
1502 if (init)
1503 TREE_TYPE (init) = itype;
1504 return stmt_expr;
1507 if (VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
1508 /* Just know that we've seen something for this node. */
1509 TREE_USED (exp) = 1;
1511 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1512 destroy_temps = stmts_are_full_exprs_p ();
1513 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1514 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1515 init, LOOKUP_NORMAL|flags, complain);
1516 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1517 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1518 TREE_READONLY (exp) = was_const;
1519 TREE_THIS_VOLATILE (exp) = was_volatile;
1521 return stmt_expr;
1524 static void
1525 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
1526 tsubst_flags_t complain)
1528 tree type = TREE_TYPE (exp);
1529 tree ctor_name;
1531 /* It fails because there may not be a constructor which takes
1532 its own type as the first (or only parameter), but which does
1533 take other types via a conversion. So, if the thing initializing
1534 the expression is a unit element of type X, first try X(X&),
1535 followed by initialization by X. If neither of these work
1536 out, then look hard. */
1537 tree rval;
1538 vec<tree, va_gc> *parms;
1540 /* If we have direct-initialization from an initializer list, pull
1541 it out of the TREE_LIST so the code below can see it. */
1542 if (init && TREE_CODE (init) == TREE_LIST
1543 && BRACE_ENCLOSED_INITIALIZER_P (TREE_VALUE (init))
1544 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
1546 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
1547 && TREE_CHAIN (init) == NULL_TREE);
1548 init = TREE_VALUE (init);
1551 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
1552 && CP_AGGREGATE_TYPE_P (type))
1553 /* A brace-enclosed initializer for an aggregate. In C++0x this can
1554 happen for direct-initialization, too. */
1555 init = digest_init (type, init, complain);
1557 /* A CONSTRUCTOR of the target's type is a previously digested
1558 initializer, whether that happened just above or in
1559 cp_parser_late_parsing_nsdmi.
1561 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
1562 set represents the whole initialization, so we shouldn't build up
1563 another ctor call. */
1564 if (init
1565 && (TREE_CODE (init) == CONSTRUCTOR
1566 || (TREE_CODE (init) == TARGET_EXPR
1567 && (TARGET_EXPR_DIRECT_INIT_P (init)
1568 || TARGET_EXPR_LIST_INIT_P (init))))
1569 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1571 /* Early initialization via a TARGET_EXPR only works for
1572 complete objects. */
1573 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
1575 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1576 TREE_SIDE_EFFECTS (init) = 1;
1577 finish_expr_stmt (init);
1578 return;
1581 if (init && TREE_CODE (init) != TREE_LIST
1582 && (flags & LOOKUP_ONLYCONVERTING))
1584 /* Base subobjects should only get direct-initialization. */
1585 gcc_assert (true_exp == exp);
1587 if (flags & DIRECT_BIND)
1588 /* Do nothing. We hit this in two cases: Reference initialization,
1589 where we aren't initializing a real variable, so we don't want
1590 to run a new constructor; and catching an exception, where we
1591 have already built up the constructor call so we could wrap it
1592 in an exception region. */;
1593 else
1594 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
1595 flags, complain);
1597 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1598 /* We need to protect the initialization of a catch parm with a
1599 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1600 around the TARGET_EXPR for the copy constructor. See
1601 initialize_handler_parm. */
1603 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1604 TREE_OPERAND (init, 0));
1605 TREE_TYPE (init) = void_type_node;
1607 else
1608 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1609 TREE_SIDE_EFFECTS (init) = 1;
1610 finish_expr_stmt (init);
1611 return;
1614 if (init == NULL_TREE)
1615 parms = NULL;
1616 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
1618 parms = make_tree_vector ();
1619 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1620 vec_safe_push (parms, TREE_VALUE (init));
1622 else
1623 parms = make_tree_vector_single (init);
1625 if (exp == current_class_ref && current_function_decl
1626 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
1628 /* Delegating constructor. */
1629 tree complete;
1630 tree base;
1631 tree elt; unsigned i;
1633 /* Unshare the arguments for the second call. */
1634 vec<tree, va_gc> *parms2 = make_tree_vector ();
1635 FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
1637 elt = break_out_target_exprs (elt);
1638 vec_safe_push (parms2, elt);
1640 complete = build_special_member_call (exp, complete_ctor_identifier,
1641 &parms2, binfo, flags,
1642 complain);
1643 complete = fold_build_cleanup_point_expr (void_type_node, complete);
1644 release_tree_vector (parms2);
1646 base = build_special_member_call (exp, base_ctor_identifier,
1647 &parms, binfo, flags,
1648 complain);
1649 base = fold_build_cleanup_point_expr (void_type_node, base);
1650 rval = build3 (COND_EXPR, void_type_node,
1651 build2 (EQ_EXPR, boolean_type_node,
1652 current_in_charge_parm, integer_zero_node),
1653 base,
1654 complete);
1656 else
1658 if (true_exp == exp)
1659 ctor_name = complete_ctor_identifier;
1660 else
1661 ctor_name = base_ctor_identifier;
1662 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
1663 complain);
1666 if (parms != NULL)
1667 release_tree_vector (parms);
1669 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
1671 tree fn = get_callee_fndecl (rval);
1672 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
1674 tree e = maybe_constant_init (rval);
1675 if (TREE_CONSTANT (e))
1676 rval = build2 (INIT_EXPR, type, exp, e);
1680 /* FIXME put back convert_to_void? */
1681 if (TREE_SIDE_EFFECTS (rval))
1682 finish_expr_stmt (rval);
1685 /* This function is responsible for initializing EXP with INIT
1686 (if any).
1688 BINFO is the binfo of the type for who we are performing the
1689 initialization. For example, if W is a virtual base class of A and B,
1690 and C : A, B.
1691 If we are initializing B, then W must contain B's W vtable, whereas
1692 were we initializing C, W must contain C's W vtable.
1694 TRUE_EXP is nonzero if it is the true expression being initialized.
1695 In this case, it may be EXP, or may just contain EXP. The reason we
1696 need this is because if EXP is a base element of TRUE_EXP, we
1697 don't necessarily know by looking at EXP where its virtual
1698 baseclass fields should really be pointing. But we do know
1699 from TRUE_EXP. In constructors, we don't know anything about
1700 the value being initialized.
1702 FLAGS is just passed to `build_new_method_call'. See that function
1703 for its description. */
1705 static void
1706 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
1707 tsubst_flags_t complain)
1709 tree type = TREE_TYPE (exp);
1711 gcc_assert (init != error_mark_node && type != error_mark_node);
1712 gcc_assert (building_stmt_list_p ());
1714 /* Use a function returning the desired type to initialize EXP for us.
1715 If the function is a constructor, and its first argument is
1716 NULL_TREE, know that it was meant for us--just slide exp on
1717 in and expand the constructor. Constructors now come
1718 as TARGET_EXPRs. */
1720 if (init && VAR_P (exp)
1721 && COMPOUND_LITERAL_P (init))
1723 vec<tree, va_gc> *cleanups = NULL;
1724 /* If store_init_value returns NULL_TREE, the INIT has been
1725 recorded as the DECL_INITIAL for EXP. That means there's
1726 nothing more we have to do. */
1727 init = store_init_value (exp, init, &cleanups, flags);
1728 if (init)
1729 finish_expr_stmt (init);
1730 gcc_assert (!cleanups);
1731 return;
1734 /* If an explicit -- but empty -- initializer list was present,
1735 that's value-initialization. */
1736 if (init == void_type_node)
1738 /* If the type has data but no user-provided ctor, we need to zero
1739 out the object. */
1740 if (!type_has_user_provided_constructor (type)
1741 && !is_really_empty_class (type))
1743 tree field_size = NULL_TREE;
1744 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
1745 /* Don't clobber already initialized virtual bases. */
1746 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
1747 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
1748 field_size);
1749 init = build2 (INIT_EXPR, type, exp, init);
1750 finish_expr_stmt (init);
1753 /* If we don't need to mess with the constructor at all,
1754 then we're done. */
1755 if (! type_build_ctor_call (type))
1756 return;
1758 /* Otherwise fall through and call the constructor. */
1759 init = NULL_TREE;
1762 /* We know that expand_default_init can handle everything we want
1763 at this point. */
1764 expand_default_init (binfo, true_exp, exp, init, flags, complain);
1767 /* Report an error if TYPE is not a user-defined, class type. If
1768 OR_ELSE is nonzero, give an error message. */
1771 is_class_type (tree type, int or_else)
1773 if (type == error_mark_node)
1774 return 0;
1776 if (! CLASS_TYPE_P (type))
1778 if (or_else)
1779 error ("%qT is not a class type", type);
1780 return 0;
1782 return 1;
1785 tree
1786 get_type_value (tree name)
1788 if (name == error_mark_node)
1789 return NULL_TREE;
1791 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1792 return IDENTIFIER_TYPE_VALUE (name);
1793 else
1794 return NULL_TREE;
1797 /* Build a reference to a member of an aggregate. This is not a C++
1798 `&', but really something which can have its address taken, and
1799 then act as a pointer to member, for example TYPE :: FIELD can have
1800 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
1801 this expression is the operand of "&".
1803 @@ Prints out lousy diagnostics for operator <typename>
1804 @@ fields.
1806 @@ This function should be rewritten and placed in search.c. */
1808 tree
1809 build_offset_ref (tree type, tree member, bool address_p,
1810 tsubst_flags_t complain)
1812 tree decl;
1813 tree basebinfo = NULL_TREE;
1815 /* class templates can come in as TEMPLATE_DECLs here. */
1816 if (TREE_CODE (member) == TEMPLATE_DECL)
1817 return member;
1819 if (dependent_scope_p (type) || type_dependent_expression_p (member))
1820 return build_qualified_name (NULL_TREE, type, member,
1821 /*template_p=*/false);
1823 gcc_assert (TYPE_P (type));
1824 if (! is_class_type (type, 1))
1825 return error_mark_node;
1827 gcc_assert (DECL_P (member) || BASELINK_P (member));
1828 /* Callers should call mark_used before this point. */
1829 gcc_assert (!DECL_P (member) || TREE_USED (member));
1831 type = TYPE_MAIN_VARIANT (type);
1832 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
1834 if (complain & tf_error)
1835 error ("incomplete type %qT does not have member %qD", type, member);
1836 return error_mark_node;
1839 /* Entities other than non-static members need no further
1840 processing. */
1841 if (TREE_CODE (member) == TYPE_DECL)
1842 return member;
1843 if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
1844 return convert_from_reference (member);
1846 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1848 if (complain & tf_error)
1849 error ("invalid pointer to bit-field %qD", member);
1850 return error_mark_node;
1853 /* Set up BASEBINFO for member lookup. */
1854 decl = maybe_dummy_object (type, &basebinfo);
1856 /* A lot of this logic is now handled in lookup_member. */
1857 if (BASELINK_P (member))
1859 /* Go from the TREE_BASELINK to the member function info. */
1860 tree t = BASELINK_FUNCTIONS (member);
1862 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1864 /* Get rid of a potential OVERLOAD around it. */
1865 t = OVL_CURRENT (t);
1867 /* Unique functions are handled easily. */
1869 /* For non-static member of base class, we need a special rule
1870 for access checking [class.protected]:
1872 If the access is to form a pointer to member, the
1873 nested-name-specifier shall name the derived class
1874 (or any class derived from that class). */
1875 if (address_p && DECL_P (t)
1876 && DECL_NONSTATIC_MEMBER_P (t))
1877 perform_or_defer_access_check (TYPE_BINFO (type), t, t,
1878 complain);
1879 else
1880 perform_or_defer_access_check (basebinfo, t, t,
1881 complain);
1883 if (DECL_STATIC_FUNCTION_P (t))
1884 return t;
1885 member = t;
1887 else
1888 TREE_TYPE (member) = unknown_type_node;
1890 else if (address_p && TREE_CODE (member) == FIELD_DECL)
1891 /* We need additional test besides the one in
1892 check_accessibility_of_qualified_id in case it is
1893 a pointer to non-static member. */
1894 perform_or_defer_access_check (TYPE_BINFO (type), member, member,
1895 complain);
1897 if (!address_p)
1899 /* If MEMBER is non-static, then the program has fallen afoul of
1900 [expr.prim]:
1902 An id-expression that denotes a nonstatic data member or
1903 nonstatic member function of a class can only be used:
1905 -- as part of a class member access (_expr.ref_) in which the
1906 object-expression refers to the member's class or a class
1907 derived from that class, or
1909 -- to form a pointer to member (_expr.unary.op_), or
1911 -- in the body of a nonstatic member function of that class or
1912 of a class derived from that class (_class.mfct.nonstatic_), or
1914 -- in a mem-initializer for a constructor for that class or for
1915 a class derived from that class (_class.base.init_). */
1916 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1918 /* Build a representation of the qualified name suitable
1919 for use as the operand to "&" -- even though the "&" is
1920 not actually present. */
1921 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1922 /* In Microsoft mode, treat a non-static member function as if
1923 it were a pointer-to-member. */
1924 if (flag_ms_extensions)
1926 PTRMEM_OK_P (member) = 1;
1927 return cp_build_addr_expr (member, complain);
1929 if (complain & tf_error)
1930 error ("invalid use of non-static member function %qD",
1931 TREE_OPERAND (member, 1));
1932 return error_mark_node;
1934 else if (TREE_CODE (member) == FIELD_DECL)
1936 if (complain & tf_error)
1937 error ("invalid use of non-static data member %qD", member);
1938 return error_mark_node;
1940 return member;
1943 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1944 PTRMEM_OK_P (member) = 1;
1945 return member;
1948 /* If DECL is a scalar enumeration constant or variable with a
1949 constant initializer, return the initializer (or, its initializers,
1950 recursively); otherwise, return DECL. If INTEGRAL_P, the
1951 initializer is only returned if DECL is an integral
1952 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
1953 return an aggregate constant. */
1955 static tree
1956 constant_value_1 (tree decl, bool integral_p, bool return_aggregate_cst_ok_p)
1958 while (TREE_CODE (decl) == CONST_DECL
1959 || (integral_p
1960 ? decl_constant_var_p (decl)
1961 : (VAR_P (decl)
1962 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
1964 tree init;
1965 /* If DECL is a static data member in a template
1966 specialization, we must instantiate it here. The
1967 initializer for the static data member is not processed
1968 until needed; we need it now. */
1969 mark_used (decl);
1970 mark_rvalue_use (decl);
1971 init = DECL_INITIAL (decl);
1972 if (init == error_mark_node)
1974 if (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
1975 /* Treat the error as a constant to avoid cascading errors on
1976 excessively recursive template instantiation (c++/9335). */
1977 return init;
1978 else
1979 return decl;
1981 /* Initializers in templates are generally expanded during
1982 instantiation, so before that for const int i(2)
1983 INIT is a TREE_LIST with the actual initializer as
1984 TREE_VALUE. */
1985 if (processing_template_decl
1986 && init
1987 && TREE_CODE (init) == TREE_LIST
1988 && TREE_CHAIN (init) == NULL_TREE)
1989 init = TREE_VALUE (init);
1990 if (!init
1991 || !TREE_TYPE (init)
1992 || !TREE_CONSTANT (init)
1993 || (!integral_p && !return_aggregate_cst_ok_p
1994 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
1995 return an aggregate constant (of which string
1996 literals are a special case), as we do not want
1997 to make inadvertent copies of such entities, and
1998 we must be sure that their addresses are the
1999 same everywhere. */
2000 && (TREE_CODE (init) == CONSTRUCTOR
2001 || TREE_CODE (init) == STRING_CST)))
2002 break;
2003 decl = unshare_expr (init);
2005 return decl;
2008 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by
2009 constant of integral or enumeration type, then return that value.
2010 These are those variables permitted in constant expressions by
2011 [5.19/1]. */
2013 tree
2014 integral_constant_value (tree decl)
2016 return constant_value_1 (decl, /*integral_p=*/true,
2017 /*return_aggregate_cst_ok_p=*/false);
2020 /* A more relaxed version of integral_constant_value, used by the
2021 common C/C++ code. */
2023 tree
2024 decl_constant_value (tree decl)
2026 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2027 /*return_aggregate_cst_ok_p=*/true);
2030 /* A version of integral_constant_value used by the C++ front end for
2031 optimization purposes. */
2033 tree
2034 decl_constant_value_safe (tree decl)
2036 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2037 /*return_aggregate_cst_ok_p=*/false);
2040 /* Common subroutines of build_new and build_vec_delete. */
2042 /* Call the global __builtin_delete to delete ADDR. */
2044 static tree
2045 build_builtin_delete_call (tree addr)
2047 mark_used (global_delete_fndecl);
2048 return build_call_n (global_delete_fndecl, 1, addr);
2051 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2052 the type of the object being allocated; otherwise, it's just TYPE.
2053 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2054 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2055 a vector of arguments to be provided as arguments to a placement
2056 new operator. This routine performs no semantic checks; it just
2057 creates and returns a NEW_EXPR. */
2059 static tree
2060 build_raw_new_expr (vec<tree, va_gc> *placement, tree type, tree nelts,
2061 vec<tree, va_gc> *init, int use_global_new)
2063 tree init_list;
2064 tree new_expr;
2066 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2067 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2068 permits us to distinguish the case of a missing initializer "new
2069 int" from an empty initializer "new int()". */
2070 if (init == NULL)
2071 init_list = NULL_TREE;
2072 else if (init->is_empty ())
2073 init_list = void_zero_node;
2074 else
2075 init_list = build_tree_list_vec (init);
2077 new_expr = build4 (NEW_EXPR, build_pointer_type (type),
2078 build_tree_list_vec (placement), type, nelts,
2079 init_list);
2080 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2081 TREE_SIDE_EFFECTS (new_expr) = 1;
2083 return new_expr;
2086 /* Diagnose uninitialized const members or reference members of type
2087 TYPE. USING_NEW is used to disambiguate the diagnostic between a
2088 new expression without a new-initializer and a declaration. Returns
2089 the error count. */
2091 static int
2092 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2093 bool using_new, bool complain)
2095 tree field;
2096 int error_count = 0;
2098 if (type_has_user_provided_constructor (type))
2099 return 0;
2101 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2103 tree field_type;
2105 if (TREE_CODE (field) != FIELD_DECL)
2106 continue;
2108 field_type = strip_array_types (TREE_TYPE (field));
2110 if (type_has_user_provided_constructor (field_type))
2111 continue;
2113 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2115 ++ error_count;
2116 if (complain)
2118 if (using_new)
2119 error ("uninitialized reference member in %q#T "
2120 "using %<new%> without new-initializer", origin);
2121 else
2122 error ("uninitialized reference member in %q#T", origin);
2123 inform (DECL_SOURCE_LOCATION (field),
2124 "%qD should be initialized", field);
2128 if (CP_TYPE_CONST_P (field_type))
2130 ++ error_count;
2131 if (complain)
2133 if (using_new)
2134 error ("uninitialized const member in %q#T "
2135 "using %<new%> without new-initializer", origin);
2136 else
2137 error ("uninitialized const member in %q#T", origin);
2138 inform (DECL_SOURCE_LOCATION (field),
2139 "%qD should be initialized", field);
2143 if (CLASS_TYPE_P (field_type))
2144 error_count
2145 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2146 using_new, complain);
2148 return error_count;
2152 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2154 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2157 /* Call __cxa_bad_array_new_length to indicate that the size calculation
2158 overflowed. Pretend it returns sizetype so that it plays nicely in the
2159 COND_EXPR. */
2161 tree
2162 throw_bad_array_new_length (void)
2164 tree fn = get_identifier ("__cxa_throw_bad_array_new_length");
2165 if (!get_global_value_if_present (fn, &fn))
2166 fn = push_throw_library_fn (fn, build_function_type_list (sizetype,
2167 NULL_TREE));
2169 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2172 /* Call __cxa_bad_array_length to indicate that there were too many
2173 initializers. */
2175 tree
2176 throw_bad_array_length (void)
2178 tree fn = get_identifier ("__cxa_throw_bad_array_length");
2179 if (!get_global_value_if_present (fn, &fn))
2180 fn = push_throw_library_fn (fn, build_function_type_list (void_type_node,
2181 NULL_TREE));
2183 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2186 /* Generate code for a new-expression, including calling the "operator
2187 new" function, initializing the object, and, if an exception occurs
2188 during construction, cleaning up. The arguments are as for
2189 build_raw_new_expr. This may change PLACEMENT and INIT. */
2191 static tree
2192 build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
2193 vec<tree, va_gc> **init, bool globally_qualified_p,
2194 tsubst_flags_t complain)
2196 tree size, rval;
2197 /* True iff this is a call to "operator new[]" instead of just
2198 "operator new". */
2199 bool array_p = false;
2200 /* If ARRAY_P is true, the element type of the array. This is never
2201 an ARRAY_TYPE; for something like "new int[3][4]", the
2202 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
2203 TYPE. */
2204 tree elt_type;
2205 /* The type of the new-expression. (This type is always a pointer
2206 type.) */
2207 tree pointer_type;
2208 tree non_const_pointer_type;
2209 tree outer_nelts = NULL_TREE;
2210 /* For arrays, a bounds checks on the NELTS parameter. */
2211 tree outer_nelts_check = NULL_TREE;
2212 bool outer_nelts_from_type = false;
2213 double_int inner_nelts_count = double_int_one;
2214 tree alloc_call, alloc_expr;
2215 /* Size of the inner array elements. */
2216 double_int inner_size;
2217 /* The address returned by the call to "operator new". This node is
2218 a VAR_DECL and is therefore reusable. */
2219 tree alloc_node;
2220 tree alloc_fn;
2221 tree cookie_expr, init_expr;
2222 int nothrow, check_new;
2223 int use_java_new = 0;
2224 /* If non-NULL, the number of extra bytes to allocate at the
2225 beginning of the storage allocated for an array-new expression in
2226 order to store the number of elements. */
2227 tree cookie_size = NULL_TREE;
2228 tree placement_first;
2229 tree placement_expr = NULL_TREE;
2230 /* True if the function we are calling is a placement allocation
2231 function. */
2232 bool placement_allocation_fn_p;
2233 /* True if the storage must be initialized, either by a constructor
2234 or due to an explicit new-initializer. */
2235 bool is_initialized;
2236 /* The address of the thing allocated, not including any cookie. In
2237 particular, if an array cookie is in use, DATA_ADDR is the
2238 address of the first array element. This node is a VAR_DECL, and
2239 is therefore reusable. */
2240 tree data_addr;
2241 tree init_preeval_expr = NULL_TREE;
2243 if (nelts)
2245 outer_nelts = nelts;
2246 array_p = true;
2248 else if (TREE_CODE (type) == ARRAY_TYPE)
2250 /* Transforms new (T[N]) to new T[N]. The former is a GNU
2251 extension for variable N. (This also covers new T where T is
2252 a VLA typedef.) */
2253 array_p = true;
2254 nelts = array_type_nelts_top (type);
2255 outer_nelts = nelts;
2256 type = TREE_TYPE (type);
2257 outer_nelts_from_type = true;
2260 /* If our base type is an array, then make sure we know how many elements
2261 it has. */
2262 for (elt_type = type;
2263 TREE_CODE (elt_type) == ARRAY_TYPE;
2264 elt_type = TREE_TYPE (elt_type))
2266 tree inner_nelts = array_type_nelts_top (elt_type);
2267 tree inner_nelts_cst = maybe_constant_value (inner_nelts);
2268 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
2270 bool overflow;
2271 double_int result = TREE_INT_CST (inner_nelts_cst)
2272 .mul_with_sign (inner_nelts_count,
2273 false, &overflow);
2274 if (overflow)
2276 if (complain & tf_error)
2277 error ("integer overflow in array size");
2278 nelts = error_mark_node;
2280 inner_nelts_count = result;
2282 else
2284 if (complain & tf_error)
2286 error_at (EXPR_LOC_OR_HERE (inner_nelts),
2287 "array size in operator new must be constant");
2288 cxx_constant_value(inner_nelts);
2290 nelts = error_mark_node;
2292 if (nelts != error_mark_node)
2293 nelts = cp_build_binary_op (input_location,
2294 MULT_EXPR, nelts,
2295 inner_nelts_cst,
2296 complain);
2299 if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
2301 error ("variably modified type not allowed in operator new");
2302 return error_mark_node;
2305 if (nelts == error_mark_node)
2306 return error_mark_node;
2308 /* Warn if we performed the (T[N]) to T[N] transformation and N is
2309 variable. */
2310 if (outer_nelts_from_type
2311 && !TREE_CONSTANT (maybe_constant_value (outer_nelts)))
2313 if (complain & tf_warning_or_error)
2314 pedwarn(EXPR_LOC_OR_HERE (outer_nelts), OPT_Wvla,
2315 "ISO C++ does not support variable-length array types");
2316 else
2317 return error_mark_node;
2320 if (VOID_TYPE_P (elt_type))
2322 if (complain & tf_error)
2323 error ("invalid type %<void%> for new");
2324 return error_mark_node;
2327 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain))
2328 return error_mark_node;
2330 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
2332 if (*init == NULL)
2334 bool maybe_uninitialized_error = false;
2335 /* A program that calls for default-initialization [...] of an
2336 entity of reference type is ill-formed. */
2337 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
2338 maybe_uninitialized_error = true;
2340 /* A new-expression that creates an object of type T initializes
2341 that object as follows:
2342 - If the new-initializer is omitted:
2343 -- If T is a (possibly cv-qualified) non-POD class type
2344 (or array thereof), the object is default-initialized (8.5).
2345 [...]
2346 -- Otherwise, the object created has indeterminate
2347 value. If T is a const-qualified type, or a (possibly
2348 cv-qualified) POD class type (or array thereof)
2349 containing (directly or indirectly) a member of
2350 const-qualified type, the program is ill-formed; */
2352 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
2353 maybe_uninitialized_error = true;
2355 if (maybe_uninitialized_error
2356 && diagnose_uninitialized_cst_or_ref_member (elt_type,
2357 /*using_new=*/true,
2358 complain & tf_error))
2359 return error_mark_node;
2362 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
2363 && default_init_uninitialized_part (elt_type))
2365 if (complain & tf_error)
2366 error ("uninitialized const in %<new%> of %q#T", elt_type);
2367 return error_mark_node;
2370 size = size_in_bytes (elt_type);
2371 if (array_p)
2373 /* Maximum available size in bytes. Half of the address space
2374 minus the cookie size. */
2375 double_int max_size
2376 = double_int_one.llshift (TYPE_PRECISION (sizetype) - 1,
2377 HOST_BITS_PER_DOUBLE_INT);
2378 /* Maximum number of outer elements which can be allocated. */
2379 double_int max_outer_nelts;
2380 tree max_outer_nelts_tree;
2382 gcc_assert (TREE_CODE (size) == INTEGER_CST);
2383 cookie_size = targetm.cxx.get_cookie_size (elt_type);
2384 gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
2385 gcc_checking_assert (TREE_INT_CST (cookie_size).ult (max_size));
2386 /* Unconditionally substract the cookie size. This decreases the
2387 maximum object size and is safe even if we choose not to use
2388 a cookie after all. */
2389 max_size -= TREE_INT_CST (cookie_size);
2390 bool overflow;
2391 inner_size = TREE_INT_CST (size)
2392 .mul_with_sign (inner_nelts_count, false, &overflow);
2393 if (overflow || inner_size.ugt (max_size))
2395 if (complain & tf_error)
2396 error ("size of array is too large");
2397 return error_mark_node;
2399 max_outer_nelts = max_size.udiv (inner_size, TRUNC_DIV_EXPR);
2400 /* Only keep the top-most seven bits, to simplify encoding the
2401 constant in the instruction stream. */
2403 unsigned shift = HOST_BITS_PER_DOUBLE_INT - 7
2404 - (max_outer_nelts.high ? clz_hwi (max_outer_nelts.high)
2405 : (HOST_BITS_PER_WIDE_INT + clz_hwi (max_outer_nelts.low)));
2406 max_outer_nelts
2407 = max_outer_nelts.lrshift (shift, HOST_BITS_PER_DOUBLE_INT)
2408 .llshift (shift, HOST_BITS_PER_DOUBLE_INT);
2410 max_outer_nelts_tree = double_int_to_tree (sizetype, max_outer_nelts);
2412 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2413 outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
2414 outer_nelts,
2415 max_outer_nelts_tree);
2418 alloc_fn = NULL_TREE;
2420 /* If PLACEMENT is a single simple pointer type not passed by
2421 reference, prepare to capture it in a temporary variable. Do
2422 this now, since PLACEMENT will change in the calls below. */
2423 placement_first = NULL_TREE;
2424 if (vec_safe_length (*placement) == 1
2425 && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
2426 placement_first = (**placement)[0];
2428 /* Allocate the object. */
2429 if (vec_safe_is_empty (*placement) && TYPE_FOR_JAVA (elt_type))
2431 tree class_addr;
2432 tree class_decl = build_java_class_ref (elt_type);
2433 static const char alloc_name[] = "_Jv_AllocObject";
2435 if (class_decl == error_mark_node)
2436 return error_mark_node;
2438 use_java_new = 1;
2439 if (!get_global_value_if_present (get_identifier (alloc_name),
2440 &alloc_fn))
2442 if (complain & tf_error)
2443 error ("call to Java constructor with %qs undefined", alloc_name);
2444 return error_mark_node;
2446 else if (really_overloaded_fn (alloc_fn))
2448 if (complain & tf_error)
2449 error ("%qD should never be overloaded", alloc_fn);
2450 return error_mark_node;
2452 alloc_fn = OVL_CURRENT (alloc_fn);
2453 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2454 alloc_call = cp_build_function_call_nary (alloc_fn, complain,
2455 class_addr, NULL_TREE);
2457 else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
2459 error ("Java class %q#T object allocated using placement new", elt_type);
2460 return error_mark_node;
2462 else
2464 tree fnname;
2465 tree fns;
2467 fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
2469 if (!globally_qualified_p
2470 && CLASS_TYPE_P (elt_type)
2471 && (array_p
2472 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
2473 : TYPE_HAS_NEW_OPERATOR (elt_type)))
2475 /* Use a class-specific operator new. */
2476 /* If a cookie is required, add some extra space. */
2477 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
2478 size = size_binop (PLUS_EXPR, size, cookie_size);
2479 else
2481 cookie_size = NULL_TREE;
2482 /* No size arithmetic necessary, so the size check is
2483 not needed. */
2484 if (outer_nelts_check != NULL && inner_size.is_one ())
2485 outer_nelts_check = NULL_TREE;
2487 /* Perform the overflow check. */
2488 tree errval = TYPE_MAX_VALUE (sizetype);
2489 if (cxx_dialect >= cxx11)
2490 errval = throw_bad_array_new_length ();
2491 if (outer_nelts_check != NULL_TREE)
2492 size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
2493 size, errval);
2494 /* Create the argument list. */
2495 vec_safe_insert (*placement, 0, size);
2496 /* Do name-lookup to find the appropriate operator. */
2497 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
2498 if (fns == NULL_TREE)
2500 if (complain & tf_error)
2501 error ("no suitable %qD found in class %qT", fnname, elt_type);
2502 return error_mark_node;
2504 if (TREE_CODE (fns) == TREE_LIST)
2506 if (complain & tf_error)
2508 error ("request for member %qD is ambiguous", fnname);
2509 print_candidates (fns);
2511 return error_mark_node;
2513 alloc_call = build_new_method_call (build_dummy_object (elt_type),
2514 fns, placement,
2515 /*conversion_path=*/NULL_TREE,
2516 LOOKUP_NORMAL,
2517 &alloc_fn,
2518 complain);
2520 else
2522 /* Use a global operator new. */
2523 /* See if a cookie might be required. */
2524 if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
2526 cookie_size = NULL_TREE;
2527 /* No size arithmetic necessary, so the size check is
2528 not needed. */
2529 if (outer_nelts_check != NULL && inner_size.is_one ())
2530 outer_nelts_check = NULL_TREE;
2533 alloc_call = build_operator_new_call (fnname, placement,
2534 &size, &cookie_size,
2535 outer_nelts_check,
2536 &alloc_fn, complain);
2540 if (alloc_call == error_mark_node)
2541 return error_mark_node;
2543 gcc_assert (alloc_fn != NULL_TREE);
2545 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
2546 into a temporary variable. */
2547 if (!processing_template_decl
2548 && placement_first != NULL_TREE
2549 && TREE_CODE (alloc_call) == CALL_EXPR
2550 && call_expr_nargs (alloc_call) == 2
2551 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
2552 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
2554 tree placement_arg = CALL_EXPR_ARG (alloc_call, 1);
2556 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))
2557 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))))
2559 placement_expr = get_target_expr (placement_first);
2560 CALL_EXPR_ARG (alloc_call, 1)
2561 = convert (TREE_TYPE (placement_arg), placement_expr);
2565 /* In the simple case, we can stop now. */
2566 pointer_type = build_pointer_type (type);
2567 if (!cookie_size && !is_initialized)
2568 return build_nop (pointer_type, alloc_call);
2570 /* Store the result of the allocation call in a variable so that we can
2571 use it more than once. */
2572 alloc_expr = get_target_expr (alloc_call);
2573 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2575 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
2576 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
2577 alloc_call = TREE_OPERAND (alloc_call, 1);
2579 /* Now, check to see if this function is actually a placement
2580 allocation function. This can happen even when PLACEMENT is NULL
2581 because we might have something like:
2583 struct S { void* operator new (size_t, int i = 0); };
2585 A call to `new S' will get this allocation function, even though
2586 there is no explicit placement argument. If there is more than
2587 one argument, or there are variable arguments, then this is a
2588 placement allocation function. */
2589 placement_allocation_fn_p
2590 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2591 || varargs_function_p (alloc_fn));
2593 /* Preevaluate the placement args so that we don't reevaluate them for a
2594 placement delete. */
2595 if (placement_allocation_fn_p)
2597 tree inits;
2598 stabilize_call (alloc_call, &inits);
2599 if (inits)
2600 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
2601 alloc_expr);
2604 /* unless an allocation function is declared with an empty excep-
2605 tion-specification (_except.spec_), throw(), it indicates failure to
2606 allocate storage by throwing a bad_alloc exception (clause _except_,
2607 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2608 cation function is declared with an empty exception-specification,
2609 throw(), it returns null to indicate failure to allocate storage and a
2610 non-null pointer otherwise.
2612 So check for a null exception spec on the op new we just called. */
2614 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2615 check_new = (flag_check_new || nothrow) && ! use_java_new;
2617 if (cookie_size)
2619 tree cookie;
2620 tree cookie_ptr;
2621 tree size_ptr_type;
2623 /* Adjust so we're pointing to the start of the object. */
2624 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
2626 /* Store the number of bytes allocated so that we can know how
2627 many elements to destroy later. We use the last sizeof
2628 (size_t) bytes to store the number of elements. */
2629 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
2630 cookie_ptr = fold_build_pointer_plus_loc (input_location,
2631 alloc_node, cookie_ptr);
2632 size_ptr_type = build_pointer_type (sizetype);
2633 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
2634 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2636 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
2638 if (targetm.cxx.cookie_has_size ())
2640 /* Also store the element size. */
2641 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
2642 fold_build1_loc (input_location,
2643 NEGATE_EXPR, sizetype,
2644 size_in_bytes (sizetype)));
2646 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2647 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
2648 size_in_bytes (elt_type));
2649 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
2650 cookie, cookie_expr);
2653 else
2655 cookie_expr = NULL_TREE;
2656 data_addr = alloc_node;
2659 /* Now use a pointer to the type we've actually allocated. */
2661 /* But we want to operate on a non-const version to start with,
2662 since we'll be modifying the elements. */
2663 non_const_pointer_type = build_pointer_type
2664 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
2666 data_addr = fold_convert (non_const_pointer_type, data_addr);
2667 /* Any further uses of alloc_node will want this type, too. */
2668 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
2670 /* Now initialize the allocated object. Note that we preevaluate the
2671 initialization expression, apart from the actual constructor call or
2672 assignment--we do this because we want to delay the allocation as long
2673 as possible in order to minimize the size of the exception region for
2674 placement delete. */
2675 if (is_initialized)
2677 bool stable;
2678 bool explicit_value_init_p = false;
2680 if (*init != NULL && (*init)->is_empty ())
2682 *init = NULL;
2683 explicit_value_init_p = true;
2686 if (processing_template_decl && explicit_value_init_p)
2688 /* build_value_init doesn't work in templates, and we don't need
2689 the initializer anyway since we're going to throw it away and
2690 rebuild it at instantiation time, so just build up a single
2691 constructor call to get any appropriate diagnostics. */
2692 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2693 if (type_build_ctor_call (elt_type))
2694 init_expr = build_special_member_call (init_expr,
2695 complete_ctor_identifier,
2696 init, elt_type,
2697 LOOKUP_NORMAL,
2698 complain);
2699 stable = stabilize_init (init_expr, &init_preeval_expr);
2701 else if (array_p)
2703 tree vecinit = NULL_TREE;
2704 if (vec_safe_length (*init) == 1
2705 && BRACE_ENCLOSED_INITIALIZER_P ((**init)[0])
2706 && CONSTRUCTOR_IS_DIRECT_INIT ((**init)[0]))
2708 vecinit = (**init)[0];
2709 if (CONSTRUCTOR_NELTS (vecinit) == 0)
2710 /* List-value-initialization, leave it alone. */;
2711 else
2713 tree arraytype, domain;
2714 if (TREE_CONSTANT (nelts))
2715 domain = compute_array_index_type (NULL_TREE, nelts,
2716 complain);
2717 else
2718 /* We'll check the length at runtime. */
2719 domain = NULL_TREE;
2720 arraytype = build_cplus_array_type (type, domain);
2721 vecinit = digest_init (arraytype, vecinit, complain);
2724 else if (*init)
2726 if (complain & tf_error)
2727 permerror (input_location,
2728 "parenthesized initializer in array new");
2729 else
2730 return error_mark_node;
2731 vecinit = build_tree_list_vec (*init);
2733 init_expr
2734 = build_vec_init (data_addr,
2735 cp_build_binary_op (input_location,
2736 MINUS_EXPR, outer_nelts,
2737 integer_one_node,
2738 complain),
2739 vecinit,
2740 explicit_value_init_p,
2741 /*from_array=*/0,
2742 complain);
2744 /* An array initialization is stable because the initialization
2745 of each element is a full-expression, so the temporaries don't
2746 leak out. */
2747 stable = true;
2749 else
2751 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2753 if (type_build_ctor_call (type) && !explicit_value_init_p)
2755 init_expr = build_special_member_call (init_expr,
2756 complete_ctor_identifier,
2757 init, elt_type,
2758 LOOKUP_NORMAL,
2759 complain);
2761 else if (explicit_value_init_p)
2763 /* Something like `new int()'. */
2764 tree val = build_value_init (type, complain);
2765 if (val == error_mark_node)
2766 return error_mark_node;
2767 init_expr = build2 (INIT_EXPR, type, init_expr, val);
2769 else
2771 tree ie;
2773 /* We are processing something like `new int (10)', which
2774 means allocate an int, and initialize it with 10. */
2776 ie = build_x_compound_expr_from_vec (*init, "new initializer",
2777 complain);
2778 init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie,
2779 complain);
2781 stable = stabilize_init (init_expr, &init_preeval_expr);
2784 if (init_expr == error_mark_node)
2785 return error_mark_node;
2787 /* If any part of the object initialization terminates by throwing an
2788 exception and a suitable deallocation function can be found, the
2789 deallocation function is called to free the memory in which the
2790 object was being constructed, after which the exception continues
2791 to propagate in the context of the new-expression. If no
2792 unambiguous matching deallocation function can be found,
2793 propagating the exception does not cause the object's memory to be
2794 freed. */
2795 if (flag_exceptions && ! use_java_new)
2797 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
2798 tree cleanup;
2800 /* The Standard is unclear here, but the right thing to do
2801 is to use the same method for finding deallocation
2802 functions that we use for finding allocation functions. */
2803 cleanup = (build_op_delete_call
2804 (dcode,
2805 alloc_node,
2806 size,
2807 globally_qualified_p,
2808 placement_allocation_fn_p ? alloc_call : NULL_TREE,
2809 alloc_fn,
2810 complain));
2812 if (!cleanup)
2813 /* We're done. */;
2814 else if (stable)
2815 /* This is much simpler if we were able to preevaluate all of
2816 the arguments to the constructor call. */
2818 /* CLEANUP is compiler-generated, so no diagnostics. */
2819 TREE_NO_WARNING (cleanup) = true;
2820 init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
2821 init_expr, cleanup);
2822 /* Likewise, this try-catch is compiler-generated. */
2823 TREE_NO_WARNING (init_expr) = true;
2825 else
2826 /* Ack! First we allocate the memory. Then we set our sentry
2827 variable to true, and expand a cleanup that deletes the
2828 memory if sentry is true. Then we run the constructor, and
2829 finally clear the sentry.
2831 We need to do this because we allocate the space first, so
2832 if there are any temporaries with cleanups in the
2833 constructor args and we weren't able to preevaluate them, we
2834 need this EH region to extend until end of full-expression
2835 to preserve nesting. */
2837 tree end, sentry, begin;
2839 begin = get_target_expr (boolean_true_node);
2840 CLEANUP_EH_ONLY (begin) = 1;
2842 sentry = TARGET_EXPR_SLOT (begin);
2844 /* CLEANUP is compiler-generated, so no diagnostics. */
2845 TREE_NO_WARNING (cleanup) = true;
2847 TARGET_EXPR_CLEANUP (begin)
2848 = build3 (COND_EXPR, void_type_node, sentry,
2849 cleanup, void_zero_node);
2851 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2852 sentry, boolean_false_node);
2854 init_expr
2855 = build2 (COMPOUND_EXPR, void_type_node, begin,
2856 build2 (COMPOUND_EXPR, void_type_node, init_expr,
2857 end));
2858 /* Likewise, this is compiler-generated. */
2859 TREE_NO_WARNING (init_expr) = true;
2863 else
2864 init_expr = NULL_TREE;
2866 /* Now build up the return value in reverse order. */
2868 rval = data_addr;
2870 if (init_expr)
2871 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2872 if (cookie_expr)
2873 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2875 if (rval == data_addr)
2876 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2877 and return the call (which doesn't need to be adjusted). */
2878 rval = TARGET_EXPR_INITIAL (alloc_expr);
2879 else
2881 if (check_new)
2883 tree ifexp = cp_build_binary_op (input_location,
2884 NE_EXPR, alloc_node,
2885 nullptr_node,
2886 complain);
2887 rval = build_conditional_expr (input_location, ifexp, rval,
2888 alloc_node, complain);
2891 /* Perform the allocation before anything else, so that ALLOC_NODE
2892 has been initialized before we start using it. */
2893 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2896 if (init_preeval_expr)
2897 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
2899 /* A new-expression is never an lvalue. */
2900 gcc_assert (!lvalue_p (rval));
2902 return convert (pointer_type, rval);
2905 /* Generate a representation for a C++ "new" expression. *PLACEMENT
2906 is a vector of placement-new arguments (or NULL if none). If NELTS
2907 is NULL, TYPE is the type of the storage to be allocated. If NELTS
2908 is not NULL, then this is an array-new allocation; TYPE is the type
2909 of the elements in the array and NELTS is the number of elements in
2910 the array. *INIT, if non-NULL, is the initializer for the new
2911 object, or an empty vector to indicate an initializer of "()". If
2912 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
2913 rather than just "new". This may change PLACEMENT and INIT. */
2915 tree
2916 build_new (vec<tree, va_gc> **placement, tree type, tree nelts,
2917 vec<tree, va_gc> **init, int use_global_new, tsubst_flags_t complain)
2919 tree rval;
2920 vec<tree, va_gc> *orig_placement = NULL;
2921 tree orig_nelts = NULL_TREE;
2922 vec<tree, va_gc> *orig_init = NULL;
2924 if (type == error_mark_node)
2925 return error_mark_node;
2927 if (nelts == NULL_TREE && vec_safe_length (*init) == 1
2928 /* Don't do auto deduction where it might affect mangling. */
2929 && (!processing_template_decl || at_function_scope_p ()))
2931 tree auto_node = type_uses_auto (type);
2932 if (auto_node)
2934 tree d_init = (**init)[0];
2935 d_init = resolve_nondeduced_context (d_init);
2936 type = do_auto_deduction (type, d_init, auto_node);
2940 if (processing_template_decl)
2942 if (dependent_type_p (type)
2943 || any_type_dependent_arguments_p (*placement)
2944 || (nelts && type_dependent_expression_p (nelts))
2945 || (nelts && *init)
2946 || any_type_dependent_arguments_p (*init))
2947 return build_raw_new_expr (*placement, type, nelts, *init,
2948 use_global_new);
2950 orig_placement = make_tree_vector_copy (*placement);
2951 orig_nelts = nelts;
2952 if (*init)
2953 orig_init = make_tree_vector_copy (*init);
2955 make_args_non_dependent (*placement);
2956 if (nelts)
2957 nelts = build_non_dependent_expr (nelts);
2958 make_args_non_dependent (*init);
2961 if (nelts)
2963 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
2965 if (complain & tf_error)
2966 permerror (input_location, "size in array new must have integral type");
2967 else
2968 return error_mark_node;
2970 nelts = mark_rvalue_use (nelts);
2971 nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
2974 /* ``A reference cannot be created by the new operator. A reference
2975 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2976 returned by new.'' ARM 5.3.3 */
2977 if (TREE_CODE (type) == REFERENCE_TYPE)
2979 if (complain & tf_error)
2980 error ("new cannot be applied to a reference type");
2981 else
2982 return error_mark_node;
2983 type = TREE_TYPE (type);
2986 if (TREE_CODE (type) == FUNCTION_TYPE)
2988 if (complain & tf_error)
2989 error ("new cannot be applied to a function type");
2990 return error_mark_node;
2993 /* The type allocated must be complete. If the new-type-id was
2994 "T[N]" then we are just checking that "T" is complete here, but
2995 that is equivalent, since the value of "N" doesn't matter. */
2996 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2997 return error_mark_node;
2999 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
3000 if (rval == error_mark_node)
3001 return error_mark_node;
3003 if (processing_template_decl)
3005 tree ret = build_raw_new_expr (orig_placement, type, orig_nelts,
3006 orig_init, use_global_new);
3007 release_tree_vector (orig_placement);
3008 release_tree_vector (orig_init);
3009 return ret;
3012 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
3013 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
3014 TREE_NO_WARNING (rval) = 1;
3016 return rval;
3019 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
3021 tree
3022 build_java_class_ref (tree type)
3024 tree name = NULL_TREE, class_decl;
3025 static tree CL_suffix = NULL_TREE;
3026 if (CL_suffix == NULL_TREE)
3027 CL_suffix = get_identifier("class$");
3028 if (jclass_node == NULL_TREE)
3030 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
3031 if (jclass_node == NULL_TREE)
3033 error ("call to Java constructor, while %<jclass%> undefined");
3034 return error_mark_node;
3036 jclass_node = TREE_TYPE (jclass_node);
3039 /* Mangle the class$ field. */
3041 tree field;
3042 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3043 if (DECL_NAME (field) == CL_suffix)
3045 mangle_decl (field);
3046 name = DECL_ASSEMBLER_NAME (field);
3047 break;
3049 if (!field)
3051 error ("can%'t find %<class$%> in %qT", type);
3052 return error_mark_node;
3056 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
3057 if (class_decl == NULL_TREE)
3059 class_decl = build_decl (input_location,
3060 VAR_DECL, name, TREE_TYPE (jclass_node));
3061 TREE_STATIC (class_decl) = 1;
3062 DECL_EXTERNAL (class_decl) = 1;
3063 TREE_PUBLIC (class_decl) = 1;
3064 DECL_ARTIFICIAL (class_decl) = 1;
3065 DECL_IGNORED_P (class_decl) = 1;
3066 pushdecl_top_level (class_decl);
3067 make_decl_rtl (class_decl);
3069 return class_decl;
3072 static tree
3073 build_vec_delete_1 (tree base, tree maxindex, tree type,
3074 special_function_kind auto_delete_vec,
3075 int use_global_delete, tsubst_flags_t complain)
3077 tree virtual_size;
3078 tree ptype = build_pointer_type (type = complete_type (type));
3079 tree size_exp = size_in_bytes (type);
3081 /* Temporary variables used by the loop. */
3082 tree tbase, tbase_init;
3084 /* This is the body of the loop that implements the deletion of a
3085 single element, and moves temp variables to next elements. */
3086 tree body;
3088 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3089 tree loop = 0;
3091 /* This is the thing that governs what to do after the loop has run. */
3092 tree deallocate_expr = 0;
3094 /* This is the BIND_EXPR which holds the outermost iterator of the
3095 loop. It is convenient to set this variable up and test it before
3096 executing any other code in the loop.
3097 This is also the containing expression returned by this function. */
3098 tree controller = NULL_TREE;
3099 tree tmp;
3101 /* We should only have 1-D arrays here. */
3102 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
3104 if (base == error_mark_node || maxindex == error_mark_node)
3105 return error_mark_node;
3107 if (! MAYBE_CLASS_TYPE_P (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3108 goto no_destructor;
3110 /* The below is short by the cookie size. */
3111 virtual_size = size_binop (MULT_EXPR, size_exp,
3112 convert (sizetype, maxindex));
3114 tbase = create_temporary_var (ptype);
3115 tbase_init
3116 = cp_build_modify_expr (tbase, NOP_EXPR,
3117 fold_build_pointer_plus_loc (input_location,
3118 fold_convert (ptype,
3119 base),
3120 virtual_size),
3121 complain);
3122 if (tbase_init == error_mark_node)
3123 return error_mark_node;
3124 controller = build3 (BIND_EXPR, void_type_node, tbase,
3125 NULL_TREE, NULL_TREE);
3126 TREE_SIDE_EFFECTS (controller) = 1;
3128 body = build1 (EXIT_EXPR, void_type_node,
3129 build2 (EQ_EXPR, boolean_type_node, tbase,
3130 fold_convert (ptype, base)));
3131 tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp);
3132 tmp = fold_build_pointer_plus (tbase, tmp);
3133 tmp = cp_build_modify_expr (tbase, NOP_EXPR, tmp, complain);
3134 if (tmp == error_mark_node)
3135 return error_mark_node;
3136 body = build_compound_expr (input_location, body, tmp);
3137 tmp = build_delete (ptype, tbase, sfk_complete_destructor,
3138 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
3139 complain);
3140 if (tmp == error_mark_node)
3141 return error_mark_node;
3142 body = build_compound_expr (input_location, body, tmp);
3144 loop = build1 (LOOP_EXPR, void_type_node, body);
3145 loop = build_compound_expr (input_location, tbase_init, loop);
3147 no_destructor:
3148 /* Delete the storage if appropriate. */
3149 if (auto_delete_vec == sfk_deleting_destructor)
3151 tree base_tbd;
3153 /* The below is short by the cookie size. */
3154 virtual_size = size_binop (MULT_EXPR, size_exp,
3155 convert (sizetype, maxindex));
3157 if (! TYPE_VEC_NEW_USES_COOKIE (type))
3158 /* no header */
3159 base_tbd = base;
3160 else
3162 tree cookie_size;
3164 cookie_size = targetm.cxx.get_cookie_size (type);
3165 base_tbd = cp_build_binary_op (input_location,
3166 MINUS_EXPR,
3167 cp_convert (string_type_node,
3168 base, complain),
3169 cookie_size,
3170 complain);
3171 if (base_tbd == error_mark_node)
3172 return error_mark_node;
3173 base_tbd = cp_convert (ptype, base_tbd, complain);
3174 /* True size with header. */
3175 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
3178 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
3179 base_tbd, virtual_size,
3180 use_global_delete & 1,
3181 /*placement=*/NULL_TREE,
3182 /*alloc_fn=*/NULL_TREE,
3183 complain);
3186 body = loop;
3187 if (!deallocate_expr)
3189 else if (!body)
3190 body = deallocate_expr;
3191 else
3192 body = build_compound_expr (input_location, body, deallocate_expr);
3194 if (!body)
3195 body = integer_zero_node;
3197 /* Outermost wrapper: If pointer is null, punt. */
3198 body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3199 fold_build2_loc (input_location,
3200 NE_EXPR, boolean_type_node, base,
3201 convert (TREE_TYPE (base),
3202 nullptr_node)),
3203 body, integer_zero_node);
3204 body = build1 (NOP_EXPR, void_type_node, body);
3206 if (controller)
3208 TREE_OPERAND (controller, 1) = body;
3209 body = controller;
3212 if (TREE_CODE (base) == SAVE_EXPR)
3213 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
3214 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
3216 return convert_to_void (body, ICV_CAST, complain);
3219 /* Create an unnamed variable of the indicated TYPE. */
3221 tree
3222 create_temporary_var (tree type)
3224 tree decl;
3226 decl = build_decl (input_location,
3227 VAR_DECL, NULL_TREE, type);
3228 TREE_USED (decl) = 1;
3229 DECL_ARTIFICIAL (decl) = 1;
3230 DECL_IGNORED_P (decl) = 1;
3231 DECL_CONTEXT (decl) = current_function_decl;
3233 return decl;
3236 /* Create a new temporary variable of the indicated TYPE, initialized
3237 to INIT.
3239 It is not entered into current_binding_level, because that breaks
3240 things when it comes time to do final cleanups (which take place
3241 "outside" the binding contour of the function). */
3243 tree
3244 get_temp_regvar (tree type, tree init)
3246 tree decl;
3248 decl = create_temporary_var (type);
3249 add_decl_expr (decl);
3251 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
3252 tf_warning_or_error));
3254 return decl;
3257 /* `build_vec_init' returns tree structure that performs
3258 initialization of a vector of aggregate types.
3260 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
3261 to the first element, of POINTER_TYPE.
3262 MAXINDEX is the maximum index of the array (one less than the
3263 number of elements). It is only used if BASE is a pointer or
3264 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
3266 INIT is the (possibly NULL) initializer.
3268 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
3269 elements in the array are value-initialized.
3271 FROM_ARRAY is 0 if we should init everything with INIT
3272 (i.e., every element initialized from INIT).
3273 FROM_ARRAY is 1 if we should index into INIT in parallel
3274 with initialization of DECL.
3275 FROM_ARRAY is 2 if we should index into INIT in parallel,
3276 but use assignment instead of initialization. */
3278 tree
3279 build_vec_init (tree base, tree maxindex, tree init,
3280 bool explicit_value_init_p,
3281 int from_array, tsubst_flags_t complain)
3283 tree rval;
3284 tree base2 = NULL_TREE;
3285 tree itype = NULL_TREE;
3286 tree iterator;
3287 /* The type of BASE. */
3288 tree atype = TREE_TYPE (base);
3289 /* The type of an element in the array. */
3290 tree type = TREE_TYPE (atype);
3291 /* The element type reached after removing all outer array
3292 types. */
3293 tree inner_elt_type;
3294 /* The type of a pointer to an element in the array. */
3295 tree ptype;
3296 tree stmt_expr;
3297 tree compound_stmt;
3298 int destroy_temps;
3299 tree try_block = NULL_TREE;
3300 int num_initialized_elts = 0;
3301 bool is_global;
3302 tree const_init = NULL_TREE;
3303 tree obase = base;
3304 bool xvalue = false;
3305 bool errors = false;
3306 tree length_check = NULL_TREE;
3308 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
3309 maxindex = array_type_nelts (atype);
3311 if (maxindex == NULL_TREE || maxindex == error_mark_node)
3312 return error_mark_node;
3314 if (explicit_value_init_p)
3315 gcc_assert (!init);
3317 inner_elt_type = strip_array_types (type);
3319 /* Look through the TARGET_EXPR around a compound literal. */
3320 if (init && TREE_CODE (init) == TARGET_EXPR
3321 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
3322 && from_array != 2)
3323 init = TARGET_EXPR_INITIAL (init);
3325 /* If we have a braced-init-list, make sure that the array
3326 is big enough for all the initializers. */
3327 if (init && TREE_CODE (init) == CONSTRUCTOR
3328 && CONSTRUCTOR_NELTS (init) > 0
3329 && !TREE_CONSTANT (maxindex))
3330 length_check = fold_build2 (LT_EXPR, boolean_type_node, maxindex,
3331 size_int (CONSTRUCTOR_NELTS (init) - 1));
3333 if (init
3334 && TREE_CODE (atype) == ARRAY_TYPE
3335 && (from_array == 2
3336 ? (!CLASS_TYPE_P (inner_elt_type)
3337 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (inner_elt_type))
3338 : !TYPE_NEEDS_CONSTRUCTING (type))
3339 && ((TREE_CODE (init) == CONSTRUCTOR
3340 /* Don't do this if the CONSTRUCTOR might contain something
3341 that might throw and require us to clean up. */
3342 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
3343 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
3344 || from_array))
3346 /* Do non-default initialization of trivial arrays resulting from
3347 brace-enclosed initializers. In this case, digest_init and
3348 store_constructor will handle the semantics for us. */
3350 stmt_expr = build2 (INIT_EXPR, atype, base, init);
3351 if (length_check)
3352 stmt_expr = build3 (COND_EXPR, atype, length_check,
3353 throw_bad_array_length (),
3354 stmt_expr);
3355 return stmt_expr;
3358 maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
3359 if (TREE_CODE (atype) == ARRAY_TYPE)
3361 ptype = build_pointer_type (type);
3362 base = decay_conversion (base, complain);
3363 if (base == error_mark_node)
3364 return error_mark_node;
3365 base = cp_convert (ptype, base, complain);
3367 else
3368 ptype = atype;
3370 /* The code we are generating looks like:
3372 T* t1 = (T*) base;
3373 T* rval = t1;
3374 ptrdiff_t iterator = maxindex;
3375 try {
3376 for (; iterator != -1; --iterator) {
3377 ... initialize *t1 ...
3378 ++t1;
3380 } catch (...) {
3381 ... destroy elements that were constructed ...
3383 rval;
3386 We can omit the try and catch blocks if we know that the
3387 initialization will never throw an exception, or if the array
3388 elements do not have destructors. We can omit the loop completely if
3389 the elements of the array do not have constructors.
3391 We actually wrap the entire body of the above in a STMT_EXPR, for
3392 tidiness.
3394 When copying from array to another, when the array elements have
3395 only trivial copy constructors, we should use __builtin_memcpy
3396 rather than generating a loop. That way, we could take advantage
3397 of whatever cleverness the back end has for dealing with copies
3398 of blocks of memory. */
3400 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
3401 destroy_temps = stmts_are_full_exprs_p ();
3402 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3403 rval = get_temp_regvar (ptype, base);
3404 base = get_temp_regvar (ptype, rval);
3405 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
3407 /* If initializing one array from another, initialize element by
3408 element. We rely upon the below calls to do the argument
3409 checking. Evaluate the initializer before entering the try block. */
3410 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
3412 if (lvalue_kind (init) & clk_rvalueref)
3413 xvalue = true;
3414 base2 = decay_conversion (init, complain);
3415 if (base2 == error_mark_node)
3416 return error_mark_node;
3417 itype = TREE_TYPE (base2);
3418 base2 = get_temp_regvar (itype, base2);
3419 itype = TREE_TYPE (itype);
3422 /* Protect the entire array initialization so that we can destroy
3423 the partially constructed array if an exception is thrown.
3424 But don't do this if we're assigning. */
3425 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3426 && from_array != 2)
3428 try_block = begin_try_block ();
3431 /* If the initializer is {}, then all elements are initialized from {}.
3432 But for non-classes, that's the same as value-initialization. */
3433 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
3434 && CONSTRUCTOR_NELTS (init) == 0)
3436 if (CLASS_TYPE_P (type))
3437 /* Leave init alone. */;
3438 else
3440 init = NULL_TREE;
3441 explicit_value_init_p = true;
3445 /* Maybe pull out constant value when from_array? */
3447 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
3449 /* Do non-default initialization of non-trivial arrays resulting from
3450 brace-enclosed initializers. */
3451 unsigned HOST_WIDE_INT idx;
3452 tree field, elt;
3453 /* Should we try to create a constant initializer? */
3454 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
3455 && (literal_type_p (inner_elt_type)
3456 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
3457 /* If the constructor already has the array type, it's been through
3458 digest_init, so we shouldn't try to do anything more. */
3459 bool digested = same_type_p (atype, TREE_TYPE (init));
3460 bool saw_non_const = false;
3461 bool saw_const = false;
3462 /* If we're initializing a static array, we want to do static
3463 initialization of any elements with constant initializers even if
3464 some are non-constant. */
3465 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
3466 vec<constructor_elt, va_gc> *new_vec;
3467 from_array = 0;
3469 if (length_check)
3471 tree throw_call;
3472 if (array_of_runtime_bound_p (atype))
3473 throw_call = throw_bad_array_length ();
3474 else
3475 throw_call = throw_bad_array_new_length ();
3476 length_check = build3 (COND_EXPR, void_type_node, length_check,
3477 throw_call, void_zero_node);
3478 finish_expr_stmt (length_check);
3481 if (try_const)
3482 vec_alloc (new_vec, CONSTRUCTOR_NELTS (init));
3483 else
3484 new_vec = NULL;
3486 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
3488 tree baseref = build1 (INDIRECT_REF, type, base);
3489 tree one_init;
3491 num_initialized_elts++;
3493 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3494 if (digested)
3495 one_init = build2 (INIT_EXPR, type, baseref, elt);
3496 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3497 one_init = build_aggr_init (baseref, elt, 0, complain);
3498 else
3499 one_init = cp_build_modify_expr (baseref, NOP_EXPR,
3500 elt, complain);
3501 if (one_init == error_mark_node)
3502 errors = true;
3503 if (try_const)
3505 tree e = one_init;
3506 if (TREE_CODE (e) == EXPR_STMT)
3507 e = TREE_OPERAND (e, 0);
3508 if (TREE_CODE (e) == CONVERT_EXPR
3509 && VOID_TYPE_P (TREE_TYPE (e)))
3510 e = TREE_OPERAND (e, 0);
3511 e = maybe_constant_init (e);
3512 if (reduced_constant_expression_p (e))
3514 CONSTRUCTOR_APPEND_ELT (new_vec, field, e);
3515 if (do_static_init)
3516 one_init = NULL_TREE;
3517 else
3518 one_init = build2 (INIT_EXPR, type, baseref, e);
3519 saw_const = true;
3521 else
3523 if (do_static_init)
3525 tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
3526 true);
3527 if (value)
3528 CONSTRUCTOR_APPEND_ELT (new_vec, field, value);
3530 saw_non_const = true;
3534 if (one_init)
3535 finish_expr_stmt (one_init);
3536 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3538 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, 0, complain);
3539 if (one_init == error_mark_node)
3540 errors = true;
3541 else
3542 finish_expr_stmt (one_init);
3544 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3545 complain);
3546 if (one_init == error_mark_node)
3547 errors = true;
3548 else
3549 finish_expr_stmt (one_init);
3552 if (try_const)
3554 if (!saw_non_const)
3555 const_init = build_constructor (atype, new_vec);
3556 else if (do_static_init && saw_const)
3557 DECL_INITIAL (obase) = build_constructor (atype, new_vec);
3558 else
3559 vec_free (new_vec);
3562 /* Clear out INIT so that we don't get confused below. */
3563 init = NULL_TREE;
3565 else if (from_array)
3567 if (init)
3568 /* OK, we set base2 above. */;
3569 else if (CLASS_TYPE_P (type)
3570 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3572 if (complain & tf_error)
3573 error ("initializer ends prematurely");
3574 errors = true;
3578 /* Now, default-initialize any remaining elements. We don't need to
3579 do that if a) the type does not need constructing, or b) we've
3580 already initialized all the elements.
3582 We do need to keep going if we're copying an array. */
3584 if (from_array
3585 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
3586 && ! (host_integerp (maxindex, 0)
3587 && (num_initialized_elts
3588 == tree_low_cst (maxindex, 0) + 1))))
3590 /* If the ITERATOR is equal to -1, then we don't have to loop;
3591 we've already initialized all the elements. */
3592 tree for_stmt;
3593 tree elt_init;
3594 tree to;
3596 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
3597 finish_for_init_stmt (for_stmt);
3598 finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
3599 build_int_cst (TREE_TYPE (iterator), -1)),
3600 for_stmt);
3601 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3602 complain);
3603 if (elt_init == error_mark_node)
3604 errors = true;
3605 finish_for_expr (elt_init, for_stmt);
3607 to = build1 (INDIRECT_REF, type, base);
3609 if (from_array)
3611 tree from;
3613 if (base2)
3615 from = build1 (INDIRECT_REF, itype, base2);
3616 if (xvalue)
3617 from = move (from);
3619 else
3620 from = NULL_TREE;
3622 if (from_array == 2)
3623 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3624 complain);
3625 else if (type_build_ctor_call (type))
3626 elt_init = build_aggr_init (to, from, 0, complain);
3627 else if (from)
3628 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3629 complain);
3630 else
3631 gcc_unreachable ();
3633 else if (TREE_CODE (type) == ARRAY_TYPE)
3635 if (init != 0)
3636 sorry
3637 ("cannot initialize multi-dimensional array with initializer");
3638 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
3639 0, 0,
3640 explicit_value_init_p,
3641 0, complain);
3643 else if (explicit_value_init_p)
3645 elt_init = build_value_init (type, complain);
3646 if (elt_init != error_mark_node)
3647 elt_init = build2 (INIT_EXPR, type, to, elt_init);
3649 else
3651 gcc_assert (type_build_ctor_call (type) || init);
3652 if (CLASS_TYPE_P (type))
3653 elt_init = build_aggr_init (to, init, 0, complain);
3654 else
3656 if (TREE_CODE (init) == TREE_LIST)
3657 init = build_x_compound_expr_from_list (init, ELK_INIT,
3658 complain);
3659 elt_init = build2 (INIT_EXPR, type, to, init);
3663 if (elt_init == error_mark_node)
3664 errors = true;
3666 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3667 finish_expr_stmt (elt_init);
3668 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3670 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
3671 complain));
3672 if (base2)
3673 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0,
3674 complain));
3676 finish_for_stmt (for_stmt);
3679 /* Make sure to cleanup any partially constructed elements. */
3680 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3681 && from_array != 2)
3683 tree e;
3684 tree m = cp_build_binary_op (input_location,
3685 MINUS_EXPR, maxindex, iterator,
3686 complain);
3688 /* Flatten multi-dimensional array since build_vec_delete only
3689 expects one-dimensional array. */
3690 if (TREE_CODE (type) == ARRAY_TYPE)
3691 m = cp_build_binary_op (input_location,
3692 MULT_EXPR, m,
3693 /* Avoid mixing signed and unsigned. */
3694 convert (TREE_TYPE (m),
3695 array_type_nelts_total (type)),
3696 complain);
3698 finish_cleanup_try_block (try_block);
3699 e = build_vec_delete_1 (rval, m,
3700 inner_elt_type, sfk_complete_destructor,
3701 /*use_global_delete=*/0, complain);
3702 if (e == error_mark_node)
3703 errors = true;
3704 finish_cleanup (e, try_block);
3707 /* The value of the array initialization is the array itself, RVAL
3708 is a pointer to the first element. */
3709 finish_stmt_expr_expr (rval, stmt_expr);
3711 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
3713 /* Now make the result have the correct type. */
3714 if (TREE_CODE (atype) == ARRAY_TYPE)
3716 atype = build_pointer_type (atype);
3717 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
3718 stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain);
3719 TREE_NO_WARNING (stmt_expr) = 1;
3722 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3724 if (const_init)
3725 return build2 (INIT_EXPR, atype, obase, const_init);
3726 if (errors)
3727 return error_mark_node;
3728 return stmt_expr;
3731 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3732 build_delete. */
3734 static tree
3735 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
3736 tsubst_flags_t complain)
3738 tree name;
3739 tree fn;
3740 switch (dtor_kind)
3742 case sfk_complete_destructor:
3743 name = complete_dtor_identifier;
3744 break;
3746 case sfk_base_destructor:
3747 name = base_dtor_identifier;
3748 break;
3750 case sfk_deleting_destructor:
3751 name = deleting_dtor_identifier;
3752 break;
3754 default:
3755 gcc_unreachable ();
3757 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
3758 return build_new_method_call (exp, fn,
3759 /*args=*/NULL,
3760 /*conversion_path=*/NULL_TREE,
3761 flags,
3762 /*fn_p=*/NULL,
3763 complain);
3766 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3767 ADDR is an expression which yields the store to be destroyed.
3768 AUTO_DELETE is the name of the destructor to call, i.e., either
3769 sfk_complete_destructor, sfk_base_destructor, or
3770 sfk_deleting_destructor.
3772 FLAGS is the logical disjunction of zero or more LOOKUP_
3773 flags. See cp-tree.h for more info. */
3775 tree
3776 build_delete (tree type, tree addr, special_function_kind auto_delete,
3777 int flags, int use_global_delete, tsubst_flags_t complain)
3779 tree expr;
3781 if (addr == error_mark_node)
3782 return error_mark_node;
3784 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3785 set to `error_mark_node' before it gets properly cleaned up. */
3786 if (type == error_mark_node)
3787 return error_mark_node;
3789 type = TYPE_MAIN_VARIANT (type);
3791 addr = mark_rvalue_use (addr);
3793 if (TYPE_PTR_P (type))
3795 bool complete_p = true;
3797 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3798 if (TREE_CODE (type) == ARRAY_TYPE)
3799 goto handle_array;
3801 /* We don't want to warn about delete of void*, only other
3802 incomplete types. Deleting other incomplete types
3803 invokes undefined behavior, but it is not ill-formed, so
3804 compile to something that would even do The Right Thing
3805 (TM) should the type have a trivial dtor and no delete
3806 operator. */
3807 if (!VOID_TYPE_P (type))
3809 complete_type (type);
3810 if (!COMPLETE_TYPE_P (type))
3812 if ((complain & tf_warning)
3813 && warning (0, "possible problem detected in invocation of "
3814 "delete operator:"))
3816 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
3817 inform (input_location, "neither the destructor nor the class-specific "
3818 "operator delete will be called, even if they are "
3819 "declared when the class is defined");
3821 complete_p = false;
3823 else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor
3824 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
3825 && TYPE_POLYMORPHIC_P (type))
3827 tree dtor;
3828 dtor = CLASSTYPE_DESTRUCTORS (type);
3829 if (!dtor || !DECL_VINDEX (dtor))
3831 if (CLASSTYPE_PURE_VIRTUALS (type))
3832 warning (OPT_Wdelete_non_virtual_dtor,
3833 "deleting object of abstract class type %qT"
3834 " which has non-virtual destructor"
3835 " will cause undefined behaviour", type);
3836 else
3837 warning (OPT_Wdelete_non_virtual_dtor,
3838 "deleting object of polymorphic class type %qT"
3839 " which has non-virtual destructor"
3840 " might cause undefined behaviour", type);
3844 if (VOID_TYPE_P (type) || !complete_p || !MAYBE_CLASS_TYPE_P (type))
3845 /* Call the builtin operator delete. */
3846 return build_builtin_delete_call (addr);
3847 if (TREE_SIDE_EFFECTS (addr))
3848 addr = save_expr (addr);
3850 /* Throw away const and volatile on target type of addr. */
3851 addr = convert_force (build_pointer_type (type), addr, 0, complain);
3853 else if (TREE_CODE (type) == ARRAY_TYPE)
3855 handle_array:
3857 if (TYPE_DOMAIN (type) == NULL_TREE)
3859 if (complain & tf_error)
3860 error ("unknown array size in delete");
3861 return error_mark_node;
3863 return build_vec_delete (addr, array_type_nelts (type),
3864 auto_delete, use_global_delete, complain);
3866 else
3868 /* Don't check PROTECT here; leave that decision to the
3869 destructor. If the destructor is accessible, call it,
3870 else report error. */
3871 addr = cp_build_addr_expr (addr, complain);
3872 if (addr == error_mark_node)
3873 return error_mark_node;
3874 if (TREE_SIDE_EFFECTS (addr))
3875 addr = save_expr (addr);
3877 addr = convert_force (build_pointer_type (type), addr, 0, complain);
3880 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3882 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3884 if (auto_delete != sfk_deleting_destructor)
3885 return void_zero_node;
3887 return build_op_delete_call (DELETE_EXPR, addr,
3888 cxx_sizeof_nowarn (type),
3889 use_global_delete,
3890 /*placement=*/NULL_TREE,
3891 /*alloc_fn=*/NULL_TREE,
3892 complain);
3894 else
3896 tree head = NULL_TREE;
3897 tree do_delete = NULL_TREE;
3898 tree ifexp;
3900 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
3901 lazily_declare_fn (sfk_destructor, type);
3903 /* For `::delete x', we must not use the deleting destructor
3904 since then we would not be sure to get the global `operator
3905 delete'. */
3906 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3908 /* We will use ADDR multiple times so we must save it. */
3909 addr = save_expr (addr);
3910 head = get_target_expr (build_headof (addr));
3911 /* Delete the object. */
3912 do_delete = build_builtin_delete_call (head);
3913 /* Otherwise, treat this like a complete object destructor
3914 call. */
3915 auto_delete = sfk_complete_destructor;
3917 /* If the destructor is non-virtual, there is no deleting
3918 variant. Instead, we must explicitly call the appropriate
3919 `operator delete' here. */
3920 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3921 && auto_delete == sfk_deleting_destructor)
3923 /* We will use ADDR multiple times so we must save it. */
3924 addr = save_expr (addr);
3925 /* Build the call. */
3926 do_delete = build_op_delete_call (DELETE_EXPR,
3927 addr,
3928 cxx_sizeof_nowarn (type),
3929 /*global_p=*/false,
3930 /*placement=*/NULL_TREE,
3931 /*alloc_fn=*/NULL_TREE,
3932 complain);
3933 /* Call the complete object destructor. */
3934 auto_delete = sfk_complete_destructor;
3936 else if (auto_delete == sfk_deleting_destructor
3937 && TYPE_GETS_REG_DELETE (type))
3939 /* Make sure we have access to the member op delete, even though
3940 we'll actually be calling it from the destructor. */
3941 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
3942 /*global_p=*/false,
3943 /*placement=*/NULL_TREE,
3944 /*alloc_fn=*/NULL_TREE,
3945 complain);
3948 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, complain),
3949 auto_delete, flags, complain);
3950 if (expr == error_mark_node)
3951 return error_mark_node;
3952 if (do_delete)
3953 expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
3955 /* We need to calculate this before the dtor changes the vptr. */
3956 if (head)
3957 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
3959 if (flags & LOOKUP_DESTRUCTOR)
3960 /* Explicit destructor call; don't check for null pointer. */
3961 ifexp = integer_one_node;
3962 else
3964 /* Handle deleting a null pointer. */
3965 ifexp = fold (cp_build_binary_op (input_location,
3966 NE_EXPR, addr, nullptr_node,
3967 complain));
3968 if (ifexp == error_mark_node)
3969 return error_mark_node;
3972 if (ifexp != integer_one_node)
3973 expr = build3 (COND_EXPR, void_type_node,
3974 ifexp, expr, void_zero_node);
3976 return expr;
3980 /* At the beginning of a destructor, push cleanups that will call the
3981 destructors for our base classes and members.
3983 Called from begin_destructor_body. */
3985 void
3986 push_base_cleanups (void)
3988 tree binfo, base_binfo;
3989 int i;
3990 tree member;
3991 tree expr;
3992 vec<tree, va_gc> *vbases;
3994 /* Run destructors for all virtual baseclasses. */
3995 if (CLASSTYPE_VBASECLASSES (current_class_type))
3997 tree cond = (condition_conversion
3998 (build2 (BIT_AND_EXPR, integer_type_node,
3999 current_in_charge_parm,
4000 integer_two_node)));
4002 /* The CLASSTYPE_VBASECLASSES vector is in initialization
4003 order, which is also the right order for pushing cleanups. */
4004 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
4005 vec_safe_iterate (vbases, i, &base_binfo); i++)
4007 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
4009 expr = build_special_member_call (current_class_ref,
4010 base_dtor_identifier,
4011 NULL,
4012 base_binfo,
4013 (LOOKUP_NORMAL
4014 | LOOKUP_NONVIRTUAL),
4015 tf_warning_or_error);
4016 expr = build3 (COND_EXPR, void_type_node, cond,
4017 expr, void_zero_node);
4018 finish_decl_cleanup (NULL_TREE, expr);
4023 /* Take care of the remaining baseclasses. */
4024 for (binfo = TYPE_BINFO (current_class_type), i = 0;
4025 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4027 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
4028 || BINFO_VIRTUAL_P (base_binfo))
4029 continue;
4031 expr = build_special_member_call (current_class_ref,
4032 base_dtor_identifier,
4033 NULL, base_binfo,
4034 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
4035 tf_warning_or_error);
4036 finish_decl_cleanup (NULL_TREE, expr);
4039 /* Don't automatically destroy union members. */
4040 if (TREE_CODE (current_class_type) == UNION_TYPE)
4041 return;
4043 for (member = TYPE_FIELDS (current_class_type); member;
4044 member = DECL_CHAIN (member))
4046 tree this_type = TREE_TYPE (member);
4047 if (this_type == error_mark_node
4048 || TREE_CODE (member) != FIELD_DECL
4049 || DECL_ARTIFICIAL (member))
4050 continue;
4051 if (ANON_UNION_TYPE_P (this_type))
4052 continue;
4053 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
4055 tree this_member = (build_class_member_access_expr
4056 (current_class_ref, member,
4057 /*access_path=*/NULL_TREE,
4058 /*preserve_reference=*/false,
4059 tf_warning_or_error));
4060 expr = build_delete (this_type, this_member,
4061 sfk_complete_destructor,
4062 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
4063 0, tf_warning_or_error);
4064 finish_decl_cleanup (NULL_TREE, expr);
4069 /* Build a C++ vector delete expression.
4070 MAXINDEX is the number of elements to be deleted.
4071 ELT_SIZE is the nominal size of each element in the vector.
4072 BASE is the expression that should yield the store to be deleted.
4073 This function expands (or synthesizes) these calls itself.
4074 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
4076 This also calls delete for virtual baseclasses of elements of the vector.
4078 Update: MAXINDEX is no longer needed. The size can be extracted from the
4079 start of the vector for pointers, and from the type for arrays. We still
4080 use MAXINDEX for arrays because it happens to already have one of the
4081 values we'd have to extract. (We could use MAXINDEX with pointers to
4082 confirm the size, and trap if the numbers differ; not clear that it'd
4083 be worth bothering.) */
4085 tree
4086 build_vec_delete (tree base, tree maxindex,
4087 special_function_kind auto_delete_vec,
4088 int use_global_delete, tsubst_flags_t complain)
4090 tree type;
4091 tree rval;
4092 tree base_init = NULL_TREE;
4094 type = TREE_TYPE (base);
4096 if (TYPE_PTR_P (type))
4098 /* Step back one from start of vector, and read dimension. */
4099 tree cookie_addr;
4100 tree size_ptr_type = build_pointer_type (sizetype);
4102 if (TREE_SIDE_EFFECTS (base))
4104 base_init = get_target_expr (base);
4105 base = TARGET_EXPR_SLOT (base_init);
4107 type = strip_array_types (TREE_TYPE (type));
4108 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
4109 sizetype, TYPE_SIZE_UNIT (sizetype));
4110 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
4111 cookie_addr);
4112 maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, complain);
4114 else if (TREE_CODE (type) == ARRAY_TYPE)
4116 /* Get the total number of things in the array, maxindex is a
4117 bad name. */
4118 maxindex = array_type_nelts_total (type);
4119 type = strip_array_types (type);
4120 base = cp_build_addr_expr (base, complain);
4121 if (base == error_mark_node)
4122 return error_mark_node;
4123 if (TREE_SIDE_EFFECTS (base))
4125 base_init = get_target_expr (base);
4126 base = TARGET_EXPR_SLOT (base_init);
4129 else
4131 if (base != error_mark_node && !(complain & tf_error))
4132 error ("type to vector delete is neither pointer or array type");
4133 return error_mark_node;
4136 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
4137 use_global_delete, complain);
4138 if (base_init && rval != error_mark_node)
4139 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
4141 return rval;