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