2011-08-19 Andrew Stubbs <ams@codesourcery.com>
[official-gcc.git] / gcc / cp / init.c
blob4fa627ba8c01c8b98ab9c31ce428e58472558494
1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* High-level class interface. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "flags.h"
32 #include "output.h"
33 #include "target.h"
35 static bool begin_init_stmts (tree *, tree *);
36 static tree finish_init_stmts (bool, tree, tree);
37 static void construct_virtual_base (tree, tree);
38 static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
39 static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
40 static void perform_member_init (tree, tree);
41 static tree build_builtin_delete_call (tree);
42 static int member_init_ok_or_else (tree, tree, tree);
43 static void expand_virtual_init (tree, tree);
44 static tree sort_mem_initializers (tree, tree);
45 static tree initializing_context (tree);
46 static void expand_cleanup_for_base (tree, tree);
47 static tree dfs_initialize_vtbl_ptrs (tree, void *);
48 static tree build_field_list (tree, tree, int *);
49 static tree build_vtbl_address (tree);
50 static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
52 /* We are about to generate some complex initialization code.
53 Conceptually, it is all a single expression. However, we may want
54 to include conditionals, loops, and other such statement-level
55 constructs. Therefore, we build the initialization code inside a
56 statement-expression. This function starts such an expression.
57 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
58 pass them back to finish_init_stmts when the expression is
59 complete. */
61 static bool
62 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
64 bool is_global = !building_stmt_list_p ();
66 *stmt_expr_p = begin_stmt_expr ();
67 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
69 return is_global;
72 /* Finish out the statement-expression begun by the previous call to
73 begin_init_stmts. Returns the statement-expression itself. */
75 static tree
76 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
78 finish_compound_stmt (compound_stmt);
80 stmt_expr = finish_stmt_expr (stmt_expr, true);
82 gcc_assert (!building_stmt_list_p () == is_global);
84 return stmt_expr;
87 /* Constructors */
89 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
90 which we want to initialize the vtable pointer for, DATA is
91 TREE_LIST whose TREE_VALUE is the this ptr expression. */
93 static tree
94 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
96 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
97 return dfs_skip_bases;
99 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
101 tree base_ptr = TREE_VALUE ((tree) data);
103 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1);
105 expand_virtual_init (binfo, base_ptr);
108 return NULL_TREE;
111 /* Initialize all the vtable pointers in the object pointed to by
112 ADDR. */
114 void
115 initialize_vtbl_ptrs (tree addr)
117 tree list;
118 tree type;
120 type = TREE_TYPE (TREE_TYPE (addr));
121 list = build_tree_list (type, addr);
123 /* Walk through the hierarchy, initializing the vptr in each base
124 class. We do these in pre-order because we can't find the virtual
125 bases for a class until we've initialized the vtbl for that
126 class. */
127 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
130 /* Return an expression for the zero-initialization of an object with
131 type T. This expression will either be a constant (in the case
132 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
133 aggregate), or NULL (in the case that T does not require
134 initialization). In either case, the value can be used as
135 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
136 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
137 is the number of elements in the array. If STATIC_STORAGE_P is
138 TRUE, initializers are only generated for entities for which
139 zero-initialization does not simply mean filling the storage with
140 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
141 subfields with bit positions at or above that bit size shouldn't
142 be added. */
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 (SCALAR_TYPE_P (type))
179 init = convert (type, integer_zero_node);
180 else if (CLASS_TYPE_P (type))
182 tree field;
183 VEC(constructor_elt,gc) *v = NULL;
185 /* Iterate over the fields, building initializations. */
186 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
188 if (TREE_CODE (field) != FIELD_DECL)
189 continue;
191 /* Don't add virtual bases for base classes if they are beyond
192 the size of the current field, that means it is present
193 somewhere else in the object. */
194 if (field_size)
196 tree bitpos = bit_position (field);
197 if (TREE_CODE (bitpos) == INTEGER_CST
198 && !tree_int_cst_lt (bitpos, field_size))
199 continue;
202 /* Note that for class types there will be FIELD_DECLs
203 corresponding to base classes as well. Thus, iterating
204 over TYPE_FIELDs will result in correct initialization of
205 all of the subobjects. */
206 if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
208 tree new_field_size
209 = (DECL_FIELD_IS_BASE (field)
210 && DECL_SIZE (field)
211 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
212 ? DECL_SIZE (field) : NULL_TREE;
213 tree value = build_zero_init_1 (TREE_TYPE (field),
214 /*nelts=*/NULL_TREE,
215 static_storage_p,
216 new_field_size);
217 if (value)
218 CONSTRUCTOR_APPEND_ELT(v, field, value);
221 /* For unions, only the first field is initialized. */
222 if (TREE_CODE (type) == UNION_TYPE)
223 break;
226 /* Build a constructor to contain the initializations. */
227 init = build_constructor (type, v);
229 else if (TREE_CODE (type) == ARRAY_TYPE)
231 tree max_index;
232 VEC(constructor_elt,gc) *v = NULL;
234 /* Iterate over the array elements, building initializations. */
235 if (nelts)
236 max_index = fold_build2_loc (input_location,
237 MINUS_EXPR, TREE_TYPE (nelts),
238 nelts, integer_one_node);
239 else
240 max_index = array_type_nelts (type);
242 /* If we have an error_mark here, we should just return error mark
243 as we don't know the size of the array yet. */
244 if (max_index == error_mark_node)
245 return error_mark_node;
246 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
248 /* A zero-sized array, which is accepted as an extension, will
249 have an upper bound of -1. */
250 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
252 constructor_elt *ce;
254 v = VEC_alloc (constructor_elt, gc, 1);
255 ce = VEC_quick_push (constructor_elt, v, NULL);
257 /* If this is a one element array, we just use a regular init. */
258 if (tree_int_cst_equal (size_zero_node, max_index))
259 ce->index = size_zero_node;
260 else
261 ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
262 max_index);
264 ce->value = build_zero_init_1 (TREE_TYPE (type),
265 /*nelts=*/NULL_TREE,
266 static_storage_p, NULL_TREE);
269 /* Build a constructor to contain the initializations. */
270 init = build_constructor (type, v);
272 else if (TREE_CODE (type) == VECTOR_TYPE)
273 init = build_zero_cst (type);
274 else
275 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
277 /* In all cases, the initializer is a constant. */
278 if (init)
279 TREE_CONSTANT (init) = 1;
281 return init;
284 /* Return an expression for the zero-initialization of an object with
285 type T. This expression will either be a constant (in the case
286 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
287 aggregate), or NULL (in the case that T does not require
288 initialization). In either case, the value can be used as
289 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
290 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
291 is the number of elements in the array. If STATIC_STORAGE_P is
292 TRUE, initializers are only generated for entities for which
293 zero-initialization does not simply mean filling the storage with
294 zero bytes. */
296 tree
297 build_zero_init (tree type, tree nelts, bool static_storage_p)
299 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
302 /* Return a suitable initializer for value-initializing an object of type
303 TYPE, as described in [dcl.init]. */
305 tree
306 build_value_init (tree type, tsubst_flags_t complain)
308 /* [dcl.init]
310 To value-initialize an object of type T means:
312 - if T is a class type (clause 9) with a user-provided constructor
313 (12.1), then the default constructor for T is called (and the
314 initialization is ill-formed if T has no accessible default
315 constructor);
317 - if T is a non-union class type without a user-provided constructor,
318 then every non-static data member and base-class component of T is
319 value-initialized;92)
321 - if T is an array type, then each element is value-initialized;
323 - otherwise, the object is zero-initialized.
325 A program that calls for default-initialization or
326 value-initialization of an entity of reference type is ill-formed.
328 92) Value-initialization for such a class object may be implemented by
329 zero-initializing the object and then calling the default
330 constructor. */
332 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
333 gcc_assert (!processing_template_decl);
335 if (CLASS_TYPE_P (type))
337 /* Instead of the above, only consider the user-providedness of the
338 default constructor itself so value-initializing a class with an
339 explicitly defaulted default constructor and another user-provided
340 constructor works properly (c++std-core-19883). */
341 if (type_has_user_provided_default_constructor (type)
342 || (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
343 && type_has_user_provided_constructor (type)))
344 return build_aggr_init_expr
345 (type,
346 build_special_member_call (NULL_TREE, complete_ctor_identifier,
347 NULL, type, LOOKUP_NORMAL,
348 complain),
349 complain);
350 else if (TYPE_HAS_COMPLEX_DFLT (type))
352 /* This is a class that needs constructing, but doesn't have
353 a user-provided constructor. So we need to zero-initialize
354 the object and then call the implicitly defined ctor.
355 This will be handled in simplify_aggr_init_expr. */
356 tree ctor = build_special_member_call
357 (NULL_TREE, complete_ctor_identifier,
358 NULL, type, LOOKUP_NORMAL, complain);
359 if (ctor != error_mark_node)
361 ctor = build_aggr_init_expr (type, ctor, complain);
362 AGGR_INIT_ZERO_FIRST (ctor) = 1;
364 return ctor;
367 return build_value_init_noctor (type, complain);
370 /* Like build_value_init, but don't call the constructor for TYPE. Used
371 for base initializers. */
373 tree
374 build_value_init_noctor (tree type, tsubst_flags_t complain)
376 /* FIXME the class and array cases should just use digest_init once it is
377 SFINAE-enabled. */
378 if (CLASS_TYPE_P (type))
380 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type));
382 if (TREE_CODE (type) != UNION_TYPE)
384 tree field;
385 VEC(constructor_elt,gc) *v = NULL;
387 /* Iterate over the fields, building initializations. */
388 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
390 tree ftype, value;
392 if (TREE_CODE (field) != FIELD_DECL)
393 continue;
395 ftype = TREE_TYPE (field);
397 /* We could skip vfields and fields of types with
398 user-defined constructors, but I think that won't improve
399 performance at all; it should be simpler in general just
400 to zero out the entire object than try to only zero the
401 bits that actually need it. */
403 /* Note that for class types there will be FIELD_DECLs
404 corresponding to base classes as well. Thus, iterating
405 over TYPE_FIELDs will result in correct initialization of
406 all of the subobjects. */
407 value = build_value_init (ftype, complain);
409 if (value == error_mark_node)
410 return error_mark_node;
412 if (value)
413 CONSTRUCTOR_APPEND_ELT(v, field, value);
416 /* Build a constructor to contain the zero- initializations. */
417 return build_constructor (type, v);
420 else if (TREE_CODE (type) == ARRAY_TYPE)
422 VEC(constructor_elt,gc) *v = NULL;
424 /* Iterate over the array elements, building initializations. */
425 tree max_index = array_type_nelts (type);
427 /* If we have an error_mark here, we should just return error mark
428 as we don't know the size of the array yet. */
429 if (max_index == error_mark_node)
431 if (complain & tf_error)
432 error ("cannot value-initialize array of unknown bound %qT",
433 type);
434 return error_mark_node;
436 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
438 /* A zero-sized array, which is accepted as an extension, will
439 have an upper bound of -1. */
440 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
442 constructor_elt *ce;
444 v = VEC_alloc (constructor_elt, gc, 1);
445 ce = VEC_quick_push (constructor_elt, v, NULL);
447 /* If this is a one element array, we just use a regular init. */
448 if (tree_int_cst_equal (size_zero_node, max_index))
449 ce->index = size_zero_node;
450 else
451 ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
452 max_index);
454 ce->value = build_value_init (TREE_TYPE (type), complain);
456 if (ce->value == error_mark_node)
457 return error_mark_node;
459 /* We shouldn't have gotten here for anything that would need
460 non-trivial initialization, and gimplify_init_ctor_preeval
461 would need to be fixed to allow it. */
462 gcc_assert (TREE_CODE (ce->value) != TARGET_EXPR
463 && TREE_CODE (ce->value) != AGGR_INIT_EXPR);
466 /* Build a constructor to contain the initializations. */
467 return build_constructor (type, v);
469 else if (TREE_CODE (type) == FUNCTION_TYPE)
471 if (complain & tf_error)
472 error ("value-initialization of function type %qT", type);
473 return error_mark_node;
475 else if (TREE_CODE (type) == REFERENCE_TYPE)
477 if (complain & tf_error)
478 error ("value-initialization of reference type %qT", type);
479 return error_mark_node;
482 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
485 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
486 arguments. If TREE_LIST is void_type_node, an empty initializer
487 list was given; if NULL_TREE no initializer was given. */
489 static void
490 perform_member_init (tree member, tree init)
492 tree decl;
493 tree type = TREE_TYPE (member);
495 /* Effective C++ rule 12 requires that all data members be
496 initialized. */
497 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
498 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
499 "%qD should be initialized in the member initialization list",
500 member);
502 /* Get an lvalue for the data member. */
503 decl = build_class_member_access_expr (current_class_ref, member,
504 /*access_path=*/NULL_TREE,
505 /*preserve_reference=*/true,
506 tf_warning_or_error);
507 if (decl == error_mark_node)
508 return;
510 if (warn_init_self && init && TREE_CODE (init) == TREE_LIST
511 && TREE_CHAIN (init) == NULL_TREE)
513 tree val = TREE_VALUE (init);
514 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
515 && TREE_OPERAND (val, 0) == current_class_ref)
516 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
517 OPT_Wuninitialized, "%qD is initialized with itself",
518 member);
521 if (init == void_type_node)
523 /* mem() means value-initialization. */
524 if (TREE_CODE (type) == ARRAY_TYPE)
526 init = build_vec_init_expr (type, init, tf_warning_or_error);
527 init = build2 (INIT_EXPR, type, decl, init);
528 finish_expr_stmt (init);
530 else
532 tree value = build_value_init (type, tf_warning_or_error);
533 if (value == error_mark_node)
534 return;
535 init = build2 (INIT_EXPR, type, decl, value);
536 finish_expr_stmt (init);
539 /* Deal with this here, as we will get confused if we try to call the
540 assignment op for an anonymous union. This can happen in a
541 synthesized copy constructor. */
542 else if (ANON_AGGR_TYPE_P (type))
544 if (init)
546 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
547 finish_expr_stmt (init);
550 else if (type_build_ctor_call (type)
551 || (init && CLASS_TYPE_P (strip_array_types (type))))
553 if (TREE_CODE (type) == ARRAY_TYPE)
555 if (init)
557 if (TREE_CHAIN (init))
558 init = error_mark_node;
559 else
560 init = TREE_VALUE (init);
561 if (BRACE_ENCLOSED_INITIALIZER_P (init))
562 init = digest_init (type, init, tf_warning_or_error);
564 if (init == NULL_TREE
565 || same_type_ignoring_top_level_qualifiers_p (type,
566 TREE_TYPE (init)))
568 init = build_vec_init_expr (type, init, tf_warning_or_error);
569 init = build2 (INIT_EXPR, type, decl, init);
570 finish_expr_stmt (init);
572 else
573 error ("invalid initializer for array member %q#D", member);
575 else
577 int flags = LOOKUP_NORMAL;
578 if (DECL_DEFAULTED_FN (current_function_decl))
579 flags |= LOOKUP_DEFAULTED;
580 if (CP_TYPE_CONST_P (type)
581 && init == NULL_TREE
582 && !type_has_user_provided_default_constructor (type))
583 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
584 vtable; still give this diagnostic. */
585 permerror (DECL_SOURCE_LOCATION (current_function_decl),
586 "uninitialized member %qD with %<const%> type %qT",
587 member, type);
588 finish_expr_stmt (build_aggr_init (decl, init, flags,
589 tf_warning_or_error));
592 else
594 if (init == NULL_TREE)
596 tree core_type;
597 /* member traversal: note it leaves init NULL */
598 if (TREE_CODE (type) == REFERENCE_TYPE)
599 permerror (DECL_SOURCE_LOCATION (current_function_decl),
600 "uninitialized reference member %qD",
601 member);
602 else if (CP_TYPE_CONST_P (type))
603 permerror (DECL_SOURCE_LOCATION (current_function_decl),
604 "uninitialized member %qD with %<const%> type %qT",
605 member, type);
607 core_type = strip_array_types (type);
609 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
610 && !type_has_constexpr_default_constructor (core_type))
612 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
613 error ("uninitialized member %qD in %<constexpr%> constructor",
614 member);
615 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
618 if (CLASS_TYPE_P (core_type)
619 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
620 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
621 diagnose_uninitialized_cst_or_ref_member (core_type,
622 /*using_new=*/false,
623 /*complain=*/true);
625 else if (TREE_CODE (init) == TREE_LIST)
626 /* There was an explicit member initialization. Do some work
627 in that case. */
628 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
629 tf_warning_or_error);
631 if (init)
632 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
633 tf_warning_or_error));
636 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
638 tree expr;
640 expr = build_class_member_access_expr (current_class_ref, member,
641 /*access_path=*/NULL_TREE,
642 /*preserve_reference=*/false,
643 tf_warning_or_error);
644 expr = build_delete (type, expr, sfk_complete_destructor,
645 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
646 tf_warning_or_error);
648 if (expr != error_mark_node)
649 finish_eh_cleanup (expr);
653 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
654 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
656 static tree
657 build_field_list (tree t, tree list, int *uses_unions_p)
659 tree fields;
661 /* Note whether or not T is a union. */
662 if (TREE_CODE (t) == UNION_TYPE)
663 *uses_unions_p = 1;
665 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
667 tree fieldtype;
669 /* Skip CONST_DECLs for enumeration constants and so forth. */
670 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
671 continue;
673 fieldtype = TREE_TYPE (fields);
674 /* Keep track of whether or not any fields are unions. */
675 if (TREE_CODE (fieldtype) == UNION_TYPE)
676 *uses_unions_p = 1;
678 /* For an anonymous struct or union, we must recursively
679 consider the fields of the anonymous type. They can be
680 directly initialized from the constructor. */
681 if (ANON_AGGR_TYPE_P (fieldtype))
683 /* Add this field itself. Synthesized copy constructors
684 initialize the entire aggregate. */
685 list = tree_cons (fields, NULL_TREE, list);
686 /* And now add the fields in the anonymous aggregate. */
687 list = build_field_list (fieldtype, list, uses_unions_p);
689 /* Add this field. */
690 else if (DECL_NAME (fields))
691 list = tree_cons (fields, NULL_TREE, list);
694 return list;
697 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
698 a FIELD_DECL or BINFO in T that needs initialization. The
699 TREE_VALUE gives the initializer, or list of initializer arguments.
701 Return a TREE_LIST containing all of the initializations required
702 for T, in the order in which they should be performed. The output
703 list has the same format as the input. */
705 static tree
706 sort_mem_initializers (tree t, tree mem_inits)
708 tree init;
709 tree base, binfo, base_binfo;
710 tree sorted_inits;
711 tree next_subobject;
712 VEC(tree,gc) *vbases;
713 int i;
714 int uses_unions_p = 0;
716 /* Build up a list of initializations. The TREE_PURPOSE of entry
717 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
718 TREE_VALUE will be the constructor arguments, or NULL if no
719 explicit initialization was provided. */
720 sorted_inits = NULL_TREE;
722 /* Process the virtual bases. */
723 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
724 VEC_iterate (tree, vbases, i, base); i++)
725 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
727 /* Process the direct bases. */
728 for (binfo = TYPE_BINFO (t), i = 0;
729 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
730 if (!BINFO_VIRTUAL_P (base_binfo))
731 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
733 /* Process the non-static data members. */
734 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
735 /* Reverse the entire list of initializations, so that they are in
736 the order that they will actually be performed. */
737 sorted_inits = nreverse (sorted_inits);
739 /* If the user presented the initializers in an order different from
740 that in which they will actually occur, we issue a warning. Keep
741 track of the next subobject which can be explicitly initialized
742 without issuing a warning. */
743 next_subobject = sorted_inits;
745 /* Go through the explicit initializers, filling in TREE_PURPOSE in
746 the SORTED_INITS. */
747 for (init = mem_inits; init; init = TREE_CHAIN (init))
749 tree subobject;
750 tree subobject_init;
752 subobject = TREE_PURPOSE (init);
754 /* If the explicit initializers are in sorted order, then
755 SUBOBJECT will be NEXT_SUBOBJECT, or something following
756 it. */
757 for (subobject_init = next_subobject;
758 subobject_init;
759 subobject_init = TREE_CHAIN (subobject_init))
760 if (TREE_PURPOSE (subobject_init) == subobject)
761 break;
763 /* Issue a warning if the explicit initializer order does not
764 match that which will actually occur.
765 ??? Are all these on the correct lines? */
766 if (warn_reorder && !subobject_init)
768 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
769 warning (OPT_Wreorder, "%q+D will be initialized after",
770 TREE_PURPOSE (next_subobject));
771 else
772 warning (OPT_Wreorder, "base %qT will be initialized after",
773 TREE_PURPOSE (next_subobject));
774 if (TREE_CODE (subobject) == FIELD_DECL)
775 warning (OPT_Wreorder, " %q+#D", subobject);
776 else
777 warning (OPT_Wreorder, " base %qT", subobject);
778 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
779 OPT_Wreorder, " when initialized here");
782 /* Look again, from the beginning of the list. */
783 if (!subobject_init)
785 subobject_init = sorted_inits;
786 while (TREE_PURPOSE (subobject_init) != subobject)
787 subobject_init = TREE_CHAIN (subobject_init);
790 /* It is invalid to initialize the same subobject more than
791 once. */
792 if (TREE_VALUE (subobject_init))
794 if (TREE_CODE (subobject) == FIELD_DECL)
795 error_at (DECL_SOURCE_LOCATION (current_function_decl),
796 "multiple initializations given for %qD",
797 subobject);
798 else
799 error_at (DECL_SOURCE_LOCATION (current_function_decl),
800 "multiple initializations given for base %qT",
801 subobject);
804 /* Record the initialization. */
805 TREE_VALUE (subobject_init) = TREE_VALUE (init);
806 next_subobject = subobject_init;
809 /* [class.base.init]
811 If a ctor-initializer specifies more than one mem-initializer for
812 multiple members of the same union (including members of
813 anonymous unions), the ctor-initializer is ill-formed.
815 Here we also splice out uninitialized union members. */
816 if (uses_unions_p)
818 tree last_field = NULL_TREE;
819 tree *p;
820 for (p = &sorted_inits; *p; )
822 tree field;
823 tree ctx;
824 int done;
826 init = *p;
828 field = TREE_PURPOSE (init);
830 /* Skip base classes. */
831 if (TREE_CODE (field) != FIELD_DECL)
832 goto next;
834 /* If this is an anonymous union with no explicit initializer,
835 splice it out. */
836 if (!TREE_VALUE (init) && ANON_UNION_TYPE_P (TREE_TYPE (field)))
837 goto splice;
839 /* See if this field is a member of a union, or a member of a
840 structure contained in a union, etc. */
841 for (ctx = DECL_CONTEXT (field);
842 !same_type_p (ctx, t);
843 ctx = TYPE_CONTEXT (ctx))
844 if (TREE_CODE (ctx) == UNION_TYPE)
845 break;
846 /* If this field is not a member of a union, skip it. */
847 if (TREE_CODE (ctx) != UNION_TYPE)
848 goto next;
850 /* If this union member has no explicit initializer, splice
851 it out. */
852 if (!TREE_VALUE (init))
853 goto splice;
855 /* It's only an error if we have two initializers for the same
856 union type. */
857 if (!last_field)
859 last_field = field;
860 goto next;
863 /* See if LAST_FIELD and the field initialized by INIT are
864 members of the same union. If so, there's a problem,
865 unless they're actually members of the same structure
866 which is itself a member of a union. For example, given:
868 union { struct { int i; int j; }; };
870 initializing both `i' and `j' makes sense. */
871 ctx = DECL_CONTEXT (field);
872 done = 0;
875 tree last_ctx;
877 last_ctx = DECL_CONTEXT (last_field);
878 while (1)
880 if (same_type_p (last_ctx, ctx))
882 if (TREE_CODE (ctx) == UNION_TYPE)
883 error_at (DECL_SOURCE_LOCATION (current_function_decl),
884 "initializations for multiple members of %qT",
885 last_ctx);
886 done = 1;
887 break;
890 if (same_type_p (last_ctx, t))
891 break;
893 last_ctx = TYPE_CONTEXT (last_ctx);
896 /* If we've reached the outermost class, then we're
897 done. */
898 if (same_type_p (ctx, t))
899 break;
901 ctx = TYPE_CONTEXT (ctx);
903 while (!done);
905 last_field = field;
907 next:
908 p = &TREE_CHAIN (*p);
909 continue;
910 splice:
911 *p = TREE_CHAIN (*p);
912 continue;
916 return sorted_inits;
919 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
920 is a TREE_LIST giving the explicit mem-initializer-list for the
921 constructor. The TREE_PURPOSE of each entry is a subobject (a
922 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
923 is a TREE_LIST giving the arguments to the constructor or
924 void_type_node for an empty list of arguments. */
926 void
927 emit_mem_initializers (tree mem_inits)
929 int flags = LOOKUP_NORMAL;
931 /* We will already have issued an error message about the fact that
932 the type is incomplete. */
933 if (!COMPLETE_TYPE_P (current_class_type))
934 return;
936 if (DECL_DEFAULTED_FN (current_function_decl))
937 flags |= LOOKUP_DEFAULTED;
939 /* Sort the mem-initializers into the order in which the
940 initializations should be performed. */
941 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
943 in_base_initializer = 1;
945 /* Initialize base classes. */
946 while (mem_inits
947 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL)
949 tree subobject = TREE_PURPOSE (mem_inits);
950 tree arguments = TREE_VALUE (mem_inits);
952 if (arguments == NULL_TREE)
954 /* If these initializations are taking place in a copy constructor,
955 the base class should probably be explicitly initialized if there
956 is a user-defined constructor in the base class (other than the
957 default constructor, which will be called anyway). */
958 if (extra_warnings
959 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
960 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
961 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
962 OPT_Wextra, "base class %q#T should be explicitly "
963 "initialized in the copy constructor",
964 BINFO_TYPE (subobject));
966 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
967 && !(type_has_constexpr_default_constructor
968 (BINFO_TYPE (subobject))))
970 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
971 error ("uninitialized base %qT in %<constexpr%> constructor",
972 BINFO_TYPE (subobject));
973 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
977 /* Initialize the base. */
978 if (BINFO_VIRTUAL_P (subobject))
979 construct_virtual_base (subobject, arguments);
980 else
982 tree base_addr;
984 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
985 subobject, 1);
986 expand_aggr_init_1 (subobject, NULL_TREE,
987 cp_build_indirect_ref (base_addr, RO_NULL,
988 tf_warning_or_error),
989 arguments,
990 flags,
991 tf_warning_or_error);
992 expand_cleanup_for_base (subobject, NULL_TREE);
995 mem_inits = TREE_CHAIN (mem_inits);
997 in_base_initializer = 0;
999 /* Initialize the vptrs. */
1000 initialize_vtbl_ptrs (current_class_ptr);
1002 /* Initialize the data members. */
1003 while (mem_inits)
1005 perform_member_init (TREE_PURPOSE (mem_inits),
1006 TREE_VALUE (mem_inits));
1007 mem_inits = TREE_CHAIN (mem_inits);
1011 /* Returns the address of the vtable (i.e., the value that should be
1012 assigned to the vptr) for BINFO. */
1014 static tree
1015 build_vtbl_address (tree binfo)
1017 tree binfo_for = binfo;
1018 tree vtbl;
1020 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1021 /* If this is a virtual primary base, then the vtable we want to store
1022 is that for the base this is being used as the primary base of. We
1023 can't simply skip the initialization, because we may be expanding the
1024 inits of a subobject constructor where the virtual base layout
1025 can be different. */
1026 while (BINFO_PRIMARY_P (binfo_for))
1027 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1029 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1030 used. */
1031 vtbl = get_vtbl_decl_for_binfo (binfo_for);
1032 TREE_USED (vtbl) = 1;
1034 /* Now compute the address to use when initializing the vptr. */
1035 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1036 if (TREE_CODE (vtbl) == VAR_DECL)
1037 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1039 return vtbl;
1042 /* This code sets up the virtual function tables appropriate for
1043 the pointer DECL. It is a one-ply initialization.
1045 BINFO is the exact type that DECL is supposed to be. In
1046 multiple inheritance, this might mean "C's A" if C : A, B. */
1048 static void
1049 expand_virtual_init (tree binfo, tree decl)
1051 tree vtbl, vtbl_ptr;
1052 tree vtt_index;
1054 /* Compute the initializer for vptr. */
1055 vtbl = build_vtbl_address (binfo);
1057 /* We may get this vptr from a VTT, if this is a subobject
1058 constructor or subobject destructor. */
1059 vtt_index = BINFO_VPTR_INDEX (binfo);
1060 if (vtt_index)
1062 tree vtbl2;
1063 tree vtt_parm;
1065 /* Compute the value to use, when there's a VTT. */
1066 vtt_parm = current_vtt_parm;
1067 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1068 vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error);
1069 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1071 /* The actual initializer is the VTT value only in the subobject
1072 constructor. In maybe_clone_body we'll substitute NULL for
1073 the vtt_parm in the case of the non-subobject constructor. */
1074 vtbl = build3 (COND_EXPR,
1075 TREE_TYPE (vtbl),
1076 build2 (EQ_EXPR, boolean_type_node,
1077 current_in_charge_parm, integer_zero_node),
1078 vtbl2,
1079 vtbl);
1082 /* Compute the location of the vtpr. */
1083 vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL,
1084 tf_warning_or_error),
1085 TREE_TYPE (binfo));
1086 gcc_assert (vtbl_ptr != error_mark_node);
1088 /* Assign the vtable to the vptr. */
1089 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
1090 finish_expr_stmt (cp_build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl,
1091 tf_warning_or_error));
1094 /* If an exception is thrown in a constructor, those base classes already
1095 constructed must be destroyed. This function creates the cleanup
1096 for BINFO, which has just been constructed. If FLAG is non-NULL,
1097 it is a DECL which is nonzero when this base needs to be
1098 destroyed. */
1100 static void
1101 expand_cleanup_for_base (tree binfo, tree flag)
1103 tree expr;
1105 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1106 return;
1108 /* Call the destructor. */
1109 expr = build_special_member_call (current_class_ref,
1110 base_dtor_identifier,
1111 NULL,
1112 binfo,
1113 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1114 tf_warning_or_error);
1115 if (flag)
1116 expr = fold_build3_loc (input_location,
1117 COND_EXPR, void_type_node,
1118 c_common_truthvalue_conversion (input_location, flag),
1119 expr, integer_zero_node);
1121 finish_eh_cleanup (expr);
1124 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1125 constructor. */
1127 static void
1128 construct_virtual_base (tree vbase, tree arguments)
1130 tree inner_if_stmt;
1131 tree exp;
1132 tree flag;
1134 /* If there are virtual base classes with destructors, we need to
1135 emit cleanups to destroy them if an exception is thrown during
1136 the construction process. These exception regions (i.e., the
1137 period during which the cleanups must occur) begin from the time
1138 the construction is complete to the end of the function. If we
1139 create a conditional block in which to initialize the
1140 base-classes, then the cleanup region for the virtual base begins
1141 inside a block, and ends outside of that block. This situation
1142 confuses the sjlj exception-handling code. Therefore, we do not
1143 create a single conditional block, but one for each
1144 initialization. (That way the cleanup regions always begin
1145 in the outer block.) We trust the back end to figure out
1146 that the FLAG will not change across initializations, and
1147 avoid doing multiple tests. */
1148 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1149 inner_if_stmt = begin_if_stmt ();
1150 finish_if_stmt_cond (flag, inner_if_stmt);
1152 /* Compute the location of the virtual base. If we're
1153 constructing virtual bases, then we must be the most derived
1154 class. Therefore, we don't have to look up the virtual base;
1155 we already know where it is. */
1156 exp = convert_to_base_statically (current_class_ref, vbase);
1158 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1159 LOOKUP_COMPLAIN, tf_warning_or_error);
1160 finish_then_clause (inner_if_stmt);
1161 finish_if_stmt (inner_if_stmt);
1163 expand_cleanup_for_base (vbase, flag);
1166 /* Find the context in which this FIELD can be initialized. */
1168 static tree
1169 initializing_context (tree field)
1171 tree t = DECL_CONTEXT (field);
1173 /* Anonymous union members can be initialized in the first enclosing
1174 non-anonymous union context. */
1175 while (t && ANON_AGGR_TYPE_P (t))
1176 t = TYPE_CONTEXT (t);
1177 return t;
1180 /* Function to give error message if member initialization specification
1181 is erroneous. FIELD is the member we decided to initialize.
1182 TYPE is the type for which the initialization is being performed.
1183 FIELD must be a member of TYPE.
1185 MEMBER_NAME is the name of the member. */
1187 static int
1188 member_init_ok_or_else (tree field, tree type, tree member_name)
1190 if (field == error_mark_node)
1191 return 0;
1192 if (!field)
1194 error ("class %qT does not have any field named %qD", type,
1195 member_name);
1196 return 0;
1198 if (TREE_CODE (field) == VAR_DECL)
1200 error ("%q#D is a static data member; it can only be "
1201 "initialized at its definition",
1202 field);
1203 return 0;
1205 if (TREE_CODE (field) != FIELD_DECL)
1207 error ("%q#D is not a non-static data member of %qT",
1208 field, type);
1209 return 0;
1211 if (initializing_context (field) != type)
1213 error ("class %qT does not have any field named %qD", type,
1214 member_name);
1215 return 0;
1218 return 1;
1221 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1222 is a _TYPE node or TYPE_DECL which names a base for that type.
1223 Check the validity of NAME, and return either the base _TYPE, base
1224 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1225 NULL_TREE and issue a diagnostic.
1227 An old style unnamed direct single base construction is permitted,
1228 where NAME is NULL. */
1230 tree
1231 expand_member_init (tree name)
1233 tree basetype;
1234 tree field;
1236 if (!current_class_ref)
1237 return NULL_TREE;
1239 if (!name)
1241 /* This is an obsolete unnamed base class initializer. The
1242 parser will already have warned about its use. */
1243 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1245 case 0:
1246 error ("unnamed initializer for %qT, which has no base classes",
1247 current_class_type);
1248 return NULL_TREE;
1249 case 1:
1250 basetype = BINFO_TYPE
1251 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1252 break;
1253 default:
1254 error ("unnamed initializer for %qT, which uses multiple inheritance",
1255 current_class_type);
1256 return NULL_TREE;
1259 else if (TYPE_P (name))
1261 basetype = TYPE_MAIN_VARIANT (name);
1262 name = TYPE_NAME (name);
1264 else if (TREE_CODE (name) == TYPE_DECL)
1265 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1266 else
1267 basetype = NULL_TREE;
1269 if (basetype)
1271 tree class_binfo;
1272 tree direct_binfo;
1273 tree virtual_binfo;
1274 int i;
1276 if (current_template_parms)
1277 return basetype;
1279 class_binfo = TYPE_BINFO (current_class_type);
1280 direct_binfo = NULL_TREE;
1281 virtual_binfo = NULL_TREE;
1283 /* Look for a direct base. */
1284 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1285 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1286 break;
1288 /* Look for a virtual base -- unless the direct base is itself
1289 virtual. */
1290 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1291 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1293 /* [class.base.init]
1295 If a mem-initializer-id is ambiguous because it designates
1296 both a direct non-virtual base class and an inherited virtual
1297 base class, the mem-initializer is ill-formed. */
1298 if (direct_binfo && virtual_binfo)
1300 error ("%qD is both a direct base and an indirect virtual base",
1301 basetype);
1302 return NULL_TREE;
1305 if (!direct_binfo && !virtual_binfo)
1307 if (CLASSTYPE_VBASECLASSES (current_class_type))
1308 error ("type %qT is not a direct or virtual base of %qT",
1309 basetype, current_class_type);
1310 else
1311 error ("type %qT is not a direct base of %qT",
1312 basetype, current_class_type);
1313 return NULL_TREE;
1316 return direct_binfo ? direct_binfo : virtual_binfo;
1318 else
1320 if (TREE_CODE (name) == IDENTIFIER_NODE)
1321 field = lookup_field (current_class_type, name, 1, false);
1322 else
1323 field = name;
1325 if (member_init_ok_or_else (field, current_class_type, name))
1326 return field;
1329 return NULL_TREE;
1332 /* This is like `expand_member_init', only it stores one aggregate
1333 value into another.
1335 INIT comes in two flavors: it is either a value which
1336 is to be stored in EXP, or it is a parameter list
1337 to go to a constructor, which will operate on EXP.
1338 If INIT is not a parameter list for a constructor, then set
1339 LOOKUP_ONLYCONVERTING.
1340 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1341 the initializer, if FLAGS is 0, then it is the (init) form.
1342 If `init' is a CONSTRUCTOR, then we emit a warning message,
1343 explaining that such initializations are invalid.
1345 If INIT resolves to a CALL_EXPR which happens to return
1346 something of the type we are looking for, then we know
1347 that we can safely use that call to perform the
1348 initialization.
1350 The virtual function table pointer cannot be set up here, because
1351 we do not really know its type.
1353 This never calls operator=().
1355 When initializing, nothing is CONST.
1357 A default copy constructor may have to be used to perform the
1358 initialization.
1360 A constructor or a conversion operator may have to be used to
1361 perform the initialization, but not both, as it would be ambiguous. */
1363 tree
1364 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1366 tree stmt_expr;
1367 tree compound_stmt;
1368 int destroy_temps;
1369 tree type = TREE_TYPE (exp);
1370 int was_const = TREE_READONLY (exp);
1371 int was_volatile = TREE_THIS_VOLATILE (exp);
1372 int is_global;
1374 if (init == error_mark_node)
1375 return error_mark_node;
1377 TREE_READONLY (exp) = 0;
1378 TREE_THIS_VOLATILE (exp) = 0;
1380 if (init && TREE_CODE (init) != TREE_LIST
1381 && !(BRACE_ENCLOSED_INITIALIZER_P (init)
1382 && CONSTRUCTOR_IS_DIRECT_INIT (init)))
1383 flags |= LOOKUP_ONLYCONVERTING;
1385 if (TREE_CODE (type) == ARRAY_TYPE)
1387 tree itype;
1389 /* An array may not be initialized use the parenthesized
1390 initialization form -- unless the initializer is "()". */
1391 if (init && TREE_CODE (init) == TREE_LIST)
1393 if (complain & tf_error)
1394 error ("bad array initializer");
1395 return error_mark_node;
1397 /* Must arrange to initialize each element of EXP
1398 from elements of INIT. */
1399 itype = init ? TREE_TYPE (init) : NULL_TREE;
1400 if (cv_qualified_p (type))
1401 TREE_TYPE (exp) = cv_unqualified (type);
1402 if (itype && cv_qualified_p (itype))
1403 TREE_TYPE (init) = cv_unqualified (itype);
1404 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1405 /*explicit_value_init_p=*/false,
1406 itype && same_type_p (TREE_TYPE (init),
1407 TREE_TYPE (exp)),
1408 complain);
1409 TREE_READONLY (exp) = was_const;
1410 TREE_THIS_VOLATILE (exp) = was_volatile;
1411 TREE_TYPE (exp) = type;
1412 if (init)
1413 TREE_TYPE (init) = itype;
1414 return stmt_expr;
1417 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1418 /* Just know that we've seen something for this node. */
1419 TREE_USED (exp) = 1;
1421 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1422 destroy_temps = stmts_are_full_exprs_p ();
1423 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1424 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1425 init, LOOKUP_NORMAL|flags, complain);
1426 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1427 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1428 TREE_READONLY (exp) = was_const;
1429 TREE_THIS_VOLATILE (exp) = was_volatile;
1431 return stmt_expr;
1434 static void
1435 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
1436 tsubst_flags_t complain)
1438 tree type = TREE_TYPE (exp);
1439 tree ctor_name;
1441 /* It fails because there may not be a constructor which takes
1442 its own type as the first (or only parameter), but which does
1443 take other types via a conversion. So, if the thing initializing
1444 the expression is a unit element of type X, first try X(X&),
1445 followed by initialization by X. If neither of these work
1446 out, then look hard. */
1447 tree rval;
1448 VEC(tree,gc) *parms;
1450 /* If we have direct-initialization from an initializer list, pull
1451 it out of the TREE_LIST so the code below can see it. */
1452 if (init && TREE_CODE (init) == TREE_LIST
1453 && BRACE_ENCLOSED_INITIALIZER_P (TREE_VALUE (init))
1454 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
1456 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
1457 && TREE_CHAIN (init) == NULL_TREE);
1458 init = TREE_VALUE (init);
1461 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
1462 && CP_AGGREGATE_TYPE_P (type))
1464 /* A brace-enclosed initializer for an aggregate. In C++0x this can
1465 happen for direct-initialization, too. */
1466 init = digest_init (type, init, complain);
1467 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1468 TREE_SIDE_EFFECTS (init) = 1;
1469 finish_expr_stmt (init);
1470 return;
1473 if (init && TREE_CODE (init) != TREE_LIST
1474 && (flags & LOOKUP_ONLYCONVERTING))
1476 /* Base subobjects should only get direct-initialization. */
1477 gcc_assert (true_exp == exp);
1479 if (flags & DIRECT_BIND)
1480 /* Do nothing. We hit this in two cases: Reference initialization,
1481 where we aren't initializing a real variable, so we don't want
1482 to run a new constructor; and catching an exception, where we
1483 have already built up the constructor call so we could wrap it
1484 in an exception region. */;
1485 else
1486 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1488 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1489 /* We need to protect the initialization of a catch parm with a
1490 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1491 around the TARGET_EXPR for the copy constructor. See
1492 initialize_handler_parm. */
1494 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1495 TREE_OPERAND (init, 0));
1496 TREE_TYPE (init) = void_type_node;
1498 else
1499 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1500 TREE_SIDE_EFFECTS (init) = 1;
1501 finish_expr_stmt (init);
1502 return;
1505 if (init == NULL_TREE)
1506 parms = NULL;
1507 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
1509 parms = make_tree_vector ();
1510 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1511 VEC_safe_push (tree, gc, parms, TREE_VALUE (init));
1513 else
1514 parms = make_tree_vector_single (init);
1516 if (true_exp == exp)
1517 ctor_name = complete_ctor_identifier;
1518 else
1519 ctor_name = base_ctor_identifier;
1521 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
1522 complain);
1524 if (parms != NULL)
1525 release_tree_vector (parms);
1527 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
1529 tree fn = get_callee_fndecl (rval);
1530 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
1532 tree e = maybe_constant_init (rval);
1533 if (TREE_CONSTANT (e))
1534 rval = build2 (INIT_EXPR, type, exp, e);
1538 /* FIXME put back convert_to_void? */
1539 if (TREE_SIDE_EFFECTS (rval))
1540 finish_expr_stmt (rval);
1543 /* This function is responsible for initializing EXP with INIT
1544 (if any).
1546 BINFO is the binfo of the type for who we are performing the
1547 initialization. For example, if W is a virtual base class of A and B,
1548 and C : A, B.
1549 If we are initializing B, then W must contain B's W vtable, whereas
1550 were we initializing C, W must contain C's W vtable.
1552 TRUE_EXP is nonzero if it is the true expression being initialized.
1553 In this case, it may be EXP, or may just contain EXP. The reason we
1554 need this is because if EXP is a base element of TRUE_EXP, we
1555 don't necessarily know by looking at EXP where its virtual
1556 baseclass fields should really be pointing. But we do know
1557 from TRUE_EXP. In constructors, we don't know anything about
1558 the value being initialized.
1560 FLAGS is just passed to `build_new_method_call'. See that function
1561 for its description. */
1563 static void
1564 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
1565 tsubst_flags_t complain)
1567 tree type = TREE_TYPE (exp);
1569 gcc_assert (init != error_mark_node && type != error_mark_node);
1570 gcc_assert (building_stmt_list_p ());
1572 /* Use a function returning the desired type to initialize EXP for us.
1573 If the function is a constructor, and its first argument is
1574 NULL_TREE, know that it was meant for us--just slide exp on
1575 in and expand the constructor. Constructors now come
1576 as TARGET_EXPRs. */
1578 if (init && TREE_CODE (exp) == VAR_DECL
1579 && COMPOUND_LITERAL_P (init))
1581 /* If store_init_value returns NULL_TREE, the INIT has been
1582 recorded as the DECL_INITIAL for EXP. That means there's
1583 nothing more we have to do. */
1584 init = store_init_value (exp, init, flags);
1585 if (init)
1586 finish_expr_stmt (init);
1587 return;
1590 /* If an explicit -- but empty -- initializer list was present,
1591 that's value-initialization. */
1592 if (init == void_type_node)
1594 /* If there's a user-provided constructor, we just call that. */
1595 if (type_has_user_provided_constructor (type))
1596 /* Fall through. */;
1597 /* If there isn't, but we still need to call the constructor,
1598 zero out the object first. */
1599 else if (type_build_ctor_call (type))
1601 init = build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
1602 init = build2 (INIT_EXPR, type, exp, init);
1603 finish_expr_stmt (init);
1604 /* And then call the constructor. */
1606 /* If we don't need to mess with the constructor at all,
1607 then just zero out the object and we're done. */
1608 else
1610 init = build2 (INIT_EXPR, type, exp,
1611 build_value_init_noctor (type, complain));
1612 finish_expr_stmt (init);
1613 return;
1615 init = NULL_TREE;
1618 /* We know that expand_default_init can handle everything we want
1619 at this point. */
1620 expand_default_init (binfo, true_exp, exp, init, flags, complain);
1623 /* Report an error if TYPE is not a user-defined, class type. If
1624 OR_ELSE is nonzero, give an error message. */
1627 is_class_type (tree type, int or_else)
1629 if (type == error_mark_node)
1630 return 0;
1632 if (! CLASS_TYPE_P (type))
1634 if (or_else)
1635 error ("%qT is not a class type", type);
1636 return 0;
1638 return 1;
1641 tree
1642 get_type_value (tree name)
1644 if (name == error_mark_node)
1645 return NULL_TREE;
1647 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1648 return IDENTIFIER_TYPE_VALUE (name);
1649 else
1650 return NULL_TREE;
1653 /* Build a reference to a member of an aggregate. This is not a C++
1654 `&', but really something which can have its address taken, and
1655 then act as a pointer to member, for example TYPE :: FIELD can have
1656 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
1657 this expression is the operand of "&".
1659 @@ Prints out lousy diagnostics for operator <typename>
1660 @@ fields.
1662 @@ This function should be rewritten and placed in search.c. */
1664 tree
1665 build_offset_ref (tree type, tree member, bool address_p)
1667 tree decl;
1668 tree basebinfo = NULL_TREE;
1670 /* class templates can come in as TEMPLATE_DECLs here. */
1671 if (TREE_CODE (member) == TEMPLATE_DECL)
1672 return member;
1674 if (dependent_scope_p (type) || type_dependent_expression_p (member))
1675 return build_qualified_name (NULL_TREE, type, member,
1676 /*template_p=*/false);
1678 gcc_assert (TYPE_P (type));
1679 if (! is_class_type (type, 1))
1680 return error_mark_node;
1682 gcc_assert (DECL_P (member) || BASELINK_P (member));
1683 /* Callers should call mark_used before this point. */
1684 gcc_assert (!DECL_P (member) || TREE_USED (member));
1686 type = TYPE_MAIN_VARIANT (type);
1687 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
1689 error ("incomplete type %qT does not have member %qD", type, member);
1690 return error_mark_node;
1693 /* Entities other than non-static members need no further
1694 processing. */
1695 if (TREE_CODE (member) == TYPE_DECL)
1696 return member;
1697 if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL)
1698 return convert_from_reference (member);
1700 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1702 error ("invalid pointer to bit-field %qD", member);
1703 return error_mark_node;
1706 /* Set up BASEBINFO for member lookup. */
1707 decl = maybe_dummy_object (type, &basebinfo);
1709 /* A lot of this logic is now handled in lookup_member. */
1710 if (BASELINK_P (member))
1712 /* Go from the TREE_BASELINK to the member function info. */
1713 tree t = BASELINK_FUNCTIONS (member);
1715 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1717 /* Get rid of a potential OVERLOAD around it. */
1718 t = OVL_CURRENT (t);
1720 /* Unique functions are handled easily. */
1722 /* For non-static member of base class, we need a special rule
1723 for access checking [class.protected]:
1725 If the access is to form a pointer to member, the
1726 nested-name-specifier shall name the derived class
1727 (or any class derived from that class). */
1728 if (address_p && DECL_P (t)
1729 && DECL_NONSTATIC_MEMBER_P (t))
1730 perform_or_defer_access_check (TYPE_BINFO (type), t, t);
1731 else
1732 perform_or_defer_access_check (basebinfo, t, t);
1734 if (DECL_STATIC_FUNCTION_P (t))
1735 return t;
1736 member = t;
1738 else
1739 TREE_TYPE (member) = unknown_type_node;
1741 else if (address_p && TREE_CODE (member) == FIELD_DECL)
1742 /* We need additional test besides the one in
1743 check_accessibility_of_qualified_id in case it is
1744 a pointer to non-static member. */
1745 perform_or_defer_access_check (TYPE_BINFO (type), member, member);
1747 if (!address_p)
1749 /* If MEMBER is non-static, then the program has fallen afoul of
1750 [expr.prim]:
1752 An id-expression that denotes a nonstatic data member or
1753 nonstatic member function of a class can only be used:
1755 -- as part of a class member access (_expr.ref_) in which the
1756 object-expression refers to the member's class or a class
1757 derived from that class, or
1759 -- to form a pointer to member (_expr.unary.op_), or
1761 -- in the body of a nonstatic member function of that class or
1762 of a class derived from that class (_class.mfct.nonstatic_), or
1764 -- in a mem-initializer for a constructor for that class or for
1765 a class derived from that class (_class.base.init_). */
1766 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1768 /* Build a representation of the qualified name suitable
1769 for use as the operand to "&" -- even though the "&" is
1770 not actually present. */
1771 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1772 /* In Microsoft mode, treat a non-static member function as if
1773 it were a pointer-to-member. */
1774 if (flag_ms_extensions)
1776 PTRMEM_OK_P (member) = 1;
1777 return cp_build_addr_expr (member, tf_warning_or_error);
1779 error ("invalid use of non-static member function %qD",
1780 TREE_OPERAND (member, 1));
1781 return error_mark_node;
1783 else if (TREE_CODE (member) == FIELD_DECL)
1785 error ("invalid use of non-static data member %qD", member);
1786 return error_mark_node;
1788 return member;
1791 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1792 PTRMEM_OK_P (member) = 1;
1793 return member;
1796 /* If DECL is a scalar enumeration constant or variable with a
1797 constant initializer, return the initializer (or, its initializers,
1798 recursively); otherwise, return DECL. If INTEGRAL_P, the
1799 initializer is only returned if DECL is an integral
1800 constant-expression. */
1802 static tree
1803 constant_value_1 (tree decl, bool integral_p)
1805 while (TREE_CODE (decl) == CONST_DECL
1806 || (integral_p
1807 ? decl_constant_var_p (decl)
1808 : (TREE_CODE (decl) == VAR_DECL
1809 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
1811 tree init;
1812 /* If DECL is a static data member in a template
1813 specialization, we must instantiate it here. The
1814 initializer for the static data member is not processed
1815 until needed; we need it now. */
1816 mark_used (decl);
1817 mark_rvalue_use (decl);
1818 init = DECL_INITIAL (decl);
1819 if (init == error_mark_node)
1821 if (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
1822 /* Treat the error as a constant to avoid cascading errors on
1823 excessively recursive template instantiation (c++/9335). */
1824 return init;
1825 else
1826 return decl;
1828 /* Initializers in templates are generally expanded during
1829 instantiation, so before that for const int i(2)
1830 INIT is a TREE_LIST with the actual initializer as
1831 TREE_VALUE. */
1832 if (processing_template_decl
1833 && init
1834 && TREE_CODE (init) == TREE_LIST
1835 && TREE_CHAIN (init) == NULL_TREE)
1836 init = TREE_VALUE (init);
1837 if (!init
1838 || !TREE_TYPE (init)
1839 || !TREE_CONSTANT (init)
1840 || (!integral_p
1841 /* Do not return an aggregate constant (of which
1842 string literals are a special case), as we do not
1843 want to make inadvertent copies of such entities,
1844 and we must be sure that their addresses are the
1845 same everywhere. */
1846 && (TREE_CODE (init) == CONSTRUCTOR
1847 || TREE_CODE (init) == STRING_CST)))
1848 break;
1849 decl = unshare_expr (init);
1851 return decl;
1854 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by
1855 constant of integral or enumeration type, then return that value.
1856 These are those variables permitted in constant expressions by
1857 [5.19/1]. */
1859 tree
1860 integral_constant_value (tree decl)
1862 return constant_value_1 (decl, /*integral_p=*/true);
1865 /* A more relaxed version of integral_constant_value, used by the
1866 common C/C++ code and by the C++ front end for optimization
1867 purposes. */
1869 tree
1870 decl_constant_value (tree decl)
1872 return constant_value_1 (decl,
1873 /*integral_p=*/processing_template_decl);
1876 /* Common subroutines of build_new and build_vec_delete. */
1878 /* Call the global __builtin_delete to delete ADDR. */
1880 static tree
1881 build_builtin_delete_call (tree addr)
1883 mark_used (global_delete_fndecl);
1884 return build_call_n (global_delete_fndecl, 1, addr);
1887 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
1888 the type of the object being allocated; otherwise, it's just TYPE.
1889 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
1890 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
1891 a vector of arguments to be provided as arguments to a placement
1892 new operator. This routine performs no semantic checks; it just
1893 creates and returns a NEW_EXPR. */
1895 static tree
1896 build_raw_new_expr (VEC(tree,gc) *placement, tree type, tree nelts,
1897 VEC(tree,gc) *init, int use_global_new)
1899 tree init_list;
1900 tree new_expr;
1902 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
1903 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
1904 permits us to distinguish the case of a missing initializer "new
1905 int" from an empty initializer "new int()". */
1906 if (init == NULL)
1907 init_list = NULL_TREE;
1908 else if (VEC_empty (tree, init))
1909 init_list = void_zero_node;
1910 else
1911 init_list = build_tree_list_vec (init);
1913 new_expr = build4 (NEW_EXPR, build_pointer_type (type),
1914 build_tree_list_vec (placement), type, nelts,
1915 init_list);
1916 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
1917 TREE_SIDE_EFFECTS (new_expr) = 1;
1919 return new_expr;
1922 /* Diagnose uninitialized const members or reference members of type
1923 TYPE. USING_NEW is used to disambiguate the diagnostic between a
1924 new expression without a new-initializer and a declaration. Returns
1925 the error count. */
1927 static int
1928 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
1929 bool using_new, bool complain)
1931 tree field;
1932 int error_count = 0;
1934 if (type_has_user_provided_constructor (type))
1935 return 0;
1937 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1939 tree field_type;
1941 if (TREE_CODE (field) != FIELD_DECL)
1942 continue;
1944 field_type = strip_array_types (TREE_TYPE (field));
1946 if (type_has_user_provided_constructor (field_type))
1947 continue;
1949 if (TREE_CODE (field_type) == REFERENCE_TYPE)
1951 ++ error_count;
1952 if (complain)
1954 if (using_new)
1955 error ("uninitialized reference member in %q#T "
1956 "using %<new%> without new-initializer", origin);
1957 else
1958 error ("uninitialized reference member in %q#T", origin);
1959 inform (DECL_SOURCE_LOCATION (field),
1960 "%qD should be initialized", field);
1964 if (CP_TYPE_CONST_P (field_type))
1966 ++ error_count;
1967 if (complain)
1969 if (using_new)
1970 error ("uninitialized const member in %q#T "
1971 "using %<new%> without new-initializer", origin);
1972 else
1973 error ("uninitialized const member in %q#T", origin);
1974 inform (DECL_SOURCE_LOCATION (field),
1975 "%qD should be initialized", field);
1979 if (CLASS_TYPE_P (field_type))
1980 error_count
1981 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
1982 using_new, complain);
1984 return error_count;
1988 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
1990 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
1993 /* Generate code for a new-expression, including calling the "operator
1994 new" function, initializing the object, and, if an exception occurs
1995 during construction, cleaning up. The arguments are as for
1996 build_raw_new_expr. This may change PLACEMENT and INIT. */
1998 static tree
1999 build_new_1 (VEC(tree,gc) **placement, tree type, tree nelts,
2000 VEC(tree,gc) **init, bool globally_qualified_p,
2001 tsubst_flags_t complain)
2003 tree size, rval;
2004 /* True iff this is a call to "operator new[]" instead of just
2005 "operator new". */
2006 bool array_p = false;
2007 /* If ARRAY_P is true, the element type of the array. This is never
2008 an ARRAY_TYPE; for something like "new int[3][4]", the
2009 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
2010 TYPE. */
2011 tree elt_type;
2012 /* The type of the new-expression. (This type is always a pointer
2013 type.) */
2014 tree pointer_type;
2015 tree non_const_pointer_type;
2016 tree outer_nelts = NULL_TREE;
2017 tree alloc_call, alloc_expr;
2018 /* The address returned by the call to "operator new". This node is
2019 a VAR_DECL and is therefore reusable. */
2020 tree alloc_node;
2021 tree alloc_fn;
2022 tree cookie_expr, init_expr;
2023 int nothrow, check_new;
2024 int use_java_new = 0;
2025 /* If non-NULL, the number of extra bytes to allocate at the
2026 beginning of the storage allocated for an array-new expression in
2027 order to store the number of elements. */
2028 tree cookie_size = NULL_TREE;
2029 tree placement_first;
2030 tree placement_expr = NULL_TREE;
2031 /* True if the function we are calling is a placement allocation
2032 function. */
2033 bool placement_allocation_fn_p;
2034 /* True if the storage must be initialized, either by a constructor
2035 or due to an explicit new-initializer. */
2036 bool is_initialized;
2037 /* The address of the thing allocated, not including any cookie. In
2038 particular, if an array cookie is in use, DATA_ADDR is the
2039 address of the first array element. This node is a VAR_DECL, and
2040 is therefore reusable. */
2041 tree data_addr;
2042 tree init_preeval_expr = NULL_TREE;
2044 if (nelts)
2046 outer_nelts = nelts;
2047 array_p = true;
2049 else if (TREE_CODE (type) == ARRAY_TYPE)
2051 array_p = true;
2052 nelts = array_type_nelts_top (type);
2053 outer_nelts = nelts;
2054 type = TREE_TYPE (type);
2057 /* If our base type is an array, then make sure we know how many elements
2058 it has. */
2059 for (elt_type = type;
2060 TREE_CODE (elt_type) == ARRAY_TYPE;
2061 elt_type = TREE_TYPE (elt_type))
2062 nelts = cp_build_binary_op (input_location,
2063 MULT_EXPR, nelts,
2064 array_type_nelts_top (elt_type),
2065 complain);
2067 if (TREE_CODE (elt_type) == VOID_TYPE)
2069 if (complain & tf_error)
2070 error ("invalid type %<void%> for new");
2071 return error_mark_node;
2074 if (abstract_virtuals_error_sfinae (NULL_TREE, elt_type, complain))
2075 return error_mark_node;
2077 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
2079 if (*init == NULL)
2081 bool maybe_uninitialized_error = false;
2082 /* A program that calls for default-initialization [...] of an
2083 entity of reference type is ill-formed. */
2084 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
2085 maybe_uninitialized_error = true;
2087 /* A new-expression that creates an object of type T initializes
2088 that object as follows:
2089 - If the new-initializer is omitted:
2090 -- If T is a (possibly cv-qualified) non-POD class type
2091 (or array thereof), the object is default-initialized (8.5).
2092 [...]
2093 -- Otherwise, the object created has indeterminate
2094 value. If T is a const-qualified type, or a (possibly
2095 cv-qualified) POD class type (or array thereof)
2096 containing (directly or indirectly) a member of
2097 const-qualified type, the program is ill-formed; */
2099 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
2100 maybe_uninitialized_error = true;
2102 if (maybe_uninitialized_error
2103 && diagnose_uninitialized_cst_or_ref_member (elt_type,
2104 /*using_new=*/true,
2105 complain & tf_error))
2106 return error_mark_node;
2109 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
2110 && !type_has_user_provided_default_constructor (elt_type))
2112 if (complain & tf_error)
2113 error ("uninitialized const in %<new%> of %q#T", elt_type);
2114 return error_mark_node;
2117 size = size_in_bytes (elt_type);
2118 if (array_p)
2119 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2121 alloc_fn = NULL_TREE;
2123 /* If PLACEMENT is a single simple pointer type not passed by
2124 reference, prepare to capture it in a temporary variable. Do
2125 this now, since PLACEMENT will change in the calls below. */
2126 placement_first = NULL_TREE;
2127 if (VEC_length (tree, *placement) == 1
2128 && (TREE_CODE (TREE_TYPE (VEC_index (tree, *placement, 0)))
2129 == POINTER_TYPE))
2130 placement_first = VEC_index (tree, *placement, 0);
2132 /* Allocate the object. */
2133 if (VEC_empty (tree, *placement) && TYPE_FOR_JAVA (elt_type))
2135 tree class_addr;
2136 tree class_decl = build_java_class_ref (elt_type);
2137 static const char alloc_name[] = "_Jv_AllocObject";
2139 if (class_decl == error_mark_node)
2140 return error_mark_node;
2142 use_java_new = 1;
2143 if (!get_global_value_if_present (get_identifier (alloc_name),
2144 &alloc_fn))
2146 if (complain & tf_error)
2147 error ("call to Java constructor with %qs undefined", alloc_name);
2148 return error_mark_node;
2150 else if (really_overloaded_fn (alloc_fn))
2152 if (complain & tf_error)
2153 error ("%qD should never be overloaded", alloc_fn);
2154 return error_mark_node;
2156 alloc_fn = OVL_CURRENT (alloc_fn);
2157 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2158 alloc_call = cp_build_function_call_nary (alloc_fn, complain,
2159 class_addr, NULL_TREE);
2161 else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
2163 error ("Java class %q#T object allocated using placement new", elt_type);
2164 return error_mark_node;
2166 else
2168 tree fnname;
2169 tree fns;
2171 fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
2173 if (!globally_qualified_p
2174 && CLASS_TYPE_P (elt_type)
2175 && (array_p
2176 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
2177 : TYPE_HAS_NEW_OPERATOR (elt_type)))
2179 /* Use a class-specific operator new. */
2180 /* If a cookie is required, add some extra space. */
2181 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
2183 cookie_size = targetm.cxx.get_cookie_size (elt_type);
2184 size = size_binop (PLUS_EXPR, size, cookie_size);
2186 /* Create the argument list. */
2187 VEC_safe_insert (tree, gc, *placement, 0, size);
2188 /* Do name-lookup to find the appropriate operator. */
2189 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
2190 if (fns == NULL_TREE)
2192 if (complain & tf_error)
2193 error ("no suitable %qD found in class %qT", fnname, elt_type);
2194 return error_mark_node;
2196 if (TREE_CODE (fns) == TREE_LIST)
2198 if (complain & tf_error)
2200 error ("request for member %qD is ambiguous", fnname);
2201 print_candidates (fns);
2203 return error_mark_node;
2205 alloc_call = build_new_method_call (build_dummy_object (elt_type),
2206 fns, placement,
2207 /*conversion_path=*/NULL_TREE,
2208 LOOKUP_NORMAL,
2209 &alloc_fn,
2210 complain);
2212 else
2214 /* Use a global operator new. */
2215 /* See if a cookie might be required. */
2216 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
2217 cookie_size = targetm.cxx.get_cookie_size (elt_type);
2218 else
2219 cookie_size = NULL_TREE;
2221 alloc_call = build_operator_new_call (fnname, placement,
2222 &size, &cookie_size,
2223 &alloc_fn);
2227 if (alloc_call == error_mark_node)
2228 return error_mark_node;
2230 gcc_assert (alloc_fn != NULL_TREE);
2232 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
2233 into a temporary variable. */
2234 if (!processing_template_decl
2235 && placement_first != NULL_TREE
2236 && TREE_CODE (alloc_call) == CALL_EXPR
2237 && call_expr_nargs (alloc_call) == 2
2238 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
2239 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))) == POINTER_TYPE)
2241 tree placement_arg = CALL_EXPR_ARG (alloc_call, 1);
2243 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))
2244 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))))
2246 placement_expr = get_target_expr (placement_first);
2247 CALL_EXPR_ARG (alloc_call, 1)
2248 = convert (TREE_TYPE (placement_arg), placement_expr);
2252 /* In the simple case, we can stop now. */
2253 pointer_type = build_pointer_type (type);
2254 if (!cookie_size && !is_initialized)
2255 return build_nop (pointer_type, alloc_call);
2257 /* Store the result of the allocation call in a variable so that we can
2258 use it more than once. */
2259 alloc_expr = get_target_expr (alloc_call);
2260 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2262 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
2263 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
2264 alloc_call = TREE_OPERAND (alloc_call, 1);
2266 /* Now, check to see if this function is actually a placement
2267 allocation function. This can happen even when PLACEMENT is NULL
2268 because we might have something like:
2270 struct S { void* operator new (size_t, int i = 0); };
2272 A call to `new S' will get this allocation function, even though
2273 there is no explicit placement argument. If there is more than
2274 one argument, or there are variable arguments, then this is a
2275 placement allocation function. */
2276 placement_allocation_fn_p
2277 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2278 || varargs_function_p (alloc_fn));
2280 /* Preevaluate the placement args so that we don't reevaluate them for a
2281 placement delete. */
2282 if (placement_allocation_fn_p)
2284 tree inits;
2285 stabilize_call (alloc_call, &inits);
2286 if (inits)
2287 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
2288 alloc_expr);
2291 /* unless an allocation function is declared with an empty excep-
2292 tion-specification (_except.spec_), throw(), it indicates failure to
2293 allocate storage by throwing a bad_alloc exception (clause _except_,
2294 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2295 cation function is declared with an empty exception-specification,
2296 throw(), it returns null to indicate failure to allocate storage and a
2297 non-null pointer otherwise.
2299 So check for a null exception spec on the op new we just called. */
2301 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2302 check_new = (flag_check_new || nothrow) && ! use_java_new;
2304 if (cookie_size)
2306 tree cookie;
2307 tree cookie_ptr;
2308 tree size_ptr_type;
2310 /* Adjust so we're pointing to the start of the object. */
2311 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
2313 /* Store the number of bytes allocated so that we can know how
2314 many elements to destroy later. We use the last sizeof
2315 (size_t) bytes to store the number of elements. */
2316 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
2317 cookie_ptr = fold_build_pointer_plus_loc (input_location,
2318 alloc_node, cookie_ptr);
2319 size_ptr_type = build_pointer_type (sizetype);
2320 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
2321 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2323 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
2325 if (targetm.cxx.cookie_has_size ())
2327 /* Also store the element size. */
2328 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
2329 fold_build1_loc (input_location,
2330 NEGATE_EXPR, sizetype,
2331 size_in_bytes (sizetype)));
2333 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2334 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
2335 size_in_bytes (elt_type));
2336 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
2337 cookie, cookie_expr);
2340 else
2342 cookie_expr = NULL_TREE;
2343 data_addr = alloc_node;
2346 /* Now use a pointer to the type we've actually allocated. */
2348 /* But we want to operate on a non-const version to start with,
2349 since we'll be modifying the elements. */
2350 non_const_pointer_type = build_pointer_type
2351 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
2353 data_addr = fold_convert (non_const_pointer_type, data_addr);
2354 /* Any further uses of alloc_node will want this type, too. */
2355 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
2357 /* Now initialize the allocated object. Note that we preevaluate the
2358 initialization expression, apart from the actual constructor call or
2359 assignment--we do this because we want to delay the allocation as long
2360 as possible in order to minimize the size of the exception region for
2361 placement delete. */
2362 if (is_initialized)
2364 bool stable;
2365 bool explicit_value_init_p = false;
2367 if (*init != NULL && VEC_empty (tree, *init))
2369 *init = NULL;
2370 explicit_value_init_p = true;
2373 if (processing_template_decl && explicit_value_init_p)
2375 /* build_value_init doesn't work in templates, and we don't need
2376 the initializer anyway since we're going to throw it away and
2377 rebuild it at instantiation time, so just build up a single
2378 constructor call to get any appropriate diagnostics. */
2379 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2380 if (type_build_ctor_call (elt_type))
2381 init_expr = build_special_member_call (init_expr,
2382 complete_ctor_identifier,
2383 init, elt_type,
2384 LOOKUP_NORMAL,
2385 complain);
2386 stable = stabilize_init (init_expr, &init_preeval_expr);
2388 else if (array_p)
2390 tree vecinit = NULL_TREE;
2391 if (*init && VEC_length (tree, *init) == 1
2392 && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *init, 0))
2393 && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *init, 0)))
2395 vecinit = VEC_index (tree, *init, 0);
2396 if (CONSTRUCTOR_NELTS (vecinit) == 0)
2397 /* List-value-initialization, leave it alone. */;
2398 else
2400 tree arraytype, domain;
2401 if (TREE_CONSTANT (nelts))
2402 domain = compute_array_index_type (NULL_TREE, nelts,
2403 complain);
2404 else
2406 domain = NULL_TREE;
2407 if (CONSTRUCTOR_NELTS (vecinit) > 0)
2408 warning (0, "non-constant array size in new, unable "
2409 "to verify length of initializer-list");
2411 arraytype = build_cplus_array_type (type, domain);
2412 vecinit = digest_init (arraytype, vecinit, complain);
2415 else if (*init)
2417 if (complain & tf_error)
2418 permerror (input_location,
2419 "parenthesized initializer in array new");
2420 else
2421 return error_mark_node;
2422 vecinit = build_tree_list_vec (*init);
2424 init_expr
2425 = build_vec_init (data_addr,
2426 cp_build_binary_op (input_location,
2427 MINUS_EXPR, outer_nelts,
2428 integer_one_node,
2429 complain),
2430 vecinit,
2431 explicit_value_init_p,
2432 /*from_array=*/0,
2433 complain);
2435 /* An array initialization is stable because the initialization
2436 of each element is a full-expression, so the temporaries don't
2437 leak out. */
2438 stable = true;
2440 else
2442 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2444 if (type_build_ctor_call (type) && !explicit_value_init_p)
2446 init_expr = build_special_member_call (init_expr,
2447 complete_ctor_identifier,
2448 init, elt_type,
2449 LOOKUP_NORMAL,
2450 complain);
2452 else if (explicit_value_init_p)
2454 /* Something like `new int()'. */
2455 tree val = build_value_init (type, complain);
2456 if (val == error_mark_node)
2457 return error_mark_node;
2458 init_expr = build2 (INIT_EXPR, type, init_expr, val);
2460 else
2462 tree ie;
2464 /* We are processing something like `new int (10)', which
2465 means allocate an int, and initialize it with 10. */
2467 ie = build_x_compound_expr_from_vec (*init, "new initializer");
2468 init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie,
2469 complain);
2471 stable = stabilize_init (init_expr, &init_preeval_expr);
2474 if (init_expr == error_mark_node)
2475 return error_mark_node;
2477 /* If any part of the object initialization terminates by throwing an
2478 exception and a suitable deallocation function can be found, the
2479 deallocation function is called to free the memory in which the
2480 object was being constructed, after which the exception continues
2481 to propagate in the context of the new-expression. If no
2482 unambiguous matching deallocation function can be found,
2483 propagating the exception does not cause the object's memory to be
2484 freed. */
2485 if (flag_exceptions && ! use_java_new)
2487 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
2488 tree cleanup;
2490 /* The Standard is unclear here, but the right thing to do
2491 is to use the same method for finding deallocation
2492 functions that we use for finding allocation functions. */
2493 cleanup = (build_op_delete_call
2494 (dcode,
2495 alloc_node,
2496 size,
2497 globally_qualified_p,
2498 placement_allocation_fn_p ? alloc_call : NULL_TREE,
2499 alloc_fn));
2501 if (!cleanup)
2502 /* We're done. */;
2503 else if (stable)
2504 /* This is much simpler if we were able to preevaluate all of
2505 the arguments to the constructor call. */
2507 /* CLEANUP is compiler-generated, so no diagnostics. */
2508 TREE_NO_WARNING (cleanup) = true;
2509 init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
2510 init_expr, cleanup);
2511 /* Likewise, this try-catch is compiler-generated. */
2512 TREE_NO_WARNING (init_expr) = true;
2514 else
2515 /* Ack! First we allocate the memory. Then we set our sentry
2516 variable to true, and expand a cleanup that deletes the
2517 memory if sentry is true. Then we run the constructor, and
2518 finally clear the sentry.
2520 We need to do this because we allocate the space first, so
2521 if there are any temporaries with cleanups in the
2522 constructor args and we weren't able to preevaluate them, we
2523 need this EH region to extend until end of full-expression
2524 to preserve nesting. */
2526 tree end, sentry, begin;
2528 begin = get_target_expr (boolean_true_node);
2529 CLEANUP_EH_ONLY (begin) = 1;
2531 sentry = TARGET_EXPR_SLOT (begin);
2533 /* CLEANUP is compiler-generated, so no diagnostics. */
2534 TREE_NO_WARNING (cleanup) = true;
2536 TARGET_EXPR_CLEANUP (begin)
2537 = build3 (COND_EXPR, void_type_node, sentry,
2538 cleanup, void_zero_node);
2540 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2541 sentry, boolean_false_node);
2543 init_expr
2544 = build2 (COMPOUND_EXPR, void_type_node, begin,
2545 build2 (COMPOUND_EXPR, void_type_node, init_expr,
2546 end));
2547 /* Likewise, this is compiler-generated. */
2548 TREE_NO_WARNING (init_expr) = true;
2552 else
2553 init_expr = NULL_TREE;
2555 /* Now build up the return value in reverse order. */
2557 rval = data_addr;
2559 if (init_expr)
2560 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2561 if (cookie_expr)
2562 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2564 if (rval == data_addr)
2565 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2566 and return the call (which doesn't need to be adjusted). */
2567 rval = TARGET_EXPR_INITIAL (alloc_expr);
2568 else
2570 if (check_new)
2572 tree ifexp = cp_build_binary_op (input_location,
2573 NE_EXPR, alloc_node,
2574 integer_zero_node,
2575 complain);
2576 rval = build_conditional_expr (ifexp, rval, alloc_node,
2577 complain);
2580 /* Perform the allocation before anything else, so that ALLOC_NODE
2581 has been initialized before we start using it. */
2582 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2585 if (init_preeval_expr)
2586 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
2588 /* A new-expression is never an lvalue. */
2589 gcc_assert (!lvalue_p (rval));
2591 return convert (pointer_type, rval);
2594 /* Generate a representation for a C++ "new" expression. *PLACEMENT
2595 is a vector of placement-new arguments (or NULL if none). If NELTS
2596 is NULL, TYPE is the type of the storage to be allocated. If NELTS
2597 is not NULL, then this is an array-new allocation; TYPE is the type
2598 of the elements in the array and NELTS is the number of elements in
2599 the array. *INIT, if non-NULL, is the initializer for the new
2600 object, or an empty vector to indicate an initializer of "()". If
2601 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
2602 rather than just "new". This may change PLACEMENT and INIT. */
2604 tree
2605 build_new (VEC(tree,gc) **placement, tree type, tree nelts,
2606 VEC(tree,gc) **init, int use_global_new, tsubst_flags_t complain)
2608 tree rval;
2609 VEC(tree,gc) *orig_placement = NULL;
2610 tree orig_nelts = NULL_TREE;
2611 VEC(tree,gc) *orig_init = NULL;
2613 if (type == error_mark_node)
2614 return error_mark_node;
2616 if (nelts == NULL_TREE && VEC_length (tree, *init) == 1)
2618 tree auto_node = type_uses_auto (type);
2619 if (auto_node)
2621 tree d_init = VEC_index (tree, *init, 0);
2622 d_init = resolve_nondeduced_context (d_init);
2623 type = do_auto_deduction (type, d_init, auto_node);
2627 if (processing_template_decl)
2629 if (dependent_type_p (type)
2630 || any_type_dependent_arguments_p (*placement)
2631 || (nelts && type_dependent_expression_p (nelts))
2632 || any_type_dependent_arguments_p (*init))
2633 return build_raw_new_expr (*placement, type, nelts, *init,
2634 use_global_new);
2636 orig_placement = make_tree_vector_copy (*placement);
2637 orig_nelts = nelts;
2638 orig_init = make_tree_vector_copy (*init);
2640 make_args_non_dependent (*placement);
2641 if (nelts)
2642 nelts = build_non_dependent_expr (nelts);
2643 make_args_non_dependent (*init);
2646 if (nelts)
2648 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
2650 if (complain & tf_error)
2651 permerror (input_location, "size in array new must have integral type");
2652 else
2653 return error_mark_node;
2655 nelts = mark_rvalue_use (nelts);
2656 nelts = cp_save_expr (cp_convert (sizetype, nelts));
2659 /* ``A reference cannot be created by the new operator. A reference
2660 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2661 returned by new.'' ARM 5.3.3 */
2662 if (TREE_CODE (type) == REFERENCE_TYPE)
2664 if (complain & tf_error)
2665 error ("new cannot be applied to a reference type");
2666 else
2667 return error_mark_node;
2668 type = TREE_TYPE (type);
2671 if (TREE_CODE (type) == FUNCTION_TYPE)
2673 if (complain & tf_error)
2674 error ("new cannot be applied to a function type");
2675 return error_mark_node;
2678 /* The type allocated must be complete. If the new-type-id was
2679 "T[N]" then we are just checking that "T" is complete here, but
2680 that is equivalent, since the value of "N" doesn't matter. */
2681 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2682 return error_mark_node;
2684 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
2685 if (rval == error_mark_node)
2686 return error_mark_node;
2688 if (processing_template_decl)
2690 tree ret = build_raw_new_expr (orig_placement, type, orig_nelts,
2691 orig_init, use_global_new);
2692 release_tree_vector (orig_placement);
2693 release_tree_vector (orig_init);
2694 return ret;
2697 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2698 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2699 TREE_NO_WARNING (rval) = 1;
2701 return rval;
2704 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2706 tree
2707 build_java_class_ref (tree type)
2709 tree name = NULL_TREE, class_decl;
2710 static tree CL_suffix = NULL_TREE;
2711 if (CL_suffix == NULL_TREE)
2712 CL_suffix = get_identifier("class$");
2713 if (jclass_node == NULL_TREE)
2715 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
2716 if (jclass_node == NULL_TREE)
2718 error ("call to Java constructor, while %<jclass%> undefined");
2719 return error_mark_node;
2721 jclass_node = TREE_TYPE (jclass_node);
2724 /* Mangle the class$ field. */
2726 tree field;
2727 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2728 if (DECL_NAME (field) == CL_suffix)
2730 mangle_decl (field);
2731 name = DECL_ASSEMBLER_NAME (field);
2732 break;
2734 if (!field)
2736 error ("can%'t find %<class$%> in %qT", type);
2737 return error_mark_node;
2741 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2742 if (class_decl == NULL_TREE)
2744 class_decl = build_decl (input_location,
2745 VAR_DECL, name, TREE_TYPE (jclass_node));
2746 TREE_STATIC (class_decl) = 1;
2747 DECL_EXTERNAL (class_decl) = 1;
2748 TREE_PUBLIC (class_decl) = 1;
2749 DECL_ARTIFICIAL (class_decl) = 1;
2750 DECL_IGNORED_P (class_decl) = 1;
2751 pushdecl_top_level (class_decl);
2752 make_decl_rtl (class_decl);
2754 return class_decl;
2757 static tree
2758 build_vec_delete_1 (tree base, tree maxindex, tree type,
2759 special_function_kind auto_delete_vec,
2760 int use_global_delete, tsubst_flags_t complain)
2762 tree virtual_size;
2763 tree ptype = build_pointer_type (type = complete_type (type));
2764 tree size_exp = size_in_bytes (type);
2766 /* Temporary variables used by the loop. */
2767 tree tbase, tbase_init;
2769 /* This is the body of the loop that implements the deletion of a
2770 single element, and moves temp variables to next elements. */
2771 tree body;
2773 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2774 tree loop = 0;
2776 /* This is the thing that governs what to do after the loop has run. */
2777 tree deallocate_expr = 0;
2779 /* This is the BIND_EXPR which holds the outermost iterator of the
2780 loop. It is convenient to set this variable up and test it before
2781 executing any other code in the loop.
2782 This is also the containing expression returned by this function. */
2783 tree controller = NULL_TREE;
2784 tree tmp;
2786 /* We should only have 1-D arrays here. */
2787 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
2789 if (base == error_mark_node || maxindex == error_mark_node)
2790 return error_mark_node;
2792 if (! MAYBE_CLASS_TYPE_P (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2793 goto no_destructor;
2795 /* The below is short by the cookie size. */
2796 virtual_size = size_binop (MULT_EXPR, size_exp,
2797 convert (sizetype, maxindex));
2799 tbase = create_temporary_var (ptype);
2800 tbase_init
2801 = cp_build_modify_expr (tbase, NOP_EXPR,
2802 fold_build_pointer_plus_loc (input_location,
2803 fold_convert (ptype,
2804 base),
2805 virtual_size),
2806 complain);
2807 if (tbase_init == error_mark_node)
2808 return error_mark_node;
2809 controller = build3 (BIND_EXPR, void_type_node, tbase,
2810 NULL_TREE, NULL_TREE);
2811 TREE_SIDE_EFFECTS (controller) = 1;
2813 body = build1 (EXIT_EXPR, void_type_node,
2814 build2 (EQ_EXPR, boolean_type_node, tbase,
2815 fold_convert (ptype, base)));
2816 tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp);
2817 tmp = fold_build_pointer_plus (tbase, tmp);
2818 tmp = cp_build_modify_expr (tbase, NOP_EXPR, tmp, complain);
2819 if (tmp == error_mark_node)
2820 return error_mark_node;
2821 body = build_compound_expr (input_location, body, tmp);
2822 tmp = build_delete (ptype, tbase, sfk_complete_destructor,
2823 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
2824 complain);
2825 if (tmp == error_mark_node)
2826 return error_mark_node;
2827 body = build_compound_expr (input_location, body, tmp);
2829 loop = build1 (LOOP_EXPR, void_type_node, body);
2830 loop = build_compound_expr (input_location, tbase_init, loop);
2832 no_destructor:
2833 /* Delete the storage if appropriate. */
2834 if (auto_delete_vec == sfk_deleting_destructor)
2836 tree base_tbd;
2838 /* The below is short by the cookie size. */
2839 virtual_size = size_binop (MULT_EXPR, size_exp,
2840 convert (sizetype, maxindex));
2842 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2843 /* no header */
2844 base_tbd = base;
2845 else
2847 tree cookie_size;
2849 cookie_size = targetm.cxx.get_cookie_size (type);
2850 base_tbd = cp_build_binary_op (input_location,
2851 MINUS_EXPR,
2852 cp_convert (string_type_node,
2853 base),
2854 cookie_size,
2855 complain);
2856 if (base_tbd == error_mark_node)
2857 return error_mark_node;
2858 base_tbd = cp_convert (ptype, base_tbd);
2859 /* True size with header. */
2860 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2863 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
2864 base_tbd, virtual_size,
2865 use_global_delete & 1,
2866 /*placement=*/NULL_TREE,
2867 /*alloc_fn=*/NULL_TREE);
2870 body = loop;
2871 if (!deallocate_expr)
2873 else if (!body)
2874 body = deallocate_expr;
2875 else
2876 body = build_compound_expr (input_location, body, deallocate_expr);
2878 if (!body)
2879 body = integer_zero_node;
2881 /* Outermost wrapper: If pointer is null, punt. */
2882 body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
2883 fold_build2_loc (input_location,
2884 NE_EXPR, boolean_type_node, base,
2885 convert (TREE_TYPE (base),
2886 integer_zero_node)),
2887 body, integer_zero_node);
2888 body = build1 (NOP_EXPR, void_type_node, body);
2890 if (controller)
2892 TREE_OPERAND (controller, 1) = body;
2893 body = controller;
2896 if (TREE_CODE (base) == SAVE_EXPR)
2897 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
2898 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
2900 return convert_to_void (body, ICV_CAST, complain);
2903 /* Create an unnamed variable of the indicated TYPE. */
2905 tree
2906 create_temporary_var (tree type)
2908 tree decl;
2910 decl = build_decl (input_location,
2911 VAR_DECL, NULL_TREE, type);
2912 TREE_USED (decl) = 1;
2913 DECL_ARTIFICIAL (decl) = 1;
2914 DECL_IGNORED_P (decl) = 1;
2915 DECL_CONTEXT (decl) = current_function_decl;
2917 return decl;
2920 /* Create a new temporary variable of the indicated TYPE, initialized
2921 to INIT.
2923 It is not entered into current_binding_level, because that breaks
2924 things when it comes time to do final cleanups (which take place
2925 "outside" the binding contour of the function). */
2927 tree
2928 get_temp_regvar (tree type, tree init)
2930 tree decl;
2932 decl = create_temporary_var (type);
2933 add_decl_expr (decl);
2935 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
2936 tf_warning_or_error));
2938 return decl;
2941 /* `build_vec_init' returns tree structure that performs
2942 initialization of a vector of aggregate types.
2944 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
2945 to the first element, of POINTER_TYPE.
2946 MAXINDEX is the maximum index of the array (one less than the
2947 number of elements). It is only used if BASE is a pointer or
2948 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
2950 INIT is the (possibly NULL) initializer.
2952 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
2953 elements in the array are value-initialized.
2955 FROM_ARRAY is 0 if we should init everything with INIT
2956 (i.e., every element initialized from INIT).
2957 FROM_ARRAY is 1 if we should index into INIT in parallel
2958 with initialization of DECL.
2959 FROM_ARRAY is 2 if we should index into INIT in parallel,
2960 but use assignment instead of initialization. */
2962 tree
2963 build_vec_init (tree base, tree maxindex, tree init,
2964 bool explicit_value_init_p,
2965 int from_array, tsubst_flags_t complain)
2967 tree rval;
2968 tree base2 = NULL_TREE;
2969 tree itype = NULL_TREE;
2970 tree iterator;
2971 /* The type of BASE. */
2972 tree atype = TREE_TYPE (base);
2973 /* The type of an element in the array. */
2974 tree type = TREE_TYPE (atype);
2975 /* The element type reached after removing all outer array
2976 types. */
2977 tree inner_elt_type;
2978 /* The type of a pointer to an element in the array. */
2979 tree ptype;
2980 tree stmt_expr;
2981 tree compound_stmt;
2982 int destroy_temps;
2983 tree try_block = NULL_TREE;
2984 int num_initialized_elts = 0;
2985 bool is_global;
2986 tree const_init = NULL_TREE;
2987 tree obase = base;
2988 bool xvalue = false;
2989 bool errors = false;
2991 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
2992 maxindex = array_type_nelts (atype);
2994 if (maxindex == NULL_TREE || maxindex == error_mark_node)
2995 return error_mark_node;
2997 if (explicit_value_init_p)
2998 gcc_assert (!init);
3000 inner_elt_type = strip_array_types (type);
3002 /* Look through the TARGET_EXPR around a compound literal. */
3003 if (init && TREE_CODE (init) == TARGET_EXPR
3004 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
3005 && from_array != 2)
3006 init = TARGET_EXPR_INITIAL (init);
3008 if (init
3009 && TREE_CODE (atype) == ARRAY_TYPE
3010 && (from_array == 2
3011 ? (!CLASS_TYPE_P (inner_elt_type)
3012 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (inner_elt_type))
3013 : !TYPE_NEEDS_CONSTRUCTING (type))
3014 && ((TREE_CODE (init) == CONSTRUCTOR
3015 /* Don't do this if the CONSTRUCTOR might contain something
3016 that might throw and require us to clean up. */
3017 && (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))
3018 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
3019 || from_array))
3021 /* Do non-default initialization of trivial arrays resulting from
3022 brace-enclosed initializers. In this case, digest_init and
3023 store_constructor will handle the semantics for us. */
3025 stmt_expr = build2 (INIT_EXPR, atype, base, init);
3026 return stmt_expr;
3029 maxindex = cp_convert (ptrdiff_type_node, maxindex);
3030 if (TREE_CODE (atype) == ARRAY_TYPE)
3032 ptype = build_pointer_type (type);
3033 base = cp_convert (ptype, decay_conversion (base));
3035 else
3036 ptype = atype;
3038 /* The code we are generating looks like:
3040 T* t1 = (T*) base;
3041 T* rval = t1;
3042 ptrdiff_t iterator = maxindex;
3043 try {
3044 for (; iterator != -1; --iterator) {
3045 ... initialize *t1 ...
3046 ++t1;
3048 } catch (...) {
3049 ... destroy elements that were constructed ...
3051 rval;
3054 We can omit the try and catch blocks if we know that the
3055 initialization will never throw an exception, or if the array
3056 elements do not have destructors. We can omit the loop completely if
3057 the elements of the array do not have constructors.
3059 We actually wrap the entire body of the above in a STMT_EXPR, for
3060 tidiness.
3062 When copying from array to another, when the array elements have
3063 only trivial copy constructors, we should use __builtin_memcpy
3064 rather than generating a loop. That way, we could take advantage
3065 of whatever cleverness the back end has for dealing with copies
3066 of blocks of memory. */
3068 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
3069 destroy_temps = stmts_are_full_exprs_p ();
3070 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3071 rval = get_temp_regvar (ptype, base);
3072 base = get_temp_regvar (ptype, rval);
3073 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
3075 /* If initializing one array from another, initialize element by
3076 element. We rely upon the below calls to do the argument
3077 checking. Evaluate the initializer before entering the try block. */
3078 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
3080 if (lvalue_kind (init) & clk_rvalueref)
3081 xvalue = true;
3082 base2 = decay_conversion (init);
3083 itype = TREE_TYPE (base2);
3084 base2 = get_temp_regvar (itype, base2);
3085 itype = TREE_TYPE (itype);
3088 /* Protect the entire array initialization so that we can destroy
3089 the partially constructed array if an exception is thrown.
3090 But don't do this if we're assigning. */
3091 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3092 && from_array != 2)
3094 try_block = begin_try_block ();
3097 /* If the initializer is {}, then all elements are initialized from {}.
3098 But for non-classes, that's the same as value-initialization. */
3099 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
3100 && CONSTRUCTOR_NELTS (init) == 0)
3102 if (CLASS_TYPE_P (type))
3103 /* Leave init alone. */;
3104 else
3106 init = NULL_TREE;
3107 explicit_value_init_p = true;
3111 /* Maybe pull out constant value when from_array? */
3113 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
3115 /* Do non-default initialization of non-trivial arrays resulting from
3116 brace-enclosed initializers. */
3117 unsigned HOST_WIDE_INT idx;
3118 tree field, elt;
3119 /* Should we try to create a constant initializer? */
3120 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
3121 && (literal_type_p (inner_elt_type)
3122 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
3123 bool saw_non_const = false;
3124 bool saw_const = false;
3125 /* If we're initializing a static array, we want to do static
3126 initialization of any elements with constant initializers even if
3127 some are non-constant. */
3128 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
3129 VEC(constructor_elt,gc) *new_vec;
3130 from_array = 0;
3132 if (try_const)
3133 new_vec = VEC_alloc (constructor_elt, gc, CONSTRUCTOR_NELTS (init));
3134 else
3135 new_vec = NULL;
3137 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
3139 tree baseref = build1 (INDIRECT_REF, type, base);
3140 tree one_init;
3142 num_initialized_elts++;
3144 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3145 if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3146 one_init = build_aggr_init (baseref, elt, 0, complain);
3147 else
3148 one_init = cp_build_modify_expr (baseref, NOP_EXPR,
3149 elt, complain);
3150 if (one_init == error_mark_node)
3151 errors = true;
3152 if (try_const)
3154 tree e = one_init;
3155 if (TREE_CODE (e) == EXPR_STMT)
3156 e = TREE_OPERAND (e, 0);
3157 if (TREE_CODE (e) == CONVERT_EXPR
3158 && VOID_TYPE_P (TREE_TYPE (e)))
3159 e = TREE_OPERAND (e, 0);
3160 e = maybe_constant_init (e);
3161 if (reduced_constant_expression_p (e))
3163 CONSTRUCTOR_APPEND_ELT (new_vec, field, e);
3164 if (do_static_init)
3165 one_init = NULL_TREE;
3166 else
3167 one_init = build2 (INIT_EXPR, type, baseref, e);
3168 saw_const = true;
3170 else
3172 if (do_static_init)
3173 CONSTRUCTOR_APPEND_ELT (new_vec, field,
3174 build_zero_init (TREE_TYPE (e),
3175 NULL_TREE, true));
3176 saw_non_const = true;
3180 if (one_init)
3181 finish_expr_stmt (one_init);
3182 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3184 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, 0, complain);
3185 if (one_init == error_mark_node)
3186 errors = true;
3187 else
3188 finish_expr_stmt (one_init);
3190 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3191 complain);
3192 if (one_init == error_mark_node)
3193 errors = true;
3194 else
3195 finish_expr_stmt (one_init);
3198 if (try_const)
3200 if (!saw_non_const)
3201 const_init = build_constructor (atype, new_vec);
3202 else if (do_static_init && saw_const)
3203 DECL_INITIAL (obase) = build_constructor (atype, new_vec);
3204 else
3205 VEC_free (constructor_elt, gc, new_vec);
3208 /* Clear out INIT so that we don't get confused below. */
3209 init = NULL_TREE;
3211 else if (from_array)
3213 if (init)
3214 /* OK, we set base2 above. */;
3215 else if (CLASS_TYPE_P (type)
3216 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3218 if (complain & tf_error)
3219 error ("initializer ends prematurely");
3220 errors = true;
3224 /* Now, default-initialize any remaining elements. We don't need to
3225 do that if a) the type does not need constructing, or b) we've
3226 already initialized all the elements.
3228 We do need to keep going if we're copying an array. */
3230 if (from_array
3231 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
3232 && ! (host_integerp (maxindex, 0)
3233 && (num_initialized_elts
3234 == tree_low_cst (maxindex, 0) + 1))))
3236 /* If the ITERATOR is equal to -1, then we don't have to loop;
3237 we've already initialized all the elements. */
3238 tree for_stmt;
3239 tree elt_init;
3240 tree to;
3242 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
3243 finish_for_init_stmt (for_stmt);
3244 finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
3245 build_int_cst (TREE_TYPE (iterator), -1)),
3246 for_stmt);
3247 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3248 complain);
3249 if (elt_init == error_mark_node)
3250 errors = true;
3251 finish_for_expr (elt_init, for_stmt);
3253 to = build1 (INDIRECT_REF, type, base);
3255 if (from_array)
3257 tree from;
3259 if (base2)
3261 from = build1 (INDIRECT_REF, itype, base2);
3262 if (xvalue)
3263 from = move (from);
3265 else
3266 from = NULL_TREE;
3268 if (from_array == 2)
3269 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3270 complain);
3271 else if (type_build_ctor_call (type))
3272 elt_init = build_aggr_init (to, from, 0, complain);
3273 else if (from)
3274 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3275 complain);
3276 else
3277 gcc_unreachable ();
3279 else if (TREE_CODE (type) == ARRAY_TYPE)
3281 if (init != 0)
3282 sorry
3283 ("cannot initialize multi-dimensional array with initializer");
3284 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
3285 0, 0,
3286 explicit_value_init_p,
3287 0, complain);
3289 else if (explicit_value_init_p)
3291 elt_init = build_value_init (type, complain);
3292 if (elt_init != error_mark_node)
3293 elt_init = build2 (INIT_EXPR, type, to, elt_init);
3295 else
3297 gcc_assert (type_build_ctor_call (type) || init);
3298 if (CLASS_TYPE_P (type))
3299 elt_init = build_aggr_init (to, init, 0, complain);
3300 else
3302 if (TREE_CODE (init) == TREE_LIST)
3303 init = build_x_compound_expr_from_list (init, ELK_INIT,
3304 complain);
3305 elt_init = build2 (INIT_EXPR, type, to, init);
3309 if (elt_init == error_mark_node)
3310 errors = true;
3312 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3313 finish_expr_stmt (elt_init);
3314 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3316 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
3317 complain));
3318 if (base2)
3319 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0,
3320 complain));
3322 finish_for_stmt (for_stmt);
3325 /* Make sure to cleanup any partially constructed elements. */
3326 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3327 && from_array != 2)
3329 tree e;
3330 tree m = cp_build_binary_op (input_location,
3331 MINUS_EXPR, maxindex, iterator,
3332 complain);
3334 /* Flatten multi-dimensional array since build_vec_delete only
3335 expects one-dimensional array. */
3336 if (TREE_CODE (type) == ARRAY_TYPE)
3337 m = cp_build_binary_op (input_location,
3338 MULT_EXPR, m,
3339 array_type_nelts_total (type),
3340 complain);
3342 finish_cleanup_try_block (try_block);
3343 e = build_vec_delete_1 (rval, m,
3344 inner_elt_type, sfk_complete_destructor,
3345 /*use_global_delete=*/0, complain);
3346 if (e == error_mark_node)
3347 errors = true;
3348 finish_cleanup (e, try_block);
3351 /* The value of the array initialization is the array itself, RVAL
3352 is a pointer to the first element. */
3353 finish_stmt_expr_expr (rval, stmt_expr);
3355 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
3357 /* Now make the result have the correct type. */
3358 if (TREE_CODE (atype) == ARRAY_TYPE)
3360 atype = build_pointer_type (atype);
3361 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
3362 stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain);
3363 TREE_NO_WARNING (stmt_expr) = 1;
3366 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3368 if (const_init)
3369 return build2 (INIT_EXPR, atype, obase, const_init);
3370 if (errors)
3371 return error_mark_node;
3372 return stmt_expr;
3375 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3376 build_delete. */
3378 static tree
3379 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
3380 tsubst_flags_t complain)
3382 tree name;
3383 tree fn;
3384 switch (dtor_kind)
3386 case sfk_complete_destructor:
3387 name = complete_dtor_identifier;
3388 break;
3390 case sfk_base_destructor:
3391 name = base_dtor_identifier;
3392 break;
3394 case sfk_deleting_destructor:
3395 name = deleting_dtor_identifier;
3396 break;
3398 default:
3399 gcc_unreachable ();
3401 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
3402 return build_new_method_call (exp, fn,
3403 /*args=*/NULL,
3404 /*conversion_path=*/NULL_TREE,
3405 flags,
3406 /*fn_p=*/NULL,
3407 complain);
3410 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3411 ADDR is an expression which yields the store to be destroyed.
3412 AUTO_DELETE is the name of the destructor to call, i.e., either
3413 sfk_complete_destructor, sfk_base_destructor, or
3414 sfk_deleting_destructor.
3416 FLAGS is the logical disjunction of zero or more LOOKUP_
3417 flags. See cp-tree.h for more info. */
3419 tree
3420 build_delete (tree type, tree addr, special_function_kind auto_delete,
3421 int flags, int use_global_delete, tsubst_flags_t complain)
3423 tree expr;
3425 if (addr == error_mark_node)
3426 return error_mark_node;
3428 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3429 set to `error_mark_node' before it gets properly cleaned up. */
3430 if (type == error_mark_node)
3431 return error_mark_node;
3433 type = TYPE_MAIN_VARIANT (type);
3435 addr = mark_rvalue_use (addr);
3437 if (TREE_CODE (type) == POINTER_TYPE)
3439 bool complete_p = true;
3441 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3442 if (TREE_CODE (type) == ARRAY_TYPE)
3443 goto handle_array;
3445 /* We don't want to warn about delete of void*, only other
3446 incomplete types. Deleting other incomplete types
3447 invokes undefined behavior, but it is not ill-formed, so
3448 compile to something that would even do The Right Thing
3449 (TM) should the type have a trivial dtor and no delete
3450 operator. */
3451 if (!VOID_TYPE_P (type))
3453 complete_type (type);
3454 if (!COMPLETE_TYPE_P (type))
3456 if ((complain & tf_warning)
3457 && warning (0, "possible problem detected in invocation of "
3458 "delete operator:"))
3460 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
3461 inform (input_location, "neither the destructor nor the class-specific "
3462 "operator delete will be called, even if they are "
3463 "declared when the class is defined");
3465 complete_p = false;
3467 else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor
3468 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
3469 && TYPE_POLYMORPHIC_P (type))
3471 tree dtor;
3472 dtor = CLASSTYPE_DESTRUCTORS (type);
3473 if (!dtor || !DECL_VINDEX (dtor))
3475 if (CLASSTYPE_PURE_VIRTUALS (type))
3476 warning (OPT_Wdelete_non_virtual_dtor,
3477 "deleting object of abstract class type %qT"
3478 " which has non-virtual destructor"
3479 " will cause undefined behaviour", type);
3480 else
3481 warning (OPT_Wdelete_non_virtual_dtor,
3482 "deleting object of polymorphic class type %qT"
3483 " which has non-virtual destructor"
3484 " might cause undefined behaviour", type);
3488 if (VOID_TYPE_P (type) || !complete_p || !MAYBE_CLASS_TYPE_P (type))
3489 /* Call the builtin operator delete. */
3490 return build_builtin_delete_call (addr);
3491 if (TREE_SIDE_EFFECTS (addr))
3492 addr = save_expr (addr);
3494 /* Throw away const and volatile on target type of addr. */
3495 addr = convert_force (build_pointer_type (type), addr, 0);
3497 else if (TREE_CODE (type) == ARRAY_TYPE)
3499 handle_array:
3501 if (TYPE_DOMAIN (type) == NULL_TREE)
3503 if (complain & tf_error)
3504 error ("unknown array size in delete");
3505 return error_mark_node;
3507 return build_vec_delete (addr, array_type_nelts (type),
3508 auto_delete, use_global_delete, complain);
3510 else
3512 /* Don't check PROTECT here; leave that decision to the
3513 destructor. If the destructor is accessible, call it,
3514 else report error. */
3515 addr = cp_build_addr_expr (addr, complain);
3516 if (addr == error_mark_node)
3517 return error_mark_node;
3518 if (TREE_SIDE_EFFECTS (addr))
3519 addr = save_expr (addr);
3521 addr = convert_force (build_pointer_type (type), addr, 0);
3524 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3526 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3528 if (auto_delete != sfk_deleting_destructor)
3529 return void_zero_node;
3531 return build_op_delete_call (DELETE_EXPR, addr,
3532 cxx_sizeof_nowarn (type),
3533 use_global_delete,
3534 /*placement=*/NULL_TREE,
3535 /*alloc_fn=*/NULL_TREE);
3537 else
3539 tree head = NULL_TREE;
3540 tree do_delete = NULL_TREE;
3541 tree ifexp;
3543 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
3544 lazily_declare_fn (sfk_destructor, type);
3546 /* For `::delete x', we must not use the deleting destructor
3547 since then we would not be sure to get the global `operator
3548 delete'. */
3549 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3551 /* We will use ADDR multiple times so we must save it. */
3552 addr = save_expr (addr);
3553 head = get_target_expr (build_headof (addr));
3554 /* Delete the object. */
3555 do_delete = build_builtin_delete_call (head);
3556 /* Otherwise, treat this like a complete object destructor
3557 call. */
3558 auto_delete = sfk_complete_destructor;
3560 /* If the destructor is non-virtual, there is no deleting
3561 variant. Instead, we must explicitly call the appropriate
3562 `operator delete' here. */
3563 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3564 && auto_delete == sfk_deleting_destructor)
3566 /* We will use ADDR multiple times so we must save it. */
3567 addr = save_expr (addr);
3568 /* Build the call. */
3569 do_delete = build_op_delete_call (DELETE_EXPR,
3570 addr,
3571 cxx_sizeof_nowarn (type),
3572 /*global_p=*/false,
3573 /*placement=*/NULL_TREE,
3574 /*alloc_fn=*/NULL_TREE);
3575 /* Call the complete object destructor. */
3576 auto_delete = sfk_complete_destructor;
3578 else if (auto_delete == sfk_deleting_destructor
3579 && TYPE_GETS_REG_DELETE (type))
3581 /* Make sure we have access to the member op delete, even though
3582 we'll actually be calling it from the destructor. */
3583 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
3584 /*global_p=*/false,
3585 /*placement=*/NULL_TREE,
3586 /*alloc_fn=*/NULL_TREE);
3589 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, complain),
3590 auto_delete, flags, complain);
3591 if (expr == error_mark_node)
3592 return error_mark_node;
3593 if (do_delete)
3594 expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
3596 /* We need to calculate this before the dtor changes the vptr. */
3597 if (head)
3598 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
3600 if (flags & LOOKUP_DESTRUCTOR)
3601 /* Explicit destructor call; don't check for null pointer. */
3602 ifexp = integer_one_node;
3603 else
3605 /* Handle deleting a null pointer. */
3606 ifexp = fold (cp_build_binary_op (input_location,
3607 NE_EXPR, addr, integer_zero_node,
3608 complain));
3609 if (ifexp == error_mark_node)
3610 return error_mark_node;
3613 if (ifexp != integer_one_node)
3614 expr = build3 (COND_EXPR, void_type_node,
3615 ifexp, expr, void_zero_node);
3617 return expr;
3621 /* At the beginning of a destructor, push cleanups that will call the
3622 destructors for our base classes and members.
3624 Called from begin_destructor_body. */
3626 void
3627 push_base_cleanups (void)
3629 tree binfo, base_binfo;
3630 int i;
3631 tree member;
3632 tree expr;
3633 VEC(tree,gc) *vbases;
3635 /* Run destructors for all virtual baseclasses. */
3636 if (CLASSTYPE_VBASECLASSES (current_class_type))
3638 tree cond = (condition_conversion
3639 (build2 (BIT_AND_EXPR, integer_type_node,
3640 current_in_charge_parm,
3641 integer_two_node)));
3643 /* The CLASSTYPE_VBASECLASSES vector is in initialization
3644 order, which is also the right order for pushing cleanups. */
3645 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
3646 VEC_iterate (tree, vbases, i, base_binfo); i++)
3648 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3650 expr = build_special_member_call (current_class_ref,
3651 base_dtor_identifier,
3652 NULL,
3653 base_binfo,
3654 (LOOKUP_NORMAL
3655 | LOOKUP_NONVIRTUAL),
3656 tf_warning_or_error);
3657 expr = build3 (COND_EXPR, void_type_node, cond,
3658 expr, void_zero_node);
3659 finish_decl_cleanup (NULL_TREE, expr);
3664 /* Take care of the remaining baseclasses. */
3665 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3666 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3668 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3669 || BINFO_VIRTUAL_P (base_binfo))
3670 continue;
3672 expr = build_special_member_call (current_class_ref,
3673 base_dtor_identifier,
3674 NULL, base_binfo,
3675 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
3676 tf_warning_or_error);
3677 finish_decl_cleanup (NULL_TREE, expr);
3680 /* Don't automatically destroy union members. */
3681 if (TREE_CODE (current_class_type) == UNION_TYPE)
3682 return;
3684 for (member = TYPE_FIELDS (current_class_type); member;
3685 member = DECL_CHAIN (member))
3687 tree this_type = TREE_TYPE (member);
3688 if (this_type == error_mark_node
3689 || TREE_CODE (member) != FIELD_DECL
3690 || DECL_ARTIFICIAL (member))
3691 continue;
3692 if (ANON_UNION_TYPE_P (this_type))
3693 continue;
3694 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
3696 tree this_member = (build_class_member_access_expr
3697 (current_class_ref, member,
3698 /*access_path=*/NULL_TREE,
3699 /*preserve_reference=*/false,
3700 tf_warning_or_error));
3701 expr = build_delete (this_type, this_member,
3702 sfk_complete_destructor,
3703 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
3704 0, tf_warning_or_error);
3705 finish_decl_cleanup (NULL_TREE, expr);
3710 /* Build a C++ vector delete expression.
3711 MAXINDEX is the number of elements to be deleted.
3712 ELT_SIZE is the nominal size of each element in the vector.
3713 BASE is the expression that should yield the store to be deleted.
3714 This function expands (or synthesizes) these calls itself.
3715 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3717 This also calls delete for virtual baseclasses of elements of the vector.
3719 Update: MAXINDEX is no longer needed. The size can be extracted from the
3720 start of the vector for pointers, and from the type for arrays. We still
3721 use MAXINDEX for arrays because it happens to already have one of the
3722 values we'd have to extract. (We could use MAXINDEX with pointers to
3723 confirm the size, and trap if the numbers differ; not clear that it'd
3724 be worth bothering.) */
3726 tree
3727 build_vec_delete (tree base, tree maxindex,
3728 special_function_kind auto_delete_vec,
3729 int use_global_delete, tsubst_flags_t complain)
3731 tree type;
3732 tree rval;
3733 tree base_init = NULL_TREE;
3735 type = TREE_TYPE (base);
3737 if (TREE_CODE (type) == POINTER_TYPE)
3739 /* Step back one from start of vector, and read dimension. */
3740 tree cookie_addr;
3741 tree size_ptr_type = build_pointer_type (sizetype);
3743 if (TREE_SIDE_EFFECTS (base))
3745 base_init = get_target_expr (base);
3746 base = TARGET_EXPR_SLOT (base_init);
3748 type = strip_array_types (TREE_TYPE (type));
3749 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
3750 sizetype, TYPE_SIZE_UNIT (sizetype));
3751 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
3752 cookie_addr);
3753 maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, complain);
3755 else if (TREE_CODE (type) == ARRAY_TYPE)
3757 /* Get the total number of things in the array, maxindex is a
3758 bad name. */
3759 maxindex = array_type_nelts_total (type);
3760 type = strip_array_types (type);
3761 base = cp_build_addr_expr (base, complain);
3762 if (base == error_mark_node)
3763 return error_mark_node;
3764 if (TREE_SIDE_EFFECTS (base))
3766 base_init = get_target_expr (base);
3767 base = TARGET_EXPR_SLOT (base_init);
3770 else
3772 if (base != error_mark_node && !(complain & tf_error))
3773 error ("type to vector delete is neither pointer or array type");
3774 return error_mark_node;
3777 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3778 use_global_delete, complain);
3779 if (base_init && rval != error_mark_node)
3780 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
3782 return rval;