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