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