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