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