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