re PR c++/51399 ([c++0x] [4.7 Regression] ICE with invalid initializer list)
[gcc.git] / gcc / cp / init.c
1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* High-level class interface. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "flags.h"
32 #include "output.h"
33 #include "target.h"
34
35 static bool begin_init_stmts (tree *, tree *);
36 static tree finish_init_stmts (bool, tree, tree);
37 static void construct_virtual_base (tree, tree);
38 static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
39 static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
40 static void perform_member_init (tree, tree);
41 static tree build_builtin_delete_call (tree);
42 static int member_init_ok_or_else (tree, tree, tree);
43 static void expand_virtual_init (tree, tree);
44 static tree sort_mem_initializers (tree, tree);
45 static tree initializing_context (tree);
46 static void expand_cleanup_for_base (tree, tree);
47 static tree dfs_initialize_vtbl_ptrs (tree, void *);
48 static tree build_field_list (tree, tree, int *);
49 static tree build_vtbl_address (tree);
50 static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
51
52 /* We are about to generate some complex initialization code.
53 Conceptually, it is all a single expression. However, we may want
54 to include conditionals, loops, and other such statement-level
55 constructs. Therefore, we build the initialization code inside a
56 statement-expression. This function starts such an expression.
57 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
58 pass them back to finish_init_stmts when the expression is
59 complete. */
60
61 static bool
62 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
63 {
64 bool is_global = !building_stmt_list_p ();
65
66 *stmt_expr_p = begin_stmt_expr ();
67 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
68
69 return is_global;
70 }
71
72 /* Finish out the statement-expression begun by the previous call to
73 begin_init_stmts. Returns the statement-expression itself. */
74
75 static tree
76 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
77 {
78 finish_compound_stmt (compound_stmt);
79
80 stmt_expr = finish_stmt_expr (stmt_expr, true);
81
82 gcc_assert (!building_stmt_list_p () == is_global);
83
84 return stmt_expr;
85 }
86
87 /* Constructors */
88
89 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
90 which we want to initialize the vtable pointer for, DATA is
91 TREE_LIST whose TREE_VALUE is the this ptr expression. */
92
93 static tree
94 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
95 {
96 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
97 return dfs_skip_bases;
98
99 if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
100 {
101 tree base_ptr = TREE_VALUE ((tree) data);
102
103 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
104 tf_warning_or_error);
105
106 expand_virtual_init (binfo, base_ptr);
107 }
108
109 return NULL_TREE;
110 }
111
112 /* Initialize all the vtable pointers in the object pointed to by
113 ADDR. */
114
115 void
116 initialize_vtbl_ptrs (tree addr)
117 {
118 tree list;
119 tree type;
120
121 type = TREE_TYPE (TREE_TYPE (addr));
122 list = build_tree_list (type, addr);
123
124 /* Walk through the hierarchy, initializing the vptr in each base
125 class. We do these in pre-order because we can't find the virtual
126 bases for a class until we've initialized the vtbl for that
127 class. */
128 dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
129 }
130
131 /* Return an expression for the zero-initialization of an object with
132 type T. This expression will either be a constant (in the case
133 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
134 aggregate), or NULL (in the case that T does not require
135 initialization). In either case, the value can be used as
136 DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
137 initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
138 is the number of elements in the array. If STATIC_STORAGE_P is
139 TRUE, initializers are only generated for entities for which
140 zero-initialization does not simply mean filling the storage with
141 zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
142 subfields with bit positions at or above that bit size shouldn't
143 be added. */
144
145 static tree
146 build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
147 tree field_size)
148 {
149 tree init = NULL_TREE;
150
151 /* [dcl.init]
152
153 To zero-initialize an object of type T means:
154
155 -- if T is a scalar type, the storage is set to the value of zero
156 converted to T.
157
158 -- if T is a non-union class type, the storage for each nonstatic
159 data member and each base-class subobject is zero-initialized.
160
161 -- if T is a union type, the storage for its first data member is
162 zero-initialized.
163
164 -- if T is an array type, the storage for each element is
165 zero-initialized.
166
167 -- if T is a reference type, no initialization is performed. */
168
169 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
170
171 if (type == error_mark_node)
172 ;
173 else if (static_storage_p && zero_init_p (type))
174 /* In order to save space, we do not explicitly build initializers
175 for items that do not need them. GCC's semantics are that
176 items with static storage duration that are not otherwise
177 initialized are initialized to zero. */
178 ;
179 else if (TYPE_PTR_P (type) || TYPE_PTR_TO_MEMBER_P (type))
180 init = convert (type, nullptr_node);
181 else if (SCALAR_TYPE_P (type))
182 init = convert (type, integer_zero_node);
183 else if (CLASS_TYPE_P (type))
184 {
185 tree field;
186 VEC(constructor_elt,gc) *v = NULL;
187
188 /* Iterate over the fields, building initializations. */
189 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
190 {
191 if (TREE_CODE (field) != FIELD_DECL)
192 continue;
193
194 /* Don't add virtual bases for base classes if they are beyond
195 the size of the current field, that means it is present
196 somewhere else in the object. */
197 if (field_size)
198 {
199 tree bitpos = bit_position (field);
200 if (TREE_CODE (bitpos) == INTEGER_CST
201 && !tree_int_cst_lt (bitpos, field_size))
202 continue;
203 }
204
205 /* Note that for class types there will be FIELD_DECLs
206 corresponding to base classes as well. Thus, iterating
207 over TYPE_FIELDs will result in correct initialization of
208 all of the subobjects. */
209 if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
210 {
211 tree new_field_size
212 = (DECL_FIELD_IS_BASE (field)
213 && DECL_SIZE (field)
214 && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
215 ? DECL_SIZE (field) : NULL_TREE;
216 tree value = build_zero_init_1 (TREE_TYPE (field),
217 /*nelts=*/NULL_TREE,
218 static_storage_p,
219 new_field_size);
220 if (value)
221 CONSTRUCTOR_APPEND_ELT(v, field, value);
222 }
223
224 /* For unions, only the first field is initialized. */
225 if (TREE_CODE (type) == UNION_TYPE)
226 break;
227 }
228
229 /* Build a constructor to contain the initializations. */
230 init = build_constructor (type, v);
231 }
232 else if (TREE_CODE (type) == ARRAY_TYPE)
233 {
234 tree max_index;
235 VEC(constructor_elt,gc) *v = NULL;
236
237 /* Iterate over the array elements, building initializations. */
238 if (nelts)
239 max_index = fold_build2_loc (input_location,
240 MINUS_EXPR, TREE_TYPE (nelts),
241 nelts, integer_one_node);
242 else
243 max_index = array_type_nelts (type);
244
245 /* If we have an error_mark here, we should just return error mark
246 as we don't know the size of the array yet. */
247 if (max_index == error_mark_node)
248 return error_mark_node;
249 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
250
251 /* A zero-sized array, which is accepted as an extension, will
252 have an upper bound of -1. */
253 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
254 {
255 constructor_elt *ce;
256
257 v = VEC_alloc (constructor_elt, gc, 1);
258 ce = VEC_quick_push (constructor_elt, v, NULL);
259
260 /* If this is a one element array, we just use a regular init. */
261 if (tree_int_cst_equal (size_zero_node, max_index))
262 ce->index = size_zero_node;
263 else
264 ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
265 max_index);
266
267 ce->value = build_zero_init_1 (TREE_TYPE (type),
268 /*nelts=*/NULL_TREE,
269 static_storage_p, NULL_TREE);
270 }
271
272 /* Build a constructor to contain the initializations. */
273 init = build_constructor (type, v);
274 }
275 else if (TREE_CODE (type) == VECTOR_TYPE)
276 init = build_zero_cst (type);
277 else
278 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
279
280 /* In all cases, the initializer is a constant. */
281 if (init)
282 TREE_CONSTANT (init) = 1;
283
284 return init;
285 }
286
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. */
298
299 tree
300 build_zero_init (tree type, tree nelts, bool static_storage_p)
301 {
302 return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
303 }
304
305 /* Return a suitable initializer for value-initializing an object of type
306 TYPE, as described in [dcl.init]. */
307
308 tree
309 build_value_init (tree type, tsubst_flags_t complain)
310 {
311 /* [dcl.init]
312
313 To value-initialize an object of type T means:
314
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);
319
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)
323
324 - if T is an array type, then each element is value-initialized;
325
326 - otherwise, the object is zero-initialized.
327
328 A program that calls for default-initialization or
329 value-initialization of an entity of reference type is ill-formed.
330
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. */
334
335 /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
336 gcc_assert (!processing_template_decl || SCALAR_TYPE_P (type));
337
338 if (CLASS_TYPE_P (type))
339 {
340 /* Instead of the above, only consider the user-providedness of the
341 default constructor itself so value-initializing a class with an
342 explicitly defaulted default constructor and another user-provided
343 constructor works properly (c++std-core-19883). */
344 if (type_has_user_provided_default_constructor (type)
345 || (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
346 && type_has_user_provided_constructor (type)))
347 return build_aggr_init_expr
348 (type,
349 build_special_member_call (NULL_TREE, complete_ctor_identifier,
350 NULL, type, LOOKUP_NORMAL,
351 complain),
352 complain);
353 else if (TYPE_HAS_COMPLEX_DFLT (type))
354 {
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 if (ctor != error_mark_node)
363 {
364 ctor = build_aggr_init_expr (type, ctor, complain);
365 AGGR_INIT_ZERO_FIRST (ctor) = 1;
366 }
367 return ctor;
368 }
369 }
370 return build_value_init_noctor (type, complain);
371 }
372
373 /* Like build_value_init, but don't call the constructor for TYPE. Used
374 for base initializers. */
375
376 tree
377 build_value_init_noctor (tree type, tsubst_flags_t complain)
378 {
379 /* FIXME the class and array cases should just use digest_init once it is
380 SFINAE-enabled. */
381 if (CLASS_TYPE_P (type))
382 {
383 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type));
384
385 if (TREE_CODE (type) != UNION_TYPE)
386 {
387 tree field;
388 VEC(constructor_elt,gc) *v = NULL;
389
390 /* Iterate over the fields, building initializations. */
391 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
392 {
393 tree ftype, value;
394
395 if (TREE_CODE (field) != FIELD_DECL)
396 continue;
397
398 ftype = TREE_TYPE (field);
399
400 /* We could skip vfields and fields of types with
401 user-defined constructors, but I think that won't improve
402 performance at all; it should be simpler in general just
403 to zero out the entire object than try to only zero the
404 bits that actually need it. */
405
406 /* Note that for class types there will be FIELD_DECLs
407 corresponding to base classes as well. Thus, iterating
408 over TYPE_FIELDs will result in correct initialization of
409 all of the subobjects. */
410 value = build_value_init (ftype, complain);
411
412 if (value == error_mark_node)
413 return error_mark_node;
414
415 if (value)
416 CONSTRUCTOR_APPEND_ELT(v, field, value);
417 }
418
419 /* Build a constructor to contain the zero- initializations. */
420 return build_constructor (type, v);
421 }
422 }
423 else if (TREE_CODE (type) == ARRAY_TYPE)
424 {
425 VEC(constructor_elt,gc) *v = NULL;
426
427 /* Iterate over the array elements, building initializations. */
428 tree max_index = array_type_nelts (type);
429
430 /* If we have an error_mark here, we should just return error mark
431 as we don't know the size of the array yet. */
432 if (max_index == error_mark_node)
433 {
434 if (complain & tf_error)
435 error ("cannot value-initialize array of unknown bound %qT",
436 type);
437 return error_mark_node;
438 }
439 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
440
441 /* A zero-sized array, which is accepted as an extension, will
442 have an upper bound of -1. */
443 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
444 {
445 constructor_elt *ce;
446
447 v = VEC_alloc (constructor_elt, gc, 1);
448 ce = VEC_quick_push (constructor_elt, v, NULL);
449
450 /* If this is a one element array, we just use a regular init. */
451 if (tree_int_cst_equal (size_zero_node, max_index))
452 ce->index = size_zero_node;
453 else
454 ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
455 max_index);
456
457 ce->value = build_value_init (TREE_TYPE (type), complain);
458
459 if (ce->value == error_mark_node)
460 return error_mark_node;
461
462 /* We shouldn't have gotten here for anything that would need
463 non-trivial initialization, and gimplify_init_ctor_preeval
464 would need to be fixed to allow it. */
465 gcc_assert (TREE_CODE (ce->value) != TARGET_EXPR
466 && TREE_CODE (ce->value) != AGGR_INIT_EXPR);
467 }
468
469 /* Build a constructor to contain the initializations. */
470 return build_constructor (type, v);
471 }
472 else if (TREE_CODE (type) == FUNCTION_TYPE)
473 {
474 if (complain & tf_error)
475 error ("value-initialization of function type %qT", type);
476 return error_mark_node;
477 }
478 else if (TREE_CODE (type) == REFERENCE_TYPE)
479 {
480 if (complain & tf_error)
481 error ("value-initialization of reference type %qT", type);
482 return error_mark_node;
483 }
484
485 return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
486 }
487
488 /* Initialize current class with INIT, a TREE_LIST of
489 arguments for a target constructor. If TREE_LIST is void_type_node,
490 an empty initializer list was given. */
491
492 static void
493 perform_target_ctor (tree init)
494 {
495 tree decl = current_class_ref;
496 tree type = current_class_type;
497
498 finish_expr_stmt (build_aggr_init (decl, init, LOOKUP_NORMAL,
499 tf_warning_or_error));
500 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
501 {
502 tree expr = build_delete (type, decl, sfk_complete_destructor,
503 LOOKUP_NORMAL
504 |LOOKUP_NONVIRTUAL
505 |LOOKUP_DESTRUCTOR,
506 0, tf_warning_or_error);
507 if (expr != error_mark_node)
508 finish_eh_cleanup (expr);
509 }
510 }
511
512 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
513 arguments. If TREE_LIST is void_type_node, an empty initializer
514 list was given; if NULL_TREE no initializer was given. */
515
516 static void
517 perform_member_init (tree member, tree init)
518 {
519 tree decl;
520 tree type = TREE_TYPE (member);
521
522 /* Use the non-static data member initializer if there was no
523 mem-initializer for this field. */
524 if (init == NULL_TREE)
525 {
526 if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
527 /* Do deferred instantiation of the NSDMI. */
528 init = (tsubst_copy_and_build
529 (DECL_INITIAL (DECL_TI_TEMPLATE (member)),
530 DECL_TI_ARGS (member),
531 tf_warning_or_error, member, /*function_p=*/false,
532 /*integral_constant_expression_p=*/false));
533 else
534 {
535 init = DECL_INITIAL (member);
536 /* Strip redundant TARGET_EXPR so we don't need to remap it, and
537 so the aggregate init code below will see a CONSTRUCTOR. */
538 if (init && TREE_CODE (init) == TARGET_EXPR
539 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
540 init = TARGET_EXPR_INITIAL (init);
541 init = break_out_target_exprs (init);
542 }
543 }
544
545 if (init == error_mark_node)
546 return;
547
548 /* Effective C++ rule 12 requires that all data members be
549 initialized. */
550 if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
551 warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
552 "%qD should be initialized in the member initialization list",
553 member);
554
555 /* Get an lvalue for the data member. */
556 decl = build_class_member_access_expr (current_class_ref, member,
557 /*access_path=*/NULL_TREE,
558 /*preserve_reference=*/true,
559 tf_warning_or_error);
560 if (decl == error_mark_node)
561 return;
562
563 if (warn_init_self && init && TREE_CODE (init) == TREE_LIST
564 && TREE_CHAIN (init) == NULL_TREE)
565 {
566 tree val = TREE_VALUE (init);
567 if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
568 && TREE_OPERAND (val, 0) == current_class_ref)
569 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
570 OPT_Wuninitialized, "%qD is initialized with itself",
571 member);
572 }
573
574 if (init == void_type_node)
575 {
576 /* mem() means value-initialization. */
577 if (TREE_CODE (type) == ARRAY_TYPE)
578 {
579 init = build_vec_init_expr (type, init, tf_warning_or_error);
580 init = build2 (INIT_EXPR, type, decl, init);
581 finish_expr_stmt (init);
582 }
583 else
584 {
585 tree value = build_value_init (type, tf_warning_or_error);
586 if (value == error_mark_node)
587 return;
588 init = build2 (INIT_EXPR, type, decl, value);
589 finish_expr_stmt (init);
590 }
591 }
592 /* Deal with this here, as we will get confused if we try to call the
593 assignment op for an anonymous union. This can happen in a
594 synthesized copy constructor. */
595 else if (ANON_AGGR_TYPE_P (type))
596 {
597 if (init)
598 {
599 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
600 finish_expr_stmt (init);
601 }
602 }
603 else if (init
604 && (TREE_CODE (type) == REFERENCE_TYPE
605 /* Pre-digested NSDMI. */
606 || (((TREE_CODE (init) == CONSTRUCTOR
607 && TREE_TYPE (init) == type)
608 /* { } mem-initializer. */
609 || (TREE_CODE (init) == TREE_LIST
610 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
611 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init))))
612 && (CP_AGGREGATE_TYPE_P (type)
613 || is_std_init_list (type)))))
614 {
615 /* With references and list-initialization, we need to deal with
616 extending temporary lifetimes. 12.2p5: "A temporary bound to a
617 reference member in a constructor’s ctor-initializer (12.6.2)
618 persists until the constructor exits." */
619 unsigned i; tree t;
620 VEC(tree,gc) *cleanups = make_tree_vector ();
621 if (TREE_CODE (init) == TREE_LIST)
622 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
623 tf_warning_or_error);
624 if (TREE_TYPE (init) != type)
625 init = digest_init (type, init, tf_warning_or_error);
626 if (init == error_mark_node)
627 return;
628 /* A FIELD_DECL doesn't really have a suitable lifetime, but
629 make_temporary_var_for_ref_to_temp will treat it as automatic and
630 set_up_extended_ref_temp wants to use the decl in a warning. */
631 init = extend_ref_init_temps (member, init, &cleanups);
632 if (TREE_CODE (type) == ARRAY_TYPE
633 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
634 init = build_vec_init_expr (type, init, tf_warning_or_error);
635 init = build2 (INIT_EXPR, type, decl, init);
636 finish_expr_stmt (init);
637 FOR_EACH_VEC_ELT (tree, cleanups, i, t)
638 push_cleanup (decl, t, false);
639 release_tree_vector (cleanups);
640 }
641 else if (type_build_ctor_call (type)
642 || (init && CLASS_TYPE_P (strip_array_types (type))))
643 {
644 if (TREE_CODE (type) == ARRAY_TYPE)
645 {
646 if (init)
647 {
648 if (TREE_CHAIN (init))
649 init = error_mark_node;
650 else
651 init = TREE_VALUE (init);
652 if (BRACE_ENCLOSED_INITIALIZER_P (init))
653 init = digest_init (type, init, tf_warning_or_error);
654 }
655 if (init == NULL_TREE
656 || same_type_ignoring_top_level_qualifiers_p (type,
657 TREE_TYPE (init)))
658 {
659 init = build_vec_init_expr (type, init, tf_warning_or_error);
660 init = build2 (INIT_EXPR, type, decl, init);
661 finish_expr_stmt (init);
662 }
663 else
664 error ("invalid initializer for array member %q#D", member);
665 }
666 else
667 {
668 int flags = LOOKUP_NORMAL;
669 if (DECL_DEFAULTED_FN (current_function_decl))
670 flags |= LOOKUP_DEFAULTED;
671 if (CP_TYPE_CONST_P (type)
672 && init == NULL_TREE
673 && default_init_uninitialized_part (type))
674 /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
675 vtable; still give this diagnostic. */
676 permerror (DECL_SOURCE_LOCATION (current_function_decl),
677 "uninitialized member %qD with %<const%> type %qT",
678 member, type);
679 finish_expr_stmt (build_aggr_init (decl, init, flags,
680 tf_warning_or_error));
681 }
682 }
683 else
684 {
685 if (init == NULL_TREE)
686 {
687 tree core_type;
688 /* member traversal: note it leaves init NULL */
689 if (TREE_CODE (type) == REFERENCE_TYPE)
690 permerror (DECL_SOURCE_LOCATION (current_function_decl),
691 "uninitialized reference member %qD",
692 member);
693 else if (CP_TYPE_CONST_P (type))
694 permerror (DECL_SOURCE_LOCATION (current_function_decl),
695 "uninitialized member %qD with %<const%> type %qT",
696 member, type);
697
698 core_type = strip_array_types (type);
699
700 if (CLASS_TYPE_P (core_type)
701 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
702 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
703 diagnose_uninitialized_cst_or_ref_member (core_type,
704 /*using_new=*/false,
705 /*complain=*/true);
706 }
707 else if (TREE_CODE (init) == TREE_LIST)
708 /* There was an explicit member initialization. Do some work
709 in that case. */
710 init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
711 tf_warning_or_error);
712
713 if (init)
714 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
715 tf_warning_or_error));
716 }
717
718 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
719 {
720 tree expr;
721
722 expr = build_class_member_access_expr (current_class_ref, member,
723 /*access_path=*/NULL_TREE,
724 /*preserve_reference=*/false,
725 tf_warning_or_error);
726 expr = build_delete (type, expr, sfk_complete_destructor,
727 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
728 tf_warning_or_error);
729
730 if (expr != error_mark_node)
731 finish_eh_cleanup (expr);
732 }
733 }
734
735 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
736 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
737
738 static tree
739 build_field_list (tree t, tree list, int *uses_unions_p)
740 {
741 tree fields;
742
743 /* Note whether or not T is a union. */
744 if (TREE_CODE (t) == UNION_TYPE)
745 *uses_unions_p = 1;
746
747 for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
748 {
749 tree fieldtype;
750
751 /* Skip CONST_DECLs for enumeration constants and so forth. */
752 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
753 continue;
754
755 fieldtype = TREE_TYPE (fields);
756 /* Keep track of whether or not any fields are unions. */
757 if (TREE_CODE (fieldtype) == UNION_TYPE)
758 *uses_unions_p = 1;
759
760 /* For an anonymous struct or union, we must recursively
761 consider the fields of the anonymous type. They can be
762 directly initialized from the constructor. */
763 if (ANON_AGGR_TYPE_P (fieldtype))
764 {
765 /* Add this field itself. Synthesized copy constructors
766 initialize the entire aggregate. */
767 list = tree_cons (fields, NULL_TREE, list);
768 /* And now add the fields in the anonymous aggregate. */
769 list = build_field_list (fieldtype, list, uses_unions_p);
770 }
771 /* Add this field. */
772 else if (DECL_NAME (fields))
773 list = tree_cons (fields, NULL_TREE, list);
774 }
775
776 return list;
777 }
778
779 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
780 a FIELD_DECL or BINFO in T that needs initialization. The
781 TREE_VALUE gives the initializer, or list of initializer arguments.
782
783 Return a TREE_LIST containing all of the initializations required
784 for T, in the order in which they should be performed. The output
785 list has the same format as the input. */
786
787 static tree
788 sort_mem_initializers (tree t, tree mem_inits)
789 {
790 tree init;
791 tree base, binfo, base_binfo;
792 tree sorted_inits;
793 tree next_subobject;
794 VEC(tree,gc) *vbases;
795 int i;
796 int uses_unions_p = 0;
797
798 /* Build up a list of initializations. The TREE_PURPOSE of entry
799 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
800 TREE_VALUE will be the constructor arguments, or NULL if no
801 explicit initialization was provided. */
802 sorted_inits = NULL_TREE;
803
804 /* Process the virtual bases. */
805 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
806 VEC_iterate (tree, vbases, i, base); i++)
807 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
808
809 /* Process the direct bases. */
810 for (binfo = TYPE_BINFO (t), i = 0;
811 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
812 if (!BINFO_VIRTUAL_P (base_binfo))
813 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
814
815 /* Process the non-static data members. */
816 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
817 /* Reverse the entire list of initializations, so that they are in
818 the order that they will actually be performed. */
819 sorted_inits = nreverse (sorted_inits);
820
821 /* If the user presented the initializers in an order different from
822 that in which they will actually occur, we issue a warning. Keep
823 track of the next subobject which can be explicitly initialized
824 without issuing a warning. */
825 next_subobject = sorted_inits;
826
827 /* Go through the explicit initializers, filling in TREE_PURPOSE in
828 the SORTED_INITS. */
829 for (init = mem_inits; init; init = TREE_CHAIN (init))
830 {
831 tree subobject;
832 tree subobject_init;
833
834 subobject = TREE_PURPOSE (init);
835
836 /* If the explicit initializers are in sorted order, then
837 SUBOBJECT will be NEXT_SUBOBJECT, or something following
838 it. */
839 for (subobject_init = next_subobject;
840 subobject_init;
841 subobject_init = TREE_CHAIN (subobject_init))
842 if (TREE_PURPOSE (subobject_init) == subobject)
843 break;
844
845 /* Issue a warning if the explicit initializer order does not
846 match that which will actually occur.
847 ??? Are all these on the correct lines? */
848 if (warn_reorder && !subobject_init)
849 {
850 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
851 warning (OPT_Wreorder, "%q+D will be initialized after",
852 TREE_PURPOSE (next_subobject));
853 else
854 warning (OPT_Wreorder, "base %qT will be initialized after",
855 TREE_PURPOSE (next_subobject));
856 if (TREE_CODE (subobject) == FIELD_DECL)
857 warning (OPT_Wreorder, " %q+#D", subobject);
858 else
859 warning (OPT_Wreorder, " base %qT", subobject);
860 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
861 OPT_Wreorder, " when initialized here");
862 }
863
864 /* Look again, from the beginning of the list. */
865 if (!subobject_init)
866 {
867 subobject_init = sorted_inits;
868 while (TREE_PURPOSE (subobject_init) != subobject)
869 subobject_init = TREE_CHAIN (subobject_init);
870 }
871
872 /* It is invalid to initialize the same subobject more than
873 once. */
874 if (TREE_VALUE (subobject_init))
875 {
876 if (TREE_CODE (subobject) == FIELD_DECL)
877 error_at (DECL_SOURCE_LOCATION (current_function_decl),
878 "multiple initializations given for %qD",
879 subobject);
880 else
881 error_at (DECL_SOURCE_LOCATION (current_function_decl),
882 "multiple initializations given for base %qT",
883 subobject);
884 }
885
886 /* Record the initialization. */
887 TREE_VALUE (subobject_init) = TREE_VALUE (init);
888 next_subobject = subobject_init;
889 }
890
891 /* [class.base.init]
892
893 If a ctor-initializer specifies more than one mem-initializer for
894 multiple members of the same union (including members of
895 anonymous unions), the ctor-initializer is ill-formed.
896
897 Here we also splice out uninitialized union members. */
898 if (uses_unions_p)
899 {
900 tree last_field = NULL_TREE;
901 tree *p;
902 for (p = &sorted_inits; *p; )
903 {
904 tree field;
905 tree ctx;
906 int done;
907
908 init = *p;
909
910 field = TREE_PURPOSE (init);
911
912 /* Skip base classes. */
913 if (TREE_CODE (field) != FIELD_DECL)
914 goto next;
915
916 /* If this is an anonymous union with no explicit initializer,
917 splice it out. */
918 if (!TREE_VALUE (init) && ANON_UNION_TYPE_P (TREE_TYPE (field)))
919 goto splice;
920
921 /* See if this field is a member of a union, or a member of a
922 structure contained in a union, etc. */
923 for (ctx = DECL_CONTEXT (field);
924 !same_type_p (ctx, t);
925 ctx = TYPE_CONTEXT (ctx))
926 if (TREE_CODE (ctx) == UNION_TYPE)
927 break;
928 /* If this field is not a member of a union, skip it. */
929 if (TREE_CODE (ctx) != UNION_TYPE)
930 goto next;
931
932 /* If this union member has no explicit initializer, splice
933 it out. */
934 if (!TREE_VALUE (init))
935 goto splice;
936
937 /* It's only an error if we have two initializers for the same
938 union type. */
939 if (!last_field)
940 {
941 last_field = field;
942 goto next;
943 }
944
945 /* See if LAST_FIELD and the field initialized by INIT are
946 members of the same union. If so, there's a problem,
947 unless they're actually members of the same structure
948 which is itself a member of a union. For example, given:
949
950 union { struct { int i; int j; }; };
951
952 initializing both `i' and `j' makes sense. */
953 ctx = DECL_CONTEXT (field);
954 done = 0;
955 do
956 {
957 tree last_ctx;
958
959 last_ctx = DECL_CONTEXT (last_field);
960 while (1)
961 {
962 if (same_type_p (last_ctx, ctx))
963 {
964 if (TREE_CODE (ctx) == UNION_TYPE)
965 error_at (DECL_SOURCE_LOCATION (current_function_decl),
966 "initializations for multiple members of %qT",
967 last_ctx);
968 done = 1;
969 break;
970 }
971
972 if (same_type_p (last_ctx, t))
973 break;
974
975 last_ctx = TYPE_CONTEXT (last_ctx);
976 }
977
978 /* If we've reached the outermost class, then we're
979 done. */
980 if (same_type_p (ctx, t))
981 break;
982
983 ctx = TYPE_CONTEXT (ctx);
984 }
985 while (!done);
986
987 last_field = field;
988
989 next:
990 p = &TREE_CHAIN (*p);
991 continue;
992 splice:
993 *p = TREE_CHAIN (*p);
994 continue;
995 }
996 }
997
998 return sorted_inits;
999 }
1000
1001 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
1002 is a TREE_LIST giving the explicit mem-initializer-list for the
1003 constructor. The TREE_PURPOSE of each entry is a subobject (a
1004 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
1005 is a TREE_LIST giving the arguments to the constructor or
1006 void_type_node for an empty list of arguments. */
1007
1008 void
1009 emit_mem_initializers (tree mem_inits)
1010 {
1011 int flags = LOOKUP_NORMAL;
1012
1013 /* We will already have issued an error message about the fact that
1014 the type is incomplete. */
1015 if (!COMPLETE_TYPE_P (current_class_type))
1016 return;
1017
1018 if (mem_inits
1019 && TYPE_P (TREE_PURPOSE (mem_inits))
1020 && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
1021 {
1022 /* Delegating constructor. */
1023 gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
1024 perform_target_ctor (TREE_VALUE (mem_inits));
1025 return;
1026 }
1027
1028 if (DECL_DEFAULTED_FN (current_function_decl))
1029 flags |= LOOKUP_DEFAULTED;
1030
1031 /* Sort the mem-initializers into the order in which the
1032 initializations should be performed. */
1033 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
1034
1035 in_base_initializer = 1;
1036
1037 /* Initialize base classes. */
1038 while (mem_inits
1039 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL)
1040 {
1041 tree subobject = TREE_PURPOSE (mem_inits);
1042 tree arguments = TREE_VALUE (mem_inits);
1043
1044 if (arguments == NULL_TREE)
1045 {
1046 /* If these initializations are taking place in a copy constructor,
1047 the base class should probably be explicitly initialized if there
1048 is a user-defined constructor in the base class (other than the
1049 default constructor, which will be called anyway). */
1050 if (extra_warnings
1051 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
1052 && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
1053 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
1054 OPT_Wextra, "base class %q#T should be explicitly "
1055 "initialized in the copy constructor",
1056 BINFO_TYPE (subobject));
1057 }
1058
1059 /* Initialize the base. */
1060 if (BINFO_VIRTUAL_P (subobject))
1061 construct_virtual_base (subobject, arguments);
1062 else
1063 {
1064 tree base_addr;
1065
1066 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
1067 subobject, 1, tf_warning_or_error);
1068 expand_aggr_init_1 (subobject, NULL_TREE,
1069 cp_build_indirect_ref (base_addr, RO_NULL,
1070 tf_warning_or_error),
1071 arguments,
1072 flags,
1073 tf_warning_or_error);
1074 expand_cleanup_for_base (subobject, NULL_TREE);
1075 }
1076
1077 mem_inits = TREE_CHAIN (mem_inits);
1078 }
1079 in_base_initializer = 0;
1080
1081 /* Initialize the vptrs. */
1082 initialize_vtbl_ptrs (current_class_ptr);
1083
1084 /* Initialize the data members. */
1085 while (mem_inits)
1086 {
1087 perform_member_init (TREE_PURPOSE (mem_inits),
1088 TREE_VALUE (mem_inits));
1089 mem_inits = TREE_CHAIN (mem_inits);
1090 }
1091 }
1092
1093 /* Returns the address of the vtable (i.e., the value that should be
1094 assigned to the vptr) for BINFO. */
1095
1096 static tree
1097 build_vtbl_address (tree binfo)
1098 {
1099 tree binfo_for = binfo;
1100 tree vtbl;
1101
1102 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
1103 /* If this is a virtual primary base, then the vtable we want to store
1104 is that for the base this is being used as the primary base of. We
1105 can't simply skip the initialization, because we may be expanding the
1106 inits of a subobject constructor where the virtual base layout
1107 can be different. */
1108 while (BINFO_PRIMARY_P (binfo_for))
1109 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
1110
1111 /* Figure out what vtable BINFO's vtable is based on, and mark it as
1112 used. */
1113 vtbl = get_vtbl_decl_for_binfo (binfo_for);
1114 TREE_USED (vtbl) = 1;
1115
1116 /* Now compute the address to use when initializing the vptr. */
1117 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
1118 if (TREE_CODE (vtbl) == VAR_DECL)
1119 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
1120
1121 return vtbl;
1122 }
1123
1124 /* This code sets up the virtual function tables appropriate for
1125 the pointer DECL. It is a one-ply initialization.
1126
1127 BINFO is the exact type that DECL is supposed to be. In
1128 multiple inheritance, this might mean "C's A" if C : A, B. */
1129
1130 static void
1131 expand_virtual_init (tree binfo, tree decl)
1132 {
1133 tree vtbl, vtbl_ptr;
1134 tree vtt_index;
1135
1136 /* Compute the initializer for vptr. */
1137 vtbl = build_vtbl_address (binfo);
1138
1139 /* We may get this vptr from a VTT, if this is a subobject
1140 constructor or subobject destructor. */
1141 vtt_index = BINFO_VPTR_INDEX (binfo);
1142 if (vtt_index)
1143 {
1144 tree vtbl2;
1145 tree vtt_parm;
1146
1147 /* Compute the value to use, when there's a VTT. */
1148 vtt_parm = current_vtt_parm;
1149 vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
1150 vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error);
1151 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
1152
1153 /* The actual initializer is the VTT value only in the subobject
1154 constructor. In maybe_clone_body we'll substitute NULL for
1155 the vtt_parm in the case of the non-subobject constructor. */
1156 vtbl = build3 (COND_EXPR,
1157 TREE_TYPE (vtbl),
1158 build2 (EQ_EXPR, boolean_type_node,
1159 current_in_charge_parm, integer_zero_node),
1160 vtbl2,
1161 vtbl);
1162 }
1163
1164 /* Compute the location of the vtpr. */
1165 vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL,
1166 tf_warning_or_error),
1167 TREE_TYPE (binfo));
1168 gcc_assert (vtbl_ptr != error_mark_node);
1169
1170 /* Assign the vtable to the vptr. */
1171 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
1172 finish_expr_stmt (cp_build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl,
1173 tf_warning_or_error));
1174 }
1175
1176 /* If an exception is thrown in a constructor, those base classes already
1177 constructed must be destroyed. This function creates the cleanup
1178 for BINFO, which has just been constructed. If FLAG is non-NULL,
1179 it is a DECL which is nonzero when this base needs to be
1180 destroyed. */
1181
1182 static void
1183 expand_cleanup_for_base (tree binfo, tree flag)
1184 {
1185 tree expr;
1186
1187 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
1188 return;
1189
1190 /* Call the destructor. */
1191 expr = build_special_member_call (current_class_ref,
1192 base_dtor_identifier,
1193 NULL,
1194 binfo,
1195 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1196 tf_warning_or_error);
1197 if (flag)
1198 expr = fold_build3_loc (input_location,
1199 COND_EXPR, void_type_node,
1200 c_common_truthvalue_conversion (input_location, flag),
1201 expr, integer_zero_node);
1202
1203 finish_eh_cleanup (expr);
1204 }
1205
1206 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1207 constructor. */
1208
1209 static void
1210 construct_virtual_base (tree vbase, tree arguments)
1211 {
1212 tree inner_if_stmt;
1213 tree exp;
1214 tree flag;
1215
1216 /* If there are virtual base classes with destructors, we need to
1217 emit cleanups to destroy them if an exception is thrown during
1218 the construction process. These exception regions (i.e., the
1219 period during which the cleanups must occur) begin from the time
1220 the construction is complete to the end of the function. If we
1221 create a conditional block in which to initialize the
1222 base-classes, then the cleanup region for the virtual base begins
1223 inside a block, and ends outside of that block. This situation
1224 confuses the sjlj exception-handling code. Therefore, we do not
1225 create a single conditional block, but one for each
1226 initialization. (That way the cleanup regions always begin
1227 in the outer block.) We trust the back end to figure out
1228 that the FLAG will not change across initializations, and
1229 avoid doing multiple tests. */
1230 flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
1231 inner_if_stmt = begin_if_stmt ();
1232 finish_if_stmt_cond (flag, inner_if_stmt);
1233
1234 /* Compute the location of the virtual base. If we're
1235 constructing virtual bases, then we must be the most derived
1236 class. Therefore, we don't have to look up the virtual base;
1237 we already know where it is. */
1238 exp = convert_to_base_statically (current_class_ref, vbase);
1239
1240 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1241 LOOKUP_COMPLAIN, tf_warning_or_error);
1242 finish_then_clause (inner_if_stmt);
1243 finish_if_stmt (inner_if_stmt);
1244
1245 expand_cleanup_for_base (vbase, flag);
1246 }
1247
1248 /* Find the context in which this FIELD can be initialized. */
1249
1250 static tree
1251 initializing_context (tree field)
1252 {
1253 tree t = DECL_CONTEXT (field);
1254
1255 /* Anonymous union members can be initialized in the first enclosing
1256 non-anonymous union context. */
1257 while (t && ANON_AGGR_TYPE_P (t))
1258 t = TYPE_CONTEXT (t);
1259 return t;
1260 }
1261
1262 /* Function to give error message if member initialization specification
1263 is erroneous. FIELD is the member we decided to initialize.
1264 TYPE is the type for which the initialization is being performed.
1265 FIELD must be a member of TYPE.
1266
1267 MEMBER_NAME is the name of the member. */
1268
1269 static int
1270 member_init_ok_or_else (tree field, tree type, tree member_name)
1271 {
1272 if (field == error_mark_node)
1273 return 0;
1274 if (!field)
1275 {
1276 error ("class %qT does not have any field named %qD", type,
1277 member_name);
1278 return 0;
1279 }
1280 if (TREE_CODE (field) == VAR_DECL)
1281 {
1282 error ("%q#D is a static data member; it can only be "
1283 "initialized at its definition",
1284 field);
1285 return 0;
1286 }
1287 if (TREE_CODE (field) != FIELD_DECL)
1288 {
1289 error ("%q#D is not a non-static data member of %qT",
1290 field, type);
1291 return 0;
1292 }
1293 if (initializing_context (field) != type)
1294 {
1295 error ("class %qT does not have any field named %qD", type,
1296 member_name);
1297 return 0;
1298 }
1299
1300 return 1;
1301 }
1302
1303 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1304 is a _TYPE node or TYPE_DECL which names a base for that type.
1305 Check the validity of NAME, and return either the base _TYPE, base
1306 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
1307 NULL_TREE and issue a diagnostic.
1308
1309 An old style unnamed direct single base construction is permitted,
1310 where NAME is NULL. */
1311
1312 tree
1313 expand_member_init (tree name)
1314 {
1315 tree basetype;
1316 tree field;
1317
1318 if (!current_class_ref)
1319 return NULL_TREE;
1320
1321 if (!name)
1322 {
1323 /* This is an obsolete unnamed base class initializer. The
1324 parser will already have warned about its use. */
1325 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1326 {
1327 case 0:
1328 error ("unnamed initializer for %qT, which has no base classes",
1329 current_class_type);
1330 return NULL_TREE;
1331 case 1:
1332 basetype = BINFO_TYPE
1333 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1334 break;
1335 default:
1336 error ("unnamed initializer for %qT, which uses multiple inheritance",
1337 current_class_type);
1338 return NULL_TREE;
1339 }
1340 }
1341 else if (TYPE_P (name))
1342 {
1343 basetype = TYPE_MAIN_VARIANT (name);
1344 name = TYPE_NAME (name);
1345 }
1346 else if (TREE_CODE (name) == TYPE_DECL)
1347 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1348 else
1349 basetype = NULL_TREE;
1350
1351 if (basetype)
1352 {
1353 tree class_binfo;
1354 tree direct_binfo;
1355 tree virtual_binfo;
1356 int i;
1357
1358 if (same_type_p (basetype, current_class_type)
1359 || current_template_parms)
1360 return basetype;
1361
1362 class_binfo = TYPE_BINFO (current_class_type);
1363 direct_binfo = NULL_TREE;
1364 virtual_binfo = NULL_TREE;
1365
1366 /* Look for a direct base. */
1367 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1368 if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1369 break;
1370
1371 /* Look for a virtual base -- unless the direct base is itself
1372 virtual. */
1373 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1374 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1375
1376 /* [class.base.init]
1377
1378 If a mem-initializer-id is ambiguous because it designates
1379 both a direct non-virtual base class and an inherited virtual
1380 base class, the mem-initializer is ill-formed. */
1381 if (direct_binfo && virtual_binfo)
1382 {
1383 error ("%qD is both a direct base and an indirect virtual base",
1384 basetype);
1385 return NULL_TREE;
1386 }
1387
1388 if (!direct_binfo && !virtual_binfo)
1389 {
1390 if (CLASSTYPE_VBASECLASSES (current_class_type))
1391 error ("type %qT is not a direct or virtual base of %qT",
1392 basetype, current_class_type);
1393 else
1394 error ("type %qT is not a direct base of %qT",
1395 basetype, current_class_type);
1396 return NULL_TREE;
1397 }
1398
1399 return direct_binfo ? direct_binfo : virtual_binfo;
1400 }
1401 else
1402 {
1403 if (TREE_CODE (name) == IDENTIFIER_NODE)
1404 field = lookup_field (current_class_type, name, 1, false);
1405 else
1406 field = name;
1407
1408 if (member_init_ok_or_else (field, current_class_type, name))
1409 return field;
1410 }
1411
1412 return NULL_TREE;
1413 }
1414
1415 /* This is like `expand_member_init', only it stores one aggregate
1416 value into another.
1417
1418 INIT comes in two flavors: it is either a value which
1419 is to be stored in EXP, or it is a parameter list
1420 to go to a constructor, which will operate on EXP.
1421 If INIT is not a parameter list for a constructor, then set
1422 LOOKUP_ONLYCONVERTING.
1423 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1424 the initializer, if FLAGS is 0, then it is the (init) form.
1425 If `init' is a CONSTRUCTOR, then we emit a warning message,
1426 explaining that such initializations are invalid.
1427
1428 If INIT resolves to a CALL_EXPR which happens to return
1429 something of the type we are looking for, then we know
1430 that we can safely use that call to perform the
1431 initialization.
1432
1433 The virtual function table pointer cannot be set up here, because
1434 we do not really know its type.
1435
1436 This never calls operator=().
1437
1438 When initializing, nothing is CONST.
1439
1440 A default copy constructor may have to be used to perform the
1441 initialization.
1442
1443 A constructor or a conversion operator may have to be used to
1444 perform the initialization, but not both, as it would be ambiguous. */
1445
1446 tree
1447 build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1448 {
1449 tree stmt_expr;
1450 tree compound_stmt;
1451 int destroy_temps;
1452 tree type = TREE_TYPE (exp);
1453 int was_const = TREE_READONLY (exp);
1454 int was_volatile = TREE_THIS_VOLATILE (exp);
1455 int is_global;
1456
1457 if (init == error_mark_node)
1458 return error_mark_node;
1459
1460 TREE_READONLY (exp) = 0;
1461 TREE_THIS_VOLATILE (exp) = 0;
1462
1463 if (init && TREE_CODE (init) != TREE_LIST
1464 && !(TREE_CODE (init) == TARGET_EXPR
1465 && TARGET_EXPR_DIRECT_INIT_P (init))
1466 && !(BRACE_ENCLOSED_INITIALIZER_P (init)
1467 && CONSTRUCTOR_IS_DIRECT_INIT (init)))
1468 flags |= LOOKUP_ONLYCONVERTING;
1469
1470 if (TREE_CODE (type) == ARRAY_TYPE)
1471 {
1472 tree itype;
1473
1474 /* An array may not be initialized use the parenthesized
1475 initialization form -- unless the initializer is "()". */
1476 if (init && TREE_CODE (init) == TREE_LIST)
1477 {
1478 if (complain & tf_error)
1479 error ("bad array initializer");
1480 return error_mark_node;
1481 }
1482 /* Must arrange to initialize each element of EXP
1483 from elements of INIT. */
1484 itype = init ? TREE_TYPE (init) : NULL_TREE;
1485 if (cv_qualified_p (type))
1486 TREE_TYPE (exp) = cv_unqualified (type);
1487 if (itype && cv_qualified_p (itype))
1488 TREE_TYPE (init) = cv_unqualified (itype);
1489 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1490 /*explicit_value_init_p=*/false,
1491 itype && same_type_p (TREE_TYPE (init),
1492 TREE_TYPE (exp)),
1493 complain);
1494 TREE_READONLY (exp) = was_const;
1495 TREE_THIS_VOLATILE (exp) = was_volatile;
1496 TREE_TYPE (exp) = type;
1497 if (init)
1498 TREE_TYPE (init) = itype;
1499 return stmt_expr;
1500 }
1501
1502 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1503 /* Just know that we've seen something for this node. */
1504 TREE_USED (exp) = 1;
1505
1506 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1507 destroy_temps = stmts_are_full_exprs_p ();
1508 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1509 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1510 init, LOOKUP_NORMAL|flags, complain);
1511 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1512 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1513 TREE_READONLY (exp) = was_const;
1514 TREE_THIS_VOLATILE (exp) = was_volatile;
1515
1516 return stmt_expr;
1517 }
1518
1519 static void
1520 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
1521 tsubst_flags_t complain)
1522 {
1523 tree type = TREE_TYPE (exp);
1524 tree ctor_name;
1525
1526 /* It fails because there may not be a constructor which takes
1527 its own type as the first (or only parameter), but which does
1528 take other types via a conversion. So, if the thing initializing
1529 the expression is a unit element of type X, first try X(X&),
1530 followed by initialization by X. If neither of these work
1531 out, then look hard. */
1532 tree rval;
1533 VEC(tree,gc) *parms;
1534
1535 /* If we have direct-initialization from an initializer list, pull
1536 it out of the TREE_LIST so the code below can see it. */
1537 if (init && TREE_CODE (init) == TREE_LIST
1538 && BRACE_ENCLOSED_INITIALIZER_P (TREE_VALUE (init))
1539 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
1540 {
1541 gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
1542 && TREE_CHAIN (init) == NULL_TREE);
1543 init = TREE_VALUE (init);
1544 }
1545
1546 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
1547 && CP_AGGREGATE_TYPE_P (type))
1548 /* A brace-enclosed initializer for an aggregate. In C++0x this can
1549 happen for direct-initialization, too. */
1550 init = digest_init (type, init, complain);
1551
1552 /* A CONSTRUCTOR of the target's type is a previously digested
1553 initializer, whether that happened just above or in
1554 cp_parser_late_parsing_nsdmi.
1555
1556 A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
1557 set represents the whole initialization, so we shouldn't build up
1558 another ctor call. */
1559 if (init
1560 && (TREE_CODE (init) == CONSTRUCTOR
1561 || (TREE_CODE (init) == TARGET_EXPR
1562 && (TARGET_EXPR_DIRECT_INIT_P (init)
1563 || TARGET_EXPR_LIST_INIT_P (init))))
1564 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
1565 {
1566 /* Early initialization via a TARGET_EXPR only works for
1567 complete objects. */
1568 gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
1569
1570 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1571 TREE_SIDE_EFFECTS (init) = 1;
1572 finish_expr_stmt (init);
1573 return;
1574 }
1575
1576 if (init && TREE_CODE (init) != TREE_LIST
1577 && (flags & LOOKUP_ONLYCONVERTING))
1578 {
1579 /* Base subobjects should only get direct-initialization. */
1580 gcc_assert (true_exp == exp);
1581
1582 if (flags & DIRECT_BIND)
1583 /* Do nothing. We hit this in two cases: Reference initialization,
1584 where we aren't initializing a real variable, so we don't want
1585 to run a new constructor; and catching an exception, where we
1586 have already built up the constructor call so we could wrap it
1587 in an exception region. */;
1588 else
1589 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1590
1591 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1592 /* We need to protect the initialization of a catch parm with a
1593 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1594 around the TARGET_EXPR for the copy constructor. See
1595 initialize_handler_parm. */
1596 {
1597 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1598 TREE_OPERAND (init, 0));
1599 TREE_TYPE (init) = void_type_node;
1600 }
1601 else
1602 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1603 TREE_SIDE_EFFECTS (init) = 1;
1604 finish_expr_stmt (init);
1605 return;
1606 }
1607
1608 if (init == NULL_TREE)
1609 parms = NULL;
1610 else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
1611 {
1612 parms = make_tree_vector ();
1613 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1614 VEC_safe_push (tree, gc, parms, TREE_VALUE (init));
1615 }
1616 else
1617 parms = make_tree_vector_single (init);
1618
1619 if (exp == current_class_ref && current_function_decl
1620 && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
1621 {
1622 /* Delegating constructor. */
1623 tree complete;
1624 tree base;
1625 tree elt; unsigned i;
1626
1627 /* Unshare the arguments for the second call. */
1628 VEC(tree,gc) *parms2 = make_tree_vector ();
1629 FOR_EACH_VEC_ELT (tree, parms, i, elt)
1630 {
1631 elt = break_out_target_exprs (elt);
1632 VEC_safe_push (tree, gc, parms2, elt);
1633 }
1634 complete = build_special_member_call (exp, complete_ctor_identifier,
1635 &parms2, binfo, flags,
1636 complain);
1637 complete = fold_build_cleanup_point_expr (void_type_node, complete);
1638 release_tree_vector (parms2);
1639
1640 base = build_special_member_call (exp, base_ctor_identifier,
1641 &parms, binfo, flags,
1642 complain);
1643 base = fold_build_cleanup_point_expr (void_type_node, base);
1644 rval = build3 (COND_EXPR, void_type_node,
1645 build2 (EQ_EXPR, boolean_type_node,
1646 current_in_charge_parm, integer_zero_node),
1647 base,
1648 complete);
1649 }
1650 else
1651 {
1652 if (true_exp == exp)
1653 ctor_name = complete_ctor_identifier;
1654 else
1655 ctor_name = base_ctor_identifier;
1656 rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
1657 complain);
1658 }
1659
1660 if (parms != NULL)
1661 release_tree_vector (parms);
1662
1663 if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
1664 {
1665 tree fn = get_callee_fndecl (rval);
1666 if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
1667 {
1668 tree e = maybe_constant_init (rval);
1669 if (TREE_CONSTANT (e))
1670 rval = build2 (INIT_EXPR, type, exp, e);
1671 }
1672 }
1673
1674 /* FIXME put back convert_to_void? */
1675 if (TREE_SIDE_EFFECTS (rval))
1676 finish_expr_stmt (rval);
1677 }
1678
1679 /* This function is responsible for initializing EXP with INIT
1680 (if any).
1681
1682 BINFO is the binfo of the type for who we are performing the
1683 initialization. For example, if W is a virtual base class of A and B,
1684 and C : A, B.
1685 If we are initializing B, then W must contain B's W vtable, whereas
1686 were we initializing C, W must contain C's W vtable.
1687
1688 TRUE_EXP is nonzero if it is the true expression being initialized.
1689 In this case, it may be EXP, or may just contain EXP. The reason we
1690 need this is because if EXP is a base element of TRUE_EXP, we
1691 don't necessarily know by looking at EXP where its virtual
1692 baseclass fields should really be pointing. But we do know
1693 from TRUE_EXP. In constructors, we don't know anything about
1694 the value being initialized.
1695
1696 FLAGS is just passed to `build_new_method_call'. See that function
1697 for its description. */
1698
1699 static void
1700 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
1701 tsubst_flags_t complain)
1702 {
1703 tree type = TREE_TYPE (exp);
1704
1705 gcc_assert (init != error_mark_node && type != error_mark_node);
1706 gcc_assert (building_stmt_list_p ());
1707
1708 /* Use a function returning the desired type to initialize EXP for us.
1709 If the function is a constructor, and its first argument is
1710 NULL_TREE, know that it was meant for us--just slide exp on
1711 in and expand the constructor. Constructors now come
1712 as TARGET_EXPRs. */
1713
1714 if (init && TREE_CODE (exp) == VAR_DECL
1715 && COMPOUND_LITERAL_P (init))
1716 {
1717 VEC(tree,gc)* cleanups = NULL;
1718 /* If store_init_value returns NULL_TREE, the INIT has been
1719 recorded as the DECL_INITIAL for EXP. That means there's
1720 nothing more we have to do. */
1721 init = store_init_value (exp, init, &cleanups, flags);
1722 if (init)
1723 finish_expr_stmt (init);
1724 gcc_assert (!cleanups);
1725 return;
1726 }
1727
1728 /* If an explicit -- but empty -- initializer list was present,
1729 that's value-initialization. */
1730 if (init == void_type_node)
1731 {
1732 /* If no user-provided ctor, we need to zero out the object. */
1733 if (!type_has_user_provided_constructor (type))
1734 {
1735 tree field_size = NULL_TREE;
1736 if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
1737 /* Don't clobber already initialized virtual bases. */
1738 field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
1739 init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
1740 field_size);
1741 init = build2 (INIT_EXPR, type, exp, init);
1742 finish_expr_stmt (init);
1743 }
1744
1745 /* If we don't need to mess with the constructor at all,
1746 then we're done. */
1747 if (! type_build_ctor_call (type))
1748 return;
1749
1750 /* Otherwise fall through and call the constructor. */
1751 init = NULL_TREE;
1752 }
1753
1754 /* We know that expand_default_init can handle everything we want
1755 at this point. */
1756 expand_default_init (binfo, true_exp, exp, init, flags, complain);
1757 }
1758
1759 /* Report an error if TYPE is not a user-defined, class type. If
1760 OR_ELSE is nonzero, give an error message. */
1761
1762 int
1763 is_class_type (tree type, int or_else)
1764 {
1765 if (type == error_mark_node)
1766 return 0;
1767
1768 if (! CLASS_TYPE_P (type))
1769 {
1770 if (or_else)
1771 error ("%qT is not a class type", type);
1772 return 0;
1773 }
1774 return 1;
1775 }
1776
1777 tree
1778 get_type_value (tree name)
1779 {
1780 if (name == error_mark_node)
1781 return NULL_TREE;
1782
1783 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1784 return IDENTIFIER_TYPE_VALUE (name);
1785 else
1786 return NULL_TREE;
1787 }
1788
1789 /* Build a reference to a member of an aggregate. This is not a C++
1790 `&', but really something which can have its address taken, and
1791 then act as a pointer to member, for example TYPE :: FIELD can have
1792 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
1793 this expression is the operand of "&".
1794
1795 @@ Prints out lousy diagnostics for operator <typename>
1796 @@ fields.
1797
1798 @@ This function should be rewritten and placed in search.c. */
1799
1800 tree
1801 build_offset_ref (tree type, tree member, bool address_p)
1802 {
1803 tree decl;
1804 tree basebinfo = NULL_TREE;
1805
1806 /* class templates can come in as TEMPLATE_DECLs here. */
1807 if (TREE_CODE (member) == TEMPLATE_DECL)
1808 return member;
1809
1810 if (dependent_scope_p (type) || type_dependent_expression_p (member))
1811 return build_qualified_name (NULL_TREE, type, member,
1812 /*template_p=*/false);
1813
1814 gcc_assert (TYPE_P (type));
1815 if (! is_class_type (type, 1))
1816 return error_mark_node;
1817
1818 gcc_assert (DECL_P (member) || BASELINK_P (member));
1819 /* Callers should call mark_used before this point. */
1820 gcc_assert (!DECL_P (member) || TREE_USED (member));
1821
1822 type = TYPE_MAIN_VARIANT (type);
1823 if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
1824 {
1825 error ("incomplete type %qT does not have member %qD", type, member);
1826 return error_mark_node;
1827 }
1828
1829 /* Entities other than non-static members need no further
1830 processing. */
1831 if (TREE_CODE (member) == TYPE_DECL)
1832 return member;
1833 if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL)
1834 return convert_from_reference (member);
1835
1836 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1837 {
1838 error ("invalid pointer to bit-field %qD", member);
1839 return error_mark_node;
1840 }
1841
1842 /* Set up BASEBINFO for member lookup. */
1843 decl = maybe_dummy_object (type, &basebinfo);
1844
1845 /* A lot of this logic is now handled in lookup_member. */
1846 if (BASELINK_P (member))
1847 {
1848 /* Go from the TREE_BASELINK to the member function info. */
1849 tree t = BASELINK_FUNCTIONS (member);
1850
1851 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1852 {
1853 /* Get rid of a potential OVERLOAD around it. */
1854 t = OVL_CURRENT (t);
1855
1856 /* Unique functions are handled easily. */
1857
1858 /* For non-static member of base class, we need a special rule
1859 for access checking [class.protected]:
1860
1861 If the access is to form a pointer to member, the
1862 nested-name-specifier shall name the derived class
1863 (or any class derived from that class). */
1864 if (address_p && DECL_P (t)
1865 && DECL_NONSTATIC_MEMBER_P (t))
1866 perform_or_defer_access_check (TYPE_BINFO (type), t, t);
1867 else
1868 perform_or_defer_access_check (basebinfo, t, t);
1869
1870 if (DECL_STATIC_FUNCTION_P (t))
1871 return t;
1872 member = t;
1873 }
1874 else
1875 TREE_TYPE (member) = unknown_type_node;
1876 }
1877 else if (address_p && TREE_CODE (member) == FIELD_DECL)
1878 /* We need additional test besides the one in
1879 check_accessibility_of_qualified_id in case it is
1880 a pointer to non-static member. */
1881 perform_or_defer_access_check (TYPE_BINFO (type), member, member);
1882
1883 if (!address_p)
1884 {
1885 /* If MEMBER is non-static, then the program has fallen afoul of
1886 [expr.prim]:
1887
1888 An id-expression that denotes a nonstatic data member or
1889 nonstatic member function of a class can only be used:
1890
1891 -- as part of a class member access (_expr.ref_) in which the
1892 object-expression refers to the member's class or a class
1893 derived from that class, or
1894
1895 -- to form a pointer to member (_expr.unary.op_), or
1896
1897 -- in the body of a nonstatic member function of that class or
1898 of a class derived from that class (_class.mfct.nonstatic_), or
1899
1900 -- in a mem-initializer for a constructor for that class or for
1901 a class derived from that class (_class.base.init_). */
1902 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1903 {
1904 /* Build a representation of the qualified name suitable
1905 for use as the operand to "&" -- even though the "&" is
1906 not actually present. */
1907 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1908 /* In Microsoft mode, treat a non-static member function as if
1909 it were a pointer-to-member. */
1910 if (flag_ms_extensions)
1911 {
1912 PTRMEM_OK_P (member) = 1;
1913 return cp_build_addr_expr (member, tf_warning_or_error);
1914 }
1915 error ("invalid use of non-static member function %qD",
1916 TREE_OPERAND (member, 1));
1917 return error_mark_node;
1918 }
1919 else if (TREE_CODE (member) == FIELD_DECL)
1920 {
1921 error ("invalid use of non-static data member %qD", member);
1922 return error_mark_node;
1923 }
1924 return member;
1925 }
1926
1927 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1928 PTRMEM_OK_P (member) = 1;
1929 return member;
1930 }
1931
1932 /* If DECL is a scalar enumeration constant or variable with a
1933 constant initializer, return the initializer (or, its initializers,
1934 recursively); otherwise, return DECL. If INTEGRAL_P, the
1935 initializer is only returned if DECL is an integral
1936 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
1937 return an aggregate constant. */
1938
1939 static tree
1940 constant_value_1 (tree decl, bool integral_p, bool return_aggregate_cst_ok_p)
1941 {
1942 while (TREE_CODE (decl) == CONST_DECL
1943 || (integral_p
1944 ? decl_constant_var_p (decl)
1945 : (TREE_CODE (decl) == VAR_DECL
1946 && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
1947 {
1948 tree init;
1949 /* If DECL is a static data member in a template
1950 specialization, we must instantiate it here. The
1951 initializer for the static data member is not processed
1952 until needed; we need it now. */
1953 mark_used (decl);
1954 mark_rvalue_use (decl);
1955 init = DECL_INITIAL (decl);
1956 if (init == error_mark_node)
1957 {
1958 if (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
1959 /* Treat the error as a constant to avoid cascading errors on
1960 excessively recursive template instantiation (c++/9335). */
1961 return init;
1962 else
1963 return decl;
1964 }
1965 /* Initializers in templates are generally expanded during
1966 instantiation, so before that for const int i(2)
1967 INIT is a TREE_LIST with the actual initializer as
1968 TREE_VALUE. */
1969 if (processing_template_decl
1970 && init
1971 && TREE_CODE (init) == TREE_LIST
1972 && TREE_CHAIN (init) == NULL_TREE)
1973 init = TREE_VALUE (init);
1974 if (!init
1975 || !TREE_TYPE (init)
1976 || !TREE_CONSTANT (init)
1977 || (!integral_p && !return_aggregate_cst_ok_p
1978 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
1979 return an aggregate constant (of which string
1980 literals are a special case), as we do not want
1981 to make inadvertent copies of such entities, and
1982 we must be sure that their addresses are the
1983 same everywhere. */
1984 && (TREE_CODE (init) == CONSTRUCTOR
1985 || TREE_CODE (init) == STRING_CST)))
1986 break;
1987 decl = unshare_expr (init);
1988 }
1989 return decl;
1990 }
1991
1992 /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by
1993 constant of integral or enumeration type, then return that value.
1994 These are those variables permitted in constant expressions by
1995 [5.19/1]. */
1996
1997 tree
1998 integral_constant_value (tree decl)
1999 {
2000 return constant_value_1 (decl, /*integral_p=*/true,
2001 /*return_aggregate_cst_ok_p=*/false);
2002 }
2003
2004 /* A more relaxed version of integral_constant_value, used by the
2005 common C/C++ code. */
2006
2007 tree
2008 decl_constant_value (tree decl)
2009 {
2010 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2011 /*return_aggregate_cst_ok_p=*/true);
2012 }
2013
2014 /* A version of integral_constant_value used by the C++ front end for
2015 optimization purposes. */
2016
2017 tree
2018 decl_constant_value_safe (tree decl)
2019 {
2020 return constant_value_1 (decl, /*integral_p=*/processing_template_decl,
2021 /*return_aggregate_cst_ok_p=*/false);
2022 }
2023 \f
2024 /* Common subroutines of build_new and build_vec_delete. */
2025
2026 /* Call the global __builtin_delete to delete ADDR. */
2027
2028 static tree
2029 build_builtin_delete_call (tree addr)
2030 {
2031 mark_used (global_delete_fndecl);
2032 return build_call_n (global_delete_fndecl, 1, addr);
2033 }
2034 \f
2035 /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
2036 the type of the object being allocated; otherwise, it's just TYPE.
2037 INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
2038 user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
2039 a vector of arguments to be provided as arguments to a placement
2040 new operator. This routine performs no semantic checks; it just
2041 creates and returns a NEW_EXPR. */
2042
2043 static tree
2044 build_raw_new_expr (VEC(tree,gc) *placement, tree type, tree nelts,
2045 VEC(tree,gc) *init, int use_global_new)
2046 {
2047 tree init_list;
2048 tree new_expr;
2049
2050 /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
2051 If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
2052 permits us to distinguish the case of a missing initializer "new
2053 int" from an empty initializer "new int()". */
2054 if (init == NULL)
2055 init_list = NULL_TREE;
2056 else if (VEC_empty (tree, init))
2057 init_list = void_zero_node;
2058 else
2059 init_list = build_tree_list_vec (init);
2060
2061 new_expr = build4 (NEW_EXPR, build_pointer_type (type),
2062 build_tree_list_vec (placement), type, nelts,
2063 init_list);
2064 NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
2065 TREE_SIDE_EFFECTS (new_expr) = 1;
2066
2067 return new_expr;
2068 }
2069
2070 /* Diagnose uninitialized const members or reference members of type
2071 TYPE. USING_NEW is used to disambiguate the diagnostic between a
2072 new expression without a new-initializer and a declaration. Returns
2073 the error count. */
2074
2075 static int
2076 diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
2077 bool using_new, bool complain)
2078 {
2079 tree field;
2080 int error_count = 0;
2081
2082 if (type_has_user_provided_constructor (type))
2083 return 0;
2084
2085 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2086 {
2087 tree field_type;
2088
2089 if (TREE_CODE (field) != FIELD_DECL)
2090 continue;
2091
2092 field_type = strip_array_types (TREE_TYPE (field));
2093
2094 if (type_has_user_provided_constructor (field_type))
2095 continue;
2096
2097 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2098 {
2099 ++ error_count;
2100 if (complain)
2101 {
2102 if (using_new)
2103 error ("uninitialized reference member in %q#T "
2104 "using %<new%> without new-initializer", origin);
2105 else
2106 error ("uninitialized reference member in %q#T", origin);
2107 inform (DECL_SOURCE_LOCATION (field),
2108 "%qD should be initialized", field);
2109 }
2110 }
2111
2112 if (CP_TYPE_CONST_P (field_type))
2113 {
2114 ++ error_count;
2115 if (complain)
2116 {
2117 if (using_new)
2118 error ("uninitialized const member in %q#T "
2119 "using %<new%> without new-initializer", origin);
2120 else
2121 error ("uninitialized const member in %q#T", origin);
2122 inform (DECL_SOURCE_LOCATION (field),
2123 "%qD should be initialized", field);
2124 }
2125 }
2126
2127 if (CLASS_TYPE_P (field_type))
2128 error_count
2129 += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
2130 using_new, complain);
2131 }
2132 return error_count;
2133 }
2134
2135 int
2136 diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
2137 {
2138 return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
2139 }
2140
2141 /* Generate code for a new-expression, including calling the "operator
2142 new" function, initializing the object, and, if an exception occurs
2143 during construction, cleaning up. The arguments are as for
2144 build_raw_new_expr. This may change PLACEMENT and INIT. */
2145
2146 static tree
2147 build_new_1 (VEC(tree,gc) **placement, tree type, tree nelts,
2148 VEC(tree,gc) **init, bool globally_qualified_p,
2149 tsubst_flags_t complain)
2150 {
2151 tree size, rval;
2152 /* True iff this is a call to "operator new[]" instead of just
2153 "operator new". */
2154 bool array_p = false;
2155 /* If ARRAY_P is true, the element type of the array. This is never
2156 an ARRAY_TYPE; for something like "new int[3][4]", the
2157 ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
2158 TYPE. */
2159 tree elt_type;
2160 /* The type of the new-expression. (This type is always a pointer
2161 type.) */
2162 tree pointer_type;
2163 tree non_const_pointer_type;
2164 tree outer_nelts = NULL_TREE;
2165 tree alloc_call, alloc_expr;
2166 /* The address returned by the call to "operator new". This node is
2167 a VAR_DECL and is therefore reusable. */
2168 tree alloc_node;
2169 tree alloc_fn;
2170 tree cookie_expr, init_expr;
2171 int nothrow, check_new;
2172 int use_java_new = 0;
2173 /* If non-NULL, the number of extra bytes to allocate at the
2174 beginning of the storage allocated for an array-new expression in
2175 order to store the number of elements. */
2176 tree cookie_size = NULL_TREE;
2177 tree placement_first;
2178 tree placement_expr = NULL_TREE;
2179 /* True if the function we are calling is a placement allocation
2180 function. */
2181 bool placement_allocation_fn_p;
2182 /* True if the storage must be initialized, either by a constructor
2183 or due to an explicit new-initializer. */
2184 bool is_initialized;
2185 /* The address of the thing allocated, not including any cookie. In
2186 particular, if an array cookie is in use, DATA_ADDR is the
2187 address of the first array element. This node is a VAR_DECL, and
2188 is therefore reusable. */
2189 tree data_addr;
2190 tree init_preeval_expr = NULL_TREE;
2191
2192 if (nelts)
2193 {
2194 outer_nelts = nelts;
2195 array_p = true;
2196 }
2197 else if (TREE_CODE (type) == ARRAY_TYPE)
2198 {
2199 array_p = true;
2200 nelts = array_type_nelts_top (type);
2201 outer_nelts = nelts;
2202 type = TREE_TYPE (type);
2203 }
2204
2205 /* If our base type is an array, then make sure we know how many elements
2206 it has. */
2207 for (elt_type = type;
2208 TREE_CODE (elt_type) == ARRAY_TYPE;
2209 elt_type = TREE_TYPE (elt_type))
2210 nelts = cp_build_binary_op (input_location,
2211 MULT_EXPR, nelts,
2212 array_type_nelts_top (elt_type),
2213 complain);
2214
2215 if (TREE_CODE (elt_type) == VOID_TYPE)
2216 {
2217 if (complain & tf_error)
2218 error ("invalid type %<void%> for new");
2219 return error_mark_node;
2220 }
2221
2222 if (abstract_virtuals_error_sfinae (NULL_TREE, elt_type, complain))
2223 return error_mark_node;
2224
2225 is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
2226
2227 if (*init == NULL)
2228 {
2229 bool maybe_uninitialized_error = false;
2230 /* A program that calls for default-initialization [...] of an
2231 entity of reference type is ill-formed. */
2232 if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
2233 maybe_uninitialized_error = true;
2234
2235 /* A new-expression that creates an object of type T initializes
2236 that object as follows:
2237 - If the new-initializer is omitted:
2238 -- If T is a (possibly cv-qualified) non-POD class type
2239 (or array thereof), the object is default-initialized (8.5).
2240 [...]
2241 -- Otherwise, the object created has indeterminate
2242 value. If T is a const-qualified type, or a (possibly
2243 cv-qualified) POD class type (or array thereof)
2244 containing (directly or indirectly) a member of
2245 const-qualified type, the program is ill-formed; */
2246
2247 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
2248 maybe_uninitialized_error = true;
2249
2250 if (maybe_uninitialized_error
2251 && diagnose_uninitialized_cst_or_ref_member (elt_type,
2252 /*using_new=*/true,
2253 complain & tf_error))
2254 return error_mark_node;
2255 }
2256
2257 if (CP_TYPE_CONST_P (elt_type) && *init == NULL
2258 && default_init_uninitialized_part (elt_type))
2259 {
2260 if (complain & tf_error)
2261 error ("uninitialized const in %<new%> of %q#T", elt_type);
2262 return error_mark_node;
2263 }
2264
2265 size = size_in_bytes (elt_type);
2266 if (array_p)
2267 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2268
2269 alloc_fn = NULL_TREE;
2270
2271 /* If PLACEMENT is a single simple pointer type not passed by
2272 reference, prepare to capture it in a temporary variable. Do
2273 this now, since PLACEMENT will change in the calls below. */
2274 placement_first = NULL_TREE;
2275 if (VEC_length (tree, *placement) == 1
2276 && (TREE_CODE (TREE_TYPE (VEC_index (tree, *placement, 0)))
2277 == POINTER_TYPE))
2278 placement_first = VEC_index (tree, *placement, 0);
2279
2280 /* Allocate the object. */
2281 if (VEC_empty (tree, *placement) && TYPE_FOR_JAVA (elt_type))
2282 {
2283 tree class_addr;
2284 tree class_decl = build_java_class_ref (elt_type);
2285 static const char alloc_name[] = "_Jv_AllocObject";
2286
2287 if (class_decl == error_mark_node)
2288 return error_mark_node;
2289
2290 use_java_new = 1;
2291 if (!get_global_value_if_present (get_identifier (alloc_name),
2292 &alloc_fn))
2293 {
2294 if (complain & tf_error)
2295 error ("call to Java constructor with %qs undefined", alloc_name);
2296 return error_mark_node;
2297 }
2298 else if (really_overloaded_fn (alloc_fn))
2299 {
2300 if (complain & tf_error)
2301 error ("%qD should never be overloaded", alloc_fn);
2302 return error_mark_node;
2303 }
2304 alloc_fn = OVL_CURRENT (alloc_fn);
2305 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2306 alloc_call = cp_build_function_call_nary (alloc_fn, complain,
2307 class_addr, NULL_TREE);
2308 }
2309 else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
2310 {
2311 error ("Java class %q#T object allocated using placement new", elt_type);
2312 return error_mark_node;
2313 }
2314 else
2315 {
2316 tree fnname;
2317 tree fns;
2318
2319 fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
2320
2321 if (!globally_qualified_p
2322 && CLASS_TYPE_P (elt_type)
2323 && (array_p
2324 ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
2325 : TYPE_HAS_NEW_OPERATOR (elt_type)))
2326 {
2327 /* Use a class-specific operator new. */
2328 /* If a cookie is required, add some extra space. */
2329 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
2330 {
2331 cookie_size = targetm.cxx.get_cookie_size (elt_type);
2332 size = size_binop (PLUS_EXPR, size, cookie_size);
2333 }
2334 /* Create the argument list. */
2335 VEC_safe_insert (tree, gc, *placement, 0, size);
2336 /* Do name-lookup to find the appropriate operator. */
2337 fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
2338 if (fns == NULL_TREE)
2339 {
2340 if (complain & tf_error)
2341 error ("no suitable %qD found in class %qT", fnname, elt_type);
2342 return error_mark_node;
2343 }
2344 if (TREE_CODE (fns) == TREE_LIST)
2345 {
2346 if (complain & tf_error)
2347 {
2348 error ("request for member %qD is ambiguous", fnname);
2349 print_candidates (fns);
2350 }
2351 return error_mark_node;
2352 }
2353 alloc_call = build_new_method_call (build_dummy_object (elt_type),
2354 fns, placement,
2355 /*conversion_path=*/NULL_TREE,
2356 LOOKUP_NORMAL,
2357 &alloc_fn,
2358 complain);
2359 }
2360 else
2361 {
2362 /* Use a global operator new. */
2363 /* See if a cookie might be required. */
2364 if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
2365 cookie_size = targetm.cxx.get_cookie_size (elt_type);
2366 else
2367 cookie_size = NULL_TREE;
2368
2369 alloc_call = build_operator_new_call (fnname, placement,
2370 &size, &cookie_size,
2371 &alloc_fn);
2372 }
2373 }
2374
2375 if (alloc_call == error_mark_node)
2376 return error_mark_node;
2377
2378 gcc_assert (alloc_fn != NULL_TREE);
2379
2380 /* If we found a simple case of PLACEMENT_EXPR above, then copy it
2381 into a temporary variable. */
2382 if (!processing_template_decl
2383 && placement_first != NULL_TREE
2384 && TREE_CODE (alloc_call) == CALL_EXPR
2385 && call_expr_nargs (alloc_call) == 2
2386 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
2387 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))) == POINTER_TYPE)
2388 {
2389 tree placement_arg = CALL_EXPR_ARG (alloc_call, 1);
2390
2391 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))
2392 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))))
2393 {
2394 placement_expr = get_target_expr (placement_first);
2395 CALL_EXPR_ARG (alloc_call, 1)
2396 = convert (TREE_TYPE (placement_arg), placement_expr);
2397 }
2398 }
2399
2400 /* In the simple case, we can stop now. */
2401 pointer_type = build_pointer_type (type);
2402 if (!cookie_size && !is_initialized)
2403 return build_nop (pointer_type, alloc_call);
2404
2405 /* Store the result of the allocation call in a variable so that we can
2406 use it more than once. */
2407 alloc_expr = get_target_expr (alloc_call);
2408 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2409
2410 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
2411 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
2412 alloc_call = TREE_OPERAND (alloc_call, 1);
2413
2414 /* Now, check to see if this function is actually a placement
2415 allocation function. This can happen even when PLACEMENT is NULL
2416 because we might have something like:
2417
2418 struct S { void* operator new (size_t, int i = 0); };
2419
2420 A call to `new S' will get this allocation function, even though
2421 there is no explicit placement argument. If there is more than
2422 one argument, or there are variable arguments, then this is a
2423 placement allocation function. */
2424 placement_allocation_fn_p
2425 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2426 || varargs_function_p (alloc_fn));
2427
2428 /* Preevaluate the placement args so that we don't reevaluate them for a
2429 placement delete. */
2430 if (placement_allocation_fn_p)
2431 {
2432 tree inits;
2433 stabilize_call (alloc_call, &inits);
2434 if (inits)
2435 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
2436 alloc_expr);
2437 }
2438
2439 /* unless an allocation function is declared with an empty excep-
2440 tion-specification (_except.spec_), throw(), it indicates failure to
2441 allocate storage by throwing a bad_alloc exception (clause _except_,
2442 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2443 cation function is declared with an empty exception-specification,
2444 throw(), it returns null to indicate failure to allocate storage and a
2445 non-null pointer otherwise.
2446
2447 So check for a null exception spec on the op new we just called. */
2448
2449 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2450 check_new = (flag_check_new || nothrow) && ! use_java_new;
2451
2452 if (cookie_size)
2453 {
2454 tree cookie;
2455 tree cookie_ptr;
2456 tree size_ptr_type;
2457
2458 /* Adjust so we're pointing to the start of the object. */
2459 data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
2460
2461 /* Store the number of bytes allocated so that we can know how
2462 many elements to destroy later. We use the last sizeof
2463 (size_t) bytes to store the number of elements. */
2464 cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
2465 cookie_ptr = fold_build_pointer_plus_loc (input_location,
2466 alloc_node, cookie_ptr);
2467 size_ptr_type = build_pointer_type (sizetype);
2468 cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
2469 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2470
2471 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
2472
2473 if (targetm.cxx.cookie_has_size ())
2474 {
2475 /* Also store the element size. */
2476 cookie_ptr = fold_build_pointer_plus (cookie_ptr,
2477 fold_build1_loc (input_location,
2478 NEGATE_EXPR, sizetype,
2479 size_in_bytes (sizetype)));
2480
2481 cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2482 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
2483 size_in_bytes (elt_type));
2484 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
2485 cookie, cookie_expr);
2486 }
2487 }
2488 else
2489 {
2490 cookie_expr = NULL_TREE;
2491 data_addr = alloc_node;
2492 }
2493
2494 /* Now use a pointer to the type we've actually allocated. */
2495
2496 /* But we want to operate on a non-const version to start with,
2497 since we'll be modifying the elements. */
2498 non_const_pointer_type = build_pointer_type
2499 (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
2500
2501 data_addr = fold_convert (non_const_pointer_type, data_addr);
2502 /* Any further uses of alloc_node will want this type, too. */
2503 alloc_node = fold_convert (non_const_pointer_type, alloc_node);
2504
2505 /* Now initialize the allocated object. Note that we preevaluate the
2506 initialization expression, apart from the actual constructor call or
2507 assignment--we do this because we want to delay the allocation as long
2508 as possible in order to minimize the size of the exception region for
2509 placement delete. */
2510 if (is_initialized)
2511 {
2512 bool stable;
2513 bool explicit_value_init_p = false;
2514
2515 if (*init != NULL && VEC_empty (tree, *init))
2516 {
2517 *init = NULL;
2518 explicit_value_init_p = true;
2519 }
2520
2521 if (processing_template_decl && explicit_value_init_p)
2522 {
2523 /* build_value_init doesn't work in templates, and we don't need
2524 the initializer anyway since we're going to throw it away and
2525 rebuild it at instantiation time, so just build up a single
2526 constructor call to get any appropriate diagnostics. */
2527 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2528 if (type_build_ctor_call (elt_type))
2529 init_expr = build_special_member_call (init_expr,
2530 complete_ctor_identifier,
2531 init, elt_type,
2532 LOOKUP_NORMAL,
2533 complain);
2534 stable = stabilize_init (init_expr, &init_preeval_expr);
2535 }
2536 else if (array_p)
2537 {
2538 tree vecinit = NULL_TREE;
2539 if (*init && VEC_length (tree, *init) == 1
2540 && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *init, 0))
2541 && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *init, 0)))
2542 {
2543 vecinit = VEC_index (tree, *init, 0);
2544 if (CONSTRUCTOR_NELTS (vecinit) == 0)
2545 /* List-value-initialization, leave it alone. */;
2546 else
2547 {
2548 tree arraytype, domain;
2549 if (TREE_CONSTANT (nelts))
2550 domain = compute_array_index_type (NULL_TREE, nelts,
2551 complain);
2552 else
2553 {
2554 domain = NULL_TREE;
2555 if (CONSTRUCTOR_NELTS (vecinit) > 0)
2556 warning (0, "non-constant array size in new, unable "
2557 "to verify length of initializer-list");
2558 }
2559 arraytype = build_cplus_array_type (type, domain);
2560 vecinit = digest_init (arraytype, vecinit, complain);
2561 }
2562 }
2563 else if (*init)
2564 {
2565 if (complain & tf_error)
2566 permerror (input_location,
2567 "parenthesized initializer in array new");
2568 else
2569 return error_mark_node;
2570 vecinit = build_tree_list_vec (*init);
2571 }
2572 init_expr
2573 = build_vec_init (data_addr,
2574 cp_build_binary_op (input_location,
2575 MINUS_EXPR, outer_nelts,
2576 integer_one_node,
2577 complain),
2578 vecinit,
2579 explicit_value_init_p,
2580 /*from_array=*/0,
2581 complain);
2582
2583 /* An array initialization is stable because the initialization
2584 of each element is a full-expression, so the temporaries don't
2585 leak out. */
2586 stable = true;
2587 }
2588 else
2589 {
2590 init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2591
2592 if (type_build_ctor_call (type) && !explicit_value_init_p)
2593 {
2594 init_expr = build_special_member_call (init_expr,
2595 complete_ctor_identifier,
2596 init, elt_type,
2597 LOOKUP_NORMAL,
2598 complain);
2599 }
2600 else if (explicit_value_init_p)
2601 {
2602 /* Something like `new int()'. */
2603 tree val = build_value_init (type, complain);
2604 if (val == error_mark_node)
2605 return error_mark_node;
2606 init_expr = build2 (INIT_EXPR, type, init_expr, val);
2607 }
2608 else
2609 {
2610 tree ie;
2611
2612 /* We are processing something like `new int (10)', which
2613 means allocate an int, and initialize it with 10. */
2614
2615 ie = build_x_compound_expr_from_vec (*init, "new initializer");
2616 init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie,
2617 complain);
2618 }
2619 stable = stabilize_init (init_expr, &init_preeval_expr);
2620 }
2621
2622 if (init_expr == error_mark_node)
2623 return error_mark_node;
2624
2625 /* If any part of the object initialization terminates by throwing an
2626 exception and a suitable deallocation function can be found, the
2627 deallocation function is called to free the memory in which the
2628 object was being constructed, after which the exception continues
2629 to propagate in the context of the new-expression. If no
2630 unambiguous matching deallocation function can be found,
2631 propagating the exception does not cause the object's memory to be
2632 freed. */
2633 if (flag_exceptions && ! use_java_new)
2634 {
2635 enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
2636 tree cleanup;
2637
2638 /* The Standard is unclear here, but the right thing to do
2639 is to use the same method for finding deallocation
2640 functions that we use for finding allocation functions. */
2641 cleanup = (build_op_delete_call
2642 (dcode,
2643 alloc_node,
2644 size,
2645 globally_qualified_p,
2646 placement_allocation_fn_p ? alloc_call : NULL_TREE,
2647 alloc_fn));
2648
2649 if (!cleanup)
2650 /* We're done. */;
2651 else if (stable)
2652 /* This is much simpler if we were able to preevaluate all of
2653 the arguments to the constructor call. */
2654 {
2655 /* CLEANUP is compiler-generated, so no diagnostics. */
2656 TREE_NO_WARNING (cleanup) = true;
2657 init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
2658 init_expr, cleanup);
2659 /* Likewise, this try-catch is compiler-generated. */
2660 TREE_NO_WARNING (init_expr) = true;
2661 }
2662 else
2663 /* Ack! First we allocate the memory. Then we set our sentry
2664 variable to true, and expand a cleanup that deletes the
2665 memory if sentry is true. Then we run the constructor, and
2666 finally clear the sentry.
2667
2668 We need to do this because we allocate the space first, so
2669 if there are any temporaries with cleanups in the
2670 constructor args and we weren't able to preevaluate them, we
2671 need this EH region to extend until end of full-expression
2672 to preserve nesting. */
2673 {
2674 tree end, sentry, begin;
2675
2676 begin = get_target_expr (boolean_true_node);
2677 CLEANUP_EH_ONLY (begin) = 1;
2678
2679 sentry = TARGET_EXPR_SLOT (begin);
2680
2681 /* CLEANUP is compiler-generated, so no diagnostics. */
2682 TREE_NO_WARNING (cleanup) = true;
2683
2684 TARGET_EXPR_CLEANUP (begin)
2685 = build3 (COND_EXPR, void_type_node, sentry,
2686 cleanup, void_zero_node);
2687
2688 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2689 sentry, boolean_false_node);
2690
2691 init_expr
2692 = build2 (COMPOUND_EXPR, void_type_node, begin,
2693 build2 (COMPOUND_EXPR, void_type_node, init_expr,
2694 end));
2695 /* Likewise, this is compiler-generated. */
2696 TREE_NO_WARNING (init_expr) = true;
2697 }
2698 }
2699 }
2700 else
2701 init_expr = NULL_TREE;
2702
2703 /* Now build up the return value in reverse order. */
2704
2705 rval = data_addr;
2706
2707 if (init_expr)
2708 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2709 if (cookie_expr)
2710 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2711
2712 if (rval == data_addr)
2713 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2714 and return the call (which doesn't need to be adjusted). */
2715 rval = TARGET_EXPR_INITIAL (alloc_expr);
2716 else
2717 {
2718 if (check_new)
2719 {
2720 tree ifexp = cp_build_binary_op (input_location,
2721 NE_EXPR, alloc_node,
2722 nullptr_node,
2723 complain);
2724 rval = build_conditional_expr (ifexp, rval, alloc_node,
2725 complain);
2726 }
2727
2728 /* Perform the allocation before anything else, so that ALLOC_NODE
2729 has been initialized before we start using it. */
2730 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2731 }
2732
2733 if (init_preeval_expr)
2734 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
2735
2736 /* A new-expression is never an lvalue. */
2737 gcc_assert (!lvalue_p (rval));
2738
2739 return convert (pointer_type, rval);
2740 }
2741
2742 /* Generate a representation for a C++ "new" expression. *PLACEMENT
2743 is a vector of placement-new arguments (or NULL if none). If NELTS
2744 is NULL, TYPE is the type of the storage to be allocated. If NELTS
2745 is not NULL, then this is an array-new allocation; TYPE is the type
2746 of the elements in the array and NELTS is the number of elements in
2747 the array. *INIT, if non-NULL, is the initializer for the new
2748 object, or an empty vector to indicate an initializer of "()". If
2749 USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
2750 rather than just "new". This may change PLACEMENT and INIT. */
2751
2752 tree
2753 build_new (VEC(tree,gc) **placement, tree type, tree nelts,
2754 VEC(tree,gc) **init, int use_global_new, tsubst_flags_t complain)
2755 {
2756 tree rval;
2757 VEC(tree,gc) *orig_placement = NULL;
2758 tree orig_nelts = NULL_TREE;
2759 VEC(tree,gc) *orig_init = NULL;
2760
2761 if (type == error_mark_node)
2762 return error_mark_node;
2763
2764 if (nelts == NULL_TREE && VEC_length (tree, *init) == 1)
2765 {
2766 tree auto_node = type_uses_auto (type);
2767 if (auto_node)
2768 {
2769 tree d_init = VEC_index (tree, *init, 0);
2770 d_init = resolve_nondeduced_context (d_init);
2771 type = do_auto_deduction (type, d_init, auto_node);
2772 }
2773 }
2774
2775 if (processing_template_decl)
2776 {
2777 if (dependent_type_p (type)
2778 || any_type_dependent_arguments_p (*placement)
2779 || (nelts && type_dependent_expression_p (nelts))
2780 || any_type_dependent_arguments_p (*init))
2781 return build_raw_new_expr (*placement, type, nelts, *init,
2782 use_global_new);
2783
2784 orig_placement = make_tree_vector_copy (*placement);
2785 orig_nelts = nelts;
2786 orig_init = make_tree_vector_copy (*init);
2787
2788 make_args_non_dependent (*placement);
2789 if (nelts)
2790 nelts = build_non_dependent_expr (nelts);
2791 make_args_non_dependent (*init);
2792 }
2793
2794 if (nelts)
2795 {
2796 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
2797 {
2798 if (complain & tf_error)
2799 permerror (input_location, "size in array new must have integral type");
2800 else
2801 return error_mark_node;
2802 }
2803 nelts = mark_rvalue_use (nelts);
2804 nelts = cp_save_expr (cp_convert (sizetype, nelts));
2805 }
2806
2807 /* ``A reference cannot be created by the new operator. A reference
2808 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2809 returned by new.'' ARM 5.3.3 */
2810 if (TREE_CODE (type) == REFERENCE_TYPE)
2811 {
2812 if (complain & tf_error)
2813 error ("new cannot be applied to a reference type");
2814 else
2815 return error_mark_node;
2816 type = TREE_TYPE (type);
2817 }
2818
2819 if (TREE_CODE (type) == FUNCTION_TYPE)
2820 {
2821 if (complain & tf_error)
2822 error ("new cannot be applied to a function type");
2823 return error_mark_node;
2824 }
2825
2826 /* The type allocated must be complete. If the new-type-id was
2827 "T[N]" then we are just checking that "T" is complete here, but
2828 that is equivalent, since the value of "N" doesn't matter. */
2829 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2830 return error_mark_node;
2831
2832 rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
2833 if (rval == error_mark_node)
2834 return error_mark_node;
2835
2836 if (processing_template_decl)
2837 {
2838 tree ret = build_raw_new_expr (orig_placement, type, orig_nelts,
2839 orig_init, use_global_new);
2840 release_tree_vector (orig_placement);
2841 release_tree_vector (orig_init);
2842 return ret;
2843 }
2844
2845 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2846 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2847 TREE_NO_WARNING (rval) = 1;
2848
2849 return rval;
2850 }
2851
2852 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2853
2854 tree
2855 build_java_class_ref (tree type)
2856 {
2857 tree name = NULL_TREE, class_decl;
2858 static tree CL_suffix = NULL_TREE;
2859 if (CL_suffix == NULL_TREE)
2860 CL_suffix = get_identifier("class$");
2861 if (jclass_node == NULL_TREE)
2862 {
2863 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
2864 if (jclass_node == NULL_TREE)
2865 {
2866 error ("call to Java constructor, while %<jclass%> undefined");
2867 return error_mark_node;
2868 }
2869 jclass_node = TREE_TYPE (jclass_node);
2870 }
2871
2872 /* Mangle the class$ field. */
2873 {
2874 tree field;
2875 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2876 if (DECL_NAME (field) == CL_suffix)
2877 {
2878 mangle_decl (field);
2879 name = DECL_ASSEMBLER_NAME (field);
2880 break;
2881 }
2882 if (!field)
2883 {
2884 error ("can%'t find %<class$%> in %qT", type);
2885 return error_mark_node;
2886 }
2887 }
2888
2889 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2890 if (class_decl == NULL_TREE)
2891 {
2892 class_decl = build_decl (input_location,
2893 VAR_DECL, name, TREE_TYPE (jclass_node));
2894 TREE_STATIC (class_decl) = 1;
2895 DECL_EXTERNAL (class_decl) = 1;
2896 TREE_PUBLIC (class_decl) = 1;
2897 DECL_ARTIFICIAL (class_decl) = 1;
2898 DECL_IGNORED_P (class_decl) = 1;
2899 pushdecl_top_level (class_decl);
2900 make_decl_rtl (class_decl);
2901 }
2902 return class_decl;
2903 }
2904 \f
2905 static tree
2906 build_vec_delete_1 (tree base, tree maxindex, tree type,
2907 special_function_kind auto_delete_vec,
2908 int use_global_delete, tsubst_flags_t complain)
2909 {
2910 tree virtual_size;
2911 tree ptype = build_pointer_type (type = complete_type (type));
2912 tree size_exp = size_in_bytes (type);
2913
2914 /* Temporary variables used by the loop. */
2915 tree tbase, tbase_init;
2916
2917 /* This is the body of the loop that implements the deletion of a
2918 single element, and moves temp variables to next elements. */
2919 tree body;
2920
2921 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2922 tree loop = 0;
2923
2924 /* This is the thing that governs what to do after the loop has run. */
2925 tree deallocate_expr = 0;
2926
2927 /* This is the BIND_EXPR which holds the outermost iterator of the
2928 loop. It is convenient to set this variable up and test it before
2929 executing any other code in the loop.
2930 This is also the containing expression returned by this function. */
2931 tree controller = NULL_TREE;
2932 tree tmp;
2933
2934 /* We should only have 1-D arrays here. */
2935 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
2936
2937 if (base == error_mark_node || maxindex == error_mark_node)
2938 return error_mark_node;
2939
2940 if (! MAYBE_CLASS_TYPE_P (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2941 goto no_destructor;
2942
2943 /* The below is short by the cookie size. */
2944 virtual_size = size_binop (MULT_EXPR, size_exp,
2945 convert (sizetype, maxindex));
2946
2947 tbase = create_temporary_var (ptype);
2948 tbase_init
2949 = cp_build_modify_expr (tbase, NOP_EXPR,
2950 fold_build_pointer_plus_loc (input_location,
2951 fold_convert (ptype,
2952 base),
2953 virtual_size),
2954 complain);
2955 if (tbase_init == error_mark_node)
2956 return error_mark_node;
2957 controller = build3 (BIND_EXPR, void_type_node, tbase,
2958 NULL_TREE, NULL_TREE);
2959 TREE_SIDE_EFFECTS (controller) = 1;
2960
2961 body = build1 (EXIT_EXPR, void_type_node,
2962 build2 (EQ_EXPR, boolean_type_node, tbase,
2963 fold_convert (ptype, base)));
2964 tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp);
2965 tmp = fold_build_pointer_plus (tbase, tmp);
2966 tmp = cp_build_modify_expr (tbase, NOP_EXPR, tmp, complain);
2967 if (tmp == error_mark_node)
2968 return error_mark_node;
2969 body = build_compound_expr (input_location, body, tmp);
2970 tmp = build_delete (ptype, tbase, sfk_complete_destructor,
2971 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
2972 complain);
2973 if (tmp == error_mark_node)
2974 return error_mark_node;
2975 body = build_compound_expr (input_location, body, tmp);
2976
2977 loop = build1 (LOOP_EXPR, void_type_node, body);
2978 loop = build_compound_expr (input_location, tbase_init, loop);
2979
2980 no_destructor:
2981 /* Delete the storage if appropriate. */
2982 if (auto_delete_vec == sfk_deleting_destructor)
2983 {
2984 tree base_tbd;
2985
2986 /* The below is short by the cookie size. */
2987 virtual_size = size_binop (MULT_EXPR, size_exp,
2988 convert (sizetype, maxindex));
2989
2990 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2991 /* no header */
2992 base_tbd = base;
2993 else
2994 {
2995 tree cookie_size;
2996
2997 cookie_size = targetm.cxx.get_cookie_size (type);
2998 base_tbd = cp_build_binary_op (input_location,
2999 MINUS_EXPR,
3000 cp_convert (string_type_node,
3001 base),
3002 cookie_size,
3003 complain);
3004 if (base_tbd == error_mark_node)
3005 return error_mark_node;
3006 base_tbd = cp_convert (ptype, base_tbd);
3007 /* True size with header. */
3008 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
3009 }
3010
3011 deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
3012 base_tbd, virtual_size,
3013 use_global_delete & 1,
3014 /*placement=*/NULL_TREE,
3015 /*alloc_fn=*/NULL_TREE);
3016 }
3017
3018 body = loop;
3019 if (!deallocate_expr)
3020 ;
3021 else if (!body)
3022 body = deallocate_expr;
3023 else
3024 body = build_compound_expr (input_location, body, deallocate_expr);
3025
3026 if (!body)
3027 body = integer_zero_node;
3028
3029 /* Outermost wrapper: If pointer is null, punt. */
3030 body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3031 fold_build2_loc (input_location,
3032 NE_EXPR, boolean_type_node, base,
3033 convert (TREE_TYPE (base),
3034 nullptr_node)),
3035 body, integer_zero_node);
3036 body = build1 (NOP_EXPR, void_type_node, body);
3037
3038 if (controller)
3039 {
3040 TREE_OPERAND (controller, 1) = body;
3041 body = controller;
3042 }
3043
3044 if (TREE_CODE (base) == SAVE_EXPR)
3045 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
3046 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
3047
3048 return convert_to_void (body, ICV_CAST, complain);
3049 }
3050
3051 /* Create an unnamed variable of the indicated TYPE. */
3052
3053 tree
3054 create_temporary_var (tree type)
3055 {
3056 tree decl;
3057
3058 decl = build_decl (input_location,
3059 VAR_DECL, NULL_TREE, type);
3060 TREE_USED (decl) = 1;
3061 DECL_ARTIFICIAL (decl) = 1;
3062 DECL_IGNORED_P (decl) = 1;
3063 DECL_CONTEXT (decl) = current_function_decl;
3064
3065 return decl;
3066 }
3067
3068 /* Create a new temporary variable of the indicated TYPE, initialized
3069 to INIT.
3070
3071 It is not entered into current_binding_level, because that breaks
3072 things when it comes time to do final cleanups (which take place
3073 "outside" the binding contour of the function). */
3074
3075 tree
3076 get_temp_regvar (tree type, tree init)
3077 {
3078 tree decl;
3079
3080 decl = create_temporary_var (type);
3081 add_decl_expr (decl);
3082
3083 finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
3084 tf_warning_or_error));
3085
3086 return decl;
3087 }
3088
3089 /* `build_vec_init' returns tree structure that performs
3090 initialization of a vector of aggregate types.
3091
3092 BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
3093 to the first element, of POINTER_TYPE.
3094 MAXINDEX is the maximum index of the array (one less than the
3095 number of elements). It is only used if BASE is a pointer or
3096 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
3097
3098 INIT is the (possibly NULL) initializer.
3099
3100 If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
3101 elements in the array are value-initialized.
3102
3103 FROM_ARRAY is 0 if we should init everything with INIT
3104 (i.e., every element initialized from INIT).
3105 FROM_ARRAY is 1 if we should index into INIT in parallel
3106 with initialization of DECL.
3107 FROM_ARRAY is 2 if we should index into INIT in parallel,
3108 but use assignment instead of initialization. */
3109
3110 tree
3111 build_vec_init (tree base, tree maxindex, tree init,
3112 bool explicit_value_init_p,
3113 int from_array, tsubst_flags_t complain)
3114 {
3115 tree rval;
3116 tree base2 = NULL_TREE;
3117 tree itype = NULL_TREE;
3118 tree iterator;
3119 /* The type of BASE. */
3120 tree atype = TREE_TYPE (base);
3121 /* The type of an element in the array. */
3122 tree type = TREE_TYPE (atype);
3123 /* The element type reached after removing all outer array
3124 types. */
3125 tree inner_elt_type;
3126 /* The type of a pointer to an element in the array. */
3127 tree ptype;
3128 tree stmt_expr;
3129 tree compound_stmt;
3130 int destroy_temps;
3131 tree try_block = NULL_TREE;
3132 int num_initialized_elts = 0;
3133 bool is_global;
3134 tree const_init = NULL_TREE;
3135 tree obase = base;
3136 bool xvalue = false;
3137 bool errors = false;
3138
3139 if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
3140 maxindex = array_type_nelts (atype);
3141
3142 if (maxindex == NULL_TREE || maxindex == error_mark_node
3143 || integer_all_onesp (maxindex))
3144 return error_mark_node;
3145
3146 if (explicit_value_init_p)
3147 gcc_assert (!init);
3148
3149 inner_elt_type = strip_array_types (type);
3150
3151 /* Look through the TARGET_EXPR around a compound literal. */
3152 if (init && TREE_CODE (init) == TARGET_EXPR
3153 && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
3154 && from_array != 2)
3155 init = TARGET_EXPR_INITIAL (init);
3156
3157 if (init
3158 && TREE_CODE (atype) == ARRAY_TYPE
3159 && (from_array == 2
3160 ? (!CLASS_TYPE_P (inner_elt_type)
3161 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (inner_elt_type))
3162 : !TYPE_NEEDS_CONSTRUCTING (type))
3163 && ((TREE_CODE (init) == CONSTRUCTOR
3164 /* Don't do this if the CONSTRUCTOR might contain something
3165 that might throw and require us to clean up. */
3166 && (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))
3167 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
3168 || from_array))
3169 {
3170 /* Do non-default initialization of trivial arrays resulting from
3171 brace-enclosed initializers. In this case, digest_init and
3172 store_constructor will handle the semantics for us. */
3173
3174 stmt_expr = build2 (INIT_EXPR, atype, base, init);
3175 return stmt_expr;
3176 }
3177
3178 maxindex = cp_convert (ptrdiff_type_node, maxindex);
3179 if (TREE_CODE (atype) == ARRAY_TYPE)
3180 {
3181 ptype = build_pointer_type (type);
3182 base = cp_convert (ptype, decay_conversion (base));
3183 }
3184 else
3185 ptype = atype;
3186
3187 /* The code we are generating looks like:
3188 ({
3189 T* t1 = (T*) base;
3190 T* rval = t1;
3191 ptrdiff_t iterator = maxindex;
3192 try {
3193 for (; iterator != -1; --iterator) {
3194 ... initialize *t1 ...
3195 ++t1;
3196 }
3197 } catch (...) {
3198 ... destroy elements that were constructed ...
3199 }
3200 rval;
3201 })
3202
3203 We can omit the try and catch blocks if we know that the
3204 initialization will never throw an exception, or if the array
3205 elements do not have destructors. We can omit the loop completely if
3206 the elements of the array do not have constructors.
3207
3208 We actually wrap the entire body of the above in a STMT_EXPR, for
3209 tidiness.
3210
3211 When copying from array to another, when the array elements have
3212 only trivial copy constructors, we should use __builtin_memcpy
3213 rather than generating a loop. That way, we could take advantage
3214 of whatever cleverness the back end has for dealing with copies
3215 of blocks of memory. */
3216
3217 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
3218 destroy_temps = stmts_are_full_exprs_p ();
3219 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3220 rval = get_temp_regvar (ptype, base);
3221 base = get_temp_regvar (ptype, rval);
3222 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
3223
3224 /* If initializing one array from another, initialize element by
3225 element. We rely upon the below calls to do the argument
3226 checking. Evaluate the initializer before entering the try block. */
3227 if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
3228 {
3229 if (lvalue_kind (init) & clk_rvalueref)
3230 xvalue = true;
3231 base2 = decay_conversion (init);
3232 itype = TREE_TYPE (base2);
3233 base2 = get_temp_regvar (itype, base2);
3234 itype = TREE_TYPE (itype);
3235 }
3236
3237 /* Protect the entire array initialization so that we can destroy
3238 the partially constructed array if an exception is thrown.
3239 But don't do this if we're assigning. */
3240 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3241 && from_array != 2)
3242 {
3243 try_block = begin_try_block ();
3244 }
3245
3246 /* If the initializer is {}, then all elements are initialized from {}.
3247 But for non-classes, that's the same as value-initialization. */
3248 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
3249 && CONSTRUCTOR_NELTS (init) == 0)
3250 {
3251 if (CLASS_TYPE_P (type))
3252 /* Leave init alone. */;
3253 else
3254 {
3255 init = NULL_TREE;
3256 explicit_value_init_p = true;
3257 }
3258 }
3259
3260 /* Maybe pull out constant value when from_array? */
3261
3262 else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
3263 {
3264 /* Do non-default initialization of non-trivial arrays resulting from
3265 brace-enclosed initializers. */
3266 unsigned HOST_WIDE_INT idx;
3267 tree field, elt;
3268 /* Should we try to create a constant initializer? */
3269 bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
3270 && (literal_type_p (inner_elt_type)
3271 || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
3272 /* If the constructor already has the array type, it's been through
3273 digest_init, so we shouldn't try to do anything more. */
3274 bool digested = same_type_p (atype, TREE_TYPE (init));
3275 bool saw_non_const = false;
3276 bool saw_const = false;
3277 /* If we're initializing a static array, we want to do static
3278 initialization of any elements with constant initializers even if
3279 some are non-constant. */
3280 bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
3281 VEC(constructor_elt,gc) *new_vec;
3282 from_array = 0;
3283
3284 if (try_const)
3285 new_vec = VEC_alloc (constructor_elt, gc, CONSTRUCTOR_NELTS (init));
3286 else
3287 new_vec = NULL;
3288
3289 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
3290 {
3291 tree baseref = build1 (INDIRECT_REF, type, base);
3292 tree one_init;
3293
3294 num_initialized_elts++;
3295
3296 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3297 if (digested)
3298 one_init = build2 (INIT_EXPR, type, baseref, elt);
3299 else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3300 one_init = build_aggr_init (baseref, elt, 0, complain);
3301 else
3302 one_init = cp_build_modify_expr (baseref, NOP_EXPR,
3303 elt, complain);
3304 if (one_init == error_mark_node)
3305 errors = true;
3306 if (try_const)
3307 {
3308 tree e = one_init;
3309 if (TREE_CODE (e) == EXPR_STMT)
3310 e = TREE_OPERAND (e, 0);
3311 if (TREE_CODE (e) == CONVERT_EXPR
3312 && VOID_TYPE_P (TREE_TYPE (e)))
3313 e = TREE_OPERAND (e, 0);
3314 e = maybe_constant_init (e);
3315 if (reduced_constant_expression_p (e))
3316 {
3317 CONSTRUCTOR_APPEND_ELT (new_vec, field, e);
3318 if (do_static_init)
3319 one_init = NULL_TREE;
3320 else
3321 one_init = build2 (INIT_EXPR, type, baseref, e);
3322 saw_const = true;
3323 }
3324 else
3325 {
3326 if (do_static_init)
3327 CONSTRUCTOR_APPEND_ELT (new_vec, field,
3328 build_zero_init (TREE_TYPE (e),
3329 NULL_TREE, true));
3330 saw_non_const = true;
3331 }
3332 }
3333
3334 if (one_init)
3335 finish_expr_stmt (one_init);
3336 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3337
3338 one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, 0, complain);
3339 if (one_init == error_mark_node)
3340 errors = true;
3341 else
3342 finish_expr_stmt (one_init);
3343
3344 one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3345 complain);
3346 if (one_init == error_mark_node)
3347 errors = true;
3348 else
3349 finish_expr_stmt (one_init);
3350 }
3351
3352 if (try_const)
3353 {
3354 if (!saw_non_const)
3355 const_init = build_constructor (atype, new_vec);
3356 else if (do_static_init && saw_const)
3357 DECL_INITIAL (obase) = build_constructor (atype, new_vec);
3358 else
3359 VEC_free (constructor_elt, gc, new_vec);
3360 }
3361
3362 /* Clear out INIT so that we don't get confused below. */
3363 init = NULL_TREE;
3364 }
3365 else if (from_array)
3366 {
3367 if (init)
3368 /* OK, we set base2 above. */;
3369 else if (CLASS_TYPE_P (type)
3370 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3371 {
3372 if (complain & tf_error)
3373 error ("initializer ends prematurely");
3374 errors = true;
3375 }
3376 }
3377
3378 /* Now, default-initialize any remaining elements. We don't need to
3379 do that if a) the type does not need constructing, or b) we've
3380 already initialized all the elements.
3381
3382 We do need to keep going if we're copying an array. */
3383
3384 if (from_array
3385 || ((type_build_ctor_call (type) || init || explicit_value_init_p)
3386 && ! (host_integerp (maxindex, 0)
3387 && (num_initialized_elts
3388 == tree_low_cst (maxindex, 0) + 1))))
3389 {
3390 /* If the ITERATOR is equal to -1, then we don't have to loop;
3391 we've already initialized all the elements. */
3392 tree for_stmt;
3393 tree elt_init;
3394 tree to;
3395
3396 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
3397 finish_for_init_stmt (for_stmt);
3398 finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
3399 build_int_cst (TREE_TYPE (iterator), -1)),
3400 for_stmt);
3401 elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
3402 complain);
3403 if (elt_init == error_mark_node)
3404 errors = true;
3405 finish_for_expr (elt_init, for_stmt);
3406
3407 to = build1 (INDIRECT_REF, type, base);
3408
3409 if (from_array)
3410 {
3411 tree from;
3412
3413 if (base2)
3414 {
3415 from = build1 (INDIRECT_REF, itype, base2);
3416 if (xvalue)
3417 from = move (from);
3418 }
3419 else
3420 from = NULL_TREE;
3421
3422 if (from_array == 2)
3423 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3424 complain);
3425 else if (type_build_ctor_call (type))
3426 elt_init = build_aggr_init (to, from, 0, complain);
3427 else if (from)
3428 elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
3429 complain);
3430 else
3431 gcc_unreachable ();
3432 }
3433 else if (TREE_CODE (type) == ARRAY_TYPE)
3434 {
3435 if (init != 0)
3436 sorry
3437 ("cannot initialize multi-dimensional array with initializer");
3438 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
3439 0, 0,
3440 explicit_value_init_p,
3441 0, complain);
3442 }
3443 else if (explicit_value_init_p)
3444 {
3445 elt_init = build_value_init (type, complain);
3446 if (elt_init != error_mark_node)
3447 elt_init = build2 (INIT_EXPR, type, to, elt_init);
3448 }
3449 else
3450 {
3451 gcc_assert (type_build_ctor_call (type) || init);
3452 if (CLASS_TYPE_P (type))
3453 elt_init = build_aggr_init (to, init, 0, complain);
3454 else
3455 {
3456 if (TREE_CODE (init) == TREE_LIST)
3457 init = build_x_compound_expr_from_list (init, ELK_INIT,
3458 complain);
3459 elt_init = build2 (INIT_EXPR, type, to, init);
3460 }
3461 }
3462
3463 if (elt_init == error_mark_node)
3464 errors = true;
3465
3466 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
3467 finish_expr_stmt (elt_init);
3468 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
3469
3470 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
3471 complain));
3472 if (base2)
3473 finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0,
3474 complain));
3475
3476 finish_for_stmt (for_stmt);
3477 }
3478
3479 /* Make sure to cleanup any partially constructed elements. */
3480 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3481 && from_array != 2)
3482 {
3483 tree e;
3484 tree m = cp_build_binary_op (input_location,
3485 MINUS_EXPR, maxindex, iterator,
3486 complain);
3487
3488 /* Flatten multi-dimensional array since build_vec_delete only
3489 expects one-dimensional array. */
3490 if (TREE_CODE (type) == ARRAY_TYPE)
3491 m = cp_build_binary_op (input_location,
3492 MULT_EXPR, m,
3493 array_type_nelts_total (type),
3494 complain);
3495
3496 finish_cleanup_try_block (try_block);
3497 e = build_vec_delete_1 (rval, m,
3498 inner_elt_type, sfk_complete_destructor,
3499 /*use_global_delete=*/0, complain);
3500 if (e == error_mark_node)
3501 errors = true;
3502 finish_cleanup (e, try_block);
3503 }
3504
3505 /* The value of the array initialization is the array itself, RVAL
3506 is a pointer to the first element. */
3507 finish_stmt_expr_expr (rval, stmt_expr);
3508
3509 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
3510
3511 /* Now make the result have the correct type. */
3512 if (TREE_CODE (atype) == ARRAY_TYPE)
3513 {
3514 atype = build_pointer_type (atype);
3515 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
3516 stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain);
3517 TREE_NO_WARNING (stmt_expr) = 1;
3518 }
3519
3520 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3521
3522 if (const_init)
3523 return build2 (INIT_EXPR, atype, obase, const_init);
3524 if (errors)
3525 return error_mark_node;
3526 return stmt_expr;
3527 }
3528
3529 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3530 build_delete. */
3531
3532 static tree
3533 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
3534 tsubst_flags_t complain)
3535 {
3536 tree name;
3537 tree fn;
3538 switch (dtor_kind)
3539 {
3540 case sfk_complete_destructor:
3541 name = complete_dtor_identifier;
3542 break;
3543
3544 case sfk_base_destructor:
3545 name = base_dtor_identifier;
3546 break;
3547
3548 case sfk_deleting_destructor:
3549 name = deleting_dtor_identifier;
3550 break;
3551
3552 default:
3553 gcc_unreachable ();
3554 }
3555 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
3556 return build_new_method_call (exp, fn,
3557 /*args=*/NULL,
3558 /*conversion_path=*/NULL_TREE,
3559 flags,
3560 /*fn_p=*/NULL,
3561 complain);
3562 }
3563
3564 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3565 ADDR is an expression which yields the store to be destroyed.
3566 AUTO_DELETE is the name of the destructor to call, i.e., either
3567 sfk_complete_destructor, sfk_base_destructor, or
3568 sfk_deleting_destructor.
3569
3570 FLAGS is the logical disjunction of zero or more LOOKUP_
3571 flags. See cp-tree.h for more info. */
3572
3573 tree
3574 build_delete (tree type, tree addr, special_function_kind auto_delete,
3575 int flags, int use_global_delete, tsubst_flags_t complain)
3576 {
3577 tree expr;
3578
3579 if (addr == error_mark_node)
3580 return error_mark_node;
3581
3582 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3583 set to `error_mark_node' before it gets properly cleaned up. */
3584 if (type == error_mark_node)
3585 return error_mark_node;
3586
3587 type = TYPE_MAIN_VARIANT (type);
3588
3589 addr = mark_rvalue_use (addr);
3590
3591 if (TREE_CODE (type) == POINTER_TYPE)
3592 {
3593 bool complete_p = true;
3594
3595 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3596 if (TREE_CODE (type) == ARRAY_TYPE)
3597 goto handle_array;
3598
3599 /* We don't want to warn about delete of void*, only other
3600 incomplete types. Deleting other incomplete types
3601 invokes undefined behavior, but it is not ill-formed, so
3602 compile to something that would even do The Right Thing
3603 (TM) should the type have a trivial dtor and no delete
3604 operator. */
3605 if (!VOID_TYPE_P (type))
3606 {
3607 complete_type (type);
3608 if (!COMPLETE_TYPE_P (type))
3609 {
3610 if ((complain & tf_warning)
3611 && warning (0, "possible problem detected in invocation of "
3612 "delete operator:"))
3613 {
3614 cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
3615 inform (input_location, "neither the destructor nor the class-specific "
3616 "operator delete will be called, even if they are "
3617 "declared when the class is defined");
3618 }
3619 complete_p = false;
3620 }
3621 else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor
3622 && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
3623 && TYPE_POLYMORPHIC_P (type))
3624 {
3625 tree dtor;
3626 dtor = CLASSTYPE_DESTRUCTORS (type);
3627 if (!dtor || !DECL_VINDEX (dtor))
3628 {
3629 if (CLASSTYPE_PURE_VIRTUALS (type))
3630 warning (OPT_Wdelete_non_virtual_dtor,
3631 "deleting object of abstract class type %qT"
3632 " which has non-virtual destructor"
3633 " will cause undefined behaviour", type);
3634 else
3635 warning (OPT_Wdelete_non_virtual_dtor,
3636 "deleting object of polymorphic class type %qT"
3637 " which has non-virtual destructor"
3638 " might cause undefined behaviour", type);
3639 }
3640 }
3641 }
3642 if (VOID_TYPE_P (type) || !complete_p || !MAYBE_CLASS_TYPE_P (type))
3643 /* Call the builtin operator delete. */
3644 return build_builtin_delete_call (addr);
3645 if (TREE_SIDE_EFFECTS (addr))
3646 addr = save_expr (addr);
3647
3648 /* Throw away const and volatile on target type of addr. */
3649 addr = convert_force (build_pointer_type (type), addr, 0);
3650 }
3651 else if (TREE_CODE (type) == ARRAY_TYPE)
3652 {
3653 handle_array:
3654
3655 if (TYPE_DOMAIN (type) == NULL_TREE)
3656 {
3657 if (complain & tf_error)
3658 error ("unknown array size in delete");
3659 return error_mark_node;
3660 }
3661 return build_vec_delete (addr, array_type_nelts (type),
3662 auto_delete, use_global_delete, complain);
3663 }
3664 else
3665 {
3666 /* Don't check PROTECT here; leave that decision to the
3667 destructor. If the destructor is accessible, call it,
3668 else report error. */
3669 addr = cp_build_addr_expr (addr, complain);
3670 if (addr == error_mark_node)
3671 return error_mark_node;
3672 if (TREE_SIDE_EFFECTS (addr))
3673 addr = save_expr (addr);
3674
3675 addr = convert_force (build_pointer_type (type), addr, 0);
3676 }
3677
3678 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3679
3680 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3681 {
3682 if (auto_delete != sfk_deleting_destructor)
3683 return void_zero_node;
3684
3685 return build_op_delete_call (DELETE_EXPR, addr,
3686 cxx_sizeof_nowarn (type),
3687 use_global_delete,
3688 /*placement=*/NULL_TREE,
3689 /*alloc_fn=*/NULL_TREE);
3690 }
3691 else
3692 {
3693 tree head = NULL_TREE;
3694 tree do_delete = NULL_TREE;
3695 tree ifexp;
3696
3697 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
3698 lazily_declare_fn (sfk_destructor, type);
3699
3700 /* For `::delete x', we must not use the deleting destructor
3701 since then we would not be sure to get the global `operator
3702 delete'. */
3703 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3704 {
3705 /* We will use ADDR multiple times so we must save it. */
3706 addr = save_expr (addr);
3707 head = get_target_expr (build_headof (addr));
3708 /* Delete the object. */
3709 do_delete = build_builtin_delete_call (head);
3710 /* Otherwise, treat this like a complete object destructor
3711 call. */
3712 auto_delete = sfk_complete_destructor;
3713 }
3714 /* If the destructor is non-virtual, there is no deleting
3715 variant. Instead, we must explicitly call the appropriate
3716 `operator delete' here. */
3717 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3718 && auto_delete == sfk_deleting_destructor)
3719 {
3720 /* We will use ADDR multiple times so we must save it. */
3721 addr = save_expr (addr);
3722 /* Build the call. */
3723 do_delete = build_op_delete_call (DELETE_EXPR,
3724 addr,
3725 cxx_sizeof_nowarn (type),
3726 /*global_p=*/false,
3727 /*placement=*/NULL_TREE,
3728 /*alloc_fn=*/NULL_TREE);
3729 /* Call the complete object destructor. */
3730 auto_delete = sfk_complete_destructor;
3731 }
3732 else if (auto_delete == sfk_deleting_destructor
3733 && TYPE_GETS_REG_DELETE (type))
3734 {
3735 /* Make sure we have access to the member op delete, even though
3736 we'll actually be calling it from the destructor. */
3737 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
3738 /*global_p=*/false,
3739 /*placement=*/NULL_TREE,
3740 /*alloc_fn=*/NULL_TREE);
3741 }
3742
3743 expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, complain),
3744 auto_delete, flags, complain);
3745 if (expr == error_mark_node)
3746 return error_mark_node;
3747 if (do_delete)
3748 expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
3749
3750 /* We need to calculate this before the dtor changes the vptr. */
3751 if (head)
3752 expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
3753
3754 if (flags & LOOKUP_DESTRUCTOR)
3755 /* Explicit destructor call; don't check for null pointer. */
3756 ifexp = integer_one_node;
3757 else
3758 {
3759 /* Handle deleting a null pointer. */
3760 ifexp = fold (cp_build_binary_op (input_location,
3761 NE_EXPR, addr, nullptr_node,
3762 complain));
3763 if (ifexp == error_mark_node)
3764 return error_mark_node;
3765 }
3766
3767 if (ifexp != integer_one_node)
3768 expr = build3 (COND_EXPR, void_type_node,
3769 ifexp, expr, void_zero_node);
3770
3771 return expr;
3772 }
3773 }
3774
3775 /* At the beginning of a destructor, push cleanups that will call the
3776 destructors for our base classes and members.
3777
3778 Called from begin_destructor_body. */
3779
3780 void
3781 push_base_cleanups (void)
3782 {
3783 tree binfo, base_binfo;
3784 int i;
3785 tree member;
3786 tree expr;
3787 VEC(tree,gc) *vbases;
3788
3789 /* Run destructors for all virtual baseclasses. */
3790 if (CLASSTYPE_VBASECLASSES (current_class_type))
3791 {
3792 tree cond = (condition_conversion
3793 (build2 (BIT_AND_EXPR, integer_type_node,
3794 current_in_charge_parm,
3795 integer_two_node)));
3796
3797 /* The CLASSTYPE_VBASECLASSES vector is in initialization
3798 order, which is also the right order for pushing cleanups. */
3799 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
3800 VEC_iterate (tree, vbases, i, base_binfo); i++)
3801 {
3802 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3803 {
3804 expr = build_special_member_call (current_class_ref,
3805 base_dtor_identifier,
3806 NULL,
3807 base_binfo,
3808 (LOOKUP_NORMAL
3809 | LOOKUP_NONVIRTUAL),
3810 tf_warning_or_error);
3811 expr = build3 (COND_EXPR, void_type_node, cond,
3812 expr, void_zero_node);
3813 finish_decl_cleanup (NULL_TREE, expr);
3814 }
3815 }
3816 }
3817
3818 /* Take care of the remaining baseclasses. */
3819 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3820 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3821 {
3822 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3823 || BINFO_VIRTUAL_P (base_binfo))
3824 continue;
3825
3826 expr = build_special_member_call (current_class_ref,
3827 base_dtor_identifier,
3828 NULL, base_binfo,
3829 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
3830 tf_warning_or_error);
3831 finish_decl_cleanup (NULL_TREE, expr);
3832 }
3833
3834 /* Don't automatically destroy union members. */
3835 if (TREE_CODE (current_class_type) == UNION_TYPE)
3836 return;
3837
3838 for (member = TYPE_FIELDS (current_class_type); member;
3839 member = DECL_CHAIN (member))
3840 {
3841 tree this_type = TREE_TYPE (member);
3842 if (this_type == error_mark_node
3843 || TREE_CODE (member) != FIELD_DECL
3844 || DECL_ARTIFICIAL (member))
3845 continue;
3846 if (ANON_UNION_TYPE_P (this_type))
3847 continue;
3848 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
3849 {
3850 tree this_member = (build_class_member_access_expr
3851 (current_class_ref, member,
3852 /*access_path=*/NULL_TREE,
3853 /*preserve_reference=*/false,
3854 tf_warning_or_error));
3855 expr = build_delete (this_type, this_member,
3856 sfk_complete_destructor,
3857 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
3858 0, tf_warning_or_error);
3859 finish_decl_cleanup (NULL_TREE, expr);
3860 }
3861 }
3862 }
3863
3864 /* Build a C++ vector delete expression.
3865 MAXINDEX is the number of elements to be deleted.
3866 ELT_SIZE is the nominal size of each element in the vector.
3867 BASE is the expression that should yield the store to be deleted.
3868 This function expands (or synthesizes) these calls itself.
3869 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3870
3871 This also calls delete for virtual baseclasses of elements of the vector.
3872
3873 Update: MAXINDEX is no longer needed. The size can be extracted from the
3874 start of the vector for pointers, and from the type for arrays. We still
3875 use MAXINDEX for arrays because it happens to already have one of the
3876 values we'd have to extract. (We could use MAXINDEX with pointers to
3877 confirm the size, and trap if the numbers differ; not clear that it'd
3878 be worth bothering.) */
3879
3880 tree
3881 build_vec_delete (tree base, tree maxindex,
3882 special_function_kind auto_delete_vec,
3883 int use_global_delete, tsubst_flags_t complain)
3884 {
3885 tree type;
3886 tree rval;
3887 tree base_init = NULL_TREE;
3888
3889 type = TREE_TYPE (base);
3890
3891 if (TREE_CODE (type) == POINTER_TYPE)
3892 {
3893 /* Step back one from start of vector, and read dimension. */
3894 tree cookie_addr;
3895 tree size_ptr_type = build_pointer_type (sizetype);
3896
3897 if (TREE_SIDE_EFFECTS (base))
3898 {
3899 base_init = get_target_expr (base);
3900 base = TARGET_EXPR_SLOT (base_init);
3901 }
3902 type = strip_array_types (TREE_TYPE (type));
3903 cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
3904 sizetype, TYPE_SIZE_UNIT (sizetype));
3905 cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
3906 cookie_addr);
3907 maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, complain);
3908 }
3909 else if (TREE_CODE (type) == ARRAY_TYPE)
3910 {
3911 /* Get the total number of things in the array, maxindex is a
3912 bad name. */
3913 maxindex = array_type_nelts_total (type);
3914 type = strip_array_types (type);
3915 base = cp_build_addr_expr (base, complain);
3916 if (base == error_mark_node)
3917 return error_mark_node;
3918 if (TREE_SIDE_EFFECTS (base))
3919 {
3920 base_init = get_target_expr (base);
3921 base = TARGET_EXPR_SLOT (base_init);
3922 }
3923 }
3924 else
3925 {
3926 if (base != error_mark_node && !(complain & tf_error))
3927 error ("type to vector delete is neither pointer or array type");
3928 return error_mark_node;
3929 }
3930
3931 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3932 use_global_delete, complain);
3933 if (base_init && rval != error_mark_node)
3934 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
3935
3936 return rval;
3937 }