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