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