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