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