/cp
[official-gcc.git] / gcc / cp / init.c
blobd18dc5e8deb900c87cdcfe053c43ec90d709cf30
1 /* Handle initialization things in C++.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* High-level class interface. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "target.h"
32 static bool begin_init_stmts (tree *, tree *);
33 static tree finish_init_stmts (bool, tree, tree);
34 static void construct_virtual_base (tree, tree);
35 static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
36 static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
37 static void perform_member_init (tree, tree);
38 static tree build_builtin_delete_call (tree);
39 static int member_init_ok_or_else (tree, tree, tree);
40 static void expand_virtual_init (tree, tree);
41 static tree sort_mem_initializers (tree, tree);
42 static tree initializing_context (tree);
43 static void expand_cleanup_for_base (tree, tree);
44 static tree dfs_initialize_vtbl_ptrs (tree, void *);
45 static tree build_field_list (tree, tree, int *);
46 static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
48 /* We are about to generate some complex initialization code.
49 Conceptually, it is all a single expression. However, we may want
50 to include conditionals, loops, and other such statement-level
51 constructs. Therefore, we build the initialization code inside a
52 statement-expression. This function starts such an expression.
53 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
54 pass them back to finish_init_stmts when the expression is
55 complete. */
57 static bool
58 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
60 bool is_global = !building_stmt_list_p ();
62 *stmt_expr_p = begin_stmt_expr ();
63 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
65 return is_global;
68 /* Finish out the statement-expression begun by the previous call to
69 begin_init_stmts. Returns the statement-expression itself. */
71 static tree
72 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
74 finish_compound_stmt (compound_stmt);
76 stmt_expr = finish_stmt_expr (stmt_expr, true);
78 gcc_assert (!building_stmt_list_p () == is_global);
80 return stmt_expr;
83 /* Constructors */
85 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
86 which we want to initialize the vtable pointer for, DATA is
87 TREE_LIST whose TREE_VALUE is the this ptr expression. */
89 static tree
90 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
92 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
93 return dfs_skip_bases;
95 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
97 tree base_ptr = TREE_VALUE ((tree) data);
99 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
100 tf_warning_or_error);
102 expand_virtual_init (binfo, base_ptr);
105 return NULL_TREE;
108 /* Initialize all the vtable pointers in the object pointed to by
109 ADDR. */
111 void
112 initialize_vtbl_ptrs (tree addr)
114 tree list;
115 tree type;
117 type = TREE_TYPE (TREE_TYPE (addr));
118 list = build_tree_list (type, addr);
120 /* Walk through the hierarchy, initializing the vptr in each base
121 class. We do these in pre-order because we can't find the virtual
122 bases for a class until we've initialized the vtbl for that
123 class. */
124 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
127 /* Return an expression for the zero-initialization of an object with
128 type T. This expression will either be a constant (in the case
129 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
130 aggregate), or NULL (in the case that T does not require
131 initialization). In either case, the value can be used as
132 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
133 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
134 is the number of elements in the array. If STATIC_STORAGE_P is
135 TRUE, initializers are only generated for entities for which
136 zero-initialization does not simply mean filling the storage with
137 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
138 subfields with bit positions at or above that bit size shouldn't
139 be added. Note that this only works when the result is assigned
140 to a base COMPONENT_REF; if we only have a pointer to the base subobject,
141 expand_assignment will end up clearing the full size of TYPE. */
143 static tree
144 build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
145 tree field_size)
147 tree init = NULL_TREE;
149 /* [dcl.init]
151 To zero-initialize an object of type T means:
153 -- if T is a scalar type, the storage is set to the value of zero
154 converted to T.
156 -- if T is a non-union class type, the storage for each nonstatic
157 data member and each base-class subobject is zero-initialized.
159 -- if T is a union type, the storage for its first data member is
160 zero-initialized.
162 -- if T is an array type, the storage for each element is
163 zero-initialized.
165 -- if T is a reference type, no initialization is performed. */
167 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
169 if (type == error_mark_node)
171 else if (static_storage_p && zero_init_p (type))
172 /* In order to save space, we do not explicitly build initializers
173 for items that do not need them. GCC's semantics are that
174 items with static storage duration that are not otherwise
175 initialized are initialized to zero. */
177 else if (TYPE_PTR_OR_PTRMEM_P (type))
178 init = convert (type, nullptr_node);
179 else if (SCALAR_TYPE_P (type))
180 init = convert (type, integer_zero_node);
181 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
183 tree field;
184 vec<constructor_elt, va_gc> *v = NULL;
186 /* Iterate over the fields, building initializations. */
187 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
189 if (TREE_CODE (field) != FIELD_DECL)
190 continue;
192 /* Don't add virtual bases for base classes if they are beyond
193 the size of the current field, that means it is present
194 somewhere else in the object. */
195 if (field_size)
197 tree bitpos = bit_position (field);
198 if (TREE_CODE (bitpos) == INTEGER_CST
199 && !tree_int_cst_lt (bitpos, field_size))
200 continue;
203 /* Note that for class types there will be FIELD_DECLs
204 corresponding to base classes as well. Thus, iterating
205 over TYPE_FIELDs will result in correct initialization of
206 all of the subobjects. */
207 if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
209 tree new_field_size
210 = (DECL_FIELD_IS_BASE (field)
211 && DECL_SIZE (field)
212 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
213 ? DECL_SIZE (field) : NULL_TREE;
214 tree value = build_zero_init_1 (TREE_TYPE (field),
215 /*nelts=*/NULL_TREE,
216 static_storage_p,
217 new_field_size);
218 if (value)
219 CONSTRUCTOR_APPEND_ELT(v, field, value);
222 /* For unions, only the first field is initialized. */
223 if (TREE_CODE (type) == UNION_TYPE)
224 break;
227 /* Build a constructor to contain the initializations. */
228 init = build_constructor (type, v);
230 else if (TREE_CODE (type) == ARRAY_TYPE)
232 tree max_index;
233 vec<constructor_elt, va_gc> *v = NULL;
235 /* Iterate over the array elements, building initializations. */
236 if (nelts)
237 max_index = fold_build2_loc (input_location,
238 MINUS_EXPR, TREE_TYPE (nelts),
239 nelts, integer_one_node);
240 else
241 max_index = array_type_nelts (type);
243 /* If we have an error_mark here, we should just return error mark
244 as we don't know the size of the array yet. */
245 if (max_index == error_mark_node)
246 return error_mark_node;
247 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
249 /* A zero-sized array, which is accepted as an extension, will
250 have an upper bound of -1. */
251 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
253 constructor_elt ce;
255 /* If this is a one element array, we just use a regular init. */
256 if (tree_int_cst_equal (size_zero_node, max_index))
257 ce.index = size_zero_node;
258 else
259 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
260 max_index);
262 ce.value = build_zero_init_1 (TREE_TYPE (type),
263 /*nelts=*/NULL_TREE,
264 static_storage_p, NULL_TREE);
265 if (ce.value)
267 vec_alloc (v, 1);
268 v->quick_push (ce);
272 /* Build a constructor to contain the initializations. */
273 init = build_constructor (type, v);
275 else if (TREE_CODE (type) == VECTOR_TYPE)
276 init = build_zero_cst (type);
277 else
278 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
280 /* In all cases, the initializer is a constant. */
281 if (init)
282 TREE_CONSTANT (init) = 1;
284 return init;
287 /* Return an expression for the zero-initialization of an object with
288 type T. This expression will either be a constant (in the case
289 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
290 aggregate), or NULL (in the case that T does not require
291 initialization). In either case, the value can be used as
292 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
293 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
294 is the number of elements in the array. If STATIC_STORAGE_P is
295 TRUE, initializers are only generated for entities for which
296 zero-initialization does not simply mean filling the storage with
297 zero bytes. */
299 tree
300 build_zero_init (tree type, tree nelts, bool static_storage_p)
302 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
305 /* Return a suitable initializer for value-initializing an object of type
306 TYPE, as described in [dcl.init]. */
308 tree
309 build_value_init (tree type, tsubst_flags_t complain)
311 /* [dcl.init]
313 To value-initialize an object of type T means:
315 - if T is a class type (clause 9) with a user-provided constructor
316 (12.1), then the default constructor for T is called (and the
317 initialization is ill-formed if T has no accessible default
318 constructor);
320 - if T is a non-union class type without a user-provided constructor,
321 then every non-static data member and base-class component of T is
322 value-initialized;92)
324 - if T is an array type, then each element is value-initialized;
326 - otherwise, the object is zero-initialized.
328 A program that calls for default-initialization or
329 value-initialization of an entity of reference type is ill-formed.
331 92) Value-initialization for such a class object may be implemented by
332 zero-initializing the object and then calling the default
333 constructor. */
335 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
336 gcc_assert (!processing_template_decl
337 || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
339 if (CLASS_TYPE_P (type))
341 /* Instead of the above, only consider the user-providedness of the
342 default constructor itself so value-initializing a class with an
343 explicitly defaulted default constructor and another user-provided
344 constructor works properly (c++std-core-19883). */
345 if (type_has_user_provided_default_constructor (type)
346 || (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
347 && type_has_user_provided_constructor (type)))
348 return build_aggr_init_expr
349 (type,
350 build_special_member_call (NULL_TREE, complete_ctor_identifier,
351 NULL, type, LOOKUP_NORMAL,
352 complain));
353 else if (TYPE_HAS_COMPLEX_DFLT (type))
355 /* This is a class that needs constructing, but doesn't have
356 a user-provided constructor. So we need to zero-initialize
357 the object and then call the implicitly defined ctor.
358 This will be handled in simplify_aggr_init_expr. */
359 tree ctor = build_special_member_call
360 (NULL_TREE, complete_ctor_identifier,
361 NULL, type, LOOKUP_NORMAL, complain);
362 ctor = build_aggr_init_expr (type, ctor);
363 if (ctor != error_mark_node)
364 AGGR_INIT_ZERO_FIRST (ctor) = 1;
365 return ctor;
368 return build_value_init_noctor (type, complain);
371 /* Like build_value_init, but don't call the constructor for TYPE. Used
372 for base initializers. */
374 tree
375 build_value_init_noctor (tree type, tsubst_flags_t complain)
377 if (!COMPLETE_TYPE_P (type))
379 if (complain & tf_error)
380 error ("value-initialization of incomplete type %qT", type);
381 return error_mark_node;
383 /* FIXME the class and array cases should just use digest_init once it is
384 SFINAE-enabled. */
385 if (CLASS_TYPE_P (type))
387 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type));
389 if (TREE_CODE (type) != UNION_TYPE)
391 tree field;
392 vec<constructor_elt, va_gc> *v = NULL;
394 /* Iterate over the fields, building initializations. */
395 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
397 tree ftype, value;
399 if (TREE_CODE (field) != FIELD_DECL)
400 continue;
402 ftype = TREE_TYPE (field);
404 /* We could skip vfields and fields of types with
405 user-defined constructors, but I think that won't improve
406 performance at all; it should be simpler in general just
407 to zero out the entire object than try to only zero the
408 bits that actually need it. */
410 /* Note that for class types there will be FIELD_DECLs
411 corresponding to base classes as well. Thus, iterating
412 over TYPE_FIELDs will result in correct initialization of
413 all of the subobjects. */
414 value = build_value_init (ftype, complain);
416 if (value == error_mark_node)
417 return error_mark_node;
419 if (value)
420 CONSTRUCTOR_APPEND_ELT(v, field, value);
423 /* Build a constructor to contain the zero- initializations. */
424 return build_constructor (type, v);
427 else if (TREE_CODE (type) == ARRAY_TYPE)
429 vec<constructor_elt, va_gc> *v = NULL;
431 /* Iterate over the array elements, building initializations. */
432 tree max_index = array_type_nelts (type);
434 /* If we have an error_mark here, we should just return error mark
435 as we don't know the size of the array yet. */
436 if (max_index == error_mark_node)
438 if (complain & tf_error)
439 error ("cannot value-initialize array of unknown bound %qT",
440 type);
441 return error_mark_node;
443 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
445 /* A zero-sized array, which is accepted as an extension, will
446 have an upper bound of -1. */
447 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
449 constructor_elt ce;
451 /* If this is a one element array, we just use a regular init. */
452 if (tree_int_cst_equal (size_zero_node, max_index))
453 ce.index = size_zero_node;
454 else
455 ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
457 ce.value = build_value_init (TREE_TYPE (type), complain);
458 if (ce.value)
460 if (ce.value == error_mark_node)
461 return error_mark_node;
463 vec_alloc (v, 1);
464 v->quick_push (ce);
466 /* We shouldn't have gotten here for anything that would need
467 non-trivial initialization, and gimplify_init_ctor_preeval
468 would need to be fixed to allow it. */
469 gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
470 && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
474 /* Build a constructor to contain the initializations. */
475 return build_constructor (type, v);
477 else if (TREE_CODE (type) == FUNCTION_TYPE)
479 if (complain & tf_error)
480 error ("value-initialization of function type %qT", type);
481 return error_mark_node;
483 else if (TREE_CODE (type) == REFERENCE_TYPE)
485 if (complain & tf_error)
486 error ("value-initialization of reference type %qT", type);
487 return error_mark_node;
490 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
493 /* Initialize current class with INIT, a TREE_LIST of
494 arguments for a target constructor. If TREE_LIST is void_type_node,
495 an empty initializer list was given. */
497 static void
498 perform_target_ctor (tree init)
500 tree decl = current_class_ref;
501 tree type = current_class_type;
503 finish_expr_stmt (build_aggr_init (decl, init,
504 LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
505 tf_warning_or_error));
506 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
508 tree expr = build_delete (type, decl, sfk_complete_destructor,
509 LOOKUP_NORMAL
510 |LOOKUP_NONVIRTUAL
511 |LOOKUP_DESTRUCTOR,
512 0, tf_warning_or_error);
513 if (expr != error_mark_node)
514 finish_eh_cleanup (expr);
518 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
519 arguments. If TREE_LIST is void_type_node, an empty initializer
520 list was given; if NULL_TREE no initializer was given. */
522 static void
523 perform_member_init (tree member, tree init)
525 tree decl;
526 tree type = TREE_TYPE (member);
528 /* Use the non-static data member initializer if there was no
529 mem-initializer for this field. */
530 if (init == NULL_TREE)
532 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
533 /* Do deferred instantiation of the NSDMI. */
534 init = (tsubst_copy_and_build
535 (DECL_INITIAL (DECL_TI_TEMPLATE (member)),
536 DECL_TI_ARGS (member),
537 tf_warning_or_error, member, /*function_p=*/false,
538 /*integral_constant_expression_p=*/false));
539 else
541 init = DECL_INITIAL (member);
542 if (init && TREE_CODE (init) == DEFAULT_ARG)
544 error ("constructor required before non-static data member "
545 "for %qD has been parsed", member);
546 init = NULL_TREE;
548 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
549 so the aggregate init code below will see a CONSTRUCTOR. */
550 if (init && TREE_CODE (init) == TARGET_EXPR
551 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
552 init = TARGET_EXPR_INITIAL (init);
553 init = break_out_target_exprs (init);
557 if (init == error_mark_node)
558 return;
560 /* Effective C++ rule 12 requires that all data members be
561 initialized. */
562 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
563 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
564 "%qD should be initialized in the member initialization list",
565 member);
567 /* Get an lvalue for the data member. */
568 decl = build_class_member_access_expr (current_class_ref, member,
569 /*access_path=*/NULL_TREE,
570 /*preserve_reference=*/true,
571 tf_warning_or_error);
572 if (decl == error_mark_node)
573 return;
575 if (warn_init_self && init && TREE_CODE (init) == TREE_LIST
576 && TREE_CHAIN (init) == NULL_TREE)
578 tree val = TREE_VALUE (init);
579 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
580 && TREE_OPERAND (val, 0) == current_class_ref)
581 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
582 OPT_Winit_self, "%qD is initialized with itself",
583 member);
586 if (init == void_type_node)
588 /* mem() means value-initialization. */
589 if (TREE_CODE (type) == ARRAY_TYPE)
591 init = build_vec_init_expr (type, init, tf_warning_or_error);
592 init = build2 (INIT_EXPR, type, decl, init);
593 finish_expr_stmt (init);
595 else
597 tree value = build_value_init (type, tf_warning_or_error);
598 if (value == error_mark_node)
599 return;
600 init = build2 (INIT_EXPR, type, decl, value);
601 finish_expr_stmt (init);
604 /* Deal with this here, as we will get confused if we try to call the
605 assignment op for an anonymous union. This can happen in a
606 synthesized copy constructor. */
607 else if (ANON_AGGR_TYPE_P (type))
609 if (init)
611 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
612 finish_expr_stmt (init);
615 else if (init
616 && (TREE_CODE (type) == REFERENCE_TYPE
617 /* Pre-digested NSDMI. */
618 || (((TREE_CODE (init) == CONSTRUCTOR
619 && TREE_TYPE (init) == type)
620 /* { } mem-initializer. */
621 || (TREE_CODE (init) == TREE_LIST
622 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
623 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init))))
624 && (CP_AGGREGATE_TYPE_P (type)
625 || is_std_init_list (type)))))
627 /* With references and list-initialization, we need to deal with
628 extending temporary lifetimes. 12.2p5: "A temporary bound to a
629 reference member in a constructor’s ctor-initializer (12.6.2)
630 persists until the constructor exits." */
631 unsigned i; tree t;
632 vec<tree, va_gc> *cleanups = make_tree_vector ();
633 if (TREE_CODE (init) == TREE_LIST)
634 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
635 tf_warning_or_error);
636 if (TREE_TYPE (init) != type)
638 if (BRACE_ENCLOSED_INITIALIZER_P (init)
639 && CP_AGGREGATE_TYPE_P (type))
640 init = reshape_init (type, init, tf_warning_or_error);
641 init = digest_init (type, init, tf_warning_or_error);
643 if (init == error_mark_node)
644 return;
645 /* A FIELD_DECL doesn't really have a suitable lifetime, but
646 make_temporary_var_for_ref_to_temp will treat it as automatic and
647 set_up_extended_ref_temp wants to use the decl in a warning. */
648 init = extend_ref_init_temps (member, init, &cleanups);
649 if (TREE_CODE (type) == ARRAY_TYPE
650 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
651 init = build_vec_init_expr (type, init, tf_warning_or_error);
652 init = build2 (INIT_EXPR, type, decl, init);
653 finish_expr_stmt (init);
654 FOR_EACH_VEC_ELT (*cleanups, i, t)
655 push_cleanup (decl, t, false);
656 release_tree_vector (cleanups);
658 else if (type_build_ctor_call (type)
659 || (init && CLASS_TYPE_P (strip_array_types (type))))
661 if (TREE_CODE (type) == ARRAY_TYPE)
663 if (init)
665 if (TREE_CHAIN (init))
666 init = error_mark_node;
667 else
668 init = TREE_VALUE (init);
669 if (BRACE_ENCLOSED_INITIALIZER_P (init))
670 init = digest_init (type, init, tf_warning_or_error);
672 if (init == NULL_TREE
673 || same_type_ignoring_top_level_qualifiers_p (type,
674 TREE_TYPE (init)))
676 init = build_vec_init_expr (type, init, tf_warning_or_error);
677 init = build2 (INIT_EXPR, type, decl, init);
678 finish_expr_stmt (init);
680 else
681 error ("invalid initializer for array member %q#D", member);
683 else
685 int flags = LOOKUP_NORMAL;
686 if (DECL_DEFAULTED_FN (current_function_decl))
687 flags |= LOOKUP_DEFAULTED;
688 if (CP_TYPE_CONST_P (type)
689 && init == NULL_TREE
690 && default_init_uninitialized_part (type))
691 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
692 vtable; still give this diagnostic. */
693 permerror (DECL_SOURCE_LOCATION (current_function_decl),
694 "uninitialized member %qD with %<const%> type %qT",
695 member, type);
696 finish_expr_stmt (build_aggr_init (decl, init, flags,
697 tf_warning_or_error));
700 else
702 if (init == NULL_TREE)
704 tree core_type;
705 /* member traversal: note it leaves init NULL */
706 if (TREE_CODE (type) == REFERENCE_TYPE)
707 permerror (DECL_SOURCE_LOCATION (current_function_decl),
708 "uninitialized reference member %qD",
709 member);
710 else if (CP_TYPE_CONST_P (type))
711 permerror (DECL_SOURCE_LOCATION (current_function_decl),
712 "uninitialized member %qD with %<const%> type %qT",
713 member, type);
715 core_type = strip_array_types (type);
717 if (CLASS_TYPE_P (core_type)
718 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
719 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
720 diagnose_uninitialized_cst_or_ref_member (core_type,
721 /*using_new=*/false,
722 /*complain=*/true);
724 else if (TREE_CODE (init) == TREE_LIST)
725 /* There was an explicit member initialization. Do some work
726 in that case. */
727 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
728 tf_warning_or_error);
730 if (init)
731 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
732 tf_warning_or_error));
735 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
737 tree expr;
739 expr = build_class_member_access_expr (current_class_ref, member,
740 /*access_path=*/NULL_TREE,
741 /*preserve_reference=*/false,
742 tf_warning_or_error);
743 expr = build_delete (type, expr, sfk_complete_destructor,
744 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
745 tf_warning_or_error);
747 if (expr != error_mark_node)
748 finish_eh_cleanup (expr);
752 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
753 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
755 static tree
756 build_field_list (tree t, tree list, int *uses_unions_p)
758 tree fields;
760 /* Note whether or not T is a union. */
761 if (TREE_CODE (t) == UNION_TYPE)
762 *uses_unions_p = 1;
764 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
766 tree fieldtype;
768 /* Skip CONST_DECLs for enumeration constants and so forth. */
769 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
770 continue;
772 fieldtype = TREE_TYPE (fields);
773 /* Keep track of whether or not any fields are unions. */
774 if (TREE_CODE (fieldtype) == UNION_TYPE)
775 *uses_unions_p = 1;
777 /* For an anonymous struct or union, we must recursively
778 consider the fields of the anonymous type. They can be
779 directly initialized from the constructor. */
780 if (ANON_AGGR_TYPE_P (fieldtype))
782 /* Add this field itself. Synthesized copy constructors
783 initialize the entire aggregate. */
784 list = tree_cons (fields, NULL_TREE, list);
785 /* And now add the fields in the anonymous aggregate. */
786 list = build_field_list (fieldtype, list, uses_unions_p);
788 /* Add this field. */
789 else if (DECL_NAME (fields))
790 list = tree_cons (fields, NULL_TREE, list);
793 return list;
796 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
797 a FIELD_DECL or BINFO in T that needs initialization. The
798 TREE_VALUE gives the initializer, or list of initializer arguments.
800 Return a TREE_LIST containing all of the initializations required
801 for T, in the order in which they should be performed. The output
802 list has the same format as the input. */
804 static tree
805 sort_mem_initializers (tree t, tree mem_inits)
807 tree init;
808 tree base, binfo, base_binfo;
809 tree sorted_inits;
810 tree next_subobject;
811 vec<tree, va_gc> *vbases;
812 int i;
813 int uses_unions_p = 0;
815 /* Build up a list of initializations. The TREE_PURPOSE of entry
816 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
817 TREE_VALUE will be the constructor arguments, or NULL if no
818 explicit initialization was provided. */
819 sorted_inits = NULL_TREE;
821 /* Process the virtual bases. */
822 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
823 vec_safe_iterate (vbases, i, &base); i++)
824 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
826 /* Process the direct bases. */
827 for (binfo = TYPE_BINFO (t), i = 0;
828 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
829 if (!BINFO_VIRTUAL_P (base_binfo))
830 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
832 /* Process the non-static data members. */
833 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
834 /* Reverse the entire list of initializations, so that they are in
835 the order that they will actually be performed. */
836 sorted_inits = nreverse (sorted_inits);
838 /* If the user presented the initializers in an order different from
839 that in which they will actually occur, we issue a warning. Keep
840 track of the next subobject which can be explicitly initialized
841 without issuing a warning. */
842 next_subobject = sorted_inits;
844 /* Go through the explicit initializers, filling in TREE_PURPOSE in
845 the SORTED_INITS. */
846 for (init = mem_inits; init; init = TREE_CHAIN (init))
848 tree subobject;
849 tree subobject_init;
851 subobject = TREE_PURPOSE (init);
853 /* If the explicit initializers are in sorted order, then
854 SUBOBJECT will be NEXT_SUBOBJECT, or something following
855 it. */
856 for (subobject_init = next_subobject;
857 subobject_init;
858 subobject_init = TREE_CHAIN (subobject_init))
859 if (TREE_PURPOSE (subobject_init) == subobject)
860 break;
862 /* Issue a warning if the explicit initializer order does not
863 match that which will actually occur.
864 ??? Are all these on the correct lines? */
865 if (warn_reorder && !subobject_init)
867 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
868 warning (OPT_Wreorder, "%q+D will be initialized after",
869 TREE_PURPOSE (next_subobject));
870 else
871 warning (OPT_Wreorder, "base %qT will be initialized after",
872 TREE_PURPOSE (next_subobject));
873 if (TREE_CODE (subobject) == FIELD_DECL)
874 warning (OPT_Wreorder, " %q+#D", subobject);
875 else
876 warning (OPT_Wreorder, " base %qT", subobject);
877 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
878 OPT_Wreorder, " when initialized here");
881 /* Look again, from the beginning of the list. */
882 if (!subobject_init)
884 subobject_init = sorted_inits;
885 while (TREE_PURPOSE (subobject_init) != subobject)
886 subobject_init = TREE_CHAIN (subobject_init);
889 /* It is invalid to initialize the same subobject more than
890 once. */
891 if (TREE_VALUE (subobject_init))
893 if (TREE_CODE (subobject) == FIELD_DECL)
894 error_at (DECL_SOURCE_LOCATION (current_function_decl),
895 "multiple initializations given for %qD",
896 subobject);
897 else
898 error_at (DECL_SOURCE_LOCATION (current_function_decl),
899 "multiple initializations given for base %qT",
900 subobject);
903 /* Record the initialization. */
904 TREE_VALUE (subobject_init) = TREE_VALUE (init);
905 next_subobject = subobject_init;
908 /* [class.base.init]
910 If a ctor-initializer specifies more than one mem-initializer for
911 multiple members of the same union (including members of
912 anonymous unions), the ctor-initializer is ill-formed.
914 Here we also splice out uninitialized union members. */
915 if (uses_unions_p)
917 tree *last_p = NULL;
918 tree *p;
919 for (p = &sorted_inits; *p; )
921 tree field;
922 tree ctx;
924 init = *p;
926 field = TREE_PURPOSE (init);
928 /* Skip base classes. */
929 if (TREE_CODE (field) != FIELD_DECL)
930 goto next;
932 /* If this is an anonymous union with no explicit initializer,
933 splice it out. */
934 if (!TREE_VALUE (init) && ANON_UNION_TYPE_P (TREE_TYPE (field)))
935 goto splice;
937 /* See if this field is a member of a union, or a member of a
938 structure contained in a union, etc. */
939 for (ctx = DECL_CONTEXT (field);
940 !same_type_p (ctx, t);
941 ctx = TYPE_CONTEXT (ctx))
942 if (TREE_CODE (ctx) == UNION_TYPE
943 || !ANON_AGGR_TYPE_P (ctx))
944 break;
945 /* If this field is not a member of a union, skip it. */
946 if (TREE_CODE (ctx) != UNION_TYPE)
947 goto next;
949 /* If this union member has no explicit initializer and no NSDMI,
950 splice it out. */
951 if (TREE_VALUE (init) || DECL_INITIAL (field))
952 /* OK. */;
953 else
954 goto splice;
956 /* It's only an error if we have two initializers for the same
957 union type. */
958 if (!last_p)
960 last_p = p;
961 goto next;
964 /* See if LAST_FIELD and the field initialized by INIT are
965 members of the same union. If so, there's a problem,
966 unless they're actually members of the same structure
967 which is itself a member of a union. For example, given:
969 union { struct { int i; int j; }; };
971 initializing both `i' and `j' makes sense. */
972 ctx = common_enclosing_class (DECL_CONTEXT (field),
973 DECL_CONTEXT (TREE_PURPOSE (*last_p)));
975 if (ctx && TREE_CODE (ctx) == UNION_TYPE)
977 /* A mem-initializer hides an NSDMI. */
978 if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
979 *last_p = TREE_CHAIN (*last_p);
980 else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
981 goto splice;
982 else
984 error_at (DECL_SOURCE_LOCATION (current_function_decl),
985 "initializations for multiple members of %qT",
986 ctx);
987 goto splice;
991 last_p = p;
993 next:
994 p = &TREE_CHAIN (*p);
995 continue;
996 splice:
997 *p = TREE_CHAIN (*p);
998 continue;
1002 return sorted_inits;
1005 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1006 is a TREE_LIST giving the explicit mem-initializer-list for the
1007 constructor. The TREE_PURPOSE of each entry is a subobject (a
1008 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1009 is a TREE_LIST giving the arguments to the constructor or
1010 void_type_node for an empty list of arguments. */
1012 void
1013 emit_mem_initializers (tree mem_inits)
1015 int flags = LOOKUP_NORMAL;
1017 /* We will already have issued an error message about the fact that
1018 the type is incomplete. */
1019 if (!COMPLETE_TYPE_P (current_class_type))
1020 return;
1022 if (mem_inits
1023 && TYPE_P (TREE_PURPOSE (mem_inits))
1024 && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1026 /* Delegating constructor. */
1027 gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
1028 perform_target_ctor (TREE_VALUE (mem_inits));
1029 return;
1032 if (DECL_DEFAULTED_FN (current_function_decl)
1033 && ! DECL_INHERITED_CTOR_BASE (current_function_decl))
1034 flags |= LOOKUP_DEFAULTED;
1036 /* Sort the mem-initializers into the order in which the
1037 initializations should be performed. */
1038 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
1040 in_base_initializer = 1;
1042 /* Initialize base classes. */
1043 for (; (mem_inits
1044 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
1045 mem_inits = TREE_CHAIN (mem_inits))
1047 tree subobject = TREE_PURPOSE (mem_inits);
1048 tree arguments = TREE_VALUE (mem_inits);
1050 /* We already have issued an error message. */
1051 if (arguments == error_mark_node)
1052 continue;
1054 if (arguments == NULL_TREE)
1056 /* If these initializations are taking place in a copy constructor,
1057 the base class should probably be explicitly initialized if there
1058 is a user-defined constructor in the base class (other than the
1059 default constructor, which will be called anyway). */
1060 if (extra_warnings
1061 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1062 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1063 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1064 OPT_Wextra, "base class %q#T should be explicitly "
1065 "initialized in the copy constructor",
1066 BINFO_TYPE (subobject));
1069 /* Initialize the base. */
1070 if (BINFO_VIRTUAL_P (subobject))
1071 construct_virtual_base (subobject, arguments);
1072 else
1074 tree base_addr;
1076 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1077 subobject, 1, tf_warning_or_error);
1078 expand_aggr_init_1 (subobject, NULL_TREE,
1079 cp_build_indirect_ref (base_addr, RO_NULL,
1080 tf_warning_or_error),
1081 arguments,
1082 flags,
1083 tf_warning_or_error);
1084 expand_cleanup_for_base (subobject, NULL_TREE);
1087 in_base_initializer = 0;
1089 /* Initialize the vptrs. */
1090 initialize_vtbl_ptrs (current_class_ptr);
1092 /* Initialize the data members. */
1093 while (mem_inits)
1095 perform_member_init (TREE_PURPOSE (mem_inits),
1096 TREE_VALUE (mem_inits));
1097 mem_inits = TREE_CHAIN (mem_inits);
1101 /* Returns the address of the vtable (i.e., the value that should be
1102 assigned to the vptr) for BINFO. */
1104 tree
1105 build_vtbl_address (tree binfo)
1107 tree binfo_for = binfo;
1108 tree vtbl;
1110 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1111 /* If this is a virtual primary base, then the vtable we want to store
1112 is that for the base this is being used as the primary base of. We
1113 can't simply skip the initialization, because we may be expanding the
1114 inits of a subobject constructor where the virtual base layout
1115 can be different. */
1116 while (BINFO_PRIMARY_P (binfo_for))
1117 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1119 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1120 used. */
1121 vtbl = get_vtbl_decl_for_binfo (binfo_for);
1122 TREE_USED (vtbl) = 1;
1124 /* Now compute the address to use when initializing the vptr. */
1125 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1126 if (VAR_P (vtbl))
1127 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1129 return vtbl;
1132 /* This code sets up the virtual function tables appropriate for
1133 the pointer DECL. It is a one-ply initialization.
1135 BINFO is the exact type that DECL is supposed to be. In
1136 multiple inheritance, this might mean "C's A" if C : A, B. */
1138 static void
1139 expand_virtual_init (tree binfo, tree decl)
1141 tree vtbl, vtbl_ptr;
1142 tree vtt_index;
1144 /* Compute the initializer for vptr. */
1145 vtbl = build_vtbl_address (binfo);
1147 /* We may get this vptr from a VTT, if this is a subobject
1148 constructor or subobject destructor. */
1149 vtt_index = BINFO_VPTR_INDEX (binfo);
1150 if (vtt_index)
1152 tree vtbl2;
1153 tree vtt_parm;
1155 /* Compute the value to use, when there's a VTT. */
1156 vtt_parm = current_vtt_parm;
1157 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1158 vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error);
1159 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1161 /* The actual initializer is the VTT value only in the subobject
1162 constructor. In maybe_clone_body we'll substitute NULL for
1163 the vtt_parm in the case of the non-subobject constructor. */
1164 vtbl = build3 (COND_EXPR,
1165 TREE_TYPE (vtbl),
1166 build2 (EQ_EXPR, boolean_type_node,
1167 current_in_charge_parm, integer_zero_node),
1168 vtbl2,
1169 vtbl);
1172 /* Compute the location of the vtpr. */
1173 vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL,
1174 tf_warning_or_error),
1175 TREE_TYPE (binfo));
1176 gcc_assert (vtbl_ptr != error_mark_node);
1178 /* Assign the vtable to the vptr. */
1179 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
1180 finish_expr_stmt (cp_build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl,
1181 tf_warning_or_error));
1184 /* If an exception is thrown in a constructor, those base classes already
1185 constructed must be destroyed. This function creates the cleanup
1186 for BINFO, which has just been constructed. If FLAG is non-NULL,
1187 it is a DECL which is nonzero when this base needs to be
1188 destroyed. */
1190 static void
1191 expand_cleanup_for_base (tree binfo, tree flag)
1193 tree expr;
1195 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1196 return;
1198 /* Call the destructor. */
1199 expr = build_special_member_call (current_class_ref,
1200 base_dtor_identifier,
1201 NULL,
1202 binfo,
1203 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1204 tf_warning_or_error);
1205 if (flag)
1206 expr = fold_build3_loc (input_location,
1207 COND_EXPR, void_type_node,
1208 c_common_truthvalue_conversion (input_location, flag),
1209 expr, integer_zero_node);
1211 finish_eh_cleanup (expr);
1214 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1215 constructor. */
1217 static void
1218 construct_virtual_base (tree vbase, tree arguments)
1220 tree inner_if_stmt;
1221 tree exp;
1222 tree flag;
1224 /* If there are virtual base classes with destructors, we need to
1225 emit cleanups to destroy them if an exception is thrown during
1226 the construction process. These exception regions (i.e., the
1227 period during which the cleanups must occur) begin from the time
1228 the construction is complete to the end of the function. If we
1229 create a conditional block in which to initialize the
1230 base-classes, then the cleanup region for the virtual base begins
1231 inside a block, and ends outside of that block. This situation
1232 confuses the sjlj exception-handling code. Therefore, we do not
1233 create a single conditional block, but one for each
1234 initialization. (That way the cleanup regions always begin
1235 in the outer block.) We trust the back end to figure out
1236 that the FLAG will not change across initializations, and
1237 avoid doing multiple tests. */
1238 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1239 inner_if_stmt = begin_if_stmt ();
1240 finish_if_stmt_cond (flag, inner_if_stmt);
1242 /* Compute the location of the virtual base. If we're
1243 constructing virtual bases, then we must be the most derived
1244 class. Therefore, we don't have to look up the virtual base;
1245 we already know where it is. */
1246 exp = convert_to_base_statically (current_class_ref, vbase);
1248 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1249 0, tf_warning_or_error);
1250 finish_then_clause (inner_if_stmt);
1251 finish_if_stmt (inner_if_stmt);
1253 expand_cleanup_for_base (vbase, flag);
1256 /* Find the context in which this FIELD can be initialized. */
1258 static tree
1259 initializing_context (tree field)
1261 tree t = DECL_CONTEXT (field);
1263 /* Anonymous union members can be initialized in the first enclosing
1264 non-anonymous union context. */
1265 while (t && ANON_AGGR_TYPE_P (t))
1266 t = TYPE_CONTEXT (t);
1267 return t;
1270 /* Function to give error message if member initialization specification
1271 is erroneous. FIELD is the member we decided to initialize.
1272 TYPE is the type for which the initialization is being performed.
1273 FIELD must be a member of TYPE.
1275 MEMBER_NAME is the name of the member. */
1277 static int
1278 member_init_ok_or_else (tree field, tree type, tree member_name)
1280 if (field == error_mark_node)
1281 return 0;
1282 if (!field)
1284 error ("class %qT does not have any field named %qD", type,
1285 member_name);
1286 return 0;
1288 if (VAR_P (field))
1290 error ("%q#D is a static data member; it can only be "
1291 "initialized at its definition",
1292 field);
1293 return 0;
1295 if (TREE_CODE (field) != FIELD_DECL)
1297 error ("%q#D is not a non-static data member of %qT",
1298 field, type);
1299 return 0;
1301 if (initializing_context (field) != type)
1303 error ("class %qT does not have any field named %qD", type,
1304 member_name);
1305 return 0;
1308 return 1;
1311 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1312 is a _TYPE node or TYPE_DECL which names a base for that type.
1313 Check the validity of NAME, and return either the base _TYPE, base
1314 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1315 NULL_TREE and issue a diagnostic.
1317 An old style unnamed direct single base construction is permitted,
1318 where NAME is NULL. */
1320 tree
1321 expand_member_init (tree name)
1323 tree basetype;
1324 tree field;
1326 if (!current_class_ref)
1327 return NULL_TREE;
1329 if (!name)
1331 /* This is an obsolete unnamed base class initializer. The
1332 parser will already have warned about its use. */
1333 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1335 case 0:
1336 error ("unnamed initializer for %qT, which has no base classes",
1337 current_class_type);
1338 return NULL_TREE;
1339 case 1:
1340 basetype = BINFO_TYPE
1341 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1342 break;
1343 default:
1344 error ("unnamed initializer for %qT, which uses multiple inheritance",
1345 current_class_type);
1346 return NULL_TREE;
1349 else if (TYPE_P (name))
1351 basetype = TYPE_MAIN_VARIANT (name);
1352 name = TYPE_NAME (name);
1354 else if (TREE_CODE (name) == TYPE_DECL)
1355 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1356 else
1357 basetype = NULL_TREE;
1359 if (basetype)
1361 tree class_binfo;
1362 tree direct_binfo;
1363 tree virtual_binfo;
1364 int i;
1366 if (current_template_parms
1367 || same_type_p (basetype, current_class_type))
1368 return basetype;
1370 class_binfo = TYPE_BINFO (current_class_type);
1371 direct_binfo = NULL_TREE;
1372 virtual_binfo = NULL_TREE;
1374 /* Look for a direct base. */
1375 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1376 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1377 break;
1379 /* Look for a virtual base -- unless the direct base is itself
1380 virtual. */
1381 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1382 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1384 /* [class.base.init]
1386 If a mem-initializer-id is ambiguous because it designates
1387 both a direct non-virtual base class and an inherited virtual
1388 base class, the mem-initializer is ill-formed. */
1389 if (direct_binfo && virtual_binfo)
1391 error ("%qD is both a direct base and an indirect virtual base",
1392 basetype);
1393 return NULL_TREE;
1396 if (!direct_binfo && !virtual_binfo)
1398 if (CLASSTYPE_VBASECLASSES (current_class_type))
1399 error ("type %qT is not a direct or virtual base of %qT",
1400 basetype, current_class_type);
1401 else
1402 error ("type %qT is not a direct base of %qT",
1403 basetype, current_class_type);
1404 return NULL_TREE;
1407 return direct_binfo ? direct_binfo : virtual_binfo;
1409 else
1411 if (identifier_p (name))
1412 field = lookup_field (current_class_type, name, 1, false);
1413 else
1414 field = name;
1416 if (member_init_ok_or_else (field, current_class_type, name))
1417 return field;
1420 return NULL_TREE;
1423 /* This is like `expand_member_init', only it stores one aggregate
1424 value into another.
1426 INIT comes in two flavors: it is either a value which
1427 is to be stored in EXP, or it is a parameter list
1428 to go to a constructor, which will operate on EXP.
1429 If INIT is not a parameter list for a constructor, then set
1430 LOOKUP_ONLYCONVERTING.
1431 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1432 the initializer, if FLAGS is 0, then it is the (init) form.
1433 If `init' is a CONSTRUCTOR, then we emit a warning message,
1434 explaining that such initializations are invalid.
1436 If INIT resolves to a CALL_EXPR which happens to return
1437 something of the type we are looking for, then we know
1438 that we can safely use that call to perform the
1439 initialization.
1441 The virtual function table pointer cannot be set up here, because
1442 we do not really know its type.
1444 This never calls operator=().
1446 When initializing, nothing is CONST.
1448 A default copy constructor may have to be used to perform the
1449 initialization.
1451 A constructor or a conversion operator may have to be used to
1452 perform the initialization, but not both, as it would be ambiguous. */
1454 tree
1455 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1457 tree stmt_expr;
1458 tree compound_stmt;
1459 int destroy_temps;
1460 tree type = TREE_TYPE (exp);
1461 int was_const = TREE_READONLY (exp);
1462 int was_volatile = TREE_THIS_VOLATILE (exp);
1463 int is_global;
1465 if (init == error_mark_node)
1466 return error_mark_node;
1468 TREE_READONLY (exp) = 0;
1469 TREE_THIS_VOLATILE (exp) = 0;
1471 if (init && init != void_type_node
1472 && TREE_CODE (init) != TREE_LIST
1473 && !(TREE_CODE (init) == TARGET_EXPR
1474 && TARGET_EXPR_DIRECT_INIT_P (init))
1475 && !(BRACE_ENCLOSED_INITIALIZER_P (init)
1476 && CONSTRUCTOR_IS_DIRECT_INIT (init)))
1477 flags |= LOOKUP_ONLYCONVERTING;
1479 if (TREE_CODE (type) == ARRAY_TYPE)
1481 tree itype;
1483 /* An array may not be initialized use the parenthesized
1484 initialization form -- unless the initializer is "()". */
1485 if (init && TREE_CODE (init) == TREE_LIST)
1487 if (complain & tf_error)
1488 error ("bad array initializer");
1489 return error_mark_node;
1491 /* Must arrange to initialize each element of EXP
1492 from elements of INIT. */
1493 itype = init ? TREE_TYPE (init) : NULL_TREE;
1494 if (cv_qualified_p (type))
1495 TREE_TYPE (exp) = cv_unqualified (type);
1496 if (itype && cv_qualified_p (itype))
1497 TREE_TYPE (init) = cv_unqualified (itype);
1498 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1499 /*explicit_value_init_p=*/false,
1500 itype && same_type_p (TREE_TYPE (init),
1501 TREE_TYPE (exp)),
1502 complain);
1503 TREE_READONLY (exp) = was_const;
1504 TREE_THIS_VOLATILE (exp) = was_volatile;
1505 TREE_TYPE (exp) = type;
1506 if (init)
1507 TREE_TYPE (init) = itype;
1508 return stmt_expr;
1511 if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
1512 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
1513 /* Just know that we've seen something for this node. */
1514 TREE_USED (exp) = 1;
1516 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1517 destroy_temps = stmts_are_full_exprs_p ();
1518 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1519 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1520 init, LOOKUP_NORMAL|flags, complain);
1521 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1522 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1523 TREE_READONLY (exp) = was_const;
1524 TREE_THIS_VOLATILE (exp) = was_volatile;
1526 return stmt_expr;
1529 static void
1530 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
1531 tsubst_flags_t complain)
1533 tree type = TREE_TYPE (exp);
1534 tree ctor_name;
1536 /* It fails because there may not be a constructor which takes
1537 its own type as the first (or only parameter), but which does
1538 take other types via a conversion. So, if the thing initializing
1539 the expression is a unit element of type X, first try X(X&),
1540 followed by initialization by X. If neither of these work
1541 out, then look hard. */
1542 tree rval;
1543 vec<tree, va_gc> *parms;
1545 /* If we have direct-initialization from an initializer list, pull
1546 it out of the TREE_LIST so the code below can see it. */
1547 if (init && TREE_CODE (init) == TREE_LIST
1548 && BRACE_ENCLOSED_INITIALIZER_P (TREE_VALUE (init))
1549 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
1551 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
1552 && TREE_CHAIN (init) == NULL_TREE);
1553 init = TREE_VALUE (init);
1556 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
1557 && CP_AGGREGATE_TYPE_P (type))
1558 /* A brace-enclosed initializer for an aggregate. In C++0x this can
1559 happen for direct-initialization, too. */
1560 init = digest_init (type, init, complain);
1562 /* A CONSTRUCTOR of the target's type is a previously digested
1563 initializer, whether that happened just above or in
1564 cp_parser_late_parsing_nsdmi.
1566 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
1567 set represents the whole initialization, so we shouldn't build up
1568 another ctor call. */
1569 if (init
1570 && (TREE_CODE (init) == CONSTRUCTOR
1571 || (TREE_CODE (init) == TARGET_EXPR
1572 && (TARGET_EXPR_DIRECT_INIT_P (init)
1573 || TARGET_EXPR_LIST_INIT_P (init))))
1574 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1576 /* Early initialization via a TARGET_EXPR only works for
1577 complete objects. */
1578 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
1580 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1581 TREE_SIDE_EFFECTS (init) = 1;
1582 finish_expr_stmt (init);
1583 return;
1586 if (init && TREE_CODE (init) != TREE_LIST
1587 && (flags & LOOKUP_ONLYCONVERTING))
1589 /* Base subobjects should only get direct-initialization. */
1590 gcc_assert (true_exp == exp);
1592 if (flags & DIRECT_BIND)
1593 /* Do nothing. We hit this in two cases: Reference initialization,
1594 where we aren't initializing a real variable, so we don't want
1595 to run a new constructor; and catching an exception, where we
1596 have already built up the constructor call so we could wrap it
1597 in an exception region. */;
1598 else
1599 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
1600 flags, complain);
1602 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1603 /* We need to protect the initialization of a catch parm with a
1604 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1605 around the TARGET_EXPR for the copy constructor. See
1606 initialize_handler_parm. */
1608 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1609 TREE_OPERAND (init, 0));
1610 TREE_TYPE (init) = void_type_node;
1612 else
1613 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1614 TREE_SIDE_EFFECTS (init) = 1;
1615 finish_expr_stmt (init);
1616 return;
1619 if (init == NULL_TREE)
1620 parms = NULL;
1621 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
1623 parms = make_tree_vector ();
1624 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1625 vec_safe_push (parms, TREE_VALUE (init));
1627 else
1628 parms = make_tree_vector_single (init);
1630 if (exp == current_class_ref && current_function_decl
1631 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
1633 /* Delegating constructor. */
1634 tree complete;
1635 tree base;
1636 tree elt; unsigned i;
1638 /* Unshare the arguments for the second call. */
1639 vec<tree, va_gc> *parms2 = make_tree_vector ();
1640 FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
1642 elt = break_out_target_exprs (elt);
1643 vec_safe_push (parms2, elt);
1645 complete = build_special_member_call (exp, complete_ctor_identifier,
1646 &parms2, binfo, flags,
1647 complain);
1648 complete = fold_build_cleanup_point_expr (void_type_node, complete);
1649 release_tree_vector (parms2);
1651 base = build_special_member_call (exp, base_ctor_identifier,
1652 &parms, binfo, flags,
1653 complain);
1654 base = fold_build_cleanup_point_expr (void_type_node, base);
1655 rval = build3 (COND_EXPR, void_type_node,
1656 build2 (EQ_EXPR, boolean_type_node,
1657 current_in_charge_parm, integer_zero_node),
1658 base,
1659 complete);
1661 else
1663 if (true_exp == exp)
1664 ctor_name = complete_ctor_identifier;
1665 else
1666 ctor_name = base_ctor_identifier;
1667 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
1668 complain);
1671 if (parms != NULL)
1672 release_tree_vector (parms);
1674 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
1676 tree fn = get_callee_fndecl (rval);
1677 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
1679 tree e = maybe_constant_init (rval);
1680 if (TREE_CONSTANT (e))
1681 rval = build2 (INIT_EXPR, type, exp, e);
1685 /* FIXME put back convert_to_void? */
1686 if (TREE_SIDE_EFFECTS (rval))
1687 finish_expr_stmt (rval);
1690 /* This function is responsible for initializing EXP with INIT
1691 (if any).
1693 BINFO is the binfo of the type for who we are performing the
1694 initialization. For example, if W is a virtual base class of A and B,
1695 and C : A, B.
1696 If we are initializing B, then W must contain B's W vtable, whereas
1697 were we initializing C, W must contain C's W vtable.
1699 TRUE_EXP is nonzero if it is the true expression being initialized.
1700 In this case, it may be EXP, or may just contain EXP. The reason we
1701 need this is because if EXP is a base element of TRUE_EXP, we
1702 don't necessarily know by looking at EXP where its virtual
1703 baseclass fields should really be pointing. But we do know
1704 from TRUE_EXP. In constructors, we don't know anything about
1705 the value being initialized.
1707 FLAGS is just passed to `build_new_method_call'. See that function
1708 for its description. */
1710 static void
1711 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
1712 tsubst_flags_t complain)
1714 tree type = TREE_TYPE (exp);
1716 gcc_assert (init != error_mark_node && type != error_mark_node);
1717 gcc_assert (building_stmt_list_p ());
1719 /* Use a function returning the desired type to initialize EXP for us.
1720 If the function is a constructor, and its first argument is
1721 NULL_TREE, know that it was meant for us--just slide exp on
1722 in and expand the constructor. Constructors now come
1723 as TARGET_EXPRs. */
1725 if (init && VAR_P (exp)
1726 && COMPOUND_LITERAL_P (init))
1728 vec<tree, va_gc> *cleanups = NULL;
1729 /* If store_init_value returns NULL_TREE, the INIT has been
1730 recorded as the DECL_INITIAL for EXP. That means there's
1731 nothing more we have to do. */
1732 init = store_init_value (exp, init, &cleanups, flags);
1733 if (init)
1734 finish_expr_stmt (init);
1735 gcc_assert (!cleanups);
1736 return;
1739 /* If an explicit -- but empty -- initializer list was present,
1740 that's value-initialization. */
1741 if (init == void_type_node)
1743 /* If the type has data but no user-provided ctor, we need to zero
1744 out the object. */
1745 if (!type_has_user_provided_constructor (type)
1746 && !is_really_empty_class (type))
1748 tree field_size = NULL_TREE;
1749 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
1750 /* Don't clobber already initialized virtual bases. */
1751 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
1752 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
1753 field_size);
1754 init = build2 (INIT_EXPR, type, exp, init);
1755 finish_expr_stmt (init);
1758 /* If we don't need to mess with the constructor at all,
1759 then we're done. */
1760 if (! type_build_ctor_call (type))
1761 return;
1763 /* Otherwise fall through and call the constructor. */
1764 init = NULL_TREE;
1767 /* We know that expand_default_init can handle everything we want
1768 at this point. */
1769 expand_default_init (binfo, true_exp, exp, init, flags, complain);
1772 /* Report an error if TYPE is not a user-defined, class type. If
1773 OR_ELSE is nonzero, give an error message. */
1776 is_class_type (tree type, int or_else)
1778 if (type == error_mark_node)
1779 return 0;
1781 if (! CLASS_TYPE_P (type))
1783 if (or_else)
1784 error ("%qT is not a class type", type);
1785 return 0;
1787 return 1;
1790 tree
1791 get_type_value (tree name)
1793 if (name == error_mark_node)
1794 return NULL_TREE;
1796 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1797 return IDENTIFIER_TYPE_VALUE (name);
1798 else
1799 return NULL_TREE;
1802 /* Build a reference to a member of an aggregate. This is not a C++
1803 `&', but really something which can have its address taken, and
1804 then act as a pointer to member, for example TYPE :: FIELD can have
1805 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
1806 this expression is the operand of "&".
1808 @@ Prints out lousy diagnostics for operator <typename>
1809 @@ fields.
1811 @@ This function should be rewritten and placed in search.c. */
1813 tree
1814 build_offset_ref (tree type, tree member, bool address_p,
1815 tsubst_flags_t complain)
1817 tree decl;
1818 tree basebinfo = NULL_TREE;
1820 /* class templates can come in as TEMPLATE_DECLs here. */
1821 if (TREE_CODE (member) == TEMPLATE_DECL)
1822 return member;
1824 if (dependent_scope_p (type) || type_dependent_expression_p (member))
1825 return build_qualified_name (NULL_TREE, type, member,
1826 /*template_p=*/false);
1828 gcc_assert (TYPE_P (type));
1829 if (! is_class_type (type, 1))
1830 return error_mark_node;
1832 gcc_assert (DECL_P (member) || BASELINK_P (member));
1833 /* Callers should call mark_used before this point. */
1834 gcc_assert (!DECL_P (member) || TREE_USED (member));
1836 type = TYPE_MAIN_VARIANT (type);
1837 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
1839 if (complain & tf_error)
1840 error ("incomplete type %qT does not have member %qD", type, member);
1841 return error_mark_node;
1844 /* Entities other than non-static members need no further
1845 processing. */
1846 if (TREE_CODE (member) == TYPE_DECL)
1847 return member;
1848 if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
1849 return convert_from_reference (member);
1851 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1853 if (complain & tf_error)
1854 error ("invalid pointer to bit-field %qD", member);
1855 return error_mark_node;
1858 /* Set up BASEBINFO for member lookup. */
1859 decl = maybe_dummy_object (type, &basebinfo);
1861 /* A lot of this logic is now handled in lookup_member. */
1862 if (BASELINK_P (member))
1864 /* Go from the TREE_BASELINK to the member function info. */
1865 tree t = BASELINK_FUNCTIONS (member);
1867 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1869 /* Get rid of a potential OVERLOAD around it. */
1870 t = OVL_CURRENT (t);
1872 /* Unique functions are handled easily. */
1874 /* For non-static member of base class, we need a special rule
1875 for access checking [class.protected]:
1877 If the access is to form a pointer to member, the
1878 nested-name-specifier shall name the derived class
1879 (or any class derived from that class). */
1880 if (address_p && DECL_P (t)
1881 && DECL_NONSTATIC_MEMBER_P (t))
1882 perform_or_defer_access_check (TYPE_BINFO (type), t, t,
1883 complain);
1884 else
1885 perform_or_defer_access_check (basebinfo, t, t,
1886 complain);
1888 if (DECL_STATIC_FUNCTION_P (t))
1889 return t;
1890 member = t;
1892 else
1893 TREE_TYPE (member) = unknown_type_node;
1895 else if (address_p && TREE_CODE (member) == FIELD_DECL)
1896 /* We need additional test besides the one in
1897 check_accessibility_of_qualified_id in case it is
1898 a pointer to non-static member. */
1899 perform_or_defer_access_check (TYPE_BINFO (type), member, member,
1900 complain);
1902 if (!address_p)
1904 /* If MEMBER is non-static, then the program has fallen afoul of
1905 [expr.prim]:
1907 An id-expression that denotes a nonstatic data member or
1908 nonstatic member function of a class can only be used:
1910 -- as part of a class member access (_expr.ref_) in which the
1911 object-expression refers to the member's class or a class
1912 derived from that class, or
1914 -- to form a pointer to member (_expr.unary.op_), or
1916 -- in the body of a nonstatic member function of that class or
1917 of a class derived from that class (_class.mfct.nonstatic_), or
1919 -- in a mem-initializer for a constructor for that class or for
1920 a class derived from that class (_class.base.init_). */
1921 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1923 /* Build a representation of the qualified name suitable
1924 for use as the operand to "&" -- even though the "&" is
1925 not actually present. */
1926 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1927 /* In Microsoft mode, treat a non-static member function as if
1928 it were a pointer-to-member. */
1929 if (flag_ms_extensions)
1931 PTRMEM_OK_P (member) = 1;
1932 return cp_build_addr_expr (member, complain);
1934 if (complain & tf_error)
1935 error ("invalid use of non-static member function %qD",
1936 TREE_OPERAND (member, 1));
1937 return error_mark_node;
1939 else if (TREE_CODE (member) == FIELD_DECL)
1941 if (complain & tf_error)
1942 error ("invalid use of non-static data member %qD", member);
1943 return error_mark_node;
1945 return member;
1948 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1949 PTRMEM_OK_P (member) = 1;
1950 return member;
1953 /* If DECL is a scalar enumeration constant or variable with a
1954 constant initializer, return the initializer (or, its initializers,
1955 recursively); otherwise, return DECL. If INTEGRAL_P, the
1956 initializer is only returned if DECL is an integral
1957 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
1958 return an aggregate constant. */
1960 static tree
1961 constant_value_1 (tree decl, bool integral_p, bool return_aggregate_cst_ok_p)
1963 while (TREE_CODE (decl) == CONST_DECL
1964 || (integral_p
1965 ? decl_constant_var_p (decl)
1966 : (VAR_P (decl)
1967 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
1969 tree init;
1970 /* If DECL is a static data member in a template
1971 specialization, we must instantiate it here. The
1972 initializer for the static data member is not processed
1973 until needed; we need it now. */
1974 mark_used (decl);
1975 mark_rvalue_use (decl);
1976 init = DECL_INITIAL (decl);
1977 if (init == error_mark_node)
1979 if (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
1980 /* Treat the error as a constant to avoid cascading errors on
1981 excessively recursive template instantiation (c++/9335). */
1982 return init;
1983 else
1984 return decl;
1986 /* Initializers in templates are generally expanded during
1987 instantiation, so before that for const int i(2)
1988 INIT is a TREE_LIST with the actual initializer as
1989 TREE_VALUE. */
1990 if (processing_template_decl
1991 && init
1992 && TREE_CODE (init) == TREE_LIST
1993 && TREE_CHAIN (init) == NULL_TREE)
1994 init = TREE_VALUE (init);
1995 if (!init
1996 || !TREE_TYPE (init)
1997 || !TREE_CONSTANT (init)
1998 || (!integral_p && !return_aggregate_cst_ok_p
1999 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
2000 return an aggregate constant (of which string
2001 literals are a special case), as we do not want
2002 to make inadvertent copies of such entities, and
2003 we must be sure that their addresses are the
2004 same everywhere. */
2005 && (TREE_CODE (init) == CONSTRUCTOR
2006 || TREE_CODE (init) == STRING_CST)))
2007 break;
2008 decl = unshare_expr (init);
2010 return decl;
2013 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by
2014 constant of integral or enumeration type, then return that value.
2015 These are those variables permitted in constant expressions by
2016 [5.19/1]. */
2018 tree
2019 integral_constant_value (tree decl)
2021 return constant_value_1 (decl, /*integral_p=*/true,
2022 /*return_aggregate_cst_ok_p=*/false);
2025 /* A more relaxed version of integral_constant_value, used by the
2026 common C/C++ code. */
2028 tree
2029 decl_constant_value (tree decl)
2031 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2032 /*return_aggregate_cst_ok_p=*/true);
2035 /* A version of integral_constant_value used by the C++ front end for
2036 optimization purposes. */
2038 tree
2039 decl_constant_value_safe (tree decl)
2041 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2042 /*return_aggregate_cst_ok_p=*/false);
2045 /* Common subroutines of build_new and build_vec_delete. */
2047 /* Call the global __builtin_delete to delete ADDR. */
2049 static tree
2050 build_builtin_delete_call (tree addr)
2052 mark_used (global_delete_fndecl);
2053 return build_call_n (global_delete_fndecl, 1, addr);
2056 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2057 the type of the object being allocated; otherwise, it's just TYPE.
2058 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2059 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2060 a vector of arguments to be provided as arguments to a placement
2061 new operator. This routine performs no semantic checks; it just
2062 creates and returns a NEW_EXPR. */
2064 static tree
2065 build_raw_new_expr (vec<tree, va_gc> *placement, tree type, tree nelts,
2066 vec<tree, va_gc> *init, int use_global_new)
2068 tree init_list;
2069 tree new_expr;
2071 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2072 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2073 permits us to distinguish the case of a missing initializer "new
2074 int" from an empty initializer "new int()". */
2075 if (init == NULL)
2076 init_list = NULL_TREE;
2077 else if (init->is_empty ())
2078 init_list = void_zero_node;
2079 else
2080 init_list = build_tree_list_vec (init);
2082 new_expr = build4 (NEW_EXPR, build_pointer_type (type),
2083 build_tree_list_vec (placement), type, nelts,
2084 init_list);
2085 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2086 TREE_SIDE_EFFECTS (new_expr) = 1;
2088 return new_expr;
2091 /* Diagnose uninitialized const members or reference members of type
2092 TYPE. USING_NEW is used to disambiguate the diagnostic between a
2093 new expression without a new-initializer and a declaration. Returns
2094 the error count. */
2096 static int
2097 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2098 bool using_new, bool complain)
2100 tree field;
2101 int error_count = 0;
2103 if (type_has_user_provided_constructor (type))
2104 return 0;
2106 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2108 tree field_type;
2110 if (TREE_CODE (field) != FIELD_DECL)
2111 continue;
2113 field_type = strip_array_types (TREE_TYPE (field));
2115 if (type_has_user_provided_constructor (field_type))
2116 continue;
2118 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2120 ++ error_count;
2121 if (complain)
2123 if (DECL_CONTEXT (field) == origin)
2125 if (using_new)
2126 error ("uninitialized reference member in %q#T "
2127 "using %<new%> without new-initializer", origin);
2128 else
2129 error ("uninitialized reference member in %q#T", origin);
2131 else
2133 if (using_new)
2134 error ("uninitialized reference member in base %q#T "
2135 "of %q#T using %<new%> without new-initializer",
2136 DECL_CONTEXT (field), origin);
2137 else
2138 error ("uninitialized reference member in base %q#T "
2139 "of %q#T", DECL_CONTEXT (field), origin);
2141 inform (DECL_SOURCE_LOCATION (field),
2142 "%qD should be initialized", field);
2146 if (CP_TYPE_CONST_P (field_type))
2148 ++ error_count;
2149 if (complain)
2151 if (DECL_CONTEXT (field) == origin)
2153 if (using_new)
2154 error ("uninitialized const member in %q#T "
2155 "using %<new%> without new-initializer", origin);
2156 else
2157 error ("uninitialized const member in %q#T", origin);
2159 else
2161 if (using_new)
2162 error ("uninitialized const member in base %q#T "
2163 "of %q#T using %<new%> without new-initializer",
2164 DECL_CONTEXT (field), origin);
2165 else
2166 error ("uninitialized const member in base %q#T "
2167 "of %q#T", DECL_CONTEXT (field), origin);
2169 inform (DECL_SOURCE_LOCATION (field),
2170 "%qD should be initialized", field);
2174 if (CLASS_TYPE_P (field_type))
2175 error_count
2176 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2177 using_new, complain);
2179 return error_count;
2183 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2185 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2188 /* Call __cxa_bad_array_new_length to indicate that the size calculation
2189 overflowed. Pretend it returns sizetype so that it plays nicely in the
2190 COND_EXPR. */
2192 tree
2193 throw_bad_array_new_length (void)
2195 tree fn = get_identifier ("__cxa_throw_bad_array_new_length");
2196 if (!get_global_value_if_present (fn, &fn))
2197 fn = push_throw_library_fn (fn, build_function_type_list (sizetype,
2198 NULL_TREE));
2200 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2203 /* Call __cxa_bad_array_length to indicate that there were too many
2204 initializers. */
2206 tree
2207 throw_bad_array_length (void)
2209 tree fn = get_identifier ("__cxa_throw_bad_array_length");
2210 if (!get_global_value_if_present (fn, &fn))
2211 fn = push_throw_library_fn (fn, build_function_type_list (void_type_node,
2212 NULL_TREE));
2214 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
2217 /* Generate code for a new-expression, including calling the "operator
2218 new" function, initializing the object, and, if an exception occurs
2219 during construction, cleaning up. The arguments are as for
2220 build_raw_new_expr. This may change PLACEMENT and INIT. */
2222 static tree
2223 build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
2224 vec<tree, va_gc> **init, bool globally_qualified_p,
2225 tsubst_flags_t complain)
2227 tree size, rval;
2228 /* True iff this is a call to "operator new[]" instead of just
2229 "operator new". */
2230 bool array_p = false;
2231 /* If ARRAY_P is true, the element type of the array. This is never
2232 an ARRAY_TYPE; for something like "new int[3][4]", the
2233 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
2234 TYPE. */
2235 tree elt_type;
2236 /* The type of the new-expression. (This type is always a pointer
2237 type.) */
2238 tree pointer_type;
2239 tree non_const_pointer_type;
2240 tree outer_nelts = NULL_TREE;
2241 /* For arrays, a bounds checks on the NELTS parameter. */
2242 tree outer_nelts_check = NULL_TREE;
2243 bool outer_nelts_from_type = false;
2244 double_int inner_nelts_count = double_int_one;
2245 tree alloc_call, alloc_expr;
2246 /* Size of the inner array elements. */
2247 double_int inner_size;
2248 /* The address returned by the call to "operator new". This node is
2249 a VAR_DECL and is therefore reusable. */
2250 tree alloc_node;
2251 tree alloc_fn;
2252 tree cookie_expr, init_expr;
2253 int nothrow, check_new;
2254 int use_java_new = 0;
2255 /* If non-NULL, the number of extra bytes to allocate at the
2256 beginning of the storage allocated for an array-new expression in
2257 order to store the number of elements. */
2258 tree cookie_size = NULL_TREE;
2259 tree placement_first;
2260 tree placement_expr = NULL_TREE;
2261 /* True if the function we are calling is a placement allocation
2262 function. */
2263 bool placement_allocation_fn_p;
2264 /* True if the storage must be initialized, either by a constructor
2265 or due to an explicit new-initializer. */
2266 bool is_initialized;
2267 /* The address of the thing allocated, not including any cookie. In
2268 particular, if an array cookie is in use, DATA_ADDR is the
2269 address of the first array element. This node is a VAR_DECL, and
2270 is therefore reusable. */
2271 tree data_addr;
2272 tree init_preeval_expr = NULL_TREE;
2274 if (nelts)
2276 outer_nelts = nelts;
2277 array_p = true;
2279 else if (TREE_CODE (type) == ARRAY_TYPE)
2281 /* Transforms new (T[N]) to new T[N]. The former is a GNU
2282 extension for variable N. (This also covers new T where T is
2283 a VLA typedef.) */
2284 array_p = true;
2285 nelts = array_type_nelts_top (type);
2286 outer_nelts = nelts;
2287 type = TREE_TYPE (type);
2288 outer_nelts_from_type = true;
2291 /* If our base type is an array, then make sure we know how many elements
2292 it has. */
2293 for (elt_type = type;
2294 TREE_CODE (elt_type) == ARRAY_TYPE;
2295 elt_type = TREE_TYPE (elt_type))
2297 tree inner_nelts = array_type_nelts_top (elt_type);
2298 tree inner_nelts_cst = maybe_constant_value (inner_nelts);
2299 if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
2301 bool overflow;
2302 double_int result = TREE_INT_CST (inner_nelts_cst)
2303 .mul_with_sign (inner_nelts_count,
2304 false, &overflow);
2305 if (overflow)
2307 if (complain & tf_error)
2308 error ("integer overflow in array size");
2309 nelts = error_mark_node;
2311 inner_nelts_count = result;
2313 else
2315 if (complain & tf_error)
2317 error_at (EXPR_LOC_OR_HERE (inner_nelts),
2318 "array size in operator new must be constant");
2319 cxx_constant_value(inner_nelts);
2321 nelts = error_mark_node;
2323 if (nelts != error_mark_node)
2324 nelts = cp_build_binary_op (input_location,
2325 MULT_EXPR, nelts,
2326 inner_nelts_cst,
2327 complain);
2330 if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
2332 error ("variably modified type not allowed in operator new");
2333 return error_mark_node;
2336 if (nelts == error_mark_node)
2337 return error_mark_node;
2339 /* Warn if we performed the (T[N]) to T[N] transformation and N is
2340 variable. */
2341 if (outer_nelts_from_type
2342 && !TREE_CONSTANT (maybe_constant_value (outer_nelts)))
2344 if (complain & tf_warning_or_error)
2345 pedwarn(EXPR_LOC_OR_HERE (outer_nelts), OPT_Wvla,
2346 "ISO C++ does not support variable-length array types");
2347 else
2348 return error_mark_node;
2351 if (VOID_TYPE_P (elt_type))
2353 if (complain & tf_error)
2354 error ("invalid type %<void%> for new");
2355 return error_mark_node;
2358 if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain))
2359 return error_mark_node;
2361 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
2363 if (*init == NULL)
2365 bool maybe_uninitialized_error = false;
2366 /* A program that calls for default-initialization [...] of an
2367 entity of reference type is ill-formed. */
2368 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
2369 maybe_uninitialized_error = true;
2371 /* A new-expression that creates an object of type T initializes
2372 that object as follows:
2373 - If the new-initializer is omitted:
2374 -- If T is a (possibly cv-qualified) non-POD class type
2375 (or array thereof), the object is default-initialized (8.5).
2376 [...]
2377 -- Otherwise, the object created has indeterminate
2378 value. If T is a const-qualified type, or a (possibly
2379 cv-qualified) POD class type (or array thereof)
2380 containing (directly or indirectly) a member of
2381 const-qualified type, the program is ill-formed; */
2383 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
2384 maybe_uninitialized_error = true;
2386 if (maybe_uninitialized_error
2387 && diagnose_uninitialized_cst_or_ref_member (elt_type,
2388 /*using_new=*/true,
2389 complain & tf_error))
2390 return error_mark_node;
2393 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
2394 && default_init_uninitialized_part (elt_type))
2396 if (complain & tf_error)
2397 error ("uninitialized const in %<new%> of %q#T", elt_type);
2398 return error_mark_node;
2401 size = size_in_bytes (elt_type);
2402 if (array_p)
2404 /* Maximum available size in bytes. Half of the address space
2405 minus the cookie size. */
2406 double_int max_size
2407 = double_int_one.llshift (TYPE_PRECISION (sizetype) - 1,
2408 HOST_BITS_PER_DOUBLE_INT);
2409 /* Maximum number of outer elements which can be allocated. */
2410 double_int max_outer_nelts;
2411 tree max_outer_nelts_tree;
2413 gcc_assert (TREE_CODE (size) == INTEGER_CST);
2414 cookie_size = targetm.cxx.get_cookie_size (elt_type);
2415 gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
2416 gcc_checking_assert (TREE_INT_CST (cookie_size).ult (max_size));
2417 /* Unconditionally subtract the cookie size. This decreases the
2418 maximum object size and is safe even if we choose not to use
2419 a cookie after all. */
2420 max_size -= TREE_INT_CST (cookie_size);
2421 bool overflow;
2422 inner_size = TREE_INT_CST (size)
2423 .mul_with_sign (inner_nelts_count, false, &overflow);
2424 if (overflow || inner_size.ugt (max_size))
2426 if (complain & tf_error)
2427 error ("size of array is too large");
2428 return error_mark_node;
2430 max_outer_nelts = max_size.udiv (inner_size, TRUNC_DIV_EXPR);
2431 /* Only keep the top-most seven bits, to simplify encoding the
2432 constant in the instruction stream. */
2434 unsigned shift = HOST_BITS_PER_DOUBLE_INT - 7
2435 - (max_outer_nelts.high ? clz_hwi (max_outer_nelts.high)
2436 : (HOST_BITS_PER_WIDE_INT + clz_hwi (max_outer_nelts.low)));
2437 max_outer_nelts
2438 = max_outer_nelts.lrshift (shift, HOST_BITS_PER_DOUBLE_INT)
2439 .llshift (shift, HOST_BITS_PER_DOUBLE_INT);
2441 max_outer_nelts_tree = double_int_to_tree (sizetype, max_outer_nelts);
2443 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2444 outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
2445 outer_nelts,
2446 max_outer_nelts_tree);
2449 alloc_fn = NULL_TREE;
2451 /* If PLACEMENT is a single simple pointer type not passed by
2452 reference, prepare to capture it in a temporary variable. Do
2453 this now, since PLACEMENT will change in the calls below. */
2454 placement_first = NULL_TREE;
2455 if (vec_safe_length (*placement) == 1
2456 && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
2457 placement_first = (**placement)[0];
2459 /* Allocate the object. */
2460 if (vec_safe_is_empty (*placement) && TYPE_FOR_JAVA (elt_type))
2462 tree class_addr;
2463 tree class_decl = build_java_class_ref (elt_type);
2464 static const char alloc_name[] = "_Jv_AllocObject";
2466 if (class_decl == error_mark_node)
2467 return error_mark_node;
2469 use_java_new = 1;
2470 if (!get_global_value_if_present (get_identifier (alloc_name),
2471 &alloc_fn))
2473 if (complain & tf_error)
2474 error ("call to Java constructor with %qs undefined", alloc_name);
2475 return error_mark_node;
2477 else if (really_overloaded_fn (alloc_fn))
2479 if (complain & tf_error)
2480 error ("%qD should never be overloaded", alloc_fn);
2481 return error_mark_node;
2483 alloc_fn = OVL_CURRENT (alloc_fn);
2484 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2485 alloc_call = cp_build_function_call_nary (alloc_fn, complain,
2486 class_addr, NULL_TREE);
2488 else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
2490 error ("Java class %q#T object allocated using placement new", elt_type);
2491 return error_mark_node;
2493 else
2495 tree fnname;
2496 tree fns;
2498 fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
2500 if (!globally_qualified_p
2501 && CLASS_TYPE_P (elt_type)
2502 && (array_p
2503 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
2504 : TYPE_HAS_NEW_OPERATOR (elt_type)))
2506 /* Use a class-specific operator new. */
2507 /* If a cookie is required, add some extra space. */
2508 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
2509 size = size_binop (PLUS_EXPR, size, cookie_size);
2510 else
2512 cookie_size = NULL_TREE;
2513 /* No size arithmetic necessary, so the size check is
2514 not needed. */
2515 if (outer_nelts_check != NULL && inner_size.is_one ())
2516 outer_nelts_check = NULL_TREE;
2518 /* Perform the overflow check. */
2519 tree errval = TYPE_MAX_VALUE (sizetype);
2520 if (cxx_dialect >= cxx11)
2521 errval = throw_bad_array_new_length ();
2522 if (outer_nelts_check != NULL_TREE)
2523 size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
2524 size, errval);
2525 /* Create the argument list. */
2526 vec_safe_insert (*placement, 0, size);
2527 /* Do name-lookup to find the appropriate operator. */
2528 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
2529 if (fns == NULL_TREE)
2531 if (complain & tf_error)
2532 error ("no suitable %qD found in class %qT", fnname, elt_type);
2533 return error_mark_node;
2535 if (TREE_CODE (fns) == TREE_LIST)
2537 if (complain & tf_error)
2539 error ("request for member %qD is ambiguous", fnname);
2540 print_candidates (fns);
2542 return error_mark_node;
2544 alloc_call = build_new_method_call (build_dummy_object (elt_type),
2545 fns, placement,
2546 /*conversion_path=*/NULL_TREE,
2547 LOOKUP_NORMAL,
2548 &alloc_fn,
2549 complain);
2551 else
2553 /* Use a global operator new. */
2554 /* See if a cookie might be required. */
2555 if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
2557 cookie_size = NULL_TREE;
2558 /* No size arithmetic necessary, so the size check is
2559 not needed. */
2560 if (outer_nelts_check != NULL && inner_size.is_one ())
2561 outer_nelts_check = NULL_TREE;
2564 alloc_call = build_operator_new_call (fnname, placement,
2565 &size, &cookie_size,
2566 outer_nelts_check,
2567 &alloc_fn, complain);
2571 if (alloc_call == error_mark_node)
2572 return error_mark_node;
2574 gcc_assert (alloc_fn != NULL_TREE);
2576 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
2577 into a temporary variable. */
2578 if (!processing_template_decl
2579 && placement_first != NULL_TREE
2580 && TREE_CODE (alloc_call) == CALL_EXPR
2581 && call_expr_nargs (alloc_call) == 2
2582 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
2583 && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
2585 tree placement_arg = CALL_EXPR_ARG (alloc_call, 1);
2587 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))
2588 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))))
2590 placement_expr = get_target_expr (placement_first);
2591 CALL_EXPR_ARG (alloc_call, 1)
2592 = convert (TREE_TYPE (placement_arg), placement_expr);
2596 /* In the simple case, we can stop now. */
2597 pointer_type = build_pointer_type (type);
2598 if (!cookie_size && !is_initialized)
2599 return build_nop (pointer_type, alloc_call);
2601 /* Store the result of the allocation call in a variable so that we can
2602 use it more than once. */
2603 alloc_expr = get_target_expr (alloc_call);
2604 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2606 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
2607 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
2608 alloc_call = TREE_OPERAND (alloc_call, 1);
2610 /* Now, check to see if this function is actually a placement
2611 allocation function. This can happen even when PLACEMENT is NULL
2612 because we might have something like:
2614 struct S { void* operator new (size_t, int i = 0); };
2616 A call to `new S' will get this allocation function, even though
2617 there is no explicit placement argument. If there is more than
2618 one argument, or there are variable arguments, then this is a
2619 placement allocation function. */
2620 placement_allocation_fn_p
2621 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2622 || varargs_function_p (alloc_fn));
2624 /* Preevaluate the placement args so that we don't reevaluate them for a
2625 placement delete. */
2626 if (placement_allocation_fn_p)
2628 tree inits;
2629 stabilize_call (alloc_call, &inits);
2630 if (inits)
2631 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
2632 alloc_expr);
2635 /* unless an allocation function is declared with an empty excep-
2636 tion-specification (_except.spec_), throw(), it indicates failure to
2637 allocate storage by throwing a bad_alloc exception (clause _except_,
2638 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2639 cation function is declared with an empty exception-specification,
2640 throw(), it returns null to indicate failure to allocate storage and a
2641 non-null pointer otherwise.
2643 So check for a null exception spec on the op new we just called. */
2645 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2646 check_new = (flag_check_new || nothrow) && ! use_java_new;
2648 if (cookie_size)
2650 tree cookie;
2651 tree cookie_ptr;
2652 tree size_ptr_type;
2654 /* Adjust so we're pointing to the start of the object. */
2655 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
2657 /* Store the number of bytes allocated so that we can know how
2658 many elements to destroy later. We use the last sizeof
2659 (size_t) bytes to store the number of elements. */
2660 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
2661 cookie_ptr = fold_build_pointer_plus_loc (input_location,
2662 alloc_node, cookie_ptr);
2663 size_ptr_type = build_pointer_type (sizetype);
2664 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
2665 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2667 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
2669 if (targetm.cxx.cookie_has_size ())
2671 /* Also store the element size. */
2672 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
2673 fold_build1_loc (input_location,
2674 NEGATE_EXPR, sizetype,
2675 size_in_bytes (sizetype)));
2677 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2678 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
2679 size_in_bytes (elt_type));
2680 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
2681 cookie, cookie_expr);
2684 else
2686 cookie_expr = NULL_TREE;
2687 data_addr = alloc_node;
2690 /* Now use a pointer to the type we've actually allocated. */
2692 /* But we want to operate on a non-const version to start with,
2693 since we'll be modifying the elements. */
2694 non_const_pointer_type = build_pointer_type
2695 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
2697 data_addr = fold_convert (non_const_pointer_type, data_addr);
2698 /* Any further uses of alloc_node will want this type, too. */
2699 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
2701 /* Now initialize the allocated object. Note that we preevaluate the
2702 initialization expression, apart from the actual constructor call or
2703 assignment--we do this because we want to delay the allocation as long
2704 as possible in order to minimize the size of the exception region for
2705 placement delete. */
2706 if (is_initialized)
2708 bool stable;
2709 bool explicit_value_init_p = false;
2711 if (*init != NULL && (*init)->is_empty ())
2713 *init = NULL;
2714 explicit_value_init_p = true;
2717 if (processing_template_decl && explicit_value_init_p)
2719 /* build_value_init doesn't work in templates, and we don't need
2720 the initializer anyway since we're going to throw it away and
2721 rebuild it at instantiation time, so just build up a single
2722 constructor call to get any appropriate diagnostics. */
2723 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2724 if (type_build_ctor_call (elt_type))
2725 init_expr = build_special_member_call (init_expr,
2726 complete_ctor_identifier,
2727 init, elt_type,
2728 LOOKUP_NORMAL,
2729 complain);
2730 stable = stabilize_init (init_expr, &init_preeval_expr);
2732 else if (array_p)
2734 tree vecinit = NULL_TREE;
2735 if (vec_safe_length (*init) == 1
2736 && BRACE_ENCLOSED_INITIALIZER_P ((**init)[0])
2737 && CONSTRUCTOR_IS_DIRECT_INIT ((**init)[0]))
2739 vecinit = (**init)[0];
2740 if (CONSTRUCTOR_NELTS (vecinit) == 0)
2741 /* List-value-initialization, leave it alone. */;
2742 else
2744 tree arraytype, domain;
2745 if (TREE_CONSTANT (nelts))
2746 domain = compute_array_index_type (NULL_TREE, nelts,
2747 complain);
2748 else
2749 /* We'll check the length at runtime. */
2750 domain = NULL_TREE;
2751 arraytype = build_cplus_array_type (type, domain);
2752 vecinit = digest_init (arraytype, vecinit, complain);
2755 else if (*init)
2757 if (complain & tf_error)
2758 permerror (input_location,
2759 "parenthesized initializer in array new");
2760 else
2761 return error_mark_node;
2762 vecinit = build_tree_list_vec (*init);
2764 init_expr
2765 = build_vec_init (data_addr,
2766 cp_build_binary_op (input_location,
2767 MINUS_EXPR, outer_nelts,
2768 integer_one_node,
2769 complain),
2770 vecinit,
2771 explicit_value_init_p,
2772 /*from_array=*/0,
2773 complain);
2775 /* An array initialization is stable because the initialization
2776 of each element is a full-expression, so the temporaries don't
2777 leak out. */
2778 stable = true;
2780 else
2782 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2784 if (type_build_ctor_call (type) && !explicit_value_init_p)
2786 init_expr = build_special_member_call (init_expr,
2787 complete_ctor_identifier,
2788 init, elt_type,
2789 LOOKUP_NORMAL,
2790 complain);
2792 else if (explicit_value_init_p)
2794 /* Something like `new int()'. */
2795 tree val = build_value_init (type, complain);
2796 if (val == error_mark_node)
2797 return error_mark_node;
2798 init_expr = build2 (INIT_EXPR, type, init_expr, val);
2800 else
2802 tree ie;
2804 /* We are processing something like `new int (10)', which
2805 means allocate an int, and initialize it with 10. */
2807 ie = build_x_compound_expr_from_vec (*init, "new initializer",
2808 complain);
2809 init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie,
2810 complain);
2812 stable = stabilize_init (init_expr, &init_preeval_expr);
2815 if (init_expr == error_mark_node)
2816 return error_mark_node;
2818 /* If any part of the object initialization terminates by throwing an
2819 exception and a suitable deallocation function can be found, the
2820 deallocation function is called to free the memory in which the
2821 object was being constructed, after which the exception continues
2822 to propagate in the context of the new-expression. If no
2823 unambiguous matching deallocation function can be found,
2824 propagating the exception does not cause the object's memory to be
2825 freed. */
2826 if (flag_exceptions && ! use_java_new)
2828 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
2829 tree cleanup;
2831 /* The Standard is unclear here, but the right thing to do
2832 is to use the same method for finding deallocation
2833 functions that we use for finding allocation functions. */
2834 cleanup = (build_op_delete_call
2835 (dcode,
2836 alloc_node,
2837 size,
2838 globally_qualified_p,
2839 placement_allocation_fn_p ? alloc_call : NULL_TREE,
2840 alloc_fn,
2841 complain));
2843 if (!cleanup)
2844 /* We're done. */;
2845 else if (stable)
2846 /* This is much simpler if we were able to preevaluate all of
2847 the arguments to the constructor call. */
2849 /* CLEANUP is compiler-generated, so no diagnostics. */
2850 TREE_NO_WARNING (cleanup) = true;
2851 init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
2852 init_expr, cleanup);
2853 /* Likewise, this try-catch is compiler-generated. */
2854 TREE_NO_WARNING (init_expr) = true;
2856 else
2857 /* Ack! First we allocate the memory. Then we set our sentry
2858 variable to true, and expand a cleanup that deletes the
2859 memory if sentry is true. Then we run the constructor, and
2860 finally clear the sentry.
2862 We need to do this because we allocate the space first, so
2863 if there are any temporaries with cleanups in the
2864 constructor args and we weren't able to preevaluate them, we
2865 need this EH region to extend until end of full-expression
2866 to preserve nesting. */
2868 tree end, sentry, begin;
2870 begin = get_target_expr (boolean_true_node);
2871 CLEANUP_EH_ONLY (begin) = 1;
2873 sentry = TARGET_EXPR_SLOT (begin);
2875 /* CLEANUP is compiler-generated, so no diagnostics. */
2876 TREE_NO_WARNING (cleanup) = true;
2878 TARGET_EXPR_CLEANUP (begin)
2879 = build3 (COND_EXPR, void_type_node, sentry,
2880 cleanup, void_zero_node);
2882 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2883 sentry, boolean_false_node);
2885 init_expr
2886 = build2 (COMPOUND_EXPR, void_type_node, begin,
2887 build2 (COMPOUND_EXPR, void_type_node, init_expr,
2888 end));
2889 /* Likewise, this is compiler-generated. */
2890 TREE_NO_WARNING (init_expr) = true;
2894 else
2895 init_expr = NULL_TREE;
2897 /* Now build up the return value in reverse order. */
2899 rval = data_addr;
2901 if (init_expr)
2902 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2903 if (cookie_expr)
2904 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2906 if (rval == data_addr)
2907 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2908 and return the call (which doesn't need to be adjusted). */
2909 rval = TARGET_EXPR_INITIAL (alloc_expr);
2910 else
2912 if (check_new)
2914 tree ifexp = cp_build_binary_op (input_location,
2915 NE_EXPR, alloc_node,
2916 nullptr_node,
2917 complain);
2918 rval = build_conditional_expr (input_location, ifexp, rval,
2919 alloc_node, complain);
2922 /* Perform the allocation before anything else, so that ALLOC_NODE
2923 has been initialized before we start using it. */
2924 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2927 if (init_preeval_expr)
2928 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
2930 /* A new-expression is never an lvalue. */
2931 gcc_assert (!lvalue_p (rval));
2933 return convert (pointer_type, rval);
2936 /* Generate a representation for a C++ "new" expression. *PLACEMENT
2937 is a vector of placement-new arguments (or NULL if none). If NELTS
2938 is NULL, TYPE is the type of the storage to be allocated. If NELTS
2939 is not NULL, then this is an array-new allocation; TYPE is the type
2940 of the elements in the array and NELTS is the number of elements in
2941 the array. *INIT, if non-NULL, is the initializer for the new
2942 object, or an empty vector to indicate an initializer of "()". If
2943 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
2944 rather than just "new". This may change PLACEMENT and INIT. */
2946 tree
2947 build_new (vec<tree, va_gc> **placement, tree type, tree nelts,
2948 vec<tree, va_gc> **init, int use_global_new, tsubst_flags_t complain)
2950 tree rval;
2951 vec<tree, va_gc> *orig_placement = NULL;
2952 tree orig_nelts = NULL_TREE;
2953 vec<tree, va_gc> *orig_init = NULL;
2955 if (type == error_mark_node)
2956 return error_mark_node;
2958 if (nelts == NULL_TREE && vec_safe_length (*init) == 1
2959 /* Don't do auto deduction where it might affect mangling. */
2960 && (!processing_template_decl || at_function_scope_p ()))
2962 tree auto_node = type_uses_auto (type);
2963 if (auto_node)
2965 tree d_init = (**init)[0];
2966 d_init = resolve_nondeduced_context (d_init);
2967 type = do_auto_deduction (type, d_init, auto_node);
2971 if (processing_template_decl)
2973 if (dependent_type_p (type)
2974 || any_type_dependent_arguments_p (*placement)
2975 || (nelts && type_dependent_expression_p (nelts))
2976 || (nelts && *init)
2977 || any_type_dependent_arguments_p (*init))
2978 return build_raw_new_expr (*placement, type, nelts, *init,
2979 use_global_new);
2981 orig_placement = make_tree_vector_copy (*placement);
2982 orig_nelts = nelts;
2983 if (*init)
2984 orig_init = make_tree_vector_copy (*init);
2986 make_args_non_dependent (*placement);
2987 if (nelts)
2988 nelts = build_non_dependent_expr (nelts);
2989 make_args_non_dependent (*init);
2992 if (nelts)
2994 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
2996 if (complain & tf_error)
2997 permerror (input_location, "size in array new must have integral type");
2998 else
2999 return error_mark_node;
3001 nelts = mark_rvalue_use (nelts);
3002 nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
3005 /* ``A reference cannot be created by the new operator. A reference
3006 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3007 returned by new.'' ARM 5.3.3 */
3008 if (TREE_CODE (type) == REFERENCE_TYPE)
3010 if (complain & tf_error)
3011 error ("new cannot be applied to a reference type");
3012 else
3013 return error_mark_node;
3014 type = TREE_TYPE (type);
3017 if (TREE_CODE (type) == FUNCTION_TYPE)
3019 if (complain & tf_error)
3020 error ("new cannot be applied to a function type");
3021 return error_mark_node;
3024 /* The type allocated must be complete. If the new-type-id was
3025 "T[N]" then we are just checking that "T" is complete here, but
3026 that is equivalent, since the value of "N" doesn't matter. */
3027 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
3028 return error_mark_node;
3030 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
3031 if (rval == error_mark_node)
3032 return error_mark_node;
3034 if (processing_template_decl)
3036 tree ret = build_raw_new_expr (orig_placement, type, orig_nelts,
3037 orig_init, use_global_new);
3038 release_tree_vector (orig_placement);
3039 release_tree_vector (orig_init);
3040 return ret;
3043 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
3044 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
3045 TREE_NO_WARNING (rval) = 1;
3047 return rval;
3050 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
3052 tree
3053 build_java_class_ref (tree type)
3055 tree name = NULL_TREE, class_decl;
3056 static tree CL_suffix = NULL_TREE;
3057 if (CL_suffix == NULL_TREE)
3058 CL_suffix = get_identifier("class$");
3059 if (jclass_node == NULL_TREE)
3061 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
3062 if (jclass_node == NULL_TREE)
3064 error ("call to Java constructor, while %<jclass%> undefined");
3065 return error_mark_node;
3067 jclass_node = TREE_TYPE (jclass_node);
3070 /* Mangle the class$ field. */
3072 tree field;
3073 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3074 if (DECL_NAME (field) == CL_suffix)
3076 mangle_decl (field);
3077 name = DECL_ASSEMBLER_NAME (field);
3078 break;
3080 if (!field)
3082 error ("can%'t find %<class$%> in %qT", type);
3083 return error_mark_node;
3087 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
3088 if (class_decl == NULL_TREE)
3090 class_decl = build_decl (input_location,
3091 VAR_DECL, name, TREE_TYPE (jclass_node));
3092 TREE_STATIC (class_decl) = 1;
3093 DECL_EXTERNAL (class_decl) = 1;
3094 TREE_PUBLIC (class_decl) = 1;
3095 DECL_ARTIFICIAL (class_decl) = 1;
3096 DECL_IGNORED_P (class_decl) = 1;
3097 pushdecl_top_level (class_decl);
3098 make_decl_rtl (class_decl);
3100 return class_decl;
3103 static tree
3104 build_vec_delete_1 (tree base, tree maxindex, tree type,
3105 special_function_kind auto_delete_vec,
3106 int use_global_delete, tsubst_flags_t complain)
3108 tree virtual_size;
3109 tree ptype = build_pointer_type (type = complete_type (type));
3110 tree size_exp;
3112 /* Temporary variables used by the loop. */
3113 tree tbase, tbase_init;
3115 /* This is the body of the loop that implements the deletion of a
3116 single element, and moves temp variables to next elements. */
3117 tree body;
3119 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3120 tree loop = 0;
3122 /* This is the thing that governs what to do after the loop has run. */
3123 tree deallocate_expr = 0;
3125 /* This is the BIND_EXPR which holds the outermost iterator of the
3126 loop. It is convenient to set this variable up and test it before
3127 executing any other code in the loop.
3128 This is also the containing expression returned by this function. */
3129 tree controller = NULL_TREE;
3130 tree tmp;
3132 /* We should only have 1-D arrays here. */
3133 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
3135 if (base == error_mark_node || maxindex == error_mark_node)
3136 return error_mark_node;
3138 if (!COMPLETE_TYPE_P (type))
3140 if ((complain & tf_warning)
3141 && warning (OPT_Wdelete_incomplete,
3142 "possible problem detected in invocation of "
3143 "delete [] operator:"))
3145 cxx_incomplete_type_diagnostic (base, type, DK_WARNING);
3146 inform (input_location, "neither the destructor nor the "
3147 "class-specific operator delete [] will be called, "
3148 "even if they are declared when the class is defined");
3150 return build_builtin_delete_call (base);
3153 size_exp = size_in_bytes (type);
3155 if (! MAYBE_CLASS_TYPE_P (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3156 goto no_destructor;
3158 /* The below is short by the cookie size. */
3159 virtual_size = size_binop (MULT_EXPR, size_exp,
3160 convert (sizetype, maxindex));
3162 tbase = create_temporary_var (ptype);
3163 tbase_init
3164 = cp_build_modify_expr (tbase, NOP_EXPR,
3165 fold_build_pointer_plus_loc (input_location,
3166 fold_convert (ptype,
3167 base),
3168 virtual_size),
3169 complain);
3170 if (tbase_init == error_mark_node)
3171 return error_mark_node;
3172 controller = build3 (BIND_EXPR, void_type_node, tbase,
3173 NULL_TREE, NULL_TREE);
3174 TREE_SIDE_EFFECTS (controller) = 1;
3176 body = build1 (EXIT_EXPR, void_type_node,
3177 build2 (EQ_EXPR, boolean_type_node, tbase,
3178 fold_convert (ptype, base)));
3179 tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp);
3180 tmp = fold_build_pointer_plus (tbase, tmp);
3181 tmp = cp_build_modify_expr (tbase, NOP_EXPR, tmp, complain);
3182 if (tmp == error_mark_node)
3183 return error_mark_node;
3184 body = build_compound_expr (input_location, body, tmp);
3185 tmp = build_delete (ptype, tbase, sfk_complete_destructor,
3186 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
3187 complain);
3188 if (tmp == error_mark_node)
3189 return error_mark_node;
3190 body = build_compound_expr (input_location, body, tmp);
3192 loop = build1 (LOOP_EXPR, void_type_node, body);
3193 loop = build_compound_expr (input_location, tbase_init, loop);
3195 no_destructor:
3196 /* Delete the storage if appropriate. */
3197 if (auto_delete_vec == sfk_deleting_destructor)
3199 tree base_tbd;
3201 /* The below is short by the cookie size. */
3202 virtual_size = size_binop (MULT_EXPR, size_exp,
3203 convert (sizetype, maxindex));
3205 if (! TYPE_VEC_NEW_USES_COOKIE (type))
3206 /* no header */
3207 base_tbd = base;
3208 else
3210 tree cookie_size;
3212 cookie_size = targetm.cxx.get_cookie_size (type);
3213 base_tbd = cp_build_binary_op (input_location,
3214 MINUS_EXPR,
3215 cp_convert (string_type_node,
3216 base, complain),
3217 cookie_size,
3218 complain);
3219 if (base_tbd == error_mark_node)
3220 return error_mark_node;
3221 base_tbd = cp_convert (ptype, base_tbd, complain);
3222 /* True size with header. */
3223 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
3226 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
3227 base_tbd, virtual_size,
3228 use_global_delete & 1,
3229 /*placement=*/NULL_TREE,
3230 /*alloc_fn=*/NULL_TREE,
3231 complain);
3234 body = loop;
3235 if (!deallocate_expr)
3237 else if (!body)
3238 body = deallocate_expr;
3239 else
3240 body = build_compound_expr (input_location, body, deallocate_expr);
3242 if (!body)
3243 body = integer_zero_node;
3245 /* Outermost wrapper: If pointer is null, punt. */
3246 body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3247 fold_build2_loc (input_location,
3248 NE_EXPR, boolean_type_node, base,
3249 convert (TREE_TYPE (base),
3250 nullptr_node)),
3251 body, integer_zero_node);
3252 body = build1 (NOP_EXPR, void_type_node, body);
3254 if (controller)
3256 TREE_OPERAND (controller, 1) = body;
3257 body = controller;
3260 if (TREE_CODE (base) == SAVE_EXPR)
3261 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
3262 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
3264 return convert_to_void (body, ICV_CAST, complain);
3267 /* Create an unnamed variable of the indicated TYPE. */
3269 tree
3270 create_temporary_var (tree type)
3272 tree decl;
3274 decl = build_decl (input_location,
3275 VAR_DECL, NULL_TREE, type);
3276 TREE_USED (decl) = 1;
3277 DECL_ARTIFICIAL (decl) = 1;
3278 DECL_IGNORED_P (decl) = 1;
3279 DECL_CONTEXT (decl) = current_function_decl;
3281 return decl;
3284 /* Create a new temporary variable of the indicated TYPE, initialized
3285 to INIT.
3287 It is not entered into current_binding_level, because that breaks
3288 things when it comes time to do final cleanups (which take place
3289 "outside" the binding contour of the function). */
3291 tree
3292 get_temp_regvar (tree type, tree init)
3294 tree decl;
3296 decl = create_temporary_var (type);
3297 add_decl_expr (decl);
3299 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
3300 tf_warning_or_error));
3302 return decl;
3305 /* `build_vec_init' returns tree structure that performs
3306 initialization of a vector of aggregate types.
3308 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
3309 to the first element, of POINTER_TYPE.
3310 MAXINDEX is the maximum index of the array (one less than the
3311 number of elements). It is only used if BASE is a pointer or
3312 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
3314 INIT is the (possibly NULL) initializer.
3316 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
3317 elements in the array are value-initialized.
3319 FROM_ARRAY is 0 if we should init everything with INIT
3320 (i.e., every element initialized from INIT).
3321 FROM_ARRAY is 1 if we should index into INIT in parallel
3322 with initialization of DECL.
3323 FROM_ARRAY is 2 if we should index into INIT in parallel,
3324 but use assignment instead of initialization. */
3326 tree
3327 build_vec_init (tree base, tree maxindex, tree init,
3328 bool explicit_value_init_p,
3329 int from_array, tsubst_flags_t complain)
3331 tree rval;
3332 tree base2 = NULL_TREE;
3333 tree itype = NULL_TREE;
3334 tree iterator;
3335 /* The type of BASE. */
3336 tree atype = TREE_TYPE (base);
3337 /* The type of an element in the array. */
3338 tree type = TREE_TYPE (atype);
3339 /* The element type reached after removing all outer array
3340 types. */
3341 tree inner_elt_type;
3342 /* The type of a pointer to an element in the array. */
3343 tree ptype;
3344 tree stmt_expr;
3345 tree compound_stmt;
3346 int destroy_temps;
3347 tree try_block = NULL_TREE;
3348 int num_initialized_elts = 0;
3349 bool is_global;
3350 tree const_init = NULL_TREE;
3351 tree obase = base;
3352 bool xvalue = false;
3353 bool errors = false;
3354 tree length_check = NULL_TREE;
3356 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
3357 maxindex = array_type_nelts (atype);
3359 if (maxindex == NULL_TREE || maxindex == error_mark_node)
3360 return error_mark_node;
3362 if (explicit_value_init_p)
3363 gcc_assert (!init);
3365 inner_elt_type = strip_array_types (type);
3367 /* Look through the TARGET_EXPR around a compound literal. */
3368 if (init && TREE_CODE (init) == TARGET_EXPR
3369 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
3370 && from_array != 2)
3371 init = TARGET_EXPR_INITIAL (init);
3373 /* If we have a braced-init-list, make sure that the array
3374 is big enough for all the initializers. */
3375 if (init && TREE_CODE (init) == CONSTRUCTOR
3376 && CONSTRUCTOR_NELTS (init) > 0
3377 && !TREE_CONSTANT (maxindex))
3378 length_check = fold_build2 (LT_EXPR, boolean_type_node, maxindex,
3379 size_int (CONSTRUCTOR_NELTS (init) - 1));
3381 if (init
3382 && TREE_CODE (atype) == ARRAY_TYPE
3383 && TREE_CONSTANT (maxindex)
3384 && (from_array == 2
3385 ? (!CLASS_TYPE_P (inner_elt_type)
3386 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (inner_elt_type))
3387 : !TYPE_NEEDS_CONSTRUCTING (type))
3388 && ((TREE_CODE (init) == CONSTRUCTOR
3389 /* Don't do this if the CONSTRUCTOR might contain something
3390 that might throw and require us to clean up. */
3391 && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
3392 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
3393 || from_array))
3395 /* Do non-default initialization of trivial arrays resulting from
3396 brace-enclosed initializers. In this case, digest_init and
3397 store_constructor will handle the semantics for us. */
3399 stmt_expr = build2 (INIT_EXPR, atype, base, init);
3400 if (length_check)
3401 stmt_expr = build3 (COND_EXPR, atype, length_check,
3402 throw_bad_array_length (),
3403 stmt_expr);
3404 return stmt_expr;
3407 maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
3408 if (TREE_CODE (atype) == ARRAY_TYPE)
3410 ptype = build_pointer_type (type);
3411 base = decay_conversion (base, complain);
3412 if (base == error_mark_node)
3413 return error_mark_node;
3414 base = cp_convert (ptype, base, complain);
3416 else
3417 ptype = atype;
3419 /* The code we are generating looks like:
3421 T* t1 = (T*) base;
3422 T* rval = t1;
3423 ptrdiff_t iterator = maxindex;
3424 try {
3425 for (; iterator != -1; --iterator) {
3426 ... initialize *t1 ...
3427 ++t1;
3429 } catch (...) {
3430 ... destroy elements that were constructed ...
3432 rval;
3435 We can omit the try and catch blocks if we know that the
3436 initialization will never throw an exception, or if the array
3437 elements do not have destructors. We can omit the loop completely if
3438 the elements of the array do not have constructors.
3440 We actually wrap the entire body of the above in a STMT_EXPR, for
3441 tidiness.
3443 When copying from array to another, when the array elements have
3444 only trivial copy constructors, we should use __builtin_memcpy
3445 rather than generating a loop. That way, we could take advantage
3446 of whatever cleverness the back end has for dealing with copies
3447 of blocks of memory. */
3449 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
3450 destroy_temps = stmts_are_full_exprs_p ();
3451 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3452 rval = get_temp_regvar (ptype, base);
3453 base = get_temp_regvar (ptype, rval);
3454 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
3456 /* If initializing one array from another, initialize element by
3457 element. We rely upon the below calls to do the argument
3458 checking. Evaluate the initializer before entering the try block. */
3459 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
3461 if (lvalue_kind (init) & clk_rvalueref)
3462 xvalue = true;
3463 base2 = decay_conversion (init, complain);
3464 if (base2 == error_mark_node)
3465 return error_mark_node;
3466 itype = TREE_TYPE (base2);
3467 base2 = get_temp_regvar (itype, base2);
3468 itype = TREE_TYPE (itype);
3471 /* Protect the entire array initialization so that we can destroy
3472 the partially constructed array if an exception is thrown.
3473 But don't do this if we're assigning. */
3474 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3475 && from_array != 2)
3477 try_block = begin_try_block ();
3480 /* If the initializer is {}, then all elements are initialized from {}.
3481 But for non-classes, that's the same as value-initialization. */
3482 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
3483 && CONSTRUCTOR_NELTS (init) == 0)
3485 if (CLASS_TYPE_P (type))
3486 /* Leave init alone. */;
3487 else
3489 init = NULL_TREE;
3490 explicit_value_init_p = true;
3494 /* Maybe pull out constant value when from_array? */
3496 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
3498 /* Do non-default initialization of non-trivial arrays resulting from
3499 brace-enclosed initializers. */
3500 unsigned HOST_WIDE_INT idx;
3501 tree field, elt;
3502 /* Should we try to create a constant initializer? */
3503 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
3504 && TREE_CONSTANT (maxindex)
3505 && (literal_type_p (inner_elt_type)
3506 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
3507 /* If the constructor already has the array type, it's been through
3508 digest_init, so we shouldn't try to do anything more. */
3509 bool digested = same_type_p (atype, TREE_TYPE (init));
3510 bool saw_non_const = false;
3511 bool saw_const = false;
3512 /* If we're initializing a static array, we want to do static
3513 initialization of any elements with constant initializers even if
3514 some are non-constant. */
3515 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
3516 vec<constructor_elt, va_gc> *new_vec;
3517 from_array = 0;
3519 if (length_check)
3521 tree throw_call;
3522 if (array_of_runtime_bound_p (atype))
3523 throw_call = throw_bad_array_length ();
3524 else
3525 throw_call = throw_bad_array_new_length ();
3526 length_check = build3 (COND_EXPR, void_type_node, length_check,
3527 throw_call, void_zero_node);
3528 finish_expr_stmt (length_check);
3531 if (try_const)
3532 vec_alloc (new_vec, CONSTRUCTOR_NELTS (init));
3533 else
3534 new_vec = NULL;
3536 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
3538 tree baseref = build1 (INDIRECT_REF, type, base);
3539 tree one_init;
3541 num_initialized_elts++;
3543 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3544 if (digested)
3545 one_init = build2 (INIT_EXPR, type, baseref, elt);
3546 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3547 one_init = build_aggr_init (baseref, elt, 0, complain);
3548 else
3549 one_init = cp_build_modify_expr (baseref, NOP_EXPR,
3550 elt, complain);
3551 if (one_init == error_mark_node)
3552 errors = true;
3553 if (try_const)
3555 tree e = one_init;
3556 if (TREE_CODE (e) == EXPR_STMT)
3557 e = TREE_OPERAND (e, 0);
3558 if (TREE_CODE (e) == CONVERT_EXPR
3559 && VOID_TYPE_P (TREE_TYPE (e)))
3560 e = TREE_OPERAND (e, 0);
3561 e = maybe_constant_init (e);
3562 if (reduced_constant_expression_p (e))
3564 CONSTRUCTOR_APPEND_ELT (new_vec, field, e);
3565 if (do_static_init)
3566 one_init = NULL_TREE;
3567 else
3568 one_init = build2 (INIT_EXPR, type, baseref, e);
3569 saw_const = true;
3571 else
3573 if (do_static_init)
3575 tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
3576 true);
3577 if (value)
3578 CONSTRUCTOR_APPEND_ELT (new_vec, field, value);
3580 saw_non_const = true;
3584 if (one_init)
3585 finish_expr_stmt (one_init);
3586 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3588 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, 0, complain);
3589 if (one_init == error_mark_node)
3590 errors = true;
3591 else
3592 finish_expr_stmt (one_init);
3594 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3595 complain);
3596 if (one_init == error_mark_node)
3597 errors = true;
3598 else
3599 finish_expr_stmt (one_init);
3602 if (try_const)
3604 if (!saw_non_const)
3605 const_init = build_constructor (atype, new_vec);
3606 else if (do_static_init && saw_const)
3607 DECL_INITIAL (obase) = build_constructor (atype, new_vec);
3608 else
3609 vec_free (new_vec);
3612 /* Any elements without explicit initializers get {}. */
3613 if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type))
3614 init = build_constructor (init_list_type_node, NULL);
3615 else
3617 init = NULL_TREE;
3618 explicit_value_init_p = true;
3621 else if (from_array)
3623 if (init)
3624 /* OK, we set base2 above. */;
3625 else if (CLASS_TYPE_P (type)
3626 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3628 if (complain & tf_error)
3629 error ("initializer ends prematurely");
3630 errors = true;
3634 /* Now, default-initialize any remaining elements. We don't need to
3635 do that if a) the type does not need constructing, or b) we've
3636 already initialized all the elements.
3638 We do need to keep going if we're copying an array. */
3640 if (from_array
3641 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
3642 && ! (host_integerp (maxindex, 0)
3643 && (num_initialized_elts
3644 == tree_low_cst (maxindex, 0) + 1))))
3646 /* If the ITERATOR is equal to -1, then we don't have to loop;
3647 we've already initialized all the elements. */
3648 tree for_stmt;
3649 tree elt_init;
3650 tree to;
3652 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
3653 finish_for_init_stmt (for_stmt);
3654 finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
3655 build_int_cst (TREE_TYPE (iterator), -1)),
3656 for_stmt);
3657 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3658 complain);
3659 if (elt_init == error_mark_node)
3660 errors = true;
3661 finish_for_expr (elt_init, for_stmt);
3663 to = build1 (INDIRECT_REF, type, base);
3665 if (from_array)
3667 tree from;
3669 if (base2)
3671 from = build1 (INDIRECT_REF, itype, base2);
3672 if (xvalue)
3673 from = move (from);
3675 else
3676 from = NULL_TREE;
3678 if (from_array == 2)
3679 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3680 complain);
3681 else if (type_build_ctor_call (type))
3682 elt_init = build_aggr_init (to, from, 0, complain);
3683 else if (from)
3684 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3685 complain);
3686 else
3687 gcc_unreachable ();
3689 else if (TREE_CODE (type) == ARRAY_TYPE)
3691 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
3692 sorry
3693 ("cannot initialize multi-dimensional array with initializer");
3694 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
3695 0, init,
3696 explicit_value_init_p,
3697 0, complain);
3699 else if (explicit_value_init_p)
3701 elt_init = build_value_init (type, complain);
3702 if (elt_init != error_mark_node)
3703 elt_init = build2 (INIT_EXPR, type, to, elt_init);
3705 else
3707 gcc_assert (type_build_ctor_call (type) || init);
3708 if (CLASS_TYPE_P (type))
3709 elt_init = build_aggr_init (to, init, 0, complain);
3710 else
3712 if (TREE_CODE (init) == TREE_LIST)
3713 init = build_x_compound_expr_from_list (init, ELK_INIT,
3714 complain);
3715 elt_init = build2 (INIT_EXPR, type, to, init);
3719 if (elt_init == error_mark_node)
3720 errors = true;
3722 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3723 finish_expr_stmt (elt_init);
3724 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3726 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
3727 complain));
3728 if (base2)
3729 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0,
3730 complain));
3732 finish_for_stmt (for_stmt);
3735 /* Make sure to cleanup any partially constructed elements. */
3736 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3737 && from_array != 2)
3739 tree e;
3740 tree m = cp_build_binary_op (input_location,
3741 MINUS_EXPR, maxindex, iterator,
3742 complain);
3744 /* Flatten multi-dimensional array since build_vec_delete only
3745 expects one-dimensional array. */
3746 if (TREE_CODE (type) == ARRAY_TYPE)
3747 m = cp_build_binary_op (input_location,
3748 MULT_EXPR, m,
3749 /* Avoid mixing signed and unsigned. */
3750 convert (TREE_TYPE (m),
3751 array_type_nelts_total (type)),
3752 complain);
3754 finish_cleanup_try_block (try_block);
3755 e = build_vec_delete_1 (rval, m,
3756 inner_elt_type, sfk_complete_destructor,
3757 /*use_global_delete=*/0, complain);
3758 if (e == error_mark_node)
3759 errors = true;
3760 finish_cleanup (e, try_block);
3763 /* The value of the array initialization is the array itself, RVAL
3764 is a pointer to the first element. */
3765 finish_stmt_expr_expr (rval, stmt_expr);
3767 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
3769 /* Now make the result have the correct type. */
3770 if (TREE_CODE (atype) == ARRAY_TYPE)
3772 atype = build_pointer_type (atype);
3773 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
3774 stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain);
3775 TREE_NO_WARNING (stmt_expr) = 1;
3778 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3780 if (const_init)
3781 return build2 (INIT_EXPR, atype, obase, const_init);
3782 if (errors)
3783 return error_mark_node;
3784 return stmt_expr;
3787 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3788 build_delete. */
3790 static tree
3791 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
3792 tsubst_flags_t complain)
3794 tree name;
3795 tree fn;
3796 switch (dtor_kind)
3798 case sfk_complete_destructor:
3799 name = complete_dtor_identifier;
3800 break;
3802 case sfk_base_destructor:
3803 name = base_dtor_identifier;
3804 break;
3806 case sfk_deleting_destructor:
3807 name = deleting_dtor_identifier;
3808 break;
3810 default:
3811 gcc_unreachable ();
3813 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
3814 return build_new_method_call (exp, fn,
3815 /*args=*/NULL,
3816 /*conversion_path=*/NULL_TREE,
3817 flags,
3818 /*fn_p=*/NULL,
3819 complain);
3822 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3823 ADDR is an expression which yields the store to be destroyed.
3824 AUTO_DELETE is the name of the destructor to call, i.e., either
3825 sfk_complete_destructor, sfk_base_destructor, or
3826 sfk_deleting_destructor.
3828 FLAGS is the logical disjunction of zero or more LOOKUP_
3829 flags. See cp-tree.h for more info. */
3831 tree
3832 build_delete (tree type, tree addr, special_function_kind auto_delete,
3833 int flags, int use_global_delete, tsubst_flags_t complain)
3835 tree expr;
3837 if (addr == error_mark_node)
3838 return error_mark_node;
3840 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3841 set to `error_mark_node' before it gets properly cleaned up. */
3842 if (type == error_mark_node)
3843 return error_mark_node;
3845 type = TYPE_MAIN_VARIANT (type);
3847 addr = mark_rvalue_use (addr);
3849 if (TYPE_PTR_P (type))
3851 bool complete_p = true;
3853 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3854 if (TREE_CODE (type) == ARRAY_TYPE)
3855 goto handle_array;
3857 /* We don't want to warn about delete of void*, only other
3858 incomplete types. Deleting other incomplete types
3859 invokes undefined behavior, but it is not ill-formed, so
3860 compile to something that would even do The Right Thing
3861 (TM) should the type have a trivial dtor and no delete
3862 operator. */
3863 if (!VOID_TYPE_P (type))
3865 complete_type (type);
3866 if (!COMPLETE_TYPE_P (type))
3868 if ((complain & tf_warning)
3869 && warning (OPT_Wdelete_incomplete,
3870 "possible problem detected in invocation of "
3871 "delete operator:"))
3873 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
3874 inform (input_location,
3875 "neither the destructor nor the class-specific "
3876 "operator delete will be called, even if they are "
3877 "declared when the class is defined");
3879 complete_p = false;
3881 else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor
3882 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
3883 && TYPE_POLYMORPHIC_P (type))
3885 tree dtor;
3886 dtor = CLASSTYPE_DESTRUCTORS (type);
3887 if (!dtor || !DECL_VINDEX (dtor))
3889 if (CLASSTYPE_PURE_VIRTUALS (type))
3890 warning (OPT_Wdelete_non_virtual_dtor,
3891 "deleting object of abstract class type %qT"
3892 " which has non-virtual destructor"
3893 " will cause undefined behaviour", type);
3894 else
3895 warning (OPT_Wdelete_non_virtual_dtor,
3896 "deleting object of polymorphic class type %qT"
3897 " which has non-virtual destructor"
3898 " might cause undefined behaviour", type);
3902 if (VOID_TYPE_P (type) || !complete_p || !MAYBE_CLASS_TYPE_P (type))
3903 /* Call the builtin operator delete. */
3904 return build_builtin_delete_call (addr);
3905 if (TREE_SIDE_EFFECTS (addr))
3906 addr = save_expr (addr);
3908 /* Throw away const and volatile on target type of addr. */
3909 addr = convert_force (build_pointer_type (type), addr, 0, complain);
3911 else if (TREE_CODE (type) == ARRAY_TYPE)
3913 handle_array:
3915 if (TYPE_DOMAIN (type) == NULL_TREE)
3917 if (complain & tf_error)
3918 error ("unknown array size in delete");
3919 return error_mark_node;
3921 return build_vec_delete (addr, array_type_nelts (type),
3922 auto_delete, use_global_delete, complain);
3924 else
3926 /* Don't check PROTECT here; leave that decision to the
3927 destructor. If the destructor is accessible, call it,
3928 else report error. */
3929 addr = cp_build_addr_expr (addr, complain);
3930 if (addr == error_mark_node)
3931 return error_mark_node;
3932 if (TREE_SIDE_EFFECTS (addr))
3933 addr = save_expr (addr);
3935 addr = convert_force (build_pointer_type (type), addr, 0, complain);
3938 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3940 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3942 if (auto_delete != sfk_deleting_destructor)
3943 return void_zero_node;
3945 return build_op_delete_call (DELETE_EXPR, addr,
3946 cxx_sizeof_nowarn (type),
3947 use_global_delete,
3948 /*placement=*/NULL_TREE,
3949 /*alloc_fn=*/NULL_TREE,
3950 complain);
3952 else
3954 tree head = NULL_TREE;
3955 tree do_delete = NULL_TREE;
3956 tree ifexp;
3958 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
3959 lazily_declare_fn (sfk_destructor, type);
3961 /* For `::delete x', we must not use the deleting destructor
3962 since then we would not be sure to get the global `operator
3963 delete'. */
3964 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3966 /* We will use ADDR multiple times so we must save it. */
3967 addr = save_expr (addr);
3968 head = get_target_expr (build_headof (addr));
3969 /* Delete the object. */
3970 do_delete = build_builtin_delete_call (head);
3971 /* Otherwise, treat this like a complete object destructor
3972 call. */
3973 auto_delete = sfk_complete_destructor;
3975 /* If the destructor is non-virtual, there is no deleting
3976 variant. Instead, we must explicitly call the appropriate
3977 `operator delete' here. */
3978 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3979 && auto_delete == sfk_deleting_destructor)
3981 /* We will use ADDR multiple times so we must save it. */
3982 addr = save_expr (addr);
3983 /* Build the call. */
3984 do_delete = build_op_delete_call (DELETE_EXPR,
3985 addr,
3986 cxx_sizeof_nowarn (type),
3987 /*global_p=*/false,
3988 /*placement=*/NULL_TREE,
3989 /*alloc_fn=*/NULL_TREE,
3990 complain);
3991 /* Call the complete object destructor. */
3992 auto_delete = sfk_complete_destructor;
3994 else if (auto_delete == sfk_deleting_destructor
3995 && TYPE_GETS_REG_DELETE (type))
3997 /* Make sure we have access to the member op delete, even though
3998 we'll actually be calling it from the destructor. */
3999 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
4000 /*global_p=*/false,
4001 /*placement=*/NULL_TREE,
4002 /*alloc_fn=*/NULL_TREE,
4003 complain);
4006 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, complain),
4007 auto_delete, flags, complain);
4008 if (expr == error_mark_node)
4009 return error_mark_node;
4010 if (do_delete)
4011 expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
4013 /* We need to calculate this before the dtor changes the vptr. */
4014 if (head)
4015 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
4017 if (flags & LOOKUP_DESTRUCTOR)
4018 /* Explicit destructor call; don't check for null pointer. */
4019 ifexp = integer_one_node;
4020 else
4022 /* Handle deleting a null pointer. */
4023 ifexp = fold (cp_build_binary_op (input_location,
4024 NE_EXPR, addr, nullptr_node,
4025 complain));
4026 if (ifexp == error_mark_node)
4027 return error_mark_node;
4030 if (ifexp != integer_one_node)
4031 expr = build3 (COND_EXPR, void_type_node,
4032 ifexp, expr, void_zero_node);
4034 return expr;
4038 /* At the beginning of a destructor, push cleanups that will call the
4039 destructors for our base classes and members.
4041 Called from begin_destructor_body. */
4043 void
4044 push_base_cleanups (void)
4046 tree binfo, base_binfo;
4047 int i;
4048 tree member;
4049 tree expr;
4050 vec<tree, va_gc> *vbases;
4052 /* Run destructors for all virtual baseclasses. */
4053 if (CLASSTYPE_VBASECLASSES (current_class_type))
4055 tree cond = (condition_conversion
4056 (build2 (BIT_AND_EXPR, integer_type_node,
4057 current_in_charge_parm,
4058 integer_two_node)));
4060 /* The CLASSTYPE_VBASECLASSES vector is in initialization
4061 order, which is also the right order for pushing cleanups. */
4062 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
4063 vec_safe_iterate (vbases, i, &base_binfo); i++)
4065 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
4067 expr = build_special_member_call (current_class_ref,
4068 base_dtor_identifier,
4069 NULL,
4070 base_binfo,
4071 (LOOKUP_NORMAL
4072 | LOOKUP_NONVIRTUAL),
4073 tf_warning_or_error);
4074 expr = build3 (COND_EXPR, void_type_node, cond,
4075 expr, void_zero_node);
4076 finish_decl_cleanup (NULL_TREE, expr);
4081 /* Take care of the remaining baseclasses. */
4082 for (binfo = TYPE_BINFO (current_class_type), i = 0;
4083 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4085 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
4086 || BINFO_VIRTUAL_P (base_binfo))
4087 continue;
4089 expr = build_special_member_call (current_class_ref,
4090 base_dtor_identifier,
4091 NULL, base_binfo,
4092 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
4093 tf_warning_or_error);
4094 finish_decl_cleanup (NULL_TREE, expr);
4097 /* Don't automatically destroy union members. */
4098 if (TREE_CODE (current_class_type) == UNION_TYPE)
4099 return;
4101 for (member = TYPE_FIELDS (current_class_type); member;
4102 member = DECL_CHAIN (member))
4104 tree this_type = TREE_TYPE (member);
4105 if (this_type == error_mark_node
4106 || TREE_CODE (member) != FIELD_DECL
4107 || DECL_ARTIFICIAL (member))
4108 continue;
4109 if (ANON_UNION_TYPE_P (this_type))
4110 continue;
4111 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
4113 tree this_member = (build_class_member_access_expr
4114 (current_class_ref, member,
4115 /*access_path=*/NULL_TREE,
4116 /*preserve_reference=*/false,
4117 tf_warning_or_error));
4118 expr = build_delete (this_type, this_member,
4119 sfk_complete_destructor,
4120 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
4121 0, tf_warning_or_error);
4122 finish_decl_cleanup (NULL_TREE, expr);
4127 /* Build a C++ vector delete expression.
4128 MAXINDEX is the number of elements to be deleted.
4129 ELT_SIZE is the nominal size of each element in the vector.
4130 BASE is the expression that should yield the store to be deleted.
4131 This function expands (or synthesizes) these calls itself.
4132 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
4134 This also calls delete for virtual baseclasses of elements of the vector.
4136 Update: MAXINDEX is no longer needed. The size can be extracted from the
4137 start of the vector for pointers, and from the type for arrays. We still
4138 use MAXINDEX for arrays because it happens to already have one of the
4139 values we'd have to extract. (We could use MAXINDEX with pointers to
4140 confirm the size, and trap if the numbers differ; not clear that it'd
4141 be worth bothering.) */
4143 tree
4144 build_vec_delete (tree base, tree maxindex,
4145 special_function_kind auto_delete_vec,
4146 int use_global_delete, tsubst_flags_t complain)
4148 tree type;
4149 tree rval;
4150 tree base_init = NULL_TREE;
4152 type = TREE_TYPE (base);
4154 if (TYPE_PTR_P (type))
4156 /* Step back one from start of vector, and read dimension. */
4157 tree cookie_addr;
4158 tree size_ptr_type = build_pointer_type (sizetype);
4160 base = mark_rvalue_use (base);
4161 if (TREE_SIDE_EFFECTS (base))
4163 base_init = get_target_expr (base);
4164 base = TARGET_EXPR_SLOT (base_init);
4166 type = strip_array_types (TREE_TYPE (type));
4167 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
4168 sizetype, TYPE_SIZE_UNIT (sizetype));
4169 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
4170 cookie_addr);
4171 maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, complain);
4173 else if (TREE_CODE (type) == ARRAY_TYPE)
4175 /* Get the total number of things in the array, maxindex is a
4176 bad name. */
4177 maxindex = array_type_nelts_total (type);
4178 type = strip_array_types (type);
4179 base = decay_conversion (base, complain);
4180 if (base == error_mark_node)
4181 return error_mark_node;
4182 if (TREE_SIDE_EFFECTS (base))
4184 base_init = get_target_expr (base);
4185 base = TARGET_EXPR_SLOT (base_init);
4188 else
4190 if (base != error_mark_node && !(complain & tf_error))
4191 error ("type to vector delete is neither pointer or array type");
4192 return error_mark_node;
4195 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
4196 use_global_delete, complain);
4197 if (base_init && rval != error_mark_node)
4198 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
4200 return rval;